/**************************************************
 * PARAMETROS DE CONFIGURACION DE LA VALIDACION
 *************************************************/
 var mostrarAlert = false;

/**************************************************
 * Expresiones regulares para la validacin 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]{1,2})?)$";
var REGEXENTERO = "^(-?[0-9]*)$";
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 REGEXHORA = "^((2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9]))$";
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 validaTextBox(clientId,requerido,label,longitud,tipo)
{
	eval(clientId + "ErrorText = new Array()");	
	var obj = document.getElementById(clientId);
	var textoOriginal = (tipo != "Fecha" && tipo != "Hora" ? eval(clientId + "TextoOriginal"):"");
	var result = true;
	
	//Si el objeto no existe salimos
	if(typeof(obj)=="undefined")return; 
	
	if(requerido && (obj.value == "" || obj.value == textoOriginal))
	{
		if(mostrarAlert)alert("Error: el campo " + label + " es obligatorio."); //Mostramos el mensaje
		if(obj.className.indexOf("error") < 0)obj.className += " error";  //Cambiamos el estilo
		eval(clientId + "ErrorText[0] = 'El campo \<strong\>" + label + "\<\/strong\> es obligatorio'");	
		result = false;
	}
	else if(obj.value!=textoOriginal && longitud != 0 && obj.value.length > longitud)
	{
		//Validamos si la longitud del campo excede la maxima permitida
		if(mostrarAlert)alert("Error: el campo " + label + " es demasiado largo."); //Mostramos el mensaje
		if(obj.className.indexOf("error") < 0)obj.className += " error"; //Cambiamos el estilo
		eval(clientId + "ErrorText")[0] = "El campo \<strong\>" + label + "\<\/strong\> es demasido largo, máximo " + longitud + " caracteres.";	
		result = false;
	}
	else
	{
		if (obj.value!=textoOriginal && obj.value!="")
		{
			switch(tipo)
			{
				case "Email":
					if(!validaRegEx(obj.value,REGEXMAIL))
					{
						result = false;
						if(mostrarAlert)alert("El formato del campo " + label + " es incorrecto.");
						eval(clientId + "ErrorText")[0] = "El formato del campo \<strong\>" + label + "\<\/strong\> es incorrecto.";	
					}
					break;
				case "DNI":
					if(!validaRegEx(obj.value,REGEXDNI))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label +  " incorrecto.");
						eval(clientId + "ErrorText")[0] = "Formato de \<strong\>" + label + "\<\/strong\> incorrecto.";	
					}
					break;
				case "Fecha":
					if(!validaRegEx(obj.value,REGEXFECHA))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(clientId + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.';");	
					}
					break;
				case "Hora":
					if(!validaRegEx(obj.value,REGEXHORA))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(clientId + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.';");	
					}
				break;
				case "Importe":
					if(!validaRegEx(obj.value,REGEXIMPORTE))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(clientId + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				case "Telefono":
					if(!validaRegEx(obj.value,REGEXTELEFONO))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(clientId + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				case "Entero":
					if(!validaRegEx(obj.value,REGEXENTERO))
					{
						result = false;
						if(mostrarAlert)alert("Formato " + label + " incorrecto.");
						eval(clientId + "ErrorText[0] = 'Formato \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				case "Largo":
					if(!validaRegEx(obj.value,REGEXENTERO))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(clientId + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				case "Simple":
					if(!validaRegEx(obj.value,REGEXENTERO))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(this.ClientID + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				case "Doble":
					if(!validaRegEx(obj.value,REGEXENTERO))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(this.ClientID + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				case "Decimal":
					if(!validaRegEx(obj.value,REGEXENTERO))
					{
						result = false;
						if(mostrarAlert)alert("Formato de " + label + " incorrecto.");
						eval(this.ClientID + "ErrorText[0] = 'Formato de \<strong\>" + label + "\<\/strong\> incorrecto.'");	
					}
					break;
				default:
					result = true;
					break;
			}
		}
	}
	
	if(tipo != "Fecha" && tipo != "Hora" && obj.value == "")
	{
		//Ponemos el texto por defecto si el campo est vaco
		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 true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de DNI incorrecto.");
		alert(obj.id + "ErrorText");	
		alert(eval(obj.id + "ErrorText"));	
		eval(obj.id + "ErrorText") = new Array("Error: formato de DNI incorrecto.");	
		return false;
	}
}

function validaFecha(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXFECHA))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de fecha incorrecto.");
		return false;
	}
}

function validaHora(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXHORA))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de hora incorrecto.");
		return false;
	}
}

function validaImporte(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXIMPORTE))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			window.alert("Error: formato de importe incorrecto.");
		return false;
	}
}

function validaTelefono(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXTELEFONO))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de tel�fono incorrecto.");
		return false;
	}
}

function validaEntero(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato entero incorrecto.");
		return false;
	}
}

function validaLargo(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de largo incorrecto.");
		return false;
	}
}

function validaSimple(obj)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de simple incorrecto.");
		return false;
	}
}

function validaDoble(e)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return true;
	}
	else
	{
		if(mostrarAlert)
			alert("Error: formato de doble incorrecto.");
		return false;
	}
}

function validaDecimal(e)
{
	// handle event
	if(validaRegEx(obj.value,REGEXENTERO))
	{
		return true;
	}
	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="";
	}
}

function borrarErrores(diverrores)
{
	var errorlist = document.getElementById(diverrores);
	if(typeof(errorlist)!="undefined" && errorlist!=null)
	{
		errorlist.className = errorlist.className + "vacio";
		errorlist.innerHTML = "";
	}
	return false;
}

function mostrarErrores(errores,diverrores)
{
	var errorlist = document.getElementById(diverrores);
	if(typeof(errorlist)!="undefined")
	{
		errorlist.className = errorlist.className.replace("vacio","");
		var errorlisttext = new Array();
		var index;
		errorlisttext[errorlisttext.length] = "Revise los siguientes errores:";
		errorlisttext[errorlisttext.length] = "<ul>";
		for(index = 0 ; index < errores.length ; index++)
		{
			errorlisttext[errorlisttext.length] = "<li>" + errores[index] + "<\/li>";	
		}
		errorlisttext[errorlisttext.length] =  "<\/ul>";
		errorlist.innerHTML = errorlisttext.join("");
		document.location = "#error";
	}
}
