
// =========================================================
// telepark.wiki Professional

// This Software is copyright (c) 2007 by telepark, 
// Inh. Patrick Thomas, www.telepark.de. 
// All rights reserved. 

// You may not modify, alter, reverse engineer or emulate 
// the functionality, or create derivative works of the 
// Software in parts or it's entirety without the prior
// written consent of telepark.
// =========================================================

var	ie=document.all ? true : false;

onerror=handleErr;

var isEval="";
var currFunc="";
var backTrace="";
var funcLine=0;
var ajaxCall="";
var executingInner=false;
var iDebugLine="none";
var headSrcDebug="";
var tOutLine="";
var funcLines=0;
var missingElement="";

function newGetElementById(id) {
	var el=document.getElementById(id);
	if (!el) missingElement=id;
	return el;
}

function setCurrFunc(n) {
	var maxBackTrace=50;
	funcLine=0;
	currFunc=n;
	if (funcLines==maxBackTrace) {
		var tmpBT=backTrace.split("\n");
		backTrace="";
		for (var i=tmpBT.length-10; i<tmpBT.length; i++) {
			if (tmpBT[i]) backTrace+=tmpBT[i]+"\n";
		}
	}
	backTrace+=n+"\n";
	funcLines++;
}

function setDebug(l) {
	funcLine=l;
}

function handleErr(msg,url,l)
{
var sendDebug=true;
if (url.indexOf("?")==-1) {
	if (tree) {
		url=url.replace(/#/g,"")+"?telepark.wiki="+tree.getSelectedItemId();
	}
}
var txt="There was an error on this page.\n\n";
txt+="Error: " + msg + "\n";
txt+="URL: " + url + "\n";
txt+="Line: " + l + "\n";
var error=txt;
error+="\nMissing element id: '"+missingElement+"'\n";
error+="\nIn function: ";
if (currFunc=="" && isEval=="" && ajaxCall=="") return false;
error+=currFunc+"\n";
if (funcLine!=0) {
	error+="\nAt debug point: ";
	error+=funcLine+"\n";
}
if (tOutLine!="") error+="\nActive timeout: \n"+tOutLine;
if (executingInner && IEhead[0]) {
	if (IEhead[0].text) {
		error+="\nIn inline script:\n";
		error+=headSrcDebug;
		error+="\nAt line: "+iDebugLine+"\n";
	}
}
if (msg.toLowerCase().indexOf("null")!=-1 && msg.toLowerCase().indexOf(" obje")!=-1 && missingElement=="") {
	sendDebug=false;
	error+="\n\nEval:\n";
	error+=isEval;
	error+="\n\n(HSD:"+headSrcDebug+"\n";
	error+="\nAt line: "+iDebugLine+"\n"+")";
	error+="\n\nAjax call to:\n";
	error+=ajaxCall;
	error+="\n\nBacktrace:\n";
	error+=backTrace;
	if (missingElement!="") sendError(error);
}
if (sendDebug) sendDebug=confirm(error+"\nClick OK to send bug info.");
if (sendDebug) {
	error+="\n\nEval:\n";
	error+=isEval;
	error+="\n\n(HSD:"+headSrcDebug+"\n";
	error+="\nAt line: "+iDebugLine+"\n"+")";
	error+="\n\nAjax call to:\n";
	error+=ajaxCall;
	error+="\n\nBacktrace:\n";
	error+=backTrace;
	sendError(error);
}
backTrace="";
return false;
}

function sendError(error) {
	var hp = new HTTPPost();
	var err = 'error=' + error;
	hp.postData('ajax/sendError.php', err, nofunction, true);
}

var persistant=false;

function working(pers) {
	var obj=document.getElementById('loading');
	if (obj!=null) obj.style.display="block";
	if (pers) persistant=true;
}

function notworking(pers) {
	if (pers) persistant=false;
	var obj=document.getElementById('loading');
	if ((obj!=null) && !persistant) obj.style.display="none";
}

// HTTPPost initialization
// ctype - content type
// encode - encoding
function HTTPPost(ctype, encode)	{
	if (ctype)
		this.ContentType = ctype;
	else
		this.ContentType = 'application/x-www-form-urlencoded';

	if (encode)
		this.Encoding = encode;
	else
		this.Encoding = 'UTF-8'; //ISO-8859-1

	this.httpRequest = null;
	
	// all browsers except IE6 and older
 	try {
 		this.httpRequest = new XMLHttpRequest();
 	}
 	// IE 6 and older
 	catch(e) {
 		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
 										"MSXML2.XMLHTTP.5.0",
 										"MSXML2.XMLHTTP.4.0",
 										"MSXML2.XMLHTTP.3.0",
 										"MSXML2.XMLHTTP",
 										"Microsoft.XMLHTTP");
 		for (var i = 0; i<xmlHttpVersions.length && !this.httpRequest; i++) {
 			try {
 				this.httpRequest = new ActiveXObject(xmlHttpVersions[i]);
 			}
 			catch(e) {
 			}
 		}
 	}
	
 	if (!this.httpRequest) {
 		alert("Unfortunately your browser doesn't support the required technology.");
 	}
 	
	this.postData = HTTPPost_postData;
	this.encodeValue = HTTPPost_encodeValue;
	this.showResponseText = HTTPPost_showResponseText;
	this.abort = HTTPPost_abort;
}


function HTTPPost_abort()	{
	this.httpRequest.abort();
}


// send AJAX POST request
// url - AJAX script url
// data - submited data
// callback - request processor
// async - false if sync method
function HTTPPost_postData(url, data, callback, async)	{
	ajaxCall=url;
	var httpRequest = this.httpRequest;
	if (httpRequest)	{
		if (async == null)
			async = true;
		if (url.indexOf("http")!=0) {
			var tmp="ajax/";
			if (url.indexOf(tmp)==0) tmp="";
			url=rootURL+tmp+url;
		}
		httpRequest.open('POST', url, async);
		if (async)	{
			httpRequest.onreadystatechange = function()	{
				if (httpRequest.readyState == 4) {
					notworking();
					try {
						callback(httpRequest);
					} catch(e) {}
	ajaxCall="";
				}
			}
		}
		httpRequest.setRequestHeader('Content-Type',this.ContentType);
		httpRequest.setRequestHeader('Accept-Encoding',this.Encoding);
		//httpRequest.setRequestHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
		//httpRequest.setRequestHeader('Cache-Control','no-store, no-cache, must-revalidate');
		//httpRequest.setRequestHeader('Pragma','no-cache');
		var requestBody = '';
		if (typeof data != 'string')	{
			for (var argName in data)	{
				var encodedArgName = this.encodeValue(argName);
				var value = data[argName];
				if (typeof value == 'object')	{
					for (var i = 0; i < value.length; i++) {
						requestBody += encodedArgName + '=' + this.encodeValue(value[i]) + '&';
					}
				}else	{
					requestBody += encodedArgName + '=' + this.encodeValue(value) + '&';
				}
			}
			requestBody = requestBody.substring(0, requestBody.length - 1);
		}else	{
			requestBody = data;
		}
		working();
		httpRequest.send(requestBody);
		if (!async)	{
			if (httpRequest.readyState == 4) {
				notworking();
					try {
						callback(httpRequest);
					} catch(e) {}
	ajaxCall="";
			}
		}
	}
}

// encode value
function HTTPPost_encodeValue(value)	{
	if (typeof encodeURIComponent != 'undefined')	{
		return encodeURIComponent(value);
	}else	{
		return escape(value);
	}
}

// alert response text
function HTTPPost_showResponseText() {
   alert(this.httpRequest.responseText);
}

// Usage examples
//postData('test.php', 'GOD=Kibo&devil=Xibo', showResponseText);
//postData('test.php', {'GOD': 'Kibo', 'devil': 'Xibo'},showResponseText);
//postData('test.php', {'riders': ['Lance', 'Jan', 'Erik'],'year': 2004}, showResponseText);


// get xml data
function getXMLData(p1)	{
	return (p1 && p1[0] && p1[0].firstChild)?p1[0].firstChild.data:'';
}

