var PRICE_F = 8.0, PRICE_V = 12.0, PRICE_FV = 20.0, PRICE_W = 20.0;
var PRICE_ADD1 = 5.0, PRICE_ADD2 = 6.0, PRICE_ADD3 = 7.0;
function MarketBasket(index, ticks, nice, winter) {
	this.index = index;
	this.ticks = ticks;
	this.nice = nice;
	this.numFruit = 0;
	this.numVeggie = 0;
	this.numFruitVeggie = 0;
	this.numWinter = 0;
	this.numAddOn1 = 0;
	this.numAddOn2 = 0;
	this.numAddOn3 = 0;
	this.winter = winter;
	this.Save = function(orderID) {
		if ((this.numFruit + this.numVeggie + this.numFruitVeggie + this.numWinter) > 0) {
			var data = 'index=' + this.index;
			data += '&ticks=' + this.ticks;
			data += '&date=' + this.nice;
			data += '&numFruit=' + this.numFruit;
			data += '&numVeggie=' + this.numVeggie;
			data += '&numFruitVeggie=' + this.numFruitVeggie;
			data += '&numWinter=' + this.numWinter;
			data += '&vegetarian=' + ($('#chkWinterVeg' + this.index).attr('checked')? '1' : '0');
			data += '&numAddOn1=' + this.numAddOn1;
			data += '&numAddOn2=' + this.numAddOn2;
			data += '&numAddOn3=' + this.numAddOn3;
			data += '&orderID=' + orderID;
			
			$basketID = null;
			$.ajax({
				type: "POST",
				url: "SaveBasket.php",
				data: data,
				async: false,
				success: function(data){
					basketID = data;
				}
			});
		}
	};
	this.SubTotal = function() {
		var tot = this.numFruit * PRICE_F;
		tot += this.numVeggie * PRICE_V;
		tot += this.numFruitVeggie * PRICE_FV;
		tot += this.numWinter * PRICE_W;
		tot += this.numAddOn1 * PRICE_ADD1;
		tot += this.numAddOn2 * PRICE_ADD2;
		tot += this.numAddOn3 * PRICE_ADD3;
		return tot;
	};
	this.UpdateQuantity = function(type, inp) {
		var quant = $(inp).val();
		var re = /^\d+$/;
		if (!re.test(quant)) {
			alert('Invalid Quantity');
			$(inp).val(0);
			quant = 0;
		}
		switch (type) {
		case 'F':
			this.numFruit = quant;
			break;
		case 'V':
			this.numVeggie = quant;
			break;
		case 'FV':
			this.numFruitVeggie = quant;
			break;
		case 'A1':
			this.numAddOn1 = quant;
			break;
		case 'W':
			this.numWinter = quant;
			break;
		case 'A2':
			this.numAddOn2 = quant;
			break;
		case 'A3':
			this.numAddOn3 = quant;
			break;
		}
		if (this.numFruit + this.numVeggie + this.numFruitVeggie + this.numWinter > 0) {
			if ($('#hdr' + this.index + ' img.star').size() == 0) {
				$('#hdr' + this.index).append('<img class="star" src="images/star.png" height="20" width="20" title="Date Selected" />');
			}
			$('#txtQA1' + this.index).removeAttr('disabled');
			$('#txtQA2' + this.index).removeAttr('disabled');
			$('#txtQA3' + this.index).removeAttr('disabled');
		} else {
			$('#hdr' + this.index + ' img.star').remove();
			this.numAddOn1 = this.numAddOn2 = this.numAddOn3 = 0;
			$('#txtQA1' + this.index).val(0).attr('disabled', 'disabled');
			$('#txtQA2' + this.index).val(0).attr('disabled', 'disabled');
			$('#txtQA3' + this.index).val(0).attr('disabled', 'disabled');
		}
		$('#st' + this.index).html('Sub Total: $' + this.SubTotal().toFixed(2));
		su.GrandTotal();
	};
	this.Draw = function() {
		var html = '';
		html += '<h3><a href="#" id="hdr' + this.index + '">' + this.nice + '</a></h3>';
		html += '<div><table>';
		html += '<tr><th>Basket<th>Price Per<th>Quantity</tr>';

		if (this.winter) {	// in the winter, only 1 basket is offered
			// Winter Basket
			html += '<tr>';
			html += '<td>Winter Basket<br /><input type="checkbox" id="chkWinterVeg' + this.index + '" /> <label for="chkWinterVeg' + this.index + '">Vegetarian</label></td>';
			html += '<td class="right">$' + PRICE_W.toFixed(2) + '</td>';
			html += '<td class="right"><input class="right" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'W\',this);" id="txtQW'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numWinter + '" name="txtQW_' + this.ticks + '" /></td>';
			html += '</tr>';
		} else {
			// Fruit Basket
			html += '<tr>';
			html += '<td>Fruit Only</td>';
			html += '<td class="right">$' + PRICE_F.toFixed(2) + '</td>';
			html += '<td class="right"><input class="right" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'F\',this);" id="txtQF'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numFruit + '" name="txtQF_' + this.ticks + '" /></td>';
			html += '</tr>';
			// Veggie Basket
			html += '<tr>';
			html += '<td>Veggies Only</td>';
			html += '<td class="right">$' + PRICE_V.toFixed(2) + '</td>';
			html += '<td class="right"><input class="right" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'V\',this);" id="txtQV'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numVeggie + '" name="txtQV_' + this.ticks + '" /></td>';
			html += '</tr>';
			// Fruit & Veggie Basket
			html += '<tr>';
			html += '<td>Fruit &amp; Veggies</td>';
			html += '<td class="right">$' + PRICE_FV.toFixed(2) + '</td>';
			html += '<td class="right"><input class="right" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'FV\',this);" id="txtQFV'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numFruitVeggie + '" name="txtQFV_' + this.ticks + '" /></td>';
			html += '</tr>';
			// Separator
			html += '<tr><td colspan="3"><hr /></td</tr>';
			// ADD-ONS
			html += '<tr><th>Add-On<th>Price Per<th>Quantity</tr>';
			// Add-On 1
			html += '<tr>';
			html += '<td>$5 Add-On</td>';
			html += '<td class="right">$' + PRICE_ADD1.toFixed(2) + '</td>';
			html += '<td class="right"><input disabled class="right addon" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'A1\',this);" id="txtQA1'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numAddOn1 + '" name="txtQA1_' + this.ticks + '" /></td>';
			html += '</tr>';
			// Add-On 2
			html += '<tr>';
			html += '<td>$6 Add-On</td>';
			html += '<td class="right">$' + PRICE_ADD2.toFixed(2) + '</td>';
			html += '<td class="right"><input disabled class="right addon" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'A2\',this);" id="txtQA2'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numAddOn2 + '" name="txtQA2_' + this.ticks + '" /></td>';
			html += '</tr>';
			// Add-On 3
			html += '<tr>';
			html += '<td>$7 Add-On</td>';
			html += '<td class="right">$' + PRICE_ADD3.toFixed(2) + '</td>';
			html += '<td class="right"><input disabled class="right addon" type="text" onchange="su.arrMBs[\''
					+ this.index
					+ '\'].UpdateQuantity(\'A3\',this);" id="txtQA3'
					+ this.index
					+ '" size="4" maxlength="2" value="'
					+ this.numAddOn3 + '" name="txtQA3_' + this.ticks + '" /></td>';
			html += '</tr>';
		}
		// Separator
		html += '<tr><td colspan="3"><hr /></td</tr>';
		// Sub-Total
		html += '<tr><td colspan="3" class="right bold" id="st' + this.index + '">Sub Total: $0.00</td></tr>';
		html += '</table></div>';
		return html;
	};
	this.Review = function() {
		var html = '';
		if (this.winter) {
			if (this.numWinter > 0) {
				html = '<fieldset><legend>' + this.nice + '</legend>';
				html += 'Num Winter Baskets: ' + this.numWinter + '<br />';
				html += '</fieldset>';
			}
		} else {
			if ((this.numFruit + this.numFruitVeggie + this.numVeggie) > 0) {
				html = '<fieldset><legend>' + this.nice + '</legend>';
				html += 'Num Fruit Baskets: ' + this.numFruit + '<br />';
				html += 'Num Veggies Baskets: ' + this.numVeggie + '<br />';
				html += 'Num Fruit &amp; Veggies Baskets: ' + this.numFruitVeggie + '<br />';
				html += '$5 Add On: ' + this.numAddOn1 + '<br />';
				html += '$6 Add On: ' + this.numAddOn2 + '<br />';
				html += '$7 Add On: ' + this.numAddOn3 + '<br />';
				html += '</fieldset>';
			}
		}
		return html;
	};
}

function SignUp () {
	this.arrMBs = new Array();
	this.Draw = function() {
		var html = '';
		for (var i=0;i<this.arrMBs.length;i++) {
			html += this.arrMBs[i].Draw();
		}
		return html;
	};
	this.Save = function() {
		if (!verify()) return 0;
		var data = 'fn=' + $('#txtFN').val();
		data += '&ln=' + $('#txtLN').val();
		data += '&email=' + $('#txtEmail').val();
		data += '&phone=' + $('#txtPhone').val();
		data += '&pickup=' + $('#dropPULocation').val();
		
		var orderID = null;
		$.ajax({
			type: "POST",
			url: "SaveCustomer.php",
			data: data,
			async: false,
			success: function(data){
				orderID = data;
			}
		});
		for (var i=0;i<this.arrMBs.length;i++) {
			this.arrMBs[i].Save(orderID);
		}
		return orderID;
	};
	this.GrandTotal = function() {
		var gt = 0.0, numDays = 0;
		for ( var mb in this.arrMBs) {
			var st = this.arrMBs[mb].SubTotal(); 
			gt += st;
			if (st > 0.0) numDays++;
		}
		if (numDays >= 15) {
			gt *= .90;	// 10% discount
			$('#gt').html('Grand Total w/10% discount: $' + gt.toFixed(2));
		} else {
			$('#gt').html('Grand Total: $' + gt.toFixed(2));
		}
	};
	this.Review = function() {
		var html = '';
		for ( var mb in this.arrMBs) {
			html += this.arrMBs[mb].Review();
		}
		return html;
	};
}

function verify() {
	var errs = "";
	var msg = "The form was not submitted because of the following errors:";
	
	if (!$('#txtFN').val()) errs += "\n     First Name is required.";
	if (!$('#txtLN').val()) errs += "\n     Last Name is required.";
	if (!$('#txtEmail').val()) errs += "\n     Email is required.";
	if (!$('#txtPhone').val()) errs += "\n     Phone is required.";
	if (!$('#dropPULocation').val()) errs += "\n     Pick Up Location is required.";
	
	if (errs) {
		alert(msg + errs);
		return false;
	}
	return true;
}

function Callback(data) {
	alert('Your basket ID is ' + data);
}
