function validateClient(theForm)	{
	// buyerName, buyerAddress, buyerPhone, realtorName, realtorCompany, realtorPhone, realtorCell, realtorEmail, agreement

		var reason = "";

			reason += validateEmpty(theForm.buyerName, 'Name');
			reason += validateEmpty(theForm.buyerAddress, 'Client Address');
			reason += validatePhone(theForm.buyerPhone, 'Phone Number');
			
			reason += validateEmpty(theForm.realtorName, 'Your Name');	
			reason += validateEmpty(theForm.realtorCompany, 'Company Name');
			reason += validatePhone(theForm.realtorPhone, 'Company Phone Number');	
			reason += validatePhone(theForm.realtorCell, 'Your Cell Number');
			  
			reason += validateEmail(theForm.realtorEmail);
			reason += validateCheckbox	(theForm.agreement, 'Agreement');
		

			if (reason != "") {
				alert("Some fields need correction:\n" + reason);
				return false;
			}

			return true;
}






///////////////////////////////////////////////////////////////////////////////////////////////////////

function validateEmpty(fld, title) {
			var error = "";
					 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = title + " is required.\n";
			} else {
				fld.style.background = 'White';
			}
			return error;  
}

function validateCheckbox(fld, title) {
			
			var error = "";
			
			if(!fld.checked) {
				error = title + " is required.\n";
				fld.parentNode.parentNode.style.background = 'Yellow'; 
			}
			else
			{
				fld.style.background = 'White';
			}
		 	/*	if(!fld.checked)
		 		{				{
					fld.style.background = 'Yellow'; 
					error = title + " is required.\n";
				}
			/*	else
				{
					fld.style.background = 'White';
				}
				*/
			return error;  
}


function validateCheckboxSet(theForm, fldset, title) {
			
		 	var reqfld = fldset.value.split(",");
			var error = "";
			var count = 0;


		 	for(i=0; i<reqfld.length; i++)
		 	{
		 		var fld = theForm[reqfld[i]];
		 		
		 		if(fld.checked) count++;

				if(count==0)
				{
					document.getElementById(fldset.name).style.background = 'Yellow'; 
					error = title + " is required. Choose at least 1.\n";
				}
				else if(count>3)
				{
					//error = title + " allows only up to 3.\n";				
				}
				else
				{
					document.getElementById(fldset.name).style.background = '#fafafa';
					error = "";
				}
			}
			return error;  
}

function validateDropdown(fld, title) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = title + " is required.\n"
			} else {
				fld.style.background = 'White';
			}
			return error;  
}

function validateUsername(fld) {
			var error = "";
			var illegalChars = /\W/; // allow letters, numbers, and underscores
		 
			if (fld.value == "") {
				fld.style.background = 'Yellow'; 
				error = "Username is required.\n";
			} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
				fld.style.background = 'Yellow'; 
				error = "The username should be 5 to 15 characters long.\n";
			} else if (illegalChars.test(fld.value)) {
				fld.style.background = 'Yellow'; 
				error = "The username contains illegal characters.\n";
			} else {
				fld.style.background = 'White';
			}
			return error;
}

function validatePassword(fld) {
			var error = "";
			var illegalChars = /[\W_]/; // allow only letters and numbers 
		 
			if (fld.value == "") {
				fld.style.background = 'Yellow';
				error = "Password is required.\n";
			} else if (fld.value.length < 7) {
				error = "The password needs at least 7 characters. \n";
				fld.style.background = 'Yellow';
			} else if (fld.value.length > 15) {
				error = "The password is too long. \n";
				fld.style.background = 'Yellow';
			} else if (illegalChars.test(fld.value)) {
				error = "The password contains illegal characters.\n";
				fld.style.background = 'Yellow';
			} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
				error = "The password must contain at least one numeral.\n";
				fld.style.background = 'Yellow';
			} else {
				fld.style.background = 'White';
			}
		   return error;
}  
function validateVerify(fldmatch, fld) {
			var error = "";
			var illegalChars = /[\W_]/; // allow only letters and numbers 
		 
			if (fld.value == "") {
				fld.style.background = 'Yellow';
				error = "Passwords do not match.\n";
			} else if ((fldmatch.value != fld.value)) {
				error = "Passwords do not match. \n";
				fld.style.background = 'Yellow';
			} else {
				fld.style.background = 'White';
			}
		   return error;
}  

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


function validatePhone(fld, title) {
			var error = "";
			var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
		
		   if (fld.value == "") {
				error = title + " is required.\n";
				fld.style.background = 'Yellow';
			} else if (isNaN(parseInt(stripped))) {
				error = title + " contains illegal characters.\n";
				fld.style.background = 'Yellow';
			} else if (!(stripped.length == 10)) {
				error = title + " is the wrong length. Make sure you included an area code.\n";
				fld.style.background = 'Yellow';
			}
			return error;
}	


function validateBirthdate(fld) {

			var error="";
			var tfld = trim(fld.value);       					// value of field with whitespace trimmed off
		    var pattern = /\d{1,2}\/\d{1,2}\/\d{4}/;
			if (fld.value == "") {
				fld.style.background = 'Yellow';
				error = "Birth date is required.\n";
			} else if (!tfld.match(pattern)) {
				fld.style.background = 'Yellow';
				error = "The birthdate contains illegal characters.\n";
			} else {
				fld.style.background = 'White';
			}
			return error;
}

function validateEmail(fld) {
			var error="";
			var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
			var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
		   
			if (fld.value == "") {
				fld.style.background = 'Yellow';
				error = "Email is required.\n";
			} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
				fld.style.background = 'Yellow';
				error = "Please enter a valid email address.\n";
			} else if (fld.value.match(illegalChars)) {
				fld.style.background = 'Yellow';
				error = "The email address contains illegal characters.\n";
			} else {
				fld.style.background = 'White';
			}
			return error;
}

