/*
 * This function will trim all whitespace from around a
 * given value.
 */
function trim(str) {
  //str.replace(/^\s*/, '').replace(/\s*$/, '');
  return ltrim(rtrim(str));
}

function rtrim(str) {
  while(str.length > 0 && str.charAt(str.length-1) == ' ') {
    str = str.substring(0, str.length-1);
  }
  return str;
}

function ltrim(str) {
  while(str.length > 0 && str.charAt(0) == ' ') {
    str = str.substring(1);
  }
  return str;
}

function stripCharsInBag(s, bag) {
 var i;
 var returnString = "";
 for (i = 0; i < s.length; i++) {
  // Check that current character isn't whitespace.
  var c = s.charAt(i);
  if (bag.indexOf(c) == -1) returnString += c;
 }
 return returnString;
}

/*
 * This function will allow a user to validate that a field is
 * all numeric.  it will also allow a user to pass in selected
 * other characters to say are ok and not fail on.
 */
function isNumber(str, isDecimalAllowed) {
  if (str.search(/^0\d+$/) != -1) return false;

  var pass=false;
  if (isDecimalAllowed) {
    if (str.search(/^\d*(.\d+)?$/) != -1) pass = true;
  } else {
    if (str.search(/^\d+$/) != -1) pass=true;
  }
  return pass;
}

/*
 * This function will allow a user to validate that a field is
 * all numeric.  it will also allow a user to pass in selected
 * other characters to say are ok and not fail on.
 */
function isNumeric(string, allowedchars) {
  if (!string) return false;
  var Chars = "0123456789";
  for (var i = 0; i < string.length; i++) {
    if (Chars.indexOf(string.charAt(i)) == -1) {
      if (allowedchars != null) {
        if (allowedchars.indexOf(string.charAt(i)) == -1 ) {
          return false;
        }
      } else {
        return false;
      }
    }
  }
  return true;
}

/*
 * This function will allow a user to validate that a field is
 * all upper alpha.  it will also allow a user to pass in selected
 * other characters to say are ok and not fail on.
 */
function isUpper(string) {
  if (!string) return false;
  var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (var i = 0; i < string.length; i++) {
    if (Chars.indexOf(string.charAt(i)) == -1) {
      return false;
    }
  }
  return true;
}

/*
 * This function will allow a user to validate that a field is
 * all lower alpha.  it will also allow a user to pass in selected
 * other characters to say are ok and not fail on.
 */
function isLower(string) {
  if (!string) return false;
  var Chars = "abcdefghijklmnopqrstuvwxyz";
  for (var i = 0; i < string.length; i++) {
    if (Chars.indexOf(string.charAt(i)) == -1) {
      return false;
    }
  }
  return true;
}

/*
 * This function will allow a user to validate that a field is
 * all alpha.  it will also allow a user to pass in selected
 * other characters to say are ok and not fail on.
 */
function isAlpha(string, allowedchars) {
  if (!string) return false;
  for (var i = 0; i < string.length; i++) {
    if (!isUpper(string)) {
      if (!isLower(string)) {
        if (allowedchars != null) {
          if (allowedchars.indexOf(string.charAt(i)) == -1 ) {
            return false;
          }
        } else {
          return false;
        }
      }
    }
  }
  return true;
}

/*
 * This function allows a user to pass in a value and determin
 * if it is alphanumeric.  it will also allow a user to check
 * against a set of allowed characters to not fail on.
 */
function isAlphaNumeric(string, allowedchars) {
  if (!string) return false;
  var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  for (var i = 0; i < string.length; i++) {
    if (Chars.indexOf(string.charAt(i)) == -1) {
      if (allowedchars != null) {
        if (allowedchars.indexOf(string.charAt(i)) == -1 ) {
          return false;
        }
      } else {
        return false;
      }
    }
  }
  return true;
}

function isValidDate(object) {
  var value = object.value;
  if (value.search(/^\d{8}$/) == 0) {
    value = value.substring(0,2) + "/" + value.substring(2,4) + "/" + value.substring(4);
  } else if (value.search(/^\d{1}[\/|-]\d{2}[\/|-]\d{4}$/) == 0) {
    value = "0" + value;
  } else if (value.search(/^\d{2}[\/|-]\d{1}[\/|-]\d{4}$/) == 0) {
    value = value.substring(0,3) + "0" + value.substring(3,9);
  } else if (value.search(/^\d{1}[\/|-]\d{1}[\/|-]\d{4}$/) == 0) {
    value = "0" + value.substring(0,2) + "0" + value.substring(2,9);
  } else if (value.search(/^\d{2}[\/|-]\d{2}[\/|-]\d{4}$/) != 0) {
    return false;
  }
  var d = new Date(value);
  if (d == 'NaN') {
    return false;
  }

  while (value.indexOf("-") > 0) value = value.replace("-","/");
  object.value = value;
  
  var m = d.getMonth()+1;
  var y = d.getYear();
  var d1= d.getDate();
  var newDate = (m<10? "0"+m: m) + "/" + (d1<10? "0"+d1: d1) + "/" + (y<1000? 1900+y: y);
    
  if (newDate != value) {
    return false;
  }
  return true;

}

/*
 * This function will tell the user if the given object has a value
 * chosen.  also, if this instance allows the choice of the default,
 * then it will always return true as long as some item is selected.
 */
function validateSelect(object, zeroallowed) {
  ind = object.selectedIndex;
  if (ind == null) {
    return false;
  } else if (ind == 0) {
    if (zeroallowed) { return true; }
    return false;
  } else if (ind > 0) {
    return true;
  }
  return false;
}

/*
 * This function will tell the user if the given object has a value
 * entered.  also, if this instance allows the user to not enter any
 * data, it will always return true.  This will normally be called by
 * one of the specific function below.
 */
function validateText(object, blankallowed, numbers, letters, allowedchars) {
  val = object.value;
  if ( (val == null) || (trim(val).length == 0) ) {
    if (blankallowed) { return true; }
    return false;
  }
  val = trim(val);
  if (numbers && letters) {
    return isAlphaNumeric(val, allowedchars);
  } else if (numbers) {
    return isNumeric(val, allowedchars);
  } else if (letters) {
    return isAlpha(val, allowedchars);
  }

  return false;
}

/*
 * This function is used as an entry point to the validateText() function.
 * this will set up the parameters to make it easier to call to validate
 * an alpha only field
 */
function validateAlphaText(object, blankallowed, allowedchars) {
  return validateText(object, blankallowed, false, true, allowedchars);
}

/*
 * This function is used as an entry point to the validateText() function.
 * this will set up the parameters to make it easier to call to validate
 * a number only field.
 */
function validateNumericText(object, blankallowed, allowedchars) {
  return validateText(object, blankallowed, true, false, allowedchars);
}

/*
 * This function is used as an entry point to the validateText() function.
 * this will set up the parameters to make it easier to call to validate
 * a alphanumeric field.
 */
function validateAlphaNumericText(object, blankallowed, allowedchars) {
  return validateText(object, blankallowed, true, true, allowedchars);
}

function getAgeByDOB(dob) {
  var now = new Date();
  if ( top.todayYear != null ) {
   now = new Date(top.todayYear, top.todayMonth - 1, top.todayDate);
  }
  var age = now.getFullYear() - dob.getFullYear();

  if (now.getMonth() < dob.getMonth()) age--;
  if ((now.getMonth() == dob.getMonth())
       && (now.getDate() < dob.getDate()) ) age--;
  return age;
}

function popUpLink(link){
  var WINOPTION_POPUP;
  WINOPTION_POPUP="'directories=1,toolbar=1,status=1,";
  WINOPTION_POPUP += "resizable=1,menubar=1,scrollbars=1,location=1,";
  WINOPTION_POPUP += "width=550,height=450'";

  window.WinObject=top.window.open(link, "popup", WINOPTION_POPUP);
}

function populateSelect(selectName, optionlist) {
  selectName.options.length = 0;
  selectName.options.length = optionlist.length;
  for (var i=0; i< optionlist.length; i++) {
    var newOption = new Option(optionlist[i][1],optionlist[i][0], false, false);
		selectName.options[i] = newOption;
  }
}

function getSelect(selectName) {
 if (selectName.length > 0) {
  return trim(selectName.options[selectName.selectedIndex].value);
 }
 else return null;
}

function getSelectText(selectName) {
 if (selectName.length > 0) {
  return trim(selectName.options[selectName.selectedIndex].text);
 }
 else return null;
}

function setSelect(selectName, entryName) {
 var howMany = selectName.length;
 var i = 0;
 while ((i < howMany) && (trim(selectName.options[i].value) != trim(entryName)))
  i++;
 if (i<howMany) selectName.selectedIndex = i;
}

function validateRadio(object, required) {
  var rtnVal=false;
  if (!object.length) {
    rtnVal = object.checked;
  } else {
    for (var i=0; i<object.length; i++) {
      if (object[i].checked) {
        rtnVal=true;
        break;
      }
    }
  }

  return rtnVal;
}

/*
 * Date general functions
 */
function checkdate(objName) {
  var datefield = objName;
  if (chkdate(objName) == false) {
    datefield.select();
    alert("That date is invalid.  Please try again.");
    datefield.focus();
    return false;
  } else {
    return true;
  }
}

function chkdate(objName) {
  var strDatestyle = "US"; //United States date style
  //var strDatestyle = "EU";  //European date style
  var strDate;
  var strDateArray;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var booFound = false;
  var datefield = objName;
  var strSeparatorArray = new Array("-"," ","/",".");
  var intElementNr;
  var err = 0;
  var strMonthArray = new Array(12);
  strMonthArray[0] = "01";
  strMonthArray[1] = "02";
  strMonthArray[2] = "03";
  strMonthArray[3] = "04";
  strMonthArray[4] = "05";
  strMonthArray[5] = "06";
  strMonthArray[6] = "07";
  strMonthArray[7] = "08";
  strMonthArray[8] = "09";
  strMonthArray[9] = "10";
  strMonthArray[10] = "11";
  strMonthArray[11] = "12";
  strDate = datefield.value;
  
  if (strDate.length < 1) {
    return true;
  }
  for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
   if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
     strDateArray = strDate.split(strSeparatorArray[intElementNr]);
     if (strDateArray.length != 3) {
       err = 1;
       return false;
     } else {
       strDay = strDateArray[0];
       strMonth = strDateArray[1];
       strYear = strDateArray[2];
     }
     booFound = true;
   }
  }
  
  if (booFound == false) {
    if (strDate.length>5) {
      strDay = strDate.substr(0, 2);
      strMonth = strDate.substr(2, 2);
      strYear = strDate.substr(4);
    }
  }
  
  if (strYear.length == 2) {
    strYear = '20' + strYear;
  }
  
  // US style
  if (strDatestyle == "US") {
    strTemp = strDay;
    strDay = strMonth;
    strMonth = strTemp;
  }
  
  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    err = 2;
    return false;
  }
  
  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i = 0;i<12;i++) {
      if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
        intMonth = i+1;
        strMonth = strMonthArray[i];
        i = 12;
      }
    }
    if (isNaN(intMonth)) {
      err = 3;
      return false;
    }
  }
  
  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    err = 4;
    return false;
  }
  if (intMonth>12 || intMonth<1) {
    err = 5;
    return false;
  }
  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
    err = 6;
    return false;
  }
  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
    err = 7;
    return false;
  }
  if (intMonth == 2) {
    if (intday < 1) {
      err = 8;
      return false;
    }
    if (LeapYear(intYear) == true) {
      if (intday > 29) {
        err = 9;
        return false;
      }
    } else {
      if (intday > 28) {
        err = 10;
        return false;
      }
    }
  }

  if (strDatestyle == "US") {
    if(intday < 10){
      datefield.value = strMonthArray[intMonth-1] + "/" + 0+intday+"/" + strYear;
    } else {
      datefield.value = strMonthArray[intMonth-1] + "/" + intday+"/" + strYear;
    }
  } else {
    datefield.value = intday + "/" + strMonthArray[intMonth-1] + "/" + strYear;
  }
  return true;
}

function LeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) { return true; }
  } else {
    if ((intYear % 4) == 0) { return true; }
  }
  return false;
}


function doDateCheck(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    alert("The dates are valid.");
  } else {
    if (from.value == "" || to.value == "") {
      alert("Both dates must be entered.");
    } else {
      alert("To date must occur after the from date.");
    }
  }
}

// var bExit is also referenced in confirmCloseWindow.js
var bExit = false;

/*
window.onunload = function(){
//alert("onunload: bExit = "+bExit);
	if( bExit ){
		var h = screen.height;
		var w = screen.width;
        //use if you really need this function for something
		//window.open("index.jsp","logoff",	"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=10,height=10,screenX=" + w + ",screenY=" + h + ",left=" + w + ",top=" + h);
	}
}
*/