// JavaScript Document
function OnOffAddress() {
	if (document.OrderForm.same_address.checked) {
		document.OrderForm.Ship_to_First_Name.value=document.OrderForm.Bill_to_First_Name.value;
		document.OrderForm.Ship_to_Last_Name.value=document.OrderForm.Bill_to_Last_Name.value;
		document.OrderForm.Ship_to_Street.value=document.OrderForm.Bill_to_Street.value;
		document.OrderForm.Ship_to_City.value=document.OrderForm.Bill_to_City.value;
		document.OrderForm.Ship_to_State.value=document.OrderForm.Bill_to_State.value;
		document.OrderForm.Ship_to_Zip.value=document.OrderForm.Bill_to_Zip.value;
		document.OrderForm.Ship_to_Phone_AC.value=document.OrderForm.Bill_to_Phone_AC.value;
		document.OrderForm.Ship_to_Phone_Prefix.value=document.OrderForm.Bill_to_Phone_Prefix.value;
		document.OrderForm.Ship_to_Phone_Suffix.value=document.OrderForm.Bill_to_Phone_Suffix.value;
		document.OrderForm.Ship_to_First_Name.disabled=true;
		document.OrderForm.Ship_to_Last_Name.disabled=true;
		document.OrderForm.Ship_to_Street.disabled=true;
		document.OrderForm.Ship_to_City.disabled=true;
		document.OrderForm.Ship_to_State.disabled=true;
		document.OrderForm.Ship_to_Zip.disabled=true;
		document.OrderForm.Ship_to_Phone_AC.disabled=true;
		document.OrderForm.Ship_to_Phone_Prefix.disabled=true;
		document.OrderForm.Ship_to_Phone_Suffix.disabled=true;
	} else {
		document.OrderForm.Ship_to_First_Name.value='';
		document.OrderForm.Ship_to_Last_Name.value='';
		document.OrderForm.Ship_to_Street.value='';
		document.OrderForm.Ship_to_City.value='';
		document.OrderForm.Ship_to_State.value='';
		document.OrderForm.Ship_to_Zip.value='';
		document.OrderForm.Ship_to_Phone_AC.value='';
		document.OrderForm.Ship_to_Phone_Prefix.value='';
		document.OrderForm.Ship_to_Phone_Suffix.value='';
		document.OrderForm.Ship_to_First_Name.disabled=false;
		document.OrderForm.Ship_to_Last_Name.disabled=false;
		document.OrderForm.Ship_to_Street.disabled=false;
		document.OrderForm.Ship_to_City.disabled=false;
		document.OrderForm.Ship_to_State.disabled=false;
		document.OrderForm.Ship_to_Zip.disabled=false;
		document.OrderForm.Ship_to_Phone_AC.disabled=false;
		document.OrderForm.Ship_to_Phone_Prefix.disabled=false;
		document.OrderForm.Ship_to_Phone_Suffix.disabled=false;
	}
}

function CheckAddress() {
	if (document.OrderForm.same_address.checked) {
		document.OrderForm.Ship_to_First_Name.value=document.OrderForm.Bill_to_First_Name.value;
		document.OrderForm.Ship_to_Last_Name.value=document.OrderForm.Bill_to_Last_Name.value;
		document.OrderForm.Ship_to_Street.value=document.OrderForm.Bill_to_Street.value;
		document.OrderForm.Ship_to_City.value=document.OrderForm.Bill_to_City.value;
		document.OrderForm.Ship_to_State.value=document.OrderForm.Bill_to_State.value;
		document.OrderForm.Ship_to_Zip.value=document.OrderForm.Bill_to_Zip.value;
		document.OrderForm.Ship_to_Phone_AC.value=document.OrderForm.Bill_to_Phone_AC.value;
		document.OrderForm.Ship_to_Phone_Prefix.value=document.OrderForm.Bill_to_Phone_Prefix.value;
		document.OrderForm.Ship_to_Phone_Suffix.value=document.OrderForm.Bill_to_Phone_Suffix.value;
		document.OrderForm.Ship_to_First_Name.disabled=true;
		document.OrderForm.Ship_to_Last_Name.disabled=true;
		document.OrderForm.Ship_to_Street.disabled=true;
		document.OrderForm.Ship_to_City.disabled=true;
		document.OrderForm.Ship_to_State.disabled=true;
		document.OrderForm.Ship_to_Zip.disabled=true;
		document.OrderForm.Ship_to_Phone_AC.disabled=true;
		document.OrderForm.Ship_to_Phone_Prefix.disabled=true;
		document.OrderForm.Ship_to_Phone_Suffix.disabled=true;
	}
}

function SetOrderRecord() {
	setCookie ('totalPayment',document.OrderForm.totalPayment.value);
	setCookie ('minPostage',document.OrderForm.minPostage.value);
	setCookie ('orderNumItems',document.OrderForm.orderNumItems.value);
	setCookie ('orderDiffKinds',document.OrderForm.orderDiffKinds.value);
	setCookie ('orderItemsName',document.OrderForm.order_items_name.value);
	setCookie ('orderItemsQty',document.OrderForm.order_items_qty.value);

	var billing = document.OrderForm.Bill_to_First_Name.value + '|' +
			document.OrderForm.Bill_to_Last_Name.value + '|' +
			document.OrderForm.Bill_to_Street.value + '|' +
			document.OrderForm.Bill_to_City.value + '|' +
			document.OrderForm.Bill_to_State.value + '|' +
			document.OrderForm.Bill_to_Zip.value + '|' +
			document.OrderForm.Bill_to_Phone_AC.value + '|' +
			document.OrderForm.Bill_to_Phone_Prefix.value + '|' +
			document.OrderForm.Bill_to_Phone_Suffix.value + '|' +
			document.OrderForm.email.value;
	setCookie ('Billing_Info', billing);

	var shipping = document.OrderForm.Ship_to_First_Name.value + '|' +
			document.OrderForm.Ship_to_Last_Name.value + '|' +
			document.OrderForm.Ship_to_Street.value + '|' +
			document.OrderForm.Ship_to_City.value + '|' +
			document.OrderForm.Ship_to_State.value + '|' +
			document.OrderForm.Ship_to_Zip.value + '|' +
			document.OrderForm.Ship_to_Phone_AC.value + '|' +
			document.OrderForm.Ship_to_Phone_Prefix.value + '|' +
			document.OrderForm.Ship_to_Phone_Suffix.value;
	setCookie ('Shipping_Info', shipping);
}

function ClearOrderRecord() {
	setCookie ('totalPayment', '');
	setCookie ('minPostage', '');
	setCookie ('orderNumItems', '');
	setCookie ('orderDiffKinds', '');
	setCookie ('orderItemsName', '');
	setCookie ('orderItemsQty', '');
	setCookie ('Billing_Info', '');
	setCookie ('Shipping_Info', '');
}

function ChangeQuantity(itemId, qty) {
	if ( isNaN(qty) ) {
      alert("Invalid Quantity.");
	  document.OrderForm['qty'+itemId].value = 0; 
	} else {
		minPostage = 0;
		totalPayment = 0;
		orderNumItems = 0;
		orderDiffKinds = 0;
		itemsName = '';
		itemsQty = '';
		for (i=1; i<=document.OrderForm.numItems.value; i++) {
			if (document.OrderForm['qty'+i].value > 0) {
				orderDiffKinds++;
				orderNumItems += document.OrderForm['qty'+i].value - 0;
				if (document.OrderForm['postage'+i].value > minPostage) {
					minPostage = document.OrderForm['postage'+i].value;
				}
				totalPayment += document.OrderForm['cost'+i].value * document.OrderForm['qty'+i].value;
				itemsName += document.OrderForm['itemName'+i].value + '|';
				itemsQty += document.OrderForm['qty'+i].value + '|';
			}
		}

		document.OrderForm.totalPayment.value = totalPayment;
		document.OrderForm.minPostage.value = minPostage;
		document.OrderForm.orderNumItems.value = orderNumItems;
		document.OrderForm.orderDiffKinds.value = orderDiffKinds;

		document.OrderForm.order_items_name.value = itemsName;
		document.OrderForm.order_items_qty.value = itemsQty;

		// for IE, NS6
 		//document.getElementById('payment').innerHTML = totalPayment;
	}
}

// Flag, Alerting if cookie exceeds 4KB
var caution = false

// name : name of the cookie
// value : value of the cookie
// [expires] : expiration date of cookie (defaults: end of current session)
// [path] : path for which the cookie is valid (default: path of calling document)
// [domain] : domain for which the cookie is valid (default: domain of calling document)
// [secure] : flag, indicating if cookie transmission requires a secure transmission (default: not require)
function setCookie(name, value, path, expires, domain, secure) {
	var curCookie = name + "=" + escape(value) +
						((expires) ? "; expires = " + expires.toGMTString() : "") +
						((path) ? "; path = " + path : "") +
						((domain) ? "; domain = " + domain : "") +
						((secure) ? "; secure" : "")
	if (!caution || (name + "=" + escape(value)).length <= 4000)
		document.cookie = curCookie
	else
		if (confirm("Cookie exceeds 4KB and will be cut!"))
			document.cookie = curCookie
}

// name : name of the desired cookie
// return string containing value of specified cookie, return NULL if cookie dose not exist
function getCookie(name) {
	var prefix = name + "="
	var cookieStartIndex = document.cookie.indexOf(prefix)
	if (cookieStartIndex == -1)
		return null
	
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// name : name of the cookie
// [path] : path of the cookie (must be same as created)
// [domain] : domain of the cookie (must be same as created)
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + 
								((path) ? "; path = " + path : "") +
								((domain) ? "; domain = " + domain : "") +
								 "; expires = Wednesday, 04-Nov-1997 23:00:00 GMT"
	}
}