// Returns an element if its ID contains the value of '_fieldName'
function getElementId(_tagName,_fieldName) {
	var elements = document.getElementsByTagName(_tagName);
	if (elements != null && elements.length != 0) {	
		for (i=0,x=elements.length;i<x;i++) {
			if (elements[i].id.match(_fieldName) != null) {
				return elements[i];
			}
		}
	}
}

	
function getStateOptions(parent_select_name, child_select_name) {

	var state_list = new Array();
	
	var parent_select = getElementId("select", parent_select_name);
	var child_select = getElementId("select", child_select_name);

	var country_code = parent_select.value;
	var state_code = child_select.value;

	var otherState = getElementId("input", "vcstateother");
	var stateLabel = getElementId("label", "state");
	var otherLabel = getElementId("label", "otherregion");
	var otherNote = getElementId("p", "otherNote");
	

	if (country_code == "US" || country_code == "CA") {

		// Show 'State/province' 
		stateLabel.style.display = "block";
		child_select.style.display = "block";
		
		// Hide the 'Other region' field
		otherLabel.style.display = "none";
		otherState.style.display = "none"; 
		otherState.value = ""; // Removes any values set for 'Other Region'
		if (otherNote) {
			otherNote.style.display = "none";
		}
	
		if (country_code == "US") {
			// Sets the number of dropdown options for "State/province"
			child_select.length = 53;
			
			child_select.options[0] = new Option("Please select a state","");
			child_select.options[1] = new Option("Alabama","AL");
			child_select.options[2] = new Option("Alaska","AK");
			child_select.options[3] = new Option("Arizona","AZ");
			child_select.options[4] = new Option("Arkansas","AR");
			child_select.options[5] = new Option("California","CA");
			child_select.options[6] = new Option("Colorado","CO");
			child_select.options[7] = new Option("Connecticut","CT");
			child_select.options[8] = new Option("Delaware","DE");
			child_select.options[9] = new Option("District of Columbia","DC");
			child_select.options[10] = new Option("Florida","FL");
			child_select.options[11] = new Option("Georgia","GA");
			child_select.options[12] = new Option("Hawaii","HI");
			child_select.options[13] = new Option("Idaho","ID");
			child_select.options[14] = new Option("Illinois","IL");
			child_select.options[15] = new Option("Indiana","IN");
			child_select.options[16] = new Option("Iowa","IA");
			child_select.options[17] = new Option("Kansas","KS");
			child_select.options[18] = new Option("Kentucky","KY");
			child_select.options[19] = new Option("Louisiana","LA");
			child_select.options[20] = new Option("Maine","ME");
			child_select.options[21] = new Option("Maryland","MD");
			child_select.options[22] = new Option("Massachusetts","MA");
			child_select.options[23] = new Option("Michigan","MI");
			child_select.options[24] = new Option("Minnesota","MN");
			child_select.options[25] = new Option("Mississippi","MS");
			child_select.options[26] = new Option("Missouri","MO");
			child_select.options[27] = new Option("Montana","MT");
			child_select.options[28] = new Option("Nebraska","NE");
			child_select.options[29] = new Option("Nevada","NV");
			child_select.options[30] = new Option("New Hampshire","NH");
			child_select.options[31] = new Option("New Jersey","NJ");
			child_select.options[32] = new Option("New Mexico","NM");
			child_select.options[33] = new Option("New York","NY");
			child_select.options[34] = new Option("North Carolina","NC");
			child_select.options[35] = new Option("North Dakota","ND");
			child_select.options[36] = new Option("Ohio","OH");
			child_select.options[37] = new Option("Oklahoma","OK");
			child_select.options[38] = new Option("Oregon","OR");
			child_select.options[39] = new Option("Pennsylvania","PA");
			child_select.options[40] = new Option("Puerto Rico","PR");
			child_select.options[41] = new Option("Rhode Island","RI");
			child_select.options[42] = new Option("South Carolina","SC");
			child_select.options[43] = new Option("South Dakota","SD");
			child_select.options[44] = new Option("Tennessee","TN");
			child_select.options[45] = new Option("Texas","TX");
			child_select.options[46] = new Option("Utah","UT");
			child_select.options[47] = new Option("Vermont","VT");
			child_select.options[48] = new Option("Virginia","VA");
			child_select.options[49] = new Option("Washington","WA");
			child_select.options[50] = new Option("West Virginia","WV");
			child_select.options[51] = new Option("Wisconsin","WI");
			child_select.options[52] = new Option("Wyoming","WY");
			
		} else if (country_code == "CA") {
			// Sets the number of dropdown options for "State/province"
			child_select.length = 14;
			
			child_select.options[0] = new Option("Please select a province","");
			child_select.options[1] = new Option("Alberta","AB");
			child_select.options[2] = new Option("British Columbia","BC");
			child_select.options[3] = new Option("Manitoba","MB");
			child_select.options[4] = new Option("New Brunswick","NB");
			child_select.options[5] = new Option("Newfoundland","NL");
			child_select.options[6] = new Option("Northwest Territories","NT");
			child_select.options[7] = new Option("Nova Scotia","NS");
			child_select.options[8] = new Option("Nunavut","NU");
			child_select.options[9] = new Option("Ontario","ON");
			child_select.options[10] = new Option("Prince Edward Island","PE");
			child_select.options[11] = new Option("Quebec","QC");
			child_select.options[12] = new Option("Saskatchewan","SK");
			child_select.options[13] = new Option("Yukon","YT");
		}
		
		for (i=0; i<child_select.length; i++) {
			if (state_code != null && child_select[i].value == state_code) {
				child_select[i].selected = true;
				break;
			} else {
				child_select.options[0].selected = true;
			}
		}

	} else { 
	// If not US or Canada, display the 'Other Region' field
	
		// Resets any value 
		child_select.options.length = 1;
		child_select.options[0] = new Option("", "35555", false, false);
		
		// Hide 'State/province' 
		stateLabel.style.display = "none";
		child_select.style.display = "none";

		// Show the 'Other region' field
		otherLabel.style.display = "block";
		otherState.style.display = "block";
	}
}


function enableSubmitButton() {
	getElementId("input","sub").disabled = false;
	getElementId("input","sub").src = "/role_based/images/button_save.gif"
}


