function getTextXmlNodeValue(xmlText, nodeName) {
	var i1 = xmlText.search(new RegExp("<" + nodeName + ">","i"));
	var i2 = xmlText.search(new RegExp("</" + nodeName + ">","i"));
	if(i1 > 0 && i2 > 0 && i2 > i1) {
		return xmlText.substring(i1 + nodeName.length + 2, i2);
	}
	return null;
}

function addtolightbox(catalogId, recordId, name, lightbox, lbname) {
	var dataSource = "lightboxcontroller.jsp?action=add&catalogId=" + catalogId + "&recordId=" + recordId + "&name=" + name + "&lightbox=" + lightbox + "&lbname=" + lbname;
	setTimeout("makeRequest(\"" + dataSource + "\",processStateChange);",1);
}

function changelightbox() {
	var lightboxNames = document.getElementById("lightboxNames");
	var dataSource;
	if (lightboxNames&&lightboxNames.value) {
		dataSource = "lightboxcontroller.jsp?action=load&lightbox=" + lightboxNames.value;
	} else {
		dataSource = "lightboxcontroller.jsp?action=load&lightbox=0";
	}
	setTimeout("makeRequest(\"" + dataSource + "\",processStateChange);",1);
}

function removefromlightbox(lightbox, catalogId, recordId) {
	var dataSource = "lightboxcontroller.jsp?action=remove&lightbox=" + lightbox + "&catalogId=" + catalogId + "&recordId=" + recordId;
	setTimeout("makeRequest(\"" + dataSource + "\",processStateChange);",1);
}

function makeRequest(dataSource,processStateChange) {
	if (window.XMLHttpRequest) { // IE7 or Non-IE browsers
		xmlHttp = new XMLHttpRequest();
		if(xmlHttp != null) {
			xmlHttp.onreadystatechange = processStateChange;
			xmlHttp.open("POST", dataSource, true);
			xmlHttp.send(null);
		}
	} else if (window.ActiveXObject) { // IE5 or IE6
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlHttp != null) {
			xmlHttp.onreadystatechange = processStateChange;
			xmlHttp.open("POST", dataSource, true);
			xmlHttp.send();
		}
	}
}

function processStateChange() {
	if (xmlHttp != null && xmlHttp.readyState == 4) { // Complete
		if (xmlHttp.status == 200) { // OK response
			//window.status = "Items in cart: " + xmlHttp.responseText;
/*			var _items = document.getElementById("_items");*/
			xml = xmlHttp.responseText;
			var error = getTextXmlNodeValue(xml,"error");
			if (error != null) {
				alert(error);
			} else {
				var html = getTextXmlNodeValue(xml,"innerhtml");
				if (html == null) {
					alert("empty response");
				} else {
					showlightboxPanel(html);
				}
			}
		} else {
			alert("Error occured when processing the request: " + xmlHttp.statusText);
		}
		xmlHttp = null;
	}
}

function showlightboxPanel(html) {
	var lightbox_panel = document.getElementById("lightboxPanel");
	lightbox_panel.innerHTML=html;
	lightbox_panel.style.display="inline";
	var show_hide_lightbox = document.getElementById("show_hide_lightbox");
	show_hide_lightbox.innerHTML="Hide Lightbox";
}

function hidelightboxPanel() {
	var lightbox_panel = document.getElementById("lightboxPanel");
	lightbox_panel.style.display="none";
	var show_hide_lightbox = document.getElementById("show_hide_lightbox");
	show_hide_lightbox.innerHTML="Show Lightbox";
}

function showHide() {
	if(lightboxHide) {
		changelightbox();
	} else {
		hidelightboxPanel();
	}
	lightboxHide = !lightboxHide;
}