// JavaScript Document to validate form fields

function validateContactForm() {
  var isValid = true;
  var myForm = document.getElementById("contactUs");	

  if (isBlank(myForm.name)) { 
     alert("Please enter your name name")
	 myForm.name.focus() ;
     isValid = false;
  }
  else if (isBlank(myForm.phone)) {
     alert("Please enter your phone number")
	 myForm.phone.focus() ;
     isValid = false;
  }
  else if (!chLen(myForm.phone,10,11)) {
  	 alert("Please check your phone number")
	 myForm.phone.focus() ;
     isValid = false;
  }
  else if((!isEmail(myForm.email)) || (isBlank(myForm.email))) {
     alert("Please enter your email address")
	 myForm.email.focus() ;
     isValid = false;
  }
  else if (isBlank(myForm.company)) { 
     alert("Please enter your company name")
	 myForm.company.focus() ;
     isValid = false;
  }
  else if (isBlank(myForm.website)) { 
     alert("Please enter your website address")
	 myForm.website.focus() ;
     isValid = false;
  }
  else if (isBlank(myForm.message)) { 
     alert("Please provide details about your enquiry")
	 myForm.message.focus() ;
     isValid = false;
  }

  if (isValid) {
     myForm.submit () ; 
  }
  else {
	  return false;
  }

}

function validateClientLoginForm() {
	
  var isValid = true;
  var myForm = document.getElementById("clientLogin");	
 
  if (isBlank(myForm.userID)) { 
     alert("Please enter your user id")
	 myForm.userID.focus() ;
     isValid = false;
  }
  else if (isBlank(myForm.password)) {
     alert("Please enter your password")
	 myForm.password.focus() ;
     isValid = false;
  }

  if (isValid) {
     myForm.submit () ; 
  }
  else {
	  return false;
  }

}

function chLen(field,strLNumber,strUNumber) {
	var string = field.value ; 
    var string = string.replace(/ /g,"");

	if ((string.length < strLNumber) || (string.length > strUNumber))  {
	   return false;
	}
	else {
	   return true;
	}
}

function isBlank(field){
	var rc = true;
	var alphastring = field.value;
	var alphalength = alphastring.length;

	for (i=0;i<alphalength;i++){
		var testchar = alphastring.charAt(i);
		if ( !(testchar == ' ')){
			rc = false;
			break;
		}
	}
	return rc;
}
function isEmail(elm) {
	// 	Purpose:	To add extra validation to email. Do not accept illegal characters

		var requiredChars = false;
		var legalChars = false;
		
		// first check that required characters exist
		
		if	(elm.value.indexOf("@") + "" != "-1" && elm.value.indexOf(".") + "" != "-1" && elm.value != ""){
			requiredChars = true;
			}
		else {
			requiredChars = false;
		}
		
		// next, check there are no illegal characters
		// NB: this list can be amended as needed
		
		var aIllegalChars = new Array(" ","+","#",",","'","~","<",">","/","\\","{","}","[","]")
		for (var i=0;i<aIllegalChars.length;i++)
			{
			if (elm.value.indexOf(aIllegalChars[i]) != -1) {
				legalChars = false;
				break;
				}
			else legalChars = true;	}
	
		// return true value only if required and legal chars
	
		if (requiredChars == true && legalChars == true) {
			return true;
			}
		else {
			return false;
		}
}
