﻿// JScript File
function trimLeft(s)
{
    var i;
    i=0;
    var n;
    n = s.length ;
    while((i<n)&&(s.charAt(i)==' ')) i++;
    s = s.substring(i);
    return(s);
}

function trimRight(s)
{
    var n;
    n = s.length;
    var i;
    i = s.length-1;
    while((i>=0)&&(s.charAt(i)== ' ')) i--;
    s = s.substring(0,i+1);
    return(s);
}

function trimCenter(s)
{
    var n;
    var result = "";
    n = s.length;
    var i;
    for(i=0;i<n;i++)
    {
        if (s.charAt(i) == ' ')
        {
            if (result.charAt(result.length-1) != ' ')
                result = result + s.charAt(i);
        }
        else
        {
            result = result + s.charAt(i);
            
        }
    }
    return (result);
}

function trim(s)
{
    s = trimLeft(s);
    s = trimRight(s);
    return(s);
}

function TrimAllTextBoxData()
{
    var arrTextBox = document.documentElement.getElementsByTagName('input');
    for(var i = 0; i < arrTextBox.length; i++)
    {
        if (arrTextBox[i].type == 'text')
        {
            arrTextBox[i].value = trim(arrTextBox[i].value);
        }
    }
}

function removeNL(s) {
    /*
    ** Remove NewLine, CarriageReturn and Tab characters from a String
    **   s  string to be processed
    ** returns new string
    */
    r = "";
    for (i=0; i < s.length; i++) {
        if (s.charAt(i) != '\n' &&
            s.charAt(i) != '\r' &&
            s.charAt(i) != '\t') {
        r += s.charAt(i);
        }
    }
    return r;
}

function isNumberInput(field, event) 
{
  var key, keyChar;
  if (window.event)
    key = window.event.keyCode;
  else if (event)
    key = event.which;
  else
    return true;
  // Check for special characters like backspace
  if (key == null || key == 0 || key == 8 || key == 13 || key == 27) {
    return true;
  }
  // Check to see if it's a number
  keyChar =  String.fromCharCode(key);
  if (/\d/.test(keyChar)) {
     return true;
  } 
  else {
    return false;
  }
}


/*===============================*/
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
}

function month_day (day, month, year)  {
    var daysInMonth = DaysArray(12)
    day = parseInt(day);
    month = parseInt(month);
    year = parseInt(year);
    if((month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])  return false;
    else return true;
}

/*============================*/
/*Regular Expression functions*/
function IsMatchNumber(stringToMatch)
{
    //var re = new RegExp("^\d+$");
    var re = new RegExp("^[0-9]+$");
//    alert(stringToMatch);
//    alert(re.test(stringToMatch));
    if (re.test(stringToMatch)) 
        return true;
    else 
        return false; 
}


function FormatTextLength(textbox) {
        var val = textbox.value;
        if(val.length > 400){
            textbox.value = val.substring(0,399);            
        }
    }