/**
 * @author Drks
 */
basedir = "content";
xmlDocH = basedir + '/greenhome.xml';
xslDocH = basedir + '/greenhome.xsl';
xmlDocG = basedir + '/greenlight.xml';
xslDocG = basedir + '/greenlight.xsl';
var xmlDoc, xslDoc;

function loadDocument(docName) {
	if(window.ActiveXObject) {
		xhttp = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
		xhttp.async = "false";
		xhttp.load(docName);
		return xhttp;
	} else {
		xhttp = new XMLHttpRequest();
		xhttp.open("GET", docName, false);
		xhttp.send();
		return xhttp.responseXML;
	}
}

function displayResult(obj, tipo) {
	if(window.ActiveXObject) {//Code for internet explorer
		var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
		XSLTCompiled.stylesheet = xslDoc.documentElement;
		// create XSL-processor
		var XSLTProc = XSLTCompiled.createProcessor();
		XSLTProc.input = xmlDoc;

		if(tipo != "") {
			XSLTProc.addParameter("tipo", tipo);
		}

		XSLTProc.transform();
		document.getElementById(obj).innerHTML = XSLTProc.output;
	} else {
		xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslDoc);

		if(tipo != "") {
			xsltProcessor.setParameter(null, "tipo", tipo);
		}
		ex = xsltProcessor.transformToFragment(xmlDoc, document);
		document.getElementById(obj).innerHTML = "";
		document.getElementById(obj).appendChild(ex);
	}
}

function displayMenu(obj, tipo) {
	if(tipo == "home") {
		xmlDoc = loadDocument(xmlDocH);
		xslDoc = loadDocument(xslDocH);
	} else {
		xmlDoc = loadDocument(xmlDocG);
		xslDoc = loadDocument(xslDocG)
	}
	x = xmlDoc.getElementsByTagName('categoria');
	txt = '<ul>';
	for( i = 0; i < x.length; i++) {
		nv = x[i].attributes.getNamedItem('tipo').value;
		nc = x[i].getElementsByTagName('articulo').length
		txt = txt + '<li class="greenlist"><a href="javascript:displayResults(\'content\',\'' + nv + '\')">' + nv + ' (' + nc + ')</a></li>';
	}
	txt = txt + '</ul>';
	document.getElementById(obj).innerHTML = txt;
}

function displayProduct(obj, partid) {
	if(window.ActiveXObject) {//Code for internet explorer
		var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
		XSLTCompiled.stylesheet = xslDoc.documentElement;
		// create XSL-processor
		var XSLTProc = XSLTCompiled.createProcessor();
		XSLTProc.input = xmlDoc;

		if(partid != "") {
			XSLTProc.addParameter("partid", partid);
		}

		XSLTProc.transform();
		document.getElementById(obj).innerHTML = XSLTProc.output;
	} else {
		xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslDoc);

		if(partid != "") {
			xsltProcessor.setParameter(null, "partid", partid);
		}
		ex = xsltProcessor.transformToFragment(xmlDoc, document);
		document.getElementById(obj).innerHTML = "";
		document.getElementById(obj).appendChild(ex);
	}
}

function displayResults(obj, type) {
	xmlDoc = loadDocument('content/greenlight.xml');
	xslDoc = loadDocument('content/greenlight.xsl');
	displayResult(obj, type);
}

function displaySpecials(obj) {
	xmlDoc = loadDocument('content/greenlight.xml');
	xslDoc = loadDocument('content/specials.xsl');
	displayResult(obj, '');
}

function displaySpecialsList(obj) {
	xmlDoc = loadDocument('content/greenlight.xml');
	xslDoc = loadDocument('content/listspecials2.xsl');
	displayResult(obj, '');
}

function Product(obj, partid) {
	xmlDoc = loadDocument('content/greenlight.xml');
	xslDoc = loadDocument('content/prodview.xsl');
	displayProduct(obj, partid);
}

function doSearch() {
	searchString = document.form1.searchbox.value.toUpperCase();
	window.location = "search.php?search=" + searchString;
	return false;
	/*
	xmlDoc = loadDocument('content/greenlight.xml');
	xslDoc = loadDocument('content/search.xsl');
	searchProduct(obj, partid);
	*/	
}

function searchProduct(obj,search) {
	xmlDoc = loadDocument('content/greenlight.xml');
	xslDoc = loadDocument('content/search.xsl');
		
	if(window.ActiveXObject) {//Code for internet explorer
		var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
		XSLTCompiled.stylesheet = xslDoc.documentElement;
		// create XSL-processor
		var XSLTProc = XSLTCompiled.createProcessor();
		XSLTProc.input = xmlDoc;

		if(search != "") {
			XSLTProc.addParameter("search", search);
		}

		XSLTProc.transform();
		document.getElementById(obj).innerHTML = XSLTProc.output;
	} else {
		xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslDoc);

		if(search != "") {
			xsltProcessor.setParameter(null, "search", search);
		}
		ex = xsltProcessor.transformToFragment(xmlDoc, document);
		document.getElementById(obj).innerHTML = "";
		document.getElementById(obj).appendChild(ex);
	}	
}

function init(page) {
	/*
	 if(window.ActiveXObject) {
	 Nifty("div", "span", "a:hover");
	 }
	 */
	x = document.getElementById(page);
	x.className = "selected";

}
