var ns4 = document.layers;
var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1)||(navigator.userAgent.indexOf("Opera/5")!=-1);
var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1);
var agt=navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);
var ie = (agt.indexOf("msie") != -1); 
var mac_ie = mac && ie;

if(ie){
	var xmlRequest = createRequest();
	var xmlInfoGroup, xmlInfoAttribute, xmlInfoIsGroup, xmlInfoFormType, xmlInfoProduct;
}else{
	var ie = false;
}

var imageWin = null;

/* added by Ardent Creative */
	function startPleaseWait(id, num){
		if(ie){
			disableForm();
		}
		removeSpans(id);
		var loc = document.getElementById(id);
		var num_text = "";
		if(num > 3 || !num){
			num = 0;
		}
		for(i = 0; i < num; i++){
			num_text += ".";
		}
		for(i = 0; i < loc.childNodes.length; i++){
			loc.childNodes[i].deleteData(0, loc.childNodes[i].length);
		}
		var please_wait_text = "Please Wait" + num_text;
		if(!loc.childNodes[0]){
			var text = document.createTextNode(please_wait_text);
			loc.appendChild(text);
		}else{
			loc.childNodes[0].nodeValue = please_wait_text;
		}
		var timeout_string = "startPleaseWait('" + id + "', " + (num+1) + ")";
		loc.please_wait_timeout = setTimeout(timeout_string, 250);
	}
	function removeSpans(id){
		var loc = document.getElementById(id);
		for(i = 0; i < loc.childNodes.length; i++){
			var cur = loc.childNodes[i];
			if(cur.nodeName.toLowerCase() == "span"){
				loc.removeChild(cur);
			}
		}
	}
	function setInfoError(id, error){
		var loc = document.getElementById(id);
		for(i = 0; i < loc.childNodes.length; i++){
			loc.childNodes[i].deleteData(0, loc.childNodes[i].length);
		}
		var error_text = "ERROR: " + error + "!";
		var spanElement = document.createElement("span");
		var text = document.createTextNode(error_text);
		spanElement.appendChild(text);
		loc.appendChild(spanElement);
		spanElement.style.textTransform = "uppercase";
		spanElement.style.color = "red";
	}
	function stopPleaseWait(id){
		if(ie){
			enableForm();
		}
		var loc = document.getElementById(id);
		for(i = 0; i < loc.childNodes.length; i++){
			loc.childNodes[i].deleteData(0, loc.childNodes[i].length);
		}
		clearTimeout(loc.please_wait_timeout);
	}
	function createRequest(){
		var request = false;
		try{
			request = new XMLHttpRequest();
		}catch(trymicrosoft){
			try{
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(othermicrosoft){
				try{
					request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(failed){
					request = false;
				}
			}
		}
		
		if(!request){
			alert("ERROR: XMLHttpRequest!");
		}	
		
		return request;
	}
	
	function updatePage() {
		if(!ie){
			var request = this.request;
			var formtype = request.formtype;
			var attr = request.attr;
			var prod = request.prod;
			var grp = request.grp;
			var isGroup = request.isGroup;
		}else{
			var request = xmlRequest;
			var formtype = xmlInfoFormType;
			var attr = xmlInfoAttribute;
			var prod = xmlInfoProduct;
			var grp = xmlInfoGroup;
			var isGroup = xmlInfoIsGroup;
		}
		if(request.readyState == 4){
			if(formtype == "item"){
				stopPleaseWait("attribute_" + attr + "_status");
			}else if(formtype == "group"){
				stopPleaseWait("group_status");
			}
			if(request.status == 200){
				if(formtype.toLowerCase() == "item"){
					document.getElementById("attribute_input_" + attr).disabled = false;
					if(isGroup){
						document.getElementById("plus_button_input").disabled = false;
						document.getElementById("minus_button_input").disabled = false;
					}
					createAttributeItems(attr, request, formtype, isGroup);
				}else if(formtype.toLowerCase() == "group"){
					document.getElementById("plus_button_input").disabled = false;
					document.getElementById("minus_button_input").disabled = false;
					createAttributeItems(grp, request, formtype, isGroup);
				}
			}else{
				if(formtype == "item"){
					setInfoError("attribute_" + attr + "_status", request.status);
				}else if(formtype == "group"){
					setInfoError("group_status", request.status);
				}
			}
		}else{
		}
	}
	
	function getXMLInfo(formtype, grp, prod, attr, isGroup){
		var url = "content/public/public_xml.php?productID=" + escape(prod) + "&groupOrder=" + escape(grp) + "&paid=" + escape(attr);
		if(attr){
			var loc = document.getElementById("attribute_" + attr + "_status");
			var waitLoc = "attribute_" + attr + "_status";
			var disabledItem = document.getElementById("attribute_input_" + attr);
			if(isGroup){
				var disabledItem2 = document.getElementById("plus_button_input");
				var disabledItem3 = document.getElementById("minus_button_input");
			}
		}else{
			var loc = document.getElementById("group_status");
			var waitLoc = "group_status";
			var disabledItem = document.getElementById("plus_button_input");
			var disabledItem2 = document.getElementById("minus_button_input");
		}
		if(!loc.request || formtype == "group"){
			startPleaseWait(waitLoc);
			disabledItem.disabled = true;
			if(disabledItem2){
				disabledItem2.disabled = true;
			}
			if(disabledItem3){
				disabledItem3.disabled = true;
			}
			if(ie){
				xmlInfoGroup = grp;
				xmlInfoProduct = prod;
				xmlInfoFormType = formtype;
				xmlInfoIsGroup = isGroup;
				xmlInfoAttribute = attr;
				xmlRequest.open("GET", url, true);
				xmlRequest.onreadystatechange = updatePage;
				xmlRequest.send(null);
			}else{
				var request = createRequest();
				loc.request = request;
				loc.request.open("GET", url, true);
				loc.request.grp = grp;
				loc.request.attr = attr;
				loc.request.prod = prod;
				loc.request.isGroup = isGroup;
				loc.request.request = request;
				loc.request.formtype = formtype;
				loc.request.onreadystatechange = updatePage;
				loc.request.send(null);
			}
		}else{
			if(formtype.toLowerCase() == "item"){
				createAttributeItems(attr, loc.request, formtype, isGroup);
			}else if(formtype.toLowerCase() == "group"){
				createAttributeItems(grp, loc.request, formtype, isGroup);
			}
		}
	}
	
	function disableForm(){
		var i;
		var formElements = document.forms["frmAddItem"].elements;
		for(i = 0; i < formElements.length; i++){
			formElements[i].disabled = true;
		}
	}
	
	function enableForm(){
		var i;
		var formElements = document.forms["frmAddItem"].elements;
		for(i = 0; i < formElements.length; i++){
			formElements[i].disabled = false;
		}
	}
	
	function removeGroup(grp){
		var loc = document.getElementById("group_data");
		var nodes = loc.getElementsByTagName("div");
		for(i = 0; i < nodes.length; i++){
			var node = nodes[i];
			if("group_" + grp == node.getAttribute("id")){
				loc.removeChild(node);
			}
		}
	}
	
	function createAttributeItems(id, request, formtype, isGroup){
		var i, c, j, k, h, l, n, m, o;
		if(formtype == "group"){
			var groupData = document.getElementById("group_data");
			var groupDiv = document.createElement("div");
			groupDiv.setAttribute("id", "group_" + id);
			groupDiv.style.borderTop = "3px dashed #91A5DA";
			groupDiv.style.paddingBottom = "7px";
		}
		var xmlDoc = request.responseXML;
		if(ie){
			var attributes = xmlDoc.childNodes[1];
		}else{
			var attributes = xmlDoc.childNodes[0];
		}
		if(formtype == "item"){
			removeAttributeItems(id, 0);
		}
		//var brk = false;
		// Build optionNameArray
		var optionNameArray = new Array();
		for(n = 0; n < attributes.childNodes.length; n++){
			var attributeN = attributes.childNodes[n];
			if(attributeN.nodeName.toLowerCase() == "attribute"){
				if(attributeN.getElementsByTagName("show_hide").length){
					for(m = 0; m < attributeN.getElementsByTagName("show_hide")[0].childNodes.length; m++){
						var optionM = attributeN.getElementsByTagName("show_hide")[0].childNodes[m];
						if(optionM.nodeName == "option"){
							optionNameArray.push(optionM.getElementsByTagName("name")[0].childNodes[0].nodeValue);
						}
					}
				}
			}
		}
		// XML attribute Node
		for(i = 0; i < attributes.childNodes.length; i++){
			var attribute = attributes.childNodes[i];
			if(attribute.nodeName.toLowerCase() == "attribute"){
				// XML show_hide Node
				for(c = 0; c < attribute.childNodes.length; c++){
					var show_hide = attribute.childNodes[c];
					if(show_hide.nodeName.toLowerCase() == "show_hide"){
						// XML option Node
						for(j = 0; j < show_hide.childNodes.length; j++){
							var option = show_hide.childNodes[j];
							// XML name and show_hide Nodes
							for(k = 0; k < option.childNodes.length; k++){
								var optionNode = option.childNodes[k];
								// XML name Node
								if(optionNode.nodeName.toLowerCase() == "name"){
									var optionName = optionNode.childNodes[0].nodeValue;
								}
								// XML show_hide Node
								if(optionNode.nodeName.toLowerCase() == "show_hide"){
									// XML option Node
									for(h = 0; h < optionNode.childNodes.length; h++){
										var valueNode = optionNode.childNodes[h];
										if(valueNode.nodeName.toLowerCase() == "option"){
											if(formtype == "group" || (formtype == "item" && unescape(document.getElementById("attribute_input_" + id).value) == valueNode.childNodes[0].nodeValue.replace(/ /g, "+"))){
												// XML top attribute Node
												for(l = 0; l < attributes.childNodes.length; l++){
													var attribute2 = attributes.childNodes[l];
													if(attribute2.nodeName.toLowerCase() == "attribute"){
														if(attribute2.getAttribute("name") == optionName){
															if(formtype == "item"){
																createAttributeItem(id, attribute2, formtype, isGroup);
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
				if(formtype == "group"){
					if(optionNameArray.length == 0){
						groupDiv.appendChild(createAttributeItem(attribute.getAttribute("paid"), attribute, formtype, isGroup));
					}else{
						var addAttr = true;
						for(o = 0; o < optionNameArray.length; o++){
							if(attribute.getAttribute("name") == optionNameArray[o]){
								addAttr = false;
							}
						}
						if(addAttr){
							groupDiv.appendChild(createAttributeItem(attribute.getAttribute("paid"), attribute, formtype, isGroup));
						}
					}
				}
			}
		}
		
		if(formtype == "group"){
			groupData.appendChild(groupDiv);
		}
	}
	function attributeSelectOnChange(){
		getXMLInfo(this.formtype, this.grp, this.prod, this.attr, this.isGroup);
		calcAttrPrice();
	}
	function createAttributeItem(attr, obj, formtype, isGroup){
		var i, c;
		if(formtype == "item"){
			var loc = document.getElementById("attribute_" + attr + "_options");
		}
		var attrDiv = document.createElement("div");
		attrDiv.setAttribute("id", "attribute_" + obj.getAttribute("paid"));
		
		var attrDiv_1 = document.createElement("div");
		attrDiv_1.setAttribute("align", "left");
		attrDiv_1.style.paddingTop = "7px";
		var attrDiv_1_text = document.createTextNode(obj.getAttribute("caption") + " : ");
		var attrDiv_1_span = document.createElement("span");
		attrDiv_1_span.setAttribute("id", "attribute_" + obj.getAttribute("paid") + "_status");
		attrDiv_1_span.setAttribute("style", "text-transform:lowercase;color:#90A7D6;");
		attrDiv_1.appendChild(attrDiv_1_text);
		attrDiv_1.appendChild(attrDiv_1_span);
		attrDiv.appendChild(attrDiv_1);
		
		var attrDiv_2 = document.createElement("div");
		attrDiv_2.setAttribute("align", "left");
		attrDiv_2.style.paddingTop = "3px";
		attrDiv_2.setAttribute("id", "attribute_" + obj.getAttribute("paid") + "_input_div");
		if(obj.getAttribute("attribute_type") == "select"){
			var attrDiv_2_select = document.createElement("select");
			attrDiv_2_select.setAttribute("id", "attribute_input_" + obj.getAttribute("paid"));
			attrDiv_2_select.setAttribute("name", "oa_attributes[" + obj.getAttribute("paid") + "]");
			attrDiv_2_select.setAttribute("class", "ProductAttributesSelect");
			var setOptions = false;
			for(i = 0; i < obj.childNodes.length; i++){
				if(obj.childNodes[i].nodeName.toLowerCase() == "show_hide"){
					var setOptions = true;
				}
			}
			if(setOptions){
				attrDiv_2_select.formtype = "item";
				attrDiv_2_select.grp = obj.getAttribute("group_order");
				attrDiv_2_select.prod = obj.getAttribute("pid");
				attrDiv_2_select.attr = obj.getAttribute("paid");
				if(formtype == "group" || isGroup){
					attrDiv_2_select.isGroup = true;
				}else{
					attrDiv_2_select.isGroup = false;
				}
				attrDiv_2_select.onchange = attributeSelectOnChange;
			}else{
				attrDiv_2_select.onchange = calcAttrPrice;
			}
			for(i = 0; i < obj.childNodes.length; i++){
				if(obj.childNodes[i].nodeName.toLowerCase() == "options"){
					for(c = 0; c < obj.childNodes[i].childNodes.length; c++){
						if(obj.childNodes[i].childNodes[c].nodeName.toLowerCase() == "option"){
							var attrDiv_2_select_option = document.createElement("option");
							var attrDiv_2_select_text = document.createTextNode(obj.childNodes[i].childNodes[c].childNodes[0].nodeValue);
							var attrDiv_2_select_option_value_remove_price = obj.childNodes[i].childNodes[c].childNodes[0].nodeValue.lastIndexOf("(");
							var attrDiv_2_select_option_value = obj.childNodes[i].childNodes[c].childNodes[0].nodeValue.substring(0,attrDiv_2_select_option_value_remove_price);
							if(attrDiv_2_select_option_value == ""){
								attrDiv_2_select_option_value = obj.childNodes[i].childNodes[c].childNodes[0].nodeValue;
							}
							attrDiv_2_select_option_value = attrDiv_2_select_option_value.replace(/ /g, "+");
							attrDiv_2_select_option.setAttribute("value", escape(attrDiv_2_select_option_value));
							attrDiv_2_select_option.appendChild(attrDiv_2_select_text);
							attrDiv_2_select.appendChild(attrDiv_2_select_option);
						}
					}
				}
			}
			attrDiv_2.appendChild(attrDiv_2_select);
		}else if(obj.getAttribute("attribute_type") == "radio"){
			// need to put radio code in here
		}else if(obj.getAttribute("attribute_type") == "textarea"){
			var attrDiv_2_textarea = document.createElement("textarea");
			attrDiv_2_textarea.setAttribute("name", "oa_attributes[" + obj.getAttribute("paid") + "]");
			attrDiv_2_textarea.setAttribute("rows", obj.getAttribute("text_length"));
			attrDiv_2_textarea.setAttribute("cols", "40");
			attrDiv_2.appendChild(attrDiv_2_textarea);
		}else{
			var attrDiv_2_textarea = document.createElement("input");
			attrDiv_2_textarea.setAttribute("type", "text");
			attrDiv_2_textarea.setAttribute("name", "oa_attributes[" + obj.getAttribute("paid") + "]");
			attrDiv_2_textarea.setAttribute("maxLength", obj.getAttribute("text_length"));
			attrDiv_2.appendChild(attrDiv_2_textarea);
		}
		if(setOptions){
			var attrDiv_2_div = document.createElement("div");
			attrDiv_2_div.setAttribute("id", "attribute_" + obj.getAttribute("paid") + "_options");
			attrDiv_2.appendChild(attrDiv_2_div);
		}
		attrDiv.appendChild(attrDiv_2);
		if(formtype == "item"){
			loc.appendChild(attrDiv);
		}else if(formtype == "group"){
			return attrDiv;
		}
	}
	function removeAttributeItems(attr){
		var loc = document.getElementById("attribute_" + attr + "_input_div");
		var node = loc.getElementsByTagName("div")[0];
		if(node){
			loc.removeChild(node);
			var optionsDiv = document.createElement("div");
			optionsDiv.setAttribute("id", "attribute_" + attr + "_options");
			loc.appendChild(optionsDiv);
		}
	}
/* end added by Ardent Creative */
function getRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while(tempEl != null){
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function showHideMenuNode(node_id, image_id){
	n = document.getElementById(node_id);
	i = document.getElementById(image_id);
	if(n){
		n.style.display = n.style.display == "none" ? "block" : "none";
		if(i){
			i.src = n.style.display == "none" ? skin_images + "/menu_tree_plus.gif" : skin_images + "/menu_tree_minus.gif";
		}
	}
}


function getElementHeight(Elem) {
	if(ns4){
		var elem = document.getElementById(Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = document.getElementById(Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}


if(document.layers){
	_browser = "nn";
}
if(document.all){
	_browser = "ie";
}
if(navigator.userAgent.toLowerCase().match("gecko")){
	_browser= "gecko";
}
function isEmail(entry){
	var rex= /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/	
	return rex.test(entry);
}

function delay(gap){ /* gap is in millisecs */
	var then, now; 
	then = new Date().getTime();
	now = then;
	while((now-then) < gap){
		now=new Date().getTime();
	}
}

function showImage(image_url, image_width, image_height){
	image_width = image_width == "" ? 400 : image_width;
	image_height = image_height == "" ? 380 : image_height;

	var w = screen.width;
	var h = screen.height;
	var ww = image_width * 1 + 30;
	var wh = image_height * 1 + 45;
	var wx = (w - ww)/2;
	var wy = (h - wh)/2;
	
	if(imageWin != null){
		imageWin.close();
	}
	imageWin = null;
	imageWin = window.open(
		"", 
		"ProductImageWindow", 
		"titlebar=yes, toolbar=no, menubar=no, status=no, directories=no, resizable=yes, scrollbars=yes, top=" + wy.toString() + ", left=" + wx.toString() + ", width=" + ww.toString() + ", height=" + wh.toString() + ""
	);
	while(imageWin==null);
	imageWin.focus();
	
	imageWin.document.body.innerHTML = "";
	imageWin.document.write('<body style="padding:5px;margin:0px">');
	imageWin.document.write('<div align="center"><img hspace="0" vspace="0" src="' + image_url + '"></div><br/>');
	imageWin.document.write('<div align="center" style="font-family:arial;font-color:black;font-size:11px;"><a href="javascript:window.close();" style="color:#0000AA;">Close Window</a></div>');
	imageWin.document.write('</body>');
	imageWin.width = ww;
	imageWin.height = wh;
}

function showPrinterPage(url){
	var prWin = null;
	prWin = window.open(
		url,
		"PrintVer", 
		"titlebar=yes, toolbar=no, menubar=yes, status=yes, directories=no, resizable=yes, scrollbars=yes, top=20, left=20, width=810, height=600"
	);
	while(prWin==null);
	prWin.focus();
}

function OnButton(bt){
	document.images[bt].src = skin_images + bt + "_on.gif";
	
}
function OffButton(bt){
	document.images[bt].src = skin_images + bt + "_off.gif";
}

function OnMenu(cid){
	document.images["menul_" + cid].src = skin_images + "catl_bg_on.gif";
	document.all["menur_" + cid].background = skin_images + "catr_bg_on.gif";
}
function OffMenu(cid){
	document.images["menul_" + cid].src = skin_images + "catl_bg_off.gif";
	document.all["menur_" + cid].background = skin_images + "catr_bg_off.gif";
}

function OnMenu(img){
	document.images[img].src = skin_images + "menu_arrow_on.gif";
}
function OffMenu(img){
	document.images[img].src = skin_images + "menu_arrow.gif";
}

function ShowPopup(src){
	var bWin = null;
	bWin = window.open(
		src, 
		"PopupWind", 
		"titlebar=no, toolbar=no, menubar=no, status=no, directories=no, resizable=no, scrollbars=no, top=20, left=20, width=320, height=240"
	);
	while(bWin==null);
	bWin.focus();
}

function PopUpImage(image_source, image_width, image_height){
	var bWin = null;
	bWin = window.open(
		image_source, 
		"ImageWind", 
		"titlebar=no, toolbar=no, menubar=no, status=no, directories=no, resizable=no, scrollbars=no, top=20, left=20, width=" + (image_width + 20) + ", height=" + (image_height + 20)
	);
	while(bWin==null);
	bWin.focus();
}

function ConfirmLogout(){
	if(orderItemsCount > 0){
		if(confirm("You have items in your cart. Logging out will empty your cart\nAre you sure want to continue?")){
			document.location = urlLogout;
		}
	}
	else{
		if(confirm("Do you really want to logout?")){
			document.location = urlLogout;
		}
	}
}

function CartConfirmDeleteItem(ocid){
	if(confirm(msg_confirm_delete_item)){
		document.location = CartDeleteItemUrl + '&ocid=' + ocid;
	}
}
function CartConfirmEmpty(){
	if(confirm(msg_confirm_empty_cart)){
		document.location = CartEmptyUrl;
	}
}
