﻿function ge(id) {return document.getElementById(id);}
function getRequestBody(oForm) {
	var aParams = new Array();
	for (var i=0; i<oForm.elements.length; i++) {
		if (oForm.elements[i].type == 'checkbox'&& oForm.elements[i].checked == false) continue;
		if (oForm.elements[i].type == 'radio'&& oForm.elements[i].checked == false) continue;
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
		//alert(sParam);
	}
	return aParams.join("&");
}
function tAjax() {
	var ax = new Object();
	ax._link = '';
	ax._form = '';
	ax.param = new Object();
	ax.onSuccess = null;
	ax.onError = null;
	ax.ajaxRequest = tAjax.oAjax();
	ax.ajaxRequest.onreadystatechange = function() {
		if (ax==null || ax.ajaxRequest==null) return false;
		if (ax.ajaxRequest.readyState==4) {
			ax.status = ax.ajaxRequest.status;
			ax.statusText = ax.ajaxRequest.statusText;
			ax.ajaxText = ax.ajaxRequest.responseText;
			ax.ajaxXML = ax.ajaxRequest.responseXML;
			if (ax.ajaxRequest.status==200 && typeof(ax.onSuccess)=='function') ax.onSuccess(ax);
			else if(typeof(ax.onError)=='function') ax.onError(ax);
			delete ax.ajaxRequest['onreadystatechange']; 
			ax.ajaxRequest = null;
		}
	}
	ax.startGET = function() {
		
		if (ax._link.length<3) return false;
		ax.ajaxRequest.open('get',ax._link,true);
		ax.ajaxRequest.send('');
	}
	ax.startPOST = function() {
		if (ax._link.length<3) return false;
		var oForm = document.forms[ax._form];
		var sBody = getRequestBody(oForm);
		ax.ajaxRequest.open('post',ax._link,true);
		ax.ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ax.ajaxRequest.send(sBody);
	}
	ax.opcje = function(opt) {
		for(i in opt) 
			if(typeof(ax[i])=='undefined') ax.param[i]=opt[i]; 
			else ax[i]=opt[i];	
	}
	return ax;
}
tAjax.oAjax = function() {
	if (window.XMLHttpRequest) return new XMLHttpRequest(); 
	else if (window.ActiveXObject) 
		try {
			return new ActiveXObject('Msxml2.XMLHTTP'); 
		} 
		catch(e) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP'); 
			} 
			catch(e) {
				return null;
			}
		} 
	return null;
}
tAjax.get = function(opt) {
	if (typeof(opt)!='undefined' && opt!=null) {
		var ajax = new tAjax();
		ajax.opcje(opt);
		ajax.startGET();
	}
}
tAjax.post = function(opt) {
	if (typeof(opt)!='undefined' && opt!=null) {
		var ajax = new tAjax();
		ajax.opcje(opt);
		ajax.startPOST();
	}
}
function loadPOST(_link,_form,_obj,v1,v2,v3,v4) {
	var height_obj = ge(_obj).offsetHeight;
	tAjax.post(
		{
			'_link':_link,
			'_form':_form,
			'onSuccess':function(ax) {
				var oferta = new SWFObject(v1, "oferta", "100%", v2, "9", "#f4eeea");
				oferta.addParam("scale", "noscale");
				oferta.addParam("salign", "tl");
				oferta.addParam("wmode", "transparent");
				oferta.addVariable("id_pages", v3);
				oferta.addVariable("admin", v4);
				setTimeout(
					function()	{
						ge(_obj).innerHTML = ax.ajaxText;
						oferta.write("oferta");
					}
					,1000
				);
			}
		}
	);
	ge(_obj).innerHTML = '<div style="height:'+height_obj+'px;"><div style="text-align:center; padding-top:'+(height_obj/3-50)+'px;"><div>Proszę czekać!!!<br>Pobieranie danych...</div><div style="padding-top:10px;"><img src="img/loading.gif" width="32" height="32" /></div></div></div>';
	return false;
}
function load_POST(_link,_form,_obj, v1, v2, v3, v4) {
	var height_obj = ge(_obj).offsetHeight;
	tAjax.post(
		{
			'_link':_link,
			'_form':_form,
			'onSuccess':function(ax) {
				setTimeout(
					function()	{
						ge(_obj).innerHTML = 'witam';
					}
					,1000
				);
			}
		}
	);
	ge(_obj).innerHTML = '<div style="height:'+height_obj+'px;"><div style="text-align:center; padding-top:'+(height_obj/3-50)+'px;"><div>Proszę czekać!!!<br>Pobieranie danych...</div><div style="padding-top:10px;"><img src="img/loading.gif" width="32" height="32" /></div></div></div>';
	return false;
}
