/****************************************************************
The following is property of FYI-Net.
****************************************************************/
function isEmpty(inputStr)
{
	if (inputStr == "" || inputStr == null)
		return true;
	return false;
}

function valDate(DateObj, NULLABLE)
{
	var inputDate = DateObj.value;

	x = inputDate;
	while (x.substring(0,1) == ' ') x = x.substring(1);
	while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	inputDate = x;
  
	if( isEmpty(inputDate) && NULLABLE ) return true;
	if( isEmpty(inputDate) && !NULLABLE ) {	alert("A date is required.  Please enter a date."); DateObj.focus(); return false; }

	inputDate = inputDate.replace(/-/g, "/")
	
	var delimCount = 0;
	var startIndex = 0;
	while( (startIndex=inputDate.indexOf("/", startIndex))!=-1 )
	{	delimCount++;
		startIndex++;
	}
	if( delimCount != 0 && delimCount != 2 )
	{
		alert("The date entry is not in an acceptable format.\n\nYou can enter dates in the following formats:  mmddyyyy, mm/dd/yyyy, or mm-dd-yyyy.");
		DateObj.focus();
		return false;
	}
	
	var delim1 = inputDate.indexOf("/")
	var delim2 = inputDate.lastIndexOf("/")
	DateObj.value = inputDate;
	
	if( delim1!=-1 )
	{
		var mm = parseInt(inputDate.substring(0, delim1), 10);
		var dd = parseInt(inputDate.substring(delim1+1, delim2), 10);
		var yyyy = parseInt(inputDate.substring(delim2+1, inputDate.length), 10);
	}
	else
	{
		var mm = parseInt(inputDate.substring(0,2), 10);
		var dd = parseInt(inputDate.substring(2,4), 10);
		var yyyy = parseInt(inputDate.substring(4, inputDate.length), 10);
	}
	
	if(isNaN(mm))   { alert("The month must be a number"); DateObj.focus(); return false; }
	if(isNaN(dd))   { alert("The day must be a number"); DateObj.focus(); return false; }
	if(isNaN(yyyy))	{ alert("The year must be a number"); DateObj.focus(); return false;	}
	
	if( mm < 1 || mm > 12 )
	{
		alert("Month must be entered between the range of 01 (January) and 12 (December).");
		DateObj.focus();
		return false;
	}
	if( dd < 1 || dd > 31 )
	{
		alert("Day must be entered between the range of 01 and a maximum of 31 (depending on the month and year).");
		DateObj.focus();
		return false;
	}
	if( yyyy < 100 )
	{
		if( yyyy >= 30 ) { yyyy += 1900; }
		else { yyyy += 2000; }
	}
	if( yyyy < 1000 || yyyy > 9999 )
	{
		alert("The year must be a four-digit number.");
		DateObj.focus();
		return false;
	}
	
	var months = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	
	if( (mm==4 || mm==6 || mm==9 || mm==11) && dd > 30)
	{
		alert(months[mm] + " has only 30 days.");
		DateObj.focus();
		return false;
	}
	if( (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12) && dd > 31)
	{
		alert(months[mm] + " has only 31 days.");
		DateObj.focus();
		return false;
	}
	if( mm==2 )
	{
		if( yyyy % 4 > 0 && dd > 28 )
		{
			alert("February of " + yyyy + " has only 28 days.");
			DateObj.focus();
			return false;
		}
		else
		{
			if( dd > 29 )
			{
				alert("February of " + yyyy + " has only 29 days.");
				DateObj.focus();
				return false;
			}
		}
	}
	DateObj.value = mm + "/" + dd + "/" + yyyy;
	return true;
}

function Trim(input)
{
	x = input;
	while (x.substring(0,1) == ' ') x = x.substring(1);
	while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	input = x;
	return input;
}
function valEmail(EmailObj, NULLABLE)
{
	var Email = Trim(EmailObj.value);
	if(Email == '') {
		if(!NULLABLE) {
			alert("Please enter an email address.");
			EmailObj.focus();
			return false;
		}
		else
			return true;
	}

	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=Email.match(emailPat);
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		EmailObj.focus();
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null) {
	    alert("The username doesn't seem to be valid.");
	    EmailObj.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 is invalid!");
		        EmailObj.focus();
				return false;
		    }
	    }
	    return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.");
		EmailObj.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 must end in a three-letter domain, or two letter country.");
	    EmailObj.focus();
	    return false;
	}
	if (len<2) {
	  alert("This address is missing a hostname!");
		EmailObj.focus();
	  return false;
	}
	return true;
}

function valPhone(PhoneObj, NULLABLE, ALLOWALPHA)
{
	var PhoneNumber = Trim(PhoneObj.value);
	if(PhoneNumber == '') {
		if(!NULLABLE) {
			alert("Please enter a phone number.");
			PhoneObj.focus();
			return false;
		}
		else
		return true;
	}
	var regexp = /\s/g;
	PhoneNumber = PhoneNumber.replace(regexp, "");
	var regexp = /-/g;
	PhoneNumber = PhoneNumber.replace(regexp, "");
	regexp = /\(/g;
	PhoneNumber = PhoneNumber.replace(regexp, "");
	regexp = /\)/g;
	PhoneNumber = PhoneNumber.replace(regexp, "");
	
	var regexp10a = /^\d{3}\w{7}$/;
	var regexp10b = /^\d{10}$/;
	var regexp11a = /^\d{4}\w{7}$/;
	var regexp11b = /^\w(\d{11})$/;
		
	if (PhoneNumber.length != 10 && PhoneNumber.length != 11){
		alert ("Phone must be a 10 or 11 characters.");
		PhoneObj.focus();
		return false;
	}
	
	if (ALLOWALPHA == false){
	   if ((PhoneNumber.length == 10 && !regexp10b.test(PhoneNumber)) || (PhoneNumber.length == 11 && !regexp11b.test(PhoneNumber))){
	      alert ("Phone must be numeric");
	      PhoneObj.focus();
	      return false;
	   }
	}else{	   
	   if (PhoneNumber.length == 11 && !regexp11a.test(PhoneNumber)){
	      alert ("The first 4 characters must be numeric and the rest has no special characters.");
	      PhoneObj.focus();
	      return false;
	   }
	   else if (PhoneNumber.length == 10 && !regexp10a.test(PhoneNumber)){
	      alert ("The first 3 characters must be numeric and the rest has no special characters.");
	      PhoneObj.focus();
	      return false;
	   }
	}
	
	if (PhoneNumber.length == 10) {
	   var newFormat = "(" + PhoneNumber.substring(0,3) + ")";
	   newFormat += " " + PhoneNumber.substring(3,6) + "-" +  PhoneNumber.substring(6,10);
	   PhoneObj.value = newFormat;
	   return true;
	}else{
	   var newFormat = PhoneNumber.substring(0,1) + "-";
	   newFormat += PhoneNumber.substring(1,4) + "-" +  PhoneNumber.substring(4,7);
	   newFormat += "-" +  PhoneNumber.substring(7,11);
	   PhoneObj.value = newFormat;
	}
	return true;
}
