﻿// JScript File

	$(document).ready(function(){
		var countrySelect = $("select[id$=uxCountry]");
		var countryOtherDiv = $("div[id$=countryotherdiv]");
		var stateotherdiv = $("div[id$=stateotherdiv]");
		var statedropdowndiv = $("div[id$=statedropdowndiv]");
		
		if (countrySelect.find("option:selected").text().toLowerCase() == "other")
		{
			countryOtherDiv.show();
			stateotherdiv.show();
			statedropdowndiv.hide();
		}
		else if (countrySelect.find("option:selected").text().toLowerCase() == "united states")
		{
			countryOtherDiv.hide();
			stateotherdiv.hide();
			statedropdowndiv.show();
		}
		else if (countrySelect.val() ==  "")
		{
			countryOtherDiv.hide();
			stateotherdiv.hide();
			statedropdowndiv.hide();
		}
		else
		{
			countryOtherDiv.hide();
			stateotherdiv.show();
			statedropdowndiv.hide();
		}
	});

	function State_Change(SelectField, OtherField, OtherDiv) 
	{
		//parameters are the clientIDs of the fields, not jQuery objects
	    var indexNum = SelectField.selectedIndex;
		if (SelectField[indexNum].text == 'APO/FPO')
		{
			$("#" + OtherDiv).show();
		}
		else 
		{
			$("#" + OtherDiv).hide();
			$("#" + OtherField).val("");
		}
	}
	
	function Country_Change(CountrySelectField, CountryOtherField, CountryOtherDiv, StateSelectField, StateSelectDiv, StateOtherField, StateOtherDiv) 
	{
		//parameters are the clientIDs of the fields, not jQuery objects
		if (CountrySelectField.options[CountrySelectField.selectedIndex].text.toLowerCase() == "other")
		{
			//Other
			$("#" + CountryOtherDiv).show();
			$("#" + StateOtherDiv).show();
			$("#" + StateSelectField).selectedIndex = 0;
			$("#" + StateSelectDiv).hide();
		}
		else if (CountrySelectField.options[CountrySelectField.selectedIndex].text.toLowerCase() == "united states")
		{
			//United States
			$("#" + CountryOtherDiv).hide();
			$("#" + StateOtherDiv).hide();
			$("#" + CountryOtherField).val("");
			$("#" + StateOtherField).val("");
			$("#" + StateSelectField).selectedIndex = 0;
			$("#" + StateSelectDiv).show();
		
		}
		else if (CountrySelectField.options[CountrySelectField.selectedIndex].value == "")
		{
		    //no country selected
		    $("#" + CountryOtherDiv).hide();
			$("#" + StateOtherDiv).hide();
			$("#" + StateSelectField).selectedIndex = $(StateSelectField).length-1;
			$("#" + StateSelectDiv).hide();
		}
		else 
		{
			//International
			$("#" + CountryOtherDiv).hide();
			$("#" + StateOtherDiv).show();
			$("#" + StateSelectField).selectedIndex = $(StateSelectField).length-1;
			$("#" + StateSelectDiv).hide();
			
		}
	}
	
	function ValidateStateSelection(state, stateOther, country)
	{
		//parameters are already jQuery objects
	    argsIsValid = true;
	    
	    if((state != null) && (stateOther != null) && (country != null))
        {        
            if((country.val() == ""))
            {
                argsIsValid = false;
            }
            else if(country.find("option:selected").text().toLowerCase() == "other")
            {
//                if (stateOther.val().length == 0)
//                    argsIsValid = false;
            }
            else if(country.find("option:selected").text().toLowerCase() == "united states")
            {
                if(state.val() == "")
                    argsIsValid = false;
                else
                    argsIsValid = true;
            }
            else
            {
				argsIsValid = true;
            }
        }
        else
            argsIsValid = false;
        
        return argsIsValid;
	}
	
	
	function ValidateCountrySelection(country, countryOther)
	{
		//parameters are already jQuery objects
	    argsIsValid = true;
	    
	    if((country != null) && (countryOther != null))
        {
            if((country.val() == ""))
                argsIsValid = false;
            else if(country.find("option:selected").text().toLowerCase() == "other")
                if (countryOther.val().length == 0)
                    argsIsValid = false;
        }
        else
            argsIsValid = false;
        
        return argsIsValid;
	}