var carcont;

function mdoCall(url,callBackFunc) {

        var xmlHttpReq = false;
        
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
  		xmlHttpReq.onreadystatechange = function() {
        			
        	if (xmlHttpReq.readyState == 4) {
        		 eval(callBackFunc+"('"+xmlHttpReq.responseText+"')");
                 			    
        	}
        }
  		
  		xmlHttpReq.open('GET', url, true);
	
        xmlHttpReq.send(null);        
  	
  } 
  
  function ac(prod) {  
  	mdoCall("/ajaxToCarrito.php?producto="+prod+"&cantidad="+document.getElementById('pp'+prod).value+"&im="+new Date().getTime(),"_bcallToCarrito");
  }
  function emp () {
	mdoCall("/ajaxVaciaCarrito.php","_bcallVaciaCarrito");
  }

  function _bcallVaciaCarrito(html) {
		if (!carcont) {
  			carcont=document.getElementById('carcont');	
		}
		carcont.innerHTML=html;
  		document.getElementById('carimg').src='/img/lcar.jpg';
	}

  function _bcallVerCarrito(html) {

	var ar=html.split("@@@");	

	if (!carcont) {
  		carcont=document.getElementById('carcont');  		
  	}
  	carcont.innerHTML=ar[1];

	if (ar[0]=='1') document.getElementById("carimg").src="/img/lcar2.jpg";
	else  document.getElementById("carimg").src="/img/lcar.jpg";
  }
  function _bcallToCarrito(param) {
  	
  	if (param==1) {
  		alert('Lo sentimos, ha habido un error');
  		return;
  	}
  	if (param==2) {
  		alert('Lo sentimos, no tenemos tantas unidades en stock');
  		return;
  	}
  	if (param==3) {
  		alert('Cantidad incorrecta');
  		return;
  	}
  	
  	//document.getElementById('carimg').src='/img/lcar2.jpg';
 	mdoCall("/ajaxVerCarrito.php?im="+new Date().getTime(),"_bcallVerCarrito");
	
  }

function NOP (cm) {
	alert("El carrito no alcanza el pedido m&iacute;imo ("+cm+" &euro;)");
}
function formenvioFactura(obj) {

	/*
	var divFactura=document.getElementById("divFactura");

	if (obj.checked==true) {
		divFactura.className="divFacturaV";		
	}
	if (obj.checked==false) {
		divFactura.className="divFactura";		
	}
	*/
	
	var fields=['_fRazon','_fDireccion','_fCP','_fLocalidad','_fProvincia','_fCIF'];
	
	var t=document.formenvio;
	
	if (obj.checked==true) {
		for (var i=0; i<fields.length; i++) {
			eval('t.'+fields[i]+'.disabled=false');
			/* IE ggg */
			eval("t."+fields[i]+".style.backgroundColor='#fff'");
		}
	}
	else {
		for (var i=0; i<fields.length; i++) {
			 eval('t.'+fields[i]+'.disabled=true');
		 	/* IE ggg */
			eval("t."+fields[i]+".style.backgroundColor='#ddd'");			
			
			if (i!='fProvincia') eval('t.'+fields[i]+'.value=""');
		}
	}
	
}
function testForm1() {
	
	document.pform1.submit();
}
function ob(dir,dom,show,su) {

	if (show==1) show=dir+'@'+dom;

	if (!su) document.write('<a href=\"mailto:' + dir + '@' + dom + '\">'+ show +'</a>');
	else document.write('<a href=\"mailto:' + dir + '@' + dom + '\"><u>'+ show +'</u></a>');
}

function amplia(cual) {
	window.open('/ampliar/'+cual,'','titlebar=0,resizable=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,height=1,width=1');
}
function amplia2(cual, ancho,alto) {
	
		var anchod=((screen.width)/2)-(ancho/2);
		
		var altod=((screen.height-16)/2)-(alto/2);
		
		var w=window.open("",'','resizable=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,width='+ancho+',height='+alto+',screenX='+anchod+',screenY='+altod+',left='+anchod+',top='+altod+'');
		w.document.write("<html><head><body style='margin:0;padding:0'><img src='"+cual+"'></body></html>");
	
}

	function testFormEnvio() {
		
		var t=document.formenvio;
		
		var fields=['_eNombre','_eDireccion','_eCP','_eLocalidad','_eCContacto','_eCTLF'];
		
		for (var i=0; i<fields.length; i++) {
			if (eval('t.'+fields[i]).value=='') {
				alert ("Por favor, rellene todos los datos");
				eval('t.'+fields[i]).focus();
				return;
			}	
		}
	
		if (isNaN(t._eLegal.value) || !t._eLegal.checked) {
			alert ("Por favor, lea y acepte las condiciones legales");
			return;	
		}
		
		fields=['_fRazon','_fDireccion','_fCP','_fLocalidad','_fProvincia','_fCIF'];
		
		if (!isNaN(t._eFactura.value) && t._eFactura.checked) {
			
			for (var i=0; i<fields.length; i++) {
				if (eval('t.'+fields[i]).value=='') {
					alert ("Por favor, rellene todos los datos para poder hacerle factura");
					eval('t.'+fields[i]).focus();
					return;
				}	
			}
		
		}
		
		t.submit();
	}
