﻿
		String.prototype.trim = function() {
			return this.replace(/^\s+|\s+$/g,"");
		}
		String.prototype.ltrim = function() {
			return this.replace(/^\s+/,"");
		}
		String.prototype.rtrim = function() {
			return this.replace(/\s+$/,"");
		}

	
// ****************************************************************************

	var request = createRequest();

// ****************************************************************************

	function createRequest() {
		var request = null;
		
		try {
			request = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					request = null;
				}
			}
		}
		
		if (request == null) {
			return false;
		} else {
			return request;
		}
	  
	}
	
// ****************************************************************************

		function sendMensaje(){
			if(request){
				request.onreadystatechange = function(){
					if (request.readyState == 4){
						if (request.status == 200 || request.status == 304){
							limpiarForm = true;
							showRespuesta();
						} else {
							limpiarForm = false;
						}
					}
				};
				document.getElementById("msg_sending").style.display = "block";
				document.getElementById("btn_send").style.display = "none";
				var mForm = document.getElementById("contactForm");
				request.open("POST", "/proc_contacto.php");
				request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				request.send("nombre=" + mForm.nombre.value.trim() + "&correo=" + mForm.correo.value.trim() + "&mensaje=" + mForm.mensaje.value.trim());
				return false;
			}else{
				//envía true para que se envíe el formulario de forma normal ya que ha fallado la respuesta ajax.
				return true;
			}
		}

// ****************************************************************************


	function showRespuesta(){
		var mForm_container = document.getElementById("cont_form");
		var respuesta_container =  document.getElementById("respuesta_container");
		
		var req = request.responseXML;
		
		stripWhiteSpace(req);
		
		respuesta_container.innerHTML = req.getElementsByTagName("datos")[0].childNodes[0].nodeValue;
		
		mForm_container.style.display = "none";
		respuesta_container.style.display = "block";
		
		request = createRequest();
	}

// ****************************************************************************


	function showForm(limpiarForm){
		if (limpiarForm){
			document.getElementById("contactForm").reset();
		}
		document.getElementById("msg_sending").style.display = "none";
		document.getElementById("respuesta_container").style.display = "none";
		document.getElementById("cont_form").style.display = "block";
		document.getElementById("btn_send").style.display = "block";
	}
	
		
	function paintFields(){
		var mForm = document.getElementById("contactForm");
		var mColor = "#F8E7CD";
		
		mForm.nombre.style.backgroundColor = mColor;
		mForm.correo.style.backgroundColor = mColor;
		mForm.mensaje.style.backgroundColor = mColor;
		
	}
	
// ****************************************************************************


		
	function checkForm(){
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
		var mForm = document.getElementById("contactForm");
		var errorColor = "#FFCC33";
		
		if (mForm.nombre.value.trim() == "" || mForm.nombre.value.trim() == "Tu nombre"){
			alert("Por favor escribe tu nombre para enviarnos un mensaje.");
			paintFields();
			mForm.nombre.style.backgroundColor = errorColor;
			mForm.nombre.focus();
			return false;
			
		} else if (mForm.correo.value.trim() == ""){
			alert("Necesitamos una dirección de correo electrónico a la que poder responderte.\nPor favor escribe una dirección de correo válida.");
			paintFields();
			mForm.correo.style.backgroundColor = errorColor;
			mForm.correo.focus();
			return false;		
			
		} else if (!re.test(mForm.correo.value.trim())){
			alert("Tu dirección de correo no parece ser correcta.\nEscríbela nuevamente.");
			paintFields();
			mForm.correo.style.backgroundColor = errorColor;
			mForm.correo.focus();
			mForm.correo.select();
			return false;
			
		} else if (mForm.mensaje.value.trim() == ""){
			alert("Debe escribir un mensaje para enviarlo.");
			paintFields();
			mForm.mensaje.style.backgroundColor = errorColor;
			mForm.mensaje.focus();
			return false;
			
		} else {
			if(!request){
				// no se ha podido crear el objeto xmlhttprequest. se envía el form de forma normal.
				return true;
			} else {
				return sendMensaje();
			}
			
		}		
	}


// ****************************************************************************


// quitar whitespaces

function is_ws(nod) {
	return !(/[^\t\n\r ]/.test(nod.data));
}

function findWhiteSpace(node, nodeNo) {
	for (i=0; i<node.childNodes.length; i++) {
		if (node.childNodes[i].nodeType == 3 && is_ws(node.childNodes[i])) {
			nodesToDelete[nodesToDelete.length] = node.childNodes[i]
		}
		if (node.childNodes[i].hasChildNodes()) {
			findWhiteSpace(node.childNodes[i], i);
		}
	}
	node = node.parentNode;
	i = nodeNo;
}

function stripWhiteSpace(node) {
	nodesToDelete = Array();
	findWhiteSpace(node, 0);
	for(i=nodesToDelete.length-1;i>=0;i--) {
		nodeRef = nodesToDelete[i];
		nodeRef.parentNode.removeChild(nodeRef)
	}
}	
// -------------------------------------------------------------------
