/*
   userRegFormCheck.js
   
   form validation for new user registration page.

*/

function checkForm(theForm, go) {
    var why = "";
    if (isEmpty(theForm.firstname.value)){
    	why += "You need to enter a First Name.\n";
    	}
    if (isEmpty(theForm.lastname.value)){
    	why += "You need to enter a Last Name.\n";
    	}
    if (isEmpty(theForm.address.value)){
    	why += "You need to enter an Address.\n";
    	}
    if (isEmpty(theForm.city.value)){
    	why += "You need to enter a City.\n";
    	}
    if (theForm.country.selectedIndex == 0){
    	if (theForm.state.selectedIndex == 0){
    		why += "You need to select a State.\n";
    	}
    }
    if (isEmpty(theForm.zip.value)){
    	why += "You need to enter a Zip Code.\n";
    	}
    if (isEmpty(theForm.phone.value)){
    	why += "You need to enter a Phone Number.\n";
    	}
    if (!checkEmail(theForm.email.value)){
    	why += "You need to enter a valid Email Address.\n";
    	}else{
    		if(theForm.email.value != theForm.emailconfirm.value){
   		 		why += "Your Email Addresses don't match.\n";
   		 	}
    	}
    if (isEmpty(theForm.username.value)){
    	why += "You need to enter a Username.\n";
    	}
	
    if (isEmpty(theForm.password.value)){
    	why += "You need to enter a Password.\n";
    	}else{
    		if(theForm.password.value.length < 5 || theForm.password.value.length > 15){
   		 		why += "Your Password must be between 5 and 15 characters long.\n";
    		}
    		if(theForm.password.value != theForm.passwordconfirm.value){
   		 		why += "Your Passwords don't match.\n";
   		 	}
    	}
    if (isEmpty(theForm.initials.value)){
    	why += "You need to enter your initials, accepting the terms and conditions of service.\n";
    	}
    if (why != "") {
       alert(why);
       return false;
    }else{
    	theForm.action=go;
		return true;
	}
}

function checkEmail(strng){
var emailFilter=/^.+@.+\..{2,3}$/;
if (!(emailFilter.test(strng))) { 
      return false;
}else{
	return true;
	}
}

function isEmpty (strng) {
 var error = "";
 if (strng == "") {
   return true;
 }else{
 	return false;
 }
}
