var grand_total;
var theheight = 0;
var thewidth = 0;
var payment_type;
var section = "application";
var cart = new Array();
var xtra_html = "";


function do_validate(){
	tmp = validate();
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {

			return radioObj[i].value;
		}
	}
	return "";
}

function get_data(){
	var theForm = document.getElementById("checkout_form")
	// user data
	data = "company||" + theForm["company"].value;
	data = data + "---first_name||" + theForm["first_name"].value;
	data = data + "---last_name||" + theForm["last_name"].value;
	data = data + "---address||" + theForm["address"].value;
	data = data + "---address_2||" + theForm["address_2"].value;
	data = data + "---suburb||" + theForm["suburb"].value;
	data = data + "---state||" + theForm["state"].value;
	data = data + "---post_code||" + theForm["post_code"].value;
	data = data + "---email||" + theForm["email"].value;
	data = data + "---telephone||" + theForm["telephone"].value;
	// product data
	data = data + "-----"
	
	for(i=0;i<cart.length;i++){
		cart[i][3] = cart[i][3].replace(/&/g, "---");
		cart[i][3] = cart[i][3].replace(/=/g, "||");
		data = data + "qty||" + cart[i][0];
		data = data + "---item||" + cart[i][3];
		data = data + "---price||" + cart[i][2];
		data = data + "|-|-|";
	}
	// price_data
	data = data + "-----total||" + grand_total;
	data = data + "---registered_post||" + registered_post
	
	// add a time stamp
	//data = data + 
	return data;
}

function get_window_dimensions(){
	if (window.innerHeight) {
		theHeight=window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		theHeight=document.documentElement.clientHeight;
	}
	else if (document.body) {
		theHeight = document.body.clientHeight;
	}
	if (window.innerWidth) {
		theWidth=window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		theWidth=document.documentElement.clientWidth;
	}
	else if (document.body) {
		theWidth = document.body.clientWidth;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;

		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function outputMoney(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
	
}

function refresh_cart(myfield){
	cart[parseInt(myfield.name)][0] = myfield.value;
	display_cart();
}
function numbersonly(myfield, e){
	
	var key;
	var keychar;

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1)){
	  	 return true;
	}
	
	else{
	   
	   return false;
	}
}
function remove_from_cart(id){
	cart.splice(id, 1);
	display_cart();
}

function add_to_cart(qty, item, price, data){
	//alert(qty+", "+item+", "+price+", "+data)
	var product = new Array;
	product[0] = qty; //qty
	product[1] = item;  //product name
	product[2] = price; //price	
	//alert(product[2])
	product[3] = item;
	cart.push(product)
}

function back_to_checkout(){
	toggleDisplay("cart");
	toggleDisplay("checkout");
	display_cart();
}

var registered_post = false
function add_registered_post(){
	if(registered_post==false){
		registered_post = true;
	}else{
		registered_post = false;	
	}
	
	display_cart();
}
function display_cart(){
	section = "cart";
	if(document.getElementById("cart").style.display != "block"){
		document.getElementById("cart").style.display = "block";
		obj_pos = findPos(document.getElementById("application"));
		document.getElementById("cart").style.left = obj_pos[0];
		document.getElementById("cart").style.top = obj_pos[1];
		document.getElementById("application").style.visibility = "hidden"
	}
	var cartHTML = "<br><form name=\"shopping_cart\" onSumbit=\"return false;\"><table align=center width=95% cellspacing=0><tr><td></td><td class=\"checkout_table_head\">qty</td><td class=\"checkout_table_head\" width=80%>item</td><td class=\"checkout_table_head\" align=right>price</td><td class=\"checkout_table_head\" align=right>total</td></tr>";

	grand_total = 0.00;
	start_shipping();
	for(i=0;i<cart.length;i++){
		cartHTML = cartHTML + "<tr><td><a href=\"javascript:remove_from_cart("+i+");\"><img src=\"img/remove.png\" border=0></a><td><input type=\"text\" name=\""+i+"\" value=\"" +  cart[i][0] + "\" class=\"input_qty\" onkeyup=\"refresh_cart(this)\" onKeyPress=\"return numbersonly(this, event);\"></td><td>" + cart[i][1] + "</td><td align=right>" + outputMoney(cart[i][2]) + "</td><td align=right>" + outputMoney((parseFloat(cart[i][2])*parseInt(cart[i][0]))) + "</td></tr>";
		set_shipping_item(cart[i][1], cart[i][0]);
		grand_total = grand_total + (parseFloat(cart[i][2])*parseInt(cart[i][0]));
	}
	if(cart.length==0){	
		cartHTML = cartHTML + "<tr><td colspan=4>There is nothing in your cart!<br><br><a href=\"javascript:hide_checkout();\"><img src=\"img/back_to_designer.png\" border=0></a></td></tr></table>";
	}else{
		var shipping = get_shipping();
		if(grand_total > 100){
			registered_post = true;
		}
		if(registered_post == true){
			shipping = shipping+10.00;
		}
		if(custom_shipping!=""){
			shipping = 0.00;
		}
		discount = discount_amount(grand_total);
		grand_total = grand_total = grand_total + shipping - discount;
		var shipinfo = "";
		if(custom_shipping != ""){
			shipinfo = "edit / delete your shipping company";
		}else{
			shipinfo = "Use your own shipping company?";
		}
		cartHTML = cartHTML + "<tr><td></td><td colspan=\"3\" align=right><b>Shipping and Packaging</b></td><td align=right>" + outputMoney(shipping) + "</td></tr>";
		cartHTML = cartHTML + "<tr><td></td><td colspan=\"3\" align=right><b>Discount</b></td><td align=right>" + outputMoney(discount) + "</td></tr>";
		
		cartHTML = cartHTML + "<tr><td></td><td colspan=3 align=right><b>Total</b><td align=right>" + outputMoney(grand_total) + "</td></td></tr>";
	
		if(custom_shipping != ""){
			cartHTML = cartHTML + "<tr><td></td><td colspan=4 align=right style=\"border-top:1px solid silver;\"><br />Shipping with <b>" + shipcname + "</b><br /><br /><a href=\"javascript:add_own_shipping();\">"+shipinfo+"</a><br /></td></tr>";
		}else{
			cartHTML = cartHTML + "<tr><td></td><td colspan=4 align=right style=\"border-top:1px solid silver;\"><br /><a href=\"javascript:add_own_shipping();\">"+shipinfo+"</a><br /><br /></td></tr>";
		}
		
		if(registered_post == false){
			cartHTML = cartHTML + "<tr><td></td><td colspan=4 align=right style=\"border-top:1px solid silver;\"><a href=\"javascript:add_registered_post();\">send as registered post</a><br /></td></tr>"
		}else{
			cartHTML = cartHTML + "<tr><td></td><td colspan=4 align=right style=\"border-top:1px solid silver;\"><a href=\"javascript:add_registered_post();\">remove registered post</a><br /></td></tr>"
		
		}
		cartHTML = cartHTML + "<tr><td colspan=5><table width=100% cellpadding=0 cellspacing=0><tr><td><a href=\"javascript:hide_checkout();\"><img src=\"img/back.png\" border=0></a></td>";
		
		cartHTML = cartHTML + "<td align=right valign=top><a href=\"javascript:checkout_step_2();\"><img src=\"img/continue_to_checkout.png\" border=0></a></td></tr></table></td></tr></table>";
		cartHTML = cartHTML + "</form>";
	}
	document.getElementById("cart").innerHTML = cartHTML;
	resize();
}	
var custom_shipping = "";
var  shipaddress1="";
var shippaddress2="";
var shipcname="";
var shipaccount="" 
var shipcontact="";
function add_own_shipping(){
		document.getElementById("Transparent").style.display = "block";
		document.getElementById("data_show").style.display = "block"
		if (navigator.appName.indexOf("Microsoft")!=-1) {
				
			leftpos = (document.body.clientWidth / 2) - (606/2);
		}else{
			leftpos = (window.innerWidth / 2) - (606/2);
		}
		document.getElementById("data_show").style.left = leftpos;
		document.getElementById("data_show").style.width = 606;
		document.getElementById("data_show_title").style.display = "block"
		
		document.getElementById("data_show").style.top = "100"
		document.getElementById("data_show_sub").style.padding = "5 0 0 0"
		document.getElementById("data_show").style.backgroundColor = "white";
		document.getElementById("data_show").style.top = "10"
		document.getElementById("data_show_title_text").innerHTML = "Set your own shipping company"
		document.getElementById("data_show_sub").innerHTML = "<form id=\"custom_ship\"><table width=90% align=center><tr><td>Shipping Courier Name</td><td><input type=\"text\" id=\"shipcname\" style=\"width:300px;\" value=\""+shipcname+"\" onKeyUp=\"shipcname=this.value\"></td></tr><tr><td>Shipping Address</td><td><input type=\"text\" id=\"shipaddress1\" style=\"width:300px;\" value=\""+shipaddress1+"\" onKeyUp=\"shipaddress1=this.value\"></td></tr><tr><td>&nbsp;</td><td><input type=\"text\" id=\"shipaddress2\" style=\"width:300px;\"  value=\""+shippaddress2+"\" onKeyUp=\"shippaddress2=this.value\"></td></tr><tr><td>Shipping Account Number</td><td><input type=\"text\" id=\"shipaccountnumber\" style=\"width:300px;\"  value=\""+shipaccount+"\" onKeyUp=\"shipaccount=this.value\"></td></tr><tr><td>Shipping Contact Number</td><td><input type=\"text\" id=\"shipcontact\" style=\"width:300px;\"  onKeyUp=\"shipcontact=this.value\" value=\""+shipcontact+"\" ></td></tr><td colspan=2 align=center><br><a href=\"javascript:remove_custom_shipping();\">Remove  custom shipping method</a> &nbsp; &nbsp; <a href=\"javascript:add_custom_shipping();\">Update custom shipping method</a></td></tr></table></form>";
		document.getElementById("data_show").style.height = 376;
}

function add_custom_shipping(){
	var shiperror="";
	shipcname = document.getElementById("shipcname").value;
	shipaccount =  document.getElementById("shipaccountnumber").value;
	shipaddress1 = document.getElementById("shipaddress1").value;
	shipaddress2 = document.getElementById("shipaddress2").value;
	shipcontact = document.getElementById("shipcontact").value;
	if(document.getElementById("shipcname").value==""){
		shiperror = shiperror + "You need to enter a courier name\n";	
	}
	if(document.getElementById("shipaccountnumber").value==""){
		shiperror = shiperror + "You need to enter the account number\n";	
	}
	if(document.getElementById("shipcontact").value==""){
		shiperror = shiperror + "You need to enter the couriers contact number\n";	
	}
	if(shiperror !=""){
		alert(shiperror);	
	}else{
		hide_data();
		custom_shipping =   "company||"+shipcname+"---address||"+shipaddress1+" "+shippaddress2+"---account||"+shipaccount+"---contact||"+shipcontact ;
		display_cart();
	}
}
function remove_custom_shipping(){
	hide_data();
	shipcname=""
	shipaddress1=""
	shippaddress2=""
	shipaccount=""
	shipcontact=""
	custom_shipping = "";
	display_cart();
}

function checkout_step_2(){
	section = "checkout";
	document.getElementById("checkout").style.display = "block";
	obj_pos = findPos(document.getElementById("cart"));
	document.getElementById("checkout").style.left = obj_pos[0];
	document.getElementById("checkout").style.top = obj_pos[1];
	document.getElementById("cart").style.display = "none";
	resize();
}

function toggleDisplay(divId) {
	var div = document.getElementById(divId);
	div.style.display = (div.style.display=="block" ? "none" : "block");
}
function toggleVisibility(divId) {
	var div = document.getElementById(divId);
	div.style.visibility = (div.style.visibility=="visible" ? "hidden" : "visible");
}

function hide_checkout(){
	toggleDisplay("cart");
	toggleVisibility("application");
	section = "application";
	
}



function show_data(data_type){
	document.getElementById("application").style.visibility = "hidden"
	document.getElementById("Transparent").style.display = "block"
	document.getElementById("data_show").style.display = "block"
	if (navigator.appName.indexOf("Microsoft")!=-1) {
			
		leftpos = (document.body.clientWidth / 2) - (606/2);
	}else{
		leftpos = (window.innerWidth / 2) - (606/2);
	}
	document.getElementById("data_show").style.left = leftpos;
	document.getElementById("data_show").style.width = 606;
	document.getElementById("data_show_title").style.display = "block"
	document.getElementById("data_show").style.top = "100"
	document.getElementById("data_show_sub").style.padding = "5 0 0 0"
	document.getElementById("data_show").style.backgroundColor = "white";
	if(data_type == "Company Details"){
		document.getElementById("data_show").style.top = "10"
		document.getElementById("data_show_title_text").innerHTML = "Company Details"
		document.getElementById("data_show_sub").innerHTML = "<div class=\"details_text\">" + site + " is run by an Australian owned & operated sign company which has been supplying stickers & signs since March 2001. By allowing you to create & design your stickers online we can acheive our aim to provide a quality product along with an outstanding, reliable & prompt service at a competitive price. We are constantly researching how to better supply & service our customers. Our product is backed by a 100% satifaction guarantee. <br><br>For any technical issues or enquires<br>Call Sign Options on 07 49720609 during business hours</div><img src=\"img/details_bottom.jpg\" border=0>";

		document.getElementById("data_show").style.height = 376;
		
	}else if(data_type == "Payment Method"){
		document.getElementById("data_show_title_text").innerHTML = "Payment Method"
		document.getElementById("data_show").style.top = "100"
		document.getElementById("data_show_sub").innerHTML = "<br>We offer payment to be made by Credit Card or Direct Deposit<br><br>For your piece of mind. All transactions are processed through PayPal<br><br><A HREF=\"pdf/Account_Application_Form.PDF\">Download an Account Application Form</a><br><img src=\"img/payment_icons.png\">";
		document.getElementById("data_show").style.height = 190;
		
	}else if(data_type == "Material Quality"){
		document.getElementById("data_show_title_text").innerHTML = "Material Quality"
		document.getElementById("data_show").style.top = "10"
		document.getElementById("data_show_sub").innerHTML = "";
		document.getElementById("data_show").style.height = 465;
	}
}

function hide_data(){
	if(section == "cart"){
		document.getElementById("cart").style.display = "block"
	}else if(section == "checkout"){
		document.getElementById("cart").style.display = "block"
	}else{
		document.getElementById("application").style.visibility = "visible"
	}
	document.getElementById("data_show_sub").innerHTML = "";
	document.getElementById("Transparent").style.display = "none";
	document.getElementById("data_show").style.display = "none";
	
}
function validate(){
	var theForm = document.getElementById("checkout_form");
	var error = ""
	
		
	for(i=0; i<theForm.elements.length; i++){
		if(theForm.elements[i].name == "first_name"){
			
			if(theForm.elements[i].value == ""){
				error = error + "You need to enter your first name\n"
			}
		}
		if(theForm.elements[i].name == "last_name"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to enter your last name\n"
			}
		}
		if(theForm.elements[i].name == "address"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to enter a delivery address\n"
			}
		}
		if(theForm.elements[i].name == "suburb"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to enter a suburb\n";
			}
		}
		if(theForm.elements[i].name == "state"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to select a state\n"
			}
		}
		if(theForm.elements[i].name == "post_code"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to select a postcode\n"
			}
		}
		if(theForm.elements[i].name == "email"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to enter a valid email address\n";
			}
		}
		if(theForm.elements[i].name == "telephone"){
			if(theForm.elements[i].value == ""){
				error = error + "You need to enter a telephone number\n";
			}
		}
		
		
	}
	payment_type = getCheckedValue(theForm.elements["payment_type"]);
	if(payment_type == "order"){
		if(theForm.elements["order_number"].value!=""){
			payment_type = theForm.elements["order_number"].value;
		}else{
			error = error + "You need to enter your purchase order number\n";
		}
	}
	if (error == ""){
		toggleDisplay("Transparent");
		document.getElementById("orderLoader").innerHTML = "<br>processing order details<br>please wait"
		toggleDisplay("orderLoader");
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			toppos = document.body.clientHeight / 2 - 30;
			leftpos = document.body.clientWidth / 2 - 125;
		}else{
			toppos = window.innerHeight / 2 -30;
			leftpos = window.innerWidth / 2 - 125;
		}
		el = document.getElementById('orderLoader');
		el.style.left = leftpos;
		el.style.top = toppos;

		element = document.createElement('script');
	    	element.type = "text/javascript";
		element.src= "create_order.php?data=" + URLEncode(get_data()) + "&payment_type="+ payment_type +"&unique=" + new Date().getTime()+"&custom_shipping="+custom_shipping;
		document.getElementsByTagName('body')[0].appendChild(element);
	}else{
		alert(error);
		
	}
	return false;
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function do_order(order_id){
	if(payment_type=="paypal"){
		exit_paypal = true;
		document.getElementById("orderLoader").innerHTML = "<br>redirecting to <b>Paypal</b><br>please wait"
		var theForm = document.getElementById("paypal");
		theForm["item_number"].value = order_id;
		theForm["amount"].value = roundNumber(grand_total,2);
		theForm["item_name"].value = "reflectivetape_" + order_id;
		theForm["return"].value = "http://"+site+"/?thankyou=true&id=" + order_id;
		theForm.submit();
	}else{
		exit_paypal = true;
		toggleDisplay("checkout");
		toggleDisplay("Transparent");
		toggleDisplay("orderLoader");
		toggleDisplay("cart");
		/*
		obj_pos = findPos(document.getElementById("container"));
		document.getElementById("display_div").style.top = obj_pos[1];
		document.getElementById("display_div").style.left = obj_pos[0];
		*/
		if(payment_type == "deposit"){
			xtra_html = "<table width=\"100%\" bgcolor=\"#dcff98\"><tr><td width=50% valign=top>Please deposit "+ outputMoney(grand_total) +" to bank account on the right, using <b>reflectivetape_"+order_id+"</b> as the payment reference.</td><td width=50% valign=top>BANK : ANZ<br>BSB : 014 580<br>ACCOUNT NO : 490054289 </td></tr></table><br>";

		
		}else{
			xtra_html = "<table width=\"100%\" bgcolor=\"#dcff98\"><tr><td width=50% valign=top>Please fax offical order to Sign Options on 07 4972 0911 quoting <b>reflectivetape_"+order_id+"</b><br>or email offical order to <a href=\"mailto:signoptions1@optusnet.com.au\">signoptions1@optusnet.com.au</a> quoting <b>reflectivetape_"+order_id+"</b></td></tr></table><br>";
		}
		load_order(order_id);
	}
}
function getURLVar(urlVarName) {
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		var urlVars = urlHalves[1].split('&');
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}

function load_order(id){
	element = document.createElement('script');
    	element.type = "text/javascript";
	element.src= "get_order.php?id=" + id;
	document.getElementsByTagName('body')[0].appendChild(element);
}


function key_data(keydata, key){
	
	i_keydata = keydata.split("---");
	//alert(i_keydata)
	for(j=0;j<i_keydata.length;j++){
		i_d = i_keydata[j].split("||");
		
		if(i_d[0] == key){
			return i_d[1];
		}
	}
}

function replaceAll(text, strA, strB)
{
    while ( text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}
function printSelection(node){

  var content=document.getElementById(node).innerHTML
  var pwin=window.open('','print_content','width=100,height=100');

  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
 
  setTimeout(function(){pwin.close();},1000);

}
function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function discount_amount(price){
	if(price>=300){
		var iprice = ((price/100)*((((price-100)/100)/2)+3));
		if((((((price-100)/100)/2)+3) > 15.1)){
			var iprice = ((price/100)*15);
			if(iprice > 2500){
				iprice = 2500;	
			}
		}else{
			if(iprice > 2500){
				iprice = 2500;	
			}
		}
		return iprice;
	}else{
		return 0.00;
	}
}

function display_order(data){
	sub_data = data.split("-----")
	stickers = sub_data[1].split("|-|-|")
	order_html = "<table width=80% align=center cellpadding=0 cellspacing=0 border=0><tr><td align=left colspan=4>";
	order_html =order_html + "<table width=100% cellpadding=0 cellspacing=0 border=0><tr><td>Thank you for your order </td><td align=right><a href=\"javascript:printSelection('cart');\"><img src=\"img/print.png\" border=0></a></td></tr></table><br>";
	order_html = order_html + xtra_html;
	order_html = order_html + "<table width=\"100%\"><tr><td width=60% valign=top><b>To</b><br>";
	if(key_data(sub_data[0], "company")!=""){
		order_html = order_html + key_data(sub_data[0], "company") + "<br>";
	}
	order_html = order_html + key_data(sub_data[0], "first_name") + " " + key_data(sub_data[0], "last_name") + "<br>";
	order_html = order_html + key_data(sub_data[0], "address") + " " + key_data(sub_data[0], "address_2") + "<br>";
	order_html = order_html + key_data(sub_data[0], "suburb") + ", " + key_data(sub_data[0], "state") + "<br>";
	order_html = order_html + key_data(sub_data[0], "post_code") + "<br>";
	order_html = order_html + "Ph: " + key_data(sub_data[0], "telephone") + "<br>";
	order_html = order_html + key_data(sub_data[0], "email") + "<br>";

	order_html = order_html + "</td><td width=40% valign=top><b>From</b><br>SIGNOPTIONS<br>66 Lord St<br>Gladstone, QLD<br>Ph: (07) 4972 0609<br>Fax: (07) 4972 0911<br>websales@signoptions.com.au</td></tr></table><br>";
	order_html = order_html+"</td></tr><tr><td class=\"checkout_table_head\" width=10%>qty</td><td class=\"checkout_table_head\">item</td><td align=right class=\"checkout_table_head\" width=10%>price</td><td align=right class=\"checkout_table_head\" width=10%>total</td></tr>";
	var _total = 0.00;
	shipping = 0.00;
	start_shipping();
	for(i=0;i<stickers.length-1;i++){
		_sticker = replaceAll(stickers[i], "---", "&");
		_sticker = replaceAll(_sticker, "||", "="); 
		order_html = order_html + "<tr><td>" + key_data(stickers[i], "qty") + "</td><td>"+ key_data(stickers[i], "item") +"</td><td align=right>"+ outputMoney(parseFloat(key_data(stickers[i], "price"))) + "</td><td align=right>"+ outputMoney(parseFloat(key_data(stickers[i], "price"))*parseFloat(key_data(stickers[i], "qty"))) + "</td></tr>";
		_total = _total + (parseFloat(key_data(stickers[i], "price"))*parseFloat(key_data(stickers[i], "qty")));
		
		set_shipping_item(key_data(stickers[i], "item"), key_data(stickers[i], "qty"));
		//alert("set_shipping_item(key_data("+stickers[i]+", \"item\"), key_data("+stickers[i]+", \"qty\"));")
	}
	var shipping = get_shipping();
	if(registered_post==true){
		shipping = shipping + 10.00;	
	}
	if(sub_data[3]!=""){
		shipping = 0.00;
	}
	discount = discount_amount(_total);
	_total = _total + shipping - discount;
	order_html = order_html + "<tr><td colspan=3 align=right>shipping and packaging</td><td class=\"checkout_table_head\" align=right>"+outputMoney(shipping)+"</td></tr>";
	order_html = order_html + "<tr><td colspan=3 align=right>discount</td><td class=\"checkout_table_head\" align=right>"+outputMoney(discount)+"</td></tr>";
	
	order_html = order_html + "<tr><td colspan=3 align=right>Total (inc GST)</td><td class=\"checkout_table_head\" align=right>" + outputMoney(parseFloat(key_data(sub_data[2], "total"))) + "</td></tr>";
	if(sub_data[3]!=""){
		iii = sub_data[3].split("---");
		iiii = iii[0].split("||");
		order_html = order_html + "<tr><td colspan=4 align=right style=\"border-top:1px solid silver;\"><br>shipping with <b>"+iiii[1]+"</b><br /><br /></td>";
	}
	order_html = order_html + "</table>";
	
	document.getElementById("cart").innerHTML = order_html;
}



function begin(){
	get_window_dimensions();
	
	obj_pos = findPos(document.getElementById("header"));
	document.getElementById("application").style.top = 120;
	document.getElementById("application").style.left = obj_pos[0];
	if(getURLVar("thankyou")=="true"){
		document.getElementById("cart").style.display = "block";
		document.getElementById("cart").style.top = 120;
		document.getElementById("cart").style.left = obj_pos[0];
		document.getElementById("cart").innerHTML = "";
		
		load_order(getURLVar("id"));
	}else{
		
	}
	make_drop_downs();
}
function show_app(){
	//var so = new SWFObject("swf/pipemarker.swf", "pipemarker", "773", "671", "8", "#ffffff");
	//so.addVariable("xml", "xml/settings.xml");
	//so.write("application");
}
function resize(){
	obj_pos = findPos(document.getElementById("header"));
	document.getElementById(section).style.top = 120;
	document.getElementById(section).style.left = obj_pos[0];
}
function porderhide(){
	document.getElementById("porder").style.display="none"
}
function pordershow(){
	document.getElementById("porder").style.display="block"
}
function make_drop_downs(){
	_top = 120;
	obj_pos = findPos(document.getElementById("header"));
	_left = obj_pos[0];
	
	for(var i=1;i<_cat.length;i++){
		for(var j=0;j<_cat[i].length;j++){
			_jcat = _cat[i][j].split("..");
			newDiv = document.createElement("div");
			newDiv.innerHTML = "&nbsp;&nbsp;<a href='javascript:_select_item("+i+","+j+");hide_drop_down("+i+")'>"+_jcat[0]+"</a>"
			newDiv.className = "menu_item"
			newDiv.id = "_"+i+"_"+j
			newDiv.style.top = _top+(j*20);
			if(i==1){
				newDiv.style.left = _left+-1;
				newDiv.style.width = 208;
			}
			if(i==2){
				newDiv.style.left = _left+208;
				newDiv.style.width = 237;
			}
			if(i==3){
				newDiv.style.left = _left+446;
				newDiv.style.width = 197;
			}
			newDiv.style.display = "none"
			newDiv.onClick = "_select_item("+i+","+j+");hide_drop_down("+i+")";
			document.getElementsByTagName('body')[0].appendChild(newDiv);
		}
	}
}
var menu_up = false;
var current_column;
function show_drop_down(column){
	current_column = column;
	menu_up = true
	obj_pos = findPos(document.getElementById("header"));
	_left = obj_pos[0];
	for(var i=1;i<_cat.length;i++){
		for(var j=0;j<_cat[i].length;j++){
			newDiv = document.getElementById("_"+i+"_"+j)
			if(i==column){
				if(i==1){
					newDiv.style.left = _left+-1;
					newDiv.style.width = 208;
				}
				if(i==2){
					newDiv.style.left = _left+208;
					newDiv.style.width = 237;
				}
				if(i==3){
					newDiv.style.left = _left+446;
					newDiv.style.width = 197;
				}
				newDiv.style.display = "block";
			}else{
				newDiv.style.display = "none";	
			}
		}
	}
}

function hide_drop_down(){
	menu_up = false;
	for(var i=1;i<_cat.length;i++){
		for(var j=0;j<_cat[i].length;j++){
			document.getElementById("_"+i+"_"+j).style.display = "none";
		}
	}
}

function _display(column){
	document.getElementById("checkout").style.display = "none";
	document.getElementById("cart").style.display = "none";
	document.getElementById("application").style.visibility="visible"
	apphtml = "";
	_ii= _icat[column] .split("..");
	if(column==3){
		apphtml = "<table width='100%' align='right' cellpadding='10'><tr><td colspan='2'><h1>" +_ii[0]+ "</h1></td></tr><tr><td valign='top'>"+_ii[1]+"</td><td width='27%' valign=top><table width='100%' cellspacing='2' cellpadding='2' align='center'><tr><td style='border:solid 1px silver; background-color:#b40118; color:white;'>&nbsp;&nbsp;Select an item</td></tr>";
		for(var j=0;j<_cat[column].length;j++){
			_jcat = _cat[column][j].split("..");
			apphtml = apphtml + "<tr><td style='border:solid 1px silver;' align='center'><a href=\"javascript:_select_item("+column+","+j+");\">" + _jcat[0] + "</a></td></tr>"	
		}
		apphtml = apphtml + "</td></tr></table></td></tr></table>";
	}else{
		apphtml = "<table width='100%' align='right' cellpadding='10'><tr><td colspan='3'><h1>select a " +_ii[0]+ " colour</h1></td></tr>";
		var c = 0;
		var c1 = 0;
		var c2 = 0;
		var ltr=0;
		for(var j=0;j<_cat[column].length;j++){
			_jcat = _cat[column][j].split("..");
			if(c==0){
				apphtml = apphtml + "<tr>";
			}
			if(_jcat[0].indexOf("class 1")!=-1){
				if(c1==0){
					if(column==2){
						apphtml = apphtml + "</tr>";
					}else{
						apphtml = apphtml + "</tr><tr><td colspan=3 align=center><img src=\"img/line.png\"></td></tr><tr>";
					}
					c=0;
				}
				c1 = 1;	
			}
			if(_jcat[0].indexOf("class 2")!=-1){
				if(c2==0){
					apphtml = apphtml + "</tr><tr><td colspan=3 align=center><img src=\"img/line.png\"></td></tr><tr>";
						c=0;
				}
				c2 = 1;	
			}
			apphtml = apphtml + "<td  align='center'>" + _jcat[0] + "<br><a href=\"javascript:_select_item("+column+","+j+");\"><img src=\"img/icons/"+_jcat[1]+"\"  border=0></a></td>";
			c++
			ltr=0;
			if(c==3){
				apphtml = apphtml + "</tr>";
				c=0;
				ltr=1;
			}
		}
		if(ltr != 0){
			apphtml = apphtml + "</tr>";
		}
		apphtml = apphtml + "<tr><td colspan=3 align=left style=\"padding:30px;\">"+_ii[1]+"</td></tr></table>";
	}
	document.getElementById("application").innerHTML = apphtml;
	resize();
}

var price_set, _by_type, _tape_width, _item;
function _select_item(column, row){
	document.getElementById("checkout").style.display = "none";
	document.getElementById("cart").style.display = "none";
	document.getElementById("application").style.visibility="visible"
	apphtml = "";
	_ii= _icat[column] .split("..");
	_jcat = _cat[column][row].split("..");
	_item = _jcat[0];
	
	// prices
	carthtml = "<table width=100%><tr><td style='border:solid 1px silver;' align='center'>add to cart</td></tr></table>";
	if((column == 1) || (column == 2)){
		_tp = tape_prices.split(";")
		for(var i=0;i<_tp.length;i++){
			_p = _tp[i].split(":")
			if(_jcat[0].indexOf(_p[0])!=-1){
				if(column==1){
					price_data_set = _tp[i];
				}else if(column==2){
					price_data_set = _tp[i+2];
				}
			}
		}
		price_set = price_data_set.split(":");
		_tape_widths = tape_widths.split(";")
		_by_type="roll";
		carthtml = "<form style='margin:none;'><table width=100% style=\"border:solid 1px silver;background-color:#efefef;\"><tr><td align=right><div id='_price'><h3>$0.00</h3></div><tr><td><tr><td style='border:solid 1px silver;background-color:#ffffff;' align='center'>select by roll or metre</td></tr>"
		carthtml = carthtml + "<tr><td><input type=\"radio\" name=\"roll_or_metre\"  onclick=\"_by_type='roll';document.getElementById('metre').style.display='none';_update_tape_price();\" checked> by the "+price_set[1]+" metre roll <br><input type=\"radio\" name=\"roll_or_metre\"  onclick=\"_by_type='metre';document.getElementById('metre').style.display='block';_update_tape_price();\"> by the metre<br><div id='metre' style='display:none;'><select id='_metre' onchange=\"_update_tape_price();\">";
		for(var i=5;i<(20);i++){
			if(i==5){
				carthtml = carthtml + "<option value='"+i+"' selected>"+i
			}else{
				carthtml = carthtml + "<option value='"+i+"'>"+i
			}
		}
		carthtml = carthtml + "</select></div></tr></td>";
		carthtml = carthtml + "<tr><td style='border:solid 1px silver;background-color:#ffffff;' align='center'>select tape width</td></tr>"
		carthtml = carthtml + "<tr><td>";
		for(var i=0;i<_tape_widths.length;i++){
			__tape_width = _tape_widths[i].split(":");
			if(i==0){
				_tape_width = _tape_widths[i];
				carthtml = carthtml + "<input type=\"radio\" name=\"tape_width_\"  onclick=\"_tape_width='"+_tape_widths[i]+"';_update_tape_price();\" checked>"+__tape_width[0]+"mm <br>";
			}else{
				carthtml = carthtml + "<input type=\"radio\" name=\"tape_width_\"  onclick=\"_tape_width='"+_tape_widths[i]+"';_update_tape_price();\">"+__tape_width[0]+"mm <br>";
			}
		}
		carthtml = carthtml + "<tr><td style='border:solid 1px silver;background-color:#ffffff;' align='center'><a href=\"javascript:	add_to_cart(1, _description, _tapetotal, '');display_cart();\">add to cart</a></td></tr>"
		carthtml = carthtml + "</table></form>";
	}else{
		if(_item.indexOf("sticker")!=-1){
			carthtml = "<table width=100% style=\"border:solid 1px silver;background-color:#efefef;\"><tr><td style='border:solid 1px silver;background-color:#ffffff;' align='center'><a href=\"javascript:add_to_cart(1, '"+_item+"', "+sticker_price+", '');display_cart();\">add to cart</a></td></tr><tr><td align=right><h3>"+outputMoney(sticker_price);+" inc GST</h3></td></tr></table>"
		}
		if(_item.indexOf("beacon")!=-1){
			carthtml = "<table width=100% style=\"border:solid 1px silver;background-color:#efefef;\"><tr><td style='border:solid 1px silver;background-color:#ffffff;' align='center'><a href=\"javascript:add_to_cart(1, '"+_item+"', "+beacon_price+", '');display_cart();\">add to cart</a></td></tr><tr><td align=right><h3>"+outputMoney(beacon_price);+" inc GST</h3></td></tr></table>"
		}
		if(_item.indexOf("triangle")!=-1){
			carthtml = "<table width=100% style=\"border:solid 1px silver;background-color:#efefef;\"><tr><td style='border:solid 1px silver;background-color:#ffffff;' align='center'><a href=\"javascript:add_to_cart(1, '"+_item+"', "+beacon_price+", '');display_cart();\">add to cart</a></td></tr><tr><td align=right><h3>"+outputMoney(triangle_price);+"inc GST</h3></td></tr></table>"
		}
	}
	//carthtml = "<table width=100%><tr><td style='border:solid 1px silver;' align='center'>add to cart</td></tr></table>";
	
	
	do_innerHTML = true;
	if(_jcat[1].indexOf(".swf")!= -1){
		do_innerHTML = false;
		var so = new SWFObject(_jcat[1], "swf_file", "800", "600", "8", "#ffffff");
		so.write("application");
	}else if(_jcat[1] == "tape"){
		img = "taperoll.jpg";
	}else{
		img = _jcat[1];
	}
	if(do_innerHTML){	
		apphtml = "<table width='100%' align='right' cellpadding='10'><tr><td colspan='2' ><h1>"+_ii[0] + " - " + apphtml + _jcat[0] + "</h1></td></tr><tr><td valign=top><img src=\"img/"+img+"\" align=\"left\">"+ _jcat[2]+"</td><td valign=\"top\" width=\"27%\">"+carthtml+"</td></table>";	
		document.getElementById("application").innerHTML = apphtml;
		if((column == 1) || (column == 2)){
			_update_tape_price();	
		}
	}
	resize();
}
var _tapetotal, _description;
function _update_tape_price(){
	__tape_width = _tape_width.split(":");
	if(_by_type=='roll'){
		_tapetotal = __tape_width[1]*price_set[2];
		_description = _item + "  - "  + price_set[1] + "m roll  - " +  __tape_width[0] + "mm wide";
	}else{
		_tapetotal  = __tape_width[1]*price_set[3]*document.getElementById("_metre").value;
		_description = _item + "  - "  + document.getElementById("_metre").value + " mtrs  - " +  __tape_width[0] + "mm wide";
	}
	document.getElementById("_price").innerHTML = "<h3>"+outputMoney(_tapetotal)+" inc GST<h3>";
}

