if(typeof(USER)=="undefined"){
	var gpLogin=Jade.Objects.Container();
	gpLogin.Location(275,130,450,150);
	gpLogin.Add();
	
	var lblUser=Jade.Objects.Text(Jade.Lang.Get('user_name')+':');
	lblUser.Location(25,25,150,20);
	lblUser.Add(gpLogin);
	
	var lblPass=Jade.Objects.Text(Jade.Lang.Get('pass')+':');
	lblPass.Location(25,50,150,20);
	lblPass.Add(gpLogin);
	
	var txtUser=Jade.Objects.TextBox();
	txtUser.Location(160,20,265,20);
	txtUser.SetText(Jade.Cookie.Get('last_user')!=null?Jade.Cookie.Get('last_user'):'');
	txtUser.SetMaxLength(25);
	txtUser.Add(gpLogin);
	
	var txtPass=Jade.Objects.TextBox('password');
	txtPass.Location(160,45,265,20);
	txtPass.SetMaxLength(80);
	txtPass.Add(gpLogin);
	
	var chkRemember=Jade.Objects.CheckBox(Jade.Lang.Get('remember_user'));
	chkRemember.Location(160,75,265,20);
	chkRemember.SetCheck(Jade.Cookie.Get('last_user')!=null?true:false);
	chkRemember.Add(gpLogin);

	var formValidator=Jade.Objects.Validator();
	formValidator.Add(txtUser,'required',Jade.Lang.Get('user_name'));
	formValidator.Add(txtPass,'required',Jade.Lang.Get('pass'));
	
	var cmdOk=Jade.Objects.Button(Jade.Lang.Get('enter'));
	cmdOk.Location(25,100,400,22);
	cmdOk.Add(gpLogin);
	
	var linkRecover=Jade.Objects.Link(Jade.Lang.Get('forgot_password'));
	linkRecover.Location(25,130,400,20);
	linkRecover.SetHref('?recover');
	linkRecover.style.textAlign='center';
	//linkRecover.Add(gpLogin);
	
	if(CONFIG.COMPANY_SHOW==1){
		gpLogin.Position(275,130);
		gpLogin.Size(450,175);
		lblUser.Position(25,50);
		lblPass.Position(25,75);
		txtUser.Position(160,45);
		txtPass.Position(160,70);
		chkRemember.Position(160,100);
		cmdOk.Position(25,125);
		linkRecover.Position(25,155);
		var lblCompany=Jade.Objects.Text(Jade.Lang.Get('company')+':');
		lblCompany.Location(25,25,150,20);
		lblCompany.Add(gpLogin);
		var cmbCompany=Jade.Objects.ComboBox();
		cmbCompany.Location(160,20,265,20);
		cmbCompany.Add(gpLogin);
		
		var ajaxCompany=Jade.Ajax();
		ajaxCompany.AddParam('query',"SELECT * FROM EMPRESA_CONFIGURACION WHERE LOWER(Nombre)=LOWER('Descripcion') ORDER BY Codigo");
		ajaxCompany.Send("lib/php/ajax_db.php");
		ajaxCompany.ClearParams();
		ajaxCompany.oncomplete=function(){
			if(!this.json.error){
				if(this.json.row.length!=0){
					for(var i=0; i<this.json.row.length; i++){
						var item=this.json.row[i];
						cmbCompany.AddItem(item.Codigo+' - '+item.Valor, item.Codigo);
					}
					cmbCompany.SetSelectedKey(CONFIG.COMPANY_DEFAULT);
				}
			}
			else{ Jade.Debug.Add(this.text); }
		}
	}
	else{
		var ajaxCompany=Jade.Ajax();
		ajaxCompany.AddParam('query',"SELECT * FROM EMPRESA_CONFIGURACION WHERE Codigo='"+CONFIG.COMPANY_DEFAULT+"'");
		ajaxCompany.Send("lib/php/ajax_db.php");
		ajaxCompany.ClearParams();
		ajaxCompany.oncomplete=function(){
			if(!this.json.error){
				if(this.json.row.length==0){
					Jade.FX.Hide(gpLogin);
					Jade.Objects.MessageBox(Jade.Strings.Format(Jade.Lang.Get('error_company_no_exist'), CONFIG.COMPANY_DEFAULT), 'error');
				}
			}
			else{ Jade.Debug.Add(this.text); }
		}
	}
	
	var gpError=Jade.Objects.Container();
	gpError.Location(gpLogin.x, gpLogin.y+gpLogin.height+5,gpLogin.width, 50);
	gpError.SetStyle('error');
	Jade.FX.Hide(gpError);
	gpError.Add();
	
	var lblError=Jade.Objects.Text();
	lblError.Location(15,5,435,20);
	lblError.Add(gpError);
	
	var ajaxLogin=Jade.Ajax();
	var ajaxLoginScript=Jade.Ajax();
	
	cmdOk.onclick=function(){
		if(formValidator.Validate()){
			$.sajax("lib/php/login.php", {company: (CONFIG.COMPANY_SHOW==1 ? cmbCompany.selectedKey : CONFIG.COMPANY_DEFAULT), user: txtUser.GetText(), pass: txtPass.GetText()}).oncomplete=function(){
				if(!this.json.error){
					if(this.json.logged){
						if(chkRemember.GetCheck()){ $.Cookie.Set('last_user', txtUser.GetText(), 3000); }
						else { $.Cookie.Delete('last_user'); }
						Jade.Redirect('?resume');
					}
					else{
						$.FX.Fade(gpError, 0, 100);
						lblError.SetText($.lng('error_login_nomatch'));					
					}
				}
				else{
					$.FX.Fade(gpError, 0, 100);
					lblError.SetText(this.json.error);
				}
			}
		}
		else{ if($.FX.IsShow(gpError)){ $.FX.Fade(gpError,50,0,2,10); } }
	}
	
	txtUser.onkeyenter=function(){ cmdOk.onclick(); }
	txtPass.onkeyenter=function(){ cmdOk.onclick(); }
}
else{
	Jade.Redirect('?resume'); 
}