var gLayer;
var gOrder;
var gItem;
var gCategory;
var gSubCategory;

 function fadeIn() {
   var fadeIt = new Spry.Effect.Fade('shopDisplayLargeItem', {duration: 1000, from: 5, to: 100 });
   fadeIt.start();
 }
 
 function fadeOut(layer, sys_order_id, sys_item_id, sys_category, sys_sub_category) {
	gLayer = layer;
	gOrder = sys_order_id;
	gItem = sys_item_id;
	gCategory = sys_category;
	gSubCategory = sys_sub_category;
	var fadeIt = new Spry.Effect.Fade('shopDisplayLargeItem', {duration: 500, from: 100, to: 5, finish: getProduct });
	fadeIt.start();
	window.scrollTo(0, 200);
 }
 
 function getProduct(elmentObj , FadeObj) {	ajaxViewProduct('shopDisplayLargeItem', gOrder, gItem, gCategory, gSubCategory); }
 function finishFunc(elmentObj , FadeObj) { fadeIn(); }

 function windowOpen(page, title, attributes) {window.open(page, title, attributes);}
 function windowClose() {window.close();}

// function scrollUp() {window.scrollTo(0, 0);}
 function viewProduct(layer, sys_order_id, sys_item_id, sys_category, sys_sub_category) {
	fadeOut(layer, sys_order_id, sys_item_id, sys_category, sys_sub_category);
 }

 function startViewProduct(layer, sys_order_id, sys_item_id, sys_category, sys_sub_category) {
	fadeOut(layer, sys_order_id, sys_item_id, sys_category, sys_sub_category);
//	setTimeout('fadeOut("' + layer + '", "' + sys_order_id + '", ' + sys_item_id + ', "' + sys_category + '", "' + sys_sub_category + '")', 250);
 }

function getProduct(elmentObj , FadeObj) {ajaxViewProduct(gLayer, gOrder, gItem,gCategory, gSubCategory);}
function setupFunc(elmentObj , FadeObj) {}
function finishFunc(elmentObj , FadeObj) {fadeIn();}
function windowOpen(page, title, attributes) {window.open(page, title, attributes);}
function windowClose() {window.close();}
function viewProduct(layer, sys_order_id, sys_item_id) {fadeOut(layer, sys_order_id, sys_item_id);}

function addVouchers(object) {
	var _layer = outpostGetLayer("voucherForm");
	var _number = new Array();
	var _value = new Array();

    var form = window.document.manage;
	if (form.elements['frm_voucher_count']) {
		if (form.frm_voucher_no.value > 0 && form.frm_voucher_no.value != "") {
			for (i =0 ; i < form.frm_voucher_count.value; i++) {
				_value[i] = form.elements["frm_voucher_no_" + i].value;
				_number[i] = form.elements["frm_voucher_value_" + i].value;
			}
			_layer.removeChild(outpostGetLayer("voucherTable"));
		}
	}
	if (form.frm_voucher_no.value > 0 && form.frm_voucher_no.value != "") {
			_div = document.createElement("div");
			_div.setAttribute("id", "voucherTable");
				_table = document.createElement("table");
				_table.setAttribute("width", "284");
				_table.border = 0;
				_table.cellPadding = 0;
				_table.cellSpacing = 0;
					_body = document.createElement("tbody");
									
						_tr = document.createElement("tr");
							_td = document.createElement("td");
							_td.setAttribute("colspan", "1");
									outpostAddText(_td, "h1", "Voucher Number");
						_tr.appendChild(_td);
							_td = document.createElement("td");
							_td.setAttribute("colspan", "1");
								outpostAddText(_td, "p", "");

						_tr.appendChild(_td);
							_td = document.createElement("td");
							_td.setAttribute("colspan", "1");
									outpostAddText(_td, "h1", "Voucher Value");
						_tr.appendChild(_td);
					_body.appendChild(_tr);

					for (i =0 ; i < form.frm_voucher_no.value; i++) {

							_tr = document.createElement("tr");
								_td = document.createElement("td");
								_td.setAttribute("colspan", "1");
									if (_value[i] == undefined) _value[i] = "";
									outpostCreateControl(_td, "text", "frm_voucher_no_" + i, _value[i]);
							_tr.appendChild(_td);
									
								_td = document.createElement("td");
								_td.setAttribute("colspan", "1");
									outpostAddText(_td, "p", "$");
							_tr.appendChild(_td);
							
								_td = document.createElement("td");
								_td.setAttribute("colspan", "1");
									if (_number[i] == undefined) _number[i] = "";
									outpostCreateControl(_td, "text", "frm_voucher_value_" + i, _number[i]);
							_tr.appendChild(_td);
						_body.appendChild(_tr);
					}
					outpostCreateControl(_body, "hidden", "frm_voucher_count",  i);

				_table.appendChild(_body);
			_div.appendChild(_table);
		_layer.appendChild(_div);
	}
}

function ajaxViewProduct(layer, sys_order_id, sys_item_id, sys_category, sys_sub_category) {
var _layer = outpostGetLayer(layer);
var multi = false;

new Ajax.Request(sys_site +"manage/AJAX/getXMLProduct.php", {
	method: 'post',
	asynchronous: false,
	parameters : { sys_order_id: sys_order_id, 
						sys_item_id: sys_item_id, 
						sys_image_width: 200, 
						sys_image_height: 200, 
						sys_category: sys_category, 
						sys_sub_category: sys_sub_category  },
		onSuccess: function(transport) { 
		xmlDoc = transport.responseXML;
		
		sys_item_id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		var title = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
		var price = xmlDoc.getElementsByTagName("price")[0].childNodes[0].nodeValue;

		var image = xmlDoc.getElementsByTagName("image")[0].childNodes[0].nodeValue;
		var thumbnail = xmlDoc.getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
		var content = unescape(xmlDoc.getElementsByTagName("value")[0].childNodes[0].nodeValue);

		var quantity = unescape(xmlDoc.getElementsByTagName("order_quantity")[0].childNodes[0].nodeValue);
		var style = unescape(xmlDoc.getElementsByTagName("order_style")[0].childNodes[0].nodeValue);
		var soldout = unescape(xmlDoc.getElementsByTagName("soldout")[0].childNodes[0].nodeValue);
		var colour = unescape(xmlDoc.getElementsByTagName("order_colour")[0].childNodes[0].nodeValue);
		if (colour == "&nbsp;") colour = "";

		if (outpostGetLayer("frm_shop")) {
			var _child_layer = outpostGetLayer("frm_shop");
			var _child_layer = outpostGetLayer("frm_shop");
			_child_layer.parentNode.removeChild(_child_layer); 
		}
		else
			document.getElementById(layer).innerHTML = "";

		_div = document.createElement("div");
		_div.setAttribute("name", "shop");
		_div.setAttribute("id", "shop");
			_new_form = document.createElement("form");
			_new_form.setAttribute("name", "frm_shop");
			_new_form.setAttribute("id", "frm_shop");

				_table = document.createElement("table");
				_table.setAttribute("width", "384");
				_table.border = 0;
				_table.cellPadding = 0;
				_table.cellSpacing = 0;
					_body = document.createElement("tbody");

						_tr = document.createElement("tr");
							_th = document.createElement("th");
							_th.setAttribute("colspan", "2");
							_th.colSpan = 2;
								outpostAddText(_th, "h1", title);
						_tr.appendChild(_th);
					_body.appendChild(_tr);

						_hold_tr = document.createElement("tr");
							_td = document.createElement("td");
							_td.setAttribute("colspan", "1");
								_sub_img = document.createElement('img');
								_sub_img.setAttribute("id", "outpost_shop_img");
								_sub_img.setAttribute("src", thumbnail);
							_td.appendChild(_sub_img);
						_hold_tr.appendChild(_td);
						
							_hold_td = document.createElement("td");
							_hold_td.setAttribute("colspan", "1");
							_hold_td.setAttribute("valign", "top");
							
										_padding_div = document.createElement("div");
										_padding_div.setAttribute("id", "shopNoPadding");
										
											_new_table = document.createElement("table");
											_new_table.border = 0;
											_new_table.cellPadding = 0;
											_new_table.cellSpacing = 0;
											_new_table.width = 100;
												_new_body = document.createElement("tbody");
												
												if (xmlDoc.getElementsByTagName("pricing").length > 0) {
														multi = true;
														_tr = document.createElement("tr");
															_td = document.createElement("td");
															_td.setAttribute("colspan", "1");
																outpostAddText(_td, "h3", "");
														_tr.appendChild(_td);	
															_td = document.createElement("td");
															_td.setAttribute("width", "50");
															_td.setAttribute("colspan", "1");
																outpostAddText(_td, "h2", "Price");
														_tr.appendChild(_td);
															_td = document.createElement("td");
															_td.setAttribute("colspan", "1");
															_td.setAttribute("align", "center");
																outpostAddText(_td, "h3", "Option");
														_tr.appendChild(_td);
														
													_new_body.appendChild(_tr);
										
														for (i = 0; i < xmlDoc.getElementsByTagName("pricing_style").length; i++ ) {
															_tr = document.createElement("tr");
																_td = document.createElement("td");
																_td.setAttribute("colspan", "1");
																	outpostAddText(_td, "p", xmlDoc.getElementsByTagName("pricing_style")[i].childNodes[0].nodeValue);
															_tr.appendChild(_td);
															
																_td = document.createElement("td");
																_td.setAttribute("colspan", "1");
																	outpostAddText(_td, "p", "$" + xmlDoc.getElementsByTagName("pricing_value")[i].childNodes[0].nodeValue);
															_tr.appendChild(_td);

																_td = document.createElement("td");
																_td.setAttribute("colspan", "1");
																_td.setAttribute("align", "center");
																	_no_border = document.createElement("div");
																	_no_border.setAttribute("name", "shopnoborder");
																	_no_border.setAttribute("id", "shopnoborder");
																		_select = outpostCreateControl(_no_border, "radio", "frm_price", xmlDoc.getElementsByTagName("pricing_value")[i].childNodes[0].nodeValue, "");
																		_select.setAttribute("id", xmlDoc.getElementsByTagName("pricing_style")[i].childNodes[0].nodeValue);
																		_select.setAttribute('border','1px solid red','false');
																		if (style == xmlDoc.getElementsByTagName("pricing_style")[i].childNodes[0].nodeValue) _select.setAttribute("checked", "checked");
																		_select.onclick = function() {
																			form = outpostGetLayer("frm_shop");
																			if (form.frm_quantity.value == "" || form.frm_quantity.value == "0") form.frm_quantity.value = 1;
																			var value = this.value * form.frm_quantity.value;
																			form.frm_value.value = "$" + value;
																		};	
																	_td.appendChild(_no_border);
																_tr.appendChild(_td);
															_new_body.appendChild(_tr);
														}
														
													}
													else {
															_tr = document.createElement("tr");
																_td = document.createElement("td");
																_td.setAttribute("colspan", "1");
																	outpostAddText(_td, "h2", "Price");
															_tr.appendChild(_td);
															
																_td = document.createElement("td");
																_td.setAttribute("colspan", "2");
																_td.colSpan = 2;
																	outpostAddText(_td, "p", "$" + price);
																	outpostCreateControl(_td, "hidden", "frm_price", price);
															_tr.appendChild(_td);
													}
											_new_body.appendChild(_tr);
															
											if (xmlDoc.getElementsByTagName("colour").length) {
													_tr = document.createElement("tr");
														_td = document.createElement("td");
															_td.setAttribute("colspan", "1");
															_td.setAttribute("valign", "top");
															outpostAddText(_td, "h2", "Style");
													_tr.appendChild(_td);
													
														_td = document.createElement("td");
															_td.setAttribute("colspan", "2");
															_td.colSpan = 2;
															_td.setAttribute("valign", "top");
																_menu_select = document.createElement("select");
																_menu_select.setAttribute("name", "frm_colours");
																_menu_select.setAttribute("id", "frm_colours");
																	_option=document.createElement("OPTION");
																		_text=document.createTextNode("[SELECT ONE]");
																		
																	_option.appendChild(_text);
																	_option.setAttribute("value", -1);
																_menu_select.appendChild(_option);

																	for (var i=0; i < xmlDoc.getElementsByTagName("colour").length; i++) {
																		_option=document.createElement("OPTION");
																			_text=document.createTextNode(unescape(xmlDoc.getElementsByTagName("colour")[i].childNodes[0].nodeValue));
																		_option.appendChild(_text);
																		_option.setAttribute("value", xmlDoc.getElementsByTagName("colour")[i].childNodes[0].nodeValue);
																		if (colour == xmlDoc.getElementsByTagName("colour")[i].childNodes[0].nodeValue) _option.setAttribute("selected", "selected");
																		_menu_select.appendChild(_option);
																	}
																
															_td.appendChild(_menu_select);
													_tr.appendChild(_td);
												_new_body.appendChild(_tr);
											}
						
													_tr = document.createElement("tr");
														_td = document.createElement("td");
														_td.setAttribute("colspan", "1");
															outpostAddText(_td, "h2", "Quantity");
													_tr.appendChild(_td);
													
														if (quantity == 0 || quantity == "") quantity = 1;
														_td = document.createElement("td");
														_td.setAttribute("colspan", "2");
														_td.colSpan = 2;
															_quantity = outpostCreateControl(_td, "text", "frm_quantity", quantity, "100");
															_quantity.onkeyup = function() {
																form = outpostGetLayer("frm_shop");
																if (form.sys_sold_out.value != "Y") {
																	if (multi) {
																		for (i =0; i < form.frm_price.length; i++) {
																			if (form.frm_price[i].checked) {
																				var value = this.value * form.frm_price[i].value;
																				form.frm_value.value = "$" + value;
																				form.frm_show_value.value = "$" + value;
																			}
																		}
																	}
																	else {
																		var value = this.value * form.frm_price.value;
																		form.frm_value.value = "$" + value;
																		form.frm_show_value.value = "$" + value;
																	}
																}
																else {
																		alert("Sorry, but this product is soldout.");
																}
															};	
													_tr.appendChild(_td);
												_new_body.appendChild(_tr);
							

										if (soldout == "Y") {
												_tr = document.createElement("tr");												
													_td = document.createElement("td");
													_td.setAttribute("colspan", "3");
													_td.colSpan = 3;
															outpostAddText(_td, "h3", "Sorry but this product is soldout.");

												_tr.appendChild(_td);
											_new_body.appendChild(_tr);
										}
										else {
													_tr = document.createElement("tr");
														_td = document.createElement("td");
														_td.setAttribute("colspan", "1");
															outpostAddText(_td, "h2", "Value");
													_tr.appendChild(_td);
													
														_td = document.createElement("td");
														_td.setAttribute("colspan", "2");
														_td.colSpan = 2;
															outpostCreateControl(_td, "hidden", "frm_value", "");
															outpostCreateControl(_td, "text", "frm_show_value", "");
													_tr.appendChild(_td);
												_new_body.appendChild(_tr);
										}
												_tr = document.createElement("tr");
														_td = document.createElement("td");
														_td.setAttribute("align", "center");
														_td.setAttribute("colspan", "3");
														_td.colSpan = 3;
															_a_div = document.createElement("div");
															_a_div.setAttribute("id", "outpostButton");
																_a = document.createElement("a");
																_a.setAttribute('href','javascript:ajaxPurchaseItem();');
																	_text=document.createTextNode("BUY ME");
																_a.appendChild(_text);
															_a_div.appendChild(_a);
														_td.appendChild(_a_div);
										
														outpostCreateControl(_td, "hidden", "sys_item_id", sys_item_id);
														outpostCreateControl(_td, "hidden", "sys_order_id", sys_order_id);
														outpostCreateControl(_td, "hidden", "sys_sold_out", soldout);

												_tr.appendChild(_td);
											_new_body.appendChild(_tr);

										_new_table.appendChild(_new_body);
										_padding_div.appendChild(_new_table);
							_hold_td.appendChild(_padding_div);
						_hold_tr.appendChild(_hold_td);
					_body.appendChild(_hold_tr);
						
						_tr = document.createElement("tr");
							_td = document.createElement("td");
							_td.setAttribute("colspan", "2");
							_td.colSpan = 2;
							_td.setAttribute("valign", "top");
								outpostAddText(_td, "div", content);
						_tr.appendChild(_td);
					_body.appendChild(_tr);

				_table.appendChild(_body);
					
				_new_form.appendChild(_table);
			_div.appendChild(_new_form);
		_layer.appendChild(_div);	

		form = outpostGetLayer("frm_shop");
		if (soldout != "Y") {
			if (multi) {
				for (i=0; i < form.frm_price.length; i++) {
					if (form.frm_price[i].checked) {
						var value = form.frm_quantity.value * form.frm_price[i].value;
						form.frm_value.value = "$" + value;
						form.frm_show_value.value = "$" + value;
					}
				}
			}
			else {
				var value = form.frm_quantity.value * form.frm_price.value;
				form.frm_value.value = "$" + value;
				form.frm_show_value.value = "$" + value;
			}
		}

		fadeIn();
	},
	onFailure: function(transport, e) { alert("ajaxViewProduct :" + e); },
	onException: function(transport, e) { alert("ajaxViewProduct :" + e); }
});
}


function ajaxPurchaseItem() {
	form = outpostGetLayer("frm_shop");
	if (form.sys_sold_out.value == "Y") {
		alert("Sorry, but this product is soldout.");
		return;
	}
	
	var quantity = form.frm_quantity.value;
	var value = form.frm_value.value;
	var sys_item_id = form.sys_item_id.value;
	var sys_order_id = form.sys_order_id.value;
	var price = value / quantity;
	var colour = "";
	var style = "";

	for (i =0; i < form.frm_price.length; i++) if (form.frm_price[i].checked) style = form.frm_price[i].id;

	if (form.elements['frm_colours']) {
		colour = form.frm_colours.options[form.frm_colours.selectedIndex].value;
		if (colour == -1) {
			alert("Please select a colour.");
			return;
		}
	}
	if (quantity == "") {
		alert("Please enter a quantity.");
		return;
	}
	if (value == "") {
		alert("Please make sure your selection is correct.");
		return;
	}
	
	var _layer = outpostGetLayer("shopMessage");
	outpostShowHide(_layer, "");
	_layer.innerHTML = "<h1>Shopping Basket Updated</h2><p>Item has been added to your shopping basket.</p><a href='javascript:closeMessage();'>continue ></a>";

	new Ajax.Request(sys_site +"manage/AJAX/removeFromBasket.php", {
		method: 'post',
		asynchronous: false,
		parameters : {  sys_order_id: sys_order_id, 
						sys_item_id: sys_item_id },
		onSuccess: function(transport) { },
		onFailure: function(transport, e) { alert("ajaxViewProduct :" + e); },
		onException: function(transport, e) { alert("ajaxViewProduct :" + e); }
	});


	new Ajax.Updater("outpostShopBasket" , sys_site +"manage/AJAX/updateBasket.php", {
		method: 'post',
		asynchronous: false,
		evalScripts: true,
		parameters : {  sys_order_id: sys_order_id, 
						sys_item_id: sys_item_id, 
						sys_quantity: quantity, 
						sys_value: value, 
						sys_price: price, 
						sys_colour: colour,
						sys_style: style,
						sys_template_category: "Shop Basket Templates",
						sys_template_header: "Basket Header",
						sys_template_line: "Basket Line"  }
	});
	
}

function closeMessage() {
	var _layer = outpostGetLayer("shopMessage");
	outpostShowHide(_layer, "hide");
}	
	
	
function outpostCreateControl(_tag, type, control, value, width) {

	if (type != "radio") {
		_control=document.createElement("input");
		_control.setAttribute("type", type);
		if (type == "textreadonly") {
			_control.setAttribute("type", "text");
			_control.setAttribute("readonly", "readonly");
		}
		_control.setAttribute("id", control);
		_control.setAttribute("name", control);
		_control.setAttribute("value", value);
	}
	else {
		var _object = "<input type='radio' id='" +control+ "' name='" +control+ "' value='" +value+ "'/>";
		try { _control=document.createElement(_object); }
		catch(err) {
			_control=document.createElement("input");
			_control.setAttribute("type", type);
			_control.setAttribute("id", control);
			_control.setAttribute("name", control);
			_control.setAttribute("value", value);
		} 
	}
	if (type == "checkbox" && value == 1) _control.setAttribute("checked", "checked");
	if (type == "checkbox" && value == "Y") _control.setAttribute("checked", "checked");
	_tag.appendChild(_control);
	return _control;
}

function outpostAddText(_object, type, value, id) {
	switch (type) {
		case "h1":
			_h1 = document.createElement("h1");
					_text = document.createTextNode(value);
				_h1.appendChild(_text);
			_object.appendChild(_h1);
			break;
		case "h2":
			_h2 = document.createElement("h2");
					_text = document.createTextNode(value);
				_h2.appendChild(_text);
			_object.appendChild(_h2);
			break;
		case "h3":
			_h3 = document.createElement("h3");
					_text = document.createTextNode(value);
				_h3.appendChild(_text);
			_object.appendChild(_h3);
			break;
		case "div":
			  var _div = document.createElement('div');
			  _div.width = "95%";
			  _div.setAttribute('id', "showDisplayText");
			  _div.innerHTML = value;

			_object.appendChild(_div);
			break;
		case "field":
			  var _div = document.createElement('div');
			  _div.width = "95%";
			  _div.setAttribute('id', id);
			  _div.innerHTML = value;

			_object.appendChild(_div);
			break;
		case "p":
		default:
			_p = document.createElement("p");
					_text = document.createTextNode(value);
				_p.appendChild(_text);
			_object.appendChild(_p);
	}
}
