/*

	EASearch Informer - utilizes XMLHttpRequest to show search results on the page.
	Creates EASI object to work with.

	!!! requires xmlhttprequest.js !!!
	http://code.google.com/p/xmlhttprequest/

*/

	function insertData (elm, response) {
		var arrTR = elm.parentNode.parentNode.parentNode.getElementsByTagName("tr");
		var rowIndex = elm.parentNode.parentNode.sectionRowIndex;
		var arrTD = arrTR[rowIndex+1].getElementsByTagName("td");
		if(arrTD[0].className == "EASIelem" || arrTD[0].className == "EASIelem2") {
			arrTD[0].innerHTML = (response);
		}
		return false;
	}

	function fAsynchronousGet(elm, url) {
		var oXMLHttpRequest = new XMLHttpRequest;
		oXMLHttpRequest.open("GET", url, true);
		oXMLHttpRequest.onreadystatechange = function() {
			if (this.readyState == XMLHttpRequest.DONE) {
				if (oXMLHttpRequest.status == 200) {
					insertData (elm, oXMLHttpRequest.responseText);
				} else {
					insertData (elm, "error. <!-" + "- " + oXMLHttpRequest.status + " --" + ">");
				}
			}
		}
		oXMLHttpRequest.send(null);
	}

	function EASearchInformer () {
		this.Boxes = new Array ();

		this.CreateIBox  = EASICreateIBox;
		this.RequestData = EASIRequestData;
		this.MoreInfo    = EASIMoreInfo;
		this.KitInfo    = EASIKitInfo;
		this.CloseBox    = EASICloseBox;
		this.findTd = EASIfindTD;
		this.ClosePrev = EASIClosePrev;
	}

	function EASIRequestData (elm, url) {
		fAsynchronousGet (elm, url);
		if(this.findTd(elm))
			this.findTd(elm).innerHTML = "<img border='0' src='http://www.ig-auto.ru/easearch/easearch/image/ajax-loader.gif' />";
	}

	function EASIMoreInfo (elm, url) {
		if (url == '') return;
		
		if (this.findTd(elm) === false) {
			this.CreateIBox (elm, 'EASIelem');
		}
		this.RequestData (elm, url);
	}
	
	function EASIKitInfo (elm, url) {
		if (url == '') return;
		
		if (this.findTd(elm) === false) {
			this.CreateIBox (elm, 'EASIelem2');
		}
		this.RequestData (elm, url);
	}

	function EASICreateIBox (elm, elm_id) {

		this.ClosePrev(elm_id);

		var rowIndex = elm.parentNode.parentNode.sectionRowIndex ;
		var arrTr = elm.parentNode.parentNode.parentNode.getElementsByTagName('tr');
		arrTr[rowIndex].className = "EASIparent";

		var sElem = elm.parentNode.parentNode.getElementsByTagName('td');
		var newTr = elm.parentNode.parentNode.parentNode.insertRow(rowIndex+1);
		var newTd = newTr.insertCell(0);
		newTd.setAttribute("colSpan",sElem.length);
		newTd.className = elm_id;
		newTd.id = elm_id;
	}

	function EASICloseBox (elm) {
		var sElem = elm.parentNode.parentNode;
		var rowIndex = sElem.sectionRowIndex;
		var arrTr = sElem.parentNode.getElementsByTagName('tr');
		arrTr[rowIndex-1].className = "none";
		sElem.parentNode.deleteRow(rowIndex);
		return false;
	}

	function EASIClosePrev (elm_id) {
		var Td = document.getElementById(elm_id);
		if(Td) {
			var rowIndex = Td.parentNode.sectionRowIndex ;
			var arrTr = Td.parentNode.parentNode.getElementsByTagName('tr');
			arrTr[rowIndex-1].className = "none";

			Td.parentNode.parentNode.deleteRow(rowIndex);
		}
	}

	function EASIfindTD (elm) {
		var arrTR = elm.parentNode.parentNode.parentNode.getElementsByTagName("tr");
		var rowIndex = elm.parentNode.parentNode.sectionRowIndex;
		if(arrTR[rowIndex+1]) {
			var arrTD = arrTR[rowIndex+1].getElementsByTagName("td");
			if(arrTD[0] && (arrTD[0].className == "EASIelem" || arrTD[0].className == "EASIelem2"))
			return arrTD[0];
		}
		return false;
	}

	EASI = new EASearchInformer ();

