/**************************************************
 * PARAMETROS DE CONFIGURACION DE LA VALIDACION
 *************************************************/
 var mostrarAlert = false;
 var errores = new Array();

/**************************************************
 * Expresiones regulares para la validación de los 
 * distintos tipos de datos.
 *************************************************/
var REGEXMAIL = "^[a-z0-9A-Z][_\\-\\.a-zA-Z0-9]*@[a-z0-9A-Z][_\\-\\.a-zA-Z0-9]*\\.[a-zA-Z0-9]{2,3}$";
var REGEXIMPORTE = "^(-?[0-9]*)([\\.,]{1}[0-9]{2})$";
var REGEXENTERO = "(^-?\d\d*$)";
var REGEXNUMERO = "^(-?([0-9]*))([.,]{0,1})([0-9]*)$";
var REGEXDNI = "^[0-9]{8}([- ]{0,1}[A-Za-z]){1}$";
var REGEXFECHA = "^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$";
var REGEXTELEFONO = "^[0-9]{2,3}(-{0,1}[0-9]){6,7}$";

function validaSubmit()
{	
	errores = new Array();
	validaFormulario();

	if(errores.length > 0)
	{
		if(mostrarAlert)
			alert("Existen datos erroneos en el formulario");
		var errorlist = document.getElementById("errorlist");
		if(typeof(errorlist)!="undefined")
		{
			errorlist.innerHTML = "";
			var index;
			errorlist.innerHTML += "Revise los siguientes errores:";
			errorlist.innerHTML += "<ul>";
			for(index = 0 ; index < errores.length ; index++)
			{
				errorlist.innerHTML += "<li>" + errores[index] + "<\/li>";	
			}
			errorlist.innerHTML += "<\/ul>";
		}
		return false;
	}
	else
	{
		return true;
	}
}

function valida() 
{
	validaCampo(this);
}

function validaCampo(obj)
{
	var result = true;
	var validar = eval(obj.id + "Validar");
	var textoOriginal = eval(obj.id + "TextoOriginal");
	var requerido = eval(obj.id + "Requerido");
	var longitudMaxima = eval(obj.id + "LogitudMaxima");
	var label = eval(obj.id + "Label");
	
	if(validar == true)
	{
		//Validamos si el campo en caso de ser obligatorio está informado
		if(requerido==true && 
		   (obj.value == "" || obj.value == textoOriginal))
		{
			//Mostramos el aviso correspondiente
			if(mostrarAlert)
				alert("Error: este campo es requerido.");
			//Cambiamos el estilo del campo para indicar el error	
			if(obj.className.indexOf("error") < 0)
				obj.className += " error";
			//Añadimos el mensaje de error 
			errores[errores.length] = "El campo " + label + " es obligatorio";	
			result = false;
		}
		else
		{
			//Validamos si la longitud del campo excede la maxima permitida
			if(longitudMaxima != 0 && obj.value.length > longitudMaxima)
			{
				//Mostramos el aviso correspondiente
				if(mostrarAlert)
					alert("Error: este campo es demasiado largo.");
				//Cambiamos el estilo del campo para indicar el error	
				if(obj.className.indexOf("error") < 0)
					obj.className += " error";
				//Añadimos el mensaje de error 
				errores[errores.length] = "El campo " + label + " es demasido largo, máximo " + longitudMaxima + " caracteres.";	
				result = false;
			}
			else
			{
				if(obj.className.toUpperCase().indexOf("EMAIL") > -1)
					result = validaEmail(obj);
				if(obj.className.toUpperCase().indexOf("DNI") > -1)
					result = validaDNI(obj);
				if(obj.className.toUpperCase().indexOf("FECHA") > -1)
					result = validaFecha(obj);	
				if(obj.className.toUpperCase().indexOf("IMPORTE") > -1)
					result = validaImporte(obj);
				if(obj.className.toUpperCase().indexOf("TELEFONO") > -1)
					result = validaTelefono(obj);
				if(obj.className.toUpperCase().indexOf("ENTERO") > -1)
					result = validaEntero(obj);	
				if(obj.className.toUpperCase().indexOf("LARGO") > -1)
					result = validaLargo(obj);
				if(obj.className.toUpperCase().indexOf("SIMPLE") > -1)
					result = validaSimple(obj);	
  				if(obj.className.toUpperCase().indexOf("DOBLE") > -1)
					result = validaDoble(obj);	
				if(obj.className.toUpperCase().indexOf("DECIMAL") > -1)
					result = validaDecimal(obj);
			}
		}
    }
    if(obj.value == "")
    {
		obj.value = textoOriginal;
	}
	//en caso de no haber error quitamos el estilo error si es necesario
	if(result)
		obj.className = obj.className.replace(/([ ]?)error/gi,"");
	else
	{
		if(obj.className.indexOf("error") < 0)
			obj.className += " error";
	}	
    return result;
}

/*************************************************************************
 *
 * FUNCIONES DE VALIDACION
 *
 *************************************************************************/

function validaRegEx(_value,_regex)
{
	var expreg = new RegExp(_regex);
	return expreg.test(_value);
}

/*******************************************************
 * DESCRIPTION: Valida que la cadena contiene una
 *              direccion de correo electronico valido.
 *
 * PARAMETROS:
 *   e - Evento provocado para validar el control
 *
 * CMENTARIO: No se realizan validaciones sobre la validez 
 *  del dominio (.com, .org, .es, etc.).
 *******************************************************/
function validaEmail(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXMAIL))
	{
		return true;
	}
	else
	{
		//Mostramos el mensaje de error correspondiente
		if(mostrarAlert)
			alert("Error: formato de e-mail incorrecto.");
		//Añadimos el mensaje de error a la lista de errores
		errores[errores.length] = "El formato del campo " + eval(obj.id + "Label") + " es erroneo, (ej. micorreo@dominio.es)";		
		return false;
	}
}

function validaDNI(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXDNI))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de DNI incorrecto.");
		return false;
	}
}

function validaFecha(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXFECHA))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de fecha incorrecto.");
		return false;
	}
}

function validaImporte(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXIMPORTE))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			window.alert("Error: formato de importe incorrecto.");
		return false;
	}
}

function validaTelefono(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXTELEFONO))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de telefono incorrecto.");
		return false;
	}
}

function validaEntero(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato entero incorrecto.");
		return false;
	}
}

function validaLargo(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de largo incorrecto.");
		return false;
	}
}

function validaSimple(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de simple incorrecto.");
		return false;
	}
}

function validaDoble(e)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de doble incorrecto.");
		return false;
	}
}

function validaDecimal(e)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return false;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de decimal incorrecto.");
		return false;
	}
}

/*************************************************************/

function borraTextoOriginal(e)
{
	if (!e) var e = window.event
	
	if(eval(this.id +  "TextoOriginal")==this.value)
	{
		this.value="";
	}
}