/*
	xmlHttpRequest
*/

/*public xmlHttpRequest*/ function xmlHttpRequest (/*string*/ method, /*mixed*/ requestData, /*boolean*/ async, /*string*/ succesMethod, /*string*/ failureMethod) {

	this.$method = method.toUpperCase();
	this.$requestData = requestData;
	this.$async = async;
	this.$successMethod = succesMethod;
	this.$failureMethod = failureMethod;

	this.$useXmlHttp = (typeof XMLHttpRequest != "undefined");
	this.$useActiveX = (typeof ActiveXObject != "undefined");
	this.$XMLHTTP_VER = "undefined";

	this.createXMLHttpRequest = function () {
		var XMLHTTP_VERS = ["MSXML2.XmlHttp.6.0","MSXML2.XmlHttp.3.0"];

		if (this.$useXmlHttp) {
			return new XMLHttpRequest();
		} else if (this.$useActiveX) {
			if (this.$XMLHTTP_VER == "undefined") {
				for (var i=0; i < XMLHTTP_VERS.length; i++) {
					try {
						new ActiveXObject(XMLHTTP_VERS[i]);
						this.$XMLHTTP_VER = XMLHTTP_VERS[i];
						break;
					} catch (oError) {
					}
				}
			}
			if (this.$XMLHTTP_VER != "undefined") {
				return new ActiveXObject(this.$XMLHTTP_VER);
			} else {
				throw new Error("Could not create XML HTTP Request.");
			}
		} else {
			throw new Error("Browser doesn't support an XML HTTP Request.");
		}
	}

	this.$xmlhttp = this.createXMLHttpRequest();

}

/*private string*/ xmlHttpRequest.prototype.encodeURL = function (/*string*/ url) {

	var $url = url;
	$url = $url.split('?');
	if ($url.length > 1) {
		var params = $url[1].split('&');
		for (var i=0;i<params.length;i++) {
			var keyvalue = params[i].split('=')
			if (keyvalue.length > 1) {
				keyvalue[1] = encodeURIComponent(keyvalue[1]);
				params[i] = keyvalue.join('=');
			}
		}
		$url[1] = params.join('&');
		return $url.join('?');
	}else return $url.toString()
}

/*private void*/ xmlHttpRequest.prototype.prepare = function () {

	if ((this.$method == "GET") && (typeof this.$requestData == "string")) {
		this.$xmlhttp.open(this.$method,this.encodeURL(this.$requestData),this.$async);
	} else if ((this.$method == "POST") && ((this.$requestData.nodeName) && (this.$requestData.nodeName.toUpperCase() == "FORM"))){
		this.$xmlhttp.open(this.$method,this.$requestData.getAttribute('action'),this.$async);
		this.$xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	}

	var _this = this;
	this.$xmlhttp.onreadystatechange = function () { 
		if (_this.$xmlhttp.readyState==4) {
			if (_this.$xmlhttp.status==200) {
/*				if (LOADER) {
					LOADER.hide();
				}
*/
				var methodToExecute = eval(_this.$successMethod);
				methodToExecute(_this.$xmlhttp);
			} else {
/*				if (LOADER) {
					LOADER.hide();
				}
*/
				var methodToExecute = eval(_this.$failureMethod);
				methodToExecute(_this.$xmlhttp);
			}
		}
	}
}

/*public void*/ xmlHttpRequest.prototype.send = function () {

/*	if (LOADER != "undefined") {
		LOADER.show();
	} else {
		LOADER = new Loader();
		LOADER.show();
	}
*/

	this.prepare();
	var requestBody = null;
	if (this.$method == "POST") {
		requestBody = FormUtil.serializeForm(this.$requestData,true);
	}
	this.$xmlhttp.send(requestBody);
}

function defaultFailure() {

}/*
	FormUtil
*/

var FormUtil = new Object();

/*public string*/ FormUtil.serializeForm = function (/*HTMLFormElement*/ oForm, /*boolean*/ urlEncoded) {

	var result = new Array();
	for(var i=0;i<oForm.elements.length;i++) {
		var serializedField = FormUtil.serializeField(oForm.elements[i],(urlEncoded)? true:false)
		if (serializedField != "") {
			result.push(serializedField)
		}
	}
	return result.join('&')
}

/*private string*/ FormUtil.serializeField = function (/*HTMLFieldElement*/ oField, /*boolean*/ urlEncoded) {

	if ((oField.nodeName.toUpperCase() == "INPUT") || (oField.nodeName.toUpperCase() == "TEXTAREA")) {
		return FormUtil.serializeTEXTnCHOICEField(oField, (urlEncoded)? true:false)
	} else if (oField.nodeName.toUpperCase() == "SELECT") {
		return FormUtil.serializeSELECTField(oField, (urlEncoded)? true:false)
	}else return ""
}

/*private string*/ FormUtil.serializeTEXTnCHOICEField = function (/*HTMLFieldElement*/ oField, /*boolean*/ urlEncoded) {

	if (((oField.checked == null) || ((oField.getAttribute('type') != null) && ((oField.getAttribute('type').toUpperCase() == "TEXT") || (oField.getAttribute('type').toUpperCase() == "HIDDEN")))) || (oField.checked)) {
		var result = oField.getAttribute('name') + "=" + ((urlEncoded)? encodeURIComponent(oField.value) : oField.value).toString();
		return result
	} else return ""
}

/*private string*/ FormUtil.serializeSELECTField = function (/*HTMLFieldElement*/ oField, /*boolean*/ urlEncoded) {

	if (oField.getAttribute('multiple') != null) {
		var result = new Array();
		for(var i=0;i<oField.options.length;i++) {
			if(oField.options[i].selected) {
				var tmp = oField.getAttribute('name') + "=" + ((urlEncoded)? encodeURIComponent(oField.options[i].value) : oField.options[i].value).toString();
				result.push(tmp)
			}
		} return result.join('&')
	} else {
		if (oField.selectedIndex > -1) {
			var result = oField.getAttribute('name') + "=" + ((urlEncoded)? encodeURIComponent(oField.options[oField.selectedIndex].value) : oField.options[oField.selectedIndex].value).toString();
			return result
		} else return ""
	}
}/*
	Loader
*/
var LOADER = "undefined";

/*public Loader*/ function Loader() {

	this.getScrollTop = function () {
		if(window.pageYOffset) {
			return window.pageYOffset
		} else if ((document.compatMode.toUpperCase() != "BACKCOMPAT") && (document.documentElement.scrollTop)) {
					return document.documentElement.scrollTop
				} else if (document.body.scrollTop) {
						return document.body.scrollTop
						} else return 0
	}
	this.$element = document.createElement('DIV');
	this.$element.innerHTML = '<table cellspacing=\"0\" cellpadding=\"0"\><tr><td><img src=\"'+ baseurl +'/_images/loader.gif\" border=\"0\" /></td><td style=\"vartical-align: middle; font-family: Tahoma; font-size: 10px; font-weight: bold;\">&nbsp;&nbsp;Przesyłanie danych...</td></tr></table>';
	this.$element.style.position = "absolute";
	this.$element.style.right = "0px";
	this.$element.style.top = this.getScrollTop() + "px";
	this.$element.style.width = "150px";
	this.$element.style.height = "20px";
	this.$element.style.border = "1px solid #aaa";
	this.$element.style.borderTopWidth = "0px";
	this.$element.style.borderRightWidth = "0px";
	this.$element.style.background = "#fff";
	this.$element.style.display = "none";

	document.body.appendChild(this.$element);
}

/*public void*/ Loader.prototype.show = function () {

	this.$element.style.top = this.getScrollTop() + "px";
	this.$element.style.display = "block";
}

/*public void*/ Loader.prototype.hide = function () {

	this.$element.style.display = "none";
}