// JavaScript Document
var ref;
var cantidadanterior;
function cambiaClass(ele, clase){
	document.getElementById(ele.id).className=clase;
}
var i; 
var imagenes = new Array('/images/logoMUNDIGARDEN.jpg', '/images/fondobotonON.jpg', '/images/fondobotonON.jpg','/images/fondobtn_enviar.png'); 
var lista_imagenes = new Array(); 

function cargarimagenes(){ 
for(i in imagenes){ 
lista_imagenes[i] = new Image(); 
lista_imagenes[i].src = imagenes[i]; 
} 
} 
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
function cerrar(){
	document.getElementById('divfoto').style.visibility="hidden";
	document.getElementById('divconfoto').style.visibility="hidden";
	document.getElementById('imgFoto').src ='/images/carga.gif';
}
function mostrar(img){
	document.getElementById('divconfoto').style.visibility="visible";
	document.getElementById('divfoto').style.visibility="visible";
	document.getElementById('imgFoto').src =  img ; 
}
function actualiza(ref){
	var cantidad;
	cantidad=document.getElementById(ref).value;
	cantidad= parseInt(cantidad);
	document.getElementById(ref).value=cantidad;
		if(cantidad==cantidadanterior){
		return;
	}
if(isNaN(cantidad)==true){
		 alert("Only number");
		 document.getElementById(ref).value=0;
		 cantidad=0;
		 }
	document.getElementById('carrito').innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	document.getElementById('importeLinea'+ref).innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	document.getElementById('totalPrecio').innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	var aleatorio=Math.random();
	ajax=nuevoAjax(); 
ajax.open("GET", "actualizaLinea.php?ref="+ref+"&cantidad="+cantidad+"&aleatorio="+aleatorio,true); 
ajax.onreadystatechange=function() { 
if (ajax.readyState==4) { 
	devuelto=ajax.responseText.split("|");
    document.getElementById('carrito').innerHTML=devuelto[0];
	document.getElementById('importeLinea'+ref).innerHTML=devuelto[1];
	document.getElementById('totalPrecio').innerHTML=devuelto[2];
   } 
} 
ajax.send(null) 
actualizaDtoApicable();
actualizaTotalAPagar();
}
function nuevoAjax(){ 
  var xmlhttp=false; 
  try { 
   // Creación del objeto ajax para navegadores diferentes a Explorer 
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) { 
   // o bien 
   try { 
     // Creación del objet ajax para Explorer 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { 
     xmlhttp = false; 
   } 
  } 

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
   xmlhttp = new XMLHttpRequest(); 
  } 
  return xmlhttp; 
} 
function cargaValor(refanterior){
	ref=refanterior;
	cantidadanterior=document.getElementById(ref).value;
}
//admin
function actualizaPrecio(ref,Destino,Etiqueta){
	var cantidad;
	cantidad=document.getElementById(Etiqueta).value;
	document.getElementById(Etiqueta).value=cantidad;
				cantidad=cantidad.replace(",",".");
	if(isNaN(cantidad)==true){
		 alert("Only number");
		 document.getElementById(Etiqueta).value=cantidadanterior;
		 return;
		 }
		 
	document.getElementById('Linea'+Etiqueta).innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	var aleatorio=Math.random();
	ajax=nuevoAjax(); 
ajax.open("GET", "../actualizaPrecio.php?ref="+ref+"&precio="+cantidad+"&Destino="+Destino+"&aleatorio="+aleatorio,true); 
ajax.onreadystatechange=function() { 
if (ajax.readyState==4) { 
	document.getElementById(Etiqueta).value=ajax.responseText;
	document.getElementById('Linea'+Etiqueta).innerHTML="";
   } 
} 
ajax.send(null) 
}
function copiaDatos(){
document.getElementById('Nombre_Envio').value=document.getElementById('Nombre_Factura').value;
document.getElementById('Telefono_Envio').value=document.getElementById('Telefono_Factura').value;
document.getElementById('direccion_Envio').value=document.getElementById('direccion_Factura').value;
}
function cambiaDestino(selObj,pagina){
	var aleatorio=Math.random();
	ajax=nuevoAjax(); 
	
ajax.open("GET", "../cambiaDestino.php?Destino="+selObj.options[selObj.selectedIndex].value+"&aleatorio="+aleatorio,true); 
ajax.onreadystatechange=function() { 
if (ajax.readyState==4) { 
		document.location.href='?option='+pagina;
   } 
} 
ajax.send(null) 
}
function actualizaDto(campo, Etiqueta){
	var cantidad;
	cantidad=document.getElementById(Etiqueta).value;
	document.getElementById(Etiqueta).value=cantidad;
				cantidad=cantidad.replace(",",".");
	if(isNaN(cantidad)==true){
		 alert("Only number");
		 document.getElementById(Etiqueta).value=cantidadanterior;
		 return;
		 }
	//alert(Etiqueta);
	document.getElementById('Linea'+Etiqueta).innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	var aleatorio=Math.random();
	ajax=nuevoAjax(); 
ajax.open("GET", "../actualizaDto.php?ref="+Etiqueta+"&cantidad="+cantidad+"&aleatorio="+aleatorio,true); 
ajax.onreadystatechange=function() { 
if (ajax.readyState==4) { 
devuelto=ajax.responseText.split("|");
	document.getElementById(Etiqueta).value=devuelto[campo];
	document.getElementById('Linea'+Etiqueta).innerHTML="";
   } 
} 
ajax.send(null) 
}
function actualizaDtoApicable(){
	document.getElementById('LineaDto').innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	var aleatorio=Math.random();
	ajax2=nuevoAjax(); 
ajax2.open("GET", "../actualizaDtoAplicable.php?aleatorio="+aleatorio,true); 
ajax2.onreadystatechange=function() { 
if (ajax2.readyState==4) { 
	document.getElementById('LineaDto').innerHTML=ajax2.responseText;
   } 
} 
ajax2.send(null) 
}
function actualizaTotalAPagar(){
	document.getElementById('LineaAPagar').innerHTML="<img src='../images/carga.gif' alt='actualizando' width='16' height='16' />";
	var aleatorio=Math.random();
	ajax3=nuevoAjax(); 
ajax3.open("GET", "../actualizaTotalAPagar.php?aleatorio="+aleatorio,true); 
ajax3.onreadystatechange=function() { 
if (ajax3.readyState==4) { 
	document.getElementById('LineaAPagar').innerHTML=ajax3.responseText;
   } 
} 
ajax3.send(null) 
}
var vent;
function calc() { 
vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
document.forms['form1'].submit();
check();
}

function check(){
  if(vent.closed){ /* La variable 'w' corresponde al objeto window que devuelve el metodo window.open(); Dentro de esta condición ubicas las instrucciones si la ventana esta cerrada. */
  document.location.href='?option=resultado';
  }

 else{ /* Dentro de esta condicion ubicas las instrucciones si la ventana no esta cerrada.*/
setTimeout('check()', 100);
  }
}
function redirige(valor){
setTimeout('redirige2('+valor+')', 5000);
}
function redirige2(valor){
	document.location.href='?option=resultado&intento='+valor
}


