//function to format phone numbers to xxx-xxx-xxxx format as the user types
function formatPhone(evt, obj) {
  if(positiveIntInputOnly(evt)) {
    var k = -1;
        
    if (evt && evt.which) k = evt.which; // NS
    else if (window.event && window.event.keyCode) k = window.event.keyCode; // IE

    //don't add hyphens on non numeric keypresses (ie: backspace, del, etc)
    if(k > 47 && k < 58) {
      var exp = /-/g;
      var num = obj.value.replace(exp, ""); 
      exp = /\(/g;
      num = num.replace(exp, "");
      exp = /\)/g;
      num = num.replace(exp, "");
    
      if(num.length > 2) num = num.substring(0,3) + "-" + num.substring(3);
      if(num.length > 6) num = num.substring(0,7) + "-" + num.substring(7);
      
      obj.value = num;
    }
    
    return true;
  } else {
    return false;
  }
}

//Function to format a passed number into US currency
//Original:  Cyanide_7 (leo7278@hotmail.com) -->
//Web Site:  http://www7.ewebcity.com/cyanide7 -->
function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');

  if(isNaN(num))
    num = "0";
  
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();

  if(cents<10)
    cents = "0" + cents;

  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
  
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function positiveIntInputOnly(e) { // KEYPRESS event
// returns true if 0-9 or BS hit, or can't get key value; otherwise false

  var k = -1;

  if (e && e.which) k = e.which; // NS
  else if (window.event && window.event.keyCode) k = window.event.keyCode; // IE

  return (k > -1 ? ((k > 47 && k < 58) || k == 8) : true);

} // positiveIntInputOnly() 

//Function to update the price and shipping when a product is selected
function updatePrice(val) {
  var price, shipping;
  price = val.split(";")[0];
  shipping = val.split(";")[1];
  unit = val.split(";")[2];
  
  if(price && shipping && unit) {
    document.getElementById("lblPrice").innerHTML = "$" + price + "/" + unit;
    document.getElementById("lblShipping").innerHTML = "$" + shipping + "/" + unit;
  } else {
    document.getElementById("lblPrice").innerHTML = "";
    document.getElementById("lblShipping").innerHTML = "";
  }
}