// Cybershop 1.4 Copyright 1999-2005 Andrew Eordogh

var extra='';
var ASSEMBLE =0;
var PAINT =0;
var INSTALL =0;
var DELIVERY = 0;
var QTY =0;
var theorder ='';
var COLOR='';
var WEIGHT='';


function goBasket() {

	if (document.cookie.indexOf("[") == -1) {
		alert('Your shopping basket is empty!!');
		} else {
		file = 'cybershop_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.\r \nYou must place an order for an item before you can proceed to the checkout.');
		} else {
		location.href = 'cybershop_checkout.php';
	}
}

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


function addBasket(DESC,PRICE,SIZE,QTY) {

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

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




var msg = 'Add '+QTY+' x ' +DESC+' @ \$'+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;
	        	}

document.cookie="myBasket="+document.cookie.substring(countbegin, countend)+'[' +DESC+ '|' +PRICE+'|' +SIZE+'|' +WEIGHT+'|' +QTY+ "]";

			}
	}


}

	
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 totitems = 0;
INSTALL=0; theorder=''; DESC=''; QTY=0; PRICE=0; DELIVERY=0; ASSEMBLE='';

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=black width=500><TR><TD>');

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

document.writeln('<TR BGCOLOR="#AE7C57" align=center height=30><td width=10 class="Wbody"><b>Qty</TD>');

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

document.writeln('<td class="Wbody" width=50><b>Size</TD>');


document.writeln('<td class="Wbody" NOWRAP  width=50><b>Unit Price</b></td>');

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

document.writeln('<td width=20 class="Wbody"><B>Delete</TD></TR>');

		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			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;
				itemtotal = (eval(PRICE * thequantity));
				temptotal = itemtotal * 100;
				totprice = totprice + itemtotal;
				itemlist=itemlist+1;
				
				var totitems = eval(totitems) + eval(thequantity);
		
				
				

document.write('<tr bgcolor=white><td align=center width=15><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=2 onchange="changeItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"></td>');

if (PRICE) { PRICE = cent(PRICE); }


document.write('<td align=left class="body">'+DESC+'</td>');

document.write('<td align=center class="body">'+SIZE+'</td>');


document.write('<td align=center class="body">'+PRICE+'</td>');

document.write('<td align=right bgcolor="#EFECEC" width=20 class="body"><B>\$'+cent(itemtotal)+'</td>');

document.write('<td align=center class="body"><a href="javascript:removeItem('+itemlist+')"><img src="cybershop_inc/images/trash.gif" border=0 width=15 height=21></td></tr>');

theorder = theorder+DESC+'|'+PRICE+'|'+SIZE+'|'+WEIGHT+'|'+thequantity+']';



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

				thisitem++;
				itemstart=i+1;
			}


		}

// DELIVERY = 20 * totitems;

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

// DISCOUNT = 20 * totitems;

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

var DISCOUNT= 0



totprice = (totprice + DELIVERY) - DISCOUNT;

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

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

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


}
	
function changeItem(itemno, newquant) {
		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 + '['+DESC+ '|' +PRICE+'|' +SIZE+'|' +WEIGHT+'|'+newquant+']';
				}
			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) DESC = fulllist.substring(itemstart, i);
				if (thisitem==2) PRICE = fulllist.substring(itemstart, i);
				if (thisitem==3) SIZE = fulllist.substring(itemstart, i);
				if (thisitem==4) WEIGHT = fulllist.substring(itemstart, i);

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

	}

function removeItem(itemno) {
	DESC = 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;
				DESC = 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);
	}
}
 