function crearInstancia() {

 XMLHttp = false;

  if (window.XMLHttpRequest){
   return new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
   var versiones = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0",
    "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
    for (var i=0; i< versiones.length; i++){
     try {
     XMLHttp = new ActiveXObject(versiones[i]);
     if (XMLHttp){
     return XMLHttp;
     break;
     }
    } catch (e) {};
    }
  }
}


function cargar_opciones_busqueda(patron){
	XMLHttp = crearInstancia();
	if (XMLHttp){
		var url='servicios.php?servicio=llenar_busqueda_nombre&patron=' + patron;
		XMLHttp.open("GET",url,true);
		XMLHttp.onreadystatechange=llenaOpcionesBusqueda;
		XMLHttp.send(null);
	}
	else{
		alert('No se puede crear la instancia de XMLHttp');
	}
}
function llenaOpcionesBusqueda(){
	 if (XMLHttp.readyState==4){
	 	document.getElementById("divOpcionesBusqueda").innerHTML = XMLHttp.responseText
	 }
}
function limpiarDiv(divId) {
	document.getElementById(divId).innerHTML = '';
}
var array_imagenes_color = new Array();
var array_precio_color = new Array();
var array_moneda_color = new Array();

function cargar_colores(articulo,talle){
	if(document.getElementById("talles").selectedIndex > 0){
		document.getElementById("colores").disabled=false
		if(document.getElementById("preview_color").selectedIndex > 0) {
			document.getElementById("boton_compra").disabled = false;
			document.getElementById("preview_color").style.display="block"
		} else {
			document.getElementById("preview_color").style.display="none";
			document.getElementById("boton_compra").disabled = true;
		}
	} else {
		document.getElementById("colores").selectedIndex = 0
		document.getElementById("colores").disabled = true
		document.getElementById("preview_color").style.display="none"
		document.getElementById("boton_compra").disabled = true;	
	}
	
	XMLHttp = crearInstancia();
	if (XMLHttp){
		var url='servicios.php?servicio=stock_colores&talle=' + talle + '&producto=' + articulo;
		XMLHttp.open("GET",url,true);
		XMLHttp.onreadystatechange = function() {
			if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
				var xmlDocument = XMLHttp.responseXML;
				cod_colores = xmlDocument.getElementsByTagName("codigo");
				nom_colores = xmlDocument.getElementsByTagName("nombre");
				array_imagenes_color = xmlDocument.getElementsByTagName("imagen");
				array_precio_color = xmlDocument.getElementsByTagName("precio");
				array_moneda_color = xmlDocument.getElementsByTagName("moneda");
				//escribo_colores(cod_colores,nom_colores);
				listoptions(nom_colores,cod_colores,'colores');

			}
		}
	}
	else{
		alert('No se puede crear la instancia de XMLHttp');
	}
	XMLHttp.send(null);
}
function cargar_colores_selected(articulo,talle,color){
	if(document.getElementById("talles").selectedIndex > 0){
		document.getElementById("colores").disabled=false
		if(document.getElementById("preview_color").selectedIndex > 0) {
			document.getElementById("boton_compra").disabled = false;
			document.getElementById("preview_color").style.display="block"
		} else {
			document.getElementById("preview_color").style.display="none";
			document.getElementById("boton_compra").disabled = true;
		}
	} else {
		document.getElementById("colores").selectedIndex = 0
		document.getElementById("colores").disabled = true
		document.getElementById("preview_color").style.display="none"
		document.getElementById("boton_compra").disabled = true;	
	}
	
	XMLHttp = crearInstancia();
	if (XMLHttp){
		var url='servicios.php?servicio=stock_colores&talle=' + talle + '&producto=' + articulo;
		XMLHttp.open("GET",url,true);
		XMLHttp.onreadystatechange = function() {
			if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
				var xmlDocument = XMLHttp.responseXML;
				cod_colores = xmlDocument.getElementsByTagName("codigo");
				nom_colores = xmlDocument.getElementsByTagName("nombre");
				array_imagenes_color = xmlDocument.getElementsByTagName("imagen");
				array_precio_color = xmlDocument.getElementsByTagName("precio");
				array_moneda_color = xmlDocument.getElementsByTagName("moneda");

				// Traigo la funcion aca porque tengo que manejar el seleccionado.
				//listoptions(nom_colores,cod_colores,'colores');
				var loopIndex;
				var selectControl = document.getElementById("colores");
				var selec;
				selectControl.options.length = 0;
				
				for (loopIndex = 0; loopIndex < cod_colores.length ; loopIndex++ ){
					if(cod_colores[loopIndex].lastChild.data == color){
						selec=true;
					}
					else{
						selec=false;
					}	
					////// Porque uno tiene FirstChild y el Otro lastChild?
					selectControl.options[loopIndex] = new Option(nom_colores[loopIndex].firstChild.data,cod_colores[loopIndex].lastChild.data,false,selec);
				}
			}
		}
	}
	else{
		alert('No se puede crear la instancia de XMLHttp');
	}
	document.getElementById("boton_compra").disabled = false;
	XMLHttp.send(null);
}

function listoptions(titulos,valores, combo) {
	var loopIndex;
	var selectControl = document.getElementById(combo);
	selectControl.options.length = 0;
	
	for (loopIndex = 0; loopIndex < valores.length ; loopIndex++ ){
		////// Porque uno tiene FirstChild y el Otro lastChild?
    	selectControl.options[loopIndex] = new Option(titulos[loopIndex].firstChild.data,valores[loopIndex].lastChild.data);
	}
	/*
	var loopIndex;
	var selectControl = document.getElementById(combo);
	selectControl.options.length = 0;
    selectControl.options[0] = new Option("Seleccione Color",0);
	for (loopIndex = 1; loopIndex < valores.length+1 ; loopIndex++ ){
		////// Porque uno tiene FirstChild y el Otro lastChild?
    	selectControl.options[loopIndex] = new Option(titulos[loopIndex-1].firstChild.data,valores[loopIndex-1].lastChild.data);
	}
	*/	
}

function mostrar_boton(){
	if(document.getElementById("colores").selectedIndex > 0) {
		document.getElementById("boton_compra").disabled = false;
		document.getElementById("preview_color").style.display = "block"
		document.getElementById("preview_color").innerHTML = '<img src="' + array_imagenes_color[document.agregado_articulo.colores.selectedIndex].firstChild.data + '"/>';
		document.getElementById("precio").innerHTML = array_moneda_color[document.agregado_articulo.colores.selectedIndex].firstChild.data + " " + array_precio_color[document.agregado_articulo.colores.selectedIndex].firstChild.data;
	} else {
		document.getElementById("boton_compra").disabled = true;
		document.getElementById("preview_color").style.display = "none"
	}
}

function sumar_a_carrito_tallecolor(articulo,talle,color,cantidad){
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block'
	//document.getElementById("modalBack").style.display="block";
	//carrito_AlertCarrito();
	XMLHttp = crearInstancia();
	if (XMLHttp){
		var url='servicios.php?servicio=sumar_a_carrito&producto=' + articulo + '&color=' + color + '&talle=' + talle + '&cantidad=' + cantidad;
		XMLHttp.open("GET",url,true);
		XMLHttp.onreadystatechange = function() {
			if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
				var xmlDocument = XMLHttp.responseXML;
				monto = xmlDocument.getElementsByTagName("monto");
				cantidad = xmlDocument.getElementsByTagName("cantidad");
				document.getElementById('monto_carrito').innerHTML=monto[0].firstChild.data;
				document.getElementById('monto_carrito_alert').innerHTML=monto[0].firstChild.data;
				document.getElementById('cantidad_carrito').innerHTML=cantidad[0].firstChild.data;
				document.getElementById('cantidad_carrito_alert').innerHTML=cantidad[0].firstChild.data;
			}
		}
	}
	else{
		alert('No se puede crear la instancia de XMLHttp');
	}
	XMLHttp.send(null);
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
/*
function carrito_AlertCarrito() {
	$('divCarrito').style.top =getScrollXY()[1]+ window.screen.height/2 - 100 + "px";
	$('divCarrito').style.display = '';
}

function carrito_CloseCarrito() {
	$('divCarrito').style.display = 'none';
	$('modalBack').style.display = 'none';

}
*/

function sumar_a_carrito(articulo,cantidad){
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';
	//document.getElementById("modalBack").style.display="block";
	//carrito_AlertCarrito();
	XMLHttp = crearInstancia();
	if (XMLHttp){
		var url='servicios.php?servicio=sumar_a_carrito&producto=' + articulo + '&cantidad=' + cantidad;
		XMLHttp.open("GET",url,true);
		XMLHttp.onreadystatechange = function() {
			if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
				var xmlDocument = XMLHttp.responseXML;
				monto = xmlDocument.getElementsByTagName("monto");
				cantidad = xmlDocument.getElementsByTagName("cantidad");
				document.getElementById('monto_carrito').innerHTML=monto[0].firstChild.data;
				document.getElementById('monto_carrito_alert').innerHTML=monto[0].firstChild.data;
				document.getElementById('cantidad_carrito').innerHTML=cantidad[0].firstChild.data;
				document.getElementById('cantidad_carrito_alert').innerHTML=cantidad[0].firstChild.data;
				
			}
		}
	}
	else{
		alert('No se puede crear la instancia de XMLHttp');
	}
	XMLHttp.send(null);
}

function actualizar_carrito(articulo,cantidad, indice,tieneiva){
	if(isNaN(cantidad)) {
		alert(alerta_car_nro);
	}else {
		if (cant_decimales < 1) {
			elemento = eval("document.form.CarCan" + indice);
			elemento.value = parseInt(cantidad);
			cantidad = parseInt(cantidad);
		}
		XMLHttp = crearInstancia();
		if (XMLHttp){
			var url='servicios.php?servicio=actualiza_cant_carrito&producto=' + articulo + '&cantidad=' + cantidad + '&indice=' + indice;
			
			XMLHttp.open("GET",url,true);
			XMLHttp.onreadystatechange = function() {
				if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
					var xmlDocument = XMLHttp.responseXML;
					a1 = xmlDocument.getElementsByTagName("montoprod");
					a2 = xmlDocument.getElementsByTagName("ivaprod");
					a3 = xmlDocument.getElementsByTagName("monto");
					a4 = xmlDocument.getElementsByTagName("cantidad");
					a5 = xmlDocument.getElementsByTagName("subtotal");
					a6 = xmlDocument.getElementsByTagName("totaliva");
					a7 = xmlDocument.getElementsByTagName("simbolo");
					a8 = xmlDocument.getElementsByTagName("valor");
					a9 = xmlDocument.getElementsByTagName("leyendavol");
					a10 = xmlDocument.getElementsByTagName("montovol");
					a11 = xmlDocument.getElementsByTagName("totalvol");
					a12 = xmlDocument.getElementsByTagName("unitprod");
					document.getElementById('subtot' + indice).innerHTML=a1[0].firstChild.data;
					document.getElementById('unitprod' + indice).innerHTML=a12[0].firstChild.data;
					
					/*PARA DESCUENTO*/
					
					if(tieneiva ==  true) {
						document.getElementById('iva' + indice).innerHTML=a2[0].firstChild.data;
						document.getElementById('subtotal').innerHTML=a5[0].firstChild.data;
						document.getElementById('totaliva').innerHTML=a6[0].firstChild.data;
					}
					document.getElementById('monto_carrito').innerHTML=a3[0].firstChild.data;
					
					//si NO TIENE descuento por volumen pongo el total comun, si tiene pongo el que viene en el XML
					if(a10[0].firstChild.data == "-") {
						document.getElementById('total').innerHTML=a3[0].firstChild.data;
						document.getElementById('leyvol').innerHTML="";
						document.getElementById('montovol').innerHTML="";
					} else {
						document.getElementById('total').innerHTML=a11[0].firstChild.data;
						document.getElementById('leyvol').innerHTML=a9[0].firstChild.data;
						document.getElementById('montovol').innerHTML=a10[0].firstChild.data;
						
					}
					document.getElementById('cantidad_carrito').innerHTML=a4[0].firstChild.data;
					document.getElementById('totart').innerHTML=a4[0].firstChild.data;
					

					if(navigator.appName=="Netscape"){
						document.getElementById("submonedas").innerHTML = " ";
					}
					if((navigator.appName=="Microsoft Internet Explorer") ||  (navigator.appName=="Opera")){
						for(j=0; j <= document.getElementById("submonedas").childNodes.length ; j++){
							document.getElementById("submonedas").deleteRow(0);
						}
					}
					for(i=0; i < a7.length; i++) {
						
						agregar_fila(a7[i].firstChild.data,a8[i].firstChild.data);
					}
					resultadoCupon = xmlDocument.getElementsByTagName("resultado");
					mensajeCupon = xmlDocument.getElementsByTagName("mensaje");
					tituloCupon = xmlDocument.getElementsByTagName("titulo");
					montoCupon = xmlDocument.getElementsByTagName("monto");
					nominalCupon = xmlDocument.getElementsByTagName("nominal");
					condescuento = xmlDocument.getElementsByTagName("total_con_descuento");
					sindescuento = xmlDocument.getElementsByTagName("total_sin_descuento");
					if(resultadoCupon[0].firstChild.data == 'S') {
						document.getElementById("info_cupon").className="tds_detalle visible";
						document.getElementById("cupon").className="oculto";
						document.getElementById("boton_asigna_cupon").className="oculto";
						document.getElementById('monto_descuento').innerHTML=nominalCupon[0].firstChild.data;
						document.getElementById('sin_descuento').innerHTML=sindescuento[0].firstChild.data;	
						document.getElementById('con_descuento').innerHTML=condescuento[0].firstChild.data;
						document.getElementById('total').innerHTML=condescuento[0].firstChild.data;
					}
				}
			}
		}
		else{
			alert('No se puede crear la instancia de XMLHttp');
		}
		XMLHttp.send(null);
	}
}



function agregar_fila(simbolo,valor){
	var tr1  = document.createElement("tr");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var td3 = document.createElement("td");
	td1.innerHTML = "&nbsp;";
	td2.innerHTML = simbolo;
	td3.innerHTML = valor;	
	tr1.appendChild(td1);
	tr1.appendChild(td2);
	tr1.appendChild(td3);
	//td1.style.background="#e0e0e0";
	tr1.className="tds";
	td1.className="tds";
	td2.className="tds";
	td3.className="tds";
	td1.style.padding="3px";
	td2.style.padding="3px";
	td3.style.padding="3px";
	//td2.style.background="#e0e0e0";
	//td2.style.font="normal 10px verdana";
	//td3.style.background="#e0e0e0";
	//td3.style.font="normal 10px verdana";
	var pepe = document.getElementById("submonedas");
	pepe.appendChild(tr1);
	td2.width="75";
	td2.align = "center";
	td3.width="75";
	td3.align="right";
}

function llama_cupon() {
	nrocupon = document.getElementById("cupon").value;
	if(nrocupon == "") {
		alert("Ingrese un código de cupón válido");
	} else {
		
		XMLHttp = crearInstancia();
		
		if (XMLHttp){
			var url='servicios.php?servicio=imputar_descuento&cupon=' + nrocupon;
			XMLHttp.open("GET",url,true);
			
			XMLHttp.onreadystatechange = function() {
				if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
					var xmlDocument = XMLHttp.responseXML;
					
					resultado = xmlDocument.getElementsByTagName("resultado");
					
					mensaje2 = xmlDocument.getElementsByTagName("mensaje");
					titulo2 = xmlDocument.getElementsByTagName("titulo");
					monto2 = xmlDocument.getElementsByTagName("monto");
					nominal2 = xmlDocument.getElementsByTagName("nominal");
					condescuento2 = xmlDocument.getElementsByTagName("total_con_descuento");
					sindescuento2 = xmlDocument.getElementsByTagName("total_sin_descuento");
					
					if(resultado[0].firstChild.data == 'S') {
						
						document.getElementById("info_cupon").className="visible tds_detalle";
						document.getElementById("cupon").className="oculto";
						document.getElementById("boton_asigna_cupon").className="oculto";
						document.getElementById("ley_cupones_texto").className="oculto";
						

						document.getElementById('mensaje').innerHTML="<div style='background:url(imagenes/cuponOK.gif) 3px 3px no-repeat #48a931; font:bold 11px arial;color:#fff; padding:8px 30px; margin:5px 0'>" + mensaje2[0].firstChild.data + "</div>";
						document.getElementById('titulo_cupon').innerHTML=titulo2[0].firstChild.data;
						document.getElementById('monto_descuento').innerHTML=nominal2[0].firstChild.data;
						document.getElementById('sin_descuento').innerHTML=sindescuento2[0].firstChild.data;	
						document.getElementById('con_descuento').innerHTML=condescuento2[0].firstChild.data;
						document.getElementById('nro_cupon').innerHTML=nrocupon;
						document.getElementById('total').innerHTML=condescuento2[0].firstChild.data;
						
					} else  {
						
						document.getElementById("cupon").className="visible";
						document.getElementById("boton_asigna_cupon").className="visible";
						document.getElementById('nro_cupon').innerHTML=nrocupon;
						document.getElementById('mensaje').innerHTML="<div style='background:url(imagenes/cuponMAL.gif) 3px 3px no-repeat #fff0bd; font:bold 11px arial;color:#f00; padding:8px 30px; margin:5px 0'>" + mensaje2[0].firstChild.data + "</div>";		
						document.getElementById('total').innerHTML=sindescuento2[0].firstChild.data;;
						
					}
				}
			}
		}
		else{
			alert('No se puede crear la instancia de XMLHttp');
		}
		XMLHttp.send(null); 
	}
}

function quito_cupon() {
	XMLHttp = crearInstancia();
		if (XMLHttp){
			var url='servicios.php?servicio=quitar_descuento';
			XMLHttp.open("GET",url,true);
			XMLHttp.onreadystatechange = function() {
				if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {
					var xmlDocument = XMLHttp.responseXML;


					document.getElementById("info_cupon").className="visible";
					document.getElementById("cupon").className="visible";
					document.getElementById("boton_asigna_cupon").className="visible";
					document.getElementById("ley_cupones_texto").className="visible tds_detalle";
					document.getElementById('mensaje').innerHTML="";
					document.getElementById("info_cupon").className="oculto";
					document.getElementById("cupon").value = "";
					sindescuento = xmlDocument.getElementsByTagName("total_sin_descuento");
					document.getElementById('total').innerHTML=sindescuento[0].firstChild.data;;
					
				}
			}
		}
		else{
			alert('No se puede crear la instancia de XMLHttp');
		}
		XMLHttp.send(null);
}

//para cuando carga el carrito ya con un cupon asignado anteriormente

