var tdlib={'_VERSION':'0.9.8',
	'_vars':{ // SYSTEM VARIABLES v0.9.8.1
		// VARS
		'BASEPATH':'_tdlib/',
		'SCRIPTPATH':'_tdlib-php/',
		'USERPATH':'_userlib/',
		'LOGGING_SCRIPT':'tdlib.log.php',
		'LOG_DIR':'_logs/',
		'WRITE_LOG':false,
		'DISABLE_LOGGING':false,
		'DISABLE_CHECKS':false,
		'LOGGING_LEVEL':0,
		'ERROR_CODES':{'f':'General Failure','304':'Not Modified','403':'Forbidden','404':'Not Found','408':'Request Timeout','500':'Internal Server Error','501':'Not Implemented','503':'Service Unavailable'},
		'UF':'unknown function',
		'LOADING_FN':function(c){try{window.status=(c<100?'Please wait ... Loading... '+c+'% complete':'Complete.');}catch(er){}},
		// VARS (private)
		'_TRY_CATCH_OK':false,
		'_INIT_OK':false,
		'_USER_INI':null,
		'_SESSION':0,
		'_SYSLOG':'',
		'_LOG_BUFFER':'',
		'_LOG_LAST_WRITE':0,
		'_LOADED_LIBRARIES':{},
		'_XMLHTTP_REQUESTS':{},
		'_XMLHTTP_REQUEST_ID':0,
		'_STR_REPLACE_FN':false
	},
	'_sys':{} // SYSTEM FUNCTIONS CONTAINER v0.9.8.2
};

// some new Sting object VARS

String._SN='string,number';
String._SNB='string,number,boolean';
String._nSN='null,string,number';
String._UB='undefined,boolean';
String._UN='undefined,number';
String._US='undefined,string';
String._USN='undefined,string,number';
String._UnSN='undefined,null,string,number';
String._UnO='undefined,null,object';
String._UnS='undefined,null,string';

// FUNCTIONS (date and time)

String.getTime=function(){var t=new Date();return t.getTime();}
String.cTime=function(){var t=new Date();return(t.getHours()<10?'0':'')+t.getHours()+':'+(t.getMinutes()<10?'0':'')+t.getMinutes()+':'+(t.getSeconds()<10?'0':'')+t.getSeconds();}

// FUNCTIONS (logging and error handling)

String.prototype.logM=function(m){if(tdlib._vars.DISABLE_LOGGING||(tdlib._vars.LOGGING_LEVEL>0)) return tdlib._sys._writeLog();m=String.cTime()+' '+(String(this)==''?tdlib._vars.UF:String(this))+': '+m+"\n";tdlib._vars._SYSLOG+=m;if(tdlib.debug) tdlib.debug.writeMsg(m);tdlib._sys._writeLog(m);return null;}
String.prototype.logW=function(m,er){if(tdlib._vars.DISABLE_LOGGING||(tdlib._vars.LOGGING_LEVEL>1)) return tdlib._sys._writeLog();m=String.cTime()+' '+(String(this)==''?tdlib._vars.UF:String(this))+': WARNING: '+m+(er?' '+'logW'.errMsg(er):'')+"\n";tdlib._vars._SYSLOG+=m;if(tdlib.debug) tdlib.debug.writeMsg(m,1);tdlib._sys._writeLog(m);return null;}
String.prototype.logE=function(m,er){if(tdlib._vars.DISABLE_LOGGING) return tdlib._sys._writeLog();m=String.cTime()+' '+(String(this)==''?tdlib._vars.UF:String(this))+': ERROR: '+m+(er?" ERR: "+'logE'.errMsg(er):'')+"\n";tdlib._vars._SYSLOG+=m;if(tdlib.debug) tdlib.debug.writeMsg(m,2);tdlib._sys._writeLog(m);return null;}
String.prototype.logC=function(m,er){m=String.cTime()+' '+(String(this)==''?tdlib._vars.UF:String(this))+': CRITICAL: '+m+(er?" ERR: "+'logC'.errMsg(er):'')+"\n";tdlib._vars._SYSLOG+=m;if(tdlib.debug) tdlib.debug.writeMsg(m,3);tdlib._sys._writeLog(m);return null;}
String.prototype.xErr=function(er){return(String(this)==''?tdlib._vars.UF:String(this)).logC('unexpected error',er);}
String.prototype.rlogM=function(rf,m){return(String(this)==''?tdlib._vars.UF:String(this)).logM('in '+(String(rf)==''?tdlib._vars.UF:String(rf))+': '+m);}
String.prototype.rlogW=function(rf,m,er){return(String(this)==''?tdlib._vars.UF:String(this)).logW('in '+(String(rf)==''?tdlib._vars.UF:String(rf))+': '+m,er);}
String.prototype.rlogE=function(rf,m,er){return(String(this)==''?tdlib._vars.UF:String(this)).logE('in '+(String(rf)==''?tdlib._vars.UF:String(rf))+': '+m,er);}
String.prototype.rlogC=function(rf,m,er){return(String(this)==''?tdlib._vars.UF:String(this)).logC('in '+(String(rf)==''?tdlib._vars.UF:String(rf))+': '+m,er);}
String.prototype.rxErr=function(rf,er){return(String(this)==''?tdlib._vars.UF:String(this)).rlogC(rf,'unexpected error',er);}
String.prototype.errMsg=function(er){
	var _THIS='errMsg';try{//v1.5
	if(tdlib._vars.ERROR_CODES[er]) return'#'+er+': '+tdlib._vars.ERROR_CODES[er].htmlChars();
	if(typeof er=='object'){var s='';for(var i in er) s+='\n:.. "'+i+':" \n\n--err-start--\n\n'+String(this).errMsg(er[i])+'\n\n--err-end--\n\n';return s;}
	return(typeof er=='number'?'#'+er:'\n\n--err-start--\n\n'+String(er).htmlChars()+'\n\n--err-end--\n\n');
}catch(er){alert(_THIS+': in '+String(this)+': failed to process error message:\n\n'+er);}}

// FUNCTIONS (checking)

String.prototype.checkArgs=function(a,f,t){
	var _THIS='checkArgs';try{//v1.5
	tdlib._sys._writeLog();if(tdlib._vars.DISABLE_CHECKS) return true;var fn=(String(this)==''?tdlib._vars.UF:String(this));
	return((arguments.length<1)||(arguments.length>3)?_THIS.rlogE(this,'expecting 1 to 3 arguments, '+arguments.length+' given')
	:((typeof a!='number')||((typeof f!='number')&&(typeof f!='undefined'))||((typeof t!='number')&&(typeof t!='undefined'))?_THIS.logE('first argument must be "number", second and third arguments must be "number" or "undefined", "'+(typeof a)+'" "'+(typeof f)+'" "'+(typeof t)+'" given')
	:((a==0)&&!(f==0)?fn.logW('no arguments given')
	:((typeof f=='undefined')&&(typeof t=='undefined')?true
	:(typeof t=='undefined'?(a!=f?fn.logE('expecting '+f+' arguments, '+a+' given'):true)
	:((f==t)&&(a!=f)?fn.logE('expecting '+f+' arguments, '+a+' given')
	:((a<f)||(a>t)?fn.logE('expecting '+f+' to '+t+' arguments, '+a+' given')
	:true)))))));
}catch(er){return _THIS.rlogE(this,'failed to check arguments',er);}}

String.prototype.checkTypes=function(){
	var _THIS='checkTypes';try{//v0.9.3
	tdlib._sys._writeLog();if(tdlib._vars.DISABLE_CHECKS) return true;
	var a=arguments;if((typeof a!='object')||(a.length<2)||(a.length%2!=0)) _THIS.rlogE(this,'expecting 2 or more arguments (odd count), "'+a.length+'" given');
	var ere='';var erg='';var tps;var bad;var erex;var s;var v;
	for(var i=0;i<a.length/2;i++){
		s=a[2*i];if(typeof s!='string') return _THIS.rlogE(this,'bad argument #'+(2*i)+', expecting "string", "'+(typeof s)+'" given');v=a[2*i+1];
		if(s.indexOf(',')!=-1){tps=s.split(',');bad=true;erex='';
			for(var ii=0;ii<tps.length;ii++){if(((typeof v==tps[ii])||((tps[ii]=='null')&&(v===null)))&&((tps[ii]!='number')||isFinite(v))){bad=false;break;};erex+=(erex==''?'':' or ')+'"'+tps[ii]+'"';}
			if(bad){ere+=' ('+erex+')';erg+=' "'+(typeof v=='number'?v:typeof v)+'"';}
		}else if(((typeof v!=s)&&((s=='null')&&(v!==null)))||((s=='number')&&!isFinite(v))){ere+=' "'+s+'"';erg=' "'+(typeof v=='number'?v:typeof v)+'"';}
	}
	return(ere!=''?_THIS.rlogE(this,'expecting'+ere+';'+erg+' given'):true);
}catch(er){return _THIS.rlogE(this,'failed to check types',er);}}

// FUNCTIONS (file and library handling)

tdlib._sys.xmlHttp=null;

String.prototype.getFile=function(p2f,iff,post,dtta){
	var _THIS='getFile';try{//v1.5
	if(!_THIS.checkArgs(arguments.length,0,4)) return null;var uri=String(this);var h=tdlib._sys.xmlHttp();var reqid=tdlib._vars._XMLHTTP_REQUEST_ID++;
	if(typeof p2f=='function'){//v1.1.1
		tdlib._vars._XMLHTTP_REQUESTS[reqid]={'request':h,'onerror':(typeof iff=='function'?iff:null)};h.open((post>0?'POST':'GET'),uri,true);
		eval("h.onreadystatechange=function(){var _THIS='getFile: P2F';"
		+"if(h.readyState==2){_THIS.logM('async request \"#"+reqid+"\" file \"'+uri.htmlChars()+'\" loaded, waiting to be \"interactive\"');}"
		+"else if(h.readyState==3){_THIS.logM('async request \"#"+reqid+"\" file \"'+uri.htmlChars()+'\" interactive, waiting to be \"completed\"');}"
		+"else if(h.readyState==4){if(!(h.status&&((h.status<200)||(h.status>=300))&&(h.status!=304))){if(h.status==304) _THIS.logW('\"'+uri.htmlChars()+'\"',h.status);"
		+"_THIS.logM('async request \"#"+reqid+"\" file \"'+uri.htmlChars()+'\" completed, passing to function');"
		+"try{var r=h.responseText;delete tdlib._vars._XMLHTTP_REQUESTS["+reqid+"];return p2f(r);}catch(err){''.xErr(err);}}else{"
		+"if(typeof tdlib._vars._XMLHTTP_REQUESTS["+reqid+"]['onerror']=='function'){var f=tdlib._vars._XMLHTTP_REQUESTS["+reqid+"]['onerror'];try{var s=h.status;delete tdlib._vars._XMLHTTP_REQUESTS["+reqid+"];f(s);}catch(err){''.xErr(err);}}"
		+"_THIS.logE('async request \"#"+reqid+"\" failed to load \"'+uri.htmlChars()+'\"',h.status);delete tdlib._vars._XMLHTTP_REQUESTS["+reqid+"];}}"
		+'};');
		if(post>0){if(post=='2') h.setRequestHeader('Content-Type','application/x-www-form-urlencoded');h.send(dtta);}else h.send(null);_THIS.logM('async request "#'+reqid+'" is loading external file "'+uri.htmlChars()+'"');return reqid;
	}
	try{_THIS.logM('sync request "#'+reqid+'" is loading external file "'+uri.htmlChars()+'"');
		h.open((post>0?'POST':'GET'),uri,false);if(post>0){if(post=='2') h.setRequestHeader('Content-Type','application/x-www-form-urlencoded');h.send(dtta);}else h.send(null);
		if(!(h.status&&((h.status<200)||(h.status>=300))&&(h.status!=304))){if(h.status==304) _THIS.logW('"'+uri.htmlChars()+'"',h.status);_THIS.logM('sync request "#'+reqid+'" file "'+uri.htmlChars()+'" loading completed');return h.responseText;}
	}catch(err){_THIS.logE('sync request "#'+reqid+'" crashed when loading "'+uri.htmlChars()+'"',err);}
	if(typeof iff=='function'){try{_THIS.logM('executing "error function"');iff(h.status);_THIS.logM('"error function" execution success');}catch(err){''.xErr(err);}}
	return 1*h.status;
}catch(er){return _THIS.xErr(er);}}

String.prototype.getFilePD=function(dtta,p2f,iff){return String(this).getFile(p2f,iff,1,dtta);}
String.prototype.getFilePV=function(dtta,p2f,iff){return String(this).getFile(p2f,iff,2,dtta);}

String.prototype.abortRequest=function(r){
	var _THIS='abortRequest';try{//v1.6
	if(!_THIS.checkArgs(arguments.length,1)||!_THIS.checkTypes('string,number',r)) return null;
	if(typeof tdlib._vars._XMLHTTP_REQUESTS[r]=='undefined') return _THIS.logW('request "'+r+'" does not exist',404);
	tdlib._vars._XMLHTTP_REQUESTS[r]['request'].onreadystatechange=function(){};tdlib._vars._XMLHTTP_REQUESTS[r]['request'].abort();
}catch(er){return _THIS.rxErr(this,er);}}

String.prototype.loadTDlib=function(iff,u){
	var _THIS='loadTDlib';try{//v1.4 todo
	if(!_THIS.checkArgs(arguments.length,0,2)) return null;var lib=String(this);
	if(tdlib._vars._LOADED_LIBRARIES[lib]&&(tdlib._vars._LOADED_LIBRARIES[lib]=='loaded')) return true;
	tdlib._vars._LOADED_LIBRARIES[lib]='loading';_THIS.logM('loading library "'+lib+'"');
	var nm=((u?tdlib._vars.USERPATH:tdlib._vars.BASEPATH)+lib+'.js').getFile();
	if((nm===null)||(typeof nm=='number')){
		tdlib._vars._LOADED_LIBRARIES[lib]='bad';_THIS.logE('failed to load library "'+lib+'"',((nm===null)||isNaN(nm)?"f":nm));
		if(typeof iff=='function') try{iff(nm);}catch(err){_THIS.logE('"error function" crashed',err);};
		return ((nm===null)||isNaN(nm)?false:nm);
	}else{
		_THIS.logM('initializing library "'+lib+'"');
		try{eval(nm);
			if(typeof tdlib[lib]!='undefined'){
				if(typeof tdlib[lib]._constructor=='function'){tdlib[lib]._constructor();_THIS.logM('constructor for library "'+lib+'" found and initialized');}
				if(typeof tdlib[lib]['_ABBR']=='object'){lib.createAbbrs();_THIS.logM('abbreviations for library "'+lib+'" found and created');}
			}
			tdlib._vars._LOADED_LIBRARIES[lib]='loaded';_THIS.logM('library "'+lib+'" loaded');
		}catch(err){tdlib._vars._LOADED_LIBRARIES[lib]='bad';_THIS.logE('initializing library "'+lib+'" failed',err);}
		return true;
	}
}catch(er){return _THIS.xErr(er);}}

String.prototype.loadUserlib=function(iff){return String(this).loadTDlib(iff,true);}

String.prototype.requires=function(){
	var _THIS='requires';try{//v1.5
	if(!_THIS.checkArgs(arguments.length)) return true;var ok=true;_THIS.rlogM(this,'loading required libraries');
	for(var i=0;i<arguments.length;i++){if(typeof tdlib._vars.LOADING_FN=='function') tdlib._vars.LOADING_FN(Math.floor((arguments.length-i)/arguments.length*100));if(!String(arguments[i]).loadTDlib()) ok=false;}
	tdlib._vars.LOADING_FN(100);if(ok) _THIS.rlogM(this,'all required libraries ok');else _THIS.rlogC(this,'one or more library failed to load');return ok;
}catch(er){return _THIS.rxErr(this,er);}}

String.prototype.userRequires=function(){
	var _THIS='userRequires';try{//v1.5
	if(!_THIS.checkArgs(arguments.length)) return true;var ok=true;_THIS.rlogM(this,'loading required libraries');
	for(var i=0;i<arguments.length;i++){if(typeof tdlib._vars.LOADING_FN=='function') tdlib._vars.LOADING_FN(Math.floor((arguments.length-i)/arguments.length*100));if(!String(arguments[i]).loadUserlib()) ok=false;}
	tdlib._vars.LOADING_FN(100);if(ok) _THIS.rlogM(this,'all required libraries ok');else _THIS.rlogC(this,'one or more library failed to load');return ok;
}catch(er){return _THIS.rxErr(this,er);}}

String.prototype.createAbbrs=function(){
	var _THIS='createAbbrs';try{//v1.0.1
	var lib=String(this);for(var i in tdlib[lib]['_ABBR']){
		if((typeof i=='string')&&(typeof tdlib[lib]['_ABBR'][i]=='string')&&(typeof tdlib[lib][tdlib[lib]['_ABBR'][i]]=='function')) tdlib[lib][i]=tdlib[lib][tdlib[lib]['_ABBR'][i]];
		else _THIS.logW('cannot create "'+i+'", "'+tdlib[lib]['_ABBR'][i]+'" is not function of library "'+lib+'"');
	}
}catch(er){return _THIS.xErr(er);}}

// FUNCTIONS (common helper) v1.6

String.prototype.htmlChars=function(){var _THIS='htmlChars';try{return String(this).replace(/\&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/\"/g,'&quot;').replace(/\'/g,'&#039;').replace(/\\n/g,"<br />").replace(/\\r/g,"").replace(/\\t/g," ");}catch(er){return _THIS.xErr(er);}}

// FUNCTIONS (private)

tdlib._sys._findXMLhttp=function(){
	var _THIS='_findXMLhttp';//v1.0.1 // xmlHttp v1.2.1
	var xh=null;var er=null;try{xh=new XMLHttpRequest();}catch(e){er=e;}
	if(!xh){var xmlHttp=['Msxml2.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.4.0'];for(var i=0;i<xmlHttp.length;i++){try{xh=new ActiveXObject(xmlHttp[i]);}catch(e){er=e;};if(xh){eval("tdlib._sys.xmlHttp=function(silent){var _THIS='xmlHttp';try{var h=new ActiveXObject('"+xmlHttp[i]+"');if(!silent) _THIS.logM('new XMLHTTP created');return h;}catch(er){if(!silent) ''.xErr(er);return null;}};");_THIS.logM('found ActiveX object '+xmlHttp[i]);break;}}}
	else{tdlib._sys.xmlHttp=function(silent){var _THIS='xmlHttp';try{var h=new XMLHttpRequest();if(!silent) _THIS.logM('new XMLHTTP created');return h;}catch(er){if(!silent) _THIS.xErr(er);return null;}};_THIS.logM('found XMLHttpRequest()');}
	if(!xh){String.prototype.getFile=function(){return'getFile'.logC('not available',503);};String.prototype.loadTDlib=function(){return'loadTDlib'.logC('not available',503);};String.prototype.requires=function(){return'requires'.logC('not available',503);};;String.prototype.loadUserlib=function(){return'loadUserlib'.logC('not available',503);};String.prototype.userRequires=function(){return'userRequires'.logC('not available',503);};_THIS.logC('XMLHTTP not available, this is severe, TDlib will not work properly',er);return false;}
	return true;
}

tdlib._sys._findEncodeUri=function(){
	var _THIS='_findEncodeUri';//v1.0.1
	if(typeof encodeURI=='function') _THIS.logM('"encodeURI" function found');
	else{encodeURI=function(str){var s=escape(str);s=s.replace('%21','!');s=s.replace('%23','#');s=s.replace('%24','$');s=s.replace('%26','&');s=s.replace('%28','(');s=s.replace('%29',')');s=s.replace('%3D','=');s=s.replace('%3A',':');s=s.replace('%3B',';');s=s.replace('%3F','?');s=s.replace('%27',"'");return s;};_THIS.logW('"encodeURI" function NOT found, but this was fixed',501);}
	if(typeof encodeURIComponent=='function') _THIS.logM('"encodeURIComponent" function found');
	else{encodeURIComponent=function(str){var s=escape(str);s=s.replace('%21','!');s=s.replace('@','%40');s=s.replace('%28','(');s=s.replace('%29',')');s=s.replace('/','%2F');s=s.replace('+','%2B');s=s.replace('%27',"'");return s;};_THIS.logW('"encodeURIComponent" function NOT found, but this was fixed',501);}
}

tdlib._sys._checkReplaceFn=function(){var _f=function(a,b){return'<ok>'+b+'</ok>';};var tx=':test:';tdlib._vars._STR_REPLACE_FN=(tx.replace(RegExp(':([^:]*):'),_f)=='<ok>test</ok>'?true:false);}

tdlib._sys._writeLog=function(m){
	var _THIS='_writeLog';try{//v1.0.1
	if(!tdlib._vars._INIT_OK||(tdlib._sys.xmlHttp===null)||(tdlib._vars.WRITE_LOG!=true)) return null;
	var tnow=String.getTime();
	if(((tnow-tdlib._vars._LOG_LAST_WRITE)>=1000)&&(tdlib._vars._LOG_BUFFER!='')){
		var h=tdlib._sys.xmlHttp(true);h.open('POST',SCRIPTPATH+tdlib._vars.LOGGING_SCRIPT+'?l='+tdlib._vars.LOG_DIR+'&i='+tdlib._vars._SESSION,true);
		h.send(tdlib._vars._LOG_BUFFER);tdlib._vars._LOG_BUFFER='';tdlib._vars._LOG_LAST_WRITE=tnow;
	}else if(typeof m!='undefined') tdlib._vars._LOG_BUFFER+=m;
	return null;
}catch(er){return _THIS.xErr(er);}}

// CONSTRUCTOR

tdlib._sys._init=function(){
	var _THIS='_init';try{//v1.1.1
	_THIS.logM('initializing TDlib');
	// create session id
	tdlib._vars._SESSION=String.getTime();tdlib._vars._LOG_LAST_WRITE=tdlib._vars._SESSION;
	// initialize TDlib
	tdlib._sys._findEncodeUri();tdlib._sys._checkReplaceFn();var ok=tdlib._sys._findXMLhttp();
	if(ok&&((typeof tdlibNoUserIni=='undefined')||!(tdlibNoUserIni>0))){var nm='tdlibini.js'.getFile();if((nm!==null)&&(typeof nm!='number')){try{eval('tdlib._vars._USER_INI=function(){'+nm+';};');_THIS.logM('user ini file found and stored');}catch(er){_THIS.logE('user ini file found, but failed to store',er);alert(tdlib._vars._SYSLOG);}}else _THIS.logW('could not find user ini file',404);}
	_THIS.logM('end of TDlib initialization');return ok;
}catch(er){return _THIS.xErr(er);}}

// ONLOAD activation

window.onload=function(){
	var _THIS='_init: onload';//v1.5.2
	try{tdlib._vars._TRY_CATCH_OK=true;}catch(er){};if(!tdlib._vars._TRY_CATCH_OK) return'try/catch check'.logC('required functions not available, application will not run, sorry',501);if(!(tdlib._vars._INIT_OK=tdlib._sys._init())) return alert(tdlib._vars._SYSLOG);
	try{_THIS.logM('navigator.userAgent: '+(navigator&&navigator.userAgent?navigator.userAgent:'unknown'));_THIS.logM('navigator.appVersion: '+(navigator&&navigator.appVersion?navigator.appVersion:'unknown'));}catch(er){}
	if((typeof tdlibIWantDebug!='undefined')&&(tdlibIWantDebug>0)){tdlib._vars.LOGGING_LEVEL=(tdlibIWantDebug==2?0:1);'debug'.loadTDlib();}else tdlib._vars.LOGGING_LEVEL=2;
	if(typeof tdlibMyUserPath=='string') tdlib._vars.USERPATH=tdlibMyUserPath;
	if(typeof tdlibMyUserIni=='string'){
		var nm=tdlibMyUserIni.getFile();
		if((nm!==null)&&(typeof nm!='number')){
			try{eval('nm=function(){'+nm+';}');nm();_THIS.logM('user ini "'+tdlibMyUserIni+'" processed');}
			catch(er){_THIS.logE('user ini "'+tdlibMyUserIni+'" failed to process',er);}
		}else{_THIS.logW('could not find user ini file: "'+tdlibMyUserIni+'"',404);}
	}else if(typeof tdlib._vars._USER_INI=='function'){
		try{tdlib._vars._USER_INI();_THIS.logM('user ini processed');tdlib._vars._USER_INI=null;}
		catch(er){_THIS.logE('user ini failed to process',er);}
	}
}