function getQueryVariable(variable, defaultValue) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for(var i = 0; i < vars.length; i++) { 
		var pair = vars[i].split("="); 
		if(pair[0] == variable) { 
			return pair[1].replace("%20", " "); 
		} 
	} 
	return defaultValue;
}

function appendOption(field, newValue, newText) {
	var elOptNew = document.createElement('option');
	elOptNew.text = newText;
	elOptNew.value = newValue;
	
	try {
		field.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		field.add(elOptNew); // IE only
	}
}

function checkCountry() {
	var country = document.getElementById("country");
	if(country.value == "US") {
		document.getElementById("state_province_para").style.display = "block";
		var stateList = document.getElementById("state");
		for(i = stateList.length; i > 0; i--) {
			stateList.remove(i);
		}

		appendOption(stateList, "AL", "Alabama");
		appendOption(stateList, "AK", "Alaska");
		appendOption(stateList, "AZ", "Arizona");
		appendOption(stateList, "AR", "Arkansas");
		appendOption(stateList, "CA", "California");
		appendOption(stateList, "CO", "Colorado");
		appendOption(stateList, "CT", "Connecticut");
		appendOption(stateList, "DE", "Delaware");
		appendOption(stateList, "DC", "Dist of Columbia");
		appendOption(stateList, "FL", "Florida");
		appendOption(stateList, "GA", "Georgia");
		appendOption(stateList, "HI", "Hawaii");
		appendOption(stateList, "ID", "Idaho");
		appendOption(stateList, "IL", "Illinois");
		appendOption(stateList, "IN", "Indiana");
		appendOption(stateList, "IA", "Iowa");
		appendOption(stateList, "KS", "Kansas");
		appendOption(stateList, "KY", "Kentucky");
		appendOption(stateList, "LA", "Louisiana");
		appendOption(stateList, "ME", "Maine");
		appendOption(stateList, "MD", "Maryland");
		appendOption(stateList, "MA", "Massachusetts");
		appendOption(stateList, "MI", "Michigan");
		appendOption(stateList, "MN", "Minnesota");
		appendOption(stateList, "MS", "Mississippi");
		appendOption(stateList, "MO", "Missouri");
		appendOption(stateList, "MT", "Montana");
		appendOption(stateList, "NE", "Nebraska");
		appendOption(stateList, "NV", "Nevada");
		appendOption(stateList, "NH", "New Hampshire");
		appendOption(stateList, "NJ", "New Jersey");
		appendOption(stateList, "NM", "New Mexico");
		appendOption(stateList, "NY", "New York");
		appendOption(stateList, "NC", "North Carolina");
		appendOption(stateList, "ND", "North Dakota");
		appendOption(stateList, "OH", "Ohio");
		appendOption(stateList, "OK", "Oklahoma");
		appendOption(stateList, "OR", "Oregon");
		appendOption(stateList, "PA", "Pennsylvania");
		appendOption(stateList, "RI", "Rhode Island");
		appendOption(stateList, "SC", "South Carolina");
		appendOption(stateList, "SD", "South Dakota");
		appendOption(stateList, "TN", "Tennessee");
		appendOption(stateList, "TX", "Texas");
		appendOption(stateList, "UT", "Utah");
		appendOption(stateList, "VT", "Vermont");
		appendOption(stateList, "VA", "Virginia");
		appendOption(stateList, "WA", "Washington");
		appendOption(stateList, "WV", "West Virginia");
		appendOption(stateList, "WI", "Wisconsin");
		appendOption(stateList, "WY", "Wyoming");
	} else if(country.value == "CA") {
		document.getElementById("state_province_para").style.display = "block";
		var stateList = document.getElementById("state");
		for(i = stateList.length; i > 0; i--) {
			stateList.remove(i);
		}
		appendOption(stateList, "AB", "Alberta");
		appendOption(stateList, "BC", "British Columbia");
		appendOption(stateList, "MB", "Manitoba");
		appendOption(stateList, "NB", "New Brunswick");
		appendOption(stateList, "NL", "Newfoundland and Labrador");
		appendOption(stateList, "NT", "Northwest Territories");
		appendOption(stateList, "NS", "Nova Scotia");
		appendOption(stateList, "NU", "Nunavut");
		appendOption(stateList, "ON", "Ontario");
		appendOption(stateList, "PE", "Prince Edward Island");
		appendOption(stateList, "QC", "Quebec");
		appendOption(stateList, "SK", "Saskatchewan");
		appendOption(stateList, "YT", "Yukon");
	} else {
		document.getElementById("state_province_para").style.display = "none";
		var stateList = document.getElementById("state");
		stateList.selectedIndex = 0;			
	}
}

function appendMissingElement(list, element) {
	if(list == "") {
		return element;
	} else {
		return list + ", " + element;
	}
}
		
function valid(form) {
	var missingItems = "";
	if(document.getElementById("first_name").value == "") {
		missingItems = appendMissingElement(missingItems, "First Name");
	}

	if(document.getElementById("last_name").value == "") {
		missingItems = appendMissingElement(missingItems, "Last Name");
	}

	if(document.getElementById("email").value == "") {
		missingItems = appendMissingElement(missingItems, "Email Address");
	}
	
	if(missingItems == "") {
		return true;
	}
	
	alert("The following required items are missing: " + missingItems);
	return false;
}

