var gsProductId

$(function()
{
	if (msOnDemandDocumentId == 29428)
	{

		//gsProductId = window.location.hash;
		//gsProductId.replace('#', '');

		gsProductId = msOnDemandContextId;
		
		document.body.setAttribute("promocode", '');
		
		if (gsProductId == '')
		{
			gsProductId = '12232';
		}	
		
		AddProductToCart('product-' + gsProductId, 1)
		
		ShowProductCart('');
		
		$('img.addtocart').click(function(event)
		{
			AddProductToCart(event.target.id);
		})

		$('#imgSubmitOrder').click(function(event)
		{
			SubmitOrder();
		})
	}

})

window.onbeforeunload = function() 
{
	
}

function AddProductToCart(sProduct, lQuantity)
{

	var aProduct = sProduct.split('-');
	var sReturn;
	var sParam;
	
	$("#divCart").html("Updating...");
			
	document.body.setAttribute("productid", aProduct[1])	
		
	sParam = 'method=SITE_ORDER_ADD_PRODUCT&site=' + msOnDemandSiteId + '&singleproduct=1&product=' + aProduct[1]

	if (lQuantity != undefined)
	{
		sParam = sParam + '&quantity=' + lQuantity
	}
		
	sReturn = onDemandSite(sParam);
	
	ShowProductCart();
	
}

function UpdateProductInCart(sProduct, lQuantity)
{

	var aProduct = sProduct.split('-');
	var sReturn;
	var sParam;
		
	sParam = 'method=SITE_ORDER_ADD_PRODUCT&site=' + msOnDemandSiteId + '&includetotal=1&product=' + aProduct[1]

	if (lQuantity != undefined)
	{
		sParam = sParam + '&quantity=' + lQuantity;
	}
		
	sParam = sParam + '&pricegrouptext=' + $('#inputPromoCode').val();
				
	sReturn = onDemandSite(sParam);

	if (sReturn.substring(0, 2) == 'OK') 
	{
      	var aValues = sReturn.split('|');

		$('#productPrice-' + aProduct[1]).text(aValues[3]);
		$('#orderTotalPrice').html('<h3><strong>$' + parseFloat(aValues[6]).toFixed(2) + '</strong></h3>');
	}
	
}

function RemoveProductFromCart(sProduct)
{

	var aProduct = sProduct.split('-');
	var sReturn = '';
	var sParam = '';

	sParam = 'method=SITE_ORDER_REMOVE_PRODUCT&site=' + msOnDemandSiteId + '&product=' + aProduct[1];
	sReturn = onDemandSite(sParam);

	CountItemsInCart();
	ShowProductCart();
}

function EmptyCart()
{
	var sReturn = '';
	var sParam = '';

	sParam = 'method=SITE_ORDER_REMOVE_PRODUCT&site=' + msOnDemandSiteId;
	sReturn = onDemandSite(sParam);
}

function ShowProductCart()
{

	var sParam;
	var oXML;
	var oRoot;
	var oRow;
	var aHTML = [];
	var h = -1;
	var cTotalPrice = 0;
	
	aHTML[++h] = '<table border="0" class="productTAB" cellspacing="0" cellpadding="0" width="820" style="margin-top:15px; margin-bottom:15px;">';
	aHTML[++h] = '<tbody>'
	
	sParam = 'method=SITE_ORDER_PRODUCT_SEARCH&cart=1&includeproducts=1&site=' + msOnDemandSiteId + '&includeproductdescription=1'
	sReturn = onDemandSiteXML(sParam);

	oXML = onDemandSiteXML(sParam);
	oRoot = oXML.getElementsByTagName("ondemand").item(0);

	if (oRoot.childNodes.length == 0)
	{
		aHTML[++h] = '<tr><td valign="top">No products in shopping cart</td></tr>';
		aHTML[++h] = '</tbody></table>';
		$("#divCart").html(aHTML.join(''));

		$("#cart").hide(1000)
		$("#divPersonalDetails").hide()
		$("#divPaymentDetails").hide()
		$("#divProducts").show(1000)

	}
	else
	{

		aHTML[++h] = '<tr>';
		aHTML[++h] = '<td width="10" valign="top"><img src="/site/1004/LHcnr.gif" alt="" width="10" height="30" /></td>';
		aHTML[++h] = '<td class="headTD" width="200">Product</td>';
		aHTML[++h] = '<td class="headTD" width="350">Description</td>';
		aHTML[++h] = '<td class="headTD" style="width: 30px;">Qty</td>';
		aHTML[++h] = '<td class="headTD" style="width: 75px;" align="right">GST</td>';
		aHTML[++h] = '<td class="headTD" style="width: 75px;" align="right">Total</td>';
		aHTML[++h] = '<td class="headTD" style="width: 70px;"></td>';
		aHTML[++h] = '<td width="10" valign="top"><img src="/site/1004/RHcnr.gif" alt="" width="10" height="30" /></td>';

		aHTML[++h] = '</tr>';

		for (var iRow = 0; iRow < oRoot.childNodes.length; iRow++) 
		{
			var oRow = oRoot.childNodes.item(iRow);
			
			if (onDemandXMLGetData(oRow, "totalprice") != '')
			{
				cTotalPrice = cTotalPrice + parseFloat(onDemandXMLGetData(oRow, "totalprice"));
			}

			document.body.setAttribute("productid", onDemandXMLGetData(oRow, "product"));
			
			aHTML[++h] = '<tr>';
			aHTML[++h] = '<td class="LHside"></td>';
			aHTML[++h] = '<td align="left" valign="top" class="contentTD">' + onDemandXMLGetData(oRow, "title") + '</td>';
			aHTML[++h] = '<td align="left" valign="top" class="contentTD">' + onDemandXMLGetData(oRow, "description") + '</td>';
			aHTML[++h] = '<td align="left" valign="top" class="contentTD">' +
						'<input id="productQuantity-' + onDemandXMLGetData(oRow, "product") + '" size="3"' +
						' type="TEXT" value="' + onDemandXMLGetData(oRow, "quantity") + '" class="productQuantity"></td>';

			aHTML[++h] = '<td align="right" valign="top" id="productGST-' + onDemandXMLGetData(oRow, "product") + '" class="contentTD">' + onDemandXMLGetData(oRow, "totalgst") + '</td>';
			aHTML[++h] = '<td align="right" valign="top" id="productPrice-' + onDemandXMLGetData(oRow, "product") + '" class="contentTD">' + onDemandXMLGetData(oRow, "totalprice") + '</td>';
		
			aHTML[++h] = '<td align="right" valign="top" class="contentTD"><a href="#" id="product-' + onDemandXMLGetData(oRow, "product") + '" class="removeproduct">Delete</a></td>';

			aHTML[++h] = '<td class="RHside"></td>';
			aHTML[++h] = '</tr>'
		
		}

    	aHTML[++h] = '<tr>';
		aHTML[++h] = '<td valign="top"><img src="/site/1197/LHside.gif" alt="" width="10" height="25" /></td>';
		aHTML[++h] = '<td align="right" colspan="4" class="botTD">Promo Code:</td>';
		aHTML[++h] = '<td align="right" class="botTD"><input type=text style="width:100px" id="inputPromoCode" value="' + 
							document.body.getAttribute("promocode") + '"></td>';
		aHTML[++h] = '<td align="right" class="botTD"><a href="#" class="updateorder" id="aUpdate">Update</a></td>';
		aHTML[++h] = '<td valign="top"><img src="/site/1197/RHside.gif" alt="" width="10" height="25" /></td>';
		aHTML[++h] = '</tr>';
    	aHTML[++h] = '<tr>';
		aHTML[++h] = '<td valign="top"><img src="/site/1197/LHcnrBot.gif" alt="" width="10" height="30" /></td>';
		aHTML[++h] = '<td align="right" colspan="4" class="botTD"><h3>Total Amount Due:</h3></td>';
		aHTML[++h] = '<td align="right" id="orderTotalPrice" class="botTD"><h3>$' + cTotalPrice.toFixed(2) + '</h3></td>';
		aHTML[++h] = '<td align="right" class="botTD"></td>';
		aHTML[++h] = '<td valign="top"><img src="/site/1197/RHcnrBot.gif" alt="" width="10" height="30" /></td>';
		aHTML[++h] = '</tr>';
		aHTML[++h] = '</tbody></table>';

		$("#divCart").html(aHTML.join(''));

		$('.removeproduct').click(function(event)
		{
			RemoveProductFromCart(event.target.id);
		})

		$('.productQuantity').change(function(event)
		{
			UpdateProductInCart(event.target.id, event.target.value);
		})

		$('#aUpdate').click(function(event)
		{
			UpdateProductCart();
		})
		
		$("#cart").show();
		$("#divPersonalDetails").show(1000);
		$("#divPaymentDetails").show(1000);
		$("#divProducts").hide()
	}
	
}

function UpdateProductCart()
{
	sParam = 'method=SITE_ORDER_PRODUCT_SEARCH&cart=1&includeproducts=1&site=' + msOnDemandSiteId + '&includeproductdescription=1'
	sReturn = onDemandSiteXML(sParam);

	oXML = onDemandSiteXML(sParam);
	oRoot = oXML.getElementsByTagName("ondemand").item(0);

	for (var iRow = 0; iRow < oRoot.childNodes.length; iRow++) 
	{
		var oRow = oRoot.childNodes.item(iRow);
		
		sParam = 'method=SITE_ORDER_ADD_PRODUCT&site=' + msOnDemandSiteId + '&includetotal=1&product=' + onDemandXMLGetData(oRow, "product") +
					'&quantity=' + onDemandXMLGetData(oRow, "quantity");

		sParam = sParam + '&pricegrouptext=' + $('#inputPromoCode').val();

		sReturn = onDemandSite(sParam);
		
		document.body.setAttribute("promocode", $('#inputPromoCode').val())	
		
		ShowProductCart();
		
	}		
}

function SubmitOrder()
{

	var sReturn;
	var sParam;
	var divLoading = document.getElementById('divLoading');
	var sInvoiceDocumentId = '29425';
	
	if (document.body.getAttribute("productid") == '12232')
	{
		sInvoiceDocumentId = '29425';
	}
	else
	{
		sInvoiceDocumentId = '29424';
	}
	
	if (OrderValidate())
	{	
		divLoading.innerHTML = 'Submitting your order please wait...';
		divLoading.setAttribute("class", "working bodytextblackbold");
		divLoading.style.display = 'block';
	
		sParam = 'method=SITE_ORDER_SUBMIT&site=' + msOnDemandSiteId +'&getaddress=1' +
			'&collectpaymentaccount=69' + 
			'&getaddress=1' +
			'&sendnews=1' + 
			'&collectpaymenttestamount=1.00' +
			'&ccname=' + $('#txtCreditCardName').val() +
			'&ccnumber=' + $('#txtCreditCardNumber').val() +
			'&ccexpirymonth=' + $('#selectCreditCardExpiryMonth').val() +
			'&ccexpiryyear=' + $('#selectCreditCardExpiryYear').val() +
			'&cccvn=' + $('#txtCreditCardCVV').val() +
			'&firstname=' + $('#txtFirstName').val() +
			'&surname=' + $('#txtSurname').val() +
			'&phone=' + $('#txtPhone').val() +
			'&mobile=' + $('#txtMobile').val() +
			'&email=' + $('#txtEmail').val() +
			'&streetaddress1=' + $('#txtAddress1').val() +
			'&streetsuburb=' + $('#txtSuburb').val() +
			'&streetstate=' + $('#txtState').val() +
			'&streetpostcode=' + $('#txtPostCode').val() +
			'&streetcountry=' + $('#txtCountry').val() +
			'&mailingaddress1=' + $('#txtAddress1').val() +
			'&mailingsuburb=' + $('#txtSuburb').val() +
			'&mailingstate=' + $('#txtState').val() +
			'&mailingpostcode=' + $('#txtPostCode').val() +
			'&mailingcountry=' + $('#txtCountry').val() +
			'&autoinvoice=1' + 
			'&autoreceipt=1' + 
			'&sendbrochuredocument=1' + 
			'&group=-1' + 
			'&invoicedocument=' + sInvoiceDocumentId + 
			'&invoicedescriptionproduct=' + document.body.getAttribute("productid") + 
			'&nosystemtemplate=1' +
			'&[ADDRESS1]=' + $('#txtAddress1').val() + 
			'&[SUBURB]=' + $('#txtSuburb').val() + 
			'&[STATE]=' + $('#txtState').val() + 
			'&[POSTCODE]=' + $('#txtPostCode').val() + 
			'&[COUNTRY]=' + $('#txtCountry').val() + 
			'&[PHONE]=' + $('#txtPhone').val() +
			'&[MOBILE]=' + $('#txtMobile').val() +
			'&[EMAIL]=' + $('#txtEmail').val() +
			'&[PROMOCODE]=' + $('#inputPromoCode').val() +
			'&salescontactpersonreference=' + $('#inputPromoCode').val();
					
		sReturn = onDemandSite(sParam);
				
		var aValues = sReturn.split('|');
		
		if (aValues[0] == "ER")
		{
			divLoading.style.display = 'none';
			window.alert('An error has occured processing your order.');
		}
		else
		{
				if (document.body.getAttribute("productid") == '12888' || document.body.getAttribute("productid") == '12846')
				{
					window.location.href = '/order_confirmation_workshop';
				}
				else if (document.body.getAttribute("productid") == '12649')
				{
					window.location.href = '/online_order_pillarsbook';

				}
				else if (document.body.getAttribute("productid") == '14154')
				{
					window.location.href = '/online_order_webinar';
				}				
				else
				{
					window.location.href = '/checkout_confirmation';
				}
			
		}	

	}
	else
	{
		window.alert('You can not submit the order as:' + sOnDemandValidateMessage);
	}
}


function OrderValidate() 
{

	sOnDemandValidateMessage = '';

	if ($('#txtFirstName').val() == '' || $('#txtSurname').val() == '' || $('#txtEmail').val() == '' || $('#txtPhone').val() == '' || $('#txtCountry').val() == '')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # You are missing some personal details.';
	}	
						
	if (document.getElementById('txtEmail').value != document.getElementById('txtEmail2').value) 
	{
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Confirmation email address does not match.';				
	}

	if (!onDemandIsEmail($('#txtEmail').val()))
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Not a valid email address.';
	}

	if ($('#deliveryaddress:checked').length != 0)
	{	

		if ($('#txtDeliveryFirstName').val() == '' || $('#txtDeliverySurname').val() == '' || $('#txtDeliveryEmail').val() == '')
		{	
			sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # You are missing some delivery details.';
		}

	}

	if ($('#txtCardHoldersName').val() == '' || $('#txtCardNumber').val() == '' || $('#mtxtCardExpiry').val() == '' || $('#ytxtCardExpiry').val() == '' || $('#txtCreditCardCVV').val() == '')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # You are missing some payment details.';
	}
	
	if (sOnDemandValidateMessage != '')
	{
		return (false);
	}
	else
	{
		return (true);
	}
}	

function CountItemsInCart()
{
	
	var oRoot;
	var sParam = 'method=SITE_ORDER_SEARCH&summary=1&site=' + msOnDemandSiteId;
	var sReturn = onDemandSite(sParam);

 	if (sReturn.substring(0, 2) == 'OK') 
	{
      	var aValues = sReturn.split('|');

		$('#click2cart').text('Cart: ' + aValues[3] + ' items');
	}

}

function ShowDeliveryAddress(bChecked)
{

	if (bChecked)
	{
		$("#divDeliveryDetails").slideDown(1000)
	}
	else
	{
		$("#divDeliveryDetails").slideUp(1000)
	}

}

function ShowPaymentDetails(bChecked)
{

	if (bChecked)
	{
		$("#divPaymentDetails").slideDown(1000)
	}
	else
	{
		$("#divPaymentDetails").slideUp(1000)
	}

}

