// JavaScript Document
function validateFormOnSubmits(theForm) {
var reason = "";
  
  reason += valid_required_title(theForm.country);
  reason += valid_required_venue(theForm.state);
  reason += valid_required_date(theForm.city);
  reason += validateUsername(theForm.name);
  reason += valid_date(theForm.testinput);
  reason += validatePhone1(theForm.cno);
  //reason += validatePhone(theForm.cno2);
  reason += validateEmailofficial(theForm.email1);
  reason += validateCondition(theForm.condition);
  //reason += validateEmailpersonal(theForm.email2);	
  
if (reason != "") {
    alert("Some fields need correction:<br />" + reason);
    return false;
  }

  return true;
}

/*
function validateCondition(fld) {
	var error = "";
	
	if (fld.value == "1") {
        fld.style.background = 'White';
        error = "Please check the terms and conditions<br />";
    } else {
        fld.style.background = 'White';
    }
	  return error;

}
*/
function validateCondition(fld) {
	var error = "";
	
	if (fld.checked == false) {
        fld.style.background = 'White';
        error = "Please check the terms and conditions<br />";
    } else {
        fld.style.background = 'White';
    }
	  return error;

}


function valid_required_title(fld) {
	var error = "";
	
	if (fld.value == "Select Program") {
        fld.style.background = 'White';
        error = "You must enter Program Title.<br />";
    } else {
        fld.style.background = 'White';
    }
	  return error;

}

function valid_required_venue(fld) {
	var error = "";
	
	if (fld.value == "Select Venue") {
        fld.style.background = 'White';
        error = "Program Title, Venue, Date all fields must be filled.<br />";
    } else {
        fld.style.background = 'White';
    }
	  return error;

}

function valid_required_date(fld) {
	var error = "";
	
	if (fld.value == "Select Date") {
        fld.style.background = 'White';
        error = "You must enter Program Title, Venue, Date.<br />";
    } else {
        fld.style.background = 'White';
    }
	  return error;

}



function validateEmpty(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'White';
        error = "The required field has not been filled in.<br />"
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmailpersonal(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

   if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'White';
        error = "Please enter a valid personal email address.<br />";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'White';
        error = "The personal email address contains illegal characters.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateEmailofficial(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 = 'White';
        error = "You must enter your official email address.<br />";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'White';
        error = "Please enter a valid official email address.<br />";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'White';
        error = "The email address contains illegal characters.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validatePhone(fld) {
	var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

if ((fld.value.length!=0)) 
{

	if (isNaN(parseInt(stripped)))
	{
        error = "Mobile no field cannot have alphabets. Please enter a valid contact number.<br />";
        fld.style.background = 'White';
    }
}
else
{
	fld.style.background = 'White';
}
    return error;
}


function validatePhone1(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
 error = "You must enter your official contact number.<br />";
        fld.style.background = 'White';
    }
else if ((fld.value.length < 8) || (fld.value.length > 12)) {
fld.style.background = 'White';
error = "Contact number must contain 8 to 12 numbers.<br />";
}
   else if (isNaN(parseInt(stripped))) {
        error = "Contact no field cannot have alphabets. Please enter a valid contact number.<br />";
        fld.style.background = 'White';
    }
    else {
fld.style.background = 'White';
}
    return error;
}


function validateUsername(fld) {
var error = "";
//var illegalChars = /[^a-zA-Z]/; // allow only letters
var illegalChars = /\d/; //allow only alphabets and special characters
if (fld.value == "") {
fld.style.background = '';
error = "You must enter your Name.<br />";
} /*else if ((fld.value.length < 5) || (fld.value.length > 15)) {
fld.style.background = 'White';
error = "The username is of wrong length.\n";
}*/ 
else if (illegalChars.test(fld.value)) {
        fld.style.background = ''; 
        error = "Name cannot contain numbers.Please enter only alphabets.<br />";
    }else {
fld.style.background = 'White';
}
return error;
}


function valid_date(fld)
{   var error = "";
	var valid = "0123456789/";
	var slashcount = 0;
	
	if(fld.value.length == 0)
	{
	fld.style.background = 'White';
    error = "You must enter your date of birth.<br />"
	}
else if (fld.value.length!=10) 
	{
	fld.style.background = 'White';
    error = "Invalid date! The correct date format is like '01/01/2004'.   Please try again.<br />"
	}
		for (var i=0; i < fld.length; i++)
		 {
		temp = "" + fld.substring(i, i+1);
		if (temp == "/") 
		slashcount++;
			if (valid.indexOf(temp) == "-1") 
			{
			fld.style.background = 'White';
		    error = "Invalid characters in your date.  Please try again.<br />"
			}
		else if (slashcount > 2) 
		{
		fld.style.background = 'White';
		error = "Invalid Date!  The slash character should be used with a properly formatted 8 digits like  '01/01/2004'.   Please try again.<br />"
		}
		if((fld.charAt(2)!= '/')||(fld.charAt(5) != '/'))
		{
		fld.style.background = 'White';
		error = "Invalid date! The slash character should be used with a properly formatted 8 digits like  '01/01/2004'.   Please try again.<br />"
		}
	}
	return error;


}

