var whitespace = " \t\n\r";
function isEmpty(s){   
	return ((s == null) || (s.length == 0));
}
function isStringWhitespace(s){   
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}
function register(f){
	Names = new Array();
	Names[1] = "First name";
	Names[2] = "Phone Number";
	Names[3] = "Last Name";
	Names[4] = "Email";
	Names[5] = "Password";
	Names[6] = "Verify Password";
	Names[7] = "City";
	Names[8] = "State";
	Names[9] = "Postal Code";
	Names[10] = "Phone";
	Names[11] = "Email";
	focusField=11;
	virgin = true;
	msg_final = "";
	msg_list = "";
	msg_invalid = "";
	notGood = false;
	msg = false;
	msg2 = false;
	var email=f.email.value;
	var password=f.password.value;
	var password2=f.password2.value;

	for(i = 1; i < f.elements.length; i ++){
		if(f.elements[i].id == "required" && isStringWhitespace(f.elements[i].value)){
			if(virgin){
				focusField = i;
				virgin = false;
			}
			msg_list+=Names[i]+"\n";
			msg=true;
			notGood = true;
		}
		if (f.elements[i].name=="email" && email!="") {
			if (!checkemail(email)) {
				if(virgin){
					focusField = 4;
					virgin = false;
				}
				msg_invalid+=Names[4]+"\n";
				msg2=true;
				notGood = true;
			}
		}
		if(f.elements[i].name.substr(f.elements[i].name.length-3,3)=="zip" && f.elements[i].value!=""){
			if(isUSPostCode(f.elements[i].value,i)){}
		}
	}
	if(password!=password2){
		if(virgin){
			focusField = 5;
			virgin = false;
		}
		msg_invalid+="The passwords don't match\n";
		msg2=true;
		notGood = true;
	}
	if(notGood){
		flag=false;
		if(msg){
			msg_final += "You haven't completed these fields:\n" + msg_list;
			flag=true;
		}
		if(msg2){
			msg_final += "These fields are invalid:\n" + msg_invalid;
			flag=true;
		}
		if(flag)
		{
			alert(msg_final);
			f.elements[focusField].focus();
			return false;
		}

	}
}

function isUSPostCode(entry,field){
var result = true;
strlen = entry.length; 
	if (strlen != 5) {result=false;}
	for (k = 0; k < strlen; k++){
		if ('0123456789'.indexOf(entry.charAt(k)) < 0) {result=false;}
	}
	if (!result){
	msg_invalid='Please enter a valid postal code.\n';
	msg2=true;
	notGood = true;
		if(virgin){
		focusField = field;
		virgin = false;
		}
	}
	return result;
}

var testresults;
function checkemail(email){
	var str=email
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str)){
		testresults=true;
		}
	return (testresults)
}