/* 

    Puneet's String Functions
    URL   : http://www.puneetarora.com
    Email : puneet@mailarts.com

*/

function left(string, n)  {         // returns the leftmost n characters of a string
    if (string.length<n) 
            return string;
    return string.substring(0,n)
}

function right(string,n) {        // returns the rightmost n characters of a string
    if (string.length<n) 
            return string;
    return string.substring(string.length - n, string.length);    
}

function ltrim(string) {                 // removes spaces from the left of a string and returns the resultant string
   for (i=0;i<string.length;i++) {
       if (string.charAt(i) != " ")
               break;
   }
   return right(string,string.length-i);
}

function rtrim(string) {                 // removes spaces from the right of a string and returns the resultant string
   for (i=string.length-1;i>-1;i--) {
       if (string.charAt(i) != " ")
               break;
   }
   return left(string,i+1);
}

function trim(string) {                // removes spaces from left and right
    return ltrim(rtrim(string));
}

function ReplaceChar(strval, charA, charB) {
    // Replaces any presence of charA in strval by charB and returns the new strval
    retval = "";
    tempchar = "";
    for (i=0; i<strval.length; i++) {
        if (strval.charAt(i) == charA)
            tempchar = charB;
          else
            tempchar = strval.charAt(i);
        retval += tempchar;
    }
    return retval;
}

function checkdate (day,month,year) {
    DaysInMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
    strYear = year+"";
    if (strYear.length != 4)
        return false;
    // Check for leap year
    if (Math.ceil(year/4) == (year/4))    // if the year is divisible by 4
        leapyear=true;
      else
        leapyear=false;
    if (leapyear)
        DaysInMonth[2] = 29;
    if (day>DaysInMonth[month])
        return false;
      else
        return true;
}
