// JavaScript Document
// These are the address formats for countries
// US
var thisAddress;

// NRG: PA STATE ID
var PA_STATEID = 44;

var address_US = "US";
var address_usestate_US = true;
var address_statelabel_US = "State/Territory";
var postcode_form_US = "<input type=\"text\" maxlength=\"5\" length=\"5\" name=\"txtPostcode[]\" id=\"txtPostcode_0\" />-<input type=\"text\" maxlength=\"4\" length=\"4\" name=\"txtPostcode[]\" id=\"txtPostcode_1\" />";
var postcode_label_US = "ZIP Code";
var postcode_valid_US = /^\d{5}([\-]\d{4})?$/;
// Canada
var address_CA = "CA";
var address_usestate_CA = true;
var address_statelabel_CA = "Province";
var postcode_form_CA = "<input type=\"text\" maxlength=\"3\" length=\"3\" name=\"txtPostcode[]\" id=\"txtPostcode_0\" /> <input type=\"text\" maxlength=\"3\" length=\"3\" name=\"txtPostcode[]\" id=\"txtPostcode_1\" />";
var postcode_label_CA = "Post Code";
var postcode_valid_CA = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i;
// Mexico
var address_MX = "MX";
var address_usestate_MX = true;
var address_statelabel_MX = "State";
var postcode_form_MX = "<input type=\"text\" maxlength=\"5\" length=\"5\" name=\"txtPostcode[]\" id=\"txtPostcode_0\" />";
var postcode_label_MX = "Post Code";
var postcode_valid_MX = /^\d{5}$/;
// United Kingdom
var address_UK = "UK";
var address_usestate_UK = false;
var postcode_form_UK = "<input type=\"text\" maxlength=\"4\" length=\"4\" name=\"txtPostcode[]\" id=\"txtPostcode_0\" /> <input type=\"text\" maxlength=\"4\" length=\"3\" name=\"postcode[]\" id=\"postcode_1\" />";
var postcode_label_UK = "Postal Code";
var postcode_valid_UK = /^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z-[CIKMOV]]{2}$/;
// Austraila
var address_AUS = "AUS";
var address_usestate_AUS = true;
var address_statelabel_AUS = "Territory";
var postcode_form_AUS = "<input type=\"text\" maxlength=\"4\" length=\"4\" name=\"txtPostcode[]\" id=\"txtPostcode_0\" />";
var postcode_label_AUS = "Postcode";
var postcode_valid_AUS = /^\d{4}$/;
// default
var address_ = "";
var address_usestate_ = true;
var address_statelabel_ = "Area, Province, or Territory (if needed)";
var postcode_form_ = "<input type=\"text\" maxlength=\"10\" length=\"10\" name=\"txtPostcode[]\" id=\"txtPostcode_0\" />";
var postcode_label_ = "Postal Code";
var postcode_valid_ = "";

// Form elements
var countryDropDown;
var stateDropDown;
var stateTextbox;
var stateDropDownContainer;
var stateDropDownLabel;
var countyDropDown;
var countyDropDownContainer;
var postcodeContainer;
var postcodeLabel;

var schoolPickerDiv;

var requestPage = "../_data/locationPicker.php";
function initLocationPicker(localPath) {
	
	countryDropDown = document.getElementById("cboCountry");
	
	stateDropDown = document.getElementById("cboState");
	//stateTextbox = document.getElementById("txtState");
	stateDropDownContainer = document.getElementById("divState");
	stateDropDownLabel = document.getElementById("lblState");
	
	countyDropDown = document.getElementById("cboCounty");
	countyDropDownContainer = document.getElementById("countyDrop");
	
	schoolPickerDiv = document.getElementById("PASchool");
	
	if (localPath != null) { requestPage = localPath; }
	
	postcodeContainer = document.getElementById("zipCode"); //document.getElementById("divPostcode");
	//postcodeLabel = document.getElementById("lblPostcode");
}

function runCountries(doZip) {
	var query = "";
	// When countries are selected set the form as appropriate
	doCounty = false;
	countryID = parseInt(countryDropDown[countryDropDown.selectedIndex].value);
	// Hide counties by default unless US
	countyDropDownContainer.style.display = "none";
	// nrg: Hide school picker form - will open when selecting state
	if (schoolPickerDiv)
		schoolPickerDiv.style.display = "none";
	//if there is an OTHER SCHOOL element, show it instead
	if (document.getElementById("OtherSchool")) {
		document.getElementById("OtherSchool").style.display = "block";
	}
	// Check for special case address fields
	// Turn off zip by default
	if (doZip)
		postcodeContainer.style.display = "none";
	switch(countryID) {
		case 1:
			// US
			thisAddress = address_US;
			// Turn on counties, set to index 0
			doCounty = true;
			// Show Zip
			if (doZip)
				postcodeContainer.style.display = "block";
			break;
		case 2:
			// Canada
			thisAddress = address_CA;
			break;
		case 3:
			// Mexico
			thisAddress = address_MX;
			break;
		case 83:
			// UK
			thisAddress = address_UK;
			break;
		case 152:
			// Austraila
			thisAddress = address_AUS;
			break;
		default:
			// Everyone else
			thisAddress = address_;
			break;
	}
	doState = eval("address_usestate_" + thisAddress);
	// Set the ZIP
	//setPostalCode(thisAddress);
	if (doState===true) {
		stateDropDownContainer.style.display = (stateDropDownContainer.nodeName=="SPAN") ? "inline" : "block";
		stateDropDownLabel.style.display = (stateDropDownContainer.nodeName=="SPAN") ? "inline" : "block";
		stateDropDownLabel.innerHTML = eval("address_statelabel_" + thisAddress);
		
		// If counties, show dropdown
		if (doCounty)
			countyDropDownContainer.style.display = (countyDropDownContainer.nodeName=="SPAN") ? "inline" : "block";
		
		if (thisAddress=="") {
			// default - swap the state combo box for a textbox
			//stateTextbox.style.display = "block";
			stateDropDown.style.display = "none";
		} else {
			//stateTextbox.style.display = "none";
			//stateDropDown.style.display = "block";
			stateDropDown.style.display = (stateDropDownContainer.nodeName=="SPAN") ? "inline" : "block";
			query = requestPage + "?mode=states&value=" + countryID;
			runAjaxCall(query,'postCountry',null,null);
		}
	} else {
		stateDropDownContainer.style.display = "none";
		stateDropDownLabel.style.display = "none";
	}
}

function runStates() {
	// Only applicable to US states to get counties
	if (countryDropDown[countryDropDown.selectedIndex].value == 1) {
		countyDropDownContainer.style.display = (countyDropDownContainer.nodeName=="SPAN") ? "inline" : "block";
		var stateID = stateDropDown[stateDropDown.selectedIndex].value;
		// nrg: if state is PA show school picker
		if (stateID==PA_STATEID) {
			if (schoolPickerDiv)
				schoolPickerDiv.style.display = "block";
			//if there is an OTHER SCHOOL element, hide it
			if (document.getElementById("OtherSchool")) {
				document.getElementById("OtherSchool").style.display = "none";
			}
		} else {
			if (schoolPickerDiv)
				schoolPickerDiv.style.display = "none";
			//if there is an OTHER SCHOOL element, show it
			if (document.getElementById("OtherSchool")) {
				document.getElementById("OtherSchool").style.display = "block";
			}
		}
		query = requestPage + "?mode=counties&value=" + stateID;
		runAjaxCall(query,'postState',null,null);
	}
}

function setValues(str,obj) {
	// parse out the result
	if (str.match("~")==null) {
		// No items were passed, just clear the object
		// clear the current content
		if(obj.options.length>1){
			obj.options.length=0;
		}
	} else {
		ary = str.split("|");
		// clear the current content
		if(obj.options.length>0){
			obj.options.length=0;
		}
		// Add an empty "select..." option
		var bo = document.createElement('option');
		bo.value = "";
		bo.text = "Select...";
		obj.options[obj.options.length] = bo;
		// populate the box
		for(o=0;o<ary.length;o++){
			tary=ary[o].split('~');
			var no=document.createElement('option');
			no.value=tary[0];
			no.text= tary[1];
			obj.options[obj.options.length] = no;
		}
	}
	return;
}

function postCountry(str) {
	setValues(str,stateDropDown);
}

function postState(str) {
	setValues(str,countyDropDown);	
}

function setPostalCode(setAddr) {
	// set the label
	postcodeLabel.innerHTML = eval("postcode_label_" + setAddr);
	// Get the form
	postcodeContainer.innerHTML = eval("postcode_form_" + setAddr);
		
}