// JavaScript Document
var oFichaRecomendar = new function FichaRecomendar(){
	//ELEMs
	this.nombre = document.getElementById('recomendarNombre');
	this.email = document.getElementById('recomendarEmail');
	this.nombreAmigo = document.getElementById('recomendarNombreAmigo');
	this.emailAmigo = document.getElementById('recomendarEmailAmigo');
	this.mensaje = document.getElementById('recomendarMensaje');
	this.seccion = document.getElementById('recomendarIdSeccion');
	this.elemento = document.getElementById('recomendarIdElemento');
	this.encargado = document.getElementById('recomendarIdEncargado');
	this.btnCerrar = document.getElementById('recomendarCerrar');
	this.btnEnviar = document.getElementById('recomendarEnviar');
	//
	this.errNombre = document.getElementById('recomendarErrorNombre');
	this.errEmail = document.getElementById('recomendarErrorEmail');
	this.errNombreAmigo = document.getElementById('recomendarErrorNombreAmigo');
	this.errEmailAmigo = document.getElementById('recomendarErrorEmailAmigo');
	this.errMensaje = document.getElementById('recomendarErrorMensaje');
	this.layError = document.getElementById('recomendarError');
	this.layExito = document.getElementById('recomendarExito');
	this.layLoader = document.getElementById('recomendarLoader');
	//
	this.btnAcepError = document.getElementById('recomendarAceptarError');
	this.btnAcepExito = document.getElementById('recomendarAceptarExito');
	
	//PROPs
	this.sImgErr = '<img src="'+DIR_SERVER_ROOT+'img/redArrow.gif" class="recomendarImgError" 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();
			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.nombreAmigo.__onblur = function(){
		if(trim(this.nombreAmigo.value)!='' && this.nombreAmigo.value.length >= 3) this.estiloError(false, this.nombreAmigo, this.errNombreAmigo, '');
		else this.estiloError(true, this.nombreAmigo, this.errNombreAmigo, 'Complete&nbsp;el&nbsp;nombre');
	}.closure(this);
	this.emailAmigo.__onblur = function(){
		if(trim(this.emailAmigo.value)!='' && this.emailAmigo.value.search(this.erEmail) != -1) this.estiloError(false, this.emailAmigo, this.errEmailAmigo, '');
		else this.estiloError(true, this.emailAmigo, this.errEmailAmigo, 'Cuenta&nbsp;de&nbsp;e-mail&nbsp;inválida');
	}.closure(this);
	this.mensaje.__onblur = function(){
		if(trim(this.mensaje.value)!='' && this.mensaje.value.length >= 10) this.estiloError(false, this.mensaje, this.errMensaje, '');
		else this.estiloError(true, this.mensaje, this.errMensaje, 'Complete&nbsp;el&nbsp;mensaje');
	}.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.nombreAmigo.__onblur();
		this.emailAmigo.__onblur();
		this.mensaje.__onblur();
	};
	//
	this.error = function(){
		this.layError.style.display = 'block';
	};
	this.exito = function(){
		this.layExito.style.display = 'block';
		this.nombre.value = this.email.value = this.nombreAmigo.value = this.emailAmigo.value = 
		this.mensaje.value = '';
	};
	//
	this.desactivar = function(si){
		this.nombre.disabled = this.email.disabled = this.nombreAmigo.disabled = this.emailAmigo.disabled = 
		this.mensaje.disabled = this.esperar = si;
	};
	//
	this.aceptar = function(){
		this.desactivar(false);
		this.layExito.style.display = this.layError.style.display = 'none';
	}.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.nombreAmigo, this.errNombreAmigo, '');
		this.estiloError(false, this.emailAmigo, this.errEmailAmigo, '');
		this.estiloError(false, this.mensaje, this.errMensaje, '');
		//
		popupCerrar(oRecomendar);
		//
		this.aceptar();
		this.desactivar(false);
		this.layLoader.style.display = 'none';
	}.closure(this);
	this.abrir = function(){
		popupAbrir(oRecomendar);
	}.closure(this);
	//
	this.recomendar = function(){
		var v = '';
		if(this.esperar) return false;
		//
		this.validar();
		if(!this.bError){
			this.desactivar(true);
			
			this.layLoader.style.display = 'block';
			
			v += 'idSeccion|=|'+this.seccion.value+'|&|';
			v += 'idElemento|=|'+this.elemento.value+'|&|';
			v += 'nombre|=|'+this.nombre.value+'|&|';
			v += 'email|=|'+this.email.value+'|&|';
			v += 'nombreAmigo|=|'+this.nombreAmigo.value+'|&|';
			v += 'emailAmigo|=|'+this.emailAmigo.value+'|&|';
			v += 'mensaje|=|'+this.mensaje.value+'|&|';
			
			this.req.pedir(DIR_SERVER_ROOT+'xmlHttpRequest/recomendar.php', v);
		}
	}.closure(this);
	
	//EVENTs
	AddEvent(this.btnCerrar, 'click', this.cerrar);
	AddEvent(this.btnEnviar, 'click', this.recomendar);
	AddEvent(this.btnAcepError, 'click', this.aceptar);
	AddEvent(this.btnAcepExito, 'click', this.aceptar);
	AddEvent(this.nombre, 'blur', this.nombre.__onblur);
	AddEvent(this.email, 'blur', this.email.__onblur);
	AddEvent(this.nombreAmigo, 'blur', this.nombreAmigo.__onblur);
	AddEvent(this.emailAmigo, 'blur', this.emailAmigo.__onblur);
	AddEvent(this.mensaje, 'blur', this.mensaje.__onblur);
	
	//llamada especial para recarga de Firefox
	this.desactivar(false);
}
