var isize = 2;
var enProceso = 0; // lo usamos para ver si hay un proceso activo 1 es marcas y 2 es familias, 3 es una unica marca
var xmlreqs = new Array();


function handle_response(data)
{
        //window.alert(data);
        if (enProceso==2) document.getElementById("div_ajax").innerHTML=document.getElementById("div_ajax").innerHTML+data;
        else eval(data);
        if (enProceso!=6) enProceso = 0;

}

function CXMLReq(type, xmlhttp) {
	this.type = type;
	this.xmlhttp = xmlhttp;
}

function xmlhttpChange() {
	if (typeof(window['xmlreqs']) == "undefined") return;
	var xmldoc = null;
	//window.alert("Response");
	for (var i=0; i < xmlreqs.length; i++) {
		if (xmlreqs[i].xmlhttp.readyState == 4) {
			if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) {
				
				if (document.implementation && document.implementation.createDocument) {
					xmldoc = document.implementation.createDocument("", "", null);
				} else if (window.ActiveXObject) {
					xmldoc = new ActiveXObject("Microsoft.XMLDOM");
				}
				xmldoc = xmlreqs[i].xmlhttp.responseText;
				xmlreqs.splice(i,1); i--;
				handle_response(xmldoc);
			} else {
				// error
				xmlreqs.splice(i,1); i--;
			}
		}
	}
}


function nuevoAjax(url,tipo,modo,data)
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
        enProceso=modo;

	
/*
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!="undefined") xmlhttp=new XMLHttpRequest();
		}
	}
*/
	if (window.XMLHttpRequest) { // Mozilla etc.
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open(tipo,url,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if (tipo === "POST" )xmlhttp.send(data);
		else xmlhttp.send(null);
	} else if (window.ActiveXObject) { // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp) {
			xmlhttp.onreadystatechange=xmlhttpChange;
			xmlhttp.open(tipo,url,true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
             	   	if (tipo === "POST" )xmlhttp.send(data);
			else xmlhttp.send(null);
		}
	}
	var xmlreq = new CXMLReq('', xmlhttp);
	xmlreqs.push(xmlreq);

	//return xmlhttp;
 
}

function getSku(numero)
{
	//window.alert('0');
	codigos = document.getElementsByName("adimpo_code");
	ids = document.getElementsByName("product_ids");
	shopper = document.getElementById("shopper_group_id");
	
	if (numero==-1) {
		enProceso = 0;
		return;
	}
	if (numero == -2) {
		
		peticion = "shopper=" + shopper.value + "&psku=" + document.getElementById("adimpo_code").value + "&id=" + document.getElementById("product_id").value + "&sig=-2";
	}
	else {
		if (codigos.length <= numero) {
			//enProceso = 0;
			return;
		}
		peticion = "shopper=" + shopper.value + "&multi=1";
		var i =numero;
		if (numero+isize-codigos.length == 1) peticion = "shopper=" + shopper.value + "&psku=" + codigos[i].value + "&id=" + ids[i].value + "&sig=-2";
		else{ 
    for (i=numero;i<numero+isize;i++)
		{
			if (i<codigos.length)
				peticion = peticion + "&" + ids[i].value + "=" + codigos[i].value;
			else break;
		}
		peticion =  peticion + "&sig=" + i;
		}
	//window.alert(peticion);
	//if (codigos[numero].value == '24337')window.alert(peticion);
	}
	//conectarAjax("/cyc/WSGetPrice.php",peticion,modo);
	return peticion; 
	
}

function setById(pid,codigo,precio,stock,lpi,imagen,carro,siguiente)
{
	document.getElementById("price_"+pid).innerHTML= precio;
	document.getElementById("lpi_"+pid).innerHTML= lpi;
	document.getElementById("stock_"+pid).innerHTML= stock;
	if (imagen != 0) document.getElementById("image_"+pid).src= imagen;
	if (carro==1) document.getElementById("carro2_"+pid).innerHTML =document.getElementById("carro_"+pid).innerHTML;
	if (siguiente != -3) getSku(siguiente);
	
}

function browseList()
{
	//window.alert("browse");
	for (var i=0;i<document.getElementsByName("adimpo_code").length;i=i+isize)
	{	
		var peticion = getSku(i);	
		nuevoAjax("/cyc/WSGetPrice.php","POST",6,peticion);
	}
	//enProceso=0;
}
function JoomLoad()
{
	var peticion=false;
//window.alert("busqueda");
	if(enProceso !=0) return;
	//shop.product_details
	if (location.href.match(".*page=shop.browse.*") )
		browseList();
	else if (location.href.match(".*page=shop.product_details.*"))
	{
                var peticion = getSku(-2);
                nuevoAjax("/cyc/WSGetPrice.php","POST",5,peticion);

	}

		
	//if (peticion) conectarAjax("/cyc/WSGetPrice.php",peticion,modo);	
}


