/*formUtils.js*/

var get = document.getElementById;

function relabel(elementId,newText) {
	var changeMe = $(elementId);
	
	if(changeMe && newText) {
		changeMe.innerHTML = newText;
	}
}
//method for flipping phone/cell 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 + ".formCellFormat").innerHTML = example;
	}
}

//show/hide "Other International Province" based on state selection
//dependency:  state/provinces length and order stays the same
function showOtherProvince(caller) {

    if (document.getElementById(caller + ".state") == null)
       return;
        
	var notOtherCountries = {"US":"US","CA":"CA"};

	//if state/province selected is "other"
	if(document.getElementById(caller + ".state").value == "OTHER") {
		//show international province
		show(caller + ".otherInternational");
		
		//and if country = US or CA
		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
		hide(caller + ".otherInternational");

		//and if country = US or CA, reselect CA or US from country
		if ((document.getElementById(caller + ".state").selectedIndex >= 2 && document.getElementById(caller + ".state").selectedIndex <= 52 )) {
			document.getElementById(caller + ".country").value = "US";
		} else if ((document.getElementById(caller + ".state").selectedIndex >= 54 && document.getElementById(caller + ".state").selectedIndex <= 67 )) {
			document.getElementById(caller + ".country").value = "CA";
		}

		//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") {
		document.getElementById(caller + ".state").value = "OTHER";
		showOtherProvince(caller);
	} 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 > 52 )) {
			document.getElementById(caller + ".state").selectedIndex = 2;
		} else if (country == "CA" &&(document.getElementById(caller + ".state").selectedIndex < 54 || document.getElementById(caller + ".state").selectedIndex > 67 )) {
			document.getElementById(caller + ".state").selectedIndex = 54;
		}
		showOtherProvince(caller);
	}
	
}

//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",
		"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);
		}

	}

}