// JavaScript Document
var oFichaComentario = new function FichaComentario(){
	//ELEMs
	this.nombre = document.getElementById('comentarioNombre');
	this.email = document.getElementById('comentarioEmail');
	this.agencia = document.getElementById('comentarioAgencia');
	this.telefono = document.getElementById('comentarioTelefono');
	this.comentario = document.getElementById('comentarioComentario');
	this.btnCerrar = document.getElementById('comentarioCerrar');
	this.btnEnviar = document.getElementById('comentarioEnviar');
	//
	this.errNombre = document.getElementById('comentarioErrorNombre');
	this.errEmail = document.getElementById('comentarioErrorEmail');
	this.errAgencia = document.getElementById('comentarioErrorAgencia');
	this.errComentario = document.getElementById('comentarioErrorComentario');
	this.layError = document.getElementById('comentarioError');
	this.layExito = document.getElementById('comentarioExito');
	this.layLoader = document.getElementById('comentarioLoader');
	//
	this.btnAcepError = document.getElementById('comentarioAceptarError');
	this.btnAcepExito = document.getElementById('comentarioAceptarExito');
	
	//PROPs
	this.sImgErr = '<img src="'+DIR_SERVER_ROOT+'img/redArrow.gif" class="comentarioImgError" alt="" />';
	this.bError = false;
	this.erEmail = /^[a-z0-9_\.\-]+@[a-z0-9_\-]+(\.[a-z0-9_\-]{2,20})*\.[a-z]{2,4}$/;
	this.esperar = false;
	
	//OBJs
	this.req = new Request();
	this.req.listener = function(){
		var o = this.req.respuestaXML;
		//loader
		this.layLoader.style.display = 'none';
		//error en el request
		if(!o || o.getAttribute('exito') != 1){
			this.error();
			//alert(this.req.respuestaHTML);
			return false;
		}
		//
		this.exito();
	}.closure(this);
	
	//METODs
	this.nombre.__onblur = function(){
		if(trim(this.nombre.value)!='' && this.nombre.value.length >= 3) this.estiloError(false, this.nombre, this.errNombre, '');
		else this.estiloError(true, this.nombre, this.errNombre, 'Complete&nbsp;su&nbsp;nombre');
	}.closure(this);
	this.email.__onblur = function(){
		if(trim(this.email.value)!='' && this.email.value.search(this.erEmail) != -1) this.estiloError(false, this.email, this.errEmail, '');
		else this.estiloError(true, this.email, this.errEmail, 'Cuenta&nbsp;de&nbsp;e-mail&nbsp;inválida');
	}.closure(this);
	this.agencia.__onblur = function(){
		if(trim(this.agencia.value)!='' && this.agencia.value.length >= 5) this.estiloError(false, this.agencia, this.errAgencia, '');
		else this.estiloError(true, this.agencia, this.errAgencia, 'Complete&nbsp;el&nbsp;nombre&nbsp;de&nbsp;la&nbsp;agencia');
	}.closure(this);
	this.comentario.__onblur = function(){
		if(trim(this.comentario.value)!='' && this.comentario.value.length >= 10) this.estiloError(false, this.comentario, this.errComentario, '');
		else this.estiloError(true, this.comentario, this.errComentario, 'Complete&nbsp;su&nbsp;comentario');
	}.closure(this);
	//
	this.estiloError = function(si, ele, lay, msj){
		if(si){
			lay.innerHTML = this.sImgErr+msj;
			lay.style.visibility = 'visible';
			ele.style.borderColor = '#ff0000';
			this.bError = true;
		}
		else{
			lay.innerHTML = '';
			lay.style.visibility = 'hidden';
			ele.style.borderColor = '';
		}
	};
	this.validar = function(){
		this.bError = false;
		this.nombre.__onblur();
		this.email.__onblur();
		this.agencia.__onblur();
		this.comentario.__onblur();
	};
	//
	this.error = function(){
		this.centrarLayer(this.layError);
	};
	this.exito = function(){
		this.centrarLayer(this.layExito);
		this.nombre.value = this.email.value = this.agencia.value = this.telefono.value = 
		this.comentario.value = '';
	};
	//
	this.desactivar = function(si){
		this.nombre.disabled = this.email.disabled = this.agencia.disabled = this.telefono.disabled = 
		this.comentario.disabled = this.esperar = si;
	};
	//
	this.aceptar = function(){
		this.desactivar(false);
		this.layExito.style.display = this.layError.style.display = 'none';
	}.closure(this);
	this.aceptarExito = function(){
		this.aceptar();
		this.cerrar();
	}.closure(this);
	//
	this.cerrar = function(){
		this.req.cancelar();
		//
		this.estiloError(false, this.nombre, this.errNombre, '');
		this.estiloError(false, this.email, this.errEmail, '');
		this.estiloError(false, this.agencia, this.errAgencia, '');
		this.estiloError(false, this.comentario, this.errComentario, '');
		//
		popupCerrar(oComentario);
		//
		this.aceptar();
		this.desactivar(false);
		this.layLoader.style.display = 'none';
	}.closure(this);
	this.abrir = function(){
		//
		popupAbrir(oComentario);
	}.closure(this);
	//
	this.comentar = function(){
		var v = '';
		if(this.esperar) return false;
		//
		this.validar();
		if(!this.bError){
			this.desactivar(true);
			
			this.centrarLayer(this.layLoader);
			
			v += 'nombre|=|'+this.nombre.value+'|&|';
			v += 'email|=|'+this.email.value+'|&|';
			v += 'agencia|=|'+this.agencia.value+'|&|';
			v += 'telefono|=|'+this.telefono.value+'|&|';
			v += 'comentario|=|'+this.comentario.value+'|&|';
			
			this.req.pedir(DIR_SERVER_ROOT+'xmlHttpRequest/consultar.php', v, 'POST');
		}
	}.closure(this);
	
	this.centrarLayer = function(l){
		l.style.display = 'block';
		l.style.marginLeft = Math.ceil((l.parentNode.offsetWidth-l.offsetWidth-4)/2)+'px';
	};
	
	//EVENTs
	AddEvent(this.btnCerrar, 'click', this.cerrar);
	AddEvent(this.btnEnviar, 'click', this.comentar);
	AddEvent(this.btnAcepError, 'click', this.aceptar);
	AddEvent(this.btnAcepExito, 'click', this.aceptarExito);
	AddEvent(this.nombre, 'blur', this.nombre.__onblur);
	AddEvent(this.email, 'blur', this.email.__onblur);
	AddEvent(this.agencia, 'blur', this.agencia.__onblur);
	AddEvent(this.comentario, 'blur', this.comentario.__onblur);
	
	//llamada especial para recarga de Firefox
	this.desactivar(false);
}