function validateFormOnSubmit(theForm) {
	var reason = "";
	
	reason += validateName(theForm.name,"Please Enter First Name");
	reason += validateEmpty(theForm.company_name,"Please Enter Company Name");  
	reason += validateEmail(theForm.email);  
	reason += validateEmpty(theForm.description,"Please describe the service you seek from Zenith");
	
	if (reason != "") {
		alert(reason);
		//alert("Following fields are mandatory:\n" + reason);
		return false;
	}

	return true;
}


//empty field validation
function validateEmpty(fld,fld_desp) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ccc'; 
        error = fld_desp + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}


//name validation
function validateName(fld,fld_desp) {
	//alert("hi...............")
    var error = "";
   // var illegalChars = /\W/; // allow letters, numbers, and underscores
 	var illegalChars = /^[a-zA-Z ]+$/; // allow only letters
    if (fld.value == "") {
        fld.style.background = '#ccc'; 
        error = fld_desp+"\n";
    } 
	/*else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } */
	else if(!fld.value.match(illegalChars)){
		fld.style.background = '#ccc'; 
        error = "The name field contains illegal characters.\n";
	}
    else {
        fld.style.background = 'White';
    }
    return error;
}

//date of birth validation
function validateBirthdate(fld){
	if (fld.selectedIndex == 0) {
        fld.style.background = '#ccc';
        error = "Select Date of Birth.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBirthmonth(fld){
	if (fld.selectedIndex == 0) {
        fld.style.background = '#ccc';
        error = "Select Month of Birth.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBirthyear(fld){
	if (fld.selectedIndex == 0) {
        fld.style.background = '#ccc';
        error = "Select Year of Birth.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}



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



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

   if (fld.value == "") {
        error = "You didn't enter a pincode.\n";
        fld.style.background = '#ccc';
    } else if (isNaN(parseInt(stripped))) {
        error = "The pincode contains illegal characters.\n";
        fld.style.background = '#ccc';
    } else if (!(stripped.length == 6)) {
        error = "The phone number is the wrong length.\n";
        fld.style.background = '#ccc';
    }
    return error;
}



//phone validation
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 = '#ccc';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#ccc';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length.\n";
        fld.style.background = '#ccc';
    }
    return error;
}
