// JavaScript Document
function emailCheck(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	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);
	var msgAlert=""
	if (matchArray==null) {
		return ("Email address seems incorrect (check @ and .'s)");
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
		return ("Ths username contains invalid characters.");
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		return ("Ths domain name contains invalid characters.");
	   }
	}

	if (user.match(userPat)==null) {
		return ("The username doesn't seem to be valid.");
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return ("Destination IP address is invalid!");
		   }
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return ("The domain name does not seem to be valid.");
		   }
		}
		if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			return ("The address must end in a well-known domain or two letter " + "country.");
		}
		if (len<2) {
			return ("This address is missing a hostname!");
		}
		return true;
	}
	
function validateContact () {
	var errorMsg = "";
	var valEmail = emailCheck(document.contactus.email.value);
	if (document.contactus.title.value==""){errorMsg += "\n\tTitle";}
	if (document.contactus.firstname.value==""){errorMsg += "\n\tFirst name";}	
	if (document.contactus.surname.value==""){errorMsg += "\n\tSurname";}
	if (document.contactus.position.value==""){errorMsg += "\n\tPosition";}
	if (document.contactus.company.value==""){errorMsg += "\n\tCompany";}
	if (document.contactus.phone.value==""){errorMsg += "\n\tTelephone";}
	if (valEmail!=true){errorMsg += "\n\tEmail: " + valEmail;}
	if (document.contactus.enquiry.value==""){errorMsg += "\n\tEnquiry";}
	if (errorMsg != ""){
		msg = "______________________________________________\n\n";
		msg += "Your request has not been submitted because the form\n";
		msg += "has not been completed properly.\n\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "_____________________________________________\n\n";
		msg += "Please correct the following field(s): -\n";	
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	return true;
}

function validateCaseStudies () {
	var errorMsg = "";
	var valEmail = emailCheck(document.casestudies.email.value);
	if (document.casestudies.title.value==""){errorMsg += "\n\tTitle";}
	if (document.casestudies.firstname.value==""){errorMsg += "\n\tFirst name";}	
	if (document.casestudies.surname.value==""){errorMsg += "\n\tSurname";}	
	if (document.casestudies.position.value==""){errorMsg += "\n\tPosition";}
	if (document.casestudies.company.value==""){errorMsg += "\n\tCompany";}
	if (document.casestudies.phone.value==""){errorMsg += "\n\tTelephone";}
	if (valEmail!=true){errorMsg += "\n\tEmail: " + valEmail;}
	if (document.casestudies.enquiry.value==""){errorMsg += "\n\tEnquiry";}
	if (errorMsg != ""){
		msg = "______________________________________________\n\n";
		msg += "Your request has not been submitted because the form\n";
		msg += "has not been completed properly.\n\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "_____________________________________________\n\n";
		msg += "Please correct the following field(s): -\n";	
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	return true;
}

function validateDownload () {
	var errorMsg = "";
	var valEmail = emailCheck(document.download.email.value);
	if (document.download.title.value==""){errorMsg += "\n\tTitle";}
	if (document.download.firstname.value==""){errorMsg += "\n\tFirst name";}	
	if (document.download.surname.value==""){errorMsg += "\n\tSurname";}	
	if (document.download.company.value==""){errorMsg += "\n\tCompany";}
	if (valEmail!=true){errorMsg += "\n\tEmail: " + valEmail;}
	if (errorMsg != ""){
		msg = "______________________________________________\n\n";
		msg += "Your request has not been submitted because the form\n";
		msg += "has not been completed properly.\n\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "_____________________________________________\n\n";
		msg += "Please correct the following field(s): -\n";	
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	return true;
}

function validateRefer () {
	var errorMsg = "";
	var valSenderEmail = emailCheck(document.refer.senderemail.value);
	var valRecipientEmail = emailCheck(document.refer.recipientemail.value);
	if (document.refer.sendername.value==""){errorMsg += "\n\tSender name";}
	if (valSenderEmail!=true){errorMsg += "\n\tSender email: " + valSenderEmail;}
	if (document.refer.recipientname.value==""){errorMsg += "\n\tRecipient name";}
	if (valRecipientEmail!=true){errorMsg += "\n\tRecipient email: " + valRecipientEmail;}
	if (errorMsg != ""){
		msg = "______________________________________________\n\n";
		msg += "Your request has not been submitted because the form\n";
		msg += "has not been completed properly.\n\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "_____________________________________________\n\n";
		msg += "Please correct the following field(s): -\n";	
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	return true;
}

function printpg() {
	window.print();
}

function bookmarkpg() { 
  var bookmarktitle = document.title;
  var bookmarkurl = document.location;
  var netscape
  netscape="Attention Netscape Users. To Bookmark this page please click the page you would like to bookmark and press CTRL+D.";
  if (window.sidebar&&window.sidebar.addPanel) { 
    alert(netscape);
  } else if (document.all) {
    window.external.AddFavorite(bookmarkurl, bookmarktitle);
  } else if (window.opera && window.print) {
    return true;
  }
}
