//<SCRIPT LANGUAGE="JavaScript">
//<!--
// ----------------------------------------------------------------
//  format_telephone formats telephone numbers
//  as (xxx) xxx-xxxx
function FormatPhone(thisControl)
{
  var strInitial = thisControl.value
  var strFinal = new String()
  var j = 0
  var rePhone = new RegExp("[(]\\d\\d\\d[)][ ]\\d\\d\\d[-]\\d{4}$");

  if (strInitial.length > 2)
    strFinal = "(";
  // here we check for the already formatted phone number and exit if true
  if ((rePhone.test(strInitial) == true))
  {
    return true;
  }
  // here we begin processing the field, placing the numbers into a new phone number string
  for (var i = 0; i < strInitial.length; ++i)
  {
    if ((strInitial.charAt(i) == '0') || (strInitial.charAt(i) == '1') ||
        (strInitial.charAt(i) == '2') || (strInitial.charAt(i) == '3') ||
        (strInitial.charAt(i) == '4') || (strInitial.charAt(i) == '5') ||
        (strInitial.charAt(i) == '6') || (strInitial.charAt(i) == '7') ||
        (strInitial.charAt(i) == '8') || (strInitial.charAt(i) == '9'))
    {
      j = j + 1
      if (j == 4)
      {
        strFinal = strFinal + ") ";
      }
      if (j == 7)
      {
        strFinal = strFinal + "-";
      }
      if (j < 11)
      {
        strFinal = strFinal + strInitial.charAt(i);
      }
    }
  }
  thisControl.value = strFinal;
  if (strFinal.length == 1)
	 thisControl.value = "";
	
  // here we check for an entry clearly too long or too short and flag it
  if ((strFinal.length != 14))
  {
    return false;
  }
  else
  {
    return true;
  }
}
// ----------------------------------------------------------------
//  format_ssn formats telephone numbers
//  as xxx-xx-xxxx
function FormatSSN(thisControl)
{
  var strInitial = thisControl.value
  var strFinal = new String()
  var j = 0
  var reSSN = new RegExp("\\d\\d\\d\-\\d\\d\-\\d\\d\\d\\d$");

  strFinal = "";
  // here we check for the already formatted phone number and exit if true
  if ((reSSN.test(strInitial) == true))
  {
    return true;
  }
  // here we begin processing the field, placing the numbers into a new phone number string
  for (var i = 0; i < strInitial.length; ++i)
  {
    if ((strInitial.charAt(i) == '0') || (strInitial.charAt(i) == '1') ||
        (strInitial.charAt(i) == '2') || (strInitial.charAt(i) == '3') ||
        (strInitial.charAt(i) == '4') || (strInitial.charAt(i) == '5') ||
        (strInitial.charAt(i) == '6') || (strInitial.charAt(i) == '7') ||
        (strInitial.charAt(i) == '8') || (strInitial.charAt(i) == '9'))
    {
      j = j + 1
      if (j == 4)
      {
        strFinal = strFinal + "-";
      }
      if (j == 6)
      {
        strFinal = strFinal + "-";
      }
      if (j < 10)
      {
        strFinal = strFinal + strInitial.charAt(i);
      }
    }
  }
  thisControl.value = strFinal;
  if (strFinal.length == 1)
	 thisControl.value = "";
	
}
// ----------------------------------------------------------------
//  format_date formats telephone numbers
//  as mm/dd/yyyy
function FormatDate(thisControl)
{
  var strInitial = thisControl.value
  var strFinal = new String()
  var j = 0
  var reDate = new RegExp("\\d\\d//\\d\\d//\\d\\\d\\d\\d\\d$");

  strInitial = InjectZero(strInitial);

  strFinal = "";
  // here we check for the already formatted phone number and exit if true
  if ((reDate.test(strInitial) == true))
  {
    return true;
  }
  // here we begin processing the field, placing the numbers into a new phone number string
  for (var i = 0; i < strInitial.length; ++i)
  {
    if ((strInitial.charAt(i) == '0') || (strInitial.charAt(i) == '1') ||
        (strInitial.charAt(i) == '2') || (strInitial.charAt(i) == '3') ||
        (strInitial.charAt(i) == '4') || (strInitial.charAt(i) == '5') ||
        (strInitial.charAt(i) == '6') || (strInitial.charAt(i) == '7') ||
        (strInitial.charAt(i) == '8') || (strInitial.charAt(i) == '9'))
    {
      j = j + 1
      if (j == 3)
      {
        strFinal = strFinal + "/";
      }
      if (j == 5)
      {
        strFinal = strFinal + "/";
      }
      if (j < 9)
      {
        strFinal = strFinal + strInitial.charAt(i);
      }
    }
  }
  thisControl.value = strFinal;
  if (strFinal.length == 1)
	 thisControl.value = "";	
}
function InjectZero(orig)
{
   var t = "";

   if ( (orig.length > 2) && (orig.charAt(1) == '/') )
	orig = "0" + orig;

   if ( (orig.length > 4) && (orig.charAt(4) == '/') )
   {
       for (var i=0;i<orig.length;i++)
       {
          if (i == 3) 
          {
            t = t + "0";
          }
          t = t + orig.charAt(i); 
       }      
       orig = t;       
   }

   return orig;
}

function isThousands(position)
{
   if (Math.floor(position/3)*3==position) return true;
   return false;
};

function FormatMoney(thisControl)
{
    FormatNumber(thisControl,15);    
	thisControl.value = FormatMoney2(thisControl.value,"$",",",".");
	if (thisControl.value == "$0.00")
		thisControl.value = "";
}
function FormatMoney2 (theNumber,theCurrency,theThousands,theDecimal)
{   
   if (theDecimal==undefined)
   {
        var theDecimalDigits = "";

   } else {

        var theDecimalDigits = Math.round((theNumber*100)-(Math.floor(theNumber)*100));
   }

   theDecimalDigits= "" + (theDecimalDigits + "0").substring(0,2);

   theNumber = "" + Math.floor(theNumber);

   var theOutput = theCurrency;

   for (x=0; x<theNumber.length; x++)
   {

      theOutput += theNumber.substring(x, x+1);

      if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1!=0))
      {
         theOutput += theThousands;
      };
   };

   if (theDecimal!=undefined)
   {
       theOutput += theDecimal + theDecimalDigits;
   }

   return theOutput;
};
function FormatRates (thisControl)
{   
   FormatNumber(thisControl,15);
   var theNumber = thisControl.value;
   
   var theCurrency = "";
   var theThousands = "";
   var theDecimal = ".";
   if (theDecimal==undefined)
   {
        var theDecimalDigits = "";

   } else {

        var theDecimalDigits = Math.round((theNumber*1000)-(Math.floor(theNumber)*1000));
   }

   theDecimalDigits= "" + (theDecimalDigits + "0").substring(0,3);

   theNumber = "" + Math.floor(theNumber);

   var theOutput = theCurrency;

   for (x=0; x<theNumber.length; x++)
   {

      theOutput += theNumber.substring(x, x+1);

      if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1!=0))
      {
         theOutput += theThousands;
      };
   };

   if (theDecimal!=undefined)
   {
       theOutput += theDecimal + theDecimalDigits;
   }

   thisControl.value = theOutput;
	if (thisControl.value == "0.00")
		thisControl.value = "";   
};
function FormatNumberYear(thisControl,nLength) {
	FormatNumber(thisControl,4);
}
function FormatNumberYearCount(thisControl,nLength) {
	FormatNumber(thisControl,2);
}
function FormatNumber(thisControl,nLength) {
	var strResult;
	var strInput;
	strInput = thisControl.value;
	strResult = "";
	for (var i = 0; i < strInput.length; ++i) {
		if ((strInput.charAt(i) == '0') ||
			(strInput.charAt(i) == '1') ||
			(strInput.charAt(i) == '2') ||
			(strInput.charAt(i) == '3') ||
			(strInput.charAt(i) == '4') ||
			(strInput.charAt(i) == '5') ||
			(strInput.charAt(i) == '6') ||
			(strInput.charAt(i) == '7') ||
			(strInput.charAt(i) == '8') ||
			(strInput.charAt(i) == '9') ||
			(strInput.charAt(i) == '.'))
		{	
			strResult = strResult + strInput.charAt(i);
			if (i >= nLength) return strResult;
		}
	}
	thisControl.value = strResult;
}
function FormatNumber(thisControl,nLength) {
	var strResult;
	var strInput;
	strInput = thisControl.value;
	strResult = "";
	for (var i = 0; i < strInput.length; ++i) {
		if ((strInput.charAt(i) == '0') ||
			(strInput.charAt(i) == '1') ||
			(strInput.charAt(i) == '2') ||
			(strInput.charAt(i) == '3') ||
			(strInput.charAt(i) == '4') ||
			(strInput.charAt(i) == '5') ||
			(strInput.charAt(i) == '6') ||
			(strInput.charAt(i) == '7') ||
			(strInput.charAt(i) == '8') ||
			(strInput.charAt(i) == '9') ||
			(strInput.charAt(i) == '.'))
		{	
			strResult = strResult + strInput.charAt(i);
			if (i >= nLength) return strResult;
		}
	}
	thisControl.value = strResult;
}
function FormatZip(thisControl) {
	var strResult;
	var strInput;
	var nLength;
	nLength = 5;
	strInput = thisControl.value;
	strResult = "";
	for (var i = 0; i < strInput.length; ++i) {
		if ((strInput.charAt(i) == '0') ||
			(strInput.charAt(i) == '1') ||
			(strInput.charAt(i) == '2') ||
			(strInput.charAt(i) == '3') ||
			(strInput.charAt(i) == '4') ||
			(strInput.charAt(i) == '5') ||
			(strInput.charAt(i) == '6') ||
			(strInput.charAt(i) == '7') ||
			(strInput.charAt(i) == '8') ||
			(strInput.charAt(i) == '9'))
		{	
			strResult = strResult + strInput.charAt(i);
			if (i >= nLength) return strResult;
		}
	}
	thisControl.value = strResult;
}

function UnformatNumber(val) {
	val = val.replace("$","");
	val = val.replace(",","");

	if (val == "") return 0;
	
	try {
		return parseFloat(val);
	} catch (e) {
		return 0;
	}
}

//  End -->
//</script>