// --- Format dollar amounts ---
function showPrices(iPrices,iMonths,bAdBefore,iFields)
{ // where: iPrices = comma separated string of prices
  //                  (retail,on-line,setup)
  //        iMonths = billing months
  //        bAdBefore = true or false then advertised before
  //        iFields = "all" to update all calc fields
  //                = "grand" to update just your_grand only
  // result: Fills in values in document for ids of
  //         your_retail, your_discount, your_price, your_setup, your_grand

  var aDollars = iPrices.split(",");
  aDollars[0] = aDollars[0] * iMonths;
  aDollars[1] = aDollars[1] * iMonths;
  if (bAdBefore) aDollars[2] = "0";

  var blank12 = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  var wRetail = "$"+CommaFormatted(CurrencyFormatted(aDollars[0]));
  var wDiscount = "-$"+CommaFormatted(CurrencyFormatted(aDollars[0]-aDollars[1]));
  var wSetup = "$"+CommaFormatted(CurrencyFormatted(aDollars[2]));
  var wGrand = "$"+CommaFormatted(CurrencyFormatted(parseFloat(aDollars[1])+parseFloat(aDollars[2])));

  if (iFields == "all")
  {
    document.getElementById("your_retail").innerHTML=blank12.substr(0,6*(12-wRetail.length))+wRetail;
    document.getElementById("your_setup").innerHTML=blank12.substr(0,6*(12-wSetup.length))+wSetup;
    document.getElementById("your_discount").innerHTML=blank12.substr(0,6*(12-wDiscount.length))+wDiscount;
    document.getElementById("your_grand").innerHTML=blank12.substr(0,6*(12-wGrand.length))+wGrand;
    if (document.getElementById("hidden_grand_total"))
    {
//      document.getElementById("hidden_grand_total").value=wGrand;
      oForm.hidden_grand_total.value=wGrand;
    }
  }
  if (iFields == "grand")
  {
        document.getElementById("your_grand").innerHTML=wGrand;
  }
}

// --- Edit USA phone with area code (10 digits) or null ---
function validatePhoneUSA(str){
  var aParts = xtractNums(str);
  if ( (aParts) && (aParts.length > 0) ) 
  {
    jParts = aParts.join("");  // join as string
    if ( (jParts.length == 10) && (jParts.substr(0,1) != "0") )  
     jParts = "("+jParts.substr(0,3)+")"+jParts.substr(3,3)+"-"+jParts.substr(6,4);
    else
     jParts = null;
  }
  else
    jParts = null;
  return jParts;
}

function xtractNums(str){
    return str.match(/\d+/g);  // return numbers only
}

// --- Validate URL (after trim(data)) ---
function checkUrl(theUrl){
  if(theUrl.match(/(^(http|https?)\:\/\/)?\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%]\w+)*\/?$/i)){
    return true;  
  } else {
    return false;
  }
}

// --- Remove beginning and ending spaces in all text and textarea fields in form ---
function trimAllText(oForm)
{
  var aoFields = oForm.elements;
  for (var t=0; t<aoFields.length; t++)
  {
    var oField = aoFields[t];
    var sText  = ""

    var sType  = (oField.getAttribute("type")) ? oField.getAttribute("type").toLowerCase() : null;
    var sTName  = (oField.tagName) ? oField.tagName.toLowerCase() : null;

    if ( (oField.value) 
         && ( ((sType == "text") && (sTName == "input"))
              || (sTName == "textarea") ) )
      oField.value = trim(oField.value); // remove head/trail spaces
  }
  return sTName.value;
} 

// --- following trim functions from http://www.somacon.com/p355.php
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// ---- General Open New Window passing URL for window ----
function openNewWindow(iURL)
{
  new_win = window.open(iURL,"new_win","scrollbars=yes,toolbar=no,status=no,width=750,height=500");  
}


// --- inArray returns true if sValue is in the array aArray, else returns false ---
function inArray(sValue,aArray)
{
  for(var i=0;i<aArray.length;i++)
  {
    if (sValue == aArray[i]) return true;
  }
  
  return false;
}

//========================================================================
// FROM: http://willmaster.com/possibilities/archives/wmp20030218001.shtml
//   to use both: CommaFormatted(CurrencyFormatted(amount))
//========================================================================
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}
// end of function CommaFormatted()
//===================================================


