//ver docu.js.php
window.fwk.common.classUser=function(){
	var mthis=this;
	this.apiLogin="/fe/sites/usuario/login";
	this.feLogin="/fe/sites/usuario/login?layout=aplicacion&skin="+$("body").attr("skin");
	this.feLogout="/fe/sites/especial?skin="+$("body").attr("skin");
	this.apiLogout="/fe/sites/usuario/logout?showxml=true";
	this.urlRedir="";
	this.USUARIO_NO_EXISTE = "NE"; 
	this.USUARIO_NO_HABILITADO = "NH";
	this.USUARIO_PENAC = "PENAC";
	this.setVerbose=function(v){if(this.debug==true)alert(v);},
	this.load=function(){
	
		$("#userInfoLogout").click(function(event){
			event.preventDefault();
			mthis.logout();
		});
		$("#userInfoModify").click(function(event){
			event.preventDefault();
			mthis.goToModifyUser();
		});
		$("#userInfoSingIn").click(function(event){
			event.preventDefault();
			mthis.goToSingupUser()
		});
		/*$("#userInfoLogin").click(function(evento){
		alert("login");
			
		});
		*/								
		 $("#userInfoLogin").live("click", function(ev){ 
			 ev.preventDefault();
		 	mthis.goToLogin();			
		});
	},
	this.validate=function(form,urlRedir){	
			$("#cargandoLogin").fadeIn(500);
			$("#userRespLogin").fadeOut(2000);
			this.urlRedir=urlRedir;
			var location=((this.urlRedir!=undefined && this.urlRedir!="")?this.urlRedir:"/");
			mthis.setVerbose("username="+form.username.value+"\npassword="+form.password.value + "\nlocation="+ location);
			if(form.username.value=="" || form.password.value==""){
						$("#userRespLogin").fadeIn(2000);
						$("#cargandoLogin").fadeOut(500);
			}else{
			var dataAjax="showxml=true&ispost=true&usuario="+form.username.value+"&clave="+form.password.value;
				mthis.setVerbose("ajax :"+this.apiLogin +"?"+dataAjax);
				$.ajax({
				type: "POST",
				url: this.apiLogin,
				data: dataAjax,
				error: function(objeto, quepaso, otroobj){
				alert(objeto +" "+ quepaso +" "+ otroobj);
				$("#cargandoLogin").fadeOut(500);
        		},
				dataType: "xml",
				success: function(xml){	
				$("#cargandoLogin").fadeOut(500);
							var Valido="";
							$(xml).find('user_get').each(function(){
										Valido=$(xml).find('valido').text();
				 			});
							
							mthis.setVerbose("ajax success User Valido :"+Valido)														
							if(Valido=="N")
							{
								window.fwk.common.metricas.hit("login_error");
								$("#userRespLogin").fadeIn(2000);
							}else{
								window.fwk.common.metricas.hit("login_ok");								
								mthis.setVerbose(mthis.USUARIO_NO_HABILITADO+"=="+$(xml).find("resultado").find("activo").text());
								if(mthis.USUARIO_NO_HABILITADO==$(xml).find("resultado").find("activo").text())
								{top.window.location=window.fwk.controllers.mipase.url.feSingupUserInst+"&urlReturn="+escape(location);}
								else							
								{top.window.location=location;}
							}

						}
				});
			}
		return false;
	},
	this.goToSingupUser=function(option){
		if(option!=undefined){
			if(option.urlReturn!=undefined){
			
				top.window.location=window.fwk.controllers.mipase.url.feSingupUser+"&urlReturn="+escape(option.urlReturn);
		  	}
		}else{			
			top.window.location=window.fwk.controllers.mipase.url.feSingupUser+"&urlReturn="+escape(top.window.location);
		}
	},
	this.goToModifyUser=function(option){
		if(option!=undefined){
			if(option.urlReturn!=undefined){
			top.window.location=window.fwk.controllers.mipase.url.feModifyUser+"&urlReturn="+escape(option.urlReturn);
		  	}
		}else{
			top.window.location=window.fwk.controllers.mipase.url.feModifyUser+"&urlReturn="+escape(top.window.location);
		}
	},
	this.goToLogin=function(option){
	if(option!=undefined){
		if(option.urlReturn!=undefined){
			top.window.location=this.feLogin+"&urlReturn="+escape(option.urlReturn);
	  	}
	}else{
		top.window.location=this.feLogin+"&urlReturn="+escape(top.window.location);
	}
		
	}
	this.catchLogin=function(){

	},
	this.logout=function(location){
		var vlocation="";
			if(location==undefined){
				vlocation=this.feLogout;
			}else{
				vlocation=location;
			}
			mthis.setVerbose("to redir a "+vlocation+ "\n al api: "+this.apiLogout	);			
			$.ajax({type: "POST",url: this.apiLogout, data: "",dataType: "xml",
							success: function(xml){							
										var resultado="";
										$(xml).find('logout_get').each(function(){
											resultado=$(xml).find('resultado').text();
							 			});
										
										mthis.setVerbose("ajax logout resultado :"+resultado);
										if(resultado=="OK")									
										{top.window.location=vlocation;}
										else
										{top.window.location=mthis.feLogout;}
			
									}
					});
				
	}
}

$(document).ready(function(){
	window.fwk.common.user=new window.fwk.common.classUser();
	window.fwk.common.user.load();
});