// This function limits size of textarea/textfield

   function checkChar(Obj,len){	
	if(Obj.value.length > len)
	{
		alert("  This Field Takes Upto "+len+" Characters");
		Obj.value= Obj.value.substr(0,len);
		Obj.focus();
		return false;
	}
   }
 

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also
   // removes consecutive spaces and replaces it with one space.
   var retValue = inputString;

   if(retValue!=""){
   retValue = String(retValue);
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
  }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


// this function validates the phone numbers entered
// onblur event of area, exchange and number fields of phone number
function checkMe(a,b, c, msg) {
	a.value = trim(a.value);
	b.value=trim(b.value);
if ((a.value.length > 0) && !(b.value.length > 0)) {
	b.focus();
	//alert("Please Enter The "+msg+" Number");
	return false;
}
c.focus();
return true;
}
function checkMe1(a,b, msg) {
	a.value = trim(a.value);
	b.value=trim(b.value);
if ((a.value.length > 0) && !(b.value.length > 0)) {
	b.focus();
	//alert("Please Enter The "+msg+" Number");
	return false;
}
return true;
}

function checkOther(area, exchange, num) {
area.value = trim(area.value);
 exchange.value = trim(exchange.value);
 num.value = trim(num.value);
 if (!(exchange.value.length > 0)) {
	exchange.value="";
	exchange.focus();
	//alert("Please Enter The Exchange Number!");
	return false;
}
 if (!(num.value.length > 0)) {
	num.value="";
	num.focus();
	//alert("Please Enter The Phone Number!");
	return false;
}
if((exchange.value.length > 0) && (num.value.length > 0) && !(area.value.length >0)) {
	area.focus();
	//alert("Please Enter The Area Number!");
	return false;
}
return true;
}

function phCounter(field, maxlength, type) {
	field.value = trim(field.value);
	if (field.value.length > 0) { 	
		if (field.value.length <= maxlength) {
			if (isNumeric(field) == true) {
				field.value = "";
				field.focus();
				alert(type+" Number Should Be Numeric Only");
			} else if (isNumeric(field) == false) {					
				if(field.value.length < maxlength) {
					field.value = "";
					field.focus();
					alert("Invalid "+type+" Number. Should Be "+maxlength+" Digits");
				}
			}
		}
	}
}
function SSN_valid(field, maxlength, type) {
	field.value = trim(field.value);
	if (field.value.length > 0) { 	
		if (field.value.length <= maxlength) {
			if (isNumeric(field) == true) {
				field.value = "";
				field.focus();
				alert(" Number Should Be Numeric Only");
			} else if (isNumeric(field) == false) {					
				if(field.value.length < maxlength) {
					field.value = "";
					field.focus();
					alert("Number Should Be "+maxlength+" Digits");
				}
			}
		}
	}
}

// this functions checks for value in the area field of the Phone number
// onfocus event of exchange and number fields of Phone

function checkArea(field) {
	field.value = trim(field.value);
	if (field.value.length > 0) {
		return true;
	} else {
		field.focus();
		//alert("Enter Area Number!");
		return false;
	}
}
function SSNArea(field) {
	field.value = trim(field.value);
	if (field.value.length > 0) {
		return true;
	} else {
		field.focus();
		//alert("Enter Area Number!");
		return false;
	}
}
// this functions checks for value in the Exchange field of the Phone number
// onfocus event of number field of Phone Number

function checkExchange(afield, efield) {
	if (checkArea(afield)) {
		efield.value = trim(efield.value);
		if (efield.value.length > 0) {
			return true;
		} else {
			efield.focus();
			alert("Enter Exchange Number!");
			return false;
		}
	} 
}

function chkssn(afield, efield) {
	if (checkArea(afield)) {
		efield.value = trim(efield.value);
		if (efield.value.length > 0) {
			return true;
		} else {
			efield.focus();
			alert("SSN Number should be 9 digits");
			return false;
		}
	} 
}

       
// this function checks for numeric characters only
// may be used as an internal method to check for numerals only
function isNumeric(field) {
	field1=field
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++)
     {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
     } 
	if (ok == "no") 
	{
		return true
	}
   return false
}

function checkDecimal(field) {

	
	if(checkD(field)) {
		field.value="";
		field.focus();
		alert("Invalid Data !");
		return false;
	} else {
		return true;
	}
	
}

function checkD(field) {
	field1=field
	var valid = "0123456789."
	var ok = "yes";
	var temp;
	var count = 0;
	var count2 = 0;
	
	for (var i=0; i<field.value.length; i++)
    	 {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
     	} 
     	for(i=0;i<field.value.length;i++){
		if(field.value.substring(i,i+1 )==".") count2++;
		if(field.value.substring(i,i+1 )==".") count=i;
	}
	if ((count >= 3) || (count == 0)||(ok == "no") || (count2 > 1)) {
		if(field.value.length > 3) {
			var charfour = field.value.substr(3,1);
			if ((charfour != ".")|| (count2>1)) {			
				return true;
			} else {
				return false;
			}
		} else if(count2 > 1){
			return true;
		} else {
			return false;
		}
	}
	
	
	
	
   return false
}

// this function is used to check for numerals only
// onblur event of any field checks whether the entered value is numeric or not
function checkNumeric(aa){
aa.value = trim(aa.value);
if (aa.value.length > 0) 
	{
		if (isNumeric(aa) == true) {
			aa.value = "";
			aa.focus();
			alert("Enter Numerals Only!");
		}
	}
}







// This function compares two dates say 'start date' and 'end date'
/* 
   onblur event of the end date field
   this function takes date field object(eg. this.form.designStartDate), 
   enddate field object(eg. this.form.designEndDate) and type (eg., 'Design', 'Construction') 
   as parameters 
*/

function dateCompare(a, b, type) {
	b.value = trim(b.value);
	if (b.value.length > 0)  {
		
		var fromDate = new Date(a.value);		
		var toDate = new Date(b.value);		
		diff = toDate.getTime() - fromDate.getTime();
	    	diff = Math.floor(diff / (1000 * 60 * 60 * 24) );
		if (diff > 0) {
		return true;
		} else {
		alert(type+" End Date Should Be After The "+type+" Start Date!");
		b.value ="";
		return false;
		}
	 }  else {
	 	return true;
	 }
}

function ApproveddateCompare(a, b) {
	b.value = trim(b.value);
	if (b.value.length > 0)  {
		
		var fromDate = new Date(a.value);		
		var toDate = new Date(b.value);		
		diff = toDate.getTime() - fromDate.getTime();
	    	diff = Math.floor(diff / (1000 * 60 * 60 * 24) );
		if (diff > 0) {
		return true;
		} else {
		alert("Design Start Date Should Be After The Approved Date!");
		b.value ="";
		return false;
		}
	 }  else {
	 	return true;
	 }
}


function StartDateCompare(a, b) {
	b.value = trim(b.value);
	if (b.value.length > 0)  {
		
		var fromDate = new Date(a.value);		
		var toDate = new Date(b.value);		
		diff = toDate.getTime() - fromDate.getTime();
	    	diff = Math.floor(diff / (1000 * 60 * 60 * 24) );
		if (diff > 0) {
		return true;
		} else {
		alert("End Date Should Be After The Start Date!");
		b.value ="";
		return false;
		}
	 }  else {
	 	return true;
	 }
}
// This function compares the field date to the current date not allowing past dates
/* 
   onblur event of the field
   this function takes startdate field object(eg. this.form.designStartDate) 
   and type (eg., 'Design', 'Construction') 
   as parameters 
*/

function currentDateCompare(a, type) {
	a.value = trim(a.value);
	if (a.value.length > 0)  {
		
	   	var fromDate = new Date(a.value);		
		var toDate = new Date();
		
		diff = fromDate.getTime() - toDate.getTime();
	   	diff = Math.floor(diff / (1000 * 60 * 60 * 24) );
		if (diff >= -1) {
		return true;
		} else {
		alert(type+" Start Date Should Be Either The Current Date Or After The Current Date!");
		a.value ="";
		return false;
		}
	 }  else {
	 	return true;
	 }
}
function currentDateCompare1(date1) {
	a.value = trim(a.value);
	if (a.value.length > 0)  {
		
	   	var fromDate = new Date(a.value);		
		var toDate = new Date();
		
		diff = fromDate.getTime() - toDate.getTime();
	   	diff = Math.floor(diff / (1000 * 60 * 60 * 24) );
		if (diff > 0) {
			return true;
		} else {
				return false;
		}
	 }  else {
	 	return true;
	 }
}

/*
  This function checks for the startdate field is non empty
  onfocus event of the end date
  this function takes startdate field object(eg. this.form.designStartDate) 
  and type (eg., 'Design', 'Construction') 
  as parameters 
*/


function startDateCheck(a, type) {
	a.value = trim(a.value);
	if (!(a.value.length > 0)) {
		a.focus();
		alert("  Enter The "+type+" Start Date!");
		return false;
	} else {
		return true;
	}
}

function appStartDateCheck(a, type) {
	a.value = trim(a.value);
	if (!(a.value.length > 0)) {
		a.focus();
		alert("  Enter The "+type+" Date!");
		return false;
	} else {
		return true;
	}
}



// this function checks for special characters only
// may be used as an internal method to check for characters only
function CharOnly(field) {
	field1=field
	var valid = "abcdefghijklmnopqrstuvwxyz (  ) - /  _ ,  : ; ABCDEFGHIJKLMNOPQRSTUVWXYZ. \" \\ "
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++)
     {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
     } 
	if (ok == "no") 
	{
		return true
	}
   return false
}

// this function is used to check for characters only
// onblur event of any field checks whether the entered value is character or not
function checkCharOnly(aa){
aa.value = trim(aa.value);
if (aa.value.length > 0) 
	{
		if (CharOnly(aa) == true) {
			aa.value = "";
			aa.focus();
			alert("Enter Characters Only!");
		}
	}
}


//

// this function checks for characters which can be allowed for Name field where they can accept forward and backward slash

function NameChar(field) {
	field1=field
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:;-_/\"\\"
	var ok = "yes";
	var temp;
	for (var i=0; i<field1.value.length; i++)
     {
		temp = "" + field1.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
     } 
	if (ok == "no") 
	{
		return true
	}
   return false
}

// this function is used to check for characters which can be allowed for  Name field where they can accept forward and backward slash
// onblur event of any field checks whether the entered value is character or not
function NameCheck(aa){
aa.value = trim(aa.value);
if (aa.value.length > 0) 
	{
		if (NameChar(aa) == true) {
			aa.value = "";
			aa.focus();
			alert("Enter Valid Characters!");
		}
	}
}
//


// this function checks for characters which can be allowed for the user name  user name or login field and password only

function userChar(field) {
	field1=field
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_"
	var ok = "yes";
	var temp;
	for (var i=0; i<field1.value.length; i++)
     {
		temp = "" + field1.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
     } 
	if (ok == "no") 
	{
		return true
	}
   return false
}

// this function is used to check for characters which can be allowed for user name or login field  and password field only
// onblur event of any field checks whether the entered value is character or not
function userNameCheck(aa){
aa.value = trim(aa.value);
if (aa.value.length > 0) 
	{
		if (userChar(aa) == true) {
			aa.value = "";
			aa.focus();
			alert("Enter Valid Characters!");
		}
	}
}
//



// this function checks for alphaNumeric characters only
// may be used as an internal method to check for alphaNumeric characters only
function alphaChar(field) {
	field1=field
	var valid = "abcdefghijklmnopqrstuvwxyz ',:  *ABCDEFGHIJKLMNOPQRSTUVWXYZ.1234567890-  / ;   _ "
	var ok = "yes";
	var temp;
	for (var i=0; i<field1.value.length; i++)
     {
		temp = "" + field1.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
     } 
	if (ok == "no") 
	{
		return true
	}
   return false
}

// this function is used to check for characters only
// onblur event of any field checks whether the entered value is character or not
function checkAlphaChar(aa){
aa.value = trim(aa.value);
if (aa.value.length > 0) 
	{
		if (alphaChar(aa) == true) {
			aa.value = "";
			aa.focus();
			alert("Enter AlphaNumeric Characters Only!");
		}
	}
}
//

// this function validates the SSN entered by checking in the no. of digits entered and putting into nnn-nn-nnnnn format.
function SSNValidation(hh) {
	hh.value = trim(hh.value);
	if (hh.value.length > 0) {
		var ssn = hh.value;
		var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
		var numDashes = ssn.split('-').length - 1;
		if (matchArr == null || numDashes == 1) {
			alert('Invalid SSN. Must Be 9 Digits Or In The Form NNN-NN-NNNN.');
			msg = "does not appear to be valid";
			hh.value ="";
			hh.focus();
		} else if (parseInt(matchArr[1],10)==0) {
			alert("Invalid SSN: SSN's Can't Start With 000.");
			msg = "does not appear to be valid";
			hh.value ="";
			hh.focus();
		}
		if (numDashes == 0 && hh.value.length == 9) {
			var strone = ssn.charAt(0)+ssn.charAt(1)+ssn.charAt(2);
			var strtwo = ssn.charAt(3)+ssn.charAt(4);
			var strthree = ssn.charAt(5)+ssn.charAt(6)+ssn.charAt(7)+ssn.charAt(8);
			var strsep = "-";
			hh.value = strone+strsep+strtwo+strsep+strthree;
		}
	} 
}


// This function eliminates the Special characters
    function Emailcheck(num,obj)
	{
     if (num.length > 0) { 	   
	    var count1=0;
	    var count=0;
	    var count2=0;
	    var i;

	 //   str = new String (escape(num));
	      str = new String(num);
            
	   if (str.match("%7E")|| str.match("%60") || str.match("%21") || str.match("%23") || str.match("%24") || str.match("%25") || str.match("%5E") || str.match("%26") || str.match("%28") || str.match("%29") || str.match("%7B") || str.match("%7D") || str.match("%5B") || str.match("%5D") || str.match("%3C") || str.match("%3E") || str.match("%3F") || str.match("%3A") || str.match("%3B")|| str.match("%22") || str.match("%27")|| str.match("%7C") || str.match("%5C")) 
	    
		 {
			alert("Enter Only Alpha Numeric Characters !!");
			obj.value="";
			//obj.focus();
	    	 }
	  else {
          	
		for(i=0;i<str.length;i++){
		if((str.substring(i,i+1 )).match("@"))
			count1++; 
				
		}
		
		
		for(i=0;i<str.length;i++){
		if(str.substring(i,i+1 )=="@") count=i;
		if(str.substring(i,i+1 )==".") count2=i;
                      
		}
		if((count>count2) || (count1!=1) || (count==0) || (count2==str.length-1) || (count2+3<str.length-1)  || (count2+1==str.length-1) ||(count==(count2-1)))
		//if((count>count2) || (count1!=1) || (count==0) ||  (count2+1==str.length-1) ||(count==(count2-1)))
			{
			alert("Not a valid Email ID!");
			obj.value="";
			obj.focus();
			}
           }


	}
 }
function datediff(date1,date2)
{
 var date1 = new Date(date1.substring(6,10),
			date1.substring(0,2)-1,date1.substring(3,5))
 var date2 = new Date(date2.substring(6,10),
            date2.substring(0,2)-1,date2.substring(3,5))
              
     if (date1.getYear() > date2.getYear())
		return true;
     else if (date1.getYear() == date2.getYear() && date1.getMonth() > date2.getMonth() )
		return true;
     else if ((date1.getYear() > date2.getYear() || date1.getYear() == date2.getYear())&& (date1.getMonth() > date2.getMonth() || date1.getMonth() == date2.getMonth())  && (date1.getDate() > date2.getDate()))
     	return true;
     else
		return false;


}

function Len(str)        /***
                IN: str - the string whose length we are interested in
                RETVAL: The number of characters in the string        ***/
        {  return String(str).length;  }
		
		
		
function gettoday(){
	var now = new Date()
	var strTemp
	Day=now.getDate()
	Month=now.getMonth()+1	
	if (Len(trim(Day))==1) Day="0"+Day
	if (Len(trim(Month))==1) Month="0"+Month
	today_date = Month+"/"+Day+"/"+now.getYear()
	return today_date;
}