// JavaScript Document
var number = "";

function adjustProv(x){
	var opt = document.Catalogue.Province.options;
	if(x == "1"){
		/*for(var i in opt){
					opt.remove(i);
		}*/
		document.getElementById("prov").innerHTML = 'Province';
		document.getElementById("pcode").innerHTML = 'Postal Code';
		opt.length = 14;
		
		opt[0] = new Option("", "");
		opt[1] = new Option("Alberta", "AB");
		opt[2] = new Option("British Columbia", "BC");
		opt[3] = new Option("Manitoba", "MB");
		opt[4] = new Option("New Brunswick", "NB");
		opt[5] = new Option("Newfoundland & Labrador", "NL");
		opt[6] = new Option("Northwest Territories", "NT");
		opt[7] = new Option("Nova Scotia", "NS");
		opt[8] = new Option("Nunavut", "NU");
		opt[9] = new Option("Ontario", "ON");
		opt[10] = new Option("Prince Edward Island", "PE");
		opt[11] = new Option("Quebec", "QC");
		opt[12] = new Option("Saskatchewan", "SK");
		opt[13] = new Option("Yukon", "YK");
	}
	if(x == "2"){
		document.getElementById("prov").innerHTML = 'State';
		document.getElementById("pcode").innerHTML = 'Zip Code';
		opt.length = 51;
		opt[0] = new Option("", "");
		opt[1] = new Option("Alabama", "AL");
		opt[2] = new Option("Alaska", "AK");
		opt[3] = new Option("Arizona", "AZ");
		opt[4] = new Option("Arkansas", "AR");
		opt[5] = new Option("California", "CA");
		opt[6] = new Option("Colorado", "CO");
		opt[7] = new Option("Connecticut", "CT");
		opt[8] = new Option("Delaware", "DE");
		opt[9] = new Option("District of Columbia", "DC");
		opt[10] = new Option("Florida", "FL");
		opt[11] = new Option("Georgia", "GA");
		opt[12] = new Option("Idaho", "ID");
		opt[13] = new Option("Illinois", "IL");
		opt[14] = new Option("Indiana", "IN");
		opt[15] = new Option("Iowa", "IA");
		opt[16] = new Option("Kansas", "KS");
		opt[17] = new Option("Kentucky", "KT");
		opt[18] = new Option("Louisiana", "LA");
		opt[19] = new Option("Maine", "ME");
		opt[20] = new Option("Maryland", "MD");
		opt[21] = new Option("Massachusetts", "MA");
		opt[22] = new Option("Michigan", "MI");
		opt[23] = new Option("Minnesota", "MN");
		opt[24] = new Option("Mississippi", "MS");
		opt[25] = new Option("Missouri", "MO");
		opt[26] = new Option("Montana", "MT");
		opt[27] = new Option("Nebraska", "NE");
		opt[28] = new Option("Nevada", "NV");
		opt[29] = new Option("New Hampshire", "NH");
		opt[30] = new Option("New Jersey", "NJ");
		opt[31] = new Option("New Mexico", "NM");
		opt[32] = new Option("New York", "NY");
		opt[33] = new Option("North Carolina", "NC");
		opt[34] = new Option("North Dakota", "ND");
		opt[35] = new Option("Ohio", "OH");
		opt[36] = new Option("Oklahoma", "OK");
		opt[37] = new Option("Oregon", "OR");
		opt[38] = new Option("Pennsylvania", "PA");
		opt[39] = new Option("Rhode Island", "RI");
		opt[40] = new Option("South Carolina", "SC");
		opt[41] = new Option("South Dakota", "SD");
		opt[42] = new Option("Tennessee", "TN");
		opt[43] = new Option("Texas", "TX");
		opt[44] = new Option("Utah", "UT");
		opt[45] = new Option("Vermont", "VT");
		opt[46] = new Option("Virginia", "VA");
		opt[47] = new Option("Washington", "WA");
		opt[48] = new Option("West Virginia", "WV");
		opt[49] = new Option("Wisconsin", "WI");
		opt[50] = new Option("Wyoming", "WY");
	}
}




function nullValidation() {
    var x = document.Catalogue;
	document.getElementById("Phone").value = number;


   for (var i = 0; i < x.length; i++) {
        if(x.elements[i].value == "" && x.elements[i].title) {
            alert("Please enter " + x.elements[i].title + ".");
            x.elements[i].focus();
            return false;
        }
    }
	return true;
    
}

function checkNumber(x){
	var i = 0;
	while(i <= x.length){
		if(x.charAt(i) >= '0' && x.charAt(i) <= '9'){
			number = number + x.charAt(i);
		}
		i++;
	}
}

function formatPostal(){

	postal = document.getElementById("PostalCode");
	value = postal.value.toUpperCase();
	if(value == ""){
		return;
	}
	invalid = false;
	var pos;
	while((pos = value.indexOf(" ")) != -1){
		value = value.substr(0, pos)+ value.substr(pos+1);
	}

	if(document.getElementById('Country2').checked){
		if(value.length != 6 || value.charAt(0) == "D" || 
			value.charAt(0) == "F" || value.charAt(0) == "I" || 
			value.charAt(0) == "0" || value.charAt(0) == "Q" || 
			value.charAt(0) == "U" || value.charAt(0) == "W" || 
			value.charAt(0) == "Z"){
				invalid = true;
		}
		else{
			for(i = 0; i < value.length; i++){
				if(!(i%2) && (value.charAt(i) < "A" || value.charAt(i) > "Z")){
					invalid = true;
			}
 				else if(i%2 && (value.charAt(i) < "0" || value.charAt(i) > "9")){
					invalid = true;
				}
 			}
		}//End Else 
		if(invalid){
 			alert("You have entered an invalid postal code");
 			postal.focus();
			postal.select();
			return;
		}
		else{
			postal.value = value.substring(0,3)+" "+value.substring(3);
		}
	}//End Country Check If
	else{ //Must be US
		zip = "";
		for(i = 0; i < value.length; i++){
			if(value.charAt(i) >= "0" && value.charAt(i) <= "9"){
				zip = zip+value.charAt(i);
			}
		}
		if(zip.length != 5 && zip.length != 9){
			alert("Invalid zip code");
			postal.focus();
			postal.select();
			return;
		}
		else{
			if(zip.length == 9){
				zip = zip.substring(0, 5)+"-"+zip.substring(5);
			}
			postal.value = zip;
		}
	}//End US condition
}


function phoneNumberFormat(str){
	numberField = document.getElementById(str);
	number = numberField.value;
	newNum = "";
	for(i = 0; i < number.length; i++){
		if(number.charAt(i) >= "0" && number.charAt(i) <= "9"){
			newNum = newNum+number.charAt(i);
		}
	}
	if(newNum.length == 0){
		numberField.value = "";
		return;
	}
	else if(newNum.charAt(0) == "1"){
		newNum = newNum.substring(1);
	}
	if(newNum.length == 10){
		newNum = "("+newNum.substring(0,3)+") "+newNum.substring(3,6)+"-"+newNum.substring(6);
		numberField.value = newNum;
	}
	else{
		alert("Invalid phone number");
		numberField.focus();
		numberField.select();
	}
}

function formatEmail(){
	EField = document.getElementById('email');
	emailAddr = EField.value;
	valid = false;
	at = false;
	if(emailAddr.length == 0){
		return;
	}
	for(i = 0; i < emailAddr.length; i++){
		if(!at && emailAddr.charAt(i) == "@"){
			at = true;
		}
		else if(at && emailAddr.charAt(i) == "." && (i+1) < emailAddr.length){
			valid = true;
			break;
		}
	}
	if(!valid){
		alert("Invalid email address");
		EField.focus();
		EField.select();
	}
}
