var updateTotals = true;

function LoadOrderPage()
{
	SetObjectEventHandler( document, kKeyDown, QtyKeyFilter );
}

function SubmitForm( action )
{
	document.forms[0].elements["action"].value = action;
	document.forms[0].submit();
}

function ChangeCategory( theRow )
{
	var menu = document.forms[0].elements["categ" + theRow];
	productList[theRow-1][0] = parseInt( menu.options[menu.selectedIndex].value );
	productList[theRow-1][1] = 0;
	LoadProducts( theRow );
}

function ChangeProduct( theRow )
{
	var menu = document.forms[0].elements["prod" + theRow];
	productList[theRow-1][1] = parseInt( menu.options[menu.selectedIndex].value );
	LoadPrice( theRow );
}

function LoadProducts( theRow, update )
{
	var menu = document.forms[0].elements["prod" + theRow];
	menu.options.length = 0;
	
	var categ = productList[theRow-1][0];
	
	if ( categ < 0 ) {
		menu.options[0] = new Option( "Please select a category first" );
		menu.disabled = true;
		ClearPrice( theRow );
		UpdateTotal();
		
	} else {
		productItems = products[categ][1];
		
		for ( var x = 0; x < productItems.length; x++ ) {
			menu.options[x] = new Option( productItems[x][0], x );
		}
		
		menu.disabled = false;
		
		if ( update != false ) ChangeProduct( theRow );
	}
}

function LoadPrice( theRow )
{
	var categ = productList[theRow-1][0];
	if ( categ >= 0 ) {
		var prod = productList[theRow-1][1];
		WriteIntoLayer( "unit" + theRow, ToMoney( products[categ][1][prod][1] ) );
	}
	
	UpdatePrice( theRow );
}

function UpdatePrice( theRow )
{
	categ = productList[theRow-1][0];
	if ( categ >= 0 ) {
		prod = productList[theRow-1][1];
		qty = productList[theRow-1][2] = parseInt( document.forms[0].elements["qty" + theRow].value, 10 );
		if ( isNaN( qty ) ) qty = productList[theRow-1][2] = 0;
		document.forms[0].elements["qty" + theRow].value = qty;
		price = ToMoney( (products[categ][1][prod][1]) * qty );
		WriteIntoLayer( "price" + theRow, price );
	} else ClearPrice( theRow );
	
	UpdateTotal();
}

function UpdateTotal()
{
	if ( !updateTotals ) return;
	
	var subtotal = GetTotal();
	
	WriteIntoLayer( "subtotal", ToMoney( subtotal ) );
	WriteIntoLayer( "gst", ToMoney( subtotal * 0.07 ) );
	WriteIntoLayer( "total", "<b>" + ToMoney( subtotal * 1.07 ) + "</b>" );
}

function ClearPrice( theRow )
{
	productList[theRow-1][0] = -1;
	productList[theRow-1][1] = 0;
	document.forms[0].elements["qty" + theRow].value = productList[theRow-1][2] = 0;
	price = ToMoney( 0 );
	WriteIntoLayer( "unit" + theRow, price );
	WriteIntoLayer( "price" + theRow, price );
}

function QtyKeyFilter( evt )
{
	theKey = parseInt( IsDefined( evt ) ? evt.which : event.keyCode );
	theObject = (IsDefined( evt ) ? evt.target : (IsDefined( event.srcElement ) ? event.srcElement : event.target));
	
	if ( IsDefined( theObject ) && String( theObject.name ).substr( 0, 3 ) == "qty" ) {
		if ( theKey == 3 || theKey == 13 ) {
			theRow = parseInt( String( theObject.name ).substr( 3 ) );
			UpdatePrice( theRow );
			theObject.blur();
			return false;
		}
	}
	
	return true;
}

function GetTotal()
{
	var total = 0;
	
	for ( var x = 0; x < productList.length; x++ ) {
		categ = productList[x][0];
		if ( categ >= 0 ) {
			qty = productList[x][2]
			if ( qty > 0 ) {
				total += products[categ][1][productList[x][1]][1] * qty;
			}
		}
	}
	
	return total;
}

function ToMoney( num )
{
	var price = String( Math.round( num * 100 ) );
	
	if ( price == "0" ) return "$0.00";
	
	var integer = price.slice( 0, -2 );
	var decimal = price.substr( price.length - 2, 2 );
	
	if ( integer.length > 3 ) {
		var newInt = "";
		var length = integer.length;
		var l = parseInt( (length - 0.1) / 3 );
		for ( var x = 1; x <= l; x++ ) {
			newInt = "," + integer.substr( length-(x*3), 3 ) + newInt;
		}
		integer = integer.substr( 0, length-(l*3) ) + newInt;
	}
	
	if ( price.length == 1 ) {
		decimal = "0" + decimal;
		integer = "0";
		
	} else if ( price.length == 2 ) {
		integer = "0";
	}
	
	return "$" + integer + "." + decimal;
}