﻿Login={
	init:function(){
		this.makeButton();
		this.Panel.Make(this);
		$("user-toolbar").show();
	},
	makeButton:function(){
		var attachTo=this;
		this.button_login=document.createElement("A");	
		this.button_login.appendChild(document.createTextNode(basics.gl({de:"Anmelden",en:"Log in"})));
		this.button_login.href="#";
		this.button_login.style.cursor="pointer";
		this.button_login.onclick=function(){
			this.blur();
			$(Login.loginpane).addClassName("wait");
			$(Login.loginpane).show();
			Login.Com.GetForm(attachTo);
			return false
		}
		this.button_login.id="button_login";
		$("user-toolbar").appendChild(this.button_login);
	},

	tryLogin:function(theform){
		if(!(Login.FormFunction.Validate(theform)=="")){alert(basics.gl({de:"Bitte füllen Sie alle Felder aus!",en:"Please complete all the form fields!"}));return false}
		$(Login.loginpane).addClassName("wait");
		$(theform).hide();
		$(theform).request({
			parameters:$(theform).serialize(true),
			onComplete:function(transport){
				$(Login.loginpad).update(transport.responseText);
				//$(Login.loginpane).removeClassName("wait");
			}
		});
	},
	
	windowReload:basics.windowReload,

	checkLoginCookie:function(){
		return(jslib.cookies.read("user.status")=="activ");	//&&(jslib.cookies.read("sUserID")!=null))
	},
	
	checkLogin:function(){
		//return((typeof(userLoginVar) == undefined));	//&&(jslib.cookies.read("sUserID")!=null))
		return(document.getElementById('logedIn'));
	},
	Logout:function(){
		new Ajax.Request('/forms/loginform.gos?recaction=logout', {
			method:'get',
			onSuccess: function(transport) {
				Login.windowReload();
			}
		});
	},
	
	FormFunction:{
		Validate:function(theform){
			elms=Form.getElements(theform);
			var msg="";
			elms.each(function(elm) {
				//msg+=(elm.rel!="required"?"":(elm.value==""?elm.name:""));
				msg+=(elm.value==""?elm.name+"\n":"");
			});
			return (msg);
		}
	},
	
	Panel:{
		Make:function(attachTo){
			attachTo.loginpane=document.createElement("DIV");
			attachTo.loginpad=document.createElement("DIV");
			attachTo.ajaxRegister=document.createElement("DIV");

			//attachTo.myForm=document.createElement("FORM");
			//attachTo.myForm.test=document.createElement("INPUT");
			attachTo.loginpane.appendChild(attachTo.loginpad);
			attachTo.loginpad.appendChild(attachTo.ajaxRegister);
			attachTo.ajaxRegister.className="ajaxRegister";

			Element.extend(attachTo.loginpane);
			$(Login.loginpane).hide();
			//attachTo.loginpane.appendChild(document.createTextNode(basics.gl({de:"Anmeldeformular wird hier geladen...",en:"Login form here..."})));
			attachTo.loginpane.id="loginpane";
			$("user-toolbar").appendChild(attachTo.loginpane);
			this.AddTools(attachTo);
		},
		AddTools:function(attachTo){
			attachTo.close_button=document.createElement("A");	
			Element.extend(attachTo.close_button);
			attachTo.close_button.className="closer button";
			$(attachTo.close_button).update("X");
			attachTo.close_button.href="#";
			attachTo.close_button.panel=attachTo.loginpane;
			attachTo.close_button.onclick=function(){this.blur();$(attachTo.loginpane).hide();return false}
			attachTo.loginpane.appendChild(attachTo.close_button);
		}
	},
	
	Com:{
		GetForm:function(attachTo){
			//if($(attachTo.loginpane).state=="loaded"){return}
			/*
			new Ajax.Request('/forms/loginform.gos?recaction=form', {
				method:'get',
				onSuccess: function(transport) {
					$(attachTo.loginpad).update(transport.responseText);
					attachTo.Form=$(attachTo.loginpad).getElementsByTagName("form")[0];
					$(attachTo.loginpane).addClassName("act");
					$(attachTo.loginpane).state="loaded";
				}
			});
			*/

			new Ajax.Request('/assistant/ajaxregister/', {
				method:'get',
				onSuccess:function(transport){
					$(attachTo.loginpane).addClassName("act");
					$(attachTo.loginpane).state="loaded";
					$(attachTo.ajaxRegister).update(transport.responseText);
					$(attachTo.loginpane).removeClassName("wait");
				}
			});
		}
	}
}

Event.observe(window,"load",function(){
	if(!Login.checkLogin()){Login.init();}
	else{$("user-toolbar").show()}
});