//------------------------------------------------------------------
// Common functions

// Shows/hides form elements
function displaySection(i, val, visibleClass)
{
    var vClass = "";
    if (visibleClass != null) vClass = visibleClass;
  
    if (val == 1)
	{
    	document.getElementById(i).className = vClass;
	}
	if (val == 0)
	{
		document.getElementById(i).className = 'hidden';
	}
}

// Escapes HTML control characters (&, <, >)
function htmlEntities(s) 
{
    return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

// Updates the options in the specific 'range' drop-down list as identified by 'prefNum'
function updateRangeOptions(prefNum, rangeVal)
{
    // Range value param
    var rangeValue = "";
    if (rangeVal != null) rangeValue = rangeVal;
        
    // Fields
    var requirementField = document.getElementById("ddlPreferenceRequirement" + prefNum);
    var propertyTypeField = document.getElementById("ddlPreferencePropertyType" + prefNum);
    var rangeField = document.getElementById("ddlPreferenceRange" + prefNum);
    
    // Field values
    var rVal = (requirementField != null ? requirementField.value : "");
    var pVal = (propertyTypeField != null ? propertyTypeField.value : "");
    
    // Range option lists
    var rangePriceShort = ["$0 - $500,000","$500,000 - $5mil","$5mil - $10mil","$10mil - $50mil","$50mil +"];
    var rangePriceShortRes = ["< $350,000","$350,000 - $500,000","$500,000 - $750,000","$750,000 - $1mil","$1mil - $2mil","$2mil - $5mil","$5mil +"];
    var rangePriceLong = ["$0 - $500,000","$500,000 - $1mil","$1mil - $2mil","$2mil - $5mil","$5mil - $10mil","$10mil - $20mil","$20mil - $50mil","$50mil - $100mil","$100mil +"];
    var rangeAreaShort = ["0 - 250 sqm","250 - 500 sqm","500 - 1,000 sqm","1,000 - 5,000 sqm","5,000 - 10,000 sqm","10,000 sqm +"];
    var rangeAreaLong = ["0 - 100 sqm","100 - 250 sqm","250 - 500 sqm","500 - 1,000 sqm","1,000 - 2,000 sqm","2,000 - 3,000 sqm","3,000 - 4,000 sqm","4,000 - 5,000 sqm","5,000 - 10,000 sqm","10,000 sqm +"];
    var rangeRural = ["Cropping","Dairy","General","Grazing","Horticultural / Intensive","Lifestyle / Hobby","Mixed Farming","Pastoral","Poultry","Viticulture"];
    
    // Clear 'range' list if not already empty (ignoring default '' entry)
    if (rangeField.length > 1)
    {
        for(var i=rangeField.length-1; i>0; i--)
        {
            rangeField.remove(i);
        }
    }
    
    var optionList = [];
    
    // Business rules applied to define 'range' drop-down values
    if (rVal == "Buy" || rVal == "Sell" || rVal == "Valuation" || rVal == "Consultancy")
    {
        if (pVal == "Rural")
        {
            optionList = rangeRural;
        }
        else if (rVal == "Buy" || rVal == "Sell")
        {
            if (pVal == "Hotels" || pVal == "Healthcare & Retirement")
                optionList = rangePriceShort;
            else if (pVal == "Residential")
                optionList = rangePriceShortRes;
            else if (pVal == "Office" || pVal == "Retail" || pVal == "Industrial") 
                optionList = rangePriceLong;
        }        
    }
    else if (rVal == "Lease")
    {
        if (pVal == "Healthcare & Retirement")
            optionList = rangeAreaShort;
        else if (pVal == "Office" || pVal == "Retail" || pVal == "Industrial" || pVal == "Hotels")
            optionList = rangeAreaLong;
    }
    
    if (optionList.length == 0)
    {
        // Disable 'range' field if no options are available
        rangeField.disabled = "disabled";
        rangeField.style.backgroundColor = "#F3F3F3";
    }
    else
    {
        // Enable 'range' field
        rangeField.disabled = "";
        rangeField.style.backgroundColor = "";
        
        // Add options to 'range' drop-down list
        for (var i=0; i<optionList.length; i++)
        {
            rangeField.options[rangeField.length] = new Option(optionList[i],encodeURIComponent(optionList[i]));
        }    
        
        // Select option based on 'rangeVal' param
        if (rangeValue != "")
        {
            for (var i=0; i<rangeField.length; i++)
            {
                if (rangeField.options[i].value == rangeValue)
                {
                    rangeField.selectedIndex = i;
                }
            }
        }
    }
}

// Common functions
//------------------------------------------------------------------

//------------------------------------------------------------------
// Event handlers

function ddlUserPrefix_onClick()
{
    if (document.registrationForm.ddlUserPrefix.value == "Other")
	{
		displaySection('txtUserPrefixOther', 1, 'medfield');
	}
	else
	{
		displaySection('txtUserPrefixOther', 0);
	}
}

function ddlPhysicalState_onClick()
{
    if (document.registrationForm.ddlPhysicalState.value == "Other")
	{
		displaySection('txtPhysicalStateOther', 1, 'medfield');
	}
	else
	{
		displaySection('txtPhysicalStateOther', 0);
	}
}

function mailAddressDifferent_init()
{   
    if(document.registrationForm.chkMailingDifferent.checked == true)
    {
        displaySection('postalAddressSection', 1);   
    }
    else
    {
        displaySection('postalAddressSection', 0);
    }  
}

function mailAddressDifferent_onClick()
{   
    if (document.getElementById('postalAddressSection').className == "hidden")
    {
        document.registrationForm.chkMailingDifferent.checked = true;
        displaySection('postalAddressSection', 1);
    }
    else
    {
        document.registrationForm.chkMailingDifferent.checked = false;
        displaySection('postalAddressSection', 0);
    }   
}

function ddlPostalState_onClick()
{
    if (document.registrationForm.ddlPostalState.value == "Other")
	{
		displaySection('txtPostalStateOther', 1, 'medfield');
	}
	else
	{
		displaySection('txtPostalStateOther', 0);
	}
}

function ddlBusinessType_onClick()
{
    if (document.registrationForm.ddlBusinessType.value == "Other, please specify")
	{
		displaySection('txtBusinessTypeOther', 1, 'medfield');
	}
	else
	{
		displaySection('txtBusinessTypeOther', 0);
	}
}

function radReceiveInfo_onClick()
{
    if (document.registrationForm.radReceiveInfo[1].checked == true)
	{
		displaySection('otherCompanyNamesSection', 1);
	}
	else
	{
		displaySection('otherCompanyNamesSection', 0);
	}
}

function radReceiveInfoOtherCompanyNames_onClick()
{
    if (document.registrationForm.radReceiveInfoOtherCompanyNames[1].checked == true)
	{
		displaySection('txtOtherCompanyNames', 1, 'medfield');
	}
	else
	{
		displaySection('txtOtherCompanyNames', 0);
	}
}

function subscribeToResearch_onClick()
{
    if (document.getElementById('subscribeToResearchSection').className == "hidden")
    {
        displaySection('subscribeToResearchSection', 1);
    }
    else
    {
        displaySection('subscribeToResearchSection', 0);
    }
}

function subscribeToPublications_onClick()
{
    if (document.getElementById('subscribeToPublicationsSection').className == "hidden")
    {
        displaySection('subscribeToPublicationsSection', 1);
    }
    else
    {
        displaySection('subscribeToPublicationsSection', 0);
    }
}

function propertyRequirementsInterests_onClick()
{
    if (document.getElementById('propertyRequirementsInterestsSection').className == "hidden")
    {
        displaySection('propertyRequirementsInterestsSection', 1);
    }
    else
    {
        displaySection('propertyRequirementsInterestsSection', 0);
    }
}

// Event handlers
//------------------------------------------------------------------