/* Revision: $Id: pimp.js 4168 2009-02-14 12:04:25Z uri $ */
if (!String.prototype.trim) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g,"");
	}
}

function pimpAjaxNewXmlHttp() {
	xmlHttp = null;
	//Creating object of XMLHTTP in IE
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc) {
			xmlHttp = null;
		}
	}

	//Creating object of XMLHTTP in Mozilla and Safari
	if(!xmlHttp && typeof XMLHttpRequest != "undefined") {
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}

function pimpEncodeQueryArray(parameters) {
	var result = '';
	for (parameter in parameters) {
		if (result.length > 0) {
			result += "&";
		}
		result += encodeURIComponent(parameter) + '=' + encodeURIComponent(parameters[parameter]);
	}
	return result;
}

function pimpAjax(url, callback, parameters) {
	xmlHttp = pimpAjaxNewXmlHttp();

	if (xmlHttp) {
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status != 200) {
					alert("There was a problem retrieving data from the server. Please try again.");
					return;
				}

				if (callback != null) {
					if (!xmlHttp.responseXML) {
						alert("The server did not return an XML response. Please try again.");
						return;
					}

					callback(xmlHttp.responseXML.documentElement);
				}
			}
		}

		if (typeof(parameters) != "undefined") {
			xmlHttp.open("POST", url, true);
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlHttp.send(pimpEncodeQueryArray(parameters));
		} else {
			xmlHttp.open("GET", url, true);
			xmlHttp.send(null);
		}
	}
}

function pimpLoadXmlDoc(url) {
	var xmlDoc;
	if (window.ActiveXObject) {
		// code for IE
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.validateOnParse = false;
	} else if (document.implementation && document.implementation.createDocument) {
		// code for Mozilla, Firefox, Opera, etc.
		xmlDoc = document.implementation.createDocument("", "", null);
	} else {
		return false;
	}

	xmlDoc.async = false;
	if (!xmlDoc.load(url)) {
		alert("XML Parse Error: " + xmlDoc.parseError.reason + " (line: " + xmlDoc.parseError.line + ").");
		return false;
	}

	return xmlDoc;
}

function pimpXslTransform(xmlDoc, xsltDoc) {
	// code for IE
	if (window.ActiveXObject) {
		var result = document.createElement("DIV");
		result.innerHTML = xmlDoc.transformNode(xsltDoc);
		return result;
	}

	// code for Mozilla, Firefox, Opera, etc.
	if (document.implementation && document.implementation.createDocument) {
		xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xsltDoc);
		return resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
	}
}

function pimpXmlInnerText(xmlNode) {
	return (xmlNode.textContent || xmlNode.innerText || xmlNode.text);
}

function pimpAddLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function pimpShowHideElement(elementId, defaultDisplay) {
	var elementObject = document.getElementById(elementId);

	if (!defaultDisplay) {
		defaultDisplay = "block";
	}

	if (elementObject.style.display != 'none') {
		elementObject.style.display = 'none';
	} else {
		elementObject.style.display = defaultDisplay;
	}
}
