// JavaScript Document
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.sender_name);
  reason += validateEmail(theForm.sender_mail);
  reason += validatePhone(theForm.sender_phone_1);
/*     for (i=0, n=theForm.Campus.length; i<n; i++) {
     if (theForm.Campus[i].checked) {
     var checkvalue = theForm.Campus[i].value;
     break;
   }
}
  reason += checkRadio(checkvalue);*/ 
  //reason += checkDropdown(theForm.recip.selectedIndex);

      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else if (document.theForm.recipient.value==0) {
  alert("Please select the Dental Office where you would like your message sent to.");
  return false;
  }
   else if (theForm.CommentsQuestions.value.length < 8) {
      alert("Please enter a \"Comment/Question\".");
      theForm.CommentsQuestions.focus();
      return false;
   }
  
 

  return true;
}

/*function checkDropdown(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't choose an option
         from the drop-down list.\n";
    }    
return error;
} */  

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field(s) have not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateName(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your Name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 35)) {
        fld.style.background = 'Yellow'; 
        error = "The name is the wrong length.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


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

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a Phone Number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    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 = "You didn't enter an Email Address.\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;
}


/*function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;    
}
*/
 
