var gLastCategory;
var gIndex = 0;

function buildForm()
{
	//console.log('WIDTH: ' + window.innerWidth);
	var html = '<form method="POST" name="mainForm" action="submit.html">';

	for (i = 0; i < gParts.length; ++i, ++gIndex) {
		html += buildRow(gParts[i]);
	}
	if (gIndex % 2 == 1) {
		html += '<td bgcolor=wheat width=50%>&nbsp;</td>';
	}
	html += '</table>';

	for (i = 0; i < gPersonalInfoSubform.length; ++i) {
		html += gPersonalInfoSubform[i];
	}

	html += '<p align=center><input type=submit value="Place Order" ' +
		'onClick="return VerifyRequireds(document.mainForm)"></p>';
	html += '<input type="hidden" name="success" ' +
		'value="http://tangentsoft.net/shop/success.html">';

	html += '</form>';

	$('orderform').innerHTML = html;

	calc();
}


function buildRow(part)
{
	var onecol = window.innerWidth < 790;
	var html = '';
	if (part.category != gLastCategory) {
		// Close off previous table, if any
		if (gLastCategory) {
			if ((gIndex % 2 == 1) && !onecol) {
				html += '<td bgcolor=wheat width=50%>&nbsp;</td>';
			}
			html += '</table><br>';
		}

		// Start new table
		html += '<table cellpadding=5 cellspacing=2 border=0 width=100% ' +
				'bgcolor=tan>';

		// Create title row in new table
		html += '<tr><td ' + (onecol ? '' : 'colspan=2') + 
				' class=shophead><p>' + part.category + '</p></td></tr>';
		gLastCategory = part.category;
		gIndex = 0;
	}

	// Build the next part box
	if (onecol || (gIndex % 2 === 0)) {
  	html += '<tr>';
  }
	html += '<td width=50% bgcolor=wheat valign=top>';
	html += '<table cellpadding=5 cellspacing=0 border=0 width=100%>';
	html += '<tr><td colspan=2 valign=bottom><span class=fauxh5>';
	if (part.url) {
  	html += '<a href="' + part.url + '">';
  }
	html += part.item;
	if (part.url) {
  	html += '</a>';
  }
	html += '</span>';
	html += '<br><span class=fauxh6>';
	if (part.restock) {
		html += '<b><font color=red>OUT OF STOCK</font></b> until ' + part.restock;
	}
	else {
		if (part.newflag == 'Yes') {
		html += '<font color=red>NEW!</font>&nbsp;';
		}
		if (part.subtitle) {
			html += part.subtitle;
		}
	}
	html += '</span>';
	html += '</td>';

	html += '<td rowspan=3 width=1% valign=top>';
	html += '<img src="/bitmaps/dot-clear.gif" width=10 height=1 alt="">';
	html += '</td>';

	var jpg = part.id.toLowerCase() + '.jpg';
	html += '<td rowspan=3 width=130 valign=top>';
	html += '<a href="bitmaps/large/' + jpg + '" rel="lightbox">' +
			'<img width="120" height="120" alt="' + part.item +
			' picture, small" src="bitmaps/small/' + jpg +
			'" class="bordered" border="0"></a>';
	html += '</td></tr>';
	html += '<tr><td colspan=2 valign=top>' + part.desc + '</td></tr>';
	html += '<tr>';
	html += '<td><nobr><font size=-1><b>price:</b>&nbsp;' +
			formatPrice(part.price) + '</font></nobr></td>';
	html += '<td align=right><nobr><font size=-1><b>quantity:</b>' +
			'&nbsp;<input name="' + part.id + '" id="' + part.id +
			'" size=3 value="0" onChange="calc()"></font></nobr></td>';
	html += '</tr>';
	html += '</table></td>';

	if (onecol || (gIndex % 2 == 1)) {
  	html += '</tr>';
  }

	return html;
}


function calc()
{
	var form = document.mainForm;

	setFieldStates(form);

	var subTotal = calcSubTotal();
	form.subtotal.value = formatPrice(subTotal);
	var discount = calcDiscount(form, subTotal);
	form.discount.value = formatPrice(discount);
	var orderSurcharge = calcOrderSurcharge(subTotal);
	form.order_surcharge.value = formatPrice(orderSurcharge);

	form.gtotal.value = formatPrice(subTotal - discount + orderSurcharge);
}


function calcDiscount(form, subTotal)
{
	if (subTotal >= 500) {
		return subTotal * 0.15;
	}
	else if (subTotal >= 250) {
		return subTotal * 0.10;
	}
	else if (subTotal >= 100) {
		return subTotal * 0.05;
	}
	else {
		return Number(0);
	}
}


function calcOrderSurcharge(subTotal)
{
	if (subTotal < 10) {
		return Number(2);
	}
	else {
		return Number(0);
	}
}


function calcSubTotal()
{
	var total = 0;
	for (i = 0; i < gParts.length; ++i) {
		total += $(gParts[i].id).value * gParts[i].price;
	}
	return total;
}


function findSelectedValue(radio)
{
	for (i = 0; i < radio.length; ++i) {
		if (radio[i].checked) {
			return radio[i].value;
		}
	}

	return null;
}


function formatPrice(numberString)
{
	var noDecimal = /^[0-9]+$/;
	var tenthsOnly = /^[0-9]+\.[0-9]$/;
	var value = String(Math.floor(numberString * 100) / 100);

	if (value.match(noDecimal)) {
		return '$' + value + '.00';
	}
	else if (value.match(tenthsOnly)) {
		return '$' + value + '0';
	}
	else {
		return '$' + value;
	}
}


function price(field) 
{
	var expr = 'prices["' + field + '"] * document.mainForm.' +
 			field + '.value;';
	return eval(expr);
}


function setFieldState(field, dis)
{
	field.disabled = dis;

	if (dis && (field.value === "")) {
		field.value = "not applicable";
	}
	else if (!dis && (field.value == "not applicable")) {
		field.value = "";
	}
}


function setFieldStates(form)
{
	var dis = (findSelectedValue(form.payment) == 'Paypal');
	
	setFieldState(form.street, dis);
	setFieldState(form.city, dis);
	setFieldState(form.state, dis);
	setFieldState(form.paypal_email, !dis);
}


