// JavaScript Document
var oRecSite = new function(){
	var dirImg = 'img/';

	var iniciado = false;
	var contenedor = null;
	var nombre = null;
	var email = null;
	var amigo = null;
	var emailAmigo = null;
	var amigoComprador = null;
	var amigoAgente = null;
	var recomendarError = null;
	var mensaje = null;
	var blockeador = null; 
	var cerrar = null;
	var enviar = null;
	var enviando = null;
	var exitoRec = null;
	var falloRec = null;
	var totalErrores = 0;


	
	var crearError = function(campo,mensaje,expresion){
		campo.expresion = expresion;
		campo.onblur = function(){
			if(!campo.expresion.test(campo.value.toLowerCase()))mostrarError(campo);
			else ocultarError(campo);
		}

	}
	var crearErrores = function(){
		crearError(nombre,'Ingrese su nombre',ER_STR);
		crearError(email,'Email incorrecto',ER_EMAIL);
		crearError(amigo,'Ingrese el nombre del amigo',ER_STR);
		crearError(emailAmigo,'Email incorrecto',ER_EMAIL);
	}
	var mostrarError = function(campo){
		totalErrores++;
		campo.style.borderColor = "#FF0000";
	}
	var ocultarError = function(campo){
		campo.style.borderColor = "";
	}

	this.mostrarCuadro = function(e){
		Blocker.show();
		if(!iniciado)iniciarClase();
		if(iniciado){
			centrarContenido(contenedor);
			contenedor.style.visibility = "visible";
			contenedor.style.zIndex = "2100";
			limpiarCampos();
			try{ nombre.focus(); }catch(e){};
		}
		if(e)StopEvent(e);
	}
	var enviarRecomendacion = function(){
		totalErrores = 0;
		FireEvent(nombre,'blur');
		FireEvent(email,'blur');
		FireEvent(amigo,'blur');
		FireEvent(emailAmigo,'blur');		
		
		nombre.onblur();
		email.onblur();
		amigo.onblur();
		emailAmigo.onblur();
		if(totalErrores == 0){
			ocultarContenido(contenedor);
			recomendarError.style.display = "none";
			centrarContenido(enviando);
			enviando.style.visibility = "visible";
			blockearCampos(true);
			var claseAmigo = 'comprador';
			if(amigoAgente.checked)claseAmigo = 'agente';
			
			var v = 'nombre'+ SEP_IGUAL + nombre.value + SEP_AND + 'email'+ SEP_IGUAL + email.value + SEP_AND + 'amigo'+ SEP_IGUAL + amigo.value + SEP_AND + 'emailAmigo' + SEP_IGUAL + emailAmigo.value + SEP_AND + 'claseAmigo' + SEP_IGUAL + claseAmigo + SEP_AND;
			req.pedir(URL_ROOT + "request/recSite.php",v);
		}
		else recomendarError.style.display = 'block';
	}.closure(this);
	var blockearCampos = function(blockeo){
		nombre.disabled = blockeo;
		email.disabled = blockeo;
		amigo.disabled = blockeo;
		emailAmigo.disabled = blockeo;
	}
	var iniciarClase = function(){
		contenedor = $('contenedorRecSite');
		if(contenedor){
			exitoRec = $('exitoRecSite');
			falloRec = $('falloRecSite');
			enviando = $('enviandoRecSite');
			iniciado = true;
			nombre = $('nombreRecSite');
			email = $('emailRecSite');
			amigo = $('nombreAmigoRecSite');
			emailAmigo = $('emailAmigoRecSite');
			recomendarError = $('recSiteError');
			amigoComprador = $('amigoCompradorRecSite');
			amigoAgente = $('amigoAgenteRecSite');	
			AddEvent($('cerrarRecSite'),'click',function(){ocultarContenido(contenedor);Blocker.hide();});
			AddEvent($('enviarRecSite'),'click',enviarRecomendacion);

			AddEvent(contenedor,'keypress',function(e){
				if(e.keyCode == 27){
					ocultarContenido(contenedor);
					mostrarBlockeador();
				}
			});
			crearErrores();
		}
		
	}
	var limpiarCampos = function(){
		amigo.value = '';
		emailAmigo.value = '';
		amigoComprador.checked = true;
		ocultarError(nombre);
		ocultarError(email);
		ocultarError(amigo);
		ocultarError(emailAmigo);
	}
	var onRequestLoad = function(){
		var nodo = req.respuestaXML;
		if(!nodo){
			Error.message = this.msgFallo;
			Error.onAcept = Error.onCancel = function(){
				Error.hide();
				blockearCampos(false);
				centrarContenido(contenedor);
				contenedor.style.visibility = "visible";

			}
			Error.show();
			ocultarContenido(enviando);
			return false;
		}
		if(nodo.getAttribute("exito") != 1){
			
			Error.message = this.msgFallo;
			Error.onAcept = Error.onCancel = function(){
				Error.hide();
				blockearCampos(false);				
				centrarContenido(contenedor);
				contenedor.style.visibility = "visible";
				
			}
			Error.show();
		}
		else{
			Exito.message = this.msgExito;
			Exito.onAcept = Exito.onCancel = function(){
				Exito.hide();
				blockearCampos(false);	
				Blocker.hide();;
			}
			Exito.show();
		}
		ocultarContenido(enviando);
	}.closure(this);
	var req = new Request(onRequestLoad);	

}
