/*
This page has most of the functions used for Basic Client validations
 Page Name : \includes\ClientValidations.js
 Called by : 
 Calls  : - None
 Input Form Data : - 
 Output Form Data : 
 Input Session Data :-
 Outut Session Data : 
 Database tables : 
 Change History :
 --------------------------------------------------------------------------------------------------------
 | Date         | Programmer  | Description                                                               |
 --------------------------------------------------------------------------------------------------------
 |		|K.G.S.Karanth|	Coded and collected 	
 --------------------------------------------------------------------------------------------------------
*/


function isValidDateText(inDate,name)   //If date pass from text
{
    var jsdate = new  Date();
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = inDate.value.match(datePat); // is the format ok?
	if (matchArray == null) {
		alert(name + " Date is not in a valid format.\n");    
		return false;
}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if ((year.length) < 4)
		{
			year= ("20" + year);
	
		}
	if (year < 1900) 
	{ // check year range
		alert(name + " Year must be greater than 1900.\n");
		return false;
	}
	if (month < 1 || month > 12) 
	{ // check month range
		alert(name + " Month must be between 1 and 12.\n");
		return false;
	}
	if (day < 1 || day > 31) 
	{   
	    alert(name +" Day must be between 1 and 31.\n");
		return false;
	}
if ((month==4 || month==6 || month==9 || month==11) && day==31) 
{       
        alert(name + " Month "+month+" doesn't have 31 days!\n");
	    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(name + " February " + year + " doesn't have " + day + " days!\n");
	    return false;
   }
}
	return true;  // date is valid */

}


function isNotDateFieldEmptyCompare(field1, name1, field2, name2)   // check for length of the date field , if length exists check for proper date
{
  if (field2.value.length > 0) 
    {
		if (checkRange1(field1, name1, field2, name2)==true )
		return true;
	 }
  else if (field2.value.length == 0) {
	return true;
  }
  else return false;
}


 function checkRange1(fromdate,fromname,todate,toname) 
 {	 
	var a = isValidDateText(fromdate,fromname) 
	var b = isValidDateText(todate,toname)

	if( a== true && b==true)
    {
         var jsdate = new  Date();
	      var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	      var matchArray = fromdate.value.match(datePat); // is the format ok?
	      month = matchArray[1]; // parse date into variables
	      day = matchArray[3];
	      year = matchArray[4];
	      if ((year.length) < 4)
		      {
			        if (year <= 50){
			      
			        year= ("20" + year)}
			      else {
			      
			        year= ("19" + year)
	               }
	
		      }
          var fromd = month+'/'+day+'/'+year;
          fromdate.value=fromd;
          var jsdate = new  Date();
	      var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
          var matchArray1 = todate.value.match(datePat); // is the format ok?
	      month1 = matchArray1[1]; // parse date into variables
	      day1 = matchArray1[3];
	      year1 = matchArray1[4];
	      if ((year1.length) < 4)
		      {
			      if (year1 <= 50){
			      
			        year1= ("20" + year1)}
			      else {
			      
			        year1= ("19" + year1)
	               }
		      }
            var tod = month1+'/'+day1+'/'+year1;
               todate.value=tod;              
    	   if (Date.parse(fromdate.value) > Date.parse(todate.value))
	    	  {		  
         		 alert(fromname + " date should be earlier to " + toname + " Date. \n");
		         return false;				 
		   
     		  } else
	     
	     	    return true;
	      
	           }
   }



function isFullName(name1,name)  //check for empty and alphabets and apostrophe
{	if(name1.value == "") 
	{
	    alert("The " + name + " field is empty. Please re-enter the " + name + ". \n");
	    nocount++;	      	
		return false;
    }
		
	if(isNotAlphabets(name1.value))  
	{
		alert("The " + name + " field contains invalid characters .\n\t Please re-enter the " + name + " again. \n");
		return false;
	
	} 	
	return true;
}

function isNotAlphabets(str)    //check for alphabets and apostrophe
{
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)&& (ch != "'") && (ch != " ") ) 
		{
		return true;
		}
	}
	return false;
}


function isName(str1,name)   //check for alphabets and numbers
{
	if (str1.value == "")
	{
		
		alert("The " + name + " field is blank .\n\t Please re-enter " + name + ". \n");
		return false;
	}
	for (var i = 0; i < str1.value.length; i++) 
	{
	var ch = str1.value.substring(i, i + 1);
	if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '-') ) 
		{
		  alert("The " + name + " accepts letters  numbers & hiphon .\n\t Please re-enter " + name + ". \n");
		  return false;
		}
	}
return true;
}


function isNotEmpty(field, name)  //check for empty
{   
	RemoveSpace(field);
	if (field.value =="")
	{
	
	    alert("The " + name + " field is empty.\n\t Please re-enter the " + name + ". \n");
	    return false;
	}
	    for (var i=0;i < field.value.length;i++)
	{
		if (field.value.substring(i,i+1) != " ")
		{
			return true
		}
	}
		
		alert("The " + name + " field is empty.\n\t Please re-enter the " + name + ". \n");
		return false;
}



function isNotFieldEmpty(field, name)
{
  if (field.value.length > 0) 
    {
		if (isNotEmpty(field, name)==true ){
		
		return true;
		}else{
		return false;
		}
	 }
  else if (field.value.length == 0) {
	return true;
  }
  else return false;
}




function amountcheck(objamount) {

if (objamount.value=="")
{
	objamount.value = "";
	return 0;
}
else  
{
		var fat = new String(objamount.value);
		var span = new RegExp(",","g");
		var pval = fat.replace(span,"")
		if (isNaN(pval) == true)
		{
			alert("Invalid Number!");
			objamount.focus();
			objamount.select()
			return 0;
		}
		else if (pval.indexOf(".") == -1)
		{
			if ((pval.length) >10)
			{
				alert("You have exceeded the maximum amount Limit of 99999999(99 Million)");
				objamount.focus()
				objamount.select()
				return 0;
			}
			else
			{	
				var pl = new Number(pval)
				return pl.valueOf();
			}
		}
		else if (pval.indexOf(".") != -1)
		{
			if ((pval.indexOf(".")) > 8)
			{
				alert("You have exceeded the maximum amount Limit of 99999999.99(99 Million and 99 cents)");
				objamount.focus()
				objamount.select()
				return 0;
			}
			else
			{	
				var pl = new Number(pval)
				return pl.valueOf();
			}
		}
		else
		{
			var pl = new Number(pval)
			return pl.valueOf();
		}	
}

}



function numericwithoutdigit_onkeypress() {
if (window.navigator.appName=='Netscape')
 {
  key=event.which;
 }
 else 
 {
  key=event.keyCode;            
 }

  if ((key < 48 || key > 57) &&  key != 8)
    event.returnValue = false;
}


function numericwithdigitcoma_onkeypress() {

if (window.navigator.appName=='Netscape')
 {
  key=event.which;
 }
 else 
 {
  key=event.keyCode;            
 }

  if ((key < 46 || key > 57) &&  key != 44)
    event.returnValue = false;
}

function numeric_onkeypress() {
if (window.navigator.appName=='Netscape')
 {
  key=event.which;
 }
 else 
 {
  key=event.keyCode;            
 }

  if ((key < 48 || key > 57) &&  key != 46)
    event.returnValue = false;
}


function autotab(objInput1,objInput2,event,len) {
if (window.navigator.appName=='Netscape')
 {
  key=event.which;
 }
 else 
 {
  key=event.keyCode;            
 }
 if (key == 9 || key == 13)
  if (objInput1.value.length == len)
  objInput2.focus()  
 if (key != 8 && key != 46) 
 {
  if (objInput1.value.length == len)
   objInput2.focus()
 }
 return (key)
}
 

function IsAmount(objamount,name) {
if (objamount.value=="")
{
		alert("The " + name + " field is empty .\n\t Please re-enter " + name + ". \n");
		return false;
}
else  
{
		var fat = new String(objamount.value);
		var span = new RegExp(",","g");
		var pval = fat.replace(span,"")
		if (isNaN(pval) == true)
		{
			alert("The " + name + " field Should be a Numeral. \n");
			return false;
		}
		else if (pval.indexOf(".") == -1)
		{
			if ((pval.length) >8)
			{
			alert("The " + name + " field   exceeded the maximum amount Limit of 99999999(99 Million). \n");
			return false;		
			
			}
			else
			{	
				var pl = new Number(pval)
				return pl.valueOf();
			}
		}
		else if (pval.indexOf(".") != -1)
		{
			if ((pval.indexOf(".")) > 8)
			{
				alert("The " + name + " field   exceeded the maximum amount Limit of 99999999(99 Million). \n");
                return false;		
			}
			else
			{	
				var pl = new Number(pval)
				return pl.valueOf();
			}
		}
		else
		{
			var pl = new Number(pval)
			return pl.valueOf();
		}	
}

}

function isNumber(str1,name)   //check for alphabets and numbers
{
	//RemoveSpace(str1);
	if (str1.value == "")
	{
		alert("The " + name + " Should be Numeral.\n\t Please re-enter " + name + ". \n");
		return false;
	}	
	for (var i = 0; i < str1.value.length; i++) 
	{
		var ch = str1.value.substring(i, i + 1);

		if ( (ch < "0" || "9" < ch)  ) 
		{
			alert("The " + name + " Should be Numeral.\n\t Please re-enter " + name + ". \n");
			return false;
		}
	}
	return true;
}


function isnotEmptyNumber(str1,name)   //check for alphabets and numbers
{
	for (var i = 0; i < str1.value.length; i++) 
	{
		var ch = str1.value.substring(i, i + 1);

		if ( (ch < "0" || "9" < ch)  ) 
		{
            alert("The " + name + " Should be Numeral.\n\t Please re-enter " + name + ". \n");
			return false;
		}
	}
	return true;
}


function textCounter(field, maxlimit) {
 if (field.value.length > maxlimit) 
	field.value = field.value.substring(0, maxlimit);
}


function date_onkeydown() {
  if ((event.keyCode < 48 || event.keyCode > 57) &&  event.keyCode != 47 &&  event.keyCode != 8)
    event.returnValue = false;
}


function CheckVal(field,name,maxlength)  //remove trailing and leading spaces
 {  
while(''+field.value.charAt(field.value.length-1)==' ')
field.value=field.value.substring(0,field.value.length-1);
while(''+field.value.charAt(0)==' ')
field.value=field.value.substring(1,field.value.length);
	if (field.value.length < maxlength )
		{
			alert("The " + name + " length is less than " + maxlength + " characters.\n      Please re-enter " + name + ".\n");
			
		}
}


function confirm1(field,field1)  //password confirm
 {  
	if (field.value != field1.value)
		{
			alert("Please check your password to be equal values .\n");						
			nocount++;			
		}
}

function RemoveSpace(field)  //remove trailing and leading spaces
{ 
while(''+field.value.charAt(field.value.length-1)==' ')
field.value=field.value.substring(0,field.value.length-1);
while(''+field.value.charAt(0)==' ')
field.value=field.value.substring(1,field.value.length);
}

function maximizeWin() {
  if (window.screen) {
    var aw = screen.availWidth;
    var ah = screen.availHeight;
    window.moveTo(0, 0);
    window.resizeTo(aw, ah);
  }
}

function checkRange(startDate,label1, endDate, label2 )
{
	var startDate1 = new Date(startDate.value);
	var endDate1 = new Date(endDate.value);
	var b = true;
	var sval,scnt;
	
	if (isNaN(endDate1.getTime())==true)
		{
		sval = str;
		scnt = nocount;
		if (sval.indexOf(label2 + " Date is not ") == -1){
			alert("Please check your password to be equal values .\n");						
			str = str + "\t" + nocount + ".  Please enter a Valid " + label2 + " Date. \n";
			nocount++;	}
			b=false;
			return b;
		}
	
	if (isNaN(startDate1.getTime())==true)
		{
		startDate.style.color ="#FF0000" ;
	    startDate.style.borderColor ="#FF0000" ;
	    sval = str;
		scnt = nocount;
		if (sval.indexOf(label1 + " Date is not ") == -1){
			str = str + "\t" + nocount + ".  Please enter a Valid " + label1 + " Date. \n";
			nocount++;	}
			b=false;
			return b;
		}
		
	if 	(startDate1.getTime() >endDate1.getTime())				
		{
		endDate.style.color ="#FF0000" ;
	    endDate.style.borderColor ="#FF0000" ;
		str = str + "\t" + nocount + ".  " + label1 + " date should be earlier to " + label2 + " Date. \n";
		nocount++;		
		
			return false;
		}
		return b;
	}
	
	
function emailCheck (emailStr,efield) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


var matchArray=emailStr.value.match(emailPat)
if (matchArray==null) {
      alert("Email address seems incorrect (check @ and .'s)")
	  emailStr.focus()     
	  emailStr.select()    	
	  return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    alert("Please Enter Valid Email Id. \n");						
	return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	       alert("Destination IP address is invalid!")
	       emailStr.focus()     
	       emailStr.select()      
	  	return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
	emailStr.focus()     
	emailStr.select()
	return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
      alert("The address must end in a three-letter domain, or two letter country.")
      emailStr.focus()     
	  emailStr.select()          
      return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
    alert(errStr)
    emailStr.focus()     
	emailStr.select()      
    return false
}

// If we've gotten this far, everything's valid!
return true;
}



function rangeCheckDateSelect1(fromday,frommonth,fromyear,fromname,today,tomonth,toyear,toname) 
 {		d = fromday.options[fromday.selectedIndex].value;
		m = frommonth.options[frommonth.selectedIndex].value;
		y = fromyear.options[fromyear.selectedIndex].value;
        var fromdate = m+'/'+d+'/'+y;

	   if (Date.parse(fromdate) > Date.parse(todate))
		  {
		    
		    alert('Please input start date less than or equal to end date');
		    return false;
				 
		   // return true
		  } else
	     
		    return true;
	      
  }



function rangeCheckDateSelect(fromday,frommonth,fromyear,fromname,today,tomonth,toyear,toname) 
 {		d = fromday.options[fromday.selectedIndex].value;
		m = frommonth.options[frommonth.selectedIndex].value;
		y = fromyear.options[fromyear.selectedIndex].value;
        var fromdate = m+'/'+d+'/'+y;
    
        d1 = today.options[today.selectedIndex].value;
		m1 = tomonth.options[tomonth.selectedIndex].value;
		y1 = toyear.options[toyear.selectedIndex].value;
        var todate = m1+'/'+d1+'/'+y1;
        
      if (isValidDate(fromday,frommonth,fromyear,fromname) &&
	     (isValidDate(today,tomonth,toyear,toname)) ) {

	   if (Date.parse(fromdate) > Date.parse(todate))
		  {
		   
		   alert("Please input " + " "+ fromname + "  " + "lesser than" + " "+ toname + " "+".\n")
		   return false;
		  		 
		   // return true
		  } else
	     
		    return true;
	      
	  }
   }


function CheckDaterange(fromday,frommonth,fromyear,fromname) 
 {		d = fromday.options[fromday.selectedIndex].value;
		m = frommonth.options[frommonth.selectedIndex].value;
		y = fromyear.options[fromyear.selectedIndex].value;
        var fromdate = m+'/'+d+'/'+y;    
        var jsdate = new  Date();   
	   if (Date.parse(fromdate) > Date.parse(jsdate))
		  {
		   
		   alert("Please input " + " "+ fromname + "  " + "lesser than system date"+".\n")
		   return false;
		  		 
		   // return true
		  } else
	     
		    return true;
	      
	  
 }



function isValidDate(dd,mm,yy,name) 
{
 d = dd.options[dd.selectedIndex].value;
 m = mm.options[mm.selectedIndex].value;
 y = yy.options[yy.selectedIndex].value;

	  var dateStr = d+'/'+m+'/'+y;
	  

    var jsdate = new  Date();
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
	    alert(name + " Date is not in a valid format.\n")		  
		return false;
}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if ((year.length) < 4)
		{
			year= ("20" + year);
	
		}
	if (year < 1900) 
	{ // check year range
		alert(name + " Year must be greater than 1900.\n");		  
		return false;
	}
	if (month < 1 || month > 12) 
	{ // check month range
	    alert(name + " Month must be between 1 and 12.\n");		  
		return false;
	}
	if (day < 1 || day > 31) 
	{   
	    alert(name + " Day must be between 1 and 31.\n");
		return false;
	}
if ((month==4 || month==6 || month==9 || month==11) && day==31) 
{
		alert(name + " Month "+month+" doesn't have 31 days!\n");		  		
	    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(name + " February " + year + " doesn't have " + day + " days!\n");		  		
	return false;
   }
}
	return true;  // date is valid */

}

//function isNotBothDateFieldEmpty(field1, name1,field2, name2)   // check for length of the date field , if length exists check for proper date
//{
  //if ((field1.value.length > 0) &&  (field2.value.length > 0) )
    //{
	//	if (checkRange(field1,name1, field2, name2 )==true )
	//	return true;
//	 }
//  else if ((field1.value.length == 0) || (field2.value.length == 0)) {
	//return true;
  //}
  //else return false;
//}



function isNotBothDateFieldEmpty(d,m,y,fromname,d1,m1,y1,toname)   // check for length of the date field , if length exists check for proper date
{
  if ((d.options[d.selectedIndex].value > '0') || (m.options[m.selectedIndex].value > '0') || (y.options[y.selectedIndex].value > '0') || (d1.options[d1.selectedIndex].value > '0') || (m1.options[m1.selectedIndex].value > '0') || (y1.options[y1.selectedIndex].value > '0') )
    {
		if (rangeCheckDateSelect(d,m,y,fromname,d1,m1,y1,toname) ==true )
		return true;
	 }
  else if ((d.options[d.selectedIndex].value == '0') && (m.options[m.selectedIndex].value == '0') &&(y.options[y.selectedIndex].value == '0') &&(d1.options[d1.selectedIndex].value == '0') &&(m1.options[m1.selectedIndex].value == '0') &&(y1.options[y1.selectedIndex].value == '0') ) 
  {
	return true;
  }
  else return false;
}


function isNotDateFieldEmpty(d,m,y,fromname)   // check for length of the date field , if length exists check for proper date
{
  if ((d.options[d.selectedIndex].value > '0') || (m.options[m.selectedIndex].value > '0') || (y.options[y.selectedIndex].value > '0'))
    {		
    if (isValidDate(d,m,y,fromname)==true )
      {
		  return true;
		}else{
	      return false;
	    }	    
	 }
  else if ((d.options[d.selectedIndex].value == '0') && (m.options[m.selectedIndex].value == '0') && (y.options[y.selectedIndex].value == '0') )
  {
	return true;
  }
  else return false;
}

function isDateFieldEmpty(d,m,y,fromname)   // check for length of the date field , if length exists check for proper date
{
  		
    if (isValidDate(d,m,y,fromname)==true )
		return true;
	 
  else return false;
}


function rangeCheck(fromday,frommonth,fromyear,fromname) 
 {		d = fromday.options[fromday.selectedIndex].value;
		m = frommonth.options[frommonth.selectedIndex].value;
		y = fromyear.options[fromyear.selectedIndex].value;
        var fromdate = m+'/'+d+'/'+y;
		var jsdate = new  Date();    

	   if (Date.parse(fromdate) < Date.parse(jsdate) )
		  {
		  alert("Please input " + " "+ fromname + "  " + "greater than System date.\n");		  		
		  return false;
		  		 
		   // return true
		  } else{
	     
		    return true;
	      }
	  
   }
   
 function isFieldEmpty(field) {   	
 
  RemoveSpace(field);
	
if (field.value =="")
	{
	
	    return true;
	}
	    for (var i=0;i < field.value.length;i++)
	{
		if (field.value.substring(i,i+1) != " ")
		{
			return false;
		}
	}
		
		return true;
		
}

function isNotEmailEmpty(field, name)
{
  if (field.value.length > 0) 
    {
		if (emailCheck(field, name)==true )
		   return true;
		   else return false;
	 }
  else if (field.value.length == 0) {
	return true;
  }
  else return false;
}



function find(x, xs) {    
	var xl  = x.length, xsl = xs.length, sValue = ""	
	for (var i=0; i < xsl; i++) {
		if (xs.tagName=="SELECT"){
			sValue = xs[i].text;
				}		
		else
			sValue = xs[i]			
			
		if (sValue.toLowerCase() == x.toLowerCase())
			return 1;
		
	}
	return 2;
} 


function update(src,selObject,comp) {            
            RemoveSpace(src);            
            if (comp !=src.value){
          	newValue = find(src.value, selObject)
			if (newValue==1){
			   alert("Duplicate Company");
			   src.value=comp
			   src.focus();
			}
          }           
}



