// JavaScript Document

function validate(){
value1=false;
value2=false;
value3=false;
if((document.form.email.value == '') ){ //email or phone must be entered
	alert('Please enter your Email Address for us to be able to Contact You. Thanks!');
	event.returnValue=false;
}else if (document.form.email.value != '') {    
	value1 = emailCheck(document.form.email.value);
	if ((value1) == false) { //email is not mandatory but if it is entered, it has to be correct
	event.returnValue=false;
	return false;
	}
}

if ((value1) == true){
if(document.form.comment.value == ''){
	alert('Please enter a brief Comment about your issue before submiting your details');
	event.returnValue=false;
	value3=false;	
}else{
	value3=true
}
}

if (((document.form.email.value != '') && (value1)) && (value3)){ //email must be correct, also a comment is mandatory
	document.form.action = "php/contact.php"; // To implement P/R/G Post/Redirect/Get
	document.form.Submit();
}else{
event.returnValue=false;
}

}


function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)

if (matchArray==null) {
	alert("Email address seems incorrect (check @ and .'s)")
	 return false
}

var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("The username doesn't seem to be valid.")
    	 return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic  host name). */

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		  return false
	    }
    }
    return true
}

// Domain is symbolic name

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
   return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The email address must end in a three-letter domain, or two letter country.")
   return false
}

// there's a host name preceding the domain.

if (len<2) {
   var errStr="This email address is missing a hostname!"
   alert(errStr)
    return false
}
// If we've gotten this far, everything's valid!
return true
}
