function numOnly(e) {
	var charCode = (e.which) ? e.which : event.keyCode;
	if (charCode > 32 && ((charCode < 48 || charCode > 57) && charCode !=40 && charCode !=41 && charCode !=45 && charCode !=46 && charCode !=120)) 
		return false; 
	return true; 
}

function validateForm(theForm) {
var reason = "";

  reason += validateName(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validateTel(theForm.tel);
  reason += validateSubject(theForm.subject);
  reason += validateMessage(theForm.message);
  reason += validateCode(theForm.code);
      
  if (reason != "") {
    alert("There were errors in the form:\n" + reason);
    return false;
  }

  return true;
}

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

function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Your name is required.\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 = "Your email is required.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "The email you entered is invalid.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email you entered contains invalid characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateTel(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Your Phone # is required.\n"
    } else if (fld.value.length < 10) {
        fld.style.background = 'Yellow';
        error = "The Phone # you entered is invalid.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateSubject(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You have to enter a subject for the email.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateMessage(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You can not send a blank email.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCode(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You have to complete the image verification.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
