function clrWin(id) {
var win2clr=document.getElementById(id);
win2clr.innerHTML="";
}




/**
* Formats the number according to the format string;
* adherses to the american number standard where a comma
* is inserted after every 3 digits.
*  note: there should be only 1 contiguous number in the format,
* where a number consists of digits, period, and commas
*        any other characters can be wrapped around this number, including $, %, or text
*        examples (123456.789):
*          0' - (123456) show only digits, no precision
*          0.00' - (123456.78) show only digits, 2 precision
*          0.0000' - (123456.7890) show only digits, 4 precision
*          0,000' - (123,456) show comma and digits, no precision
*          0,000.00' - (123,456.78) show comma and digits, 2 precision
*          0,0.00' - (123,456.78) shortcut method, show comma and digits, 2 precision
*
* @method format
* @param format {string} the way you would like to format this text
* @return {string} the formatted number
* @public
*/ 
/*
Number.prototype.format = function(format) {
  if (! isType(format, 'string')) {return '';} // sanity check
 
  var hasComma = -1 < format.indexOf(','),
    psplit = format.stripNonNumeric().split('.'),
    that = this;
 
  // compute precision
  if (1 < psplit.length) {
    // fix number precision
    that = that.toFixed(psplit[1].length);
  }
  // error: too many periods
  else if (2 < psplit.length) {
    throw('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
  }
  // remove precision
  else {
    that = that.toFixed(0);
  } 
 
  // get the string now that precision is correct
  var fnum = that.toString();
 
  // format has comma, then compute commas
  if (hasComma) {
    // remove precision for computation
    psplit = fnum.split('.');
 
    var cnum = psplit[0],
      parr = [],
      j = cnum.length,
      m = Math.floor(j / 3),
      n = cnum.length % 3 || 3; // n cannot be ZERO or causes infinite loop
 
    // break the number into chunks of 3 digits; first chunk may be less than 3
    for (var i = 0; i < j; i += n) {
      if (i != 0) {n = 3;}
      parr[parr.length] = cnum.substr(i, n);
      m -= 1;
    }
 
    // put chunks back together, separated by comma
    fnum = parr.join(',');
 
    // add the precision back in
    if (psplit[1]) {fnum += '.' + psplit[1];}
  } 
 
  // replace the number portion of the format with fnum
  return format.replace(/[\d,?\.?]+/, fnum);
}
*/



// This function removes non-numeric characters
function stripNonNumeric( str )
{
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for( var i = 0; i < str.length; i++ )
  {
    if( rgx.test( str.charAt(i) ) ){
      if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
             ( str.charAt(i) == '-' && out.length != 0 ) ) ){
        out += str.charAt(i);
      }
    }
  }
  return out;
}


function CalculateTotal(frm) {
    var order_total = parseFloat(frm.yar_price.value);
    var item_quantity = parseInt(frm.qty.value);
    var prid = frm.yar_price_id.value;
    var display_total = document.getElementById(prid);

    if (item_quantity >= 1) {
    for (var i=0; i < frm.elements.length; ++i) {
        var form_name = frm.elements[i].name;
        if (frm.elements[i].type == "select-one") {
			var res = frm.elements[i].options[frm.elements[i].selectedIndex].text;
			var words1 = res.split("($");
			if (words1.length > 1) {
			var words2 = words1[1].split(")");
			var word = words2[0];
			word = stripNonNumeric(word);
			var item_price = parseFloat(word);
			if (item_price != "") {
            order_total += item_price;
            } } }
        }
    order_total = item_quantity * order_total;
//alert("Testing: "+order_total);
	var bleh=round_decimals(order_total, 2);
    display_total.innerHTML = bleh;
    }
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString();
    var decimal_location = value_string.indexOf(".");
    if (decimal_location == -1) {
        decimal_part_length = 0;
        value_string += decimal_places > 0 ? "." : "";
    } else {
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    var pad_total = decimal_places - decimal_part_length;
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) {
            value_string += "0";
        } }
    return value_string;
}
