var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9
var phoneNumberDelimiters = "()-Xx ";
var errPHONE = "Invalid phone number format.\nPlease use the following format: (555)555-1212"
var defaultEmptyOK = false
var digitsInUSPhoneNumber = 14;
var reInteger = /^\d+$/

function checkUSPhone (theField, emptyOK)
{   if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { if (theField.length==2) return false;
	   //alert("'"+theField.value+"'");
	   var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone, false))
          return false;
       else
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }
    }
}

function checkPhone (theString, emptyOK){
if (checkPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theString))) return true;
    else {
    	if (theString.length==2) return false;
	  var normalizedPhone = stripCharsInBag(theString, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone, false))
          return false;
       else{
	  return true;
       }
    }
}

function reformatUSPhone (USPhone){
	if(USPhone.length == 10)
		return (reformat (USPhone, "(", 3, ") ", 3, "-", 4));
	else
		return (reformat (USPhone, "(", 3, ") ", 3, "-", 4, " X ", 4));
}

function isEmpty(s){
	return ((s == null) || (s.length == 0))
}

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;
}
function reformat (s){
var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}
function isUSPhoneNumber (s){
    if (isEmpty(s))
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    if ((isInteger(s) && s.length <= digitsInUSPhoneNumber) && (isInteger(s) && s.length >= 10))
		return true;
	else
		return false;
}
function isInteger (s){
var i;
    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}

function isAlphanumeric(s)
{   var i;
    if (isEmpty(s))
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }
    return true;
}

function isAlphaAndNumeric(s)
{   var i;
    var hasLetter = false;
    var hasDigit = false;

    if (isEmpty(s))
       if (isAlphaAndNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphaAndNumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (isLetter(c))
        	hasLetter = true;
       	if (isDigit(c))
        	hasDigit = true;
    }
    if (hasDigit && hasLetter)
    	return true;
    else
	return false;
}


function isEmail(string,EmptyOK) {
    if (isEmpty(string)) return EmptyOK
    //if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/) != -1)
    //if (string.search(/^(([\w!#-'*+\-\/=?^`{-~]+(\.[\w!#-'*+\-\/=?^`{-~]+)*)|(\".+\"))@((([a-zA-Z0-9]+[a-zA-Z0-9\-]*[a-zA-Z0-9]+\.)+[a-zA-Z]{2,})|(\[[0-9]{1,3}(\.[0-9]{1,3}){3}\]))$/ ) != -1)
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/) != -1)
        return true;
    else
        return false;
}

function isLetter (c){
return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c){
return ((c >= "0") && (c <= "9"))
}

function isZIPCode (s,EmptyOK)
{  if (isEmpty(s))
       if (isZIPCode.arguments.length == 1) return EmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) &&
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}
function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}
function isValidFile(string,EmptyOK){
    var test_str = /#+/;
    if (isEmpty(string)) return EmptyOK
    if (string.search(test_str) == -1)
        return true;
    else
        return false;
}

function currency(anynum) {
//-- Returns passed number as string in $xxx,xxx.xx format.
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}

//--- Adds comma in thousands place.
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}

//-- Adds comma in millions place.
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval = dStr + pStr
//-- Put numbers in parentheses if negative.
if (currency_symbol != '')
	cur_smb = currency_symbol
else
	cur_smb = "$"

if (anynum<0) {

	if (currency_symbol_position == 'R')
	   	return "- " + retval + " " + cur_smb
	else
	   	return "- " + cur_smb + retval
}
else {
	if (currency_symbol_position == 'R')
	   	return retval + " " + cur_smb
	else
   		return cur_smb + retval
      }
}

function isFloat(value) {
	return (parseFloat(value) == value);
}

function isNumeric(string, ignoreWhiteSpace) {
if (string.search) {
	if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
}
return true;
}
function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            //alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }

   return "";
}

function removeCurrency(strValue) {
// DESCRIPTION: Removes currency formatting from source string.

  var objRegExp = /\(/;
  var strMinus = '';
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

