var data = new Array();

data['dfs'] = new Array();
data['dfs']['edu'] = {10 : 18.75, 25 : 18.75,100 : 18.75,250 : 18.75,500 : 18.75,1000 : 18.75,2000 : 18.75, 5000: 18.75};
data['dfs']['lib'] = {10 : 18.75, 25 : 18.75,100 : 18.75,250 : 18.75,500 : 18.75,1000 : 18.75,2000 : 18.75, 5000: 18.75};
data['dfs']['cor'] = {10 : 18.75, 25 : 18.75,100 : 18.75,250 : 18.75,500 : 18.75,1000 : 18.75,2000 : 18.75, 5000: 18.75};
data['dfe'] = new Array();
data['dfe']['edu'] = {10 : 18.75, 25 : 18.75,100 : 18.75,250 : 18.75,500 : 18.75,1000 : 18.75,2000 : 18.75, 5000: 18.75};
data['dfe']['lib'] = {10 : 18.75, 25 : 18.75,100 : 18.75,250 : 18.75,500 : 18.75,1000 : 18.75,2000 : 18.75, 5000: 18.75};
data['dfe']['cor'] = {10 : 18.75, 25 : 18.75,100 : 18.75,250 : 18.75,500 : 18.75,1000 : 18.75,2000 : 18.75, 5000: 18.75};
data['dfm'] = new Array();
data['dfm']['edu'] = {10 : 34.79, 25 : 29.59,100 : 23.66,250 : 20.11,500 : 17.10,1000 : 14.53,2000 : 12.35, 5000: 10.50};
data['dfm']['lib'] = {10 : 45.24, 25 : 38.45,100 : 30.76,250 : 26.15,500 : 22.22,1000 : 18.90,2000 : 16.06, 5000: 13.658};
data['dfm']['cor'] = {10 : 81.88, 25 : 69.60,100 : 55.68,250 : 47.33,500 : 40.23,1000 : 34.20,2000 : 29.07, 5000: 24.70};
data['dfl'] = new Array();
data['dfl']['edu'] = {10 : 20.16, 25 : 17.14,100 : 13.70,250 : 11.66,500 : 9.91,1000 : 8.42,2000 : 7.15, 5000: 6.08};
data['dfl']['lib'] = {10 : 26.20, 25 : 22.27,100 : 17.82,250 : 15.14,500 : 12.87,1000 : 10.95,2000 : 9.30, 5000: 7.91};
data['dfl']['cor'] = {10 : 47.43, 25 : 40.31,100 : 32.25,250 : 27.42,500 : 23.30,1000 : 19.90,2000 : 16.84, 5000: 14.31};


var maintenanceArr = new Array();
//Maintenance costs as a decimal
maintenanceArr['1'] = .2;
maintenanceArr['2'] = .35;
maintenanceArr['3'] = .5;


function update()
{
	var prod = document.getElementById("productSel");
	var cust = document.getElementById("customerSel");
	
	if(prod.selectedIndex > -1 && cust.selectedIndex > -1)
	{
		var sProd = prod.options[prod.selectedIndex].value;
		var sCust = cust.options[cust.selectedIndex].value;
		var units = document.getElementById("unitField").value;
		if(!isInteger(units) || units < 10)
		{
			document.getElementById("unitField").value = 10;
			units = 10;
		}
		
		// Get unit price
		if(units > 0)
		{
			var price = 0;
			var i = units;
			while(price <= 0 && i >= 0)
			{
				 if(typeof data[sProd][sCust][i] != "undefined")
				 {
				 	price = data[sProd][sCust][i];
				 }
				 i--;
			}
		}
		
		var prodTotal = price * units;
		
		// Get maintenance
		var mainTotal = 0.00;
		var main = document.getElementById("mainSel");
		if(main.selectedIndex > -1)
		{
			mainTotal = prodTotal * maintenanceArr[main.options[main.selectedIndex].value];
		}
		
	
		
		var subTotal = prodTotal + mainTotal;
		
		// Get GST
		var gst = 0.00;
		if(document.getElementById("gstCheck").checked)
		{
			gst = subTotal * .1;
		}
		
		var grandTotal = subTotal + gst;
		
		document.getElementById("priceField").innerHTML = formatCurrency(price);
		document.getElementById("prodField").innerHTML = formatCurrency(prodTotal);
		document.getElementById("mainField").innerHTML = formatCurrency(mainTotal);
		document.getElementById("subField").innerHTML = formatCurrency(subTotal);
		document.getElementById("gstField").innerHTML = formatCurrency(gst);
		document.getElementById("grandField").innerHTML = formatCurrency(grandTotal);
	}
	
	setDate();
}


function produceOrder()
{
	document.order.submit();
}

function loadOrder()
{
	var sProd = document.getElementById("productSel").value;
	var sCust = document.getElementById("customerSel").value;
	var units = document.getElementById("unitField").value;
	
	if(sProd != '' && sCust != '' && units >= 10)
	{
		// Get unit price
		var price = 0;
		var i = units;
		while(price <= 0 && i >= 0)
		{
			 if(typeof data[sProd][sCust][i] != "undefined")
			 {
				price = data[sProd][sCust][i];
			 }
			 i--;
		}
		
		var prodTotal = price * units;
		
		// Get maintenance
		var mainTotal = prodTotal * maintenanceArr[document.getElementById("mainSel").value];
		
	
		
		var subTotal = prodTotal + mainTotal;
		
		// Get GST
		var gst = 0.00;
		if(document.getElementById("gstCheck") != null)
		{
			gst = subTotal * .1;
			document.getElementById("gstField").innerHTML = formatCurrency(gst);
		}
		
		var grandTotal = subTotal + gst;
		
		document.getElementById("priceField").innerHTML = formatCurrency(price);
		document.getElementById("prodField").innerHTML = formatCurrency(prodTotal);
		document.getElementById("mainField").innerHTML = formatCurrency(mainTotal);
		document.getElementById("subField").innerHTML = formatCurrency(subTotal);
		document.getElementById("grandField").innerHTML = formatCurrency(grandTotal);
	}
	
	setDate();
}




function clearForm()
{
	document.getElementById("priceField").innerHTML = "$0.00";
	document.getElementById("prodField").innerHTML = "$0.00";
	document.getElementById("mainField").innerHTML = "$0.00";
	document.getElementById("subField").innerHTML = "$0.00";
	document.getElementById("gstField").innerHTML = "$0.00";
	document.getElementById("grandField").innerHTML = "$0.00";
	document.getElementById("unitField").value = "";
}

function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

function setDate()
{
	var today = new Date();
	document.getElementById("todayDate").innerHTML = today.toString();
	today.setDate(today.getDate()+14); 
	document.getElementById("quoteDate").innerHTML = today.toString();
}
