var RequestObject = false;
// var Archivo = 'http://localhost/ajax.php'; //directorio donde tenemos el archivo ajax.php

if (window.XMLHttpRequest) //
RequestObject = new XMLHttpRequest();

if (window.ActiveXObject)
RequestObject = new ActiveXObject("Microsoft.XMLHTTP");

function ReqChange()
{
	if (RequestObject.readyState==4)
		{
			if (RequestObject.responseText.indexOf('invalid') == -1)
			{
				document.getElementById("mostrar").innerHTML=RequestObject.responseText;
			}else {
				document.getElementById("mostrar").innerHTML = "Error llamando";
			}
		}
}

function IsEmpty(mytext) 
{
	var re = "/^\s{1,}$/g"; //match any white space including space, tab, form-feed, etc.
	if ((mytext.length==0) || (mytext==null)) 
		{
			return true;
		}
	else 
		{
			return false;
		}
}

function cambiarProducto(prod,resaltado,cod,ruta,ruta_g){
	//document.getElementById("img_nva").src=ruta_g;
	maximo=document.getElementById("maximo").value;
	//for(i=1;i<(maximo);i++)
	//{
		//document.getElementById("flow_"+i).style.background="#F6F5EA";
	//}
	//alert(resaltado);
	//alert(document.getElementById("imagencolor"+resaltado).border);	
	//document.getElementById("imagencolor"+resaltado).style.border="solid 4px #333333";
	links = "ajax/productos/cambiosku.php?prod="+cod+"&tabla=tbl_product&dato=pd_id&ruta="+ruta+"&ruta2="+ruta_g;
	//alert(links);
	RequestObject.open("GET",links,true);
	RequestObject.send(null);
	RequestObject.onreadystatechange = cargar_resultado;
	document.getElementById("asociados").style.display="block";
}

function cambiarProductoSimple(prod,resaltado,cod,ruta,ruta_g){
	//alert(prod);
	//document.getElementById("imagenZoom").href=prod;
	//alert(prod);
	document.getElementById("marcofoto1").style.display="none";
	document.getElementById("img_nva").src=ruta_g;
	document.getElementById("marcofoto2").href=prod;
	links = "ajax/productos/cambiosku.php?prod="+cod+"&tabla=tbl_product&dato=pd_id&ruta="+ruta+"&ruta2="+ruta_g;
	RequestObject.open("GET",links,true);
	RequestObject.send(null);
	RequestObject.onreadystatechange = cargar_resultado;
	document.getElementById("asociados").style.display="block";
	document.getElementById("marcofoto1").style.display="block";	
}
function cargar_resultado()
{
	var TablaAsociados, TablaDivsAdicionales;
	var Formulario;
	var band = true;
	if ((RequestObject.readyState==1) || (RequestObject.readyState==2) || (RequestObject.readyState==3))
	{
	//document.getElementById("imagen").src="../modules/facebox/loading.gif";		
	}
	
	if(RequestObject.readyState==4)
	{
		hola = RequestObject.responseXML;
		h1=hola.getElementsByTagName("hijos");
		f1=hola.getElementsByTagName("foto");
		nombre=hola.getElementsByTagName("nombre");
		d1=hola.getElementsByTagName("descripcion");				
		pp1=hola.getElementsByTagName("principal_precios_hijos");
		p1=hola.getElementsByTagName("precios_hijos");
		c1=hola.getElementsByTagName("codigo");
		ch1=hola.getElementsByTagName("codigo_hijos");		
		cat=hola.getElementsByTagName("asociado_cantidad_0");		
		i1=hola.getElementsByTagName("imagen");
		izomm1=hola.getElementsByTagName("fotozoom");	
		descri_hijos = hola.getElementsByTagName("descripcion_hijos");
		cat1 = hola.getElementsByTagName("cat");	
		subcat1 = hola.getElementsByTagName("subcat");
		dispon = hola.getElementsByTagName("disponible");	
		//alert(dispon[0].firstChild.nodeValue);
		disponHijo = hola.getElementsByTagName("cantHijo");	
		tipomuestra=hola.getElementsByTagName("tipomuestra");
		
		// La siguiente linea cambia la imagen del producto nuevo con menor precio
		document.getElementById("img_nva").src=i1[2].firstChild.nodeValue;
		if (IsEmpty(dispon))
			{	
				if (dispon[0].firstChild.nodeValue==0)
					{
						band = false;
						document.getElementById("agotadoProductoPrincipal").style.display="block";
						document.getElementById("cantProductoPrincipal").style.display ="none";
					} else {
						document.getElementById("cantProductoPrincipal").style.display="block";			
						document.getElementById("agotadoProductoPrincipal").style.display="none";
						
					}
			} else {
				document.getElementById("agotadoProductoPrincipal").style.display="none";
				document.getElementById("cantProductoPrincipal").style.display ="block";
			}
			
		extra_img=hola.getElementsByTagName("extra");
		totalHijos=hola.getElementsByTagName("totalhijos");
		maximo=h1.length;
		document.getElementById("extras_img").innerHTML = extra_img[0].firstChild.nodeValue;
		document.getElementById("form1").action="cart.php?action=add&imagen="+i1[0].firstChild.nodeValue+"&p="+c1[0].firstChild.nodeValue;

		document.getElementById("descripcion").innerHTML=d1[0].firstChild.nodeValue;
		document.getElementById("titulo_prod").innerHTML=nombre[0].firstChild.nodeValue;
		document.getElementById("asociado_cantidad_0").value=1;
		vTituloPD = nombre[0].firstChild.nodeValue;
		//document.getElementById("titulo_pd_name").innerHTML = vTituloPD;
		
		document.getElementById("pp_cantidad").value = 0;			
		document.getElementById("pp_precio").value = pp1[0].firstChild.nodeValue;			
		document.getElementById("pp_id").value = c1[0].firstChild.nodeValue;		

		document.getElementById("precio_int").innerHTML= "BsF. " + pp1[0].firstChild.nodeValue;
		document.getElementById("imagenNueva").src=i1[0].firstChild.nodeValue;
		document.getElementById("ShowImagenNueva").href="javascript:muestraAllProduct('"+nombre[0].firstChild.nodeValue+"','"+i1[2].firstChild.nodeValue+"','BsF. "+pp1[0].firstChild.nodeValue+"','"+d1[0].firstChild.nodeValue+"');";
		//alert(i1[2].firstChild.nodeValue);
	
		
		document.getElementById("asociados").innerHTML="";
		document.getElementById("precios").style.display="block";
		TablaAsociados = "";
		TablaHeader = "";
		TablaDivsAdicionales = "";
	for(var i=0;i<(maximo);i++)
		{
			
			var cat, subcat, pdid, nombrep
			cat = cat1[i].firstChild.nodeValue;
			subcat = subcat1[i].firstChild.nodeValue;
			pdid = ch1[i].firstChild.nodeValue;
			nombrep = h1[i].firstChild.nodeValue;
			descri = descri_hijos[i].firstChild.nodeValue;
			disHijo = disponHijo[i].firstChild.nodeValue;

			var preciosItem = (p1[i].firstChild.nodeValue * 1.09);
			var totalpreciosItem = preciosItem.toFixed(2);

			TablaAsociados +="<tr align=\"left\">"+
				"<td rowspan=\"3\">"+
					"<table width=\"0%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#f6f5ea\" align=\"left\">"+
					"<tr>"+
					"<td>"+
					"<a href=\"javascript:muestraAllProduct('"+h1[i].firstChild.nodeValue+"','"+izomm1[i].firstChild.nodeValue+"','BsF."+ p1[i].firstChild.nodeValue+"','"+descri_hijos[i].firstChild.nodeValue+"');\"><img src='"+f1[i].firstChild.nodeValue+"' border=\"0\"  width=\"100\" height=\"100\" vspace=\"0\" hspace=\"0\" ></a>"+
					"</td>"+
					"</tr>"+
					"</table>"+
				"</td>"+
				"<td class=\"textoSimple1\" colspan=\"2\">"+h1[i].firstChild.nodeValue +"</td></tr>"+
				"<tr>"+
				"<td class=\"texto-simple\" align=\"right\" colspan=\"2\" height=\"19\" ><img src=\"images/precio_cantidad.png\" width=\"204\" height=\"19\" vspace=\"5\"/></td>"+
				"</tr>"+
				"<tr><td class=\"precio\" width=\"100\">BsF."+ p1[i].firstChild.nodeValue +"</td>"+
				"<td class=\"texto-simple\" width=\"50px\"  align=\"right\">"+			
				"<div name='"+i+"'>";
			if (disHijo=='0')
				{
					TablaAsociados += "<span class='agotado'>AGOTADO</span>";
				} else {
					TablaAsociados += "<table width=\"0%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"right\">"+
						"<tr>"+
						"<a name=\"cantidad"+i+"\" id=\"cantidad"+i+"\"></a>"+
						"<td><input name='asociado_cantidad"+i+"' id='asociado_cantidad"+i+"' type='text' value='0' size='4' maxlength='3'/></td>"+
						"<td><a href=\"#"+i+"\" onclick=\"sumar('asociado_cantidad"+i+"','a_cantidad"+i+"','"+pdid+"'); return\"><img src=\"images/plus_button.gif\" width=\"15\" height=\"10\"  border=\"0\"/></a><br />"+
						"<a href=\"#"+i+"\"  onclick=\"restar('asociado_cantidad"+i+"','a_cantidad"+i+"'); return\"><img src=\"images/minus_button.gif\" width=\"15\" height=\"10\" border=\"0\" /></a>"+
						"</td>"+
						"<td class=\"texto-simple\">"+
						"	<img src=\"images/separador.gif\" width=\"5\"  vspace=\"5\"/>"+
						"</td></tr></table>";			
				}
			TablaAsociados +="</div></td><tr rowspan=\"3\"><td height=\"24\" >&nbsp;</td></tr>";
			document.getElementById("a_cantidad"+i).value = 0;			
			document.getElementById("a_precio"+i).value = p1[i].firstChild.nodeValue;			
			document.getElementById("a_id"+i).value = ch1[i].firstChild.nodeValue;		
			
			if (TablaAsociados!="")
			{
				TablaAsociados = TablaHeader + TablaAsociados; //+ TablaFooter	
			} else {
				TablaAsociados = TablaAsociados;	// + TablaFooter
			}
			document.getElementById("asociados").innerHTML = "<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">"+ TablaAsociados+ "</table>";
			document.getElementById("asociado_total").value = totalHijos[0].firstChild.nodeValue;
		} 
		
		/*
		if(band == true){
			document.getElementById("botonAgregarCarrito").style.display="block";
		}else{
			document.getElementById("botonAgregarCarrito").style.display="none";
		}
		*/
		if (tipomuestra[0].firstChild.nodeValue=="color") 
			{
				document.getElementById("imagentipomuestra").src="images/boton_vercolores_2.png";
			}
		if (tipomuestra[0].firstChild.nodeValue=="tamano") 
			{
				document.getElementById("imagentipomuestra").src="images/boton_vertamano2.png";
			}
		
	} 
}
function modificar_banco_de_imagenes(ruta,codigo){
	RequestObject.open("GET","funciones/banco_imagenes.php?ruta="+ruta+"&codigo="+codigo,true);
	RequestObject.send(null);
	RequestObject.onreadystatechange= mostrar_banco_img;
}

function mostrar_banco_img()
{
	if(RequestObject.readyState==4)
	{
		 document.getElementById("relacion_imagenes").innerHTML=RequestObject.responseText;
	}
}

function CambiarPrecio(precio,codigo)
{
	document.getElementById("precio").value=precio;
	document.getElementById("form1").action="cart.php?action=add&p="+codigo;
}

function TraerCombo(){
	combo=document.getElementById("segun").value;
	if(combo=="---"){
		
	}else{
		RequestObject.open("GET","comboload.php?combo="+combo,true);
		RequestObject.send(null);
		RequestObject.onreadystatechange=cargar_combo;
	}
}

function cargar_combo()
{
	if(RequestObject.readyState==4)
	{
		hola=RequestObject.responseXML;
		v1=hola.getElementsByTagName("valor");
		t1=hola.getElementsByTagName("texto");
		maximo=t1.length;
		document.getElementById("producto_ppal").length=0;
		document.getElementById("producto_ppal").options[0] = new Option(t1[0].firstChild.nodeValue,v1[0].firstChild.nodeValue,"defaultChecked");
	}else{
	}
}

function cambio_sub_categoria(){
	categoria=document.getElementById("cboCategory").value;
	if(categoria==""){
		
	}else{
		RequestObject.open("GET","categorias.php?categoria="+categoria,true);
		RequestObject.send(null);
		RequestObject.onreadystatechange=mostrar_subs;
	}
}

function mostrar_subs(){
	comboSubs=document.getElementById("cboSubCat");
	if(RequestObject.readyState==4){
		// alert(RequestObject.responseText);
		comboSubs.length=0;
		epa=RequestObject.responseXML;
		valores=epa.getElementsByTagName("valor");
		textos=epa.getElementsByTagName("texto");
		maximo=valores.length;
		for(var j=0;j<maximo;j++){
			comboSubs.options[j] = new Option(textos[j].firstChild.nodeValue,valores[j].firstChild.nodeValue);
		}
	}
}

function cambiarProducto2(prod,resaltado,tabla,dato){
	RequestObject.open("GET","funciones/cambiosku.php?prod="+prod+"&tabla="+tabla+"&dato="+dato,true);	
	RequestObject.send(null);
	total=document.getElementById("maximo").value;
	for(var h=1;h<total;h++)
		{
			document.getElementById(h).style.background="#F6F5EA";
		}
	document.getElementById(resaltado).style.background="#006600";
	RequestObject.onreadystatechange=cargar_resultado;
}

function cargar_resultado2(){
	if(RequestObject.readyState==4){
		hola = RequestObject.responseText;
		document.getElementById("asociados").innerHTML = hola;
	}
}