/*
------------------------------------------------------------------------------------
						//	JavaScript Validations functions //
------------------------------------------------------------------------------------
*/

	//Checking for the alphabets when entered
    function Alphabets(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For A-Z
		if(keyval >= 65 && keyval<=90) 
		{
			return true;
		}
		//For a-z
		else  if(keyval >= 97 && keyval<=122)
		{
			return true;
		}
		//For Backspace
		else if(keyval==8)
		{
			return true;
		}
		//For General
		else if(keyval==0)
		{
			return true;
		}	
		//For Space
		else if(keyval==32)
		{
			return true;
		}
		else
		{
			return false;
		}
	}// End of the function


	//function for validating the Alphanumeric charecters
	function AlphaNumarics(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For A-Z
		else if(keyval >= 65 && keyval<=90) 
		{
		return true;
		}
		//For a-z
		else if(keyval >= 97 && keyval<=122) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		 return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		 return true;
		}
		//For Space
		else if(keyval==32)
		{
		 return true;
		}
		else 
		{
		return false;
		}

	}//End of the Function

	//Function for validating the Numerics
	function Numerics(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		return true;
		}
		else 
		{
		return false;
		}

	}//End of the function
	
	//Function for validating the Numerics
	function NumericsWithColon(nkey)
	{
		//debugger;
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		return true;
		}
		else if(keyval==58)
		{
			return true;
		}
		else 
		{
		return false;
		}

	}//End of the function
	
	
	
	//Function for validating the Numerics and /
	function NumericsWithbackslsh(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		
		//For 0-9
		if(keyval >= 47 && keyval<=57) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		return true;
		}
		else 
		{
		return false;
		}

	}//End of the function
	
	
	//Function for Validating the Email Address
	function EmailValidation(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For A-Z
		else if(keyval >= 65 && keyval<=90) 
		{
		return true;
		}
		//For a-z
		else if(keyval >= 97 && keyval<=122) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		 return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		 return true;
		}
		//For .
		else if(keyval==46)
		{
		 return true;
		}
		//For _
		else if(keyval==95)
		{
		 return true;
		}
		//For @
		else if(keyval==64)
		{
		 return true;
		}
		else 
		{
		return false;
		}

	}//End of the Function
	
	
	 function AlphaNumaricsCompany(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
     if(keyval >= 32 && keyval<=127) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		 return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		 return true;
		}
		else if(keyval==14)
		{
		 return true;
		}
		else if(keyval==15)
		{
		 return true;
		}
		else 
		{
		 return false;
		}


	}//End of the Function
	
	//Function for User Name field for Key Press event
	function AlphaNumaricsUserName(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
     if(keyval > 32 && keyval<=127) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		 return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		 return true;
		}
		else if(keyval==14)
		{
		 return true;
		}
		else if(keyval==15)
		{
		 return true;
		}
		else 
		{
		 return false;
		}


	}//End of the Function
	
	
	//Checking for the URL 
    function RestrictURL(nkey)
	{
	    //debugger;
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
			//alert(keyval);
		}
		//For Space
		else if(keyval!=32)
		{
			return true;
		}
		else
		{
			return false;
		}
	}// End of the function
	
	//Function for validating the Decimals
	function Decimals(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		return true;
		}
		//For .
		else if(keyval==46)
		{
		return true;
		}
		else 
		{
		return false;
		}

	}//End of the function
	
	//Date On key press validation
	function DateValidation(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		 keyval = window.event.keyCode;
		 
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}

		if(keyval >= 47 && keyval<58) 
		{
		return true;
		}
		//For Backspace
		else if(keyval==8)
		{
			return true;
		}
		//For General
		else if(keyval==0)
		{
			return true;
		}
		else
		{
		return false;
		}

	}//End of the function
	
	
	
	    // Declaring valid date character, minimum year and maximum year
        var dtCh= "/";
        var minYear=1900;
        var maxYear=2100;
        
        function isDate(dtStr)
        {
	        if(dtStr!="")
	        {
	        var daysInMonth = DaysArray(12)
	        var pos1=dtStr.indexOf(dtCh)
	        var pos2=dtStr.indexOf(dtCh,pos1+1)
	        var strDay=dtStr.substring(0,pos1)
	        var strMonth=dtStr.substring(pos1+1,pos2)
	        var strYear=dtStr.substring(pos2+1)
	        strYr=strYear
	        if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	        if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	        for (var i = 1; i <= 3; i++) {
		        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	        }
	        month=parseInt(strMonth)
	        day=parseInt(strDay)
	        year=parseInt(strYr)
	        if (pos1==-1 || pos2==-1){
		        //alert("The date format should be : dd/mm/yyyy ")
		        return false
	        }
	        if (strMonth.length<1 || month<1 || month>12){
		        //alert("Please enter a valid month")
		       // alert("Please enter a valid 'Date Of Birth'")
		        return false
	        }
	        if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		        //alert("Please enter a valid day")
		        //alert("Please enter a valid 'Date Of Birth'")
		        return false
	        }
	        if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		       // alert("Please enter a valid 4 digit 'Date Of Birth' year between "+minYear+" and "+maxYear)
		        return false
	        }
	        if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		        //alert("Please enter a valid date")
		       // alert("Please enter a valid 'Date Of Birth'")
		        return false
	        }
	       }
        return true
        }
        
        
        
            function isDateMMDDYYY(dtStr)
            {
	            var daysInMonth = DaysArray(12)
	            var pos1=dtStr.indexOf(dtCh)
	            var pos2=dtStr.indexOf(dtCh,pos1+1)
	            var strMonth=dtStr.substring(0,pos1)
	            var strDay=dtStr.substring(pos1+1,pos2)
	            var strYear=dtStr.substring(pos2+1)
	            strYr=strYear
	            if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	            if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	            for (var i = 1; i <= 3; i++) {
		            if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	            }
	            month=parseInt(strMonth)
	            day=parseInt(strDay)
	            year=parseInt(strYr)
	            if (pos1==-1 || pos2==-1){
		            alert("The date format should be : mm/dd/yyyy")
		            return false
	            }
	            if (strMonth.length<1 || month<1 || month>12){
		            alert("Please enter a valid 'month'")
		            return false
	            }
	            if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		            alert("Please enter a valid 'day'")
		            return false
	            }
	            if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		            alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		            return false
	            }
	            if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		            alert("Please enter a valid 'date'")
		            return false
	            }
               return true
        }
        
        function isFromDateMMDDYYY(dtStr)
       {
        //debugger;
            
            var daysInMonth = DaysArray(12)
            var pos1=dtStr.indexOf(dtCh)
            var pos2=dtStr.indexOf(dtCh,pos1+1)
            var strMonth=dtStr.substring(0,pos1)
            var strDay=dtStr.substring(pos1+1,pos2)
            var strYear=dtStr.substring(pos2+1)
            
            strYr=strYear
            if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
            if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
            
            for (var i = 1; i <= 3; i++) 
            {
                if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
            }
            
            month=parseInt(strMonth)
            day=parseInt(strDay)
            year=parseInt(strYr)
            
            if (pos1==-1 || pos2==-1)
            {
                alert("The 'From Date' format should be : mm/dd/yyyy")
                return false
            }
            else if (strMonth.length<1 || month<1 || month>12)
            {
                alert("Please enter a valid month for 'From Date'")
               
                return false
            }
            else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
            {
                alert("Please enter a valid day for 'From Date'")
               
                return false
            }
            else if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
            {
                alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+" for 'From Date'")
               
                return false
            }
            else if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
            {
                alert("Please enter a valid 'From Date'")
               
                return false
            } 
            else
            { 
                return true
            }
       }
       
       function isToDateMMDDYYY(dtStr)
       {
        //debugger;
                
            var daysInMonth = DaysArray(12)
            var pos1=dtStr.indexOf(dtCh)
            var pos2=dtStr.indexOf(dtCh,pos1+1)
            var strMonth=dtStr.substring(0,pos1)
            var strDay=dtStr.substring(pos1+1,pos2)
            var strYear=dtStr.substring(pos2+1)
            
            strYr=strYear
            if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
            if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
            
            for (var i = 1; i <= 3; i++) 
            {
                if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
            }
            
            month=parseInt(strMonth)
            day=parseInt(strDay)
            year=parseInt(strYr)
            
            if (pos1==-1 || pos2==-1)
            {
                alert("The 'To Date' format should be : mm/dd/yyyy")
                
                return false
            }
            else if (strMonth.length<1 || month<1 || month>12)
            {
                alert("Please enter a valid month for 'To Date'")
               
                return false;
            }
            else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
            {
                alert("Please enter a valid day for 'To Date'")
                
                return false
            }
            else if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
            {
                alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+" for 'To Date'")
                
                return false
            }
            else if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
            {
                alert("Please enter a valid 'To Date'")
               
                return false
            } 
            else
            { 
                return true
            }
       }
       
        function isInteger(s)
        {
	        var i;
            for (i = 0; i < s.length; i++){   
                // Check that current character is number.
                var c = s.charAt(i);
                if (((c < "0") || (c > "9"))) return false;
            }
            // All characters are numbers.
            return true;
        }

        function stripCharsInBag(s, bag)
        {
	        var i;
            var returnString = "";
            // Search through string's characters one by one.
            // If character is not in bag, append to returnString.
            for (i = 0; i < s.length; i++){   
                var c = s.charAt(i);
                if (bag.indexOf(c) == -1) returnString += c;
            }
            return returnString;
        }

        function daysInFebruary (year)
        {
	        // February has 29 days in any year evenly divisible by four,
            // EXCEPT for centurial years which are not also divisible by 400.
            return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
        }
        function DaysArray(n) 
        {
	        for (var i = 1; i <= n; i++) {
		        this[i] = 31
		        if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		        if (i==2) {this[i] = 29}
           } 
           return this
        }
        
        
        //Validating the Numeric data
        function IsNumeric(str)
        {
            var reg =/^[0-9]+$/;
            
            if(str!="0" && str!="00" && str!="000" && str!="0000" && str!="00000")
            {
                if(!reg.test(str))
                {
                    alert("Please enter numerics only");
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
               alert("Number must be greater than zero");
               return false; 
            }
        }//End of the function


        function days_between(date1, date2)
        { 
            //debugger;
            //var difference=date1-date2 
            var one_day=1000*60*60*24; 
            var date22=new Date(date2) 
            var date11=new Date(date1) 
            formatdifference=Math.ceil((date11.getTime()-date22.getTime())/(one_day)); 
            return formatdifference 
        }

        //JavaScript function for comparing the two dates        
        function CompareDates(date1, date2)
        {
           //debugger;
           
           //****Get current date****
            var currentTime = new Date()
            var month = currentTime.getMonth() + 1
            if(month <= 9)
            {
                month="0" + month;
            }
            var day = currentTime.getDate()
            if(day <= 9)
            {
                day="0" + day;
            }
            
            var year = currentTime.getFullYear()
            var currentDate=month + "/" + day + "/" + year;
            var FromDate=date1;
            var ToDate=date2;
           //******
                      
           var str1  = date1;
           var str2  = date2;
           var dt1   = parseInt(str1.substring(0,2),10);
           var mon1  = parseInt(str1.substring(3,5),10);
           var yr1   = parseInt(str1.substring(6,10),10);
           var dt2   = parseInt(str2.substring(0,2),10);
           var mon2  = parseInt(str2.substring(3,5),10);
           var yr2   = parseInt(str2.substring(6,10),10);
           var date1 = new Date(yr1, mon1-1, dt1);
           var date2 = new Date(yr2, mon2-1, dt2);
           
           //if(FromDate < currentDate)
           //{
           //   alert("\'From Date\' should be greater than or equal to Todays Date");
           //   return false;
           //}
           //else if(ToDate < currentDate)
           //{
           //   alert("\'To Date\' should be greater than or equal to Todays Date");
           //   return false;
           //}
           //else 
           if(date1 > date2)
           {
              alert("\'To Date\' should be greater than 'From Date'");
              return false;
           }           
           else
           {
              return true;
           }
        } //End of the function


String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() 
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() 
{
	return this.replace(/\s+$/,"");
}


//Function for Validating the Chemical Formula
	function ChemicalFormulaValidation(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For A-Z
		else if(keyval >= 65 && keyval<=90) 
		{
		return true;
		}
		//For a-z
		else if(keyval >= 97 && keyval<=122) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		 return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		 return true;
		}
		//For -
		else if(keyval==45)
		{
		 return true;
		}
		else 
		{
		return false;
		}

	}//End of the Function
	
	
	 function jumpScroll() 
    {
   	    window.scroll(0,0); // horizontal and vertical scroll targets
     
	}//End of the Function
	
	//JavaScript function for comparing the two dates        
        function CompareDates1(date1, date2)
        {
           //debugger;
           
           //****Get current date****
            var currentTime = new Date()
            var month = currentTime.getMonth() + 1
            if(month <= 9)
            {
                month="0" + month;
            }
            var day = currentTime.getDate()
            if(day <= 9)
            {
                day="0" + day;
            }
            
            var year = currentTime.getFullYear()
            var currentDate=month + "/" + day + "/" + year;
            var FromDate=date1;
            var ToDate=date2;
           //******
                      
           var str1  = date1;
           var str2  = date2;
           var dt1   = parseInt(str1.substring(0,2),10);
           var mon1  = parseInt(str1.substring(3,5),10);
           var yr1   = parseInt(str1.substring(6,10),10);
           var dt2   = parseInt(str2.substring(0,2),10);
           var mon2  = parseInt(str2.substring(3,5),10);
           var yr2   = parseInt(str2.substring(6,10),10);
           var date1 = new Date(yr1, mon1-1, dt1);
           var date2 = new Date(yr2, mon2-1, dt2);
           
//           if(FromDate < currentDate)
//           {
//              alert("\'From Date\' should be greater than or equal to Todays Date");
//              return false;
//           }
//           else if(ToDate < currentDate)
//           {
//              alert("\'To Date\' should be greater than or equal to Todays Date");
//              return false;
//           }
           if(date1 > date2)
           {
              alert("\'To Date\' should be greater than 'From Date'");
              return false;
           }           
           else
           {
              return true;
           }
        } //End of the function
	
	//Function for Validating the Chemical Formula
	function PhoneValidation(nkey)
	{
		var keyval
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		keyval = window.event.keyCode;
		}
		else if (navigator.appName == 'Netscape')
		{
		  nkeycode = nkey.which;
			keyval = nkeycode;
		}
		//For 0-9
		if(keyval >= 48 && keyval<=57) 
		{
		return true;
		}
		//For General
		else if(keyval==0)
		{
		 return true;
		}
		//For Backspace
		else if(keyval==8)
		{
		 return true;
		}
		//For -
		else if(keyval==45)
		{
		 return true;
		}
		else 
		{
		return false;
		}

	}//End of the Function

