IE=(document.all)?1:0;
NS=(document.layers)?1:0;

//var whitespace = " \t\n\r";
//var bool = false;
//var splChar=".!@#$%^&*()_+|}{:><,?';/`~=-[]";

// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
// Check whether string str contains spaces
// returns true, if a space is found


function isSpaces(fieldName, lablename) 
{
    var str=fieldName.value;
    var numspaces = 0;
    
    for(var count=0; count < str.length; count++)
    {
        if(str.charAt(count) == ' ') 
        {
            numspaces++;
        }
    }

    if (numspaces != 0)
    {
        if (numspaces == str.length)
        {
            alert(lablename + " cannot be spaces !")
            fieldName.select();
            fieldName.focus();
            return true   
        }
    }
    
    if(str.charAt(0) == ' ') 
    {
        alert(lablename + " has leading spaces !")    
        fieldName.select();
        fieldName.focus();
        return true
    }

    if(str.charAt(str.length-1) == ' ') 
    {
    	alert(lablename + " has trailing spaces !")            
    	fieldName.select();
        fieldName.focus();        
        return true
    }
            
    return false;
}
  
function isSpecialCharacter(fieldName, lablename, specialchar)
{
    var str=fieldName.value;
   
    var flag = false;
    
    for(var count=0; count < str.length; count++)
    {
        if(str.charAt(count) == specialchar) 
        {
            flag = true;
        }
    }
    
    if (flag)
    {
        alert(lablename + " cannot have '" + specialchar + "' !")
        fieldName.select();
        fieldName.focus();        
        return true;
    }
    
    return false;
}

function isSpecialCharacters(fieldName, lablename, insplchars)
{
    var exsplChar=".!@#$%^&*()_+|}{:><,?';/`~=-[]";
    
    var str=fieldName.value;
   
    var flag = false;
    
    for(var count=0; count < str.length; count++)
    {
        if((insplchars == "") && (exsplChar.indexOf(str.charAt(count)) != -1)) 
        {
            flag = true;
            alert(lablename + " cannot have special characters.")
            break;
            
        }
        else if((insplchars.indexOf(str.charAt(count)) == -1) && (exsplChar.indexOf(str.charAt(count)) != -1)) 
	{
	    flag = true;
	    alert(lablename + " cannot have special characters.")
	    break;
	}
    }
    if (flag)
    {
    	fieldName.select();
        fieldName.focus();        
        return true;
    }
    
    return false;
}
    

// Check whether string str is Empty/Null
// returns true, if str is Empty/Null

function isEmpty(fieldName, lablename) 
{
    var str=fieldName.value;
    
    if((str == null)||(str.length==0)) 
    {
        alert(lablename + " cannot be empty")
        fieldName.select();
        fieldName.focus();        
        return true;    
    }
    
    return false;
}

// Check whether string str is AlphaNumeric
// returns true, if str is Alphanumeric

function isAlphaNumeric(fieldName,labelname) 
{
var str=fieldName.value;
// alert("the value is:	"+str);
	for(var count=0; count < str.length; count++)
	{
		if(((str.charAt(count)>='0')&&(str.charAt(count)<='9'))
		  ||(((str.charAt(count)>='A')&&(str.charAt(count)<='Z'))
  		  ||((str.charAt(count)>='a')&&(str.charAt(count)<='z')))) 
  		 {
			continue;  		   
  		 }		
  		else  
  		{
			alert(labelname + "is not alpha-numeric.Please enter a alpha-numeric value")
			fieldName.select();
			fieldName.focus();
			return true;
  		}
    	}
  return false;
}

// Check whether string str is Numeric
// returns true, if str is Numeric
// function isNumeric(str,label)


function isNumeric(fieldName,labelname)
{
 	var str=fieldName.value;

 	for(var count=0; count<str.length; count++)
 	{
 		if(((str.charAt(count)>='0')&&(str.charAt(count)<='9'))) 
 		{  		
	  		continue;
  		}
  		else  
  		{
   			alert(labelname +" is not a number.Please enter numbers");	
   			fieldName.select();
  			fieldName.focus();
 			return true;
  		}
  	   }
     	return false;   
}

// code for text area.
//here we r checking only the length of the textarea

function textArea(fieldName,labelname,length)
{
  var str=fieldName.value;
  if(str.length <=length)
  {
      return false;
  }
  else
  {
      alert(labelname +"s cannot exceed "+ length +" characters !");
      fieldName.select();
      fieldName.focus();
      return true;
  }
}

//check whether the givne number is negative or not
//return true,if the number is negative.

function isNumNegative(fieldName,labelname)
{
	var str=fieldName.value;

	if((str.charAt('0')=='-')||(str.indexOf('-')==0))
	{
		alert(labelname+"cannot be nagative!");
		fieldName.select();
		fieldName.focus();
		return true;
	}
	
	return false;
}
	

// check the precision
//the precision value is '2' then this is 45.34 is correct one.
//enter the value after decimal that should be lessthan or equal to precision. 
//the precision value is '5' then this is 45.3445455 is wrong one.

//code has been modified on 15-05-01

function checkPrecision(fieldName,labelname,precision)
{
	var numck=fieldName.value;
	for(var count=0;count<numck.length;count++)
		{
			if(((numck.charAt(count)>='0')&&(numck.charAt(count)<='9'))||(numck.charAt(count)=='.'))
			{

			  var len=numck.length;			
			  var pos1=numck.indexOf('.')			

			if(pos1==-1)
			{
				numck+=".00";			
				break;
			}

			if((pos1!="")||(pos1!=null))
				{
				var pos2=numck.substring(pos1+1,len)
				

			       if((pos2.length) > precision)
					{
					   alert(labelname+" has more than "+ precision+" decimal places.")
					   fieldName.select();
					   fieldName.focus();
					   return true;


					} //end of pos2
				else
					{
						return false;
					}
				}
			}//end of main if below for loop...
		}// end of forloop

 return false;
} //end of function

// Check whether string str is Alphabetic
// returns true, if str is Alphabetic

function isAlphabetic(fieldName,labelname) {

var str=fieldName.value;

  	for(var count=0; count < str.length; count++)
  	{
  		if (((str.charAt(count)>='A')&&(str.charAt(count)<='Z')) 
  		   	||((str.charAt(count)>='a')&&(str.charAt(count)<='z')))
  		   		{continue;}
  		else  
  		{
  			alert(labelname +"	the value should be alphabet!");
  			fieldName.select();
  			fieldName.focus();
  			return true;
  		}
  	}
  	   
  	return false;
}

//date validation is here....

function dateCheck(fromdate,fromlabelname,todate,tolabelname)
{
	var fromDate=fromdate.value;
	var toDate=todate.value;
	var bool=false;

	if((fromDate != null) ||(fromDate != ""))
	{
		for(var count=0;count<=fromDate.length;count++)
		{ 
			if((fromDate.charAt(count) >='0')&&(fromDate.charAt(count) <='9')||
				(fromDate.indexOf('/')==2)||(fromDate.indexOf('/')==5))
			{	
				var fromDay=fromDate.substring(0,2);
				var fromMonth=fromDate.substring(3,5);
				var fromYear=fromDate.substring(6,10);
			}
			else
			{
			  	alert(fromlabelname+"Enter valid date....");
			  	fromdate.focus();
			  	return true;
			}
		}
	}
	
	if((toDate != null) ||(toDate != ""))
	{
		for(var count=0;count<=toDate.length;count++)
		{ 
			if((toDate.charAt(count) >='0')&&(toDate.charAt(count) <='9')||
				(toDate.indexOf('/')==2)||(toDate.indexOf('/')==5))
			{	
				var toDay=toDate.substring(0,2);
				var toMonth=toDate.substring(3,5);
				var toYear=toDate.substring(6,10);
			}
			else
			{
				todate.focus();
				return true;
			}
		}
	}
     bool=compareDate(fromDay,fromMonth,fromYear,toDay,toMonth,toYear);
  		if(bool==false)
  			{
  				alert(tolabelname+" should be greaterthan "+fromlabelname);
  				todate.focus();
  				return true;
  			}
    return false
}

//dates comparison is going on here....

function compareDate(d1,m1,y1,d2,m2,y2)
{
	if(y2 < y1) return false;
	else if((y2 == y1)&&(m2 < m1)) return false;
	else if((y2 ==y1)&&(m2==m1)&&(d2<d1)) return false;
	else
	{
		return true;
	}	
}

//email validation
//if we enter correct email_id it will return true;
//else it will return false;

function isEmail(fieldName,labelname)
{
    var len,i,st,j;	
    var ema=fieldName.value;
    len=ema.length;
    
    if(trim(ema)=="")
    	return false;
		
    if((ema.indexOf('.'))-(ema.indexOf('@'))== -1)
    {
	alert("Invalid " + labelname + " address");
	fieldName.select();
	fieldName.focus();
	return true;
    }
	
    if(ema.length<5 || ema.charAt(0)=="@" || ema.charAt(0)==".")
    {
        alert("Invalid " + labelname + " address");
        fieldName.select();
	fieldName.focus();
	return true;
    }
    
    for(i=0;i<len;i++)
    {
        if (ema.charAt(i)=="@") break;
    }
    
    if(i==len)  
    {
        alert("Invalid " + labelname + " address");
        fieldName.select();
	fieldName.focus();
	return true;
    }
	
    st=i+1;
    
    for(i=st;i<len;i++)
    {
        if ( ema.charAt(i)=="@") 
	{
            alert("Invalid " + labelname + " address");
            fieldName.select();
	    fieldName.focus();
	    return true;
	}
    }
    
    for(i=st;i<len;i++)
    {
	if(ema.charAt(i)==".") break;
    }
	
    if(i==st || i==len || i==(len-1))
    {
        alert("Invalid " + labelname + " address");
        fieldName.select();
        fieldName.focus();
        return true;
    }

    for(j=i;j<len;j++)
    {
        if(ema.charAt(i)==".") 
	{
	    if (ema.charAt(i-1)==".")
	    {
                alert("Invalid " + labelname + " address");
                fieldName.select();
		fieldName.focus();
		return true;
            }
        }
    }
    
    if (ema.charAt(len-1)==".")
    {
        alert("Invalid " + labelname + " address");
        fieldName.select();
        fieldName.focus();
        return true;
    }
    
    return false;
}

//function for telephone number check
//if the given number is valid it will return true;
//else it will return false;

function isTelephone(fieldName,labelname)
{
    var no=fieldName.value;
    var len=no.length;

    if (isSpaces(fieldName, labelname))
    {
        return true;
    }
    
    if (isSpecialCharacters(fieldName,labelname,"+() -"))
    {
        return true;
    }
    
    for(var i=0;i<no.length;i++)
    {
        if(((no.charAt(i) >= 'a')&&(no.charAt(i) <='z'))
             ||((no.charAt(i) >= 'A')&&(no.charAt(i) <='Z')))
        {
            alert( "Invalid " + labelname + " !");
            fieldName.select();
            fieldName.focus();
            return true;
        }
    }
        
    return false;
}

// Check whether string str is valid day time
// returns true, if time is valid otherwise returns false

function isValidTime(fieldName, lablename) 
{
    var str=trim(fieldName.value);
    fieldName.value=str;
    
    if(str != null && str.length>0) 
    {
	if(str.length<4)
     	{
       		alert("Please note that timings must be presented in the 24-hour format. Invalid "+lablename);
       		fieldName.select();
       		fieldName.focus();        
       		return false;
	}
    	for(var count=0; count<4; count++)
 	{
 		if(((str.charAt(count)>='0')&&(str.charAt(count)<='9'))) 
 		{  		
			continue;
  		}
  		else  
  		{
   			alert("Invalid "+lablename);
   			fieldName.select();
  			fieldName.focus();
 			return false;
  		}
     	}     	
        var strHours = str.substring(0,2);
        var strMin = str.substr(2);
        if(parseInt(strHours)>23 || parseInt(strHours)<0 || parseInt(strMin)>59 || parseInt(strMin)<0)
        {
        	alert("Invalid "+lablename);
        	fieldName.select();
		fieldName.focus();        
        	return false;
        }
        
        if(str.length>4)
        {
        	if(str.length!=6)
        	{
			alert("Invalid "+lablename);
   			fieldName.select();
  			fieldName.focus();
 			return false;
        	}
        	else
        	{
        		var v1= str.charAt(4);
        		if(v1!='+' && v1!='-')
        		{
				alert("Invalid "+lablename+". 5th char should be either + for arr/dep on the next day  or - for arr/dep on the day before");
   				fieldName.select();
  				fieldName.focus();
 				return false;
        		}
			v1= str.charAt(5);
        		//if(v1!='1' && v1!='2' )
        		if(v1!='1' && v1!='2'  && v1!='3')
        		{
				alert("Invalid "+lablename+". 6th char should be either 1 or 2");
   				fieldName.select();
  				fieldName.focus();
 				return false;
        		}
        	}
        }
    }    
    return true;
}

	function leadtrim(str)
	{	
		var x=0;
		var c="";
		//alert(str);
		for (var i=0;i<str.length;i++)
		{
			if(str.charAt(i) == " ")
			{			
				x=i+1;
				//alert(x);
				continue;
			}
			else
				 break;
		}	
		for(var j=x;j<str.length;j++)
		{
			c=c+str.charAt(j);
		}
		return c;
	}

	function backtrim(str)
	{	
		var x=str.length;

		var c="";

		for (var i=str.length-1;i>=0;i--)
		{
			if(str.charAt(i)  ==" ")
			{
			  	x = i;
				continue;
		  	}
	  		else
	     			break;
		}
		for(var j=0;j<x;j++)
		{
			c=c+str.charAt(j);
		}
		return c;
	}

	function trim(str)
	{
		str=leadtrim(str);

		str=backtrim(str);

		return str;
	}
	
//date validation is here....

function dateDiff(fromDate,toDate)
{
	var strTemp;
	if((fromDate != null) ||(fromDate != ""))
	{
		strTemp = fromDate.substring(0,2);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var fromDay=parseInt(strTemp);
		strTemp = fromDate.substring(3,5);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var fromMonth=parseInt(strTemp);
		var fromYear=parseInt(fromDate.substring(6,10));		
	}
	
	if((toDate != null) ||(toDate != ""))
	{
		strTemp = toDate.substring(0,2);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var toDay=parseInt(strTemp);
		strTemp = toDate.substring(3,5);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var toMonth=parseInt(strTemp);
		var toYear=parseInt(toDate.substring(6,10));		
	}
        
    if(toYear < fromYear)
    	return -1;
    else
    if((toYear == fromYear) && (toMonth < fromMonth))
  		return -1;
  	else
  	if((toYear == fromYear) && (toMonth == fromMonth) && (toDay < fromDay))
  		return -1;
  	else
  	if((toYear == fromYear) && (toMonth == fromMonth) && (toDay == fromDay))
  		return 0;
  	else
  	{
		date1 = new Date();
		date2 = new Date();
		diff  = new Date();

		date1temp = new Date(fromYear,fromMonth-1,fromDay);
		date1.setTime(date1temp.getTime());
	
		date2temp = new Date(toYear,toMonth-1,toDay);
		date2.setTime(date2temp.getTime());

		diff.setTime(Math.abs(date2.getTime() - date1.getTime()));

		timediff = diff.getTime();		
		return Math.floor(timediff / (1000 * 60 * 60 * 24))+1;		
  	}
}

function getDayofGivenDate(fromDate)
{
	var strTemp;
	if((fromDate != null) ||(fromDate != ""))
	{		
		strTemp = fromDate.substring(0,2);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var fromDay=parseInt(strTemp);
		strTemp = fromDate.substring(3,5);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var fromMonth=parseInt(strTemp);
		var fromYear=parseInt(fromDate.substring(6,10));		
	}	
    	var date1 = new Date(fromYear,fromMonth-1,fromDay);
	var dayOfOps = date1.getDay();	
	if(dayOfOps==0)
		dayOfOps=7;
		
  	return dayOfOps;
}

function addDays(fromDate,noOfDaysToAdd)
{
	if(noOfDaysToAdd == 0)
		return fromDate;
		
	strTemp = fromDate.substring(0,2);
	if(strTemp.charAt(0)=='0')
		strTemp=strTemp.substr(1);
	var fromDay=parseInt(strTemp);
	strTemp = fromDate.substring(3,5);
	if(strTemp.charAt(0)=='0')
		strTemp=strTemp.substr(1);
	var fromMonth=parseInt(strTemp);
	var fromYear=parseInt(fromDate.substring(6,10));
	
	date1temp = new Date(fromYear,fromMonth-1,fromDay);
	
	date1 = new Date();
	date1.setTime(date1temp.getTime()+(1000 * 60 * 60 * 24 * noOfDaysToAdd));
	
	newDay = date1.getDate();
	if(newDay<10)
		newDay = "0"+newDay;
	newMonth = date1.getMonth()+1;
	if(newMonth<10)
		newMonth = "0"+newMonth;
	
	return newDay+"/"+newMonth+"/"+date1.getYear();
}

/*--------------------------------------------------------------
	Function Name: isValidDate
	Purpose      : This Function Will Check For validity of  a
   		 	   Date value(dd/mm/yyyy)
   			   Should Be called in "onChange" event of element.
	Argument 	 : field   - Reference to the element(this)
		   	   required- 'Y' (Will not allow focus to 
					change without entering a valid    Date) 
 				 'N' 
	Return Value : Boolean
	Example	 	 : <INPUT   type="text" 
		      		 	Name="xxx"
	    				onChange="return isValidDate(this,'N')">

  ---------------------------------------------------------------*/

function isValidDate(field,required)
{
	if(field.value=="")
	{	
		if(required=="Y")
			{
//				alert("Value is Required For This Item");
				if(IE)
				{
					field.select();
					field.focus();
				}
				return false;
			}
			return true;
	}
		
	var abc=field.value;
	
	var a1 = abc.substring(0,abc.indexOf("/"));
	var a2 = abc.substring(abc.indexOf("/")+1,abc.lastIndexOf("/"));
	var a3 = abc.substring(abc.lastIndexOf("/")+1,abc.length);
	
        //alert(a1);
        //alert(a2);
        //alert(a3);
        
	if(isNaN(a1) || isNaN(a2) || isNaN(a3))
	{
           //alert("Invalid Date");
           	if(IE)
		{
			field.select();
			field.focus();
		}
	   return false;	
	}

	var day = parseFloat(a1);
	var month = parseFloat(a2);
	var year = parseFloat(a3);
	
	//var day= parseFloat(abc.substring(0,abc.indexOf("/")));
	//var month=parseFloat(abc.substring(abc.indexOf("/")+1,abc.lastIndexOf("/")));
	//var year =parseFloat(abc.substring(abc.lastIndexOf("/")+1,abc.length));
         
			
	<!--- Return If some Characters Enterd --->
	if (isNaN(day) ||isNaN(month)||isNaN(year))
	{
// 		alert("Invalid Date");
				if(IE)
				{
					field.select();
					field.focus();
				}
		return false;	
	}
		
	<!--- Validate Year --->
	if((year<1900)||( year >9999))
	{
// 		alert("Invalid Date");
				if(IE)
				{
					field.select();
					field.focus();
				}
		return false;	
	}
	<!--- Validate Month --->
	if((month>12)||(month < 1))
	{
//		alert('Invalid Date');
		if(IE)
						{
							field.select();
							field.focus();
				}
		return false;
	}
	
	<!--- Validate Days based on Month --->
	if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
	{
		if((day>31)||(day<1))
		{
//			alert('Invalid Date');
			if(IE)
							{
								field.select();
								field.focus();
				}
			return false;
		}
	}
	else if((month==4)||(month==6)||(month==9)||(month==11))
	{
		if((day>30)||(day<1))
		{
//			alert('Invalid Date');
			if(IE)
							{
								field.select();
								field.focus();
				}
			return false;
		}
	}
	else 
	{
		if((year%4)==0)
		{
			if((day>29)||(day<1))
			{
//				alert('Invalid Date');
				if(IE)
								{
									field.select();
									field.focus();
				}
				return false;
			}
		}
		else
		{	
			if((day>28)||(day<1))
			{
//				alert('Invalid Date');
				if(IE)
								{
									field.select();
									field.focus();
				}
				return false;
			}
		}
	}
	if(day<10)
		day="0"+day;
	if(month<10)
		month="0"+month;
	field.value = day + "/" + month + "/" + year;
	return true;
}



//added by Jagan

function hasSpecialCharacters(fieldName, insplchars)
{
    var exsplChar=".!@#$%^&*()_+|}{:><,?';/`~=-[]";
    
    var str=fieldName.value;    
    
    for(var count=0; count < str.length; count++)
    {
        if((insplchars == "") && (exsplChar.indexOf(str.charAt(count)) != -1)) 
        	return true;        
        else 
        if(insplchars.indexOf(str.charAt(count)) != -1) 	
	    return true;	
    }    
    return false;
}

var first=0;

function callAfter5min()
{
	setTimeout("callAfter5min()",1000*300);
	if(first==1)
	alert("You can save your workings using 'Save Draft' option");
	first=1;
}
