// Declare object classes first
function shopitem(controlName, name, desc, p, q) {
	this.controlName = controlName;
	this.name = name;
	this.desc = desc;
	this.p = p;
	this.q = q;
}
shopitem.prototype.subtotal = function () { return (this.p * this.q); }

shopitem.prototype.setQ = function(q) {
// 	alert("q from setQ: " + q);
	this.q = q;
	if (this.controlName == "indiv10") {
		if (this.q < 200 ) 				this.p = 2.00;
		else if (this.q >=200 && this.q <= 499) 	this.p = 1.75;
		else if (this.q >= 500) 			this.p = 1.25;
	} else if (this.controlName == "indiv11") {
		if (this.q < 200 ) 				this.p = 2.25;
		else if (q >=200 && q <= 499) 	this.p = 2.00;
		else if (q >= 500) 				this.p = 1.50;
	} else if (this.controlName == "checkvalve") {
		if (q >= 100) {
			this.p = 5.50;
		} else {
			this.p = 6.00;
		}
	}
}

function shoppingCart() {
	//this.ctl = new Array("indiv10","indiv11","bulk10","bulk11","checkvalve","basichome","cflow","probasic","proult","kneerest","pad");
	this.ctl = new Array("indiv10","indiv11","bulk10","bulk11","checkvalve","basichome","cflow","kneerest","pad");
	this.indiv10 	= new shopitem(this.ctl[0],"10\" Individual Tips", "10\" tip, individually wrapped and labeled.", 2.00, 0);
 	this.indiv11 	= new shopitem(this.ctl[1], "11\" Individual Tips", "11\" tip, individually wrapped and labeled.", 2.25, 0);
 	this.bulk10 	= new shopitem(this.ctl[2], "100 10\" Tips", "10\" tips, bulk packaged in lots of 100.", 125.00, 0);
 	this.bulk11 	= new shopitem(this.ctl[3], "100 11\" Tips", "11\" tips, bulk packaged in lots of 100.", 150.00, 0);
 	this.checkvalve = new shopitem(this.ctl[4], "Check Valves", "", 6.00, 0);
 	this.basichome 	= new shopitem(this.ctl[5], "Basic Home Unit", "Includes board, tubing, new 5 gal. molded bucket, 2 nozzles.", 265.00, 0);
 	this.cflow 		= new shopitem(this.ctl[6], "Continuous Flow Home Unit", "Includes board, tubing, 2 nozzles, Continuous Flow pail, water filter mixing valve, molded infusion pail.", 850.00, 0);
	this.kneerest	= new shopitem(this.ctl[7], "Knee Rest","For use with Colenz Professional System.", 159.00, 0);
	this.pad		= new shopitem(this.ctl[8], "Upholstered Pad and Pillow", "For use with Colenz Professional System.", 79.00, 0);
}

shoppingCart.prototype.total = function () { 
	var total = 0;
	for (prop in this.ctl) {
		//alert(this.ctl[prop]);
		total = total + this[this.ctl[prop]].subtotal();
	}
	return total + this.calculateShipping();
}

shoppingCart.prototype.calculateShipping = function() {
	var baseTips = 6.50;
	var incTips = 3.00;
	
	var baseBoard = 28.00;
	var incBoard = 0.00;
	
	var basePro = 650.00;
	
	var baseKneerest = 15.00;
	var basePad = 9.00;
	
	var ctl = new Array("indiv10","indiv11","bulk10","bulk11","checkvalve");
	var qTot = 0;
	
	for (var i=0; i < ctl.length; i++) {
		var item = this[ctl[i]]
		if (item.controlName == "bulk10" || item.controlName == "bulk11") {
			qTot = (qTot + (parseInt(item.q, 10)*100))
		} else {
			qTot = (qTot + parseInt(item.q, 10));
		}
	}
	
	// Calculate tips shipping amt
	multiplier = Math.floor((qTot-1)/100);
	var shippingTips = 0;
	if (qTot > 0) shippingTips = (baseTips + (multiplier * incTips));
	
	// Calculate amount for boards
	var qBoards = this["basichome"].q + this["cflow"].q;
	var shippingBoards = qBoards * baseBoard;
	
	// Calculate amount for pro boards
	//var qPro = this["probasic"].q + this["proult"].q;
	//var shippingPro = qPro * basePro;

	// Calculate amount for pad and kneerests
	var qKneerest = this["kneerest"].q;
	var qPad = this["pad"].q;
	var shippingPads = (qKneerest * baseKneerest) + (qPad * basePad);
	
	//return (shippingTips + shippingBoards + shippingPro + shippingPads) * 100/100;
	return (shippingTips + shippingBoards + shippingPads) * 100/100;
}

// end declaration of classes and methods


var sc = new shoppingCart();


function computeTotal() {
	// loop through the inputs and calculate totals
	for (var i=0; i < sc.ctl.length; i++) {
		
		var q = document.getElementById(sc.ctl[i]+"q");
		
		// if the user blanks a field, make it 0
		if (q.value == "" || q.value == null) q.value = 0;
		// check if the value is a number
		if ( isNaN(parseInt(q.value, 10)) ) {
			alert("Please type a number for the quantity.");
			q.value = 0;
			return;
		}
		// set the quantities (thereby figuring out if bulk discounts are in order
		sc[sc.ctl[i]].setQ(parseInt(q.value, 10));
		// write out the new unit cost and sub total
		replaceText(document.getElementById(sc.ctl[i] + "t"), "$" + sc[sc.ctl[i]].subtotal().toFixed(2));
		replaceText(document.getElementById(sc.ctl[i] + "uc"), "$" + sc[sc.ctl[i]].p.toFixed(2));
	}
	
	// compute the total
	var shipTips = sc.calculateShipping();
	var gt = sc.total();

	// write out values to controls
	replaceText(document.getElementById("shipamt"), "$" + shipTips.toFixed(2));
	replaceText(document.getElementById("total"), "$" + gt.toFixed(2));
}

function validateForm() {
	//clear out the old hidden fields in the googleinputs div
	f = document.getElementById("googleinputs");
	while (f.hasChildNodes()) {
		f.removeChild(f.firstChild);
	}
	
	var itemNum = 1;
	var qTotal = 0;
	for (var i=0; i < sc.ctl.length; i++) {
		// only process ones that are not zero quantity
		var item = sc[sc.ctl[i]];
		//alert("item q: " + item.q);
		if (item.q != 0) {
			qTotal = qTotal + item.q;
		
			var amt = item.p;
			
			f.appendChild(newGoogleInput("item_name_" + itemNum, item.name));
			f.appendChild(newGoogleInput("item_description_" + itemNum, item.desc));
			f.appendChild(newGoogleInput("item_quantity_" + itemNum, item.q));
			f.appendChild(newGoogleInput("item_price_" + itemNum, item.p));
			itemNum++;
		}
	}
	
	// write out the shipping tags
	f.appendChild(newGoogleInput("ship_method_name_1", "DHL"));
	f.appendChild(newGoogleInput("ship_method_price_1", sc.calculateShipping()));
	
	// post the cart
	if (qTotal > 0) {
		return true;
	} else {
		alert("Please select something to buy before checking out");
		return false;
	}
}

function newGoogleInput(name, value) {
	var input = document.createElement("input");
	input.setAttribute("type", "hidden");
	input.setAttribute("name", name);
	input.setAttribute("value", value);
	return input;
}

