//function to remove all the trailing and leading spaces in a string, returns the string with no //leading and trailing spaces



function trim(obj)
{		
	if (typeof(obj)=='object') obj=obj.value
	for(i=obj.length-1;i>=0;i--)
		if(obj.charAt(i)!=' ') break		
	obj=obj.substring(0,i+1)
	for(j=0;j<=i+1;j++)
		if(obj.charAt(j)!=' ') break
	return obj.substring(j,i+1)
}

//function to check for null values, returns true or false
function isNull(obj)

{
	if (typeof(obj)=='object') obj=obj.value
	//alert('obj=' + obj)
	obj=trim(obj);
	return obj.length == 0 ? true:false;
}


//function to check if the value entered is a number, returns true or false
function isNum(obj)
{		
	if (typeof(obj)=='object') obj=obj.value
	obj=trim(obj);	
	retstatus=true;
	if (obj.length!=0)
	{
		for ( i = 0 ; i < obj.length ; i++ )
			if ( obj.charAt(i) < '0' || obj.charAt(i) > '9' ) retstatus= false;
	}
	else
		retstatus= false	
	return retstatus;
}

//function to check for a valid email ID, returns true or false
function chkEmail(obj)
{
	strEmail=trim(obj);
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/) != -1)
	{
		return true;
	}
	else
	return false;	
}

//function to chech for valid url
function chkUrl(obj)
{
	strEmail=trim(obj);
if (strEmail.search(/^(((WWW\.)|(HTTPS\:\/\/)|(HTTP\:\/\/)|(http\:\/\/)|(https\:\/\/)|(www\.))+)([A-Za-z0-9]+)((((\-)[A-Za-z0-9]+)|((\.)[A-Za-z]+))*)((\.)[A-Za-z]+$)/) != -1)	
	{
	return true;
	}
	else
	return false;	
}

//function to check for a valid phone number, returns true or false
function chkPhone(obj1,obj2,obj3)
{	
	if (typeof(obj1)=='object') obj1=obj1.value
	if (typeof(obj2)=='object') obj2=obj2.value
	if (typeof(obj3)=='object') obj3=obj3.value
	obj1=trim(obj1);obj2=trim(obj2);obj3=trim(obj3)
	return (!((obj1.length==3) && (obj2.length==3) && (obj3.length==4))) ? false : (!((isNum(obj1)==true && isNum(obj2)==true && isNum(obj3)==true))) ? false: (!((allZeros(obj1)==true && allZeros(obj2)==true && allZeros(obj3)==true))) ? true : false
}

//function to compare two strings, returns true or false
function strCmp(obj1,obj2)
{
	if (typeof(obj1)=='object') obj1=obj1.value
	if (typeof(obj2)=='object') obj2=obj2.value
	if (obj1.length==0 && obj2.length==0) return false;
	return obj1==obj2 ? true:false
}

//t0 check combo is blank
function isComboBlank(obj)
{
//alert(obj.options[obj.selectedIndex].value)
	//return (obj.options[obj.selectedIndex].value=='blank') ? true:false;
	
	return (obj.value=='' || obj.options[obj.selectedIndex].value== '' ) ? true:false;
}

//function to check if the date value passed is a number, returns true or false
function isNumDt(obj)
{	
	
	var str1 = obj.toString();	
	len = str1.length;
	retstatus=true;
	for ( i = 0 ; i < len ; i++ )
	{		
		if ( str1.charAt(i) < '0' || str1.charAt(i) > '9' )
		retstatus= false ;
	}
	 return retstatus;
}


//function
function isChar1(obj,schr,flg)
{
	//array sarr contains all the valid characters 
 	sval=trim(obj);
	sarr=schr;
	if(flg==0)	
	sarr1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	else if(flg==1)
	sarr1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'";
	else if(flg==2)
	sarr1='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"';	
	retstatus=true;	
	for(var i=0; i <= sval.length; i++)
	{		
		var sval1=sarr.indexOf(sval.charAt(i));
		var sval2=sarr1.indexOf(sval.charAt(i));			
		if((sval2==-1) && (sval1==-1))
		{
		 retstatus=false;		 
		 break;
		}		
	}
	 return retstatus;
}

function isValidChar(obj,schr,flg)
{
	if (typeof(obj)=='object') obj=obj.value
	if (flg==1) 
		sarr1='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';
	else if (flg==2) 
		sarr1='ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789';
	else if (flg==3) 
		sarr1='0123456789';
	else if (flg==4) 
		sarr1='0123456789,-';
	sarr1=sarr1 + schr;	
	for(var i=0;i <=obj.length-1;i++)	
	{	
		spflag=(sarr1.indexOf(obj.charAt(i))!=-1)==true ? true:false
		if (spflag==false) return false
	}
	return true;	
}

function isSpecialChar(obj,schr)
{
	sval=trim(obj);
	sarr='~`@#$%^&*+={}:;?/<>|\\"!,.[]-_()'
	sarr1=schr;
	retstatus=true;
	for(var i=0;i <= sval.length;i++)
	{
		var sval1=sarr.indexOf(sval.charAt(i));
		var sval2=sarr1.indexOf(sval.charAt(i));
		//alert(sval1);
		if((sval1!=-1) && (sval2==-1))
		{
			retstatus=false;
			break;
		}
	}
	return retstatus;	
}
function comboset(obj,cmbname,setcmbvalue)
{
	for (i=0;i<=(obj[cmbname].length-1);i++)
	{
		if ((obj[cmbname][i].value)==setcmbvalue)
		{
			obj[cmbname][i].selected=true;
			break;
		}
	}
}

function chkset(obj,chkname,setchkvalue)
{
	if (setchkvalue == 'False')
	{
		obj[chkname].checked = false;
	}
	if (setchkvalue == 'True')
	{
		obj[chkname].checked = true;
	}
}


function multiComboSet1(obj,cmbName,setStr) //Old code
{
	var i,j,ln;
	ln=obj[cmbName].length
	splitStr=setStr.split(',')
	obj[cmbName][0].selected=false;
	for (i=0; i<=splitStr.length-1;i++)
	{
		//alert ('[' + trimStr(splitStr[i]) + ']')
		for (j=0; j<=ln-1;j++)
		{
				if (obj[cmbName][j].value==trimStr(splitStr[i]))
				{
					obj[cmbName][j].selected=true;
				//	alert('is set [' + splitStr[i] + ']')
					break;
				}
		}
	}
}

function multiComboSet(obj,cmbName,setStr)  //Modified by raikar for  31/10/2001
{
	var i,j,ln;
	ln=obj[cmbName].length
	splitStr=setStr.split(',')
	obj[cmbName][0].selected=false;
	for (i=0; i<=splitStr.length-1;i++)
	{
		
		for (j=0; j<=ln-1;j++)
		{
			if (trim(splitStr[i])!="" )  //This new condition has been inserted
			{
				if (obj[cmbName][j].value==trimStr(splitStr[i]))
				{
					obj[cmbName][j].selected=true;
					break;
				}
			}
		}
	}
}
function trimStr(str)
{		
	str1=str.length;
	for(i=str1-1;i>=0;i--)
	{			
		if(str.charAt(i)!=' ') break;		
	}

	str2=str.substring(0,i+1)

	for(j=0;j<=i+1;j++)
	{	
		if(str2.charAt(j)!=' ') break;
	}

	str2=str2.substring(j,i+1);
	return str2;
}

function allZeros(obj)
{
	if (typeof(obj)=='object') obj=obj.value
	for (i=0;i<obj.length;i++)
		if (obj.charAt(i) !='0') return false;
	return true;
}

function isZip(obj)
{
if (typeof(obj)=='object') obj=obj.value
return  (isNum(obj)==false) ? false: (allZeros(obj)==true) ? false:true;
}

//for used in resource portal
function cmpExperience(frmobj)
{
	return (parseFloat(frmobj.cmbusexp.options[frmobj.cmbusexp.selectedIndex].value) >parseFloat(frmobj.cmbexperience.options[frmobj.cmbexperience.selectedIndex].value)) ? false:true
}

// function to convert the first character of a string to upper case.
function convertCase(obj)
{
var ftchar=obj.charAt(0);
var str=obj.substr(1,obj.length-1);
ftchar=ftchar.toUpperCase();
obj=ftchar + str;
return obj;
}

function chkValidText(obj)
{
    var flg=0;
    obj=trim(obj);
    for (i=0;i<obj.length;i++)
    {
        if (((obj.charCodeAt(i) >=65) && (obj.charCodeAt(i) <=90)) ||((obj.charCodeAt(i) >=97) && (obj.charCodeAt(i) <=122)))
		{
			flg=1;
			return flg;
		 }
	}
}

//function to check for a valid phone number, returns true or false
function chkPhne(obj1,obj2,obj3)
{	
	if (typeof(obj1)=='object') obj1=obj1.value
	if (typeof(obj2)=='object') obj2=obj2.value
	if (typeof(obj3)=='object') obj2=obj3.value
	obj1=trim(obj1);obj2=trim(obj2);obj3=trim(obj3)
	return (!((obj1.length < 6) && (obj2.length < 6) && (obj3.length < 6) )) ? false : (!((isNum(obj1)==true && isNum(obj2)==true && isNum(obj3)==true ))) ? false: (!((allZeros(obj1)==true && allZeros(obj2)==true && allZeros(obj3)==true ))) ? true : false;
}


// this is same as chkvalidtext  where you also check for numeric ,hypen and underscore - shiraz

function chkValidTxt(obj)
{
    
    obj=trim(obj);
       
    for (i=0;i<obj.length;i++)
    {
		
        if (((obj.charCodeAt(i) >=65) && (obj.charCodeAt(i) <=90)) ||((obj.charCodeAt(i) >=97) && (obj.charCodeAt(i) <=122))  ||  ((obj.charCodeAt(i) >=48) && (obj.charCodeAt(i) <=57)) || ((obj.charCodeAt(i)==95) ||(obj.charCodeAt(i)==45)))
		{
			continue;
		}
		else
		{
		    return false; 
		}
	}
}

// To check for first character

function chkfirstTxt(obj)
{
    
    obj=trim(obj);
       
    for (i=0;i<obj.length;i++)
    {
		
        if (((obj.charCodeAt(i) >=65) && (obj.charCodeAt(i) <=90)) ||((obj.charCodeAt(i) >=97) && (obj.charCodeAt(i) <=122)))
		{
			return true;
		}
		else
		{
		    return false; 
		}
	}
}

// To check the length

function chkuserlen(obj)
{
    
    obj=trim(obj);
    
    return (!(obj.length < 4)) ? true : false; 
    
}



// to check only numeric and alphabetic characters


function chkpwdTxt(obj)
{
    
    obj=trim(obj);
       
    for (i=0;i<obj.length;i++)
    {
		
        if (((obj.charCodeAt(i) >=65) && (obj.charCodeAt(i) <=90)) ||((obj.charCodeAt(i) >=97) && (obj.charCodeAt(i) <=122))  ||  ((obj.charCodeAt(i) >=48) && (obj.charCodeAt(i) <=57)) )
		{
			continue;
		}
		else
		{
		    return false; 
		}
	}
}


//added by Rajesh on 01/02/01
function checkdate(objName) {
	var datefield = objName;
	if (chkdate(objName) == false) {
	return false;
	}
	else {
	return true;
	   }
}
function chkdate(objName) {//Short date should be in format dd/mm/yyyy for this fn
	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("/");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield;
	if (strDate.length < 1) {
	return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			//alert(strDateArray.length)
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
		else {
				err = 1;
				return false;
		}
	}
	
		
	if (booFound == false) {
		err = 1;
		return false;
	}
	if (strYear.length == 2) {
	strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
	}
	
	if (isNaN(strDay)) {
	err = 2;
	return false;
	}
	if (isNaN(strMonth)) {
	err = 2;
	return false;
	}
	if (isNaN(strYear)) {
	err = 2;
	return false;
	}
	
	
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
	err = 2;
	return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
	for (i = 0;i<12;i++) {
	if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
	intMonth = i+1;
	strMonth = strMonthArray[i];
	i = 12;
	   }
	}
	if (isNaN(intMonth)) {
	err = 3;
	return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
	err = 4;
	return false;
	}
	if (strYear >= '0' && strYear <= '1910') {
		err = 1;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
	err = 5;
	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
	err = 6;
	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
	err = 7;
	return false;
	}
	if (intMonth == 2) {
	if (intday < 1) {
	err = 8;
	return false;
	}
	if (LeapYear(intYear) == true) {
	if (intday > 29) {
	err = 9;
	return false;
	}
	}
	else {
	if (intday > 28) {
	err = 10;
	return false;
	}
	}
	}
	/*if (strDatestyle == "US") {
	datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
	datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}*/
	return true;
}
function LeapYear(intYear) {
	if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
	}
	else {
	if ((intYear % 4) == 0) { return true; }
	}
	return false;
}
function doDateCheck(from, to) {//Short date should be in format mm/dd/yyyy for this fn
	if (typeof(from)=='object') from=from.value
	if (typeof(to)=='object') to=to.value
	if (Date.parse(from) <= Date.parse(to)) {
		return true;
	}
	else {
		return false;
	}
}

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;
	var decimalPointDelimiter = "."
    if (isNull(s)) 
       if (isFloat.arguments.length == 1) return false;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (isValidChar(c,'',3)==false) return false;
    }

    // All characters are numbers.
    return true;
}


function isValChar(obj,schr,flg)
{
	if (typeof(obj)=='object') obj=obj.value
	if (flg==1) 
		sarr1='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	else if (flg==2) 
		sarr1='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,./';
	else if (flg==3) 
		sarr1='0123456789';

	sarr1=sarr1 + schr;	
	for(var i=0;i <=obj.length-1;i++)	
	{	
		spflag=(sarr1.indexOf(obj.charAt(i))!=-1)==true ? true:false
		if (spflag==false) return false
	}
	return true;	
}


function IsValidTime(timeStr) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
	
		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 > 13) {           
		return false;
	}
	
	
	if (hour <= 12 && ampm == null) {
		return false;
	}
	
	
	if  (hour > 12 && ampm != null) {
		return false;
	}
	if (minute<0 || minute > 59) {
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		return false;
	}
	return true;
}
