/* ##########################################USTAWIENIA ###################################################### */
var error_occured_info=false; // powiadamiaj alertem o wystapieniu bledu, jesli true i brak my_error_catch powiadamia przez funkcje alert()
var my_error_catch=showOverInfo; // nazwa funkcji do obslugi bledow np. my_error_catch=pokaz_blad;
var messages=new Array('Nie można utworzyć obiektu XMLHttpRequest','Nie ukończono poprzedniej akcji','Brak zdefiniowanego formularza'); // wlasne komunikaty
var progress_loading_interval=null; // 
//DOROBIC
//  tablice parametryczna
// obsluge bledow i metod ajaxa
// podlaczanie pod sam formularz
/* ######################################################################################################### */

function cXHRO(){
  var xmlHttp;
  if(window.ActiveXObject){ 
    try{
		xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
    }catch (e){ 
		try{
			xmlHttp = new ActiveXObject('Msml2.XMLHTTP');
		}catch(e){
			xmlHttp = false;
		}
	}
  }else{
	try{
      xmlHttp = new XMLHttpRequest(); 
    }catch (e){
      xmlHttp = false;
    } 
  }
  if (xmlHttp)
    return xmlHttp;
}

function jAjax(){
	this.xmlHttp=cXHRO();
	if(this.xmlHttp){
		this.url=null;
		this.jStatus='Gotowy';
		this.endFunc=null;
	}else{
		this.jStatus='Niestworzony';
		showOverInfo(messages[0]);
	}
}

jAjax.prototype.koniec=function(func){
	jAjax.xmlHttp.onreadystatechange=afterReturn;
}
function afterReturn(){
	if (jAjax.xmlHttp.readyState == 4 && jAjax.xmlHttp.status === 200){
		var func=jAjax.endFunc;
		eval('func(jAjax.xmlHttp)');
		jAjax.clr();
	}
}
jAjax.prototype.error_occured=function(result){
	if(error_occured_info)
		if(my_error_catch!=null)
			eval('my_error_catch(result)');
		else
			alert(result);
	return false;
}

jAjax.prototype.get=function(url,end){
	if (jAjax.xmlHttp.readyState==4 || jAjax.xmlHttp.readyState==0){
		jAjax.endFunc=end;
		jAjax.xmlHttp.onreadystatechange=afterReturn;
		jAjax.xmlHttp.open('get',url,true);
		jAjax.xmlHttp.send(null);
		jAjax.clr();
		return true;
	}
	jAjax.error_occured(messages[1]);
	jAjax.clr();
	return false;
}

jAjax.prototype.post=function(url,end,data){
	if (this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0 ){
			jAjax.endFunc=end;
			jAjax.xmlHttp.onreadystatechange=afterReturn;
			jAjax.xmlHttp.open('post',url,true);
			var parametry='jAjax=1';
			if(typeof(data)=='string')
				if(document.getElementById(data))
					data=document.getElementById(data);
				else
					return jAjax.error_occured(messages[2]);
			data.onsubmit=function(){ return false; };
			if(typeof(data)=='object'){
				for(var i=0; i<data.elements.length; i++)
					if(data.elements[i].name!=undefined && data.elements[i].value!=undefined)
						parametry+='&'+data.elements[i].name+'='+data.elements[i].value;
			}else
				return false;
			jAjax.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			jAjax.xmlHttp.setRequestHeader("Content-length", parametry.length);
			jAjax.xmlHttp.setRequestHeader("Connection", "close");
			jAjax.xmlHttp.send(parametry);
		this.clr();
		return true;
	}
	this.error_occured(messages[1]);
	this.clr();
	return false;
}

jAjax.prototype.file=function(form,end,progress,progress_loading_interval_val){
	if(form==null || form==undefined)
		return jAjax.error_occured(messages[2]);
	if(document.getElementById('tmp_upload_target')==null){
		var ifr=document.createElement('iframe');
		//ifr.setAttribute('style','position: absolute; display: none; width: 0px; height: 0px;');
		ifr.setAttribute('id','tmp_upload_target');
		ifr.setAttribute('name','tmp_upload_target');
		ifr.setAttribute('src','about:blank');
		ifr.style.position='absolute';
		ifr.style.display='none';
		ifr.style.width='0px';
		ifr.style.height='0px';
		ifr.onload=function(){ eval('end(this.contentDocument.body.innerHTML);'); /*clearInterval(progress_loading_interval);*/ };
		//if(progress!=undefined && progress_loading_interval_val!=undefined)
		//	progress_loading_interval=setInterval(eval('progress()'),progress_loading_interval_val*1000);
		document.body.appendChild(ifr);
	}
	form.setAttribute('target','tmp_upload_target');
	return true;
}

jAjax.prototype.clr=function(){
	for(a in this){
		if(a!='xmlHttp')
			delete this[a];
	}
}

jAjax.prototype.sendRequest=function(url,end,method,data,loading){ // adres pliku docelowego, funkcja wykonywana po zakonczeniu zapytania, metoda(get,post,file),dane do post(!!), druga funkcja wykonuwana po zakonczeniu zapytania
	if (this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0 ){
		jAjax.endFunc=end;
		if(method=='GET' || method=='get'){
			jAjax.xmlHttp.open('get',url,true);
			jAjax.xmlHttp.onreadystatechange=afterReturn;
			jAjax.xmlHttp.send(null);
		}else if(method=='POST' || method=='post'){
			jAjax.xmlHttp.open('post',url,true);
			jAjax.xmlHttp.onreadystatechange=afterReturn;
			var parametry='jAjax=1';
			if(typeof(data)=='string')
				if(document.getElementById(data))
					data=document.getElementById(data);
				else
					return jAjax.error_occured(messages[2]);
			data.onsubmit=function(){ return false; };
			if(typeof(data)=='object'){
				for(var i=0; i<data.elements.length; i++)
					if(data.elements[i].name!=undefined && data.elements[i].value!=undefined)
						parametry+='&'+data.elements[i].name+'='+data.elements[i].value;
			}else
				return false;
			jAjax.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			jAjax.xmlHttp.setRequestHeader("Content-length", parametry.length);
			jAjax.xmlHttp.setRequestHeader("Connection", "close");
			jAjax.xmlHttp.send(parametry);
		}
	}
}

jAjax.prototype.status=function(){
	alert('Status: '+jAjax.jStatus);
}
var jAjax=new jAjax();
