$(document).ready(function() {
	$("input[type='text']").blur(function(){
		checkInputs(this);
	});

	$("#Comments").blur(function(){
		checkInputs(this);
	});

	$(".price").attr('readonly','true');
	$(".qty").attr('readonly','true');

	$(".orderForm input[type='text']").not(".price").not(".qty").blur(function(){
		updateTotals(this);
	});

	$('#SubmitOrder').click(function(e){
		e.preventDefault();
	});
	
});



function checkInputs(ob)
{
	$("#ContactErrorMsgs").empty();
	if(ob.value == '')
		return;

	if(ob.name == 'PostalCode')
	{
		if(ob.value.length < 5)
			$("#ContactErrorMsgs").empty().append('Not enough digits.');		
		else if(ob.value.length == 5)
		{
			if(isNaN(ob.value))
				$("#ContactErrorMsgs").empty().append('Zip Codes contain 5 numbers.');
		}
		else if(ob.value.length == 6 || ob.value.length == 7)
		{
			if(!/[a-zA-z][0-9][a-zA-Z][ ]?[0-9][a-zA-Z][0-9]/.test(ob.value))
				$("#ContactErrorMsgs").empty().append('Invalid Postal Code, should be in the format L#L #L#');
		}
		else
			$("#ContactErrorMsgs").empty().append('Invalid Postal Code or Zip Code.<br>');
	}

	if(ob.name == 'Phone')
	{
		if(ob.value.length < 8)
			$("#ContactErrorMsgs").empty().append('Not enough digits.');
		else if(!/[(]?[0-9]{3}[-.)][ ]?[0-9]{3}[-.][0-9]{4}/.test(ob.value))
			$("#ContactErrorMsgs").empty().append('Invalid format - ###.###.####');
	}
	
	if(ob.name == 'Email')
	{
		if(!/[a-zA-Z0-9]{1,80}[@][a-zA-Z0-9]{1,30}\.[a-zA-Z]{2,3}/.test(ob.value))
			$("#ContactErrorMsgs").empty().append('Invalid Email format - username@domain.tld');
	}
}

function submitOrder()
{


	data = new Object();
	inputs = $('.ContactInfo input,textarea');
	
	for(i=0;i<inputs.length;i++)
	{
		//check for empty fields
		if(inputs[i].value == '' && inputs[i].name != 'Address2' && inputs[i].name != 'OrderDescription')
		{
			$('#OrderErrorMsgs').empty().append('All Fields need to be filled out');
			return;
		}
		data[inputs[i].name] = inputs[i].value;	
	}

	inputs = $('#OrderForm input,textarea');
	
	for(i=0;i<inputs.length;i++)
	{
		data[inputs[i].name] = inputs[i].value;	
	}

	data['order'] = 'true';

	$('#SubmitOrder').attr('disabled','true');
	$.post('submitOrder.php', data, function(xml) {
		$('#OrderErrorMsgs').empty().append(xml);
		//if(xml != 'Your order has been submitted, a sales representative will contact you within 24/48 business hours.  Thank you for your order!')
		$('#SubmitOrder').removeAttr('disabled');
	});


}

function updateTotals(ob)
{
	
	if(ob.value != "" && isNaN(ob.value))
	{
		ob.value = '';
		alert('Must be numeric');
		return;
	}

	qty = 0;
	id = ob.parentNode.parentNode.id;
	for(i=0;i<7;i++)
	{
		a = parseInt($("#" + id + " :text:eq(" + i + ")").val());
		if(!isNaN(a))
			qty += a;
	}
	$("#" + id + " :text:eq(" + 7 + ")").val(qty);

	unitPrice = lookupUnitPrice(id,qty).toFixed(2);
	
	$("#" + id + " :text:eq(" + 8 + ")").val(unitPrice);

	$("#" + id + " :text:eq(" + 9 + ")").val((qty*unitPrice).toFixed(2));

	var subTotal = 0;
	var totalQty = 0;
	for(i=0;i<34;i++)
	{
		a = parseFloat($(".price:odd:eq(" + i + ")").val());
		if(!isNaN(a))
			subTotal += a;
		a = parseInt($(".qty:eq(" + i + ")").val());
		if(!isNaN(a))
			totalQty += a;
	}

	$("[name='totalQty']").val(totalQty);
	$("[name='subTotal']").val(subTotal.toFixed(2));
	$("[name='HST']").val((subTotal * 0.13).toFixed(2));
	$("[name='PST']").val((subTotal * 0.08).toFixed(2));
	$("[name='total']").val((subTotal * 1.13).toFixed(2));
	
}

function lookupUnitPrice(id,qty)
{
	if(qty == 0)
		return 0;

	list = priceList[id];
	i = 0;

	for(i=0;i<list.length;i++)
	{
		if(list[i][0]>=qty)
		{
			if(i==0)
				return list[0][1];
			else
				return list[i-1][1];
		}
	}

	return list[i-1][1];
}
