// -------------------------------------------------------------------------
// JavaScript 1.1: Online Ordering System Part III
// Author: All rights reserved, Copyright 2004, Chris Colefax <ccolefax@yahoo.com>
// This script, or any part thereof, may not be modified or used without
// express permission from the author
// -------------------------------------------------------------------------
// Requires common script


var Step2OrderingPage = 'ordering2.html';
var SecureOrderPage = 'https://wic042u.server-secure.com//vs31181_secure/ordering3.html';
var ThankYouPage = 'http://www.sandravidendesign.com/thankyou.html';


var PostageType = (CurCurrency > 0 ? 1 : 0);


function LoadItems() {
	var i = 0;

	var SS = document.cookie.indexOf('OrderDetails=');
	if (SS > -1) {
		SS = document.cookie.indexOf('~P', SS) + 2;
		PostageType = parseInt(document.cookie.substring(SS, SS+1));
		}

	for(var itmCookieNum = 0; itmCookieNum < MaxItems; itmCookieNum++) {
		SS = document.cookie.indexOf('OrderItem' + itmCookieNum + '=');
		if (SS != -1) {
			SS = document.cookie.indexOf('(', SS) + 1;
			ES = document.cookie.indexOf(')', SS);
			itmCookie = document.cookie.substring(SS, ES);

			Items[i] = new Item();
			SS = 0; ES = itmCookie.indexOf('~', SS);		Items[i].code = unescape(itmCookie.substring(SS, ES));
			SS = ES + 1; ES = itmCookie.indexOf('~', SS); Items[i].colour = unescape(itmCookie.substring(SS, ES));
			SS = ES + 1; ES = itmCookie.indexOf('~', SS); Items[i].size = unescape(itmCookie.substring(SS, ES));
			SS = ES + 1; ES = itmCookie.indexOf('~', SS); Items[i].description = unescape(itmCookie.substring(SS, ES));
			SS = ES + 1; ES = itmCookie.indexOf('~', SS); Items[i].price = parseFloat(unescape(itmCookie.substring(SS, ES)));
			SS = ES + 1; ES = itmCookie.indexOf('~', SS); Items[i].weight = unescape(itmCookie.substring(SS, ES));
			SS = ES + 1; ES = itmCookie.indexOf('~', SS); Items[i].pagefile = unescape(itmCookie.substring(SS, ES));
			SS = ES + 2;											 Items[i].quantity = parseInt(unescape(itmCookie.substring(SS)));
			Items[i].cookiename = 'OrderItem' + itmCookieNum;
			i++;
			}
		}

	return true;
	}


function ListOrder(Step) {
	var i = 0, OrderWeight = 0, OrderPostage = 0, OrderCost = 0;

	// Load currently ordered items from cookies
	LoadItems();

	document.open();
	document.writeln('<blockquote><blockquote>');

	if (Step == 2) { document.writeln('<input type="hidden" name="Number Of Items" value="' + Items.length + '">'); }

	// List ordered items
	if (Items.length == 0) {
		document.writeln('<p>Here at Sandra Viden Design we offer quick and easy Online Ordering, using a <b>secure server</b> to protect your identity and credit card details.</p>');
		document.writeln('<p>To use our point-and-click ordering system simply browse through the Catalogue to find the items you wish to order, and click the <b>Add to Basket</b> button.  For each item you can choose your preferred colours, sizes, and quantities. To view, edit, or complete and send your order, click on the <b>View My Items</b> (shopping bag icon) located on the product pages.</p>');
		document.writeln('<p>To begin ordering, simply click <a href="javascript:window.close();" >&otimes;&nbsp;Close</a> to return to the product pages.</p>');
		document.writeln('<div style="clear: both; height: 25px;"></div>');
		document.writeln('<div style="margin-left: 180px; padding-bottom: 60px;"><p><big><i>&quot;Thank you for visiting Sandra Viden Design online!&quot;</i></big><br /> &nbsp; Sandra Viden</p></div>');
		document.writeln('<div style="clear: both; height: 10px;"></div>');
	} else {
		if (Step == 1) {
			document.writeln('<p>To <b>change the quantity</b> of an item simply enter the new quantity you want to order and click on the <i>Update My Basket</i> button.  You can <b>remove an item</b> altogether by entering a quantity of zero.  To <b>remove all the items</b> from your order click on the <i>Empty My Basket</i> button.</p>');
			document.writeln('<p>When you are ready to send your order to us using our <b>secure server</b> click on the <i>Next</i> button to continue.</p>');
			document.writeln('<p>Postage and handling charges shown are estimated based on the items you have selected, and may be subject to change.  You can select (below) whether the freight will be for postage within Australia or Airmail overseas.  The exact postage and handling costs will be charged with your order.  All prices include Goods and Services Tax.</p>');
			document.writeln('<div class="spacer"></div>');
			document.writeln('<form name="OrderForm">');
			}

		document.writeln('<table width="100%" border="0" cellpadding="2" cellspacing="2" id="main-tbl">');
		document.writeln('	<tr><th>Description</th> <th>Size</th> <th>Colour</th> <th>Quantity</th> <th>Price AUD$</th> <th>Cost ' + Currencies[CurCurrency].prefix + Currencies[CurCurrency].suffix + '</th></tr>');

		for(i = 0; i < Items.length; i++) {
			itmSubCost = Items[i].quantity * Items[i].price;
			OrderWeight += Items[i].quantity * Items[i].weight;
			OrderCost += itmSubCost;

			if (i % 2 == 1) { document.writeln('	<tr bgcolor="#CFCFE3" class="main">'); } else { document.writeln('	<tr bgcolor="#E6E6E6" class="main">'); }
			document.writeln('		<td align="left"><b>' + Items[i].code + '</b> ~ ' + Items[i].description + '</td>');
			document.writeln('		<td align="center">' + Items[i].size + '</td>');
			document.writeln('		<td align="left"><img src="images/' + ItemColours[Items[i].colour].swatch + '" border="0" hspace="0" vspace="0" align="left"> &nbsp; ' + ItemColours[Items[i].colour].description + '</td>');

			if (Step == 1) {
				document.writeln('		<td align="center"><input type="text" size="2" name="Quantity' + i + '" value="' + Items[i].quantity + '" onChange="UpdateQuantity(' + i + ')"></td>');
			} else {
				document.writeln('		<td align="center"><input type="hidden" name="Order Item ' + (i+1) + '" value="' + Items[i].code + ' ~ ' + Items[i].description + ' ~ ' + ItemColours[Items[i].colour].description + ' ~ Size ' + Items[i].size + ' ~ ' + Items[i].quantity + ' @ $' + FormatNumber(Items[i].price, 2) + ' = $' + FormatNumber(itmSubCost, 2) + '">' + Items[i].quantity + '</td>');
				}				
			document.writeln('		<td align="right">$' + FormatNumber(Items[i].price, 2) + '</td>');
			document.writeln('		<td align="right"><b>' + FormatNumber(itmSubCost * Currencies[CurCurrency].rate, Currencies[CurCurrency].decimals) + '</b></td>');
			document.writeln('	</tr>');
			}

			if (PostageType == 0) {  // Postage within Australia
				 if (Items.length == 1 && OrderWeight == 0) {
					 OrderPostage = 0.0;
				 } else if (Items.length == 1 && OrderWeight < 0.5) {
					 OrderPostage = 10;
				 } else if (OrderWeight < 3.0) {
					 OrderPostage = 15;
				 } else {
					 OrderPostage = 6.70 + Math.ceil(OrderWeight) * 1.70;
				 }
			} else {  // Airmail to North America
				 if (OrderWeight <= 2) {
					 OrderPostage = 2 + (4.75 * Math.ceil(OrderWeight/0.25));
				 } else {
					 OrderPostage = 40 + (6 * Math.ceil((OrderWeight - 2)/0.5));
				 }
				 OrderPostage += 2;  // Packaging
				 OrderPostage += (5 * Math.ceil(OrderPostage / 100));  // Insurance
			}


		document.writeln('	<tr><td colspan="5" align="right">Subtotal</font></td> <td align="right" bgcolor="#62719C"><nobr>' + FormatNumber(OrderCost * Currencies[CurCurrency].rate, Currencies[CurCurrency].decimals) + '</nobr></td></tr>');
		document.writeln('	<tr><td colspan="5" align="right">Estimated Postage and Handling Charges ');

		if (Step == 1) {
			document.writeln('<select name="PostageType" size="1" onChange="UpdatePostageType()">');
			if (PostageType == 0) {
				document.writeln(' <option selected="selected">Within Australia</option> <option>Airmail Outside Australia</option></select>');
			} else {
				document.writeln(' <option>Within Australia</option> <option selected="selected">Airmail Outside Australia</option></select>');
				}
		} else {
			if (PostageType == 0) { document.writeln('(within Australia)'); }
				else { document.writeln('(Airmail Outside Australia)'); }
			}

		document.writeln('</font></td> <td align="right"><nobr>');
			if (Step == 2) { document.writeln('<input type="hidden" name="Estimated Postage Charges" value="$' + FormatNumber(OrderPostage, 2) + '">'); }
			document.writeln(FormatNumber(OrderPostage * Currencies[CurCurrency].rate, Currencies[CurCurrency].decimals) + '</nobr></td></tr>');

		document.writeln('	<tr><td colspan="5" align="right"><b>Total ' + Currencies[CurCurrency].prefix + Currencies[CurCurrency].suffix + '</b>');
//			document.writeln('<br />[<a href="currencies.html" target="SandraVidenCurrency" onclick="window.open(\'\', \'SandraVidenCurrency\', \'width=300,height=340,menubar=0,resizable=1\');">Change Currency</a>]');
			document.writeln('</td> <td align="right" bgcolor="#62719C" id="total">');
			if (Step == 2) {
				document.writeln('<input type="hidden" name="Total Cost" value="$' + FormatNumber(OrderPostage + OrderCost, 2) + '" />');
				document.writeln('<input type="hidden" name="Total Cost to Customer" value="' + Currencies[CurCurrency].prefix + FormatNumber((OrderPostage + OrderCost) * Currencies[CurCurrency].rate, Currencies[CurCurrency].decimals) + Currencies[CurCurrency].suffix + '" />');
				}
			document.writeln('<nobr><b>' + FormatNumber((OrderPostage + OrderCost) * Currencies[CurCurrency].rate, Currencies[CurCurrency].decimals) + '</b></nobr></td></tr>');

		document.writeln('</table>');

		if (Step == 1) {
			document.writeln('<br>');
			document.writeln('<center><table width="60%" border="0"><tr>');
			document.writeln('	<td align="left">[<a href="" onclick="self.location=self.location; return false;">Update My Basket</a>]');
			document.writeln('		&nbsp; &nbsp; [<a href="" onclick="EraseOrder(); return false;">Empty My Basket</a>] &nbsp; &nbsp; [<a href="" class="continue" onclick="javascript:window.close();;">Continue Shopping</a>]</td>');
			document.writeln('	<td align="right">[<a href="" onclick="GotoOrder2(); return false;" class="next">Next &gt;</a>]</td>');
			document.writeln('</tr></table></center>');
			document.writeln('</form>');
			}
		}

	document.writeln('</blockquote></blockquote>');
	document.close();

	return true;
	}


function UpdatePostageType() {
	document.cookie = 'OrderDetails=~P' + document.OrderForm.PostageType.selectedIndex + ';PATH=/';
	self.location = self.location;
	}


function UpdateQuantity(i) {
	var newQuantity = parseInt(document.OrderForm['Quantity' + i].value);
	if (newQuantity > 0) {
		Items[i].quantity = newQuantity;
		SaveItem(i);
		self.location = self.location;
	} else {
		if (confirm('Are you sure you want to remove "' + Items[i].description + '" from your order?')) {
			DeleteCookie(Items[i].cookiename);
			Items.length = 0;
			self.location = self.location;
		} else {
			document.OrderForm['Quantity' + i].value = Items[i].quantity;
			}
		}
	}


function EraseOrder() {
	if (confirm('Are you sure you want to erase all the items in your order?')) {
		ClearOrderCookies();
		}
	}


function ClearOrderCookies() {
	for(var itmCookieNum = 0; itmCookieNum < MaxItems; itmCookieNum++) {
		DeleteCookie('OrderItem' + itmCookieNum);
		}
	Items.length = 0;
	self.location = self.location;
	}


function GotoOrder2() {
	self.location = Step2OrderingPage;
	}


function GotoOrder3() {
	if (document.OrderForm['Number Of Items'].value == '0') {
		alert('You have not selected any items to buy!');
		return false;
	} else { if (document.OrderForm.realname.value == '') {
		alert('Please enter your name');
		document.OrderForm.realname.focus();
		return false;
	} else { if (document.OrderForm.Address1.value == '') {
		alert('Please enter your address');
		document.OrderForm.Address1.focus();
		return false;
	} else { if (document.OrderForm.Phone1.value == '') {
		alert('Please enter your Phone number');
		document.OrderForm.Phone1.focus();
		return false;
	} else { if (document.OrderForm.email.value == '') {
		alert('Please enter your email address');
		document.OrderForm.email.focus();
		return false;
	} else { if (document.OrderForm.Payment[0].checked == false && document.OrderForm.Payment[1].checked == false && document.OrderForm.Payment[2].checked == false) {
		alert('Please select your payment method');
		document.OrderForm.Payment[0].focus();
		return false;
	} else {

		if (document.OrderForm.Payment[0].checked) {
			document.OrderForm.redirect.value = SecureOrderPage;
		} else {
			document.OrderForm.redirect.value = ThankYouPage;
			}

		ClearOrderCookies();
		document.OrderForm.submit();
		return true;
	} } } } } }	}

