//  $Id: formUtils.js 73028 2008-04-21 20:49:43Z dselfrid $ 
/*formUtils.js*/
//method for flipping phone/fax extension
function swapPhoneExtension(caller) {

	//check that a phone field exists first
	if (document.getElementById(caller + ".phone")) {
		//if the phone format is in this list
		var formatLikeUS = {"US":"US","CA":"CA","-":"-"," ":" ","":""};
	
		var USFormat = "XXX-XXX-XXXX";
		var internationalFormat = "XX-XXXXXXXXXX";
		var example = "Example: ";
	
		//create instructions based on value
		if (document.getElementById(caller + ".country").value in formatLikeUS) {
			example += USFormat;
		} else {
			example += internationalFormat;
		}
	
		//set the inner HTML of the instruction containers
		document.getElementById(caller + ".formTelephoneFormat").innerHTML = document.getElementById(caller + ".formFaxFormat").innerHTML = example;
	}
}







//show/hide "Other International Province" based on state selection
//dependency:  state/provinces length and order stays the same
function showOtherProvince(caller) {

	var notOtherCountries = {"US":"US","CA":"CA","AU":"AU","GB":"GB"};

	//if state/province selected is "other"
	if(document.getElementById(caller + ".state").value == "OTHER") {
		//show international province
		//show(caller + ".otherInternational");
		hide("stateSelect");

		//and if country = US or CA or AU
		if(document.getElementById(caller + ".country").value in notOtherCountries) {
			//change to the first element which instructs to select a country
			document.getElementById(caller + ".country").selectedIndex = 0;

			//and reformat the phone number help if there's a phone number
			if(document.getElementById(caller + ".phone")) {
				swapPhoneExtension(caller);
			}
		}
	} else {
		//otherwise hide that section
		
		if(!(document.getElementById(caller + ".country").value in notOtherCountries)) {
		hide("stateSelect");
		}
		
		//if the international province bit exists, and it gets hidden, set its value to null.
		if(document.getElementById('contact.internationalProvince')){
			document.getElementById('contact.internationalProvince').value = "";
		}

		//and if country = US or CA, reselect CA or US from country
		if ((document.getElementById(caller + ".state").selectedIndex >= 2 && document.getElementById(caller + ".state").selectedIndex <= 53 )) {
			document.getElementById(caller + ".country").value = "US";
		} else if ((document.getElementById(caller + ".state").selectedIndex >= 54 && document.getElementById(caller + ".state").selectedIndex <= 68 )) {
			document.getElementById(caller + ".country").value = "CA";
		} else if ((document.getElementById(caller + ".state").selectedIndex >= 71 && document.getElementById(caller + ".state").selectedIndex <= 78 )) {
					document.getElementById(caller + ".country").value = "AU";
		} else if ((document.getElementById(caller + ".state").selectedIndex >= 81 && document.getElementById(caller + ".state").selectedIndex <= 84 )) {
					document.getElementById(caller + ".country").value = "GB";
		}

		//and reformat the phone number help if there's a phone number
		if(document.getElementById(caller + ".phone")) {
			swapPhoneExtension(caller);
		}

	}

}

//show/hide/reset state/province other international province when country changes
//dependency:  state/provinces length and order stays the same
function showCountryProvince(caller) {
	//get the country
	var country = document.getElementById(caller + ".country").value;
	//if country !US and !CA, switch State/Province to "OTHER"
	if(country != "US" && country != "CA" && country != "AU" && country != "GB") {
		document.getElementById(caller + ".state").value = "OTHER";
		showOtherProvince(caller);
		hide("stateSelect");
		document.getElementById(caller + ".state").selectedIndex = 0;
		
	} else {
		//select "US States" if country == "US" and the selected index of state drop-down is out of range of the US states
		if (country == "US" &&(document.getElementById(caller + ".state").selectedIndex < 2 || document.getElementById(caller + ".state").selectedIndex > 53 )) {
			document.getElementById(caller + ".state").selectedIndex = 2;
			
		} else if (country == "CA" &&  (document.getElementById(caller + ".state").selectedIndex < 55 || document.getElementById(caller + ".state").selectedIndex > 68 )) {
			document.getElementById(caller + ".state").selectedIndex = 55;
		} else if (country == "AU" &&  (document.getElementById(caller + ".state").selectedIndex < 71 || document.getElementById(caller + ".state").selectedIndex > 78 )) {
			document.getElementById(caller + ".state").selectedIndex = 70;
		} else if (country == "GB" &&  (document.getElementById(caller + ".state").selectedIndex < 81 || document.getElementById(caller + ".state").selectedIndex > 84 )) {
			document.getElementById(caller + ".state").selectedIndex = 80;
		}
		show("stateSelect");
	}
	
}

//method for copying address values
function copyAddress(to, from) {
	function fromTo(elementId) {
		document.getElementById(to + "." + elementId).value = document.getElementById(from + "." + elementId).value;
	}

	var addressFields = new Array(
		"address1",
		"address2",
		"city",
		"state",
		"internationalProvince",
		"zipCode",
		"country",
		"phone",
		"fax",
		"email");

	if(to && from) {
		for(i = 0; i <addressFields.length; i++) {
			fromTo(addressFields[i]);
		}

		//change the state/address/other drop-downs accordingly
		showOtherProvince(to);

		//and reformat the phone number help if there's a phone number
		if(document.getElementById(to + ".phone")) {
			swapPhoneExtension(to);
		}

	}

}

function dropTheFocus(){
	//if the applicationForm exists
	if(document.forms.applicationForm){
		//give the third element (the first visible element after page and the first fieldset) focus
		document.forms.applicationForm.elements[2].focus();
	}else{
		//do nothing
	}
}
