//********************************************************************************
//ORIGINAL AUTHOR: David Flanagan, Javascript book from O'Reilly p 261
//
//LAST CHANGED BY: 
//
//PURPOSE: Function which returns true if a field contains only blank space
//
//PSEUDOCODE:	
//********************************************************************************		
function isblank(s) 
{

	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) 
			return false;
	}
	return true; 
}

//********************************************************************************
//ORIGINAL AUTHOR: David Flanagan, Javascript book from O'Reilly p 261
//
//LAST CHANGED BY:     CHANGED DATE: 2/8/2000
//
//PURPOSE: function to perform validation of fields on a form.   Expects that the
//			rules of validation have already been set.
//
//
//Returns:  True if the form is valid.
//			False if the form is not valid
//
//PSEUDOCODE:
//	for all elements on the form
//		if the field is a text or textarea field and not optional
//			if the field is blank, add it to the error list
//		if the field is numeric then
//			if the number is not within min and max, or not a number then
//				adds the field to the errors list
//
//********************************************************************************		
function form_validate(pForm) 
{
	var lsMsg;
	var lsEmpty_fields = "";
	var lsErrors = "";	

	for (var i = 0; i < pForm.length; i++)
	{
		var elem = pForm.elements[i];
		if (((elem.type == "text") || (elem.type == "textarea") || (elem.type == "password")) && !elem.optional)
		{
			if ((elem.value == null) || (elem.value == "") || isblank(elem.value))
			{
				lsEmpty_fields += "\n              " + elem.title;
				continue;
			}
				
			if (elem.numeric && ( (elem.min != null) || (elem.max != null)))
			{
				var v = parseFloat(elem.value);
				if (isNaN(v) || 
				   ((elem.min != null) && (v < elem.min)) ||
				   ((elem.max != null) && (v > elem.max)))
				{
					lsErrors += "- The " + elem.name + " must be a number";
					if (elem.min != null) 
						lsErrors += " that is greater than " + elem.min;
					if (elem.max != null && elem.min != null) 
						lsErrors += " and less than " + elem.max;
					else if (elem.max != null)
						lsErrors += " that is less than " + elem.max;
					lsErrors += ".\n";
				}
			}
		}
	 }
	 	
	if (!lsEmpty_fields && !lsErrors)
		return true;
	
	if(lsEmpty_fields.search("undefined") != -1)
	{
		lsMsg =  "_____________________________________________________\n\n";
		lsMsg += "The form was not submitted because of missing data.\n";
		lsMsg += "Please correct these error(s) and re-submit.\n";
		lsMsg += "_____________________________________________________\n\n";
		
		alert(lsMsg);
		return false;
	} 
	
	lsMsg =  "_____________________________________________________\n\n";
	lsMsg += "The form was not submitted because of the following error(s).\n";
	lsMsg += "Please correct these error(s) and re-submit.\n";
	lsMsg += "_____________________________________________________\n\n";
	
	if (lsEmpty_fields)
	{
		lsMsg += "- The following required field(s) are empty:" + lsEmpty_fields + "\n";
		if (lsErrors)
			lsMsg += "\n";
	}
	lsMsg += lsErrors;
	alert(lsMsg);
	return false;
}

//*******************************************************************************
//
//Purpose: checks if a text string is in the form of a valid email address
//
//Parameters: object of which the value is an email address to check
//
//Returns:   true if it is a valid email address
//			 else false
//********************************************************************************
function isEmail(poEmail)
{
    if (poEmail.value + "" == "")
		return true;
		
    if (poEmail.value.indexOf("@") + "" != "-1" &&
		poEmail.value.indexOf(".") + "" != "-1" && poEmail.value != "") 
		return true;    
    else 
		return false;
}

//*******************************************************************************
//
//Purpose: checks if the value of a passed object is a positive integer
//
//Parameters: object to check value of
//
//Returns:	true if it is a positive integer
//			else false
//********************************************************************************
function isInt(poNum) 
{
	var lsNum = poNum.value + ""; 
    
    if (lsNum == "") 
		return true;
    for (var i = 0; i < lsNum.length; i++) 
    {
        if (lsNum.charAt(i) < "0" || lsNum.charAt(i) > "9")
			return false;
    }
	return true;
}

//*******************************************************************************
//
//Purpose: checks if the value of a passed object is a valid zip code
//			Will accept #####
//						#####-####
//						##### ####
//						#########
//Parameters: object to check value of
//
//Returns:	true if it is a zip code
//			else false
//********************************************************************************
function isZip(poNum)
{
	var lsNum = poNum.value + "";
	
	if (lsNum == "")
		return true;
	
	if((lsNum.length == 5) || (lsNum.length == 9))
	{
		for (var i = 0; i < lsNum.length; i++) 
		{
			if (lsNum.charAt(i) < "0" || lsNum.charAt(i) > "9")
				return false;
		}
	}
	else if (lsNum.length == 10)
	{		
		for (var i = 0; i < 10; i++) 
		{
			if (i == 5)
			{
				if ( (lsNum.charAt(i) != "-") && (lsNum.charAt(i) != " "))
					return false;
			}
			else if ((lsNum.charAt(i) < "0") || (lsNum.charAt(i) > "9"))
				return false;
		}
	}
	else
		return false;

	return true;
}

//*******************************************************************************
//
//Purpose: checks if the value of a passed object is a phone number
//			Will accept: ###x###x####   where x = - or ' ' len 12
//						 ##########						   len 10	
//						(###)x###x####	 where x = '-' or ' ' len 14
//			
//Parameters: object to check value of
//
//Returns:	true if it is a phone number
//			else false
//*********************************************************************************
function isPhone(poPhone) 
{
	var lsPhone = poPhone.value + "";
	
    if (lsPhone.length == 0) 
		return true;
    else if (lsPhone.length == 14)
    {
		for (var i = 0; i < 14; i++) 
		{
			if (i == 0)
			{
				if (lsPhone.charAt(i) != "(" )
					return false;
			}
			
			else if (i==4)
			{
				if (lsPhone.charAt(i) != ")")	
					return false;
			}
			else if ( (i==5) || (i==9) )
			{
				
				if ( (lsPhone.charAt(i) != "-") && (lsPhone.charAt(i) != " ") )
				{
					return false;
				}
			}	
			else if ( (lsPhone.charAt(i) < "0") || (lsPhone.charAt(i) > "9") ) 
			{
				return false;
			}
		}
    }        
	else if (lsPhone.length == 12)
	{
		for (var i = 0; i < 12; i++) 
		{
			if ((i==3) || (i==7))
			{
				if ((lsPhone.charAt(i) != "-") && (lsPhone.charAt(i) != " "))
					return false;
			
			}	
			else if ((lsPhone.charAt(i) < "0") || (lsPhone.charAt(i) > "9")) 
				return false;
		}
	}
	else if (lsPhone.length == 10)
	{
		for (var i = 0; i < lsPhone.length; i++) 
		{
			if ((lsPhone.charAt(i) < "0") || (lsPhone.charAt(i) > "9")) 
				return false;
		}	
	}
	else
		return false;
    
	return true;
}

//*******************************************************************************
//
//Purpose: Checks if the three numbers passed in are a valid date
//			
//Parameters: year, month, day - all select list objects
//
//Returns:	true if it is a valid date,
//			else returns false
//*********************************************************************************
function isDate(poYear, poMonth, poDay)
{
	var liYear;
	var liMonth;
	var liDay;
		
	liYear = poYear.options[poYear.selectedIndex].value;
	liMonth = poMonth.options[poMonth.selectedIndex].value;
	liDay = poDay.options[poDay.selectedIndex].value;
				
	if (liMonth < 1 || liMonth > 12) 
		return false;
	if (liDay < 1 || liDay > 31)
		return false;
	
	if ((liMonth==4 || liMonth==6 || liMonth==9 || liMonth==11) && liDay==31)
		return false;

	if (liMonth == 2) 
	{ 
		var isleap = (liYear % 4 == 0 && (liYear % 100 != 0 || liYear % 400 == 0));
		if (liDay>29 || (liDay==29 && !isleap)) {
			return false;
	  }
	}
	return true; 
		
}

//*******************************************************************************
//
//Purpose: Checks if the date string passed in are a valid date
//			
//Parameters: date (mm/dd/yyyy)
//
//Returns:	true if it is a valid date,
//			else returns false
//*********************************************************************************
function isDate2(poDate)
{
   var lsDate,liMonth,liDay,liYear;
   var arrDate=new Array(3);
   var liSlash=0;
 
   lsDate=poDate;
   
   //Check there are two slashes in the date
   for (var i=0; i < lsDate.length; i++){
     if (lsDate.charAt(i)=="/") 
        ++liSlash;
   }  
   if (liSlash < 2 || liSlash > 2){
    return false;
   }
   
   //Check month,day,year are interger  
   arrDate=lsDate.split("/")
   for (var j=0; j < 3 ; j++)
   {
      lsNum=arrDate[j];
      for (var i = 0; i < lsNum.length; i++) 
      {
          if (lsNum.charAt(i) < "0" || lsNum.charAt(i) > "9")
			return false;
       }//for i
    }//for j
    
   liMonth=arrDate[0];
   liDay=arrDate[1];
   liYear=arrDate[2];
   
   //
   if (liMonth < 1 || liMonth > 12) 
		return false;
	if (liDay < 1 || liDay > 31)
		return false;
	
	if ((liMonth==4 || liMonth==6 || liMonth==9 || liMonth==11) && liDay==31)
		return false;

	if (liMonth == 2) 
	{ 
		var isleap = (liYear % 4 == 0 && (liYear % 100 != 0 || liYear % 400 == 0));
		if (liDay>29 || (liDay==29 && !isleap)) {
			return false;
	  }
	}
	return true; 
}

//*******************************************************************************
//Purpose: Compare two date value poDate1 and poDate2.
//         Return false if poDate1 is later than poDate2.			
//Parameters: date (mm/dd/yyyy)
//
//Returns:	true if second date is later than first one,
//			else returns false
//*********************************************************************************
function CompareDate(poDate1,poDate2)
{
	var lsDate,liMonth,liDay,liYear;
	var lsDate1,lsDate2;
	var laTemp;
	var laDate1=new Array(3),laDate2=new Array(3);
	var liSlash=0;
	var ldtPassed, ldtNow;
 
	lsDate1=poDate1;
	lsDate2=poDate2;
   
	//parse date value into the array to get month,day year value
	laTemp=lsDate1.split("/");
	laDate1[0]=laTemp[2]; //year
	laDate1[1]=laTemp[0]; //month
	laDate1[2]=laTemp[1]; //day
   
	laTemp=lsDate2.split("/");
	laDate2[0]=laTemp[2];
	laDate2[1]=laTemp[0];
	laDate2[2]=laTemp[1];
	    
	laDate1[0]=FormatYear(laDate1[0]);
	laDate2[0]=FormatYear(laDate2[0]);
	  
	//convert string to integer and compare, and return proper value.
	for (var j=0; j<3; j++)
	{
		liDatePart1=parseInt(laDate1[j]);
		liDatePart2=parseInt(laDate2[j]);	     
		if (liDatePart1 < liDatePart2) 
		{ 
		   return true;
		}
		else if (liDatePart1 > liDatePart2)
		{
			return false;
		}
	}	//for  
	return false;
}

function FormatYear(psYear)
{
	var lsYear;
	lsYear=psYear;
	if (lsYear < 50 )  
		lsYear="20" + lsYear;
	return lsYear;
}


//***********************************************************************************************
//Function which returns true if the string passed contains characters not permited when searching
//  The chars searched for at '%' and '_'
//***********************************************************************************************
function BadSearchChars(s) 
{
	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (c == '%' || c == '_') 
			return true;
	}
	return false; 
}


function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


