 
//*********************************************************
// GLOBAL variables
//*********************************************************
var daysInMonth=new Array(12);
daysInMonth[1]=31;
daysInMonth[2]=29;// must programmatically check this
daysInMonth[3]=31;
daysInMonth[4]=30;
daysInMonth[5]=31;
daysInMonth[6]=30;
daysInMonth[7]=31;
daysInMonth[8]=31;
daysInMonth[9]=30;
daysInMonth[10]=31;
daysInMonth[11]=30;
daysInMonth[12]=31;
var whitespace=" \t\n\r";
var smallChars="abcdefghijklmnopqrstuvwxyz";
var capitalChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var characters=smallChars + capitalChars;
var numericChars="0123456789";
var alphaNumeric=characters + numericChars;
var hCnt=0;


//*********************************************************
// GLOBAL functions
//*********************************************************

function isEmpty(str)
{
if(str=="")return true;
for(i=0;i<str.length;i++)
{
ch=str.charAt(i);
if(whitespace.indexOf(ch)==-1)return false
}
return true;
}
//*********************************************************
function isInteger(str)
{
if(str.charAt(0)=="-" || str.charAt(0)=="+")
i=1;
else
i=0;
for(;i<str.length;i++)
{
ch=str.charAt(i);
if(ch<"0" || ch>"9")return false;
}
return true;
}
//*********************************************************
function isPosInteger(str)
{
if(isInteger(str))
{
return !(str.charAt(0)=="-");
}
else return false;
return true;
}
//*********************************************************
function isUnsignedInteger(str)
{
if(isInteger(str))
{
return(str.charAt(0)!="-"&&str.charAt(0)!="+");
}
else
return false;
return true;
}
//*********************************************************
function isReal(str)
{
strTemp=new String("0" + str);
decimalPos=strTemp.indexOf(".")
if(decimalPos !=strTemp.lastIndexOf("."))return false;
if(decimalPos>-1)
{
return isInteger(strTemp.substring(0,decimalPos))&isUnsignedInteger(strTemp.substring(decimalPos+1,strTemp.length));
}
return(isInteger(str));

}
//*********************************************************
function isPosReal(str)
{
if(isReal(str))
{
return !(str.charAt(0)=="-");
}
else return false;
}
//*********************************************************
function validatePosReal(txtObj,mesg,emptyOk)
{
if(isEmpty(txtObj.value))
{
if(emptyOk)return true;
else
{
alert(mesg);
txtObj.select();
txtObj.focus();
return false;
}
}
if(!isPosReal(txtObj.value))
{
alert(mesg);
txtObj.select();
txtObj.focus();
return false;
}
return true;
}
//*********************************************************
function validatePosInteger(txtObj,mesg,emptyOk)
{
if(isEmpty(txtObj.value))
{
if(emptyOk)return true;
else
{
alert(mesg);
txtObj.select();
txtObj.focus();
return false;
}
}
if(!isPosInteger(txtObj.value))
{
alert(mesg);
txtObj.select();
txtObj.focus();
return false;
}
return true;
}
//*********************************************************
function chkChecked(name,lbound,ubound)
{
var i;
for(i=lbound;i<ubound+1;i++)
{
chkBox=eval(name + i);
if(chkBox.checked)
return true;
}
return false;
}
//*********************************************************
function radioChecked(name,ubound)
{
var i=0;
radioButton=eval(name);
for(;i<ubound+1;i++)
{
if(radioButton[i].checked)return true;
}
return false;
}
//*********************************************************
function clickChk(chkObj,txtObj)
{
if(!isEmpty(txtObj.value))
chkObj.checked=true;
else
chkObj.checked=false;
}
//*********************************************************
function clickRadio(radObj,val,txtObj)
{
if(!isEmpty(txtObj.value))
{
radObj[val-1].checked=true;
}
else
{
radObj[val-1].checked=false;
}
}
//*********************************************************
/*function notDate(str1,sep)
{
return notDate(str1,sep,"mm"+sep+"dd"+sep+"yyyy");
}
*/
function notDate(str1,sep,format)
{
var str=new String(str1);
if(str.length<6 || str.length>10)return true;
pos=str.indexOf(sep);
lpos=str.lastIndexOf(sep);
if(pos==lpos)return true;
arrDay=str.split(sep);
if(arguments.length==3)
arrFormat=format.split(sep);
else
{
var format="mm/dd/yyyy";
arrFormat=format.split("/");
}
switch(arrFormat[0].charAt(0))
{
case 'm':
intMm=parseInt(arrDay[0]);
break;
case 'd':
intDd=parseInt(arrDay[0]);
break;
case 'y':
intYy=arrDay[0];
if(arrDay[0].length !=arrFormat[0].length)
{
alert("Please give the year in the required format");
return true;
}
if(arrDay[0].length !=2&&arrDay[0].length !=4)
{
alert("Invalid year ");
return true;
}
break;
}
switch(arrFormat[1].charAt(0))
{
case 'm':
intMm=parseInt(arrDay[1]);
break;
case 'd':
intDd=parseInt(arrDay[1]);
break;
case 'y':
intYy=arrDay[1];
if(arrDay[1].length !=arrFormat[1].length)
{
alert("Please give the year in the required format");
return true;
}
if(arrDay[1].length !=2&&arrDay[1].length !=4)
{
alert("Invalid year ");
return true;
}
break;
}
switch(arrFormat[2].charAt(0))
{
case 'm':
intMm=parseInt(arrDay[2]);
break;
case 'd':
intDd=parseInt(arrDay[2]);
break;
case 'y':
intYy=arrDay[2];
if(arrDay[2].length !=arrFormat[2].length)
{
alert("Please give the year in the required format");
return true;
}
if(arrDay[2].length !=2&&arrDay[2].length !=4)
{
alert("Invalid year ");
return true;
}
break;
}
if(isNaN(intMm))
{
alert("Please enter an integer for month<mm>");
return true;
}
if(intMm<0 || intMm>12)
{
alert("Invalid month");
return true;
}
if(isNaN(intDd))
{
alert("Please enter an integer for day<dd>");
return true;
}
if(intDd<0 || intDd>daysInMonth[intMm])
{
alert("Invalid day");
return true;
}
if(isNaN(intYy))
{
alert("Please enter an integer for year<yyyy>");
return true;
}
if(intMm==2)
{
if(!(((intYy % 4==0)&&!(intYy % 100==0))||(intYy % 400==0))&&intDd>28)
{
alert(intYy + " is not leap year ");
return true;
}
}
return false;

}
//*********************************************************
function leftTrim(str)
{
var i=0;
len=str.length;
for(i=0;i<len;i++)
{
if(whitespace.indexOf(str.charAt(i))==-1)break;
}
str=str.substr(i);
return str;
}
//*********************************************************
function rightTrim(str)
{
var i=0;
len=str.length;
for(i=len-1;i>=0;i--)
{
if(whitespace.indexOf(str.charAt(i))==-1)break;
}
if(i !=len-1)str=str.substr(i);
return str;
}
//*********************************************************
function trim(str)
{
return leftTrim(rightTrim(str));
}
//*********************************************************
function isEmail(s)
{

if(isEmpty(s))return false;
var invalidChars=" \t\n\r\\\"\'#$%^&*()+=!~`/;:<>?,[]{}"
var i,pos,pos1,posPeriod;

s=trim(s);

for(i=0;i<s.length;i++)//filter for invalid chars
{
if(invalidChars.indexOf(s.charAt(i))!=-1)return false;
}
if(characters.indexOf(s.charAt(0))==-1)return false;//starts with alphabet
pos=s.indexOf("@")
if(pos==-1 || pos==0)return false;// @ present and not first char

pos1=s.lastIndexOf("@")
if(pos1 !=pos)return false;// multiple at present

posPeriod=s.lastIndexOf(".")
if(posPeriod==-1 || posPeriod<(pos + 2)|| posPeriod==(s.length-1))return false;

return true;
}

//************************************************************
function isNameValid(s)
{
var i;
for(i=0;i<s.length;i++)//filter for invalid chars
{
if(numericChars.indexOf(s.charAt(i))!=-1)return false;
}

return true;
}
//************************************************************
function isTel(s)
{
var i;
for(i=0;i,s.length;i++) // filter for tel no.
{
if(characters.indexOf(s.charAt(i))!=-1)return false;
}
return true;
}

function str2Date(date,sep,format)
{
if(!notDate(date,sep,format))
{
arrDay=date.split(sep);
arrFormat=format.split(sep);
newDate="";
for(i=0;i<3;i++)
{
if(arrFormat[i].charAt(0)=='m')
newDate +=arrDay[i];
}
newDate +="/";
for(i=0;i<3;i++)
{
if(arrFormat[i].charAt(0)=='d')
newDate +=arrDay[i];
}
newDate +="/";
for(i=0;i<3;i++)
{
if(arrFormat[i].charAt(0)=='y')
newDate +=arrDay[i];
}
return new Date(newDate);
}
}
/************************************************************/
function datediff(date1,date2)
{				
	text1=date1;
	arrServerdate=text1.split("/");
	text2=date2;
	arrClientdate=text2.split("/");
	if((arrClientdate[0]<=0) || (arrClientdate[1]<=0) || (arrClientdate[2]<=0))
	{
		return false;
	}

	if(arrClientdate[2]>arrServerdate[2])
	{
		return false;
	}
	else 
	{
		if((arrClientdate[0]>arrServerdate[0])&&(arrClientdate[2]==arrServerdate[2]))
		{
			return false;
		}
		else 
		{
			if((arrClientdate[1]>arrServerdate[1])&&(arrClientdate[0]==arrServerdate[0])&&(arrClientdate[2]==arrServerdate[2]))
			{
			return false;
			}					
		}

	}
return true;

}
function isInvalid(str)
{
 var invalidChars = "\t\n\r\\\"\'#$%^&*()+=!~`/;:<>?[]{}";
 for(s=0;s<str.length;s++) //filter for invalid chars
	    {
	    	if(invalidChars.indexOf(str.charAt(s)) != -1) return false;
	    }
	   return true; 
}  


//*****************************************************

//added by Haseeb to check that pwd field should contain $ or # or !
function isRequired(str)
{
var n;
var flag=true;
var validChars="$#!";
for(n=0; n<str.length; n=n+1) //filter for valid chars
{
   	if(validChars.indexOf(str.charAt(n))==-1) 
      flag=true;
	 else
	 {
	  flag=false;
	  break;	
	 }
 }  
   //	
if (flag==true)
   return false;
else
     return true; 
}  

//*****************************************************



//*****************************************************
//for address field validation
function isbad(str)
{
 var inbadChars = "\t\n\r^&*+=~`';<>?[]{}";
 for(n=0;n<str.length;n++) //filter for invalid chars
	    {
	    	if(inbadChars.indexOf(str.charAt(n)) != -1) return false;
	    }
	   return true; 
}  


/****************************************************************************/

//For calculating AGE
function displayAge(yr, mon, day, unit, decimal, round)
{
	var one_day=1000*60*60*24
	var one_month=1000*60*60*24*30
	var one_year=1000*60*60*24*30*12

	today=new Date()
	var pastdate=new Date(yr, mon-1, day)

	var countunit=unit
	var decimals=decimal
	var rounding=round

	finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
	decimals=(decimals<=0)? 1 : decimals*10

	if (unit=="days")
	{
		if (rounding=="rounddown")
			 //document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
			 return ((Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals)-1)
			//var noOfDays = (Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
		else
			document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
	}
	else
	{
	//alert(today.getFullYear())
	//alert(yr)
		yearspast	= today.getFullYear()-yr-1
		tail		= (today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()<=day)? 1 : 0
		pastdate.setFullYear(today.getFullYear())
		pastdate2	= new Date(today.getFullYear()-1, mon-1, day)
		tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
		//alert(yearspast+tail+" "+countunit)
		//alert(tail);
		if (tail!="0")
		{
			//alert(yearspast+tail);
			return (yearspast+tail)
		}
		else
		{
			//alert(yearspast);
			return yearspast
		}
		//alert(yearspast+tail);
		//return (yearspast+tail);
	}
}
/****************************************************************************/
//Validate Date
function makeArray()    
{
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
		this[i+1] = makeArray.arguments[i];
}
var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var monthsofyear  = new makeArray('January','February','March','April','May','June','July','August','September','October','November','December');

function LeapYear(year) 
{
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function IsValidDate(day,month,year) 
{
    if ( (LeapYear(year) && (day > daysofmonthLY[month])) || (!LeapYear(year) && (day > daysofmonth[month])) )
        return false;
    else
        return true;
}
