//////////////////////////////////////////////////////////////////////////////////////////
function Trim(str)
//	trims whitespace from the front and back of a string
  {
	var s = new String(str);
	s = s.replace(/^\s+/,"");
	s = s.replace(/\s+$/,"");
	return s;
  }
  
////////////////////////////////////////////////////////////////////////////////////////

function isBlank(testStr)
//	returns true if text is blank or all spaces
  {
	 
	testStr = Trim(testStr);

	if (testStr.length == 0) 
	 	{        // nothing entered?
			  return true;
		}
			  
	return false;
 }
  
////////////////////////////////////////////////////////////////////////////////////////

function isNumeric(testNumber)
//returns true if text is numeric
{
	testNumber = Trim(testNumber);

	for (var i = 0; i < testNumber.length; i++)
		{
		  if (!(testNumber.charAt(i) >= "0" && testNumber.charAt(i) <= "9"))
			{	
			  return false;
			} 
 		}
	
	return true;
 }

////////////////////////////////////////////////////////////////////////////////////////
//function added 8/23/2002 checks to see if a number is a decimal
//at some point will put in added functionality to test or restrict # of decimal places
function isDecimal(testNumber)
//returns true if text is decimal
{
	if (isBlank(testNumber)){							//test for blank
	  	return false;
	 }
	
	var countDecimal = 0;
	for (var i = 0; i < testNumber.length; i++)
		{
		  if (!(isNumeric(testNumber.charAt(i))))		//check to see if numeric
			{	
				if (!(testNumber.charAt(i) == "."))	//check for decimal
			    {
				  //alert(testNumber.charAt(i)+ ' not 0-9 or .');
				  return false;						//was niether number or decimal
				}
		  		else								//check to see if only decimal entered
			    {
				countDecimal++;
				if ((testNumber.length == 1) || (countDecimal > 1))
				  {
					//alert("testNumber.length == " + testNumber.length);
					return false;					//entered only decimal or more than one in string
				  }		
				}	
			} 
 		}
	
	return true;
 }

///////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////
//function added 9/26/2002 checks to see if a number is a decimal with two places
function isMoney(testNumber)
//returns true if text is decimal with two places
{
	if (isBlank(testNumber)){							//test for blank
	  	return false;
	 }
	
	var countDecimal = 0;
	for (var i = 0; i < testNumber.length; i++)
		{
		  if (!(isNumeric(testNumber.charAt(i))))	//check to see if numeric
			{	
				if (!(testNumber.charAt(i) == "."))	//check for decimal
			    {
				  //alert(testNumber.charAt(i)+ ' not 0-9 or .');
				  return false;						//was niether number or decimal
				}
		  		else								//check to see if only decimal entered
			    {
				countDecimal++;
				if ((testNumber.length == 1) || (countDecimal > 1))
				  {
					//alert("testNumber.length == " + testNumber.length);
					return false;					//entered only decimal or more than one in string
				  }		
				}	
			} 
 		}
	if (countDecimal > 0)
		{
			var intPosition = testNumber.indexOf('.');// find the first /
			var tempNumber = new String(testNumber.substring(intPosition + 1, testNumber.length));// dd/yy(yy)
			if (tempNumber.length != 2)
				{
					return false;					//entered anything but two digits after decimal
				}
		}
	
	return true;
 }

///////////////////////////////////////////////////////////////////////////////////////

function isDate(testDate)
{
//	returns false for an invalid date
	
	var myRegExp = /^(\d{1,2}[/]\d{1,2}[/]\d{4})$/;

	var strTempstr;
	var intPlaceHolder;
	var intYear;
	var intMonth;
	var intDay;
	var errSW = 0;
	var sErrMsg;
	
	sErrMsg = '';
	dateTestDate = new String(testDate);//  convert to string so we can use string functions
	
	intPlaceHolder = dateTestDate.indexOf('/');// find the first /
	intMonth = dateTestDate.substring(0, intPlaceHolder);// month
	strTempstr = new String(dateTestDate.substring(intPlaceHolder + 1, dateTestDate.length));// dd/yy(yy)
	
	//we need to break out the day and month, year is the leftover
	intPlaceHolder = strTempstr.indexOf('/');// find the first /
	intDay = strTempstr.substring(0, intPlaceHolder);// day
	intYear = strTempstr.substring(intPlaceHolder + 1, strTempstr.length);
	
	//basic error checking
	if (isNumeric(intYear))
	  {   
	    if (intYear < 1753)
	       {
	           sErrMsg = 'You must enter a date between 1/1/1753 and 12/31/9999.';
			   errSW = 1;
	       }
      }
     if (isNumeric(intDay))
	   {  
	   	  if (intDay < 1 || intDay > 31) 
	   	  	 {
			  sErrMsg = 'Day must be between 1 and 31.';
	   	   	  errSW = 1;
	   		 }
	   } 
	if (isNumeric(intMonth))
	   {
	   	  if (intMonth < 1 || intMonth > 12) 
	   	  	 {  
	   	  	 	sErrMsg = 'Month must be between 1 and 12.';
				errSW = 1;
	   		 }
		}
	  	
	//advanced error checking

	// months with 30 days
	if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11)
	   {
		    if (intDay == 31) 
			   {
			   	sErrMsg = 'There is no day 31 in this month.';
			   	 errSW = 1;
			   }
	   }

	// february, leap year
	if (intMonth == 2)
	   {
		// feb
		var g = parseInt(intYear/4);
		
		if (intDay > 29) 
		   {	
		   	sErrMsg = 'There are only 28 days in February.';
		     errSW = 1;
			 
		   }
		   
		if (intDay == 29 && ((intYear/4)!=parseInt(intYear/4))) 
		   {
		   	  sErrMsg = 'This is not a leap year.';
			  errSW = 1;
			  
		   }
		   
		}
	//Format check
	 if (!(myRegExp.test(testDate))) 
	    {
	      sErrMsg = 'Date must be in mm/dd/yyyy format and mm, dd, yyyy must be numeric.';
		  errSW = 1;
	    }

	if (errSW == 1)
	   {
		  alert (sErrMsg);
		  return false;
	    }
	else
		{
			
		  return true;
	    }

}  
////////////////////////////////////////////////////////////////////////////////////////

function isEmail(emailTest)
//returns true if valid email
  {
	//checks for text, then an @, then any amount of sections with text followed by a dot, then text
	//the below version is the same as above except it will allow whitespace
	///^.+@([^\.]+\.)+[^\.]+$/

	//Safari version removes the vertical tab \v part which doesn't seem to work in Safari
	var strBrowserVersion
	strBrowserVersion = navigator.appVersion
	if (strBrowserVersion.indexOf("Safari") == -1)
	  {
		var myRegExp = /^\S+@([^ \f\n\r\t\v\.]+\.)+[^ \f\n\r\t\v\.]+$/
	  }
	else
	  {
		var myRegExp = /^\S+@([^ \f\n\r\t\.]+\.)+[^ \f\n\r\t\.]+$/
	  }
	
	if (!(myRegExp.test(emailTest)))
	  {
		alert('E-mail Address must be in the correct format with text followed by an @ followed by text separated by periods and containing no spaces.');
		return false;
	  }
   return true
  }
  
////////////////////////////////////////////////////////////////////////////////////////

function isPassword(passwordTest)
//returns true if valid password
  {
	//first part matches at least 1 number followed by at least 1 non-number or the reverse order
	//second part looks for a negative match to any of the unacceptable characters
	//third part looks for from 8-10 characters

	//Safari version removes the vertical tab \v part which doesn't seem to work in Safari
	var strBrowserVersion
	strBrowserVersion = navigator.appVersion
	if (strBrowserVersion.indexOf("Safari") == -1)
	  {
		var myRegExp = /\d+[^ \d\\\/:\*\?""<>|'%\+\. \f\n\r\t\v]+|[^ \d\\\/:\*\?""<>|'%\+\. \f\n\r\t\v]+\d+/
		var myRegExp2 = /[\\\/:\*\?""<>|'%\+\. \f\n\r\t\v]+/
	  }
	else
	  {
		var myRegExp = /\d+[^ \d\\\/:\*\?""<>|'%\+\. \f\n\r\t]+|[^ \d\\\/:\*\?""<>|'%\+\. \f\n\r\t]+\d+/
		var myRegExp2 = /[\\\/:\*\?""<>|'%\+\. \f\n\r\t]+/
	  }
	
	var myRegExp3 = /.{8,10}/
	if (!(myRegExp.test(passwordTest)) || (myRegExp2.test(passwordTest)) || !(myRegExp3.test(passwordTest)))
	  {
		alert('Password must be at least 8 characters long and contain at least one number, at least one non-number, and none of the characters \ / : * ? " < > | \' % + . or whitespace.');
		return false;
	  }
   return true
  }
  
///////////////////////////////////////////////////////////////////////////////////////

function isLogin(text)
//returns true if text contains no special characters
  {
  	if (text.indexOf('*') >= 0 || text.indexOf('%') >= 0 ||  
		text.indexOf('+') >= 0 || text.indexOf('\'') >= 0)
		{
			return false;
		}
	else
		{
			return true;
		}
  }
  

/////////////////////////////////////////////////////////////////////////////////////////

function isZip(zip)
// returns true if string passed is a valid zip code
{
	if (zip.length > 0) {
		if ((zip.length != 5) && (zip.length != 10)) {
			alert('Zip Code must be in either 11111 or 11111-2222 format.');
			return false;
			}

		if (zip.length == 10) {

				if (zip.charAt(5) != '-') {
					alert('A 10-character Zip Code must be in 11111-2222 format.');
					return false;
				}

				for (var a = 0; a < 5; a++) {
					if (!(isNumeric(zip.charAt(a)))) {
						alert('Zip Code must use numbers only.');
						return false;
						}
					}

				for (var b = 6; b < 10; b++) {
					if (!(isNumeric(zip.charAt(b)))) {
						alert('Zip Code must use numbers only.');
						return false;
						}
					}
			}

		if (zip.length == 5) {
			for (var c = 0; c < 5; c++) {
				if (!(isNumeric(zip.charAt(c)))) {
					alert('Zip Code must use numbers only.');
					return false;
					}
				}
			}
		}

return true;
}

////////////////////////////////////////////////////////////////////////////////////////
function isPhone(phone)
//returns true if valid phone/fax number
{
    var myRegExp = /^(\d{3}-\d{3}-\d{4})$/
      if (!(myRegExp.test(phone))) 
	    {
		    alert('Phone(Fax) number must be in 333-444-5555 format.');
		    return false;
	    }
   return true;
}

////////////////////////////////////////////////////////////////////////////////////////
function isTodayPrior(testDate)
//returns false for a future date
{
    var strTestDate = new String(testDate);//  convert to string to use string functions
	//break out the day and month, year is the leftover
	var intPlaceHolder = strTestDate.indexOf('/');// find the first /
	var intMonth = strTestDate.substring(0, intPlaceHolder);// month
	strTestDate = new String(strTestDate.substring(intPlaceHolder + 1, strTestDate.length));// dd/yy(yy)
	intPlaceHolder = strTestDate.indexOf('/');// find the first /
	var intDay = strTestDate.substring(0, intPlaceHolder);// day
	var intYear = strTestDate.substring(intPlaceHolder + 1, strTestDate.length);
	var dateTestDate = new Date(intYear,intMonth - 1,intDay);
	//set todays date
	var today = new Date();
    var year = today.getYear();
    if(year<1000) year+=1900;
     var todaydate = new Date(year,today.getMonth(),today.getDate());
 
	if (dateTestDate > todaydate)
	  {
		alert('Date cannot come after today.');
		return false;
	   }
    return true;
}
////////////////////////////////////////////////////////////////////////////////////////