/* dynamicStateProvince.js
**************************
This is an attempt at a universally useable javascript for the dynamic generation of the state/province select lists that are used throughout the site.
As with javascript 1 or all parameters could be omitted when calling the function. Only in special circumstances should this be attempted (ex: search form on the index page - country & state/province are in the same TD, so the TdId is not required)

Accepts:
  countryBox:		*required*
 	A reference to the country box that we must reference to know which country we're dealing with
  stateProvBoxName:
  	The name of the state/province select list. This will also be used to see if there's a label associated with it so it can be updated as well
  selectedStateProv:
  	If there's a default value passed, we'll select it when we loop through.
  stateProvTdId:
  	In some *very specific* cases, the country & state/province select lists will be in TDs that are side by side (country, followed by state/province). The class names will be altered as well to remove or replace a line that separates the two.
  classOn:
  	If applicable change the class for the TD (thus, requires the TdId be set)
  classOff:
  	Similar to classOff, but if it's off.
*/

function loadStateProvinceList(countryBox, stateProvBoxName, selectedStateProv, stateProvTdId, classOn, classOff){
	var selectedCountry;
	var stateProvBox = document.getElementById(stateProvBoxName);
	var stateProvLabel = document.getElementById(stateProvBoxName+"_label");
	var stateProvTd = document.getElementById(stateProvTdId);
	var optionArray = new Array();
	var i = 0;
	var offset = 0;

	selectedCountry = countryBox.options[countryBox.selectedIndex].value;

	// only proceed if the destination dropdown exists
	if(stateProvBox != null){

		// reset
		stateProvBox.options.length = 0;

		if(selectedCountry == "United States"){
			stateProvBox.style.display = 'block';
			if(stateProvLabel != null){
				//stateProvLabel.style.display = 'block';
				stateProvLabel.innerHTML = 'State:';
			}
			if((stateProvTd != null) && (classOn != '')){
				stateProvTd.className = classOn;
			}

			// very special case where the index page search needs a differen title
			if(stateProvBoxName == "workingSearchStateProvince"){
				stateProvBox.options[0] = new Option("By State", "");
			}
			else{
				stateProvBox.options[0] = new Option("Select a State", "");
			}
			stateProvBox.options[1] = new Option("--------------------------------", "");
			offset = 2;

			optionArray[i++] = 'Alabama';
			optionArray[i++] = 'Alaska';
			optionArray[i++] = 'Arkansas';
			optionArray[i++] = 'Arizona';
			optionArray[i++] = 'California';
			optionArray[i++] = 'Colorado';

			optionArray[i++] = 'Connecticut';
			optionArray[i++] = 'District of Columbia';
			optionArray[i++] = 'Delaware';
			optionArray[i++] = 'Florida';
			optionArray[i++] = 'Georgia';
			optionArray[i++] = 'Hawaii';

			optionArray[i++] = 'Iowa';
			optionArray[i++] = 'Idaho';
			optionArray[i++] = 'Illinois';
			optionArray[i++] = 'Indiana';
			optionArray[i++] = 'Kansas';
			optionArray[i++] = 'Kentucky';

			optionArray[i++] = 'Louisiana';
			optionArray[i++] = 'Massachusetts';
			optionArray[i++] = 'Maryland';
			optionArray[i++] = 'Maine';
			optionArray[i++] = 'Michigan';
			optionArray[i++] = 'Minnesota';

			optionArray[i++] = 'Missouri';
			optionArray[i++] = 'Mississippi';
			optionArray[i++] = 'Montana';
			optionArray[i++] = 'North Carolina';
			optionArray[i++] = 'North Dakota';
			optionArray[i++] = 'Nebraska';

			optionArray[i++] = 'New Hampshire';
			optionArray[i++] = 'New Jersey';
			optionArray[i++] = 'New Mexico';
			optionArray[i++] = 'Nevada';
			optionArray[i++] = 'New York';
			optionArray[i++] = 'Ohio';

			optionArray[i++] = 'Oklahoma';
			optionArray[i++] = 'Oregon';
			optionArray[i++] = 'Pennsylvania';
			optionArray[i++] = 'Rhode Island';
			optionArray[i++] = 'South Carolina';
			optionArray[i++] = 'South Dakota';

			optionArray[i++] = 'Tennessee';
			optionArray[i++] = 'Texas';
			optionArray[i++] = 'Utah';
			optionArray[i++] = 'Virginia';
			optionArray[i++] = 'Vermont';
			optionArray[i++] = 'Washington';

			optionArray[i++] = 'Wisconsin';
			optionArray[i++] = 'West Virginia';
			optionArray[i++] = 'Wyoming';
		}
		else if(selectedCountry == "Canada"){
			stateProvBox.style.display = 'block';
			if(stateProvLabel != null){
				//stateProvLabel.style.display = 'block';
				stateProvLabel.innerHTML = 'Province:';
			}
			if((stateProvTd != null) && (classOn != '')){
				stateProvTd.className = classOn;
			}

			// very special case where the index page search needs a differen title
			if(stateProvBoxName == "workingSearchStateProvince"){
				stateProvBox.options[0] = new Option("By Province", "");
			}
			else{
				stateProvBox.options[0] = new Option("Select a Province", "");
			}
			stateProvBox.options[1] = new Option("------------------------------", "");
			offset = 2;

			optionArray[i++] = 'Alberta';
			optionArray[i++] = 'British Columbia';
			optionArray[i++] = 'Manitoba';
			optionArray[i++] = 'New Brunswick';
			optionArray[i++] = 'Newfoundland';
			optionArray[i++] = 'Northwest Territories';
			optionArray[i++] = 'Nova Scotia';
			optionArray[i++] = 'Nunavut';
			optionArray[i++] = 'Ontario';
			optionArray[i++] = 'Prince Edward Island';
			optionArray[i++] = 'Quebec';
			optionArray[i++] = 'Saskatchewan';
			optionArray[i++] = 'Yukon';
		}
		else{
			// very special case
			if(stateProvBoxName == "workingSearchStateProvince"){
				stateProvBox.style.display = 'none';
			}
			else{
				stateProvBox.style.display = 'none';
				stateProvBox.options[0] = new Option("- not applicable -", "");
			}
			if(stateProvLabel != null){
				//stateProvLabel.style.display = 'none';
				stateProvLabel.innerHTML = '&nbsp;';
			}
			if((stateProvTd != null) && (classOff != '')){
				stateProvTd.className = classOff;
			}
		}


		// now cycle through our array
		for(i=0; i<optionArray.length; i++)
		{
			stateProvBox.options[(i+offset)] = new Option(optionArray[i],optionArray[i]);
			// selectedStateProv
			if(optionArray[i] == selectedStateProv){
				stateProvBox.options[(i+offset)].selected = true;
			}
		}
	}
}

/* countChars
*************
This is a great universally useable function to count the chars in a form input and display the value.
Ex: onKeyUp=\"countChars(this, 'db_Personal_philosophy_charCount');\"
*/
function countChars(inField, outCount){
	var count = 0;
	var index = 0;
	var outDisplay = document.getElementById(outCount);

	// get the initial total
	count =  inField.value.length;

	if(count > 0){
		// JavaScript does NOT treat a new line as a char, so find 'em
		index = inField.value.indexOf('\n');
		while(index != -1){
			count ++;
			index = inField.value.indexOf('\n', index+1);
		}
	}

	if(outDisplay != null){
		outDisplay.innerHTML = count;
	}
}

