/*
    Javascript Validations
*/
IE=(document.all)?1:0;
NS=(document.layers)?1:0;

var splChar= "*|,\":<>[]{}`\';()&$#%+-@!~^=";

var onlyNum ="1234567890";
var onlyNam ="abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ .-_";

function isSplChar(number,allow)
{	
	var i;
	var err=1;
	var lastocc=0;
	for (var i = 0;i<number.length;i++ )
	{	
		err=1
		for(var j=0; j<splChar.length;j++)
			{
				if (number.charAt(i)==splChar.charAt(j))
				{
					if(allow.length > 0)
					{
						for(var k=0;k<allow.length;k++)
						{
							if (number.charAt(i)==allow.charAt(k))
							{
								if (lastocc==0)
								{
									lastocc=i;
									err=0;
									break;
								}
								else
								{
									err=0;
									if ((i-lastocc)==1)
									{										 
										 return	true;
									}
									else
									{
										lastocc=i;
										err=0;
										break;
									}
								 }
							  }
						  }
			    	 }
					if(err!=0)
					{						
						return true;
					}
				}
			}
			if (number.charAt(i)=="\"") 
			{				
				return true;
			}
			if (number.charAt(i)=="\\") 
			{				
				return true;
			}
	}
	return false;
}



//function for trimming leading and trailing white spaces
function trim(strText) 
{ 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

    return strText;
} 



//For Checking for numbers alone
//return true when all are numbers
function onlyNumbers(numInput)
{
    if(numInput==null || numInput=="")
    {
        return false;
    }
    else
    {
        for (var ni=0; ni<numInput.length;ni++)
        {
           if(onlyNum.indexOf(numInput.charAt(ni)) == -1)
              return false;             
        }
        return true;
    }
    return false;
   
}

//For Checking for valid charachters for name
//return true when all are between a-z or A-Z and for white space and '.' character.
function onlyName(namInput)
{
    if(namInput==null || namInput=="")
    {
        return false;
    }
    else
    {
        for (var na=0; na<namInput.length;na++)
        {
           if(onlyNam.indexOf(namInput.charAt(na)) == -1)
              return false;             
        }
        return true;
    }
    return false;
   
}


//Returns the numerical value for a month if given the argument is in the MMM format.
//Ex for an argument of "Jan", it will return 1.
//please take care of the case for the arguement.

function getMonth(mth)
{
   var mArr = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
   ind = 0;
   for(ii=0;ii<mArr.length;ii++)
   {
       if(mth==mArr[ii])
       {
          ind = ii+1;
          break;
       }   
   } 
   
   return ind;
}

//email validation
//if we enter correct email_id it will return true;
//else it will return false;

function isEmail(fieldName)
{
    var len,i,st,j;	
    var ema=fieldName.value;
    len=ema.length;
    
    if(trim(ema)=="")
    	return false;
		
    if((ema.indexOf('.'))-(ema.indexOf('@'))== -1)
    {
	alert("Please enter a valid Email Address");
	fieldName.select();
	fieldName.focus();
	return true;
    }
	
    if(ema.length<5 || ema.charAt(0)=="@" || ema.charAt(0)==".")
    {
        alert("Please enter a valid Email Address");
        fieldName.select();
	fieldName.focus();
	return true;
    }
    
    for(i=0;i<len;i++)
    {
        if (ema.charAt(i)=="@") break;
    }
    
    if(i==len)  
    {
        alert("Please enter a valid Email Address");
        fieldName.select();
	fieldName.focus();
	return true;
    }
	
    st=i+1;
    
    for(i=st;i<len;i++)
    {
        if ( ema.charAt(i)=="@") 
	{
            alert("Please enter a valid Email 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("Please enter a valid Email Address");
        fieldName.select();
        fieldName.focus();
        return true;
    }

    for(j=i;j<len;j++)
    {
        if(ema.charAt(i)==".") 
	{
	    if (ema.charAt(i-1)==".")
	    {
                alert("Please enter a valid Email Address");
                fieldName.select();
		fieldName.focus();
		return true;
            }
        }
    }
    
    if (ema.charAt(len-1)==".")
    {
        alert("Please enter a valid Email Address");
        fieldName.select();
        fieldName.focus();
        return true;
    }
    
    return false;
}

function isEmptyText(whichString) {
	str = trim(whichString);
	
	 	
	return (str.length == 0);

}

function isANumber(number) 
{
	
	if (isNaN(number)==true)
		{ 
			answer=0;
			
			return false;
		} 
	else
		{
			answer=1;
			return true;
		}
}


// 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 +" cannot exceed "+ length +" characters.");
      fieldName.select();
      fieldName.focus();
      return true;
  }
}


function checkPrecision(fieldName,labelname,fLength,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)=='.')))
		{
			if(!(count==0 && numck.charAt(count)=="-"))
			{
				alert("Please enter a valid number")
				fieldName.select();
				fieldName.focus();
				return true;
			}
		}
	}
	
	var pos1=numck.indexOf('.');
	if(pos1 >= 0)
	{
		var numck1=numck.substring(0,pos1);	
		if(numck1.length > (fLength-precision))
		{
			alert("Please enter a valid number with maximum of "+(fLength-precision)+" digits");
			fieldName.select();
			fieldName.focus();
			return true;
		}

		numck1=numck.substring(pos1+1,numck.length);
		if(numck1.length>precision)
		{
			alert(labelname+" has more than "+ precision+" decimal places.")
			fieldName.select();
			fieldName.focus();
			return true;
		}
	}
	else
	{
		if(numck.length > (fLength-precision))
		{
			alert("Please enter a valid number with maximum of "+(fLength-precision)+" digits");
			fieldName.select();
			fieldName.focus();
			return true;
		}
	}
 	return false;
}


function displaySearch(evt,obj)
{
    
    var x=evt.screenX;
    var y=evt.screenY;
    var scrX=screen.width;
    var scrY=screen.height;
    y=y-254;
    var temp=x+210;
    if(temp>scrX)
     {
      var temp1=temp-scrX;
      x=x-temp1
     } 
    if(y<0)
     {
     y=0;
     }  
     
     window.dateField = obj;                         
     myWin1=window.open('/timetable/jsp/common/calendar.html','Calendar','WIDTH=270,HEIGHT=295');
     myWin1.moveTo(x,y);      
     myWin1.focus();
    
} 


/*--------------------------------------------------------------
	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;
}


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 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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



var param
function funPrintable(url)
{	
	//alert(url);			
    myWin2=window.open(url,'printPage','toolbar=yes,scrollbars=yes,status=yes,WIDTH=650,HEIGHT=450,resizable=yes');	
}


function leadtrim(str)
{
	var x=0;
	var c="";
	//alert(str);
	for (var i=0;i<str.length;i++)
	{
		if(str.charAt(i) == " " || str.charAt(i)  =="\n" || str.charAt(i)  =="\r" || str.charAt(i)  =="\f" )
		{
			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)  ==" " || str.charAt(i)  =="\n" || str.charAt(i)  =="\r" || str.charAt(i)  =="\f")
		{
		  x = i;
		  continue;
	  	}
		else
	         break;
	}
	for(var j=0;j<x;j++)
	{
		c=c+str.charAt(j);
	}
	return c;
}

function trimStr(str)
{
	str=leadtrim(str);

	str=backtrim(str);

	return str;
}
function OpenTimechart()
{
	
	myWin4=window.open("PUTimeChartInfo.servlet",'printPage','toolbar=yes,scrollbars=yes,status=yes,WIDTH=750,HEIGHT=600,resizable=yes');	
}

function OpenPaxDownload()
{
    
	myWin5=window.open("/timetable/jsp/publicuser/passengerDownload.jsp",'paxDownload','WIDTH=300,HEIGHT=150,move');	
	myWin5.moveTo(200,200);
}

function OpenFreightDownload()
{
	myWin6=window.open("/timetable/jsp/publicuser/freighterDownload.jsp",'freightDownload','WIDTH=300,HEIGHT=150');	
	myWin6.moveTo(150,150);
}
