﻿// JScript File

//Created By: Himanshu Gupta 
//Dated: 09/10/2007
//Purpose: Purpose is to make the general code for all the functions being used in the javascript 
//especially for validation.

//<%--code for including JS file Added by Himanshu on 17-10-2007--%>
//<script language = "javascript" type="text/javascript" src="../generalJSScript.js" ></script>
//<%--code for including CSS file Added by Himanshu on 08-08-2007--%>

////////////FUNCTION FOR CHECKING THE VALUE IS NUMBER ONLY OR NOT/////////////////////
//It checks that only numeric values are allowed in an entry control it does not even allow "."

    function adjustMyFrameHeight()
    {

        var frame = parent.window.getElement("frmContent");
        var frameDoc = parent.window.getIFrameDocument("frmContent");
        frame.height = frameDoc.body.offsetHeight;
               
    }

    function NumOnly(field)
    {
        var err = 0;
        var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\/\'\\";: ?-!,()[]\{\}^|@!$%^&*_+~.';
        for (i=0; i < alpha.length; i++) 
        {
            if (field.indexOf(alpha.charAt(i),0) > -1)
            {
                return 1;
            }
            
        }
    }

//It checks that only numeric values are allowed in an entry control it does allow "."
    function IsNumeric(sText)
            {
            
                var ValidChars = "0123456789.";
                var IsNumber=0;
                var Char;
                var dot=".";
		        var lstr=sText.length;
		        var ldot=sText.indexOf(dot);
		        var fdot=sText.lastIndexOf(dot);

                for (i = 0; i < sText.length && IsNumber == 0; i++) 
                { 
                    Char = sText.charAt(i); 
                    if (ValidChars.indexOf(Char) == -1) 
                    {
                        IsNumber = 1;
                    }
                }
                             
                //checking for the wrong placement of "." or multiple "."   
		        if (sText.indexOf(dot)==lstr)
		        {
		            IsNumber = 1;
		        }
                if (sText.indexOf(dot) > 0 && ldot > 0 && sText.indexOf(dot) != ldot)
		        {
		            IsNumber = 1;
		        }
		        if(ldot!=fdot)
		        {
		          IsNumber=2;
		        }
                
                return IsNumber;
            }

//&&&&&&&&&&&&&&&&&&&&&& Function to check alphanumeric entry &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

        function alphanumeric(alphane)
        {
	        var numaric = alphane;
	        for(var j=0; j<numaric.length; j++)
		    {
		          var alphaa = numaric.charAt(j);
		          var hh = alphaa.charCodeAt(0);
		          if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		          {
		          }
		          else	
		          {
			         return 1;
		          }
		    }
            return 0;
        }


//&&&&&&&&&&&&&&&&&&&&&& Function to check only character entry &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

        function CharOnly(field)
        {
            var err = 0;
            var num = '1234567890';
            for (i=0; i < num.length; i++) 
            {
                if (field.indexOf(num.charAt(i),0) > -1)
                {
                    return 1;
                }
            }
        }

//&&&&&&&&&&&&&&&&&&&&&& Function to check Invalid characters &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

        function CheckInvalid(field, Tag)
        {
            var err = 0;
            var invalidChars = '';
            if (Tag == 'NAME')   //Validates invalid charactes for Name like text boxes. Spaces are allowed
            { 
                var sName = '\/\'\\";:,?-!()[]\{\}^|@!$%^&*_+~#`=<>';
                invalidChars = sName;
            }
            if (Tag == 'USERNAME')   //Validates invalid charactes for Name like text boxes. Spaces are allowed
            { 
                var sName = '\/\'\\";:,?-!()[]\{\}^|!$%^&*+~#`=<>';
                invalidChars = sName;
            }
            if (Tag == 'CODE')   //Validates invalid charactes for Name like text boxes. Spaces are allowed
            { 
                var sName = '\/\'\\";:,?!()[]\{\}^|@!$%^&*_+~#`=<>';
                invalidChars = sName;
            }
            if (Tag == 'PHONE')   //Validates invalid charactes for Phone like text boxes. Commas allowed
            { 
                var sPhone = '\/\'\\";:?-!()[]\{\}^|@!$%^&*_+~#`=<>qwertyuoplkmjnhbgvfcdxszaQWERTYUIOPLMKNJBHVGFCDXSZA';
                invalidChars = sPhone;
            }
            if (Tag == 'PIN')   //Validates invalid charactes for Pincode like text boxes. No Commas, spaces allowed
            { 
                var sPin = '\/\'\\";:?, !()-[]\{\}^|@!$%^&*_+~#`=<>';
                invalidChars = sPin;
            }
            if (Tag == 'PFNO')   //Validates invalid charactes for PFNo like text boxes. Spaces, Hyphens, Slashes is allowed
            { 
                var sPFN = '\'\\";:?,!()[]\{\}^|@!$%^&*_+~#`=<>';
                invalidChars = sPFN;
            }
            if (Tag == 'FORMULA')   //Validates invalid charactes for formula entry
            { 
                var sFormula = '\'";:,?![]{}^|@!$%&_~#%`';
                invalidChars = sFormula;
            }
            if(Tag == 'EMAIL')
            {
                var sFormula = '\'"';
                invalidChars = sFormula;
            }
            if(Tag == 'Blood')
            {
                var sFormula = '\'";:,?![]{}^|@!$%&_~#%`.';
                invalidChars = sFormula;
            }
            //Loop to check each character
            for (i=0; i < invalidChars.length; i++) 
            {
                if (field.indexOf(invalidChars.charAt(i),0) > -1)
                {
                    return 1;
                }
            }
        }

//&&&&&&&&&&&&&&&&&&&&&&&&&&& Check Invalid Email &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

function echeck(str) 
{
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1)
		{
		   return 1;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   return 1;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		   return 1;
		}
		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		   return 1;
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		   return 1;
		 }
		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		   return 1;
		 }
		 if (str.indexOf(" ")!=-1)
		 {
		   return 1;
		 }
 		 return 0;
	}

//&&&&&&&&&&&&&&&&&&&&&& Function to Compare Dates &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

function DateCompare(date1Str, date2Str) 
{
    var date1 = new Date(CovertDateTime(date1Str));
    var date2 = new Date(CovertDateTime(date2Str));
//Check if date1 is smaller that date2
    //alert(date1);
    //alert(date2);
    if (date1 < date2) 
    {
       return 1;
    }
//Check if date1 is greater that date2
    if (date1 > date2) 
    {
       return 2;
    }
//Check if dates are equal
    return 0;
}
//&&&&&&&&&&&&&&&&&&&&&& Function to check date &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 function check_date(field)
        {
           var checkstr = "0123456789";
           var Datevalue = "";
           var DateTemp = "";
           var day;
           var month;
           var year;
           var leap = 0;
           var err = 0;
           var i;
           var sLen = 0;
           err = 0;
           Datevalue = field;
           sLen = Datevalue.length;
           /*Code Added By Susmit Singh on 30 oct 2007 */
           /*Start Date Format is DD/MM/YYYY */
           
           if(Datevalue.indexOf("/") != 2)
           {
            return 22;
           }
           
           if( Datevalue.lastIndexOf("/") != 5)
           {
            return 21;
           }
           /*End*/
           /* Delete all chars except 0..9 */
           for (i = 0; i < sLen; i++) 
           {
	          if (checkstr.indexOf(Datevalue.substr(i,1)) >= 0) 
	          {
	             DateTemp = DateTemp + Datevalue.substr(i,1);
	          }
           }
           Datevalue = DateTemp;
           /* Always change date to 8 digits - string*/
           /* year is wrong if year = 0000 */
           year = Datevalue.substr(4,4);
           if (year == 0) 
           {
              return 20;
           }
           /*Validation for year less than 1900 */
           if (year < 1900)
           {
              return 20;
           }
                      
           /* Validation of month*/
           month = Datevalue.substr(2,2);
           if ((month < 1) || (month > 12)) 
           {
              return 21;
           }
           
           /* Validation of day*/
           day = Datevalue.substr(0,2);
           if (day < 1) 
           {
              return 22;
           }
           /* Validation leap-year / february / day */
           if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) 
           {
              leap = 1;
           }
           if ((month == 2) && (leap == 1) && (day > 29)) 
           {
              return 21;
           }
           if ((month == 2) && (leap != 1) && (day > 28)) 
           {
              return 21;
           }
           /* Validation of other months */
           if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) 
           {
              return 22;
           }
           if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) 
           {
              return 22;
           }
           return 0;
        }

//&&&&&&&&&&&&&&&&&&&&&& Function to Replace charactes with specified values &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

        function replace(string,text,by) 
        {
        // Replaces text with by in string
            var strLength = string.length, txtLength = text.length;
            if ((strLength == 0) || (txtLength == 0)) 
            {
                return string;
            }
            var i = string.indexOf(text);
            
            if ((!i) && (text != string.substring(0,txtLength))) 
            {
                return string;
            }
            
            if (i == -1) 
            {
            return string;
            }

            var newstr = string.substring(0,i) + by;

            if (i+txtLength < strLength)
            {
                newstr += replace(string.substring(i+txtLength,strLength),text,by);
            }
            return newstr;
        }


//&&&&&&&&&&&&&&&&&&&&&& Function to get Number of Days in a Month&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    function GetDays(field,year)
    {
       if ((field == 1) || (field == 3) || (field == 5) || (field == 7) || (field == 8) || (field == 10) || (field == 12)) 
       {
          return 31;
       }
       if ((field == 2)) 
       {
          if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
          {
            return 29;
          }
          else
          {
            return 28;
          }
       }
       if ((field == 4) || (field == 6) || (field == 9) || (field == 11)) 
       {
          return 30;
       }
    }
    
/*Code Added by Susmit Singh on 31 oct 2007 to check the datecomparison value
  DateComparison function required value in MM/DD/YYYY format 
  Purpose of this function is to convert the datetime passed as argument to different format 
  that is if the "MM/DD/YYYY" Date is passed then it will be converted to "DD/MM/YYYY"
  and vice versa */
  function CovertDateTime(strDate)
  {
  var strNewDate = "";
  var tmpDate = new Array();
  tmpDate= strDate.split("/");
  strNewDate= tmpDate[1] + "/" + tmpDate[0] + "/" + tmpDate[2];
  return strNewDate;
  }

//By Vittal
function IsNumericMinus(sText)
{
    var ValidChars = "0123456789.-";
    var IsNumber=0;
    var Char;
    var dot=".";
    var lstr=sText.length;
    var ldot=sText.indexOf(dot);

    for (i = 0; i < sText.length && IsNumber == 0; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = 1;
        }
    }
                 
    //checking for the wrong placement of "." or multiple "."   
    if (sText.indexOf(dot)==lstr)
    {
        IsNumber = 1;
    }
    if (sText.indexOf(dot) > 0 && ldot > 0 && sText.indexOf(dot) != ldot)
    {
        IsNumber = 1;
    }
    
    return IsNumber;
} 
//Fuction for the trimming fo rht string.

// Removes leading whitespaces

function LTrim( value ) {
      var re = /\s*((\S+\s*)*)/;
      return value.replace(re, "$1");
}

// Removes ending whitespaces

function RTrim( value ) {
      var re = /((\s*\S+)*)\s*/;

      return value.replace(re, "$1");
}

 

// Removes leading and ending whitespaces

function trim( value ) {      
      return LTrim(RTrim(value));     
}   

//-------------

function emailcheck(emailId)
{
    
    if (replace(emailId.value,' ','')!='')
    {
        var emailFormat = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-.])+.)+([a-zA-Z0-9]{2,4})+$/;
        if(emailFormat.test(replace(emailId.value,' ',''))==false)
        {
            alert('Invalid Id');
            emailId.focus();
            return false;
        }
    }
}


//----------------------FOR DATE-------------------
function checkDates(txtbx)
{
    
    if (replace(txtbx.value,' ','')!='' )
    {
        validdate=check_date(txtbx.value,' ','')
        if (validdate>0)
        {
            if (validdate==20)
            {
                alert('Enter Valid Date');
                txtbx.focus();
                return false;
            }
            if (validdate==21)
            {
                alert('Enter Valid Date');
                txtbx.focus();
                return false;
            }
            if (validdate==22)
            {
                alert('Enter Valid Date');
                txtbx.focus();
                return false;
            } 
            return false;                       
        }
       
    }
}

/////////////////////////////////AJAX OBJECT///////////////////////////////////////////////


