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;
}
  function addAllRecalcs () {
    jQuery('form[@id^=orderform-]').each(function(index){
       addRecalc(jQuery(this));
    })
  }
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
  function addRecalc(currForm) {
     $('input:checkbox:not([@name^=vw])',currForm).click(function() {
       var formID = $(this).parents('form').attr('id').substring(10)
       reCalc(formID); 
     })
     $('input:radio:not([@name^=vw])',currForm).click(function() {
       var formID = $(this).parents('form').attr('id').substring(10)
       reCalc(formID); 
     })
     $('select:not([@name^=vw])',currForm).change(function() {
       var formID = $(this).parents('form').attr('id').substring(10)
       reCalc(formID); 
     })
     $('input:text[@name^=vw-inscription]',currForm).keyup(function() {
       var formID = $(this).parents('form').attr('id').substring(10)
       reCalc(formID); 
     })
     reCalc($(currForm).attr('id').substring(10));
  }
  function getModifier(myval)
  {
     myval = myval.replace(',','').replace('$','');
	  var re = /\((\+|\-)(.*)\)/i;
	  if (myval.match(re)) {
	    if (myval.match(re).length > 2) {
		    var answer = myval.match(re)[2];
			 if (myval.match(re)[1] == '-') answer = '-' + answer;
			 }
		 else
		 {
		   var answer = 0;
		  }
	  }
	  else
	  { 
		  var answer = 0;
	  }  
     return answer;
  }
  
  function reCalc(id) {
     var diff=0;
     $('input:radio:checked','form#orderform-' + id).each(function(index){
        var myval = $(this).val();
        myval = rtrim(myval);
        var answer =  getModifier(myval);
        diff += answer * 1;
     });
     $('input:checkbox:checked','form#orderform-' + id).each(function(index){
        var myval = $(this).val();
        myval = rtrim(myval);
//        var re = /\(.*\)/i;
        var answer =  getModifier(myval);
        diff += answer * 1;
     });
     $('select option:selected','form#orderform-' + id).each(function(index){
        var myval = $(this).val();
        myval = rtrim(myval);
        var answer =  getModifier(myval);
        diff += answer * 1;
     });
     $('input:text[@name^=vw-inscription]','form#orderform-' + id).each(function(index){
        if ($(this).val() != '') diff += $(this).attr('rel') * 1;
     });
     
     if (!(isNaN(diff))) {
     diff = Math.round(diff * 100) / 100;
     var newPrice = Math.round(($('span#price-' + id).attr('rel') * 1 + diff) * 100) / 100;
     if ($('span#saleprice-' + id)) {
        var newPrice = Math.round(($('span#price-' + id).attr('rel') * 1 + diff * 2) * 100) / 100;
		  
        var newSalePrice = Math.round(($('span#saleprice-' + id).attr('rel') * 1 + diff) * 100) / 100;
        var result = newSalePrice.toFixed(2);
        result = CommaFormatted(result);
        $('span#saleprice-' + id).html('$' + result);
        var newSavings = Math.round((newPrice - newSalePrice) * 100 / newPrice);
        $('span#savings-' + id).html(newSavings.toString());
     }
     var result = newPrice.toFixed(2);
     result = CommaFormatted(result);
     $('span#price-' + id).html('$' + result);
    }
  }
