
// =========================================================
// 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 waiting=false;

function working() {
	waiting=true;
}

function notworking() {
	waiting=false;
}

// 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)	{
	var httpRequest = this.httpRequest;
	if (httpRequest)	{
		if (async == null)
			async = true;
		httpRequest.open('GET', url, async);
		if (async)	{
			httpRequest.onreadystatechange = function()	{
				if (httpRequest.readyState == 4) {
					notworking();
					callback(httpRequest);
				}
			}
		}
		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();
				callback(httpRequest);
			}
		}
	}
}

// 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:'';
}

