function redireccionar(url) 
{
	location.href=url;
}
function goto(url)
{
	window.location=url;
}

function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new XMLHttpRequest()
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp; 
}

function actualizaArticulos(idarticulo)
{
	var divDestino = document.getElementById("compra_container");
	
	var ajax=nuevoAjax();
	ajax.open("GET", MIurl+"view/body/secciones/tienda/ajax.compra.php?nuevo&idarticulo="+idarticulo, true);
	ajax.onreadystatechange=function() 
	{ 
		if (ajax.readyState==4)
			divDestino.innerHTML=ajax.responseText;
		actualizaImporte();
		actualizaCarrito();
	}
	ajax.send(null);
}
function borraArticulo(idarticulo)
{
	var divDestino = document.getElementById("compra_container");
	
	var ajax=nuevoAjax();
	ajax.open("GET", MIurl+"view/body/secciones/tienda/ajax.compra.php?elimina&idarticulo="+idarticulo, true);
	ajax.onreadystatechange=function() 
	{ 
		if (ajax.readyState==4)
			divDestino.innerHTML=ajax.responseText;
		actualizaImporte();
		actualizaCarrito();
	}
	ajax.send(null);
}
function actualizaImporte()
{
	var divDestino = document.getElementById("total_precio");
	
	var ajax=nuevoAjax();
	ajax.open("GET", MIurl+"view/body/secciones/tienda/ajax.compra.php?precio", true);
	ajax.onreadystatechange=function() 
	{ 
		if (ajax.readyState==4)
			divDestino.innerHTML=ajax.responseText;
	}
	ajax.send(null);
}
function actualizaCarrito()
{
	var divDestino = document.getElementById("carrito-container");
	
	var ajax=nuevoAjax();
	ajax.open("GET", MIurl+"view/body/secciones/tienda/ajax.compra.php?carrito", true);
	ajax.onreadystatechange=function() 
	{ 
		if (ajax.readyState==4)
			divDestino.innerHTML=ajax.responseText;
	}
	ajax.send(null);
}
function vaciaCarrito()
{
	var ajax=nuevoAjax();
	ajax.open("GET", MIurl+"view/body/secciones/tienda/ajax.compra.php?vaciacarrito", true);
	ajax.send(null);
		goto(MIurl);

}

function compruebaForm(objeto){
	
	// Lo primero que hacemos es desactivar el boton
	objeto.disabled=true;

	var buffer="";
	
	var nombre=document.getElementById("nombre").value.length;
	if (nombre == 0){
		buffer = buffer + "\nNo has escrito tu nombre"; 
	}
	
	var apellidos=document.getElementById("apellidos").value.length;
	if (apellidos == 0){
		buffer = buffer + "\nNo has escrito tus apellidos"; 
	}
			
	var direccion=document.getElementById("direccion").value.length;
	if (direccion == 0){
		buffer = buffer + "\nNo has escrito tu DIRECCION"; 
	}
	
	var localidad=document.getElementById("localidad").value.length;
	if (localidad == 0){
		buffer = buffer + "\nNo has escrito tu LOCALIDAD"; 
	}
	
	var provincia=document.getElementById("provincia").value.length;
	if (provincia == 0){
		buffer = buffer + "\nNo has escrito tu PROVINCIA"; 
	}
	
	var cp=document.getElementById("cp").value.length;
	if (cp == 0){
		buffer = buffer + "\nNo has escrito tu CODIGO POSTAL"; 
	}
	
	var email=document.getElementById("email").value.length;
	if (email == 0){
		buffer = buffer + "\nNo has escrito tu E-MAIL"; 
	}
	else
	{
			if(!mailValido(document.getElementById("email").value))
			{
				buffer = buffer + "\nTienes que escribir una direccion de e-mail valida"; 
			}

	}
	
	var email2=document.getElementById("email2").value.length;
	if (email != email2){
		buffer = buffer + "\nAmbas direcciones de e-mail tienen que coincidir"; 
	}
	
	var telefono=document.getElementById("telefono").value.length;
	if (telefono == 0){
		buffer = buffer + "\nNo has escrito tu TELEFONO"; 
	}
	

	if (buffer=="")
	{
		document.forms[0].submit();
	}else{
		alert("Por favor, revisa los siguientes campos \n"+buffer);
		// Activamos el boton puesto que ha habido un error
		objeto.disabled=false;
	}
	return false;
}// fin funcion

function mailValido(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres;
} 

