/*Check for valid Email*/
function chk_email(str) 
{
  var supported = 0;
  if(!chkempty(str, "Email")) return false;
  if (window.RegExp)
  {
	  var tempStr = "a";
	  var tempReg = new RegExp(tempStr);
	  if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported)
  	  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r3 = RegExp("^.+ .+$");

  var r2 = new
  RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  if(  (!r1.test(str) && r2.test(str) && !r3.test(str)) == false)
  {
                alert("'" + str + "' is invalid email address.");
                //eval(name + ".focus()");
                return false;
  }
 else   return true; 
}

//function added by Ganesh for date validation


function chk_date(date1)
 {

	var datePat =/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    dateStr=date1;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null)
	{
		alert(document.forms[0].letter_dt.value + " Date is not in a valid format.")
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[5];

	if (month < 1 || month > 12)
	{ // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31)
	{
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
		alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2)
	{
	    // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap))
		{
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
        return true;

   }
 
 
 
 
 
/*Check for Empty*/
function chkempty(temp,temp1,par)
{
if (temp==0 && !par)
{
	   alert("Enter "+temp1);
	   return false;
}
else if(temp!=0 && par>0)
{
	switch(par) {
	case 1:
		if((chkinvalidchar(temp,temp1))&&   
		   (chk_spaces(temp,temp1))) return true;
		else return false;
		break;
	case 2:
	       if(!IsCorrectFormat(temp)) return false;
	       if(!ValidDate(temp)) return false;
	       return true;
	       break;
	case 3:
	      if (chk_email(temp)) return true;
	      else return false;
	      break;
	}
}
return true;
}

/*Check listbox value is selected*/
function chk_selected(temp,temp1,temp2)
{
	if (temp<=0)
        {
           alert("Select "+temp1);
           return false;
        }
	return true;
}
function chk_selected(temp,temp1,temp2)
{
	if (temp==false)
        {
           alert("Select "+temp1);
           return false;
        }
	return true;
}
/*Check empty and focus*/
function chkempty1(temp,temp1)
{
	if (eval("document.forms[0]."+temp+".value")==0)
	{
	   alert("Enter "+temp1);
	   eval("document.forms[0]."+temp+".focus()");
	   return false;
	}
	return true;
}

/*Check Valid IP*/
function chkvalidip(IPvalue,theName) {
errorString = "";
if ((theName!='Match On (IP Address)')&&(IPvalue!='any')){
var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString= errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString=errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
}
extensionLength = 3;
if (errorString == "") return true;
else { 
alert (errorString);
return false;
}
}
      
/*Jump to the Link*/
function gobackrefresh(fname)
{
var x=navigator.appName;
if(x=="Netscape")
document.location.href=fname;
else
window.location.replace(fname);
}

/*Check for phone no. allow(0-9) and "-" */
function chkphone(temp,temp1)
{
regex = new RegExp("^.+[0-9\\-\\.]$");
r3 = RegExp("^.+[a-zA-Z].+$");
if(temp)
{
	var flag=regex.test(temp);
	var flag1=r3.test(temp);
	if (flag==false || flag1==true)
	{
		alert(temp1+" contains invalid characters.");
		return false;
	}
}
return true;
}

/*Check for invalid char*/
function chkinvalidchar(temp,temp1)
{
regex=new RegExp("[!@#%$^&*()=]","g");
var flag=regex.test(temp);
if (flag==true)
{
	alert(temp1+" contains invalid characters.");
	return false;
}
return true;
}

function compare_passwd(temp,temp1)
{
var temp2=new String(temp);
if(temp2.length<6)
{
	alert("Password should be minimum 6 charcters.");
    	return false;
}
if(temp!=temp1)
	{
	alert("Password should match confirm password.");
    	return false;
	}
return true;
}
function chkfile(temp,temp1)
{
	alert(temp);
        if(temp=='none'){		
    		alert(temp1);
    		return false;
	}
	return true;
}

function chknumeric(temp,temp1)
{
        if(isNaN(temp)){		
    		alert(temp1+" must be numeric.");
    		return false;
	}
	return true;
}

/////To check amount (should be numeric and greater than zero)
function chkamount(temp,temp1,par)
{
        if(par){
        	if(!chkempty(temp,temp1)) 
        		return false;
        }
        if(temp){
        	if(isNaN(temp)){		
        		alert(temp1+" must be numeric.");
			return false;
		}
		if(Number(temp) < 0){		
				alert(temp1+" must be greater than zero.");
				return false;
		}
	}
	return true;
}


function chk_spaces(temp,temp1)
{
regex=new RegExp(" +","g");
var flag=regex.test(temp);
if (flag==true)
        {
        alert(temp1+" should not contain space character.");
        return false;
        }
return true;
}

function firstelement(i)
{
if(document.forms.length)
document.forms[0].elements[i].focus();
}                                                 

//This function checks the format of date dd-mm-yyyy
function IsCorrectFormat(datefield)
{
  if(datefield=="")
  {	
  	//alert("Date field cannot be empty");    
    	flag+=1;
  }
  else
  { 
	  var flag = 0;
	  var s = datefield;
	  var j = s.split("/");
	  if (isNaN(j[0]))
	   flag+= 1;
	  if (isNaN(j[1]))
	   flag+= 1;
	  if (isNaN(j[2]))
	     flag+= 1;
	  else
	    {
	       if( j[2].length < 4)  
	       flag+=1;
	    }

  }   
  if(flag!=0) 
  {
  	alert("Invalid date format")
    	return false;
  }
  else 
    	return true;
}
 
//This Function Checks Whether Valid Date is entered or not.
function ValidDate(datefield)
{
   var s = datefield;
   var myF = 0;
   if(datefield == "")
        myF+=1;
   else
   {
 	if(!IsCorrectFormat(datefield)) //if invalid date then 
	  myF+=1;
	else //correct date format, check if this is a valid date ??
	 {
	   	var j = s.split("/");
	        var dd = Number(j[0]);
		var mm = Number(j[1]);
		var yy = Number(j[2]);
		var NFlag  = "0";
		if(mm>12 || dd > 31 || dd < 1 || mm < 1)
		{
		 myF+=1;
		} 
		else 
		 if (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
		 {
		     if(dd > 31)
		         myF+=1;
		 }
		 else if (mm==4 || mm==6 || mm==9 || mm==11)
		 {
		    if(dd > 30)
		 	    myF+=1;
		 }
		 else if (mm==2)
		 {
		   	
		    	/* New Modifed Code Below*/
			var isleap = (yy % 4 == 0 && (yy % 100 != 0 || yy % 400 == 0))			
			if (dd>29 || (dd==29 && !isleap))
				myF+=1;				  
				 
		 }
		 if (yy > 9999 || yy <= 1800)
		 {
		    myF+=1;
		 }
	}
  }	 
  if(myF==0)
  	 return true;
  else
  {
  	alert("Enter Valid date")
   	return false; 
   }
 } 

//Function to compare today's date with some other date..
function CompareToday(findt,issdt,str)
{
	var i = ChangeFormat(findt);
	var f = ChangeFormat(issdt);
	var findt= new Date(i.substring(6,10),Number(i.substring(3,5))-1,i.substring(0,2));
	var issdt= new Date(f.substring(6,10),Number(f.substring(3,5))-1,f.substring(0,2));
	if(issdt < findt)
	{
		alert(str+" should be less than today's date");
		return false;
	}
	return true; 
}

//compare Expiry date
function CompareExpiry(findt,issdt,str)
{
        var i = ChangeFormat(findt);
        var f = ChangeFormat(issdt);
        var findt= new Date(i.substring(6,10),Number(i.substring(3,5))-1,i.substring(0,2));
        var issdt= new Date(f.substring(6,10),Number(f.substring(3,5))-1,f.substring(0,2));
	if(issdt > findt)
	{
		alert(str+" should be today's date or Greater than today's date ");
		return false;
	}
	return true;
}

//Compares date1 with date2
function Compare(date1,date2,str1,str2)
{
	var i = ChangeFormat(date1);
	var f = ChangeFormat(date2);
	var issdt = new Date(i.substring(6,10),Number(i.substring(3,5))-1,i.substring(0,2));
	var findt = new Date(f.substring(6,10),Number(f.substring(3,5))-1,f.substring(0,2));
	if(issdt > findt)
	{
		alert(str1 + " should be greater than " + str2 );
		return false;
	}
	else
		return true; 
}


//This function converts a dates like 2/2/2000 to 02/02/2000 
function ChangeFormat(datefield)
{
   var s = datefield;
   var j = s.split("/");
   var dd = Number(j[0]);
   var mm = Number(j[1]);
   var yy = Number(j[2]);
   var mystr;
   if (dd<10 && mm<10)
    mystr = "0" + dd + "/0" + mm + "/" + yy;
   else if (mm<10)
    mystr = dd + "/0" + mm + "/" + yy;
   else if (dd < 10)
    /*mystr = "0" + dd + "/0" + mm + "/" + yy; */
    /* New Modifed Code Below*/
    mystr = "0" + dd + "/" + mm + "/" + yy; 
   else
    mystr = s; 
   return mystr;   
} 

function showWin(temp,temp1,temp2,temp3)
{
	var mybar="directories=no, location=no, menubar=no, status=no";
	mybar+=",titlebar=no, toolbar=no,scrollbars=yes";
	mybar+=",width="+temp2+", height="+temp3+",resizabel=no,top=0,left=0";
	newwin=window.open(temp,temp1,mybar);
	newwin.document.close();
	newwin.focus();
}

function FillSelectCombo(temp,temp1,temp2)
{
var ind=document.forms[0].elements[temp1].selectedIndex;
var curdata=document.forms[0].elements[temp1].options[ind].value;
var opt=1;
if(temp2) {
	var ind1=document.forms[0].elements[temp2].selectedIndex;
	var cmpdata=document.forms[0].elements[temp2].options[ind1].value;
}
	
for (k=document.forms[0].elements[temp].options.length-1;k>=0;k--)
	 document.forms[0].elements[temp].options[k]=null;

document.forms[0].elements[temp].options[0]=new Option('--Select--','0');
for(i=0;i<dt.length;i++){
	var strary=dt[i].split("|");
	if(strary[0]=='ND'){
	document.forms[0].elements[temp].options[0]=new Option(strary[1],strary[0]);
	document.forms[0].elements[temp].options[0].selected=true;
	break;
}

if(temp2 && strary[1]==cmpdata){ continue; }

if(strary[0]==curdata){
	if(strary.length > 2) 
	   document.forms[0].elements[temp].options[opt]=new Option(strary[2],strary[1]);
	else if(strary.length==2) 
	   document.forms[0].elements[temp].options[opt]=new Option(strary[1],strary[0]);
	   opt++;
	}
	document.forms[0].elements[temp].options[0].selected=true;
    }
}

function ComparetNumber(num1,num2,msg1,msg2)
{
if(num2>0 && num1>0){	
        if(Number(num2)>Number(num1))
                return true;
        else {
                alert(msg2+" should be larger than "+msg1+".");
                return false;
             }
}
return true;
}

//function to multiply amount and check the value not greater than
function CalculateAmount(No1,No2)
{
	No = No1 * No2;
	if (No > "99999999")
	{
		alert("Number cannot be greater than ");
		return false;
	}
	else 	
		return true;
}

function Call_close_refresh(fname) {
	var x=navigator.appName;
	if(x=="Netscape")
	parent.document.location.href=fname;
	else
	opener.document.location.replace(fname)
	window.close()
}


////* To add n Number of years to given date
function YearAdd(startdate, years)
{
  var stdate , months ,cldt ;
  stdate = startdate ;
  months = years * 12 ;

  if ( isNaN(months) == false)
  {
         var tempArr , tempStr ,resDay , resMonth , resYear ,addYear , addMonth , dtDay , dtMonth , dtYear ;
	     tempStr = new String(stdate) ;
	     tempArr = tempStr.split("/") ;

	     var myClDt = new Date(tempArr[2],tempArr[1],tempArr[0]);
	     resDay   = tempArr[0] ;
	     resMonth = tempArr[1] ;
	     resYear  = tempArr[2] ;

	     addYear  = 0;
	     addMonth = 0 ;

	     if (  Number(months)  >= 12 )
	     {
	       addYear = Math.floor( Number(months) / 12 ) ;
	       addMonth = Number(months) - ( addYear * 12 ) ;
	     }
	     else
	     {
	       addYear = 0 ;
	       addMonth = Number(months) ;
	     }

	     dtMonth = Number(resMonth) + Number(addMonth) ;
	     dtYear = Number(resYear) + Number(addYear)

	     if (Number(dtMonth) > 12)
	     {
	       dtMonth = Number(dtMonth) - 12 ;
	       dtYear = dtYear + 1
	     }


	     if ( Number(dtMonth) == 2)
		 {
			  if ( Number(resDay) > 28 )
			  {
			    dtDay = "28"
			  }
			  else
			  {
			    dtDay = resDay ;
			  }
		 }
		 else
		 {
			  dtDay = resDay ;
		 }


	     if ( Number(resDay) == 31)
		 {
			  if ( Number(dtMonth) == 1 ||  Number(dtMonth) == 3 || Number(dtMonth) == 5 || Number(dtMonth) == 7 || Number(dtMonth) == 8  || Number(dtMonth) == 10  || Number(dtMonth) == 12 )
			  {
			    dtDay = "31"
			  }
			  else
			  {
			    dtDay = "30" ;
			  }
		 }
		 else
		 {
			  dtDay = resDay ;
		 }

	     return ( dtDay + "/" + dtMonth  + "/" + dtYear );
  }
}
