var _isFF=false; var _isIE=false; var _isOpera=false; var _isKHTML=false; var _isMacOS=false;

if (navigator.userAgent.indexOf('Macintosh') != -1) _isMacOS=true;
if ((navigator.userAgent.indexOf('Safari') != -1)||(navigator.userAgent.indexOf('Konqueror')!= -1))
    _isKHTML=true;
else if (navigator.userAgent.indexOf('Opera') != -1){
    _isOpera=true;
    _OperaRv=parseFloat(navigator.userAgent.substr(navigator.userAgent.indexOf('Opera')+6,3));
    }
else if(navigator.appName.indexOf("Microsoft")!=-1)
    _isIE=true;
else {
    _isFF=true;
    var _FFrv=parseFloat(navigator.userAgent.split("rv:")[1])
    }


function AuthClass()
{
	this.xhreq=null;
	this.bg=null;
	this.adiv=null;
	this.uninput=null;
	this.pwinput=null;
	this.message=null;
	this.oldmessage=null;
	this.url="auth/loginxml";
	this._debug=false;
	this.__loginactions=new Array();
	this.__logoutactions=new Array();
	this._loggedin=false;
	this._username=null;
	this._timer=null;
	this._expmsg="Session expired, log in again";
}

AuthClass.prototype.init=function(url,adiv,bg,uinp,pinp,msg)
{
	this.url=url
	if (bg) this.bg=bg
		else this.bg=document.getElementById("authtransbg")
	if (adiv) this.adiv=adiv
		else this.adiv=document.getElementById("authdiv");
	if (uinp) this.uninput=uinp
		else this.uninput=document.getElementById("uninput");
	if (pinp) this.pwinput=pinp
		else this.pwinput=document.getElementById("pwinput");
	if (msg) this.message=msg
		else this.message=document.getElementById("authmessage");
}

AuthClass.prototype.popup=function(m)
{	
	if (m) this.writeMessage(m);
	if (this.bg) bg.style.visibility="visible";
	this.adiv.style.visibility="visible";
	this.uninput.focus();
	this.uninput.select();
}

AuthClass.prototype.close=function ()
{
	if (this.bg) this.bg.style.visibility="hidden";
	this.adiv.style.visibility="hidden";
}		

AuthClass.prototype.sendauth=function (logout)
{
	if ((!_isIE)&&(window.XMLHttpRequest))
		this.xhreq = new XMLHttpRequest();
	else
	{
   		if (document.implementation && document.implementation.createDocument)
   		{
   			this.xhreq = document.implementation.createDocument("", "", null);
   			this.xhreq.onload = new this.onLoad(this);
			this.xhreq.load(url);
			return;
   		}
   		else
       			this.xhreq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	var sdata;
	if (logout)
		sdata="<?xml version=\"1.0\"?>\n<auth>\n\t<logout>true</logout>\n</auth>\n";
	else	
		sdata="<?xml version=\"1.0\"?>\n<auth>\n\t<username>"+escape(this.uninput.value)+"</username>\n\t<password><![CDATA["+this.pwinput.value+"]]></password>\n</auth>\n";
	this.xhreq.open("POST",this.url,true);
	this.xhreq.setRequestHeader('Content-type','text/xml');
	this.xhreq.onreadystatechange=new this.onLoad(this,logout);
	this.xhreq.send(sdata);
	if (this._debug) alert("Sending data:\n"+sdata);

};
AuthClass.prototype.logout=function ()
{
	this.sendauth(true);
};

AuthClass.prototype.onLoad=function(obj,logout)
{ 
	var once=true;
	var islogout=logout;
	this.check=function (){
		if  ((!obj.xhreq.readyState)||(obj.xhreq.readyState == 4))
		{
			if (!once) return; once=false; //IE 5 fix
			obj.processReply(islogout);
		}
	};
	return this.check;
};

AuthClass.prototype.processReply=function(islogout)
{
	if (this._debug) alert("Received data:\n"+this.xhreq.responseText);
	var xml=this.xhreq.responseXML;
	if (islogout)
	{
		if (xml)
		{
			var status=xml.getElementsByTagName("status");
			if (valOf(status)=="OK")
			{
				this._loggedOut(valOf(xml.getElementsByTagName("shop")),xml);
			}
			else
			{
			}
		} 
		else
		{
		}
	
	}
	else
	{
		if (xml)
		{
			var status=xml.getElementsByTagName("status");
			if (valOf(status)=="OK")
			{
				this.close();
				this._loggedIn(valOf(xml.getElementsByTagName("username")),valOf(xml.getElementsByTagName("shop")),xml.getElementsByTagName("transport"),xml);
				this.pwinput.value="";
				
				
			}
			else
			{
				this.writeMessage(valOf(xml.getElementsByTagName("message")),6);
				this.pwinput.value="";
				this.uninput.focus();
			}
		} 
		else
		{
			this.writeMessage("Login problem, try again later",10);
			this.pwinput.value="";
			this.uninput.focus();				
		}	
	}
}

AuthClass.prototype._loggedIn=function(username,shop,transport,xml)
{
	this._loggedin=true;
	this._username=username;
	for(var i=0;i<this.__loginactions.length;i++)
	{
		if (typeof(this.__loginactions[i])=="function") this.__loginactions[i].call(null,username,shop,transport,xml);
	}
	this.refreshExpire();
	
}

AuthClass.prototype._loggedOut=function(shop,xml)
{
	this._loggedin=false;
	for(var i=0;i<this.__logoutactions.length;i++)
	{
		if (typeof(this.__logoutactions[i])=="function") this.__logoutactions[i].call(null,shop,xml);
	}	
	if (this._timeout) { clearTimeout(this._timeout); this._timeout=null; }
}

AuthClass.prototype.addLoginAction=function(handler)
{
	if (typeof(handler)=="function") this.__loginactions.push(handler)
		else this.__loginactions.push(function() { eval(handler) });
}

AuthClass.prototype.addLogoutAction=function(handler)
{
	if (typeof(handler)=="function") this.__logoutactions.push(handler)
		else this.__logoutactions.push(function() { eval(handler) });
}

AuthClass.prototype.isLogged=function()
{
	return this._loggedin;
}
AuthClass.prototype.setExpire=function(secs)
{
	this._expire=secs;
	if (this._loggedin) this.refreshExpire();
}


AuthClass.prototype.refreshExpire=function()
{
	if (this._timeout) { clearTimeout(this._timeout); this._timeout=null; }
	if (this._expire>0 && this._loggedin) 
	{
		var that=this;
		this._timeout=setTimeout(function() { that.sessionExpired(); },this._expire*1000);
	}
}

AuthClass.prototype.setExpMsg=function(msg)
{
	this._expmsg=msg;
}

AuthClass.prototype.writeMessage=function(msg,to)
{
	if (this.timer) { clearTimeout(this.timer); }
	var that=this;
	this.oldmessage=this.message.innerHTML;
	this.message.innerHTML=msg;
	if (to) {
		var that=this;
		this.timer=setTimeout(function() { 
			that.message.innerHTML=that.oldmessage;
			that.oldmessage=null;
			that.timer=null;
		},to*1000);
	}
}

AuthClass.prototype.sessionExpired=function()
{
	this.logout();
	this.writeMessage(this._expmsg);
	this.popup();
}

function valOf(e)
{
	if (e && e[0] && e[0].firstChild) return e[0].firstChild.nodeValue;
		else return "";
}
