/***********
AUTHOR : RADHIKA SUDHIR 

DATE ADDED	:	DEC'2-02

***********/
//*******************************--CLIENT SIDE VALIDATIONS (BEGIN)---***********************

/************************************************
NAME	: trimAll

DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 

//---BEGIN(trimAll)----
		function trimAll(strValue)
		{
			var objRegExp = /^(\s*)$/;
			//check for all spaces
			if(objRegExp.test(strValue)) {
			strValue = strValue.replace(objRegExp, '');
			if( strValue.length == 0)
				return strValue;
				
		}    
		//check for leading & trailing spaces
		objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
		if(objRegExp.test(strValue)) {
			//remove leading and trailing whitespace characters
			strValue = strValue.replace(objRegExp, '$2');
			
			}
		return strValue;
		}
//---END(trimAll)----
/************************************************
NAME:			validRequired

DESCRIPTION:	Make given field as required field.

PARAMETERS:		fieldName , fieldLabel as Errorlabel.

RETURNS:		Source string with whitespaces removed.

CHECKED For:    Run Space bar , Left Blank.
*************************************************/ 
//---BEGIN(validRequired)----
	function validRequired(formField,fieldLabel)
	{
//		alert("Required:"+formField);
		var fieldValue = trimAll(formField.value);
		if ((fieldValue=="") || (fieldValue.length==0))
		{
			//formField.focus();	
			alert(fieldLabel + " " + "cannot be empty.  Please fill it in.");
			formField.value="";
			if(formField.disabled == false)
				formField.focus();
			return false;
		}
			return true;
	}

	function validNumeric(formField,fieldLabel)
	{
		var fieldValue =  trimAll(formField.value);
		if(isNaN(fieldValue))
		{
		alert(fieldLabel+' '+"must be numeric.");
		formField.focus();
		return false;
		}
		return true;
	}
//---END(validRequired)----
/************************************************
NAME:			f_validLength

DESCRIPTION:	Checking for maxlength.

PARAMETERS:		fieldName , fieldLabel as Errorlabel,length(ex:3 -characters).

RETURNS:		Boolean value accordingly.

CHECKED For:    specified length
*************************************************/ 
//---BEGIN(f_validLength)----

	function f_validLength(formField,fieldLabel,nLength)
	{
//		alert("valid length:"+formField);
		var fieldValue =  trimAll(formField.value);	
		if(fieldValue!="")
		{
			if ( (fieldValue.length)< (parseInt(nLength)) )
			{	
				alert(fieldLabel+' '+"must be minimum"+' '+nLength+' '+"charcters.");			
				formField.focus();
				return false; 
			}
		}
			return true;
	}
	
//---END(f_validLength)----


	

/************************************************
/************************************************
NAME:			f_chkNumrange

DESCRIPTION:	Checking range specified between two numbers.

PARAMETERS:		fieldName , num1Label as range 1,numLabel2 as range 2.

RETURNS:		Boolean value accordingly.

CHECKED For:    specified range.
*************************************************/ 
//---BEGIN(f_validLength)----
function f_chkNumrange(formField,fieldLabel,num1Label,num2Label)
{
	var fieldValue =  trimAll(formField.value);
	if(fieldValue!="")	
	{
			if((fieldValue.length)>2)//1
			{
				alert(fieldLabel+' '+"should be between"+' '+num1Label+' '+num2Label+".");
				//formField.value = "";
				formField.focus();
				return false;
			}
			//else
			//{
				if(isNaN(fieldValue))
				{
				alert(fieldLabel+' '+"must be numeric.");
				//formField.value = "";
				formField.focus();
				return false;
				}
				//else
				//{
					if(fieldValue.charAt(0)==0)
					{
					var fieldValue1 = fieldValue.charAt(1);
					fieldValue = fieldValue1;
					}
					
					if( (fieldValue>=parseInt(num1Label)) && (fieldValue<=parseInt(num2Label)) )
					{
					return true;
					}
					else
					{
					alert(fieldLabel+' '+"should be between"+' '+num1Label+' '+"and"+' '+num2Label+".");
					//formField.value = "";
					formField.focus();
					return false;
					}
					
					//}		
			//}
	}
return true;
}
//---END(f_validLength)----
/************************************************
NAME:			funDate_Req,funDate_Date(This Function is mainly used When you are handling
					value of formField AS "00/00/0000" (As in Client-Server Model))
					,funDate_Comp = for comparing above type of dates

DESCRIPTION:	First one makes given field as required field.
				Second one makes required field as valid date field.
				Third one compare dates depending on operator passed.

PARAMETERS:		formField as fieldname to be validated , fieldLabel as Errorlabel.
				//for comparing dates = date1,date2,date1Msg,date2Msg,operator need to be passed.

CHECKED For:    For First Case field value = "" , "00/00/0000" , space bar
				For Second Case = Any Invalid Date Format  Excluding "00/00/0000"
				For Third Case = Comparing dates with Operator passed.
Reference :    For Reference/Usage Model see IEP > IEP FORM > Considerations > Dates / Disability Tab				
*************************************************/ 
//---BEGIN(CUSTOM DATE)----
	//---	
     	function funDate_Req(formField,fieldLabel)
     	{
     		var fieldValue = trimAll(formField.value);
     		if( (fieldValue=="") || (fieldValue=="00/00/0000") )
     		{
     			if (fieldValue=="")
     			{     			
     				alert(fieldLabel + " " + "cannot be empty.  Please fill it in.");				
     				
     			}
     		
     			if(fieldValue=="00/00/0000")
     			{	
     				alert("The"+' '+fieldLabel +' '+"format should be valid : mm/dd/yyyy or mm-dd-yyyy")
     			}
     			
				formField.focus();
				return false;						
     		}
     		return true;
     	}
     	//--
     	function funDate_Date(formField,fieldLabel)
     	{
     		var fieldValue = trimAll(formField.value);
     		//if( (fieldValue!="") || (fieldValue!="00/00/0000") )
     		if(fieldValue != "00/00/0000")
     		{ 
     		
     			if (isDate(fieldValue,fieldLabel)==false)
				{				
					formField.value="";
					formField.focus();
					return false;
				}				
     		}
     		else
     		{
     			alert('The '+fieldLabel+' should be a valid date');
     			return false;
     		}
     		return true;
     	}	
//--
//----------
     	function funDate_Comp(formField1,fieldLabel1,formField2,fieldLabel2,operator)
     	{ 
     		var fieldValue1 = trimAll(formField1.value);
     		if( (fieldValue1!="") || (fieldValue1!="00/00/0000") )
     		{   
     		//--------
     			var dt1 = trimAll(formField1.value);
				var dt2 = trimAll(formField2.value);
	
					//--make sure both values are in date format
					//if ( (dt1!="") && (dt2!=""))	
					if(dt1!="")
					{
						if (isDate(dt1,fieldLabel1)==false) {		
							formField1.focus();
							return false;
						}
					}
					
					if(dt2!="")
					{
						if (isDate(dt2,fieldLabel2)==false) {		
							formField2.focus();
							return false;
						}
					}
					
					
				if ( dt1!="")
					{
				if(dt2!=""){

						if (isDate(dt1,fieldLabel1)==false) {		
							formField1.focus();
							return false;
						}
					
						if (isDate(dt2,fieldLabel2)==false) {		
							formField2.focus();
							return false;
						}

						//-----------
						var p1 = new Date(dt1);	
						var p2 = new Date(dt2);
						dt1 = p1;
						dt2 = p2;

						var s;
						
						switch (operator) {
							case "notequal":
								s = (dt1 != dt2);
								d1 = "not equal";
								break;
							case "greaterthan":				
								s = (dt1 > dt2);
								d1 = "greater than";
								break;
							case "greaterthanequal":				
								s =  (dt1 >= dt2);
								d1 = "greater than  or equal to";
								break;
							case "lessthan":
								s =  (dt1 < dt2);	
								d1 = "less than";			
								break;
							case "lessthanequal":
								s =  (dt1 <= dt2);
								d1 = "less than  or equal to";
								break;
							case "equal":	
								s = (dt1 == dt2);     
								d1 = "equal";	       
								break;
						}
						//-----------	
						
							if(!s)
							{
								alert(fieldLabel1+'  '+'must be'+' '+d1+ '  '+fieldLabel2+'.');				
								formField1.focus();	
								return false;
							}		
					}
					}
			//--------							
     		}
     		return true;
     	}	
     	//-----------
//---END(CUSTOM DATE)----
/************************************************
NAME:			f_testregx

DESCRIPTION:	Checking for valid chars through regular expression pattern property.

PARAMETERS:		regexp as pattern,fieldName , fieldLabel as Errorlabel.

RETURNS:		Boolean value accordingly.

CHECKED For:    tested all characters against regular expression pattern.
*************************************************/ 
//---BEGIN(f_testregx)----
	function f_testregx(regexp,formField,fieldLabel)
	{
//			alert("Regular Expression:"+formField);
		var fieldValue = trimAll(formField.value);
		exp = new RegExp(regexp);		
		var v1 = fieldValue.match(exp);		
		if(fieldValue!="")
		{	
			if(v1==null)
			{
				alert("Invalid characters for "+' '+fieldLabel+".");
				//formField.value="";
				formField.focus();
				return false; 
			}	
		}	
			return true;
	}
//---END(f_testregx)----

/************************************************
NAME:			f_email

DESCRIPTION:	Checking for valid email ID.

PARAMETERS:		fieldName to be Checked against.

RETURNS:		Boolean value accordingly.

CHECKED For:    tested all valid characters for email. ex: radhika@yahoo.com,
															radhika@yahoo.co.uk
*************************************************/ 
//---BEGIN(f_testregx)----
	function f_email(formField,fieldLabel)
	{
		var fieldValue = trimAll(formField.value);		
		if(fieldValue!="")
		{	
			// modified for #4026 - Siva
			// modified to allow '-' prior to '.'
			var v3 =  /^[\w\.\-\']+@[\w\.-]+\.[a-zA-Z]+$/;
			if(!v3.test(fieldValue))
			{
				alert("Email is not valid.");
				//formField.value="";
				formField.focus();
				return false; 
			}			
		}	
			return true;
	}
//---END(f_testregx)----


//----*****FOR DATE VALIDATIONS -- BEGIN --*****------
/************************************************
NAME:			isInteger

DESCRIPTION:    Make sure all Characters are numbers.

PARAMETERS:		s AS Source string from to be tested

RETURNS:		Boolean value accordingly.

CHECKED For:    numbers only
*************************************************/ 

//var dtCh= "/";
var minYear=1900;
var maxYear=2050;
//---BEGIN(isInteger)----
	function isInteger(s)
	{
		var i;
		for (i = 0; i < s.length; i++)
		{   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
			// All characters are numbers.
			return true;
	}

//---END(isInteger)----
//---BEGIN(stripCharsInBag)----
	function stripCharsInBag(s, bag)
	{
			var i;
			var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++)
		{   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
			return returnString;
	}
//---END(stripCharsInBag)----
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
   return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
//----
//----*****FOR DATE VALIDATIONS -- BEGIN --*****------
/************************************************
NAME:			isDate

DESCRIPTION:    Make sure all Characters are numbers.

PARAMETERS:		s AS Source string from to be tested

RETURNS:		Boolean value accordingly.

CHECKED For:    numbers only
*************************************************/ 
//---BEGIN(isDate)----
	function isDate(dtStr,dtlabel){	

		var s = dtStr.indexOf("/");
		if (!(s=="-1"))
		{
			dtCh ="/";
		}
		else
		{
			dtCh = "-";
		}
		
	
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)	
	strYr=strYear
	
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The"+' '+dtlabel +' '+"format should be : mm/dd/yyyy or mm-dd-yyyy")
		return false
	}
/////////////////////////////////////////////////////////////////////////////
/// Changed By        :  ChandranK
/// Date of Change    :  26-Aug-2005 
/// Purpose of change :  14633
/// Details of Change :  Change the Alert Message Month to Date
/////////////////////////////////////////////////////////////////////////////
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid Date in"+' '+dtlabel)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
	//if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > DaysArray(month)){
		alert("Please enter a valid day in"+' '+dtlabel)
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+' '+"for"+' '+dtlabel)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid"+' '+dtlabel +' ')
		return false
	}
return true
}

//---END(isDate)----
//---
function f_valdate(formField,fieldLabel)
{
//	alert("Date:"+formField);
	var dt =  trimAll(formField.value);
	var n;
	if(dt.length==0)
	{
		//formField.focus();
		//formField.value="";
		n="yes";	
	}
	else
	{
		n="no";
	}	
	//if  (dt!="") //commented temp on 20/02/2003
	if(n=="no")
	{
		//formField.focus();
		if (isDate(dt,fieldLabel)==false)
		{
				formField.focus();		//added by jackson
				formField.value="";				
				return false;
		}
		
	}	
    return true;
 }
//---
//--
function f_compdates(formField1,fieldLabel1,formField2,fieldLabel2,operator)
{
//	alert("Compare Date:"+formField1);
	var dt1 = trimAll(formField1.value);
	var dt2 = trimAll(formField2.value);
	
	//--make sure both values are in date format
	//if ( (dt1!="") && (dt2!=""))	
	if(dt1!="")
	{
		if (isDate(dt1,fieldLabel1)==false) {		
			formField1.focus();
			return false;
		}
	}
	
	if(dt2!="")
	{
		if (isDate(dt2,fieldLabel2)==false) {		
			formField2.focus();
			return false;
		}
	}
	
	
if ( dt1!="")
	{
if(dt2!=""){

		if (isDate(dt1,fieldLabel1)==false) {		
			formField1.focus();
			return false;
		}
	
		if (isDate(dt2,fieldLabel2)==false) {		
			formField2.focus();
			return false;
		}

		//-----------
		var p1 = new Date(dt1);	
		var p2 = new Date(dt2);
		dt1 = p1;
		dt2 = p2;

		var s;
		switch (operator) {
			case "notequal":
				s = (dt1 != dt2);
				d1 = "not equal";
				break;
			case "greaterthan":				
				s = (dt1 > dt2);
				d1 = "greater than";
				break;
			case "greaterthanequal":				
				s =  (dt1 >= dt2);
				d1 = "greater than  or equal to";
				break;
			case "lessthan":
				s =  (dt1 < dt2);	
				d1 = "less than";			
				break;
			case "lessthanequal":
				s =  (dt1 <= dt2);
				d1 = "less than  or equal to";
				break;
			case "equal":	
				s = (dt1 == dt2);     
				d1 = "equal";	       
				break;
		 }
		//-----------	
		
			if(!s)
			{
				if( (fieldLabel1!="") && (fieldLabel2!="") )
				{
					alert(fieldLabel1+'  '+'must be'+' '+d1+ '  '+fieldLabel2+'.');				
				}
				return false;
			}		
	}
	}
	
		
	return true;
}
//--
//----*****FOR DATE VALIDATIONS -- END --*****--------

/************************************************
NAME:			valid

DESCRIPTION:    function to be called in user control.

PARAMETERS:		Ctrl_ID,Ctrl_Errmsg,required,nLength,pattern

RETURNS:		Boolean value accordingly.

CHECKED For:    depending on parameters passed to function ,validation checked accordingly.
*************************************************/ 
//---BEGIN(valid)----(NOT USED AT ANY PLACE IN THE PROJECT)
function valid(Ctrl_ID,Ctrl_Errmsg,required,nLength,pattern,Date1_vID,Date1_Errmsg,Date2_vID,Date2_Errmsg,date_sep,operator)
	{
		//---required field--BEGIN		
			if(required!="")
			{							
				if(!validRequired(Ctrl_ID,Ctrl_Errmsg))
				{return false;}			
			}
		//---required field--END
	
		//---Maxlength--BEGIN		
			if(nLength!="")
			{
				if(!f_validLength(Ctrl_ID,Ctrl_Errmsg,nLength))
				{return false;}				
			}
		//---Maxlength--END
		
		//---Regular expression pattern--BEGIN
			if(pattern!="")
			{
				if(!f_testregx(pattern,Ctrl_ID,Ctrl_Errmsg))			
				{return false;}				
			}
		//---Regular expression pattern--END
	
		//---Date Validation--BEGIN										
				if( (!f_valdate(Ctrl_ID,Ctrl_Errmsg,date_sep)) )
				{return false;}			
		//---Date Validation--END
		//---Comparing dates--BEGIN
			if (Date2_vID!="")
			{	 dt1= document.forms[0].item(Date1_vID);					
				 dt2= document.forms[0].item(Date2_vID);				
				if( (!f_compdates(dt1,Date1_Errmsg,dt2,Date2_Errmsg,date_sep,operator)) )
					{return false;}		
			}
		
		//---Comparing dates--END		
		
				return true; //--function returning boolean value.		

	}
//---END(valid)----

//--- For Time Validation
function IsValidTime(formField)
{
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.
//	alert("valid time:"+formField);
	timeStr = trimAll(formField.value);
	if(timeStr != "") {
		var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

		var matchArray = timeStr.match(timePat);
		if (matchArray == null) {
			//..alert("Time is not in a valid format.");			
			alert("Enter Time in format ex: 12:30:35 PM/AM or 22:30");
			formField.focus();
			return false;
		}
		hour = matchArray[1];
		minute = matchArray[2];
		second = matchArray[4];
		ampm = matchArray[6];
		if (second=="") { second = null; }
		if (ampm=="") { ampm = null }

		if (hour < 0 || hour > 23) {
			alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
			return false;
		}
		if (hour <= 12 && ampm == null) {
			var Cmsg = confirm("Please indicate which time format you are using. OK = Standard Time, CANCEL = Military Time");
			if (Cmsg) {
				alert("You must specify AM or PM.");
				return false;
			}
			else
			{
				return false;
			}
		}
		if (hour > 12 && ampm != null) {
			alert("You can't specify AM or PM for military time.");
			return false;
		}
		if (minute<0 || minute > 59) {
			alert ("Minute must be between 0 and 59.");
			return false;
		}
		if (second != null && (second < 0 || second > 59)) {
			alert ("Second must be between 0 and 59.");
			return false;
		}

	}	
	return true;
}
//----
// End -->

//*******************************--CLIENT SIDE VALIDATIONS (END)---***********************

/***********
AUTHOR : RADHIKA SUDHIR 

DATE ADDED	:	DEC'23-02

***********/
//*******************************--CLIENT SIDE VALIDATIONS (BEGIN)---***********************

/************************************************
NAME	: Chkradio

DESCRIPTION: Ensure atleast one option selected in RadioButton's Group

PARAMETERS: RadioButton Group Name

RETURNS: Boolean value.
*************************************************/ 
//--
  			function Chkradio(fieldName)
  			{
  				var radio_choice = false;
					for (counter = 0; counter < fieldName.length; counter++)
					{					
						if (fieldName[counter].checked)
						radio_choice = true; 
					}
					if (!radio_choice)
					{						
						return false;
					}
						return true;	
  			}
//--

/************************************************
NAME	: ChkSingle

DESCRIPTION: Checking whether a checkbox is checked / unchecked.

PARAMETERS: CheckBox Name to be checked.

RETURNS: Boolean value.
*************************************************/ 
  	function ChkSingle(fieldName)
  	{
  		var chkBox = false;
  		if(fieldName.checked)
  		{
  			chkBox = true;
  		}
  		if(!chkBox)
  		{
  			return false;
  		}
  		return true;
  	}
 //--	

  			
//*******************************--CLIENT SIDE VALIDATIONS (END)---***********************  		



	function f_valNumeric(formField,fieldLabel)
	{
		var fieldValue =  trimAll(formField.value);
		if(isNaN(fieldValue))
		{
		alert(fieldLabel+' '+"must be numeric.");
		formField.focus();
		return false;
		}
		return true;
	}