//validate required fields
function requiredCheck(field, strLabel){
strField=field.value
if (strField == "" || strField == null){
alert(strLabel + " is a required field");
field.focus();
return false;
}
return true;
}
//end validate required fields

//validate numbers only
function numbsOnly(field, strLabel) {

if (field=="") return true;
var valid = "0123456789-() "
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1"){
alert ("You cannot use letters in the " + strLabel);
field.focus()
return false;
}
}
return true;
}
//end validate numbers only

//email validate
function emailCheck (emailCtrl, strLabel) {
var emailStr = emailCtrl.value
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var firstChars=validChars
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom="(" + firstChars + validChars + "*" + ")"
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)

if (emailStr=="") return true;

if (matchArray==null) {
	alert("Format for " + strLabel + " seems incorrect (check @ and .'s)");
	emailCtrl.focus();
	return false;
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("The username for " + strLabel + " doesn't seem to be valid.");
    emailCtrl.focus();
    return false;
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	    alert("Destination IP address for " + strLabel + " is invalid!");
	    emailCtrl.focus();
		return false;
	    }
    }
    return true;
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name for " + strLabel + " doesn't seem to be valid.");
	emailCtrl.focus();
    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) {
   alert("The address for " + strLabel + " must end in a three-letter domain, or two letter country.");
   emailCtrl.focus();
   return false;
}

if (domArr[domArr.length-1].length==2 && len<3) {
   var errStr="This address ends in two characters, which is a country"
   errStr+=" code.  Country codes must be preceded by "
   errStr+="a hostname and category (like com, co, pub, pu, etc.)";
   alert(errStr);
   emailCtrl.focus();
   return false;
}

if (domArr[domArr.length-1].length==3 && len<2) {
   var errStr="The address for " + strLabel + " is missing a hostname!"
   alert(errStr);
   emailCtrl.focus();
   return false;
}

return true;
}
//end email validate
