// Worling File : ValidateForm.js
// Developer    : Lenin Lopez
// Date			: 7/31/00 
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function roundit(num,places)
{
	if ( places > 0) 
	{
		if((num.toString().length - num.toString().lastIndexOf('.')) > (places + 1))
		{
			var Rounder = Math.pow(10, places);
			return Math.round(num * Rounder) / Rounder;
		}
		else
			return num;
	}
	else		
		return Math.round(num)
}
function formatCurrency(num,places)
{
	var curr = "$" + (roundit(num,places)).toString();
	
	//alert(curr.indexOf('.'))
	if (curr.indexOf(".") == -1 )
		curr += ".00";
	else
	{
		var currA = curr.toString().split(".");			
		if (currA[1].toString().length == 1 )
			curr += "0";
	}
	return curr;
}


function trim (s)
{
	var iLen = s.length;
	var sOut = "";
	var chr = "";

	for (var i=0; i<iLen; i++)
	{
	chr = s.charAt (i); 
	if (chr!=" ")
	{
	sOut = sOut + chr; 
	}
	}
	return sOut;
}

function isEmpty(inputStr){
	if ( inputStr == "" || inputStr == null)
		 return true
		 
	else if( (trim(inputStr).toString()).length == 0 )
		return true
  else 		
		return false

}

function isPositiveInt(inputVal){			//Checks if the input fiels contains a character other than a didgit
var inputStr = inputVal.toString()
	for ( var i = 0 ; i  < inputStr.length; i++){
		var oneChar = inputStr.charAt(i)
		if(oneChar < "0" || oneChar > "9") {
			return false
			}
	}
	return true
}

function isPositiveIntOrEmpty(inputVal){			//Checks if the input fiels is apossitive or empty 
var inputStr = inputVal.toString()
    if( isEmpty(inputStr) ){
      return true
      }
	for ( var i = 0 ; i  < inputStr.length; i++){
		var oneChar = inputStr.charAt(i)
		if(oneChar < "0" || oneChar > "9") {
			return false
			}
	}
	return true
}

function isNegativeInt(inputVal){			//Checks if the input fiels contains a character other than a didgit

var inputStr = inputVal.toString() //checks if the inpt field is a valid integer (- or +)
	for ( var i = 0 ; i  < inputStr.length; i++){
	  var oneChar = inputStr.charAt(i)
	  if (i == 0 ){
			if (oneChar == "-")
				continue
			  else return false
		}
		
		if ( oneChar < "0" || oneChar > "9"){
			return false
		}
	}
	return true
}	


function isPositiveFloat(inputVal){
var NOFDECIMALS = 2
var MAX =5000
var MIN =0
var ptFound = false
var rtStr = ""
var lfStr = ""
var inputStr = inputVal.toString()
var initialIndex = 0	  
var negativeSign = ""
	
	for ( var i = 0 ; i < inputStr.length; i++){
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
		  initialIndex = 1;
		  negativeSign = "-";
		  return "NONEG";
			continue
		}	
		if (oneChar == "."){
			ptFound = true;
			rtStr = inputStr.slice(i+1);
			lfStr = inputStr.slice(initialIndex,-rtStr.length-1)
			break;
			}
		}
		if (!ptFound ){
			if(!isPositiveInt(inputStr)&&(!isNegativeInt(inputStr)))
	        return false
	      else
	      if (parseFloat(inputStr) > MAX){
	      		return "MAX"}
				if (parseFloat(inputStr) < MIN){
					return "MIN"}
	        return true;
	  }
	    if (rtStr.length > 2){
					return "EXDEC"}
			if (isPositiveInt(rtStr)&&(isPositiveInt(lfStr))){
			  if (parseFloat(eval(negativeSign+lfStr+"."+rtStr)) > MAX){
			  		return "MAX"}
				if (parseFloat(eval(negativeSign+lfStr+"."+rtStr)) < MIN){
						return "MIN"}
						return true
			 }else {
			  	  return false;
			}
	  	
}
// New featueres added on 12/26/2001
function isValidInt_n(obj,objName,req,neg,pos,max,min){			//Checks if the input fiels contains a character other than a didgit
//( obj ) -> incomming object
//( objName ) -> name of the incomming object
//( req ) -> the value is required ( 1 -> yes; 0 -> no)
//( neg ) -> allow negative values ( 1 -> allow negative values; 0 -> do not allow negative values )
//( pos ) -> allow positive values ( 1 -> allow positive values; 0 -> do not allow positive values )
//[ max ] -> specify the maximum for the input parameter ( default is open, match DB data type 
//[ min ] -> specify the minimum for the input parameter ( default is open, match DB data type 

if ( neg == 0 && pos == 0)
{
	neg = 1;
	pos = 1;
}

if (req == 1)
{
	if(isEmpty(obj.value))
	{
		alert("Missing " + objName)
		obj.focus();
		return false;
	}
}	
var inputStr = obj.value.toString() //checks if the inpt field is a valid integer 
var inputStrBKP = inputStr;

  if ((pos == 0)&&(neg ==1)) //Test for required negative only values 
  {
    if (inputStr.length > 0)
    {
			if(inputStr.charAt(0) != "-")
			{
				alert("The value entered in the field " + objName + " must be negative");
				obj.focus();
				return false
			}
			else
			  inputStr = inputStr.substring(1,inputStr.length);
		}
	}	
  if ((pos == 1)&&(neg ==0)) //Test for required positive only values
  {
    if (inputStr.length > 0)
			if(inputStr.charAt(0) == "-")
			{
				alert("The value entered in the field " + objName + " must be positive");
				obj.focus();
				return false
			}
	}
	
  if ((pos == 1)&&(neg ==1)) //Test for both positive or negative allowed
  {
    if (inputStr.length > 0)
			if(inputStr.charAt(0) == "-")
			  inputStr = inputStr.substring(1,inputStr.length);
	}	
	
	if(!isValidInt_private(inputStr))
	{
		alert("Invalid character in the field " + objName);
	  obj.focus();
	  return false
	} 
		//Max check	  
		if (max.length > 0) 
		  		var MAX = parseInt(max)				  		
	  if (parseInt(inputStrBKP) > MAX)
		{
			alert("Value entered in field " + objName + " exceeds the Maximum allowed for this field wich is " + MAX);
		  obj.focus();
		  return false;
		}	
		//Min check	  
		if (min.length > 0) 
		  		var MIN = parseInt(min)
					  		
	  if (parseInt(inputStrBKP) < MIN)
		{
			alert("Value entered in field " + objName + " exceeds the Minimum allowed for this field wich is " + MIN);
		  obj.focus();
		  return false;
		}	
					  
	return true
}	

function isValidInt_private(inputVal){	
// private function which main objective is to aid the isValidFloat_n funtion to determine whether the digts on both sides of the decimal place are valid integers
  if (!isEmpty(inputVal))
	{
		var inputStr = inputVal.toString() //checks if the inpt field is a valid integer 
		for ( var i = 0 ; i  < inputStr.length; i++)
		{
		  var oneChar = inputStr.charAt(i)			  
			if ( oneChar < "0" || oneChar > "9")
			{
				return false
				break;			
			}
		}
		return true
	}
	else
	return true;	
}

function isValidFloat_n(obj,objName,req,neg,pos,dec,max,min){
//( obj ) -> incomming object
//( objName ) -> name of the incomming object
//( req ) -> the value is required ( 1 -> yes; 0 -> no)
//( neg ) -> allow negative values ( 1 -> allow negative values; 0 -> do not allow negative values )
//( pos ) -> allow positive values ( 1 -> allow positive values; 0 -> do not allow positive values )
//[ dec ] -> specify the number of decimals ( default is 2 decimal nubers )
//[ max ] -> specify the maximum for the input parameter ( default is open, match DB data type 
//[ min ] -> specify the minimum for the input parameter ( default is open, match DB data type 
var Negative = false;
var Decimals;
var MIN;
var MAX;
var ptFound = false
var rtStr = ""
var lfStr = ""
var objValue = obj.value;
var inputStrBKP;
var inputStr = objValue.toString()
var initialIndex = 0	  
var negativeSign = ""


//if (inputStr.charAt(0) == "$")
//  inputStr = inputStr.substring(1,inputStr.length)
inputStr = inputStr.replace("$","")
inputStr = inputStr.replace(",","")

inputStrBKP = inputStr
if (dec.length > 0 )
	Decimals = parseInt(dec);
else
	Decimals = 2;
	

	if ( neg == 0 && pos == 0)
	{
		neg = 1;
		pos = 1;
	}
		
	if (req == 1)   //-- if parameter indicated that the field is required, check for its existance
	{
		if (isEmpty(obj.value))
			{
				alert("Missing " + objName );
				obj.focus()
				return false;
			}
	}					  
  if ((pos == 0)&&(neg ==1)) //Test for required negative only values 
  {
    if (inputStr.length > 0)
    {
			if(inputStr.charAt(0) != "-")
			{
				alert("The value entered in the field " + objName + " must be negative");
				obj.focus();
				return false
			}
			else
			  inputStr = inputStr.substring(1,inputStr.length);
		}
	}	
  if ((pos == 1)&&(neg ==0)) //Test for required positive only values
  {
    if (inputStr.length > 0)
			if(inputStr.charAt(0) == "-")
			{
				alert("The value entered in the field " + objName + " must be positive");
				obj.focus();
				return false
			}
	}
	
  if ((pos == 1)&&(neg ==1)) //Test for both positive or negative allowed
  {
    if (inputStr.length > 0)
			if(inputStr.charAt(0) == "-")
			  inputStr = inputStr.substring(1,inputStr.length);
	}	
	
	for ( var i = 0 ; i < inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)		
		if (oneChar == ".")
		{
			ptFound = true;
			rtStr = inputStr.slice(i+1);
			lfStr = inputStr.slice(initialIndex,-rtStr.length-1)
			break;
		}
	}

		if ( ptFound )
		{
		  if(rtStr.length == 0 && lfStr.length == 0)
			{
				alert("Invalid float data in field " + objName);
		    obj.focus();
		    return false
		  }				
			if(!isValidInt_private(rtStr))
			{
				alert("Invalid character in decimal part of the field " + objName);
		    obj.focus();
		    return false
		  }  
			if(!isValidInt_private(lfStr))
			{
				alert("Invalid character in the integer part of the field " + objName);
		    obj.focus();
		    return false
		  }  		  
		}  
		else
			if(!isValidInt_private(inputStr))
			{
				alert("Invalid character in the field " + objName);
		    obj.focus();
		    return false
		  } 
		  
		//Decimal check
		if(rtStr.length > Decimals )
		{
			alert("Plese limit the number of decimals to " + Decimals  + " digits after the decimal place in the field " + objName );
		  obj.focus();
		  return false;
		}	
		//Max check	  
		if (max.length > 0) 
		  		MAX = parseFloat(max)				  		
	  if (parseFloat(inputStrBKP) > MAX)
		{
			alert("Value entered in field " + objName + " exceeds the Maximum allowed for this field wich is " + MAX);
		  obj.focus();
		  return false;
		}	
		//Min check	  
		if (min.length > 0) 
		  		MIN = parseFloat(min)
					  		
	  if (parseFloat(inputStrBKP) < MIN)
		{
			alert("Value entered in field " + objName + " exceeds the Minimum allowed for this field wich is " + MIN);
		  obj.focus();
		  return false;
		}	

 return true;	  	
}	








// end of new features added	
function isPositiveFloat_1(inputVal){
var NOFDECIMALS = 2
var MAX =5000
var MIN =0
var ptFound = false
var rtStr = ""
var lfStr = ""
var inputStr = inputVal.toString()
var initialIndex = 0	  
var negativeSign = ""

	for ( var i = 0 ; i < inputStr.length; i++){
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
		  initialIndex = 1;
		  negativeSign = "-";
		  return "NONEG";
			continue
		}	
		if (oneChar == "."){
			ptFound = true;
			rtStr = inputStr.slice(i+1);
			lfStr = inputStr.slice(initialIndex,-rtStr.length-1)
			break;
			}
		}
		if (!ptFound ){
			if(!isPositiveInt(inputStr)&&(!isNegativeInt(inputStr)))
	        return false
	      else
	      if (parseFloat(inputStr) > MAX){
	      		return "MAX"}
				if (parseFloat(inputStr) < MIN){
					return "MIN"}
	        return true;
	  }
	    if (rtStr.length > 2){
					return "EXDEC"}
			if (isPositiveInt(rtStr)&&(isPositiveInt(lfStr))){
			  if (parseFloat(eval(negativeSign+lfStr+"."+rtStr)) > MAX){
			  		return "MAX"}
				if (parseFloat(eval(negativeSign+lfStr+"."+rtStr)) < MIN){
						return "MIN"}
						return true
			 }else {
			  	  return false;
			}
	  	
}	



   function isSelectedDateValid(objectMonth,objectDay,objectYear,option)
   { 
      if ( option == 1 ) //date must be set
      {
					if (parseInt(objectMonth)==0 && parseInt(objectDay)==0 && parseInt(objectYear)==0)
						return false;
					else if (parseInt(objectMonth)!=0 && parseInt(objectDay)!=0 && parseInt(objectYear)!=0)
					{
						if( objectMonth == 2 )
						{
							if( objectDay > 29 )
									return false;
							else {
								if( objectYear % 4  != 0 && objectDay == 29 ) 
									return false;
								if ( objectYear % 4  == 0 && objectDay == 29)
								{
									if (objectYear % 100 == 0)
										{
											if (objectYear % 400 != 0)
											{
											return false;
											}
										}
								}																		
							}
						}
						else
						{
							if( objectMonth == 4 || objectMonth == 6 || objectMonth == 9 || objectMonth == 11 ) 
								if( objectDay > 30 ) 
									return false;
						}
						return true;						
					}
					else
					  return false;	
	   }
	  else //date is optional
      {
					if (parseInt(objectMonth)==0 && parseInt(objectDay)==0 && parseInt(objectYear)==0)
						return true;
					else if (parseInt(objectMonth)!=0 && parseInt(objectDay)!=0 && parseInt(objectYear)!=0)
						return true;
					else
					  return false;	
			}			
   }
   
   function isSelectedDateValid_1(objMonth,objDay,objYear,option)
   { 
			var eMonth= parseInt( objMonth.value, 10 );
			var eDay = parseInt( objDay.value, 10 );
			var eYear = parseInt( objYear.value, 10 );
			
		if (isNaN( eMonth ) )
			eMonth = 0;
		if (isNaN( eDay ) )
			eDay = 0;
		if (isNaN( eYear ) )
			eYear = 0;				
		
		if ( option == 1 ) //date must be set
		{
			if ( eMonth + eDay + eYear == 0 )
				{
					alert("Date field is required")
					objMonth.focus();
					return false;
				}
		}
		else
		{
			if ( eMonth + eDay + eYear == 0 )
				return true;

		}
 				if (eMonth!=0 && eDay !=0 && eYear !=0)
 				{ 				
						if( eMonth == 2 )
						{
							if( eDay > 29 )
							{
									alert("Ivalid Day")
									objDay.focus();
									return false;
							}
							else {
								if( eYear % 4  != 0 && eDay == 29 ) 
								{
									alert("Ivalid Day")
									objDay.focus();
									return false;
								}
							}
						}
						else
						{
							if( eMonth == 4 || eMonth == 6 || eMonth == 9 || eMonth == 11 ) 
								if( eDay > 30 ) 
								{
									alert("Ivalid Day")
									objDay.focus();
									return false;
								}
						}
						return true;		
				}
				else
				{
					alert("All three fields of the date must be set")
					objMonth.focus();
					return false;
				}				 				

 }
   
   function isSelectedAfterToday(objectMonth,objectDay,objectYear)
   {
			var todaysDate = new Date()
			var todayMonth = todaysDate.getMonth() + 1;
			var todayDay = todaysDate.getDate();
			var todayYear = todaysDate.getFullYear(); 
				    if ( objectMonth != 0 && objectMonth != 0 && objectYear != 0)
				    {
								if (parseInt(objectYear,10) == parseInt(todayYear,10))
								{
								  if (parseInt(objectMonth,10) > parseInt(todayMonth,10))
								  {
										return false;
								  }
									else if (parseInt(objectMonth,10) == parseInt(todayMonth,10))
									{
									  if (parseInt(objectDay,10) > parseInt(todayDay,10))
									  {
										  return false;
									  }
									}
								}
								else if (parseInt(objectYear,10) > parseInt(todayYear,10))
								{
								  return false;
								} 	  
						}	
				return true;
		}
	
   function isSelectedBeforeToday(objectMonth,objectDay,objectYear)
   {
			var todaysDate = new Date()
			var todayMonth = todaysDate.getMonth() + 1;
			var todayDay = todaysDate.getDate();
			var todayYear = todaysDate.getFullYear(); 
				    if ( objectMonth != 0 && objectMonth != 0 && objectYear != 0)
				    {
								if (parseInt(objectYear,10) == parseInt(todayYear,10))
								{
								  if (parseInt(objectMonth,10) < parseInt(todayMonth,10))
								  {
										return false;
								  }
									else if (parseInt(objectMonth,10) == parseInt(todayMonth,10))
									{
									  if (parseInt(objectDay,10) < parseInt(todayDay,10))
									  {
										  return false;
									  }
									}
								}
								else if (parseInt(objectYear,10) < parseInt(todayYear,10))
								{
								  return false;
								} 	  
						}	
				return true;
		}
					
function isDateOneAfterDateTwo(objOneMonth,objOneDay,objOneYear,objTwoMonth,objTwoDay,objTwoYear)
{
			if (parseInt(objOneYear) == parseInt(objTwoYear))
			{	
				  if (parseInt(objOneMonth,10) > parseInt(objTwoMonth,10))
				  {			  
						return false;
				  }
					else if (parseInt(objOneMonth,10) == parseInt(objTwoMonth,10))
					{				
					  if (parseInt(objOneDay,10) > parseInt(objTwoDay,10))
					  {				  
						  return false;
					  }
					}
			}
			else if (parseInt(objOneYear,10) > parseInt(objTwoYear,10))
			{
				  return false;
			} 
			return true;
					  
}

function isDateOneAfterOrEqualToDateTwo(objOneMonth,objOneDay,objOneYear,objTwoMonth,objTwoDay,objTwoYear)
{
			if (parseInt(objOneYear,10) == parseInt(objTwoYear,10))
			{
				  if (parseInt(objOneMonth,10) > parseInt(objTwoMonth,10))
				  {
						return false;
				  }
					else if (parseInt(objOneMonth,10) == parseInt(objTwoMonth,10))
					{
					  if (parseInt(objOneDay,10) >= parseInt(objTwoDay,10))
					  {				  
						  return false;
					  }
					}
			}
			else if (parseInt(objOneYear,10) > parseInt(objTwoYear,10))
			{
				  return false;
			} 
			return true;
					  
}
function isLStringEqualToRString(lStringIn,rStringIn)
{
	var lString = trim(lStringIn);
	var rString = trim(rStringIn);	
	var stringsAreDif = true;
	
	if ( lString.length != rString.length)
		return false; 


	for ( var i=0; i < lString.length; i++)
	{
		var lChar = lString.charAt(i);
		var rChar = rString.charAt(i);
		if ( lChar != rChar )
		{
			stringsAreDif = false;
			break;
		}	
	}	
	return stringsAreDif;
	
}

function isValidSSN(ssn1Obj,ssn2Obj,ssn3Obj)
{
	if(!isValidInt_n(ssn1Obj,"SSN - First Three digits",1,0,1,"",""))
		return false;
	if(	((ssn1Obj.value).toString()).length < 3 )
	{
		alert("Invalid number of digits in SSN - First Three digits")
		ssn1Obj.focus();
		return false;
	}	
	if(!isValidInt_n(ssn2Obj,"SSN - Second Two digits",1,0,1,"",""))
		return false;
	if(	((ssn2Obj.value).toString()).length < 2 )
	{
		alert("Invalid number of digits in SSN - Second Two digits")
		ssn2Obj.focus();
		return false;
	}
	if(!isValidInt_n(ssn3Obj,"SSN - Last Four digits",1,0,1,"",""))
		return false;
	if(	((ssn3Obj.value).toString()).length < 4 )
	{
		alert("Invalid number of digits in SSN - Last Four digits")
		ssn3Obj.focus();
		return false;
	}	
	return true;
}
function isValidPhone(phoneObj1,phoneObj2,phoneObj3)
{

	if(!isValidInt_n(phoneObj1,"Phone filed - Area Code digits.",1,0,1,"",""))
		return "";
	if(	((phoneObj1.value).toString()).length < 3 )
	{
		alert("Invalid number of digits in Phone Field - Area Code.")
		phoneObj1.focus();
		return "";
	}	
	if(!isValidInt_n(phoneObj2,"Phone Field - First Three digits",1,0,1,"",""))
		return "";
	if(	((phoneObj2.value).toString()).length < 3 )
	{
		alert("Invalid number of digits in Phone Field  - First Three digits")
		phoneObj2.focus();
		return "";
	}
	if(!isValidInt_n(phoneObj3,"Phone Field - Last Four digits",1,0,1,"",""))
		return "";
	if(	((phoneObj3.value).toString()).length < 4 )
	{
		alert("Invalid number of digits in Phone Field  - Last Four digits")
		phoneObj3.focus();
		return "";
	}	
	return ((phoneObj1.value).toString() + (phoneObj2.value).toString() + (phoneObj3.value).toString());
	
}

function isValidEmail( obj )
{

	var pattern = "[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+[.][a-zA-Z0-9]+";
	var regExp = new RegExp( pattern, "i" );
	var text = obj.value

	if( !regExp.test( text ) )
	{		
		obj.select();
		obj.focus();
		return false;
	}
	return true;
}
		
	
