// JavaScript Document
function g_checkStatus() {	
	var status_msg = document.getElementById("status_msg");
		
	if (status_msg != null && status_msg.value > "") {
		alert(status_msg.value);
	}
}

function initHeader(autoFocusSearch, current_menu) {
	// set cookie to try to let server know when not a bot
	createCookie("user_agent", "1", 1);
	
	if (autoFocusSearch == null || autoFocusSearch != false)
		document.getElementById("searchText").focus();
	
	if (current_menu != null && current_menu != "") {
		var currMenu = document.getElementById(current_menu);
		if (currMenu != null) currMenu.style.textDecoration = "underline";
	}
}

function doSearch(e) {
	if (!e) var e = window.event

	var searchField = document.getElementById("searchText");
		
	searchField.value = trim(searchField.value)
	if (searchField.value > "") {
		// the following uses the GET method
		var action = varWebRoot  + "/search.jsp?searchText=" + URLencode(searchField.value);
		document.location.href = action;
	}
	
	// return false so Mozilla based browsers will not
	// continue to submit forms with the default method
	// handle event
	e.cancelBubble = true;
	e.returnValue = false;
	if (e.stopPropagation) e.stopPropagation();
	return false;
}

function gotoLightbox(lightboxType) {
	if (lightboxType != null && lightboxType == "lightboxcd")
		document.location.href = varWebRoot  + "/lightbox?cmd=gotoPage&pageNum=1&hid.mode=lightboxcd";
	else
		document.location.href = varWebRoot  + "/lightbox?cmd=gotoPage&pageNum=1&hid.mode=lightbox";
}

function viewLightbox(lightboxType) {
	// determine which lightbox we want to take them to first in case lightboxType is not specified
	var lightbox = readCookie("lightbox");
	var itemCount = 0;
	if (lightbox != null) itemCount = lightbox.split('|').length;
	
	var lightboxCD = readCookie("lightboxcd");
	var cdCount = 0;
	if (lightboxCD != null) cdCount = lightboxCD.split('|').length;
	
	// the following uses the GET method
	if ((lightboxType != null && lightboxType == "lightboxcd") || (itemCount == 0 && cdCount > 0))
		document.location.href = varWebRoot + "/lightbox?cmd=gotoPage&pageNum=1&hid.mode=lightboxcd";
	else
		document.location.href = varWebRoot + "/lightbox?cmd=gotoPage&pageNum=1&hid.mode=lightbox";
}

function showCartItemCount() {
	var cart = readCookie("cart");
	var cdCart = readCookie("cdcart");
	var subCart = readCookie("subcart");
	
	var itemCount = 0;
	
	if (cart != null) itemCount = cart.split('|').length;
	if (cdCart != null) itemCount = itemCount + cdCart.split('|').length;
	if (subCart != null) itemCount = itemCount + subCart.split('|').length;
	
	document.getElementById("my_cart").innerHTML = "My Cart (" + itemCount + ")";
	
	return itemCount;
}

function showLightboxCount() {
	var lightbox = readCookie("lightbox");
	var lightboxcd = readCookie("lightboxcd");
	var itemCount = 0;
	
	if (lightbox != null) itemCount = lightbox.split('|').length;
	if (lightboxcd != null) itemCount = itemCount + lightboxcd.split('|').length;
	
	if (document.getElementById("my_lightbox") != null)
		document.getElementById("my_lightbox").innerHTML = "My Lightbox (" + itemCount + ")";
}

function ajaxRefreshCart() {
	if (document.getElementById("ctrl_cart") == null) return false;
	if (xmlHttp == null) return false;
	
	var url = "shop?cmd=refreshCartXML";
	
	// reinitialize Ajax object for Opera compatability
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = ajxRefreshCartReturn;
	xmlHttp.send(null);
}

function couponTerms() {
	open(varWebRoot + "/learnMoreCoupons.html", "",
		"resizable=" + (NS ? "yes" : "no") + ", status=no, width=425, height=250");
}

function logout () {
	var userID = document.getElementById("curr_user_id");
	if (userID == null) return;
	
	if (confirm("Logout Member " + userID.innerHTML + "?") != true) return;

	document.location.href = "accounts?cmd=logout";
}

// do we even need this anymore?
//return IE4+or W3C DOM reference for an ID
function getObject(obj){
	var theObj
	if (document.all){
		if (typeof obj =="string"){
			return document.all(obj)
		}else {
			return obj.style
		}
	}
	if (document.getElementById){
		if (typeof obj =="string"){
			return document.getElementById(obj)
		}else {
			return obj.style
		}
	}
	return null
}

function viewCDImages(cdID) {
	document.location.href =
		varWebRoot + "/cdcatalog?cmd=gotoPage&pageNum=1&cd_id=" + cdID + "&hid.mode=cdimages";
}

function getPosition(e) {
	var left = 0;
	var top  = 0;
	
	while (e.offsetParent) {
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e = e.offsetParent;
	}
	
	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	return {x:left, y:top};
}

function mouseCoords(ev) {
	if(ev.pageX || ev.pageY) {
		return {x:ev.pageX, y:ev.pageY};
	}
	
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function getMouseOffset(target, ev) {
	ev = ev || window.event;

	var docPos = getPosition(target);
					
	var mousePos  = mouseCoords(ev);
		
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function StopBubble(e) {
    /* http://www.quirksmode.org/js/events_order.html */
    if (!e) e = window.event;

    e.cancelBubble = true; /* Microsoft */
    if (e.stopPropagation)
        e.stopPropagation(); /* W3C */
}

function checkXMLResponse(xmlStatus, silent) {
	if (xmlStatus.length == 0) return 0;
	
	var statusCode = xmlStatus[0].getElementsByTagName("code")[0].firstChild.nodeValue;
	
	if (statusCode != "success") {
		if (silent != null && silent == true) {
			var statusMsg = xmlStatus[0].getElementsByTagName("message")[0].firstChild.nodeValue;
			alert("Error:\n\n" + statusMsg);
		}
		
		return 0;
		
	} else
		return 1;
}

function imgOptOver(ev) {
	ev = ev || window.event;
	
	var img = ev.target || ev.srcElement;
			
	if (img.tagName == "A") {
		if (img.getAttribute("href") == null || img.getAttribute("href") == "")
			img.style.textDecoration = "underline";
		
		for (var i = 0; i < img.childNodes.length; i++) {
			if (img.childNodes[i].tagName == "IMG") {
				img = img.childNodes[i];
				break;
			}
		}
	}
	
	if (img.getAttribute("_on") != null)
		img.src = getCachedImage(img.getAttribute("_on")).src;
		//img.src = img.getAttribute("_on");
}

function imgOptOut(ev) {
	ev = ev || window.event;
	
	var img = ev.target || ev.srcElement;
	
	if (img.tagName == "A") {
		if (img.getAttribute("href") == null || img.getAttribute("href") == "")
			img.style.textDecoration = "none";
			
		for (var i = 0; i < img.childNodes.length; i++) {
			if (img.childNodes[i].tagName == "IMG") {
				img = img.childNodes[i];
				break;
			}
		}
	}
	
	if (img.getAttribute("_off") != null)
		img.src = getCachedImage(img.getAttribute("_off")).src;
		//img.src = img.getAttribute("_off");
}

function getCachedImage(src) {
	var img = imgCache[src];

	if (img == null) {
		imgCache[src] = new Image();
		imgCache[src].src = src;
		img = imgCache[src];
	}
	
	return img;
}

function checkClosePopUp(ev) {
	ev = ev || window.event;
		
	var target = ev.target || ev.srcElement;
		
	var popupName = "lightbox-popup";
	var popupMemo = "memo-popup";
	var popupDecline = "declinereason-popup";
	
	if (target) {
		do {
			if (target.id == popupName || target.id == popupMemo || target.id == popupDecline) return;
			target = target.parentNode;
		} while (target);
	}
	
	hidePopup(popupName);
	hidePopup(popupMemo);
	hidePopup(popupDecline);
}

function hidePopup(popupName) {
	var popup = document.getElementById(popupName);
	if (popup) {
		popup.style.display = "none";
		document.onclick = null;
	}
}