

function isBlank(s) {
	var c = "";
	for (var i = 0; i < s.length; i++) {
 		c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) {
			return false;
		}
	}
	return true;
}

function isEmailInvalid(str) {
	var reg1 = /^.+@.+\..{2,4}$/;
	var reg2 = /@.*@|\.\.|@\.|\.@|^\.|\.$|\(|\)|\|\>|\,|\;|\:|\"|\`|\s+/;
	if (!reg1.test(str) || reg2.test(str)) {
		return true;
	} else {
		return false;
	}
}

function isPhoneInvalid(areacode, phone) {
	var reg3 = /\d{3}/;
	var reg7 = /\d{3}.*\d{4}/;
	if (!reg3.test(areacode) || !reg7.test(phone)) {
		return true;
	} else {
		return false;
	}
}

function ValidateEntry(myForm) {
	var errors = new Array();
	
	var mdUserId = mdManager.getUserId();
	var mdEmail = mdManager.getUserIdEmail();
 
 	if ((mdEmail == myForm.hproEmail.value) && (mdUserId > 0)) {
   		myForm.USER_ID.value = mdUserId;
  	} else {
   		myForm.USER_ID.value = ""; 
  	}	

	//Check each required field to make sure it has data.
	
	if (isBlank(myForm.hproFirstName.value)) {
		errors.push("\tFirst Name");
	}
	
	if (isBlank(myForm.hproLastName.value)) {
		errors.push("\tLast Name");
	}
	
	if (isBlank(myForm.hproCity.value)) {
		errors.push("\tCity");
	}
	
	if (isBlank(myForm.hproState.value)) {
		errors.push("\tState");
	}
	
	if (isBlank(myForm.hproZip.value)) {
		errors.push("\tZip");
	}
	
	if (isBlank(myForm.hproPhoneAreaCode.value)) {
		errors.push("\tArea Code");
	}
	
	if (isBlank(myForm.hproPhone.value)) {
		errors.push("\tPhone Number");		
	}
	
	if ((myForm.hproGender[1].checked) == false && (myForm.hproGender[0].checked) == false) {
		errors.push("\tGender");
	}
		
	if (isBlank(myForm.hproBirthYear.value)) {
		errors.push("\tBirthYear");
	}
		
	if (isBlank(myForm.hproEmail.value)) {
		errors.push("\tEmail Address");
	}
	
	//Throw an alert if any required fields were left blank
	if (errors.length > 0) {
		alert ("The following required fields were missing:\n\n" +
			errors.join("\n"));
		return false;
		
	//Throw an alert if the email and confirm email fields don't match
	} else if (myForm.hproEmail.value != myForm.hproConfirmEmail.value) {
		alert ("Email addresses do not match");
		return false;	
	
	//Make sure that a valid email address was input
	} else if (isEmailInvalid(myForm.hproEmail.value)) {
		alert ("Email address is invalid.  Format should be like\n\n" + 
			"         \"username@somedomain.com\"       \n");
		return false;
		
	//Make sure that a valid phone number was input
	} else if (isPhoneInvalid(myForm.hproPhoneAreaCode.value, myForm.hproPhone.value)) {
		alert ("Please enter a valid area code and phone number\n");
		return false;
	
	//Throw an alert if no trade categories were selected
	} else if (!(myForm.hproRemodeling_Addition.checked) && !(myForm.hproKitchen_Bath.checked) && !(myForm.hproCustom_Home.checked) && !(myForm.hproArchitect.checked) && !(myForm.hproDesigner.checked) && !(myForm.hproAir_Heating.checked) && !(myForm.hproAttic_Basement.checked) && !(myForm.hproCleaning.checked) && !(myForm.hproDeck.checked) && !(myForm.hproDrywall_Plastering.checked) && !(myForm.hproElectrical.checked) && !(myForm.hproFlooring.checked) && !(myForm.hproFoundation_Retain.checked) && !(myForm.hproGlass_Screens.checked) && !(myForm.hproGutter_Sheet_Metal.checked) && !(myForm.hproHandyman.checked) && !(myForm.hproHome_Security.checked) && !(myForm.hproInsulation.checked) && !(myForm.hproLandscaping.checked) && !(myForm.hproPainting.checked) && !(myForm.hproPaving_Patio.checked) && !(myForm.hproPet_Control.checked) && !(myForm.hproPlumbing.checked) && !(myForm.hproRoofing.checked) && !(myForm.hproSiding.checked) && !(myForm.hproTile_Stone.checked) &&	!(myForm.hproWindow_Door.checked) && !(myForm.hproOther.checked)) {
		alert ("At least one trade category must be selected.");
		return false;
	
	//Otherwise, let the form submission pass through
	} else {
		return true;
	}			
}	

//===================================================================