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 + "---data||" + cart[i][3] ;
		data = data + "---item||" + cart[i][1];
		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] = data;
	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 Company 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 = "stencil_" + 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>stencil_"+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>stencil_"+order_id+"</b><br>or email offical order to <a href=\"mailto:signoptions1@optusnet.com.au\">signoptions1@optusnet.com.au</a> quoting <b>stencil_"+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;
}


var apphtml = ""
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];
	apphtml = document.getElementById("application").innerHTML;
	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_application(appdata){
	section = "application";
	document.getElementById("cart").style.display = "none"
	document.getElementById("application").style.visibility = "visible"
	var so = new SWFObject("swf/stencil_builder.swf", "stencils", "800", "460", "8", "#ffffff");
	so.addVariable("external_stencil_data", appdata);
	so.write("application");
	 resize();
}
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 stencil_templates(){
	section = "application";
	document.getElementById("cart").style.display = "none"
	document.getElementById("application").style.visibility = "visible"
	document.getElementById("application").innerHTML = apphtml;
	resize();
}
var stencil_detail;
var stencil_size;
var stencil_price;
function get_stencil_price(ivalue){
	jprices = stencil_prices.split(";")
	for(i=0;i<jprices.length;i++){
		iji = jprices[i].split(",");
		if(iji[0]==ivalue){
			stencil_price = iji[1];
			stencil_size = ivalue;
			document.getElementById("stencil_price").innerHTML="<h1>"+outputMoney(iji[1])+"</h1>";	
		}
	}
}

function add_stencil_to_cart(){
	add_to_cart(1, "Stencil : " + stencil_detail+" - "+stencil_size+" mm" , stencil_price, '');
	display_cart();
}

function display_stencil(stencil_image, name, description){
	section = "application";
	stencil_detail = name;
	scroll(0,0);
	sthtml = "<select name='size' onchange=\"get_stencil_price(this.value);\">";
	var i = 200;
	while(i<=1100){
		i=i+100;
		sthtml = sthtml + "<option value=\""+i+"\">"+i+" x "+i+" mm </option>";
	}
	sthtml = sthtml + "</select>";
	document.getElementById("application").innerHTML =  "<table width=100% cellpadding=10 cellpsacing=5><tr><td colspan=3><h1>Stencil : "+name+"</h1></td></tr><tr><td valign=top><image src=\""+stencil_image+"\" align=left></td><td valign=top>"+description+"</td><td valign=top align=right><div id=\"stencil_price\"><h1>$0.00</h1></div><br>Select Size "+sthtml+"<br><br><a href=\"javascript:add_stencil_to_cart();\"><img src=\"img/add_to_cart.png\" border=0></a></td></tr></table><br><br>";
	get_stencil_price(300);
	resize();
}



