//**************************************************************************
// ltrim() -> /^\s+/
// rtrim() -> /\s+$/
// trim() -> /^\s+|\s+$/g
//**************************************************************************
String.prototype.ltrim = function() {return this.replace(/^\s+/,'');};
String.prototype.rtrim = function() {return this.replace(/\s+$/,'');};
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,'');};

//**************************************************************************
// isDefined(s)
//**************************************************************************
function isDefined(s)
{
 return (typeof(window[s]) == "undefined") ? false: true;
}

//**************************************************************************
// isYearRange(s)
//**************************************************************************
function isYearRange(s)
{
 return /^\d{4,4}-\d{4,4}$/.test(s) && compareInteger(left(s,4),right(s,4)) != '>';
}

//**************************************************************************
// isDBObject(s)
// First character must be alpha-numeric and rest not special or white-space
//**************************************************************************
function isDBObject(s)
{
 return /^([0-9]|[a-z])+\w*$/i.test(s);
}

//**************************************************************************
// isHexColor(s)
//**************************************************************************
function isHexColor(s)
{
 return /^(\d|[A-F]){6,6}$/.test(s);
}

//**************************************************************************
// isMedicareProviderID(s)
//**************************************************************************
function isMedicareProviderID(s)
{
 return /^(\d|[A-Z]){6,6}$/.test(s);
}

//**************************************************************************
// isString(s)
//**************************************************************************
function isString(s)
{
 return /^.+$/.test(s);
}

//**************************************************************************
// isNotAllowedComment(s)
//**************************************************************************
function isNotAllowedComment(s)
{
 return /(<|>)/.test(s);
}

//**************************************************************************
// isInteger(s)
//**************************************************************************
function isInteger(s)
{
 return /^\-*\d+$/.test(s);
}

//**************************************************************************
// isNonNegativeInteger(s)
//**************************************************************************
function isNonNegativeInteger(s)
{
 return /^\d+$/.test(s);
}

//**************************************************************************
// isPositiveInteger(s)
//**************************************************************************
function isPositiveInteger(s)
{
 return /^\d+$/.test(s) && s > 0;
}

//**************************************************************************
// compareInteger(i1,i2)
// Expects valid Integer values for i1, i2
//**************************************************************************
function compareInteger(i1,i2)
{
 if (i1 > i2)
  return '>';
 else if (i1 < i2)
  return '<';
 else
  return '=';
}

//**************************************************************************
// isMoney(s)
//**************************************************************************
function isMoney(s)
{
 return /\d+/.test(s) && /^\-*\d*\.*\d*$/.test(s);
}

//**************************************************************************
// compareMoney(m1,m2)
// Expects valid Money values for m1, m2
//**************************************************************************
function compareMoney(m1,m2)
{
 if (m1 > m2)
  return '>';
 else if (m1 < m2)
  return '<';
 else
  return '=';
}

//**************************************************************************
// isEmail(s)
//**************************************************************************
function isEmail(s)
{
 return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/.test(s);
}

//**************************************************************************
// appendEmail(o)
//**************************************************************************
function appendEmail(o)
{
 if (o.value.length != 0 && /@/.test(o.value) == false)
 {
  o.value = o.value + '@healthcarepayment.com';
 }
}

//**************************************************************************
// isPassword(s)
// Minimum 8 non-whitespace characters
// Must contain at least one alpha, and at least one digit or special
//**************************************************************************
function isPassword(s)
{
 specialchr = /[^a-z0-9]/i;
 alphachr = /[a-z]/i;
 digitchr = /\d/;
 validchr = /^\S{8,}$/g;

 return validchr.test(s) && alphachr.test(s) && (specialchr.test(s) || digitchr.test(s));
}

//**************************************************************************
// isPhone(s)
//**************************************************************************
function isPhone(s)
{
 return /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(s);
}

//**************************************************************************
// isYear(s)
//**************************************************************************
function isYear(s)
{
 return /^\d{4,4}$/.test(s) && isDate('1/1/'+s);
}

//**************************************************************************
// isMonthDay(s)
//**************************************************************************
function isMonthDay(s)
{
 return /^\d{1,2}\/\d{1,2}$/.test(s) && isDate(s+'/1900');
}

//**************************************************************************
// isDate(s) - [%m/%d/yyyy] format
//**************************************************************************
function isDate(s)
{
 var a = s.split("/");
 var m = a[0];
 var d = a[1];
 var y = a[2];

 if (y == undefined || (y.length != 4))
 {
  return false;
 }

 // new Date(s) - takes in any string value
 var dt = new Date(s);

 // only valid incoming s.split into %m/%d/yyyy will match to Date(s) Month/Date/FullYear components
 // getMonth() - returns 0-11
 return ((m==dt.getMonth()+1) && (d==dt.getDate()) && (y==dt.getFullYear()));
}

//**************************************************************************
// compareDate(d1,d2) - [%m/%d/yyyy] format
// Expects valid Date values for d1, d2
//**************************************************************************
function compareDate(d1,d2)
{
 if (Date.parse(d1) > Date.parse(d2))
  return '>';
 else if (Date.parse(d1) < Date.parse(d2))
  return '<';
 else
  return '=';
}


//**************************************************************************
// left(s,n)
// return left 'n' characters found in 's' string
//**************************************************************************
function left(s,n)
{
 if (n <= 0)
  return '';
 else if (n > String(s).length)
  return s;
 else
  return String(s).substring(0,n);
}

//**************************************************************************
// right(s,n)
// return right 'n' characters found in 's' string
//**************************************************************************
function right(s,n)
{
 if (n <= 0)
  return '';
 else if (n > String(s).length)
  return str;
 else
 {
  return String(s).substring(String(s).length, String(s).length - n);
 }
}

//**************************************************************************
// disableButtons()
// disable all form element where type submit, reset, or button
//**************************************************************************
function disableButtons()
{
 var f = document.getElementsByTagName("form");

 // for each form obj
 for(i = 0; i < f.length; i++)
 {
  // for each form element obj
  for(j = 0; j < f[i].length; j++)
  {
   e = f[i][j];
   if(e.getAttribute('type') == 'submit' || e.getAttribute('type') == 'reset' || e.getAttribute('type') == 'button')
   {
    e.disabled = true;
   }
  }
 }
}

//**************************************************************************
// appendContent(layerID,content)
//**************************************************************************
function appendContent(layerID,content)
{
 if(document.getElementById)
 {
  document.getElementById(layerID).innerHTML+=content;
 }
 else if(document.all)
 {
  document.all[layerID].innerHTML+=content;
 }
}

//**************************************************************************
// replaceContent(layerID,content)
//**************************************************************************
function replaceContent(layerID,content)
{
 if(document.getElementById)
 {
  document.getElementById(layerID).innerHTML=content;
 }
 else if(document.all)
 {
  document.all[layerID].innerHTML=content;
 }
 else if(document.layers)
 {
  with(document.layers[layerID].document)
  {
   open();
   write(content);
   close();
  }
 }
}

//**************************************************************************
// displayLength(layerID,obj,suffix)
//**************************************************************************
function displayLength(layerID,obj,suffix)
{
 var content = obj.value.length + suffix;

 replaceContent(layerID,content);
}

//**************************************************************************
// addOption(selectbox,text,value)
//**************************************************************************
function addOption(selectbox,text,value )
{
 var optn = document.createElement("OPTION");
 optn.text = text;
 optn.value = value;
 selectbox.options.add(optn);
}

//**************************************************************************
// getCheckedValue(radioObj)
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
//**************************************************************************
function getCheckedValue(radioObj)
{
 if(!radioObj)
  return "";
 var radioLength = radioObj.length;
 if(radioLength == undefined)
  if(radioObj.checked)
   return radioObj.value;
  else
   return "";

 for(var i = 0; i < radioLength; i++)
 {
  if(radioObj[i].checked)
  {
   return radioObj[i].value;
  }
 }
 return "";
}

//**************************************************************************
// setCheckedValue(radioObj,value)
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
//**************************************************************************
function setCheckedValue(radioObj, value)
{
 if(!radioObj)
  return;
 var radioLength = radioObj.length;
 if(radioLength == undefined)
 {
  radioObj.checked = (radioObj.value == value.toString());
  return;
 }

 for(var i = 0; i < radioLength; i++)
 {
  radioObj[i].checked = false;
  if(radioObj[i].value == value.toString())
  {
   radioObj[i].checked = true;
  }
 }
}

//**************************************************************************
// initSelectItems(lst,items)
// lst --> Select Field
// items --> Array {id, name} of items to populate Select Field with
//**************************************************************************
function initSelectItems(lst,items)
{
 var i = 0;
 lst.value = 0;
 // Erase all items but the first
 for (i=lst.options.length-1;i>0;i--)
 {
  lst.options[i] = null;
 }
 // Insert new items
 if (items!==false)
 {
  for (i in items)
  {
   lst.options[lst.options.length] = new Option(items[i].name,items[i].id);
  }
 }
}

//**************************************************************************
// getCurrentDateTime()
//**************************************************************************
function getCurrentDateTime()
{
 now = new Date();
 year = now.getFullYear();
 month = right('00' + (now.getMonth() + 1),2);
 date = right('00' + (now.getDate()),2);
 hour = now.getHours();
 min = now.getMinutes();
 sec = now.getSeconds();

 if (min <= 9)
 {
  min = "0" + min;
 }

 if (sec <= 9)
 {
  sec = "0" + sec;
 }

 if (hour > 12)
 {
  hour = hour - 12;
  add = " PM";
 }
 else
 {
  hour = hour;
  add = " AM";
 }

 if (hour == "12")
 {
  add = " PM";
 }

 if (hour == "00")
 {
  hour = "12";
 }

 return year + "-" + month + "-" + date + " " + ((hour<=9) ? "0" + hour : hour) + ":" + min + ":" + sec + add;
}
