//----------------------------------------------------------------------------------------------------------------------
// Archivo: errores.js
// Version: 1.3
// Fecha: Octubre 2009
// Autor: elRevoltijo-Network.com
// Descripcion: funciones javascript para errores
//----------------------------------------------------------------------------------------------------------------------

var errores_emailInicioVar = false;
var errores_errorInicioVar = false;

//----------------------------------------------------------------------------------------------------------------------
// Funcion errores_emailInicio
//----------------------------------------------------------------------------------------------------------------------
// Devuelve: false
//----------------------------------------------------------------------------------------------------------------------
function errores_emailInicio() {
	if(errores_emailInicioVar == false) {
		document.getElementById('errores_email').className = "errores_input_normal";
		document.getElementById('errores_email').value = "";
		errores_emailInicioVar = true;
	}
	return false;
}

//----------------------------------------------------------------------------------------------------------------------
// Funcion errores_errorInicio
//----------------------------------------------------------------------------------------------------------------------
// Devuelve: false
//----------------------------------------------------------------------------------------------------------------------
function errores_errorInicio() {
	if(errores_errorInicioVar == false) {
		document.getElementById('errores_error').className = "errores_input_normal";
		document.getElementById('errores_error').value = "";
		errores_errorInicioVar = true;
	}
	return false;
}



//----------------------------------------------------------------------------------------------------------------------
// errores_notificar(). Funcion primaria para notificar un error
//----------------------------------------------------------------------------------------------------------------------
function errores_notificar() {
	// Obtencion del XmlHttpObject
	xmlHttpObject = ajax_obtenerXmlHttpObject();
	
	// Comprobacion de campos y procesamiento Ajax
	if (xmlHttpObject == null) {
		alert ("ERROR: Tu navegador no soporta 'HTTP Request'");
		return;
	}
	
	// Reinicio de mensajes
	var errores = false;
	document.getElementById("errores_email_error").innerHTML = "";
	document.getElementById("errores_error_error").innerHTML = "";				// Todos los mensajes de los campos
	document.getElementById("errores_enviar_error").innerHTML = "";			// y del boton "enviar"
	document.getElementById("errores_enviar_mensaje").innerHTML = "";

	// Errores en el email
	errores = formularios_comprobarEmailValido("errores_email", "errores_email_error", "errores_enviar") || errores;
	errores = formularios_comprobarValorInicial("errores_email", "errores_email_error", "errores_enviar", "Email") || errores;
	errores = formularios_comprobarValorInicial("errores_email", "errores_email_error", "errores_enviar", "") || errores;
	errores = formularios_comprobarLongitudMaxima("errores_email", "errores_email_error", "errores_enviar", 255) || errores;

	// Errores en el error
	errores = formularios_comprobarValorInicial("errores_error", "errores_error_error", "errores_enviar", "Descripcion del error") || errores;
	errores = formularios_comprobarValorInicial("errores_error", "errores_error_error", "errores_enviar", "") || errores;
	errores = formularios_comprobarLongitudMaxima("errores_error", "errores_error_error", "errores_enviar", 2048) || errores;

	// Recuento de errores
	if(errores == false) {
		document.getElementById("errores_enviar_mensaje").innerHTML = "<img src='imagenes/ajax-load.gif'>";

		// Inhabilitacion del formulario
		formularios_inhabilitarCampo("errores_email");
		formularios_inhabilitarCampo("errores_error");			// Todos los campos del formulario
		formularios_inhabilitarBoton("errores_enviar");			// y el boton enviar

		var url = "../modulos/errores/ajax/errores_notificar.php";
	 	var direccion = document.getElementById("errores_direccion").value;
	 	var email = document.getElementById("errores_email").value;
	 	var error = document.getElementById("errores_error").value;

		xmlHttpObject.onreadystatechange = errores_notificar_respuesta;
		xmlHttpObject.open("POST", url, true);
		xmlHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttpObject.send("direccion=" + direccion + "&email=" + email + "&error=" + error);
	}
}

//----------------------------------------------------------------------------------------------------------------------
// errores_notificar_respuesta(). Funcion secundaria para notificar un error
//----------------------------------------------------------------------------------------------------------------------
function errores_notificar_respuesta() {
	if (xmlHttpObject.readyState == 4 || xmlHttpObject.readyState == "complete") {
		var xmlDoc = xmlHttpObject.responseXML;
		if(xmlDoc.getElementsByTagName("resultado")[0].firstChild != null) { var resultado = xmlDoc.getElementsByTagName("resultado")[0].firstChild.nodeValue; }

		// Retirada de mensaje de espera
		document.getElementById("errores_enviar_mensaje").innerHTML = "";

		if (resultado == "1") {
			// Habilitacion del formulario
			formularios_habilitarCampo("errores_email");
			formularios_habilitarCampo("errores_error");
			formularios_habilitarBoton("errores_enviar");
				
			document.getElementById("errores_enviar").className = "input_boton";
			document.getElementById("errores_enviar_mensaje").innerHTML = "Error notificado.";
			errores_borrarFormulario();
		}
		else {
			document.getElementById("errores_enviar_error").innerHTML = "Error. No se envi&oacute; el error.";
		}
	}
}



//----------------------------------------------------------------------------------------------------------------------
// FUNCION errores_borrarFormulario. Funcion para borrar el formulario
//----------------------------------------------------------------------------------------------------------------------
function errores_borrarFormulario() {
	// Vaciado de los campos
	document.getElementById("errores_email").value = "Email";
	document.getElementById("errores_error").value = "Descripcion del error";
	
	// Aplicacion de los estilos
	document.getElementById("errores_email").className = "errores_input_inicial";
	document.getElementById("errores_error").className = "errores_input_inicial";
	
	// Reseteo de las variables
	errores_emailInicioVar = false;
	errores_errorInicioVar = false;
}

