/*
This file contains JavaScript functions to validate HTML form datas

Author : Rajan

The functions in this file are

1.  isPassportNoOld(strField)           -   Checks for valid Passport number
2.  isPassportNo(strField)              -   Checks for valid Passport number
3.  isDate(dteCheck)                    -   Checks for valid Date and date not => today's date (m/d/Y or m-d-Y)
4.  isEarlier(dteCheck)                 -   Checks for valid Date and date not < today's date (m/d/Y or m-d-Y)
5.  isDateDiff(FromDate, ToDate)        -   Checks if the two dates differ (m/d/Y or m-d-Y)
6.  isLater(dteCheck)                   -   Checks for valid Date and date not > today's date (m/d/Y or m-d-Y)
7.  DaysInMonth(intMon,intYr)           -   Returns the No. of days for given valid Month & Year.
8.  isNumber(objValue)                  -   Checks for valid Numeric Entry
9.  isPhone(objValue)                   -   Checks for valid Phone Numbers
10. isZipcode(strZip)                   -   Checks for valid Indian ZipCode
11. isEmpty(s)                          -   Force Entry for a particular field
12. isWhitespace (s)                    -   Checks if the string contains space, tab, carriage return
13. Trim(s)                             -   Trims a string on both sides
14. RTrim(strVal)                       -   Right Trims a string
15. LTrim(strVal)                       -   Left Trims a given string
16. blnCheckSplChar(prmStrTextField)    -   Check for invalid entry of special characters in the email
17. ForceDate(val)                      -   Checks for valid date
18. CheckIsmail(obj)                    -   Checks for valid email string
19. ContainsSpecialChar(strVal)         -   Checks for invalid entry of special characters
20. isAlphabetsOnly(strVal)             -   Check if value contains Alphabets only
21. isAlphaNumericOnly(strVal)          -   Check if value contains Alphabets and Numbers only
22. isAllowed(strVal)                   -   Check if the Characters in the value are allowed
23. isDateDMY(dteCheck)                 -   Checks for valid Date (d-m-Y)
24. ChngDtDMY2MDY(InputDate)            -   Change Date format d-m-Y or d/m/Y to m/d/Y
25. ChngDtDMY2YMD(InputDate)            -   Change Date format d-m-Y or d/m/Y to Y/m/d

*/



// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";

/************************** By Rajan *************************
Checks if the 1st letter is a character and subsequent entries are numbers
****************************************************************/
function isPassportNoOld(strField)
{
    if (strField.charAt(0)>=0 && strField.charAt(0)<=9)
        return false;
    if (strField.length < 5)
        return false;
    for (var i = 1; i < strField.length; i++)
        if (strField.charCodeAt(i)<48 || strField.charCodeAt(i)>57)
            return false;
    return true;
}


function isPassportNo(strField)
{
    for (var i = 0; i < strField.length; i++)
        if ((strField.charCodeAt(i)<48) || ((strField.charCodeAt(i)>57) && (strField.charCodeAt(i)<65)) || ((strField.charCodeAt(i)>90) && (strField.charCodeAt(i)<97)) || (strField.charCodeAt(i)>123))
            return false;
    return true;
}

/*********************** By Rajan ***********************
Checks whether the given date is valid or not and it is not equal to or greater than today's date
Checks for the Following formats
    (mm/dd/yyyy) or (m/dd/yyyy) or (mm/d/yyyy) or (m/d/yyyy)
    (mm-dd-yyyy) or (m-dd-yyyy) or (mm-d-yyyy) or (m-d-yyyy)
This function calls the daysInMonth to get the number of days for a given month & date
Returns true if dteCheck is a valid date else false.
It checks for leap year also.
****************************************************************/
function isDate(dteCheck)
{
    var intDate
    var today
    dtetoday = new Date();
    for(var intCtr=0;intCtr<=dteCheck.length-1;intCtr++)
        if(dteCheck.charAt(intCtr)=="-")
            dteCheck=dteCheck.replace(/\-/,"/");
    intDate=dteCheck.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After dteCheck is split it stores in intDate as
    // intDate[0] is the month
    // intdate[1] is the day
    // intDate[2] is the year
    //
    if (intDate.length != 3)
        return false;
    if (intDate[0].length > 2 || intDate[1].length > 2 || intDate[0].length < 1 || intDate[1].length < 1 || intDate[2].length != 4 )
        return false;
    if (eval(intDate[2])>dtetoday.getFullYear())
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0])> (dtetoday.getMonth() + 1))
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0]) == (dtetoday.getMonth() +1) && eval(intDate[1]) >= dtetoday.getDate())
        return false;
    if (intDate[2]>1850 && intDate[0]>=1 && intDate[0]<=12 && intDate[1]>=1 && intDate[1]<=DaysInMonth(intDate[0],intDate[2]))
        return true;
    else
        return false;
}


function isEarlier(dteCheck)
{
    var intDate
    var today
    dtetoday = new Date();
    for(var intCtr=0;intCtr<=dteCheck.length-1;intCtr++)
        if(dteCheck.charAt(intCtr)=="-")
            dteCheck=dteCheck.replace(/\-/,"/");
    intDate=dteCheck.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After dteCheck is split it stores in intDate as
    // intDate[0] is the month
    // intdate[1] is the day
    // intDate[2] is the year
    //
    if (intDate.length != 3)
        return false;
    if (intDate[0].length > 2 ||  intDate[1].length > 2 || intDate[0].length < 1 || intDate[1].length < 1 || intDate[2].length != 4 )
        return false;
    if (eval(intDate[2])<dtetoday.getFullYear())
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0])< (dtetoday.getMonth() + 1))
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0]) == (dtetoday.getMonth() +1) && eval(intDate[1]) < dtetoday.getDate())
        return false;
    if (intDate[2]>1850 && intDate[0]>=1 && intDate[0]<=12 && intDate[1]>=1 && intDate[1]<=DaysInMonth(intDate[0],intDate[2]))
        return true;
    else
        return false;
}

function isDateDiff(FromDate, ToDate)
{
    var intDate
    var today
    dtetoday = new Date(FromDate);
    for(var intCtr=0;intCtr<=ToDate.length-1;intCtr++)
        if(ToDate.charAt(intCtr)=="-")
            ToDate=ToDate.replace(/\-/,"/");
    intDate=ToDate.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After dteCheck is split it stores in intDate as
    // intDate[0] is the month
    // intdate[1] is the day
    // intDate[2] is the year
    //
    if (intDate.length != 3)
        return false;
    if (intDate[0].length > 2 || intDate[1].length > 2 || intDate[0].length < 1 || intDate[1].length < 1 || intDate[2].length != 4 )
        return false;
    if (eval(intDate[2])<dtetoday.getFullYear())
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0])< (dtetoday.getMonth() + 1))
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0]) == (dtetoday.getMonth() +1) && eval(intDate[1]) < dtetoday.getDate())
        return false;
    if (intDate[2]>1850 && intDate[0]>=1 && intDate[0]<=12 && intDate[1]>=1 && intDate[1]<=DaysInMonth(intDate[0],intDate[2]))
        return true;
    else
        return false;
}



/**************************** By Rajan ****************************
Checks whether the given date is valid & is later than Today's Date
Checks for the Following formats
    (mm/dd/yyyy) or (m/dd/yyyy) or (mm/d/yyyy) or (m/d/yyyy)
    (mm-dd-yyyy) or (m-dd-yyyy) or (mm-d-yyyy) or (m-d-yyyy)
This function calls the isDate function to Check for valid date
Then checks for date greater than today.
Returns true if dteCheck is greater than today else false
****************************************************************/
function isLater(dteCheck)
{
    var intDate
    var today
    dtetoday = new Date();
    for(var intCtr=0;intCtr<=dteCheck.length-1;intCtr++)
        if(dteCheck.charAt(intCtr)=="-")
            dteCheck=dteCheck.replace(/\-/,"/");
    intDate=dteCheck.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After dteCheck is split it stores in intDate as
    // intDate[0] is the month
    // intdate[1] is the day
    // intDate[2] is the year
    if (intDate.length != 3)
        return false;
    if (eval(intDate[2])>dtetoday.getFullYear())
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0])> (dtetoday.getMonth() + 1))
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[0]) == (dtetoday.getMonth() +1) && eval(intDate[1]) > dtetoday.getDate())
        return false;
    if (intDate[2]>1850 && intDate[0]>=1 && intDate[0]<=12 && intDate[1]>=1 && intDate[1]<=DaysInMonth(intDate[0],intDate[2]))
        return true;
    else
        return false;
}


/**************************** By Rajan ****************************
This function DaysInMonth returns the No. of days for given valid Month & Year.
It checks for leap year also which accurs in years divisible by four.
If the year is a century checks it is divisible by 400.
****************************************************************/
function DaysInMonth(intMon,intYr)
{
//    switch(parseInt(intMon,10))
    switch(eval(intMon))
    {
        case 2:
            if ((intYr%100)==0)
                if ((intYr%400)==0)
                    return 29;
                else
                    return 28;
            else if((intYr%4)==0)
                    return 29;
            else
                return 28;
            break;
        case 4:
            return 30
            break;
        case 6:
            return 30
            break;
        case 9:
            return 30
            break;
        case 11:
            return 30
            break;
        default:
            return 31;
            break;
    }
}



/***************************** By Rajan *****************************
Returns true if the string passed in is a valid number
****************************************************************/
function isNumber(objValue)
{
    var strField = objValue;

    if (isWhitespace(strField)) return false;

    var i = 0;

    for (i = 0; i < strField.length; i++)
        if (strField.charAt(i) < '0' || strField.charAt(i) > '9')
        {
            return false;
        }
    return true;
}

/******************************************************************
Returns true if the string passed in is a valid phone number
****************************************************************/
function isPhone(objValue)
{
    var i,s;
    var condition;
    //var obj=Trim(str.value);
    var obj=objValue;
    var len = obj.length;

    for (i=0;i<len;i=i+1)
    {
        s =  obj.charCodeAt(i);
        condition=((s >= 48) && (s <= 57)) || (s==32)||(s==45)||(s==40)||(s==41);
        if (!(condition))
        {
            return false;
        }
    }
    return true;
}


/***************************** By Rajan *****************************
 This function determines if the string passed in is a valid
 Indian zip code.  It accepts  ######. If the
 string is valid, it returns true, else false.
****************************************************************/
function isZipcode(strZip)
{
    var s = strZip;

    if (s.length != 6)
    {
        return false;
    }

    for (var i=0; i < s.length; i++)
        if (s.charAt(i) < '0' || s.charAt(i) > '9')
        {
            return false;
        }
    return true;
}




/***************************** By Rajan *****************************
Check whether string s is empty.
***************************************************************/
function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}



/***************************** By Rajan *****************************
 Returns true if string is empty or whitespace characters only.
*****************************************************************/
function isWhitespace (s)

{
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
    // Check that current character isn't whitespace.
    var c = s.charAt(i);

    if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


/*********************** By Rajan *************************
Trim blank spaces in the given string on both sides(right & left)
****************************************************************/

function Trim(s)
{
    var j='';
    var i ='';

    var first='';
    var last ='';

    var val;
    val = s;

	if (val == '') return '';

    for(i = 0;i<val.length;i++)
    {
        if(val.charCodeAt(i)!=32)
        {
         for(j=i;j <val.length ;j++)
            first = first + val.charAt(j);

        break;
        }
    }

    i= first.length-1;
    while(first.charCodeAt(i)==32)
        i=i-1;

    j=0;
    for(j=0;j<i+1;j++)
        last = last + first.charAt(j);
    return last;
}


/**************************** By Rajan ****************************
Trim blank spaces in the given string on right side
****************************************************************/
function RTrim(strVal)
{
	if (strVal == '') return '';
    var j=strVal.length-1;
    while(strVal.charAt(j--)==' ');
    return strVal.substr(0,j+2);
}


/**************************** By Rajan ****************************
Trim blank spaces in the given string on left side
****************************************************************/
function LTrim(strVal)
{
	if (strVal == '') return '';
    var i=0;
    while(strVal.charAt(i++)==' ');
    return strVal.substr(--i,strVal.length-i+2);
}


/************************ By Rajan ****************************
Check for invalid entry of special characters in the email text box
*****************************************************************/
function blnCheckSplChar(prmStrTextField)
{
    var strX = prmStrTextField     //strX and strY variables to check for a matching string
    var strY = (prmStrTextField.match("\[^@]+\[@]{1}\[^@]+"));

    if ((prmStrTextField.match("\[^@]+\[@]{1}\[^@]+"))==null)
        {
            return true;
        }
        else
        {
            if (strX == strY)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
}

function ForceDate(val)
{
    var intDate
    var today
    dtetoday = new Date();
    for(var intCtr=0;intCtr<=val.length-1;intCtr++)
        if(val.charAt(intCtr)=="-")
            val=val.replace(/\-/,"/");
    intDate=val.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After dteCheck is split it stores in intDate as
    // intDate[0] is the month
    // intdate[1] is the day
    // intDate[2] is the year
    if (intDate.length != 3)
        return false;
    if (intDate[0].length > 2 || intDate[0].length==0 || intDate[1].length > 2 || intDate[1].length==0 )
        return false;
    if (intDate[2].length != 4)
        return false;
    if (intDate[2]>1950 && intDate[0]>=1 && intDate[0]<=12 && intDate[1]>=1 && intDate[1]<=DaysInMonth(intDate[0],intDate[2]))
        return true;
    else
        return false;
}


function CheckIsmail(obj)
 {
  var locAt=0;
  var locDot=0;
  var flagAt=0;
  var flagDot=0;
  var cntAt=0;
  var cntDot=0;

  for (i=0;i<obj.length;i++)
   {
     s =  obj.charCodeAt(i)
     if (i==0 || i==obj.length-1)
       {
         if ((s ==45) || (s==95))
          {
            return false;
          }
       }
     cndt=(s >=48 && s <= 57) || (s>=65 && s<=90) || (s>=97 && s<=122) || (s==64) || (s==46) || (s==95) || (s==45)
     if (!(cndt))
        {
          return false;
        }
      s1=obj.substr(i,1)
      if (s1=='@')
        {
          cntAt=cntAt+1
          locAt=i
        }
      if (s1=='.')
        {
          cntDot=cntDot+1
          locDot=i
        }
     /* if (locDot!=0 && locAt!=0)
      {
        if(locDot<locAt)
          {
            return false;
          }
       }*/
      if (cntAt >1)
      {
        return false;
      }
  }
    s2=obj.charCodeAt(locAt+1)
    s3=obj.charCodeAt(locAt-1)
    s=s2
    cndtBef=(s >= 48 && s <= 57) || (s>=65 && s<=90) || (s>=97 && s<=122)
    s=s3
    cndtAft=(s >= 48 && s <= 57) || (s>=65 && s<=90) || (s>=97 && s<=122)
    if (cndtBef && cndtAft)
     {
       flagAt=1
     }
     s2=obj.charCodeAt(locDot+1)
     s3=obj.charCodeAt(locDot-1)
     s=s2
     cndtBef=(s >= 48 && s <= 57) || (s>=65 && s<=90) || (s>=97 && s<=122)
     s=s3
     cndtAft=(s >= 48 && s <= 57) || (s>=65 && s<=90) || (s>=97 && s<=122)
     if (cndtBef && cndtAft)
      {
        flagDot=1
      }

     if (flagAt==1 && flagDot==1)
        {
          return true;
        }
     else
      {
        return false;
      }
 }


function ContainsSpecialChar(strVal)
{
    var pos;
    for (pos=0;pos<strVal.length;pos++)
    {
        if(strVal.charCodeAt(pos)==60 ||strVal.charCodeAt(pos)==62 ||strVal.charCodeAt(pos)==34)
            return true;
    }
    return false;
}

function isAlphabetsOnly(strVal)
{
    var pos;
    for (pos=0;pos<strVal.length;pos++)
    {
        if (strVal.charCodeAt(pos) != 32)
        {
            if ((strVal.charCodeAt(pos) > 90 && strVal.charCodeAt(pos) < 97) || strVal.charCodeAt(pos)< 65 || strVal.charCodeAt(pos)> 122)
                return false;
        }
    }
    return true;
}

function isAlphaNumericOnly(strVal)
{
    var pos;
    for (pos=0;pos<strVal.length;pos++)
    {
        if (strVal.charCodeAt(pos) != 32)
        {
            if ((strVal.charCodeAt(pos) > 90 && strVal.charCodeAt(pos) < 97) || (strVal.charCodeAt(pos) > 57 && strVal.charCodeAt(pos) < 65) || strVal.charCodeAt(pos)< 48 || strVal.charCodeAt(pos)> 122)
                return false;
        }
    }
    return true;
}

function isAllowed(strVal)
{
    var pos;
    for (pos=0;pos<strVal.length;pos++)
    {
        if (strVal.charCodeAt(pos) != 38 && strVal.charCodeAt(pos) != 39 && strVal.charCodeAt(pos) != 32)
        {
            if ((strVal.charCodeAt(pos) > 90 && strVal.charCodeAt(pos) < 97) || (strVal.charCodeAt(pos) > 57 && strVal.charCodeAt(pos) < 65) || strVal.charCodeAt(pos)< 48 || strVal.charCodeAt(pos)> 122)
                return false;
        }
    }
    return true;
}

function isDateDMY(dteCheck)
{
    var intDate
    var today
    dtetoday = new Date();
    for(var intCtr=0;intCtr<=dteCheck.length-1;intCtr++)
        if(dteCheck.charAt(intCtr)=="-")
            dteCheck=dteCheck.replace(/\-/,"/");
    intDate=dteCheck.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After dteCheck is split it stores in intDate as
    // intDate[0] is the day
    // intdate[1] is the month
    // intDate[2] is the year
    //
    if (intDate.length != 3)
        return false;
    if (intDate[1].length > 2 || intDate[0].length > 2 || intDate[1].length < 1 || intDate[0].length < 1 || intDate[2].length != 4 )
        return false;
    if (eval(intDate[2])>dtetoday.getFullYear())
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[1])> (dtetoday.getMonth() + 1))
        return false;
    if (eval(intDate[2])==dtetoday.getFullYear() && eval(intDate[1]) == (dtetoday.getMonth() +1) && eval(intDate[0]) >= dtetoday.getDate())
        return false;
    if (intDate[2]>1850 && intDate[1]>=1 && intDate[1]<=12 && intDate[0]>=1 && intDate[0]<=DaysInMonth(intDate[1],intDate[2]))
        return true;
    else
        return false;
}

function ChngDtDMY2MDY(InputDate)
{
    var OutputDate;
    var dteDate;
    for(var intCtr=0;intCtr<=InputDate.length-1;intCtr++)
        if(InputDate.charAt(intCtr)=="-")
            InputDate=InputDate.replace(/\-/,"/");
    dteDate=InputDate.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After InputDate is split it stores in intDate as
    // dteDate[0] is the day
    // dtedate[1] is the month
    // dteDate[2] is the year
    OutputDate = dteDate[1] + '/' + dteDate[0] + '/' + dteDate[2];
    return OutputDate;
}



function ChngDtDMY2YMD(InputDate)
{
    var OutputDate;
    var dteDate;
    for(var intCtr=0;intCtr<=InputDate.length-1;intCtr++)
        if(InputDate.charAt(intCtr)=="-")
            InputDate=InputDate.replace(/\-/,"/");
    dteDate=InputDate.split("/") //Splits the given date as day,month,year & stores in intDate array
    //After InputDate is split it stores in intDate as
    // dteDate[0] is the day
    // dtedate[1] is the month
    // dteDate[2] is the year
    OutputDate = dteDate[2] + '/' + dteDate[1] + '/' + dteDate[0];
    return OutputDate;
}
