// JavaScript Document

function validateTest(theForm) {
	var reason = "";
	
	reason += ValidateForm(form.qn1);
	reason += validateName(theForm.Name);
	reason += validateCName(theForm.Company_Name);
	reason += validateEmail(theForm.Email);
	reason += validateCNumber(theForm.Contact_Number);
	
	
	if (reason != "") {
	  alert("Please complete the following:\n" + reason);
	  return false;
	}
	  alert("Thank you for your time.\nWe will get back to you as soon as possible.");
	  return true;
}

 function validateName(fld) {
	var error = "";
	var illegalChars = /\W/;	// allow letters, numbers, and underscores
	
	if (fld.value == "") {
	  fld.style.background = 'Yellow'; 
	  error = "- Please enter your Name.\n";
	} else if (illegalChars.test(fld.value)) {
	  fld.style.background = 'Yellow'; 
	  error = "- Your Name contains illegal characters.\n";
	} else {
	  fld.style.background = 'White';
	} 
	return error;
}

function validateEmpty(fld) {
    var error = "";   
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "- Please complete all Questions.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function ValidateOption(fld) { 
  var error = ""; 

  if (fld[0].checked == false) && if (fld[1].checked == false) {
      error  = "- Please complete all Questions.\n"
  } else {
      return error;
  }
  return error; 
}

function ValidateForm(form){ 
  var error = "";
  
  if ( ( form.qn1[0].checked == false ) && ( form.qn1[1].checked == false ) ) { 
    error  = "- Please complete all Questions.\n" 
  } else { 
    return error; 
  } 
} 

function validateCName(fld) {
    var error = "";
    var illegalChars = /\W/;	// allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "- Please enter your Company Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "- Your Company Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

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

   if (fld.value == "") {
        error = "- Please enter your Contact Number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "- The Contact Number contains illegal characters.\n";
        fld.style.background = 'Yellow';
   } else {
        fld.style.background = 'White';
   } 
    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 = 'Yellow';
        error = "- Please enter your 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 = "- Your Email Address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
