function PassportAuthentication() {
	var self = this;
	this.passportServer = '';
	this.passportUrl = '';
	this.onNobody = '';
	this.onLogin = '';
	this.onInvalidLogin = '';
	this.onLogout = '';
	this.onRegister = '';
	this.onFreeMail = '';
	this.onFreeUser = '';
	this.onMailExists = '';
	this.onUserExists = '';
	this.onWrongCode = '';
	this.onRightCode = '';
	this.onChangeMail = '';
	this.onChangeUser = '';
	this.onInvalidMail = '';
	this.onInvalidUser = '';
	this.onAuthKey = '';
	this.passport_mail = '';
	this.onCookiesInfo = '';
	this.onInit = '';
	this.pKey = false;
	this.pUser = false;
	this.pRemember = false;
	var m;
	var p;
	var pEvent;
	var param;
	var str_data;
	var attr;
	var path;
	var str;
	var rememberme;
	var subject;
	var k;
	var u;
	var r;
	var c;
	var path;
	var domain;

	this.init = function() {
		var c = self.getCookie('PHPSESSID');
		if (!c) {
			alert('To work passport properly, you must enable your cookies from the browser.');
			return false;
		}
		self.passportServer = self.getCookie('passportServer');
		self.passportPath = self.getCookie('passportPath');
		self.passportUrl = self.getCookie('passportUrl');
		self.onInit = self.getCookie('passport_onInit');
		self.onLogin = self.getCookie('passport_onLogin');
		self.onInvalidLogin = self.getCookie('passport_onInvalidLogin');
		self.onRegister = self.getCookie('passport_onRegister');
		self.onLogout = self.getCookie('passport_onLogout');
		self.onMailExists = self.getCookie('passport_mailExists');
		self.onUserExists = self.getCookie('passport_userExists');
		self.onFreeMail = self.getCookie('passport_freeMail');
		self.onFreeUser = self.getCookie('passport_freeUser');
		self.onWrongCode = self.getCookie('passport_wrongCode');
		self.onRightCode = self.getCookie('passport_rightCode');
		self.onInvalidMail = self.getCookie('passport_invalidMail');
		self.onAuthKey = self.getCookie('passport_onAuthKey');
		self.onInvalidUser = self.getCookie('passport_invalidUser');
		self.onChangeMail = self.getCookie('passport_onChangeMail');
		self.onChangeUser = self.getCookie('passport_onChangeUser');
		self.onInvalidPassword = self.getCookie('passport_invalidPassword');
		self.onCookiesInfo = self.getCookie('passport_onCookiesInfo');
		self.passport_mail = self.getCookie('passport_mail');
		self.pUser = self.getCookie('passport_userforlogin');
		self.pRemember = self.getCookie('passport_rememberme');
		var inited = self.getCookie('passport_inited');
		if (!self.getUrlParam('authKey') || (self.getCookie('passport_loginViaAuth') && self.getCookie('passport_loginViaAuth') == self.getUrlParam('authKey'))) {
			if (inited == 0) {
				self.setCookie('passport_inited','1');
				self.whoami();
			}
			else {
				if (self.onInit) {
					eval(self.onInit);
				}
			}
		}
		else {
			self.setCookie('passport_loginViaAuth',self.getUrlParam('authKey'));
			self.getKey('whoami',self.getUrlParam('authKey'),'nobody',0,1);
		}
		self.makeComunication();
	}
	this.getUrlParam = function(param) {
		param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+param+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if(results == null) {  return ""; }
		else {
			return results[1];
		}
	}
	this.setCookie = function(name,param,attr,path,domain) {
		var today = new Date();
		today.setTime(today.getTime());
		if (attr) { attr = attr * 1000 * 60 * 60 * 24; }
		var expires_date = new Date(today.getTime()+(attr));
		document.cookie = name + "=" +escape(param) +
		((attr) ? ";expires=" + expires_date.toGMTString() : "") +
		((path) ? ";path=" + path : "" ) +
		((domain) ? ";domain=" + domain : "" );
	}
	this.getCookie = function(param) {
		var start = document.cookie.indexOf( param + "=" );
		var len = start + param.length + 1;
		if (!start && param != document.cookie.substring(0,param.length)) { return null; }
		if (start == -1) { return null; }
		var end = document.cookie.indexOf(";",len);
		if (end == -1) { end = document.cookie.length; }
		var str = document.cookie.substring(len,end);
		if (str) { str = str_replace('+','%20',str); return unescape(str); }
		return null;
	}
	this.deleteCookie = function(param, path, attr) {
		if (self.getCookie(param)) {
			document.cookie = param+"=" +((path) ? ";path=" + path : "")+(( attr ) ? ";domain=" + attr : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}
	this.makeComunication = function() {
		var pactions = self.getCookie('passportActions');
		self.deleteCookie('passportActions');
		if (pactions && pactions != null) { eval(pactions); }
	}
	this.whoami = function() {
		if (typeof(rAjax) != 'object') { return false; }
		rAjax.makeRequest(self.passportServer+'/requests/clientside/whoami.php','function=pAuth.getKey("whoami","%k","%u","%r","%c")');
		return true;
	}
	this.login = function(u,param,attr) {
		if (!u || !param) { return false; }
		if (!attr) { attr = ''; }
		rAjax.makeRequest(self.passportServer+'/requests/clientside/login.php','username='+urlencode(u)+'&password='+md5(param)+'&rememberme='+attr+'&function=pAuth.getKey("login","%k","%u","%r")');
		return true;
	}
	this.logout = function() {
		rAjax.makeRequest(self.passportServer+'/requests/clientside/logout.php','function=pAuth.getKey("logout","nobody","%u","%r")');
		return true;
	}
	this.processForbiddenPassword = function(param,attr) {
		if (!attr || !param) { return false; }
		if (!attr) { attr = ''; }
		rAjax.makeRequest(self.passportServer+'/requests/clientside/forbiddenPassword.php','mail='+urlencode(param)+'&function='+attr);
		return true;
	}
	this.is_logged = function() {
		var c = self.getCookie('passport_mail');
		if (c == null || c == undefined) { return false; }
		return true;
	}
	this.get_mail = function() {
		return self.getCookie('passport_mail');
	}
	this.get_username = function() {
		return urldecode(self.getCookie('passport_username'));
	}
	this.is_enabledCookies = function() {
		var c = self.getCookie('passport_enabledCookies');
		if (c == 1) { return true; }
		return false;
	}
	this.get_userforlogin = function() {
		return self.getCookie('passport_userforlogin');
	}
	this.get_rememberme = function() {
		var r = self.getCookie('passport_rememberme');
		if (r == 1) { return true; }
		return false;
	}
	this.write_cookies = function(param,c) {
		self.enabledCookies = c;
		self.setCookie('passport_enabledCookies',c);
		if (param == 'whoami') {
			if (self.onInit) {
				eval(self.onInit);
			}
			return true;
		}
		else {
			if (self.onCookiesInfo) {
				eval(self.onCookiesInfo);
			}
		}
	}
	this.tryCookies = function() {
		rAjax.makeRequest(self.passportServer+'/requests/clientside/checkforcookies.php','function=pAuth.write_cookies("try","%c");');
	}
	this.getAuthKey = function(param) {
		if (!self.is_enabledCookies() || !self.is_logged) { return false; }
		if (!param) {
			rAjax.makeRequest(self.passportServer+'/requests/clientside/getnewkey.php','function=pAuth.receiveAuthKey("request","%k");');
		}
		else {
			rAjax.makeRequest(self.passportServer+'/requests/clientside/getnewkey.php','function='+param);
		}
		return true;
	}
	this.receiveAuthKey = function(param,k) {
		if (!k || k == 'nobody') { k = false; }
		self.pKey = k;
		if (self.onAuthKey) {
			eval(self.onAuthKey);
		}
	}
	this.getReceivedKey = function() {
		return self.pKey;
	}
	this.getKey = function(pEvent,k,u,r,c) {
		if (!pEvent) { pEvent = 'whoami'; }
		if (k == 'nobody') { k = false; }
		if (c) {
			self.setCookie('passport_enabledCookies',c);
		}
		self.setCookie('passport_rememberme',r);
		if (u != '' && u != 'nobody') { self.setCookie('passport_userforlogin',u); self.pUser = u; }
		else { self.pUser = self.getCookie('passport_userforlogin'); self.setCookie('passport_userforlogin',''); }
		self.pRemember = r;
		self.pKey = k;
		if (!k && pEvent == 'login') {
			if (self.onInvalidLogin) {
				eval(self.onInvalidLogin);
			}
			return true;
		}
		if (!k && pEvent == 'whoami') {
			if (c == 0) {
				rAjax.makeRequest(self.passportServer+'/requests/clientside/checkforcookies.php','function=pAuth.write_cookies("whoami","%c");');
				return false;
			}
			if (self.onInit) {
				eval(self.onInit);
			}
			return true;
		}
		if (!k && pEvent == 'logout') {
			lAjax.makeRequest(self.passportUrl+'ajax/logout.php','GET','',true);
			return true;
		}
		if (k && (pEvent == 'login' || pEvent == 'whoami' || pEvent == 'register')) {
			self.passport_mail = u;
			if (pEvent == 'register') { var act = 'onRegister'; self.deleteCookie('passport_cookieCapture',''); }
			if (pEvent == 'login') { var act = 'onLogin'; }
			if (pEvent == 'whoami') { var act = 'onInit'; }
			lAjax.makeRequest(self.passportUrl+'ajax/getKey.php?act='+act+'&key='+k,'GET','',true);
			return true;
		}
	}
	this.checkMail = function(param,p) {
		if (typeof(p) == 'string') {
			if (self.is_validMail(param)) {
				if (!param) { return false; }
				rAjax.makeRequest(self.passportServer+'/requests/clientside/checkmail.php','mail='+param+'&function='+p);
				return true;
			}
			else {
				var str = str_replace('%r','invalid',p);
				try { eval(str); }
				catch(e) { return false; }
				return false;
			}
		}
		else {
			if (self.is_validMail(param)) {
				if (!param) { return false; }
				rAjax.makeRequest(self.passportServer+'/requests/clientside/checkmail.php','mail='+param+'&function=pAuth.checkMailResponse("%r")');
				return true;
			}
			else {
				if (self.onInvalidMail) {
					eval(self.onInvalidMail);
				}
				return false;
			}
		}
	}
	this.checkMailResponse = function(param) {
		if (param == 1) {
			if (self.onMailExists) {
				eval(self.onMailExists);
			}
			return true;
		}
		else {
			if (self.onFreeMail) {
				eval(self.onFreeMail);
			}
			return true;
		}
	}
	this.is_validMail = function(param) {
		if (typeof(param) != 'string' || param.length < 10 || param.indexOf('@') < 3 || param.indexOf('.') == -1) { return false; }
		if ((param.length - param.indexOf('@')) < 3) { return false; }
		return true;
	}
	this.is_validUser = function(param) {
		if (typeof(param) != 'string' || param.length < 4 || param.indexOf('@') != -1) { return false; }
		return true;
	}
	this.is_validPassword = function(param) {
		if (typeof(param) == 'string' && param.length > 3) { return true; }
		return false;
	}
	this.checkUser = function(param,p) {
		if (typeof(p) == 'string') {
			if (!self.is_validUser(param)) {
				var str = str_replace('%r','invalid',p);
				try { eval(str); }
				catch(e) { return false; }
				return false;
			}
			else {
				rAjax.makeRequest(self.passportServer+'/requests/clientside/checkuser.php','username='+param+'&function='+p);
				return true;
			}
		}
		else {
			if (!self.is_validUser(param)) {
				if (self.onInvalidUser) {
					eval(self.onInvalidUser);
				}
				return false;
			}
			else {
				rAjax.makeRequest(self.passportServer+'/requests/clientside/checkuser.php','username='+param+'&function=pAuth.checkUserResponse("%r")');
				return true;
			}
		}
	}
	this.checkUserResponse = function(param) {
		if (param == 1) {
			if (self.onUserExists) {
				eval(self.onUserExists);
			}
			return true;
		}
		else {
			if (self.onFreeUser) {
				eval(self.onFreeUser);
			}
			return true;
		}
	}
	this.cookieCapture = function(param,attr) {
		if (!param && !attr) {
			return self.getCookie('passport_cookieCapture');
		}
		else {
			self.setCookie('passport_cookieCapture',attr);
			self.getCapture(param);
		}
	}
	this.getCapture = function(param,attr) {
		if (!document.getElementById(param)) { return false; }
		if (self.is_enabledCookies()) {
			document.getElementById(param).src = self.passportServer+'/requests/clientside/getcapture.php?rand='+Math.random(1000,20000);
		}
		else {
			if (!self.cookieCapture()) {
				rAjax.makeRequest(self.passportServer+'/requests/clientside/setcapture.php','function=pAuth.cookieCapture("'+param+'","%c")');
			}
			else {
				if (!attr) { attr = 0; }
				else { attr = 1; }
				document.getElementById(param).src = self.passportServer+'/requests/clientside/getcapture.php?rand='+Math.random(1000,20000)+'&newcap='+attr+'&capture_id='+self.cookieCapture();
			}
		}
	}
	this.checkCapture = function(param) {
		if (!param) { return false; }
		if (!self.cookieCapture()) {
			rAjax.makeRequest(self.passportServer+'/requests/clientside/checkcapture.php','code='+md5(param)+'&function=pAuth.checkCaptureResponse("%r")');
		}
		else {
			rAjax.makeRequest(self.passportServer+'/requests/clientside/checkcapture.php','code='+md5(param)+'&capture_id='+self.cookieCapture()+'&function=pAuth.checkCaptureResponse("%r")');
		}
		return true;
	}
	this.checkCaptureResponse = function(param) {
		if (param == 1) {
			if (self.onRightCode) {
				eval(self.onRightCode);
			}
			return true;
		}
		else {
			if (self.onWrongCode) {
				eval(self.onWrongCode);
			}
			return true;
		}
	}
	this.register = function(m,u,p,attr,param) {
		if (!self.is_validMail(m)) {
			if (self.onInvalidMail) {
				eval(self.onInvalidMail);
			}
			return false;
		}
		if (typeof(u) != 'string' || u.length < 4 || u.indexOf('@') != -1) {
			if (self.onInvalidUser) {
				eval(self.onInvalidUser);
			}
			return false;
		}
		if (typeof(p) != 'string' || !self.is_validPassword(p)) {
			if (self.onInvalidPassword) {
				eval(self.onInvalidPassword);
			}
			return false;
		}
		var str = 'function=pAuth.getKey("register","%k","%u","0")';
		if (self.cookieCapture()) {
			str += '&capture_id='+self.cookieCapture();
		}
		str += '&mail='+urlencode(m);
		str += '&username='+urlencode(u);
		str += '&password='+md5(p);
		str += '&code='+attr;
		str += '&data='+urlencode(param);
		rAjax.makeRequest(self.passportServer+'/requests/clientside/register.php',str);
	}
	this.changeMail = function(param,attr) {
		if (!param || param == self.get_mail() || (!self.is_enabledCookies() && !attr) || !self.is_validMail(param)) { return false; }
		var str = '&newmail='+param;
		str += '&mail='+self.get_mail();
		str += '&function=pAuth.receiveNewMail("%m");';
		if (attr) {
			str += '&password='+md5(attr);
		}
		rAjax.makeRequest(self.passportServer+'/requests/clientside/changemail.php',str);
		return true;
	}
	this.receiveNewMail = function(param) {
		if (!param || param == self.get_mail()) { return false; }
		else {
			lAjax.makeRequest(self.passportUrl+'ajax/changeMail.php?mail='+param,'GET','',true);
		}
	}
	this.changeUser = function(param,attr) {
		if (!param || param == self.get_username() || (!self.is_enabledCookies() && !attr) || !self.is_validUser(param)) { return false; }
		var str = '&newuser='+param;
		str += '&mail='+self.get_mail();
		str += '&function=pAuth.receiveNewUser("%u");';
		if (attr) {
			str += '&password='+md5(attr);
		}
		rAjax.makeRequest(self.passportServer+'/requests/clientside/changeuser.php',str);
		return true;
	}
	this.receiveNewUser = function(param) {
		if (!param || param == self.get_username()) { return false; }
		else {
			lAjax.makeRequest(self.passportUrl+'ajax/changeUser.php?username='+urlencode(param),'GET','',true);
		}
	}
	this.changePassword = function(param,attr,p) {
		if (!param || !attr || !self.is_validPassword(param) || (!self.is_enabledCookies() && !p)) { return false; }
		var str='&newpassword='+md5(param);
		str += '&mail='+self.get_mail();
		str += '&function='+attr;
		if (p) {
			str += '&password='+md5(p);
		}
		rAjax.makeRequest(self.passportServer+'/requests/clientside/changepassword.php',str);
	}
	this.getPrivateData = function(param,attr) {
		if ((!self.is_enabledCookies() && !attr) || !param) { return false; }
		var str = '&mail='+self.get_mail();
		if (attr) {
			str += '&password='+md5(attr);
		}
		str += '&function='+param;
		rAjax.makeRequest(self.passportServer+'/requests/clientside/getPrivateData.php',str);
	}
	this.updatePrivateData = function(str_data,param,attr) {
		if ((!self.is_enabledCookies() && !attr) || !param) { return false; }
		var str = '&mail='+self.get_mail();
		if (attr) {
			str += '&password='+md5(attr);
		}
		str += '&function=pAuth.receivePrivateData("%d");'+param;
		str += '&data='+urlencode(str_data);
		rAjax.makeRequest(self.passportServer+'/requests/clientside/updatePrivateData.php',str);
	}
	this.receivePrivateData = function(param) {
		if (!param) { return false; }
		lAjax.makeRequest(self.passportUrl+'ajax/receivePrivateData.php','POST','data='+urlencode(param),true);
	}
	this.evalActions = function(param) {
		if (!param) { return false; }
		if (eval('self.'+param)) {
			var str = eval('self.'+param);
			eval(str);
		}
	}
	this.getDomain = function() {
		if (window.location.href.indexOf('http://') == 0) {
			return 'http://'+document.domain;
		}
		if (window.location.href.indexOf('https://') == 0) {
			return 'https://'+document.domain;
		}
		return document.domain;
	}
}
var pAuth = new PassportAuthentication;
pAuth.init();
