// JavaScript Document
function validarCampoForm (regexpTipo, texto, tamMax, tamMin, error, textoError)
{
	var textoErrorExtra = "";
	
	switch (regexpTipo)
	{
		case "mail":
			regexp = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
		break;
		
		case "textoCorto":
			regexp = /^([0-9]|[a-zA-Z]|\'|\.|\,| |ñ|Ñ|á|é|í|ó|ú|Á|É|Í|Ó|Ú|ä|ë|ï|ö|ü|Ä|Ë|Ï|Ö|Ü)*$/;
		break;
		
		case "textoLargo":
			regexp = /^([0-9]|[a-zA-Z]| |\"|\'|\,|\;|\(|\)|_|\/|\.|\r|\n|\t|#|:|\?|¿|¡|\!|-|ñ|Ñ|á|é|í|ó|ú|Á|É|Í|Ó|Ú|ä|ë|ï|ö|ü|Ä|Ë|Ï|Ö|Ü)*$/;
		break;
		
		case "telefono":
			regexp = /^(\+{0,1}[0-9]+){0,1}([0-9]+){0,1}([0-9]+)$/;
			textoErrorExtra = "(formato valido: +xxxxxx o xxxxxxx [x = numeros])";
		break;
		
		case "fechaAMD":
			regexp = /^(([0-9]){4}-([0-9]){2}-([0-9]){2})*$/;
			textoErrorExtra = "(formato valido: aaaa-mm-dd [d{ia}, m{es} y a{&ntilde;o} son numeros])";
		break;
		
		case "fechaDMA":
			regexp = /^(([0-9]){2}\/([0-9]){2}\/([0-9]){4})*$/;
			textoErrorExtra = "(formato valido: dd/mm/aaaa [d{ia}, m{es} y a{&ntilde;o} son numeros])";
		break;
		
		case "numeros":
			regexp = /^[0-9]*$/;
			textoErrorExtra = "(formato valido: solo numeros)";
		break;
		
		case "numerosDecimales":
			regexp = /^(([0-9])*(\.|\,)*([0-9]*))*$/;
			textoErrorExtra = "(formato valido: xxx o xx.xx o xx,xx [x = numeros])";
		break;
		
		case "linkWeb":
			//regexp = /^(http[s]?:\/\/(\w|_|-|\/)+[\.\w]+(\w|_|-|\?|\&|=|\/|\.)*)$/;
			regexp = /^(ftp|https|http):\/\/([a-zA-Z]|[0-9]|-|:|_|%)+((\.|\/)([a-zA-Z]|[0-9]|-|:|_|%)+)*(\.[a-zA-Z]{2,6})((\/|\?)([a-zA-Z]|[0-9]|-|:|_|%)+=([a-zA-Z]|[0-9]|-|:|_|%)+(&([a-zA-Z]|[0-9]|-|:|_|%)+=([a-zA-Z]|[0-9]|-|:|_|%)+)*)?$/;
			textoErrorExtra = "(formato ejemplo: http://www.dominio.com/)";
		break;
	}
	
	if (regexp != "" && (texto.search(regexp)))
		error = error + "<li><strong>" + textoError + " &raquo; </strong> Debe revisar la estructura y no debe utilizar caracteres especiales. "+textoErrorExtra+"</li>\n\n";
	if (texto.length > tamMax)
		error = error + "<li><strong>" + textoError + " &raquo; </strong> El tamaño es mayor al permitido, (tamaño máximo permitido de caracteres es <span>" + tamMax + "</span>) [Tamaño actual <span>" + texto.length + "</span> Caracteres]</li>\n\n";
	if (texto.length < tamMin)
		error = error + "<li><strong>" + textoError + " &raquo; </strong> El tamaño es menor al permitido, (tamaño mínimo permitido de caracteres es <span>" + tamMin + "</span>) [Tamaño actual <span>" + texto.length + "</span> Caracteres]</li>\n\n";
	return error;
}

function regresaObjeto(whichLayer) 
{
	var elem;
	if(document.getElementById) // this is the way the standards work
		elem = document.getElementById(whichLayer);
	else if(document.all) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if(document.layers) // this is the way nn4 works
		elem = document.layers[whichLayer];
	return elem;
}

function imprimeMensaje(mensaje)
{
	var item = null;
	item = regresaObjeto("errorexito");
	if (item.style)
		item.style.display = 'block';
	else 
		item.visibility = "show";
	item.innerHTML = mensaje;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
function espaciosSeguidos(stringToTrim) {
	return stringToTrim.replace(/ +/," ");
}

/* Validaciones FORM */
function validarFormMail (form)
{		
	var error = "";
	
	// Nombre
	form.nombre.value = trim(espaciosSeguidos(form.nombre.value));
	error = validarCampoForm ("textoCorto", form.nombre.value, 12, 3, error, "Nombre (no debe estar vacio)");
	// Apellido
	form.apellido.value = trim(espaciosSeguidos(form.apellido.value));
	error = validarCampoForm ("textoCorto", form.apellido.value, 12, 3, error, "Apellido (no debe estar vacio)");
	// Correo Electronico
	form.correo.value = trim(espaciosSeguidos(form.correo.value));
	error = validarCampoForm ("mail", form.correo.value, 90, 5, error, "Correo Electronico (no debe estar vacio)");
	// Telefono
	form.telefono.value = trim(espaciosSeguidos(form.telefono.value));
	if (form.telefono.value.length > 0)
		error = validarCampoForm ("telefono", form.telefono.value, 16, 6, error, "Telefono");
	// Ciudad
	form.ciudad.value = trim(espaciosSeguidos(form.ciudad.value));
	error = validarCampoForm ("textoCorto", form.ciudad.value, 24, 4, error, "Ciudad (no debe estar vacio)");
	// Pais
	form.pais.value = trim(espaciosSeguidos(form.pais.value));
	error = validarCampoForm ("textoCorto", form.pais.value, 24, 4, error, "Pais (no debe estar vacio)");
	// Comentario
	form.comentario.value = trim(espaciosSeguidos(form.comentario.value));
	error = validarCampoForm ("textoLargo", form.comentario.value, 2500, 10, error, "Comentario (no debe estar vacio)");

	if (error != "")
	{
		//alert("Se han encontrado algunos inconvenientes con el llenado de los datos. Ir a la parte superior para observar las correcciones a realizar.");
		error = "<h4><span>**</span> Se han encontrado algunos inconvenientes con el llenado de los datos: <span>**</span></h4>\n\n" + "<ul title='Resultado Formulario'>" + error + "</ul>";
		imprimeMensaje(error);		
		return false;
	}
	return true;
}

/** Valida los campos de la recomendacion **/
function validarRecomendacion(form)
{
	var error = "";

	error = validarCampoForm ("textoCorto", trim(espaciosSeguidos(form.tellname.value)), 40, 3, error, "Tu Nombre (no debe estar vacio)");
	error = validarCampoForm ("mail", trim(espaciosSeguidos(form.tellemail.value)), 90, 10, error, "Tu E-Mail (no debe estar vacio)");
	error = validarCampoForm ("mail", trim(espaciosSeguidos(form.tellfriend.value)), 90, 10, error, "E-Mail de tu Amigo (no debe estar vacio)");

	if (error != "")
	{
		error = "<h4><span>**</span> Se han encontrado algunos inconvenientes con el llenado de los datos: <span>**</span></h4>\n\n" + "<ul title='Resultado Formulario'>" + error + "</ul>";
		imprimeMensaje(error);		
		return false;
	}
	return true;
}