﻿function validate(theForm) 
{

var reason = "";

  reason += validateName(theForm.name);
  
  reason += validateEmail(theForm.email);
  
  reason += validateMessage(theForm.comment);
  
  /*reason += validateCountries(theForm.country);*/
   

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

    return false;    
    }
    
  return true;
}

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

	reason += validateName(theForm.name);
	reason += validateEmail(theForm.email);
	reason += validateMessage(theForm.message);
	reason += validateCountries(theForm.country);
  
  

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

    return false;    
    }
    
  return true;

}

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

   if (fld.value == "") {
        error = "Select your country.\n";
        fld.style.background = '#FFFFCC';
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}


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

   if (fld.value == "") {
        error = "Enter your  name.\n";
        fld.style.background = '#FFFFCC';
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}

/** Email **/

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 == "") {
        error = "Enter email.\n";
        fld.style.background = '#FFFFCC';
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "Please enter a valid email address.\n";
        fld.style.background = '#FFFFCC';
        
    } else if (fld.value.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";

    } else {
        fld.style.background = 'white';
       
    }
    return error;

}

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


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

   if (fld.value == "") {
        error = "Enter your comment.\n";
        fld.style.background = '#FFFFCC';
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}

