/*
function writeDate(d) {
  aday = new Date(d);
  // return aday.toLocaleString();
  document.write (aday.toLocaleString());
}
*/
function isEmail(s, prompt) {
  var i = 1;
  var sLength = s.length;
  while ((i < sLength) && (s.charAt(i) != "@")) {
    i++
  }
  if ((i >= sLength) || (s.charAt(i) != "@")){
     alert(prompt);
     return false;
  }
  else
    i += 2;
  while ((i < sLength) && (s.charAt(i) != ".")) {
    i++
  }
  if ((i >= sLength - 1) || (s.charAt(i) != "."))
  {
    alert(prompt);
    return false;
  }
  else
    return true;
}

function isPhoneNumber(s, prompt) {

  if (isEmpty(s)){
      alert(prompt);
      return false;
    }

  for (var i = 0; i < s.length; i++) {
    if (!(s.charAt(i) <= "9" && s.charAt(i) >= "0") && s.charAt(i) != "-" && s.charAt(i) != "x")
    {
      alert(prompt);
      return false;
    }
  }
  return true;
}


function isBlank(s, prompt){
   if (isEmpty(s)){
      alert(prompt);
      return true;
   }
   return false;
}

function isEmpty(s) {
  if (s.length == 0)
    return true;

  for (var i = 0; i < s.length; i++) {
    if (s.charAt(i) != " ")
      return false;
  }
  return true;
}

function isNumber(s,prompt) {
  if (isEmpty(s)){
    alert(prompt);
    return true;
  }
  for (var i = 0; i < s.length; i++) {
    if (!(s.charAt(i) <= "9" && s.charAt(i) >= "0")){
      alert(prompt);
      return false;
    }
  }
  return true;
}

function isValidDate2(dateStr) {
// Date validation function courtesty of
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

// Checks for the following valid date formats:  YYYY-MM-DD
// old --- MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
if (isEmpty(dateStr))
  return true;

var datePat = /^(\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert(dateStr + " Date is not in a valid format.")
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[4];
year = matchArray[1];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}


function isTimeString(stringValue) {

  // this function is designed to mimic the "time" portion of the
  // VBScript IsDate() function, allowing times to be validated
  // with JavaScript in browsers before you run into a problem
  // in ASP pages with date database columns or the VBScript
  // CDate() function

  // you obviously want to strip the comments from production scripts
  // and place this function in the library file

  // create a String object
  var theString = new String(stringValue);

  // the string must have either two (hours and minutes) or three
  // (hours, minutes and seconds) tokens, delimited by ":";
  // split the string into an array of tokens
  var theTokens = theString.split(':');
  if ( theTokens.length < 2 || theTokens.length > 3 )
    return false;

  // convert the tokens to String objects, which will be needed later,
  // stripping whitespace
  var firstToken = new String(theTokens[0])
  firstToken = trim(firstToken);
  var middleToken;
  if ( theTokens.length == 3 ) {
    middleToken = new String(theTokens[1])
    middleToken = trim(middleToken);
  }
  var lastToken = new String(theTokens[theTokens.length - 1])
  lastToken = trim(lastToken);

  // the first token (hours) must be an integer between 0 and 23
  if ( ! isInteger(firstToken) )
    return false;
  if ( ! isIntegerInRange(firstToken, 0, 23) )
    return false;

  // are there three tokens?
  if ( theTokens.length == 3 ){

    // the middle token (minutes) must be an integer between 0 and 59
    if ( ! isInteger(middleToken) )
      return false;
    if ( ! isIntegerInRange(middleToken, 0, 59) )
      return false;
  }

  // the first one or two characters of the last token (either minutes
  // and optional am/pm indicator or seconds and am/pm indicator) must
  // be digits
  if ( ! isDigit(lastToken.charAt(0)) )
    return false;

  // the first character is a digit; split the last token into the minutes
  // or seconds value and the indicator; depending on the second character
  var lastValue;
  var ampmIndicator;
  if ( isDigit(lastToken.charAt(1)) ) {
    lastValue = new String(lastToken.substring(0, 2));
    if ( lastToken.length >= 3 )
      ampmIndicator = new String(trim(lastToken.substring(2, lastToken.length)));
    else
      ampmIndicator = new String();
  }
  else {
    lastValue = new String(lastToken.substring(0, 1));
    if ( lastToken.length >= 2 )
      ampmIndicator = new String(trim(lastToken.substring(1, lastToken.length)));
    else
      ampmIndicator = new String();
  }
  ampmIndicator = ampmIndicator.toUpperCase();

  // the last value must be between 0 and 59
  if ( ! isIntegerInRange(lastValue, 0, 59) )
    return false;

  // check the am/pm indicator, if there is one
  if ( ampmIndicator.length > 0 )
    if ( ! ( ampmIndicator == "AM" || ampmIndicator == "PM" ) )
      return false;

  // valid time
  return true;
}

// most of the following was derived from Netscape's FormChek.js
// library, which should be reviewed for documentation and comments

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

var whitespace = " \t\n\r";

function charInString(c, s) {
  for (i = 0; i < s.length; i++) {
    if (s.charAt(i) == c)
      return true;
    }
    return false
}

function isDigit(c) {
  return ( ( c >= "0" ) && ( c <= "9" ) )
}

function isInteger(s) {
  var i;
    for ( i = 0; i < s.length; i++ )
    {
        var c = s.charAt(i);
        if ( ! isDigit(c) ) return false;
    }
    return true;
}

function isIntegerInRange(s, a, b) {
    if ( ! isInteger(s) ) return false;
    var num = parseInt (s);
    return ( ( num >= a ) && ( num <= b ) );
}

function isNonnegativeInteger(s) {
    return ( isSignedInteger(s) && ( parseInt(s) >= 0 ) );
}

function isSignedInteger(s) {
    var startPos = 0;
    if ( ( s.charAt(0) == "-" ) || ( s.charAt(0) == "+" ) )
       startPos = 1;
    return ( isInteger(s.substring(startPos, s.length)) )
}

function lTrim(s) {
  var i = 0;
    while ( (i < s.length) && charInString(s.charAt(i), whitespace) )
       i++;
    return s.substring(i, s.length);
}

function makeArray(n) {
   for ( var i = 1; i <= n; i++ ) {
      this[i] = 0
   }
   return this
}

function rTrim(s) {
  var i = 0;
    while ( (i < s.length) && charInString(s.charAt(i), whitespace) )
       i++;
    return s.substring(i, s.length);
}

function trim(s) {
  return lTrim(rTrim(s));
}

function toggleVal(ctlName)
{
   var iValue = eval("document.formEdit." + ctlName + ".value");
   if (iValue == 0) {
     iValue = 1;
   }else
   {
     iValue = 0;
   }
   eval("document.formEdit." + ctlName + ".value = " + iValue);
}
function trim2(s) {
//new rtrim
 var i = s.length;
    while (s.charAt(i-1)==" ")
       i--;
//	alert(i);
    return s.substring(0, i);
}
function trimInput(Name) {
  eval("if(document.formEdit." + Name + ".value==trim2(document.formEdit."+Name+".value)){ } else {"+"document.formEdit." + Name + ".value=trim2(document.formEdit."+Name+".value);}")
// alert("if(document.formEdit." + Name + ".value==trim2(document.formEdit."+Name+".value)){ return;} else {"+"document.formEdit." + Name + ".value=trim2(document.formEdit."+Name+".value);}") // eval("document.formEdit." + Name + ".value=trim2(document.formEdit."+Name+".value);")

}
