// shopmastery 1.0 Copyright 1999-2005 Andrew Eordogh


var secure_server = "https://www.lumberbunker.com.au/";
var URL = "http://www.lumberbunker.com.au";

var international;
var CODE = '';
var DESC = '';
var theorder = '';
var page;
var DELIVERY = '';
var WGT = '';
var totweight = '';

function goBasket() {


	if (document.cookie.indexOf("[") == -1) {
		alert('Your shopping basket is empty!!');
		} else {
		file = secure_server + 'shopmastery_basket.php';
		mywindow=open(file,"","height=450,width=750,top=20,left=250,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no");
		

	}
}

function goCheckout() {
	if (document.cookie.indexOf("[") == -1) {
		alert("You have not ordered any items.\nYou must place an order for an item before you can proceed to the checkout.");
		} else {

		thecookie = document.cookie;
		
		location.href = secure_server + 'shopmastery_checkout1.php?cookie=' +thecookie;
	}
}



function goCheckout2() {
	if (document.cookie.indexOf("[") == -1) {
		alert("You have not ordered any items.\nYou must place an order for an item before you can proceed to the checkout.");
		} else {

		thecookie = document.cookie;

		parent.opener.location.href = secure_server + 'shopmastery_checkout1.php?cookie=' +thecookie;
		self.close();
	}
}



function recalc() {
	history.go(0);
}


function addBasket(NAME,PRICE,CODE,DESC,WEIGHT,itemno,QTY) {


//if (document.shopmastery.elements['type_'+itemno]) {
//	CODE = document.shopmastery.elements['type_'+itemno].value;
//}

	if (QTY =="") { 
		QTY=0;
		}
	
		// QTY=parseInt(QTY)
		TOTAL = cent(QTY * PRICE);
		PRICE = cent(PRICE);

		if (QTY <= 0) {
			rc = alert("Please tell us how many \n" +NAME+ "\r\n you need.");
			} else {




var msg = 'Add '+QTY+' x '+NAME+ ' '+' @ \$'+PRICE+' each for a total of \$'+TOTAL+' to your order?';
			
			if (confirm(msg)) {
			index = document.cookie.indexOf("myBasket");
			countbegin = (document.cookie.indexOf("=", index) + 1);
			countend = document.cookie.indexOf(";", index);

				if (countend == -1) {
					countend = document.cookie.length;
	        	}
//path="/";
document.cookie="myBasket="+document.cookie.substring(countbegin, countend)+'[' +NAME+ '|' +PRICE+'|' +DESC+'|' +CODE+'|' +WEIGHT+'|' +QTY+ "]"   + (("/" == null)    ? "" : "; path=" + "/") ;

			}
	}

}

	
function cent(amount) {
     amount -= 0;
     amount = (Math.round(amount*100))/100;
     return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
 }


function showBasket() {

// var state = '$state';

index = document.cookie.indexOf("myBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
		countend = document.cookie.length;
	}
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;

document.writeln('<FORM NAME="updateform">');

document.writeln('<CENTER><TABLE border=0 cellspacing=0 cellpadding=0 BGCOLOR="#3B4818" width="95%"><TR><TD>');

document.writeln('<CENTER><TABLE border=0 cellspacing=1 cellpadding=3 bgcolor=black class="w10" width=100%>');

document.writeln('<TR BGCOLOR="#3B4818" align=center height=30><td width=20 class="w10"><b>Qty</TD>');


document.writeln('<td class="w10"><b>Code</TD>');

document.writeln('<td class="w10" NOWRAP><b>Item</b></td>');


document.writeln('<td class="w10"><b>Description</TD>');

document.writeln('<td class="w10" NOWRAP><b>Unit<br>Price</b></td>');

document.writeln('<td class="w10"><b>Wgt (kg)</TD>');

document.writeln('<td class="w10"><b>Total Wgt</TD>');



document.writeln('<td width=55 class="w10"><b>Sub<br>Total</b></td>');

if (page != 'checkout') {
document.writeln('<td width=20 class="w10"><B>Delete</TD></TR>');
}

		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
totweight = 0;
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
				thisitem = 1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);
				itemtotal = 0;
				
if (international == 1) { PRICE = cent(PRICE / 1.1); }
				
				itemtotal = (eval(PRICE * thequantity));
				temptotal = itemtotal * 100;
				totprice = totprice + itemtotal;
				itemlist=itemlist+1;
				totweight = eval(totweight + (thequantity * WEIGHT));
				WGT = eval(totweight + WGT);
				totweight = totweight.toFixed(1);

			


if (page == 'checkout' || page == 'checkout2') {

document.write('<tr bgcolor="#FFFFFF"><td align=center><font color=black>' +thequantity+ '</td>');

} else {

document.write('<tr bgcolor="#FFFFFF"><td align=center><INPUT CODE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" size=2 onchange="changeItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"></td>');

}

document.write('<td align=center><font color=black>'+CODE+'</td>');
document.write('<td align=left width=250><font color=black>'+NAME+'</td>');


document.write('<td align=center nowrap><font color=black nowrap>'+DESC+'</td>');

document.write('<td align=center><font color=black>'+PRICE+'</td>');

document.write('<td align=center nowrap><font color=black nowrap>'+WEIGHT+'</td>');

document.write('<td align=center nowrap><font color=black nowrap>'+totweight+'</td>');


document.write('<td align="right" bgcolor="#EFECEC" width=30><font color=black><B>'+cent(itemtotal)+'</td>');



if (page != 'checkout') {
document.write('<td align=center><a href="javascript:removeItem('+itemlist+')"><img src="admin/images/trash.gif" border=0 width=15 height=21></td></tr>');
}

theorder = theorder+NAME+'|'+PRICE+'|'+DESC+'|'+CODE+'|'+WEIGHT+'|'+thequantity+']';



			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) NAME = fulllist.substring(itemstart, i);
				if (thisitem==2) PRICE = fulllist.substring(itemstart, i);
				if (thisitem==3) DESC = fulllist.substring(itemstart, i);
				if (thisitem==4) CODE = fulllist.substring(itemstart, i);
				if (thisitem==5) WEIGHT = fulllist.substring(itemstart, i);

				thisitem++;
				itemstart=i+1;
			}


		}


WGT = WGT.toFixed(1);

if (page == 'checkout2') {

if (totweight < 3) { DELIVERY = 10.70; }
if (totweight <= 0.5) { DELIVERY = 7.8; }

if (totweight > 3) {

if (totweight < 10 && (state == 'NSW' || state == 'ACT')) { DELIVERY = 14; }
if (totweight < 10 && (state == 'VIC' || state == 'TAS')) { DELIVERY = 15; }
if (totweight < 10 && state == 'QLD') { DELIVERY = 20; }
if (totweight < 10 && state == 'SA') { DELIVERY = 18; }
if (totweight < 10 && (state == 'NT' || state == 'WA')) { DELIVERY = 30; }

if (international == 1) { DELIVERY = 0; }

}


if (international == 0) {

document.writeln('<tr BGCOLOR="#EFECEC"><td colspan=6 align=right><font color=black><B>DELIVERY</b>&nbsp;</td><td align=center colspan=1 bgcolor="#EFECEC"><font color=black><font color=black>' +WGT+ '</td><td  align=right colspan=1 bgcolor="#EFECEC" class="body"><font color=black><B>\$'+cent(DELIVERY)+'</td><td bgcolor=white>&nbsp;</td></tr>');
totprice = totprice + DELIVERY;
}

}



document.writeln('<tr BGCOLOR="#3B4818"><td colspan=7 align=right><font color=white><B>TOTAL</b>&nbsp;</td><td align=right colspan=1 bgcolor="#EFECEC"><font color=black><font color=black><B>\$'+cent(totprice));

if (page != 'checkout') {
document.writeln('</td><td bgcolor=white>&nbsp;</td>');
}

document.writeln('</TR></TABLE>');

document.writeln('</TD></TR></TABLE>');


}
	
function changeItem(itemno, newquant) {


if (newquant == 0) {
	alert("You cannot order 0 items.\r Please delete this item instead.");
	return;
}

		newItemList = null;
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				thisitem = 1;
				itemstart = i+1;
				fullstart = i+1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				itemlist=itemlist+1;
				if (itemlist != itemno) {
					newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
				} else {
					newItemList = newItemList + '['+NAME+ '|' +PRICE+'|' +DESC+'|' +CODE+'|' +WEIGHT+'|'+newquant+']';
				}
			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) NAME = fulllist.substring(itemstart, i);
				if (thisitem==2) PRICE = fulllist.substring(itemstart, i);
				if (thisitem==3) DESC = fulllist.substring(itemstart, i);
				if (thisitem==4) CODE = fulllist.substring(itemstart, i);
				if (thisitem==5) WEIGHT = fulllist.substring(itemstart, i);
				

				thisitem++;
				itemstart=i+1;
			}
		}
		index = document.cookie.indexOf("myBasket");
		document.cookie="myBasket="+newItemList;
		history.go(0);

	}

function removeItem(itemno) {
	NAME = fulllist.substring(itemstart, itemend);
	
		if (confirm('Are you sure you want to delete this item from your order?')) {

		newItemList = null;
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				NAME = fulllist.substring(itemstart, itemend);
				itemlist=itemlist+1;
				if (itemlist != itemno) {
					newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
				}
			}
		}
		index = document.cookie.indexOf("myBasket");
		document.cookie="myBasket="+newItemList;
		history.go(0);
	}
}


function goPop(id) {

location.href = 'shopmastery_pop.php?id=' +id;
				
}

