// JavaScript Document

function validatePhone(fld) {

 var frm = document.forms.demo
 var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');   

   if (fld.value == "Contact Number"  ) {
		error = "Please Enter Phone or Mobile Number.\n";
        fld.style.borderColor = 'red'; 		

    }	else if (isNaN((stripped))) {
        error = "Invalid Contact Number.\n";
        fld.style.borderColor = 'red';
    }else if (!(stripped.length <= 11)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.borderColor = 'red';
    } else if (fld.value != "")	{ 
	fld.style.borderColor = 'grey'; 
		}
    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 == "Email Address") {
        fld.style.borderColor = 'red';
        error = "Please Enter Email.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.borderColor = 'red';
        error = "Invalid Email Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.borderColor = 'red';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.borderColor = 'grey';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 

    if (fld.value == "") {
        fld.style.borderColor = 'red';
        error = "You didn't enter a surname.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 15)) {

        error = "The surname is the wrong length. \n";
        fld.style.borderColor = 'red';
    } else if (illegalChars.test(fld.value)) {
        error = "The surname contains illegal characters.\n";
        fld.style.borderColor = 'red';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The surname must contain at least one numeral.\n";
        fld.style.borderColor = 'red';
    } else {
        fld.style.borderColor = 'grey';
    }
   return error;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /^[A-Za-z]\s$/; // allow letters, numbers, and underscores

    if (fld.value == "Fullname") {
        fld.style.borderColor = 'red'; 
        error = "Please Enter Fullname.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        fld.style.borderColor = 'red'; 
        error = "The first name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.borderColor = 'red'; 
        error = "The first name is not valid. \n";
    } else {
        fld.style.borderColor = 'grey';
    } 
    return error;
}
function validateEmpty(fld) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.borderColor = 'red'; 
        error = "Please accept Privacy Policy.\n"
    } else {
        fld.style.borderColor = 'grey';
    }
    return error;   
}
function validateTitle(fld) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.borderColor = 'red'; 
        error = "Please select title.\n"
    } else {
        fld.style.borderColor = 'grey';
    }
    return error;   
}

function checkboxValue(){
	if (document.forms.demo.Privacy_Policy.value ==""){
		document.forms.demo.Privacy_Policy.value = 1;
		}else if (document.forms.demo.Privacy_Policy.value = 1){
		document.forms.demo.Privacy_Policy.value = "";	
		}
	return;
	}
	
function privacy(fld){
//var x=document.forms["myform"]["privacy-policy"];	
	error ="";
	if (fld.checked == false){
		error = "Please Accept The Privacy Policy";	
	}
	return error;	
	}
	
function validateFormOnSubmit(theForm) {
var reason = "";
  reason += validateUsername(theForm.Fullname);
 //reason += validateTitle(theForm.title);
  reason += validateEmail(theForm.EmailAddress);
  reason += validatePhone(theForm.ContactNumber);
  reason += privacy(theForm.privacy_policy);
  //reason += validateEmpty(theForm.Contact_Time);
  //reason += validateEmpty(theForm.Privacy_Policy);
  if (reason != "") {
   alert("Some fields Need Correction:\n" + reason);
  // document.getElementById("validation").style.display = "inline";
  // document.getElementById("error").innerHTML = "<strong>Some fields need correction:</strong><br>" + reason+ "<br/>";
   return false;
  }
  return true;

}



