function ponValorFormulario(strNombre, strTitulo, strValor)
{
	obj=leeElementoPagina(strNombre + "_val")
	obj.value=strTitulo;

	obj=leeElementoPagina(strNombre)
	obj.value=strValor;
	if (obj.onchange!=null) obj.onchange()
}

function eliminaValorFormulario(strNombre)
{
	obj=leeElementoPagina(strNombre + "_val")
	if (obj.value!="")
	{
		obj.value=""
		obj=leeElementoPagina(strNombre)
		obj.value=""
		if (obj.onchange!=null) obj.onchange()
	}
}

function leeCodigoCaracterFormulario(varEvento)
{
	if (window.event)
		if (window.event.ctrlKey)
			return(window.event.keyCode+1000)
		else
			return(window.event.keyCode)
	else if (varEvento)
		return varEvento.which;
	else
		return null
}

function leeCaracterFormulario(varEvento, strCaracteres)
{
	var key, keychar
	key = leeCodigoCaracterFormulario(varEvento)
	if (key == null) return true

	keychar = String.fromCharCode(key)
	keychar = keychar.toLowerCase()
	strCaracteres = strCaracteres.toLowerCase()
	alert(key + ","+  keychar + "," + strCaracteres.indexOf(keychar))
	if (strCaracteres.indexOf(keychar) != -1)
		return true
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==16  || key==18 || key==27 || key==37 || key==39 || key==46 || key==116 || key==1017 || key==1067 || key==1086 || key==1088)
		return true
	return false
}

function leeCaracterFormulario(varEvento, strCaracteres)
{
	var key, keychar
	key = leeCodigoCaracterFormulario(varEvento)
	if (key == null) return true

	keychar = String.fromCharCode(key)
	keychar = keychar.toLowerCase()
	strCaracteres = strCaracteres.toLowerCase()
	if (strCaracteres.indexOf(keychar) != -1)
		return true
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==16  || key==18 || key==27 || key==37 || key==39 || key==46 || key==116 || key==1017 || key==1067 || key==1086 || key==1088)
	   return true
	return false;
}

function validaHora(obj)
{
	var timePat = /^(\d{1,2}):(\d{2})$/
	var matchArray = obj.value.match(timePat)

	if (matchArray == null)
		obj.value=""
	else
	{
		intHora = matchArray[1]
		intMinutos = matchArray[2]
		if (intHora<0) intHora=0
		if (intHora>23) intHora=23
		if (intMinutos<0) intMinutos=0
		if (intMinutos>59) intMinutos=59
		obj.value=intHora + ":" + intMinutos
	}
}

function aceptaUrlFormulario(strNombre, strUrl)
{
	var obj=leeElementoPagina(strNombre)
	
	if (!obj) return
	obj.action=strUrl
	obj.submit()
}

function ponBoton(objObject, intTipo)
{
	ponBgcolorPagina(objObject.id, (intTipo)?'#BB9900':'#BB9900');
}

function enviarFormularioSugerencias()
{
	var obj = document.forms[0];
	msgError = "";
	
	if (trim(obj.nombre.value) == "")
		msgError += "* Debe introducir su nombre\n";
		
	if (trim(obj.apellido1.value) == "")
		msgError += "* Debe introducir su primer apellido\n";
		
	if (trim(obj.apellido2.value) == "")
		msgError += "* Debe introducir su segundo apellido\n";
		
	if (trim(obj.nif.value) == "")
		msgError += "* Debe introducir su NIF\n";
		
	if (trim(obj.direccion.value) == "")
		msgError += "* Debe introducir su dirección\n";
		
	if (trim(obj.poblacion.value) == "")
		msgError += "* Debe introducir su población\n";
		
	if (trim(obj.cp.value) == "")
	{
		msgError += "* Debe introducir su código postal\n";
	}
	else
	{
		if (trim(obj.cp.value).length != 5)
			msgError += "* El código postal debe tener 5 dígitos\n";
	}
	
	if ((trim(obj.telfijo.value) == "") && (trim(obj.telmovil.value) == ""))
		msgError += "* Debe introducir, al menos, un teléfono de contacto\n";	

	if (!validarEmail(trim(obj.email.value)))
		msgError += "* Debe introducir un correo electrónico correcto\n";

	if (trim(obj.expone.value) == "")
		msgError += "* Debe introducir el campo Expone\n";

	if (trim(obj.solicita.value) == "")
		msgError += "* Debe introducir el campo Solicita\n";
		
	if (msgError != "")
	{
		alert("Se han producido los siguientes errores:\n\n" + msgError);
	}
	else
	{
		// Enviamos el formulario
		obj.accion.value = "enviarCorreo";
		obj.submit();		
	}
}

function trim(cadena)
{
	for (i=0; i<cadena.length;)
	{
		if (cadena.charAt(i) == " ")
			cadena = cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for (i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if (cadena.charAt(i) == " ")
			cadena = cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}

function validarEmail(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	res = true;
	
	if (str.indexOf(at) == -1)
	{
		res = false;
	}
	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		res = false;
	}
	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		res = false;
	}
	else if (str.indexOf(at,(lat+1))!=-1)
	{
		res = false;
	}
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		res = false;
	}
	else if (str.indexOf(dot,(lat+2))==-1)
	{
		res = false;
	}
	else if (str.indexOf(" ")!=-1)
	{
		res = false;
	}

	return res;	
}

