//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	//Creando Objeto
  var objetoAjax=false;
  try {
   /*Para navegadores distintos a internet explorer*/
   objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
  }catch (e) {
  	 try {
     	/*Para explorer*/
     	objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
     } 
     catch (E) {
     	objetoAjax = false;
   	 }
  }
  if (!objetoAjax && typeof XMLHttpRequest!='undefined') {
  		objetoAjax = new XMLHttpRequest();
  }
  return objetoAjax;
}

//Creacion del Objeto XMLHTTPREQUEST
var preciosReq = getXmlHttpRequestObject();

//Creacion del Objeto XMLHTTPREQUEST
var preciosReqsh = getXmlHttpRequestObject();

variante = new Array (); 
variantesh = new Array (); 


//Llamada cada q se desliza una prenda.
function jersey_exist() { 
	if(document.getElementById('jerseysel').value==""){
		document.getElementById('div_wsjsy').innerHTML = "<span>No se ha seleccionado un jersey</span>";
	}
		
	else if (preciosReq.readyState == 4 || preciosReq.readyState == 0) {
		var str = escape(document.getElementById('codigo_jsy').innerHTML); //Campo de codigo del jersey.
		
		preciosReq.open("GET", 'ajax_pyc.php?cod_jersey=' + str, true);
		preciosReq.onreadystatechange = handlejersey_exist; 
		preciosReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handlejersey_exist() {
	//Crea resultado de la consulta.
	var ss = document.getElementById('div_wsjsy');//Etiqueta DIV de resultados.
	
	 if (preciosReq.readyState==1) {
                 ss.innerHTML="<span>Actualizando informaci&oacute;n.......</span>";
         }
	if (preciosReq.readyState == 4) {
		ss.innerHTML = '';
		cadena = preciosReq.responseText;
		//var respuesta = escape(charstring); 
			var str = cadena.split("|");
			document.getElementById('preciojsy').innerHTML = str[0];
			document.getElementById('preciojsytc').innerHTML = str[0];
			document.getElementById('preciocpl').value = str[0];
					
			var i;
			var j;
			j=0;
			variante = null;
			variante = new Array (); 
			for(i=1; i < str.length - 1; i++) {
				//Build our element string.  This is cleaner using the DOM, but
				//IE doesn't support dynamically added attributes.
				variante[j] = new Array(str[i],str[i+1],str[i+2]);
				j++;
				i+2;
			}
			update_talla();
	}
}


function update_exist(){
	var tamsel = document.getElementById('tam').value;
	var colorseleccionado = document.getElementById('colorpl').value;
	var codigoprod = document.getElementById('codcpl').value;
	var sku = codigoprod+""+colorseleccionado+""+tamsel;
	
	for(ind = 0; ind < variante.length; ind++){
		if(variante[ind][0] == sku){		
			selectamano = "<select name=\"cantidad\" class=\"lista\" id=\"cantidad\"><option selected value=\"0\">Seleccione una cantidad</option>";
            if(variante[ind][2]>=1) { selectamano +="<option value=\"1\">1</option>"; }
			if(variante[ind][2]>=2) { selectamano +="<option value=\"2\">2</option>"; }
			if(variante[ind][2]>=3) { selectamano +="<option value=\"3\">3</option>"; }
			if(variante[ind][2]>=4) { selectamano +="<option value=\"4\">4</option>"; }
			if(variante[ind][2]>=5) { selectamano +="<option value=\"5\">5</option>"; }
            selectamano += "</select>"; 
			document.getElementById('combotamjsy').innerHTML = selectamano;
			
		}
	}

}

function update_talla(){
	var colorseleccionado2 = document.getElementById('colorpl').value;
	var codigoprod2 = document.getElementById('codcpl').value;
	var sku2 = codigoprod2+""+colorseleccionado2;
	var selectalla = "<select name=\"tam\" class=\"lista\" id=\"tam\" onChange=\"update_exist();\"><option value=\"0\">Seleccione la Talla</option>";
	var ind2;
	for(ind2 = 0; ind2 < variante.length; ind2++){
		if(variante[ind2][0].substring(0,11) == sku2){		
			//alert("valor:"+variante[ind2][0].substring(0,11)+"\n sku="+sku2);
			selectalla +="<option value=\""+variante[ind2][1]+"\">"+variante[ind2][1]+"</option>"; 
		}
	}
	selectalla += "</select>"; 
	
	/*
	if(variante.length == 0){
		selectalla = "no disponible para este modelo";
		document.getElementById('combotamjsy').innerHTML = "";
	}
	*/
	
	document.getElementById('div_wsjsy').innerHTML = selectalla;
}

//------------------------ Para el short --------------------------------------------------------------
//Llamada cada q se desliza una prenda short.
function short_exist() { 
	if(document.getElementById('shortsel').value==""){
		document.getElementById('div_wssht').innerHTML = "<span>No se ha seleccionado un Short</span>";
	}
		
	else if (preciosReqsh.readyState == 4 || preciosReqsh.readyState == 0) {
		var str2 = escape(document.getElementById('codcsh').value); //Campo de codigo del jersey.
		//alert(str2);
		preciosReqsh.open("GET", 'ajax_pycsht.php?cod_short=' + str2, true);
		preciosReqsh.onreadystatechange = handleshort_exist; 
		preciosReqsh.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleshort_exist() {
	//Crea resultado de la consulta.
	var ss2 = document.getElementById('div_wssht');//Etiqueta DIV de resultados.
	
	 if (preciosReqsh.readyState==1) {
                 ss2.innerHTML="<span>Actualizando informaci&oacute;n.......</span>";
         }
	if (preciosReqsh.readyState == 4) {
		ss2.innerHTML = '';
		cadena2 = preciosReqsh.responseText;
		//alert(cadena2);
		//var respuesta = escape(charstring); 
			var str2 = cadena2.split("|");
			document.getElementById('preciosht').innerHTML = str2[0];
			document.getElementById('precioshttc').innerHTML = str2[0];
			document.getElementById('preciocsh').value = str2[0];
					
			var i2;
			var j2;
			j2=0;
			variantesh = null;
			variantesh = new Array (); 
			for(i2=1; i2 < str2.length - 1; i2++) {
				//Build our element string.  This is cleaner using the DOM, but
				//IE doesn't support dynamically added attributes.
				variantesh[j2] = new Array(str2[i2],str2[i2+1],str2[i2+2]);
				j2++;
				i2+2;
			}
			update_tallash();
	}
}


function update_existsh(){
	var tamsel = document.getElementById('tamsh').value;
	var colorseleccionado = document.getElementById('colorsh').value;
	var codigoprod = document.getElementById('codcsh').value;
	var sku = codigoprod+""+colorseleccionado+""+tamsel;
	
	for(ind = 0; ind < variantesh.length; ind++){
		if(variantesh[ind][0] == sku){		
			selectamano = "<select name=\"cantidadsh\" class=\"lista\" id=\"cantidadsh\"><option selected value=\"0\">Seleccione una cantidad</option>";
            if(variantesh[ind][2]>=1) { selectamano +="<option value=\"1\">1</option>"; }
			if(variantesh[ind][2]>=2) { selectamano +="<option value=\"2\">2</option>"; }
			if(variantesh[ind][2]>=3) { selectamano +="<option value=\"3\">3</option>"; }
			if(variantesh[ind][2]>=4) { selectamano +="<option value=\"4\">4</option>"; }
			if(variantesh[ind][2]>=5) { selectamano +="<option value=\"5\">5</option>"; }
            selectamano += "</select>"; 
			document.getElementById('combotamsht').innerHTML = selectamano;
			
		}
	}

}

function update_tallash(){
	var colorseleccionado2 = document.getElementById('colorsh').value;
	var codigoprod2 = document.getElementById('codcsh').value;
	var sku2 = codigoprod2+""+colorseleccionado2;
	var selectalla = "<select name=\"tamsh\" class=\"lista\" id=\"tamsh\" onChange=\"update_existsh();\"><option value=\"0\">Seleccione la Talla</option>";
	var ind2;
	for(ind2 = 0; ind2 < variantesh.length; ind2++){
		if(variantesh[ind2][0].substring(0,11) == sku2){		
			//alert("valor:"+variante[ind2][0].substring(0,11)+"\n sku="+sku2);
			selectalla +="<option value=\""+variantesh[ind2][1]+"\">"+variantesh[ind2][1]+"</option>"; 
		}
	}
	selectalla += "</select>"; 
	document.getElementById('div_wssht').innerHTML = selectalla;
}
