﻿var Community = (function()
{
	var community = {};
	
	var disableFirstNameValidationVariableName = '';
    var disableLastNameValidationVariableName = '';
    var disableEmailValidationVariableName = '';
    var disablePasswordValidationVariableName = '';
    var disableProfessionValidationVariableName = '';
    var hlShowInLineRegistrationFormClientID = '';
    var imgBtnSubmitClientID = '';
    var validationGroupName = '';
    var validationAnchorName = '';
	
    function isIE()
    {
        var isIE = false;

        if (navigator.appName.toLowerCase().indexOf('internet explorer') >= 0)
        {
            isIE = true;
        }

        return isIE;
    }
    
    community.OnLoginSuccessful = function()
    {
		if ((window.Users) &&
			(typeof(Users.DisableValidation) == 'function'))
		{
			Users.DisableValidation(
				validationGroupName);
		}
		
		community.ToggleInLineRegistrationForm(
			false);
    };

    community.SetShowInLineRegistrationFormHyperLinkClientID = function(
        hlShowInLineRegistrationFormClientIDLocal)
    {
        hlShowInLineRegistrationFormClientID = hlShowInLineRegistrationFormClientIDLocal;
    };

    community.SetSubmitImageButtonClientID = function(
        imgBtnSubmitClientIDLocal)
    {
        imgBtnSubmitClientID = imgBtnSubmitClientIDLocal;
    };
    
    community.ToggleInLineRegistrationForm = function(
	    show,
	    showErrorMessage)
    {
        var inLineRegistrationForm = document.getElementById(
			'cmnty_register');

        if (typeof (showErrorMessage) == 'undefined')
        {
            showErrorMessage = false;
        }

        if (inLineRegistrationForm != null)
        {
            if (typeof (show) != 'undefined')
            {
                if (show)
                {
                    inLineRegistrationForm.style.display = '';
                }
                else
                {
                    inLineRegistrationForm.style.display = 'none';
                }
            }
            else
            {
                if (inLineRegistrationForm.style.display == '')
                {
                    inLineRegistrationForm.style.display = 'none';
                }
                else
                {
                    inLineRegistrationForm.style.display = '';
                }
            }
        }

        if (hlShowInLineRegistrationFormClientID != null)
        {
            var hlShowInLineRegistrationForm = document.getElementById(
                hlShowInLineRegistrationFormClientID);

            if (hlShowInLineRegistrationForm != null)
            {
                hlShowInLineRegistrationForm.style.display = 'none';
            }
        }

        if (imgBtnSubmitClientID != null)
        {
            var imgBtnSubmit = document.getElementById(
                imgBtnSubmitClientID);

            if (imgBtnSubmit != null)
            {
                imgBtnSubmit.style.display = '';
            }
        }

        if (showErrorMessage)
        {
            var errorMessage = 'In order to submit your question you must be registered and logged in.  Please see the form below.';

            var addErrorMessageFunctionCall = (
				'if ((typeof(Validation) != "undefined") && (typeof(Validation.Add' +
				validationGroupName +
				'ErrorMessage) == "function")) Validation.Add' +
				validationGroupName +
				'ErrorMessage(errorMessage);');

            eval(addErrorMessageFunctionCall);
            
            window.top.location = (
				'#' +
				validationAnchorName);
        }
    };
    
    community.GetCountiesByRegion = function(
		loadingMessageClientID,
		countyCodeListClientID)
    {
        var countyCodeList = document.getElementById(
			countyCodeListClientID);

        if ((this != null) &&
			(countyCodeList != null) &&
			(typeof(GetCountiesByRegions) == 'function') &&
			(this.selectedIndex > 0))
        {
            GetCountiesByRegions(
		        loadingMessageClientID,
		        this,
		        countyCodeList,
		        true)
        }
        else if (countyCodeList != null)
        {
            countyCodeList.selectedIndex = 0;
            countyCodeList.disabled = true;
        }
    };
    
    community.ToggleOtherProfessionInput = function(
		divEnterOtherProfessionClientID,
		txtOtherProfessionClientID,
		disableOtherProfessionValidationVarName,
		otherProfessionID)
	{
		if ((this != null) &&
			(typeof(this.value != 'undefined')) &&
			(disableOtherProfessionValidationVarName != ''))
		{
			var disableOtherProfessionValidation = (
				 disableOtherProfessionValidationVarName +
				 ' = true;');
				 
			var enableOtherProfessionValidation = (
				disableOtherProfessionValidationVarName +
				' = false;');
				 
			var divEnterOtherProfession = document.getElementById(
				divEnterOtherProfessionClientID);
				
			var txtOtherProfession = document.getElementById(
				txtOtherProfessionClientID);
			
			if ((divEnterOtherProfession != null) &&
				(txtOtherProfession != null))
			{
				if (this.value == otherProfessionID)
				{
					divEnterOtherProfession.style.display = '';
					
					eval(
						enableOtherProfessionValidation);
				}
				else
				{
					divEnterOtherProfession.style.display = 'none';
					
					txtOtherProfession.value = '';
					
					eval(
						disableOtherProfessionValidation);
				}
			}
		}
	};
    
    community.InitFileUploadInputs = function(
		fileUploadInputClientIDs)
	{
		if ((typeof(fileUploadInputClientIDs) != 'undefined') &&
			(fileUploadInputClientIDs != null) &&
			(typeof(fileUploadInputClientIDs.length) != 'undefined') &&
			(fileUploadInputClientIDs.length > 0))
		{
			for (var index = 0; index < fileUploadInputClientIDs.length; index++)
			{
				var realFileUploadInput = document.getElementById(
					fileUploadInputClientIDs[index].Real);
					
				var fakeFileUploadInput = document.getElementById(
					fileUploadInputClientIDs[index].Fake);
					
				if ((realFileUploadInput != null) &&
					(fakeFileUploadInput != null))
				{
					realFileUploadInput.onchange = (
						function(element) {
							return function() 
							{ 
								element.value = this.value;
							};
						})(fakeFileUploadInput);
				}
			}
		}
	};
    
    community.InitializeSelectElements = function()
    {
        if (isIE())
        {
            var selects = document.getElementsByTagName(
                'select');

            if ((selects) &&
                (selects.length > 0))
            {
                for (var index = 0; index < selects.length; index++)
                {
                    initalizeSelectElement(
                        selects[index]);
                }
            }
        }
    };
    
    community.SetValidationGroupName = function(
		validationGroupNameLocal)
	{
		validationGroupName = validationGroupNameLocal;
	};
	
	community.SetValidationAnchorName = function(
		validationAnchorNameLocal)
	{
		validationAnchorName = validationAnchorNameLocal;
	};

    function initalizeSelectElement(
        selectElement)
    {
        if ((selectElement) &&
            (selectElement.options) &&
            (selectElement.options.length > 0))
        {
            for (var index = 0; index < selectElement.options.length; index++)
            {
                if (selectElement.options[index].disabled)
                {
                    var option = selectElement.options[index];

                    var optGroup = document.createElement(
                        'optgroup');

                    optGroup.label = option.innerHTML.replace(
                        '&amp;',
                        '&');

                    optGroup._option = option;

                    var parent = option.parentNode;

                    if (parent)
                    {
                        parent.replaceChild(
                            optGroup,
                            option);
                    }
                }
            }
        }
    }

    return community;
})();