function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.conName);
  reason += validateEmail(theForm.conEmail);
  reason += validatePhone(theForm.conPhone);
  reason += needPhone(theForm.conPhone);
  reason += validateEmpty(theForm.conMessage);
      
  if (reason != "") {
  document.getElementById('errorMessage').innerHTML='<b>Please correct the following fields:<br></b>' + reason + '<br><br>';
    document.getElementById('errorMessage').style.color='#FF0000';
    //alert("Please correct the following fields:\n" + reason);
    return false;
  }

 
    document.getElementById('errorMessage').innerHTML='';
    document.getElementById('errorMessage').style.color='#000066';
  //alert("All fields are filled correctly");
  return true;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red'; 
        error = " &nbsp;<em>- Enter your message.</em><br>"
		//error = "Please enter your message.\n"
    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';    }
    return error;  
}
function validateName(fld) {
    var error = "";
    var illegalChars = /[0-9]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
        error = " &nbsp;<em>- Enter your name.</em><br>";
		//error = "Please enter your name.\n";
    } else if ((fld.value.length < 2)) {
        error = " &nbsp;<em>- Enter a valid name.</em><br>";
		//error = "Please enter a valid name. \n";
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
    } else if (illegalChars.test(fld.value)) {
        error = " &nbsp;<em>- Enter only non-numeric characters for your name.</em><br>";
		//error = "Please enter only Letters and Spaces for your name.\n";
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';

    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';
    }
   return error;
}  

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
    } 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 = '#ffffcc';
		fld.style.color = 'Red';
    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';    }
   return error;
}  
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
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 = '#ffffcc';
		fld.style.color = 'Red';
        error = " &nbsp;<em>- Enter your email address.</em><br>";
		//error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
        error = " &nbsp;<em>- Enter a valid email address.</em><br>";
		//error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
        error = " &nbsp;<em>- The email address contains illegal characters.</em><br>";
		//error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\+\e\x\t\X\ ]/g, '');    

   if (fld.value == "") {
       // error = "Please enter your phone number.\n";
       // fld.style.background = '#ffffcc';
		//fld.style.color = 'Red';
    //} else if (isNaN(parseInt(stripped))) {
      //  error = " &nbsp;<em>- The phone number contains illegal characters.</em><br>";
		//error = "The phone number contains illegal characters.\n";
        //fld.style.background = '#ffffcc';
		//fld.style.color = 'Red';
    } else if (!(stripped.length > 9)) {
        error = " &nbsp;<em>- The phone number is the wrong length. <br> &nbsp;&nbsp; (Make sure you included an area code.)</em><br>";
		//error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';    }
    return error;
}

function needPhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
	
	var reqPhone

	for (var i=0; i<document.theForm.conPref.length; i++)  { 
		if (document.theForm.conPref[i].checked)  {
		
		reqPhone = document.theForm.conPref[i].value
		
		} 
	} 

    if ((fld.value == "") && (reqPhone == "phone")) {
        error = " &nbsp;<em>- Enter your phone number.</em><br>";
		//error = "Please enter your phone number.\n";
        fld.style.background = '#ffffcc';
		fld.style.color = 'Red';
    } else {
        fld.style.background = 'White';
		fld.style.color = 'Black';    } 
    return error;
}



function checkPhone() { 
 var phone = document.getElementById('phone'); 
 var label = getLabelForId('phone'); 
 var digits = phone.value.replace(/[^0-9]/ig, ''); 
 if (!digits) { 
   return; 
 } 
 if (digits.length == 10) { 
   phone.value = '(' + digits.substring(0, 3) + ') ' +  
     digits.substring(3, 6) + '-' +  
     digits.substring(6, 10); 
 } else { 
   phone.value = digits; 
 } 
} 

