// JavaScript Document
var oNews = {//
	l:document.getElementById('news'),//
	f:document.getElementById('newsBg'),//
	c:document.getElementById('newsContenido')//
};
opacidad(oNews.f, 45);
	
var oFichaNews = new function FichaNewsletter(){
	this.nombre = document.getElementById('newsNombre');
	this.apellido = document.getElementById("newsApellido");
	this.agencia = document.getElementById('newsAgencia');
	this.email = document.getElementById('newsEmail');
	this.pais = document.getElementById('newsPais');
	this.ciudad = document.getElementById('newsCiudad');
	this.tipoParticular = document.getElementById("tipoParticular");
	this.tipoAgente = document.getElementById("tipoAgente");
	titAg = document.getElementById("titAgencia");
	
	//
	this.btnEnviar = document.getElementById('newsEnviar');
	this.btnCerrar = document.getElementById('newsCerrar');
	this.btnAcepError = document.getElementById('newsAceptarExito');
	this.btnAcepExito = document.getElementById('newsAceptarError');
	//
	this.errNombre = document.getElementById('newsNombreError');
	this.errApellido = document.getElementById('newsApellidoError');	
	this.errAgencia = document.getElementById('newsAgenciaError');
	this.errEmail = document.getElementById('newsEmailError');
	this.errPais = document.getElementById('newsPaisError');
	this.errCiudad = document.getElementById('newsCiudadError');
	//
	this.layExito = document.getElementById('newsExito');
	this.layError = document.getElementById('newsError');
	this.layLoader = document.getElementById('newsLoader');
	
	//PROPs
	this.sImgErr = '<img src="'+DIR_SERVER_ROOT+'img/redArrow.gif" class="newsImgError" 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.tipoParticular.onclick = function(){
		titAg.innerHTML = "Agencia Preferida";	
	}	
	this.tipoAgente.onclick = function(){
		titAg.innerHTML = "Agencia";	
	}
	this.tipoParticular.onclick();
	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();
			return false;
		}
		//
		this.exito();
	}.closure(this);
	//
	this.idPais = '';
	this.oFC = new Filtrador('newsPais', {archivo:DIR_SERVER_ROOT+'xmlHttpRequest/listadoDePaises.php', obligatorio:false, posicionar:true, ancho:250});
	this.oFC.cuandoSelecciono = function(o){
		this.idPais = o.idPais;
	}.closure(this);
	this.oFC.cuandoFallo = function(){
		this.idPais = '';
	}.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.apellido.__onblur = function(){
		if(trim(this.apellido.value)!='' && this.apellido.value.length >= 3) this.estiloError(false, this.apellido, this.errApellido, '');
		else this.estiloError(true, this.apellido, this.errApellido, 'Complete&nbsp;su&nbsp;apellido');
	}.closure(this);
	this.pais.__onblur = function(){
		if(this.idPais != '') this.estiloError(false, this.pais, this.errPais, '');
		else this.estiloError(true, this.pais, this.errPais, 'Seleccione&nbsp;el&nbsp;pais');
	}.closure(this);
	this.ciudad.__onblur = function(){
		if(trim(this.ciudad.value)!='' && this.ciudad.value.length >= 3) this.estiloError(false, this.ciudad, this.errCiudad, '');
		else this.estiloError(true, this.ciudad, this.errCiudad, 'Complete&nbsp;la&nbsp;ciudad');
	}.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.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.apellido.__onblur();		
		this.email.__onblur();
		this.agencia.__onblur();
		this.ciudad.__onblur();
		this.pais.__onblur();
	};
	//
	this.error = function(){
		this.centrarLayer(this.layError);
	};
	this.exito = function(){
		this.centrarLayer(this.layExito);
		this.nombre.value = this.apellido.value = this.email.value = this.ciudad.value = this.agencia.value = this.pais.value = '';
		this.tipoParticular.checked = true;
		titAg.innerHTML = "Agencia Preferida";
	};
	//
	this.desactivar = function(si){
		this.nombre.disabled = this.email.disabled = this.agencia.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.apellido, this.errApellido, '');		
		this.estiloError(false, this.email, this.errEmail, '');
		this.estiloError(false, this.agencia, this.errAgencia, '');
		//
		popupCerrar(oNews);
		//
		this.aceptar();
		this.desactivar(false);
		this.layLoader.style.display = 'none';
	}.closure(this);
	this.abrir = function(){
		popupAbrir(oNews);
	}.closure(this);
	//
	this.registrarse = function(){
		var v = '';
		if(this.esperar) return false;
		//
		this.validar();
		if(!this.bError){
			this.desactivar(true);
			
			this.centrarLayer(this.layLoader);
			var tipReg = 'agente';
			if(this.tipoParticular.checked)tipReg = "particular";
			v += 'nombre|=|'+this.nombre.value+'|&|';
			v += 'apellido|=|'+this.apellido.value+'|&|';
			v += 'email|=|'+this.email.value+'|&|';
			v += 'agencia|=|'+this.agencia.value+'|&|';
			v += 'idPais|=|'+this.idPais+'|&|';
			v += 'ciudad|=|'+this.ciudad.value+'|&|';
			v += 'tipoRegistrado|=|'+tipReg+'|&|';
			
			this.req.pedir(DIR_SERVER_ROOT+'xmlHttpRequest/registrarse.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.registrarse);
	AddEvent(this.btnAcepError, 'click', this.aceptar);
	AddEvent(this.btnAcepExito, 'click', this.aceptarExito);
	AddEvent(this.nombre, 'blur', this.nombre.__onblur);
	AddEvent(this.apellido, 'blur', this.apellido.__onblur);	
	AddEvent(this.pais, 'blur', this.pais.__onblur);
	AddEvent(this.ciudad, 'blur', this.ciudad.__onblur);
	AddEvent(this.email, 'blur', this.email.__onblur);
	AddEvent(this.agencia, 'blur', this.agencia.__onblur);
	
	//llamada especial para recarga de Firefox
	this.desactivar(false);
}

if(!!argNewsletter){ oFichaNews.abrir.apply(oFichaNews, argNewsletter); }