function ltrim(s) {     return s.replace(/^\s+/, ""); }  
function rtrim(s) {     return s.replace(/\s+$/, ""); }  
function trim(s) {     return rtrim(ltrim(s)); }
var ventanaCalendario=false;
var expresion0 = "[\'\@\.\,\*\+\|\^\$\?\{\(\[]";
var expresion1 = "[\'\@\ \.\,\*\+\|\^\$\?\{\(\[]";
var expresion2 = "[0-9]";
var expresion3 = "[a-z]|[A-Z]|ñ|Ñ";
var expresion4 = "[\á\é\í\ó\ú\Á\É\Í\Ó\Ú]";

var exp0 = new RegExp(expresion0);
var exp1 = new RegExp(expresion1);
var exp2 = new RegExp(expresion2);
var exp3 = new RegExp(expresion3);
var exp4 = new RegExp(expresion4);
var exp5 = new RegExp(/^\d+$/); //acepta solo numeros sin signos
function GeneralVeriPhone(tel1,tel2,tel3){
	res = "";
	tel1 = trim(tel1); tel2 = trim(tel2); tel3 = trim(tel3);
	if( tel1.length==0 && tel2.length==0 && tel3.length==0 ){ res="vacio"; 
	}else{
		if( exp5.test(tel1) && exp5.test(tel2) && exp5.test(tel3) ){
			if( tel1.length==3 && tel2.length==3 && tel3.length==4 ){ res="ok"; }else{ res="incompleto"; }
		}else{
			res="invalido"; 
		}
	}
	return res;
}
var ventanaCalendario=false;
function muestraCalendario(raiz,formulario_destino,campo_destino,mes_destino,ano_destino,formato){
	//funcion para abrir una ventana con un calendario.
	//Se deben indicar los datos del formulario y campos que se desean editar con el calendario, es decir, los campos donde va la fecha.
	if (typeof ventanaCalendario.document == "object") {
		ventanaCalendario.close()
	}
	ventanaCalendario = window.open("calendario/index.php?formato=" + formato + "&formulario=" + formulario_destino + "&nomcampo=" + campo_destino,"calendario","width=300,height=300,left=100,top=100,scrollbars=no,menubars=no,statusbar=NO,status=NO,resizable=YES,location=NO")
}
function FSfncValidateEmailAddress (FormField,CheckTLD) {
	// CheckTLD es opcionalis optional, acepta los valores true, false, y null.
	emailStr = FormField.value.toLowerCase()
	if (CheckTLD==null) {CheckTLD=true}
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {alert(FormField.name + " parece ser incorrecta (Compruebe arroba (@) y puntos (.))"); FormField.focus(); FormField.focus(); return false}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {alert(FormField.title + ": nombre de usuario contiene caracteres inválidos"); return false}}
	for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {alert(FormField.title + ": nombre de dominio contiene caracteres inválidos"); FormField.focus(); return false}}
	if (user.match(userPat)==null) {alert(FormField.title + ": nombre de usuario no es válido."); FormField.focus(); return false}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {for (var i=1;i<=4;i++) {if (IPArray[i]>255) {alert(FormField.title + ": dirección IP no es válida"); FormField.focus(); return false}}; return true}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {if (domArr[i].search(atomPat)==-1) {alert(FormField.name + ": nombre de dominio no es válido."); FormField.focus(); return false}}
	if ((CheckTLD) && (domArr[domArr.length-1].length!=2) && (domArr[domArr.length-1].search(knownDomsPat)==-1)) {alert(FormField.title + ": debe terminar en dominio bien conocido o país de dos letras."); FormField.focus(); return false}
	if (len<2) {alert(FormField.title + ": le falta un nombre de anfitrión"); FormField.focus(); return false}
	return true;
}
function formReservas(){
	msn = "";
	campo1 = window.document.getElementById("cliente").value;
	campo2 = window.document.getElementById("email").value;
	campo3 = window.document.getElementById("area").value;
	campo4 = window.document.getElementById("telepri").value;
	campo5 = window.document.getElementById("telefono").value;
	
	if( campo1=="" ){ msn+="- Nombre vacio.\n"; }else{ 
		if( exp0.test(campo1) || exp2.test(campo1) ){ msn+="- Nombre incorrecto.\n";  }
	}
	if(campo2==""){ msn+="- Email vacio.\n"; }else{
		if( !FSfncValidateEmailAddress(window.document.getElementById("email"),false) ){ msn+="- Email incorrecto.\n"; }
	}
	msnTel = GeneralVeriPhone(campo3,campo4,campo5);
	if( msnTel!="ok" ){ msn+="- Telefono "+msnTel+".\n"; }

	if( msn!="" ){ alert(msn); }else{ window.document.form1.submit(); }

}
function abrir(pagina,titulo){
	ventana = window.open(pagina,titulo,'fullscreen=1,scrollbars=yes,reizable=yes,status=no,location=no,toolbar=yes');
}