/* FUNCIONES GLOBALES*/
	
	/* Crea objeto Ajax*/
	function nuevoAjax(){ 
		var xmlhttp=false; 
		try { 
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
		}
		catch(e){ 
			try	{ 
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			catch(E) { xmlhttp=false; }
		}
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 
		return xmlhttp; 
	}
	
	/* Comprueba si el parametro es una dirección de email*/
	function isEmail(valor){
		var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (valor.length == 0 ) return false;
		if (filter.test(valor))
			return true;
		else
			return false;
	}

	/* VALIDAN DATOS FORMULARIO*/
	function enviarMail(formulario, editor, obligatorios, ruta){
		var Formulario = document.getElementById(formulario);
		var longitudFormulario = Formulario.elements.length;
		var cadenaFormulario = ""
		var sepCampos ="";
		var info="";
		obligatorios_array = obligatorios.split(',');
		for (i = 0; i <  Formulario.elements.length; i++) {
			if ((Formulario.elements[i].type == "text" && Formulario.elements[i].value == "") && (obligatorios_array[i]=='1')) {
				info = "Todos los datos marcados con * son obligatorios. Rellenelos correctamente por favor";
				break;
			} else {
				cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
				sepCampos="&";
			}
		}
		if(editor){
			texto = tinyMCE.getInstanceById('texto');
			if (texto.getHTML() =="")
				info = "Rellene el cuerpo del mensaje";
			else cadenaFormulario += sepCampos+'texto='+escape(texto.getHTML());
		}else {
			if (document.getElementById("texto").value == "")
				info = "Rellene el cuerpo del mensaje";
		}
		if(info!=""){
			document.getElementById("resultado_mail").innerHTML= info;
			return false;
		} else {
			if(!(isEmail(document.getElementById("email").value))){
				document.getElementById("resultado_mail").innerHTML= "Introduzca una dirección de e-mail valida";
				return false;
			}
		}
		
		ajax=nuevoAjax();
		ajax.open("POST", ruta+"envio_mail.php", true);
		ajax.onreadystatechange=function(){
			if (ajax.readyState==1){	
				document.getElementById("resultado_mail").innerHTML="Enviando...";	
			}
			if (ajax.readyState==4){
				document.getElementById("resultado_mail").innerHTML= ajax.responseText;
				Formulario.reset();
				if(editor) texto.setHTML('');
			}
		}
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(cadenaFormulario);
	}