function wait (div,host) {
	document.getElementById(div).setAttribute('id',div+"_loading");
	document.getElementById(div+'_hide_img').setAttribute('id',div+'_loading_img');
}

function ready_state (host,div,active,img_width,img_height,lang) {
	if (request.readyState == 4) {
		interpret_BoxShelves_request(host,div,active,img_width,img_height,lang);
	} else {
		setTimeout("ready_state('"+host+"','"+div+"','"+active+"','"+img_width+"','"+img_height+"','"+lang+"');",50);
	}
}
function send_BoxShelves_request(host,categories,lines,columns,active,div,img_width,img_height,lang) {
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject('Msxml2.XMLHTTP.5.0');
		} catch (e) {
			try {
				request = new ActiveXObject('Msxml2.XMLHTTP.4.0');
			} catch (e) {
				try {
					request = new ActiveXObject('Msxml2.XMLHTTP.3.0');
				} catch (e) {
					try {
						request = new ActiveXObject('Microsoft.XMLHTTP');
					} catch (e) {
						request = false;
					}
				}
			}
		}
	}
	if (!request) {
		alert("XMLHTTP-Object could not be created!");
		return false;
	} else {
		var url = "http://"+ host +'/cgi-bin/virtualib/box_shelves';
		var parameters = '?categories='+categories+'&lines='+lines+'&columns='+columns+'&active='+active+'&div='+div;
		parameters += '&img_width='+img_width+'&img_height='+img_height+'&language='+lang;
		request.open('GET',url+parameters,true);
		request.send(null);
		wait(div,host);
		request.onreadystatechange = ready_state(host,div,active,img_width,img_height,lang);
	}
}

function interpret_BoxShelves_request(host,div,new_active,img_width,img_height,lang) {
	if (request.readyState == 4) {
		if (request.status != 200) {
			window.alert("Request is finished but there is an error!\nError:"+request.status);
		} else {
			var xmldoc = request.responseXML;
			var links = xmldoc.getElementsByTagName('links').item(0);
			var total_links = links.getAttribute("total");
			var new_link_html = "";
			var new_title_html = "";

			var lines = "";
			var columns = "";
			var com = "";
			var sid = "";

			for (var iNode = 0; iNode < links.childNodes.length; iNode++) {
				if (links.childNodes.item(iNode).tagName=="link") {
					var link = links.childNodes.item(iNode);
					var number = 0;
					var host = "";
					var title = "";
					var categories = "";
					var active = "";
					for (var jNode = 0; jNode < link.childNodes.length; jNode++) {
						if (link.childNodes.item(jNode).firstChild !== null) {
							if (link.childNodes.item(jNode).tagName=="number") {
								number = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="title") {
								title = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="host") {
								host = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="com") {
								com = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="sid") {
								sid = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="categories") {
								categories = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="lines") {
								lines = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="columns") {
								columns = link.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (link.childNodes.item(jNode).tagName=="active") {
								active = link.childNodes.item(jNode).firstChild.nodeValue;
							}
						}
					}
					if (number == 0) {
						new_link_html = "<img class='"+div+"_hide_img' id='"+div+"_hide_img' border= '0' alt='Please wait...' src='http://"+host+"/osr/giflib/time.gif'/><table id='links' class='links'><tr>";
					} else {
						new_link_html = new_link_html.concat("<td>|</td>");
					}
					if (active == new_active) {
						new_link_html = new_link_html.concat("<td class='link_"+number+"'><a href=\"javascript:send_BoxShelves_request('"+host+"','"+categories+"','"+lines+"','"+columns+"','"+active+"','"+div+"','"+img_width+"','"+img_height+"','"+lang+"')\"><u>"+title+"</u></a></td>");
					} else {
						new_link_html = new_link_html.concat("<td class='link_"+number+"'><a href=\"javascript:send_BoxShelves_request('"+host+"','"+categories+"','"+lines+"','"+columns+"','"+active+"','"+div+"','"+img_width+"','"+img_height+"','"+lang+"')\">"+title+"</a></td>");
					}
				}
			}
			new_link_html = new_link_html.concat("</tr></table>");
			new_link_html = new_link_html.concat("<table id='shelves' class='shelves'><tr class='img'>");
			var shelves = xmldoc.getElementsByTagName('shelves').item(0);
			var total_shelves = shelves.getAttribute("total");
			var actual_line = 0;
			var actual_columns = 0;
			var image_ratio = img_width / img_height;
			for (var iNode = 0; iNode < shelves.childNodes.length; iNode++) {
				if (shelves.childNodes.item(iNode).tagName=="shelve") {
					var shelve = shelves.childNodes.item(iNode);
					var item_code = "";
					var title = "";
					var base = "";
					var width = "";
					var height = "";

					for (var jNode = 0; jNode < shelve.childNodes.length; jNode++) {
						if (shelve.childNodes.item(jNode).firstChild !== null) {
							if (shelve.childNodes.item(jNode).tagName=="item_code") {
								item_code = shelve.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (shelve.childNodes.item(jNode).tagName=="width") {
								width = shelve.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (shelve.childNodes.item(jNode).tagName=="height") {
								height = shelve.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (shelve.childNodes.item(jNode).tagName=="title") {
								title = shelve.childNodes.item(jNode).firstChild.nodeValue;
							}
							if (shelve.childNodes.item(jNode).tagName=="base") {
								base = shelve.childNodes.item(jNode).firstChild.nodeValue;
							}
						}
					}
					
					new_link_html = new_link_html.concat("<td><a href='http://"+host+"/cgi-bin/apps/base?sid=&com=&base="+base+"&item="+item_code+"'><img border='0' alt='' src='http://"+host+"/resources/images/_base_"+base+"/"+item_code+".jpg' width='"+width+"' height='"+height+"'/></a></td>");
					new_title_html = new_title_html.concat("<td><a href='http://"+host+"/cgi-bin/apps/base?sid=&com=&base="+base+"&item="+item_code+"'>"+title+"</a></td>");
					actual_columns++;
					if (actual_columns == columns) {
						actual_line++;
						new_link_html = new_link_html.concat("</tr><tr class='title'>"+new_title_html+"</tr>");
						new_title_html = "";
						if (actual_line < lines) {
							new_link_html = new_link_html.concat("<tr>");
						}
					}
				}
			}
			new_link_html = new_link_html.concat("</tr></table>");
			document.getElementById(div+"_loading").innerHTML = new_link_html;
			document.getElementById(div+"_loading").setAttribute('id',div);
			document.getElementById(div+'_loading_img').setAttribute('id',div+'_hide_img');
		}
	}
}