// JavaScript Document
var Reserva = function(){
	this.req = new Request(this);
	var nombre = $('nombre');
	var apellido = $('apellido');
	var pais = $('pais');
	var ciudad = $('ciudad');
	var codPais = $('codPais');
	var codArea = $('codArea');
	var telefono = $('telefono');
	var email = $('email');
	var celular = $('celular');
	var tipoHabitacion = $('tipoHabitacion');
	var visitantes = $('visitantes');
	var trasladoSi = $('trasladoSi');
	var trasladoNo = $('trasladoNo');
	var comentarios = $("comentarios");
	var error = $("divError");
	var msjExito = $("exitoReserva");
	var msjFallo = $("falloReserva");
	var fechaIngreso = null;
	var fechaEgreso = null;
	var sobreMensaje = false;
	var totalErrores = 0;
	var lMinima = 3;

	var crearError = function(campo,mensaje,expresion,longitud){
		campo.expresion = expresion;

			if(longitud){
				campo.onblur = function(){
					campo.value = trim(campo.value);				
					if(!campo.noObligatorio || (campo.noObligatorio && campo.value != '')){
						if(!campo.expresion.test(trim(campo.value).toLowerCase()) || campo.value.length < lMinima)mostrarError(campo);
						else ocultarError(campo);
					}
					else ocultarError(campo);
				}				
			}
			else{

				campo.onblur = function(){
					campo.value = trim(campo.value);				
					if(!campo.noObligatorio || (campo.noObligatorio && campo.value != '')){
						if(!campo.expresion.test(trim(campo.value).toLowerCase()))mostrarError(campo);
						else ocultarError(campo);
					}
					else ocultarError(campo);					
				}				
			}

	}
	this.analizarTecla = function(e){
		if(e.keyCode == 13 && !sobreMensaje)this.enviar();
	}
	
	var crearErrores = function(){
		crearError(nombre,'',ER_STR,true);
		crearError(pais,'',ER_STR,true);
		crearError(email,'',ER_EMAIL);
		crearError(telefono,'',ER_STR,true);
		visitantes.noObligatorio = true;
		crearError(visitantes,'',ER_NATURAL_NOCERO);
		/*fechaIngreso.noObligatorio = true;
		crearError(fechaIngreso,'',ER_FECHA);
		fechaEgreso.noObligatorio = true;
		crearError(fechaEgreso,'',ER_FECHA);*/
	}
	var mostrarError = function(campo){
		totalErrores++;
		campo.className = "error";
	}
	var ocultarError = function(campo){
		campo.className = '';
	}
	this.enviar = function(e){
		totalErrores = 0;
		
		FireEvent(nombre,'blur');
		FireEvent(pais,'blur');
		FireEvent(telefono,'blur');
		FireEvent(visitantes,'blur');
		FireEvent(email,'blur');

		if(fechaEgreso){
			if(fechaIngreso.cont.className.substr(fechaIngreso.cont.className.length-('dijitError').length) == 'dijitError')totalErrores++;
			if(fechaIngreso.cont.className.substr(fechaIngreso.cont.className.length-('dijitError').length) == 'dijitError')totalErrores++;		
		}
		
		
		if(totalErrores == 0){
//			MostrarContenido(enviando);
			blockearCampos(true);
			Blocker.show();;
			Loader.show();			
			var trans = '';
			if(trasladoSi.checked)trans = 'Si';
			else if(trasladoNo.checked)trans = 'No';
			var v = 'nombre' + SEP_IGUAL + trim(nombre.value) + SEP_AND + 'apellido' + SEP_IGUAL + trim(apellido.value) + SEP_AND + 'pais' + SEP_IGUAL + trim(pais.value) + SEP_AND + 'ciudad' + SEP_IGUAL + trim(ciudad.value) + SEP_AND + 'celular' + SEP_IGUAL + trim(celular.value) + SEP_AND;
			v += 'telefono' + SEP_IGUAL + trim(telefono.value) + SEP_AND + 'email' + SEP_IGUAL + trim(email.value) + SEP_AND  + 'codPais' + SEP_IGUAL + trim(codPais.value) + SEP_AND + 'codArea' + SEP_IGUAL + trim(codArea.value) + SEP_AND;
			if(fechaEgreso){
				v += 'fechaEgreso' + SEP_IGUAL + trim(fechaEgreso.value) + SEP_AND + 'fechaIngreso' + SEP_IGUAL+trim(fechaIngreso.value) + SEP_AND;
			}
			v += 'tipoHabitacion' + SEP_IGUAL + trim(tipoHabitacion.value) + SEP_AND + 'visitantes' + SEP_IGUAL + trim(visitantes.value) + SEP_AND + 'traslado' + SEP_IGUAL + trans + SEP_AND + 'comentarios' + SEP_IGUAL + trim(comentarios.value) + SEP_AND;
			 
			this.req.pedir(URL_ROOT+"requests/reservas.php",v);
		}
		else{
			error.style.display = "inline";
			
		}
		if(e)StopEvent(e);
	}.closure(this);
	var blockearCampos = function(blockeo){
		nombre.disabled = blockeo;
		apellido.disabled = blockeo;
		pais.disabled = blockeo;
		ciudad.disabled = blockeo;
		fechaIngreso.disabled = blockeo;
		fechaEgreso.disabled = blockeo;
		codPais.disabled = blockeo;
		codArea.disabled = blockeo;
		telefono.disabled = blockeo;
		celular.disabled = blockeo;		
		email.disabled = blockeo;
		tipoHabitacion.disabled = blockeo;
		visitantes.disabled = blockeo;
		comentarios.disabled = blockeo;
	}

	var limpiarCampos = function(){
		fechaIngreso.value = '';
		fechaEgreso.value = '';
		visitantes.value = '';
		comentarios.value = '';
		$('habitacionDefecto').selected = true;
		error.style.display = "none";
		trasladoSi.checked = false;
		ocultarError(nombre);
		ocultarError(pais);		
		ocultarError(email);
		ocultarError(visitantes);		
		ocultarError(telefono);		
	}
	this.onRequestLoad = function(){
		var nodo = this.req.respuestaXML;
		if(!nodo){
			Loader.hide();
			Error.message = msjFallo.innerHTML;
			Error.onAcept = Error.onCancel = function(){
				Error.hide();
				blockearCampos(false);
				Blocker.hide();;
			}
			Error.show();

			return false;
		}
		if(nodo.getAttribute("exito") != 1){
			Error.message = msjFallo.innerHTML;
			Error.onAcept = Error.onCancel = function(){
				Error.hide();
				blockearCampos(false);
				Blocker.hide();;
			}
			Error.show();

		}
		else{
			Exito.message = msjExito.innerHTML;
			Exito.onAcept = Exito.onCancel = function(){
				Exito.hide();
				blockearCampos(false);	
				Blocker.hide();;
				limpiarCampos();
			}
			Exito.show();

		}
		Loader.hide();
	}
	crearErrores();
	
	AddEvent(comentarios,'focus',function(){
		sobreMensaje = true;								   
    });
	AddEvent(comentarios,'blur',function(){
		sobreMensaje = false;								   
    });
	var setearFechas = function(){
		var dateBox = new dijit.form.DateTextBox({
			required: false,
			id: 'fechaIngreso',
			maxLength: '10'
		});
		$('contFechaIngreso').appendChild(dateBox.domNode);
		
		fechaIngreso = $('fechaIngreso');
		fechaIngreso.cont = $('widget_fechaIngreso');
		fechaIngreso.cont.style.width = '110px';

		
		var dateBox = new dijit.form.DateTextBox({
			required: false,
			id: 'fechaEgreso',
			maxLength: '10'
		});
		$('contFechaEgreso').appendChild(dateBox.domNode);
		fechaEgreso = $('fechaEgreso');
		fechaEgreso.cont = $('widget_fechaEgreso');
		fechaEgreso.cont.style.width = '110px';	
		if(Nav.esIE6){
			fechaIngreso.cont.style.height = '26px';			
			fechaEgreso.cont.style.height = '26px';

		}
		
	}
	dojo.addOnLoad(setearFechas);
	

}
