
var totalQty=0;
var totalProducts=0;
var totalMaintenance =0;
// add savings calcultaion.
function addprod(e, qty)
{
  document.forms["quote"].elements[e+"_qty"].value = qty;
  calculate();
}
function round5(x) 
{
  return (Math.round(Number(x)));
}

function submitQuote()
{
  calculate();
  document.getElementById("quote").submit();    
}

function discount(rate, qty)
{
  if(qty==2)
    rate -= rate * 0.34;
  else if(qty== 10 || qty == 20)
    rate -= rate * 0.33;
  return rate;
}

function discountPrice(base, qty) 
{
  sub = 0;
  for(var i = 1; i <= qty + 0; i++)
  {
    base = discount(base, i);
    sub += base;
  }
  return sub;
}

function display(test,id,subtotal, qty) 
{
    if(document.forms["quote"].elements[id+"_qty"])
	    document.forms["quote"].elements[id+"_qty"].value =  test ? qty : "" ;
    else
	    document.getElementById(id+"_qty").innerHTML =  test ? qty : "" ;
    document.getElementById(id+"_up").innerHTML = test ? "$"+addCommas(Math.floor((round5(subtotal)/qty))) : "";
    document.getElementById(id+"_sub").innerHTML = test ? "$"+addCommas(round5(subtotal)) : "";
}

function productCalc(id, base, qty) 
{
    var sub = discountPrice(base, qty);
    
    display( (qty > 0), id, sub, qty); 
    totalMaintenance += round5(discountPrice(Math.round(base*.18), qty));
    
    return round5(sub); 
}

function productNoDiscountCalc(id, base, qty) 
{
    var sub = base * qty;
    
    display( (qty > 0), id, sub, qty); 
    totalMaintenance += round5(discountPrice(Math.round(base*.18), qty));
    
    return round5(sub); 
}

function manualCalc(type,international)
{
  var subtotal=40;
  
  if(international)
	  subtotal+=20;
  if(type==1 && totalProducts>1)
	  subtotal+=(totalProducts-1)*15;
  else if(type==2 && totalQty>1)
	  subtotal+=(totalQty-1)*15;

  if(type != 0 && totalQty > 0)
  {
    document.getElementById("media_sub").innerHTML = "$" + addCommas(round5(subtotal));
  }
  else
  {
    document.getElementById("media_sub").innerHTML = "";
	  subtotal=0;
  }
	return subtotal;
}
function maintenanceCalc(id, subtotal, qty)
{
  var sub = (qty > 0) ? ( subtotal * qty) : 0; 
    
  display( qty>0 , id, sub, qty);
  return round5(sub); 
}


function bundleCalc(id, qty)
{
  
  var license = id.substr(1,1);
  // pro licenses are 80% of the bundle
  var pro_base = document.forms["quote"].elements['UPL'+id.substr(1,1)+'_base'].value;
  var anl_base = document.forms["quote"].elements['UAL'+id.substr(1,1)+'_base'].value;
  var sub =  discountPrice(Number(pro_base), (qty * Number(id.substr(2,2))  * 0.8));
  // ada licenses are 20% of the bundle
  sub += discountPrice(Number(anl_base), (qty * Number(id.substr(2,2))  * 0.2));
  // 10% savings
  sub *= 0.9;
  // with scripting 
  if(id.substring(id.length -1) == 'S')
    sub += 2000;
  
  display( (qty > 0), id, sub, qty );
  if(qty <1)
    sub=0;
  return sub;
}

function calculate(e){


    max =Number(document.forms["quote"].Max.value);
    var quote=document.forms["quote"].elements;
    var total=0;
    var retail=0;
    totalQty=0;
    totalProducts=0;
    totalMaintenance=0;
    var at="@";
    var domain="scitools";
    if(e && e.value > max){
	alert("For orders more than "+max+" licenses contact our sales department.\n sales"+at+domain+".com");
}
    var rows= document.getElementById('productTable').rows;
    var row;
    for(row in rows)
    {
      if(rows[row].id && quote[rows[row].id+"_base"])
      {
        if(quote[rows[row].id+"_qty"].value > max)
	        quote[rows[row].id+"_qty"].value = max;
        var qty  = Number(quote[rows[row].id+"_qty"].value);
        var base = Number(quote[rows[row].id+"_base"].value)
        
        totalQty += qty;
        if(qty > 0)
	        totalProducts++;
	         
	      if(rows[row].id.substr(0,1) == 'B')
	        total += bundleCalc(rows[row].id, qty);
          else if (rows[row].id.substr(0,4) == 'UMT-')
            total += productNoDiscountCalc(rows[row].id, base, qty);
	      else
          total += productCalc(rows[row].id, base, qty);
        retail += qty * base;
      }
    }
 
    if(quote.media)
    {
      var sub =manualCalc(quote.media.selectedIndex, quote['international'].checked);
      total += sub;
      retail += sub;
    }
    
    if(quote.MT_qty)
    {
      var qty = quote["MT_qty"].value < 4 ? quote["MT_qty"].value : 4; 
      sub = maintenanceCalc("MT", totalMaintenance, qty);
      total += sub;
      retail += sub;
    }
    var savedHTML = retail-total > 0 ? addCommas(round5(retail-total)) : 0;
    document.getElementById("total").innerHTML="$"+ addCommas(round5(total)); 
    document.getElementById("saved").innerHTML="$"+ savedHTML; 
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


