function creaAjax(){
	 var objetoAjax=false;
	 try {
	  /*Para navegadores distintos a internet explorer*/
	  objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
	 }catch(e){
		 try{
			/*Para explorer*/
			objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
		 }catch(E){
		 	objetoAjax = false;
		 }
	 }
	 if (!objetoAjax && typeof XMLHttpRequest!='undefined'){
	 	objetoAjax = new XMLHttpRequest();
	 }
	 return objetoAjax;
}

function Ajax(url,capa,valores,metodo){
	var ajax=creaAjax();
	var capaContenedora = document.getElementById(capa);
	/*Creamos y ejecutamos la instancia si el metodo elegido es POST*/
	if(metodo.toUpperCase()=='POST'){
		ajax.open ('POST', url, true);
		ajax.onreadystatechange = function() {
		if (ajax.readyState==1){
			capaContenedora.innerHTML='<img src="/imagenes/cargando.gif" border="0" alt="Cargando..."/>';
		}else if (ajax.readyState==4){
			if(ajax.status==200){
				document.getElementById(capa).innerHTML=ajax.responseText;
			}else if(ajax.status==404){
					capaContenedora.innerHTML = "La direccion no existe";
				}else{
					capaContenedora.innerHTML = "Error: ".ajax.status;
				}
			}
		}
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(valores);
		return;
	}
}

function consultarPrecioMedidas(idSelectMedida,producto_id){
	var selectMedida=document.getElementById(idSelectMedida);
	var medida_id=selectMedida.options[selectMedida.selectedIndex].value;
	if(medida_id!=""){
		Ajax('/includes/consulta_precio.php','precio_medida','medida_id='+medida_id+'&producto_id='+producto_id,'post');
	}else{
		alert('Debes seleccionar una medida.');	
	}
	return false;
}

function jsTrim(JSvalue){
	var JStemp = JSvalue;
	var JSobj = /^(\s*)([\W\w]*)(\b\s*$)/;
	//Elimina los espacios iniciales y finales
	if (JSobj.test(JStemp))
		 JStemp = JStemp.replace(JSobj, '$2')
	//Elimina los espacios duplicados
	var JSobj = / +/g;
	JStemp = JStemp.replace(JSobj, " ");
	if (JStemp == " ")
		JStemp = "";
	//devolvemos la cadena limpia
	return JStemp;
}

function buscar(){
	var str = jsTrim(document.buscador.p.value);
	if (str != ''){
		document.buscador.submit();
	}else{
		alert('Debes introducir un texto para poder buscar.');	
	}
}

function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true)
	} else {
		alert("La direcci"+String.fromCharCode(243)+"n de email es incorrecta.");
		return (false);
	}
}

function validaEnvioCorreo(formulario){
	var nombre = formulario.nombre.value;
	var email = formulario.email.value;
	var consulta = formulario.consulta.value;
	if(nombre!="" && consulta!="" && email!=""){
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
			formulario.submit();
		}else{
			alert("La direcci"+String.fromCharCode(243)+"n de email es incorrecta.");
		}
	}else{
		alert('Debes rellenar los campos obligatorios.');
	}
	return false;	
}

function cambiaFoto(foto){
	if(document.getElementById('fotoGrande').alt!=foto){
		document.getElementById('fotoGrande').src = '/fotos/'+foto;
		document.getElementById('fotoGrande').alt = foto;
	}
	return false;
}
