var browser = '';
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer") {
	browser = 'ie';
}
else {
	browser='nonie';
}

var pool = new Array();

function ajaxCommunicator(){
	
	if(browser=='ie'){
		function pickRecentProgID(idList, enabledList){
		    // found progID flag
		    var bFound = false;
		    for(var i=0; i < idList.length && !bFound; i++){
		        try{
		            var oDoc = new ActiveXObject(idList[i]);
		            o2Store = idList[i];
		            bFound = true;
		            for(var j=0;j<enabledList.length;j++)
		                if(i <= enabledList[j][1])
		                    Sarissa["IS_ENABLED_"+enabledList[j][0]] = true;
		        }catch (objException){
		            // trap; try next progID
		        };
		    };
		    if (!bFound)
		        throw "Could not retreive a valid progID of Class: " + idList[idList.length-1]+". (original exception: "+e+")";
		    idList = null;
		    return o2Store;
		};
		// pick best available MSXML progIDs
		this._PIM_XMLHTTP_PROGID = pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"], [["XMLHTTP", 4]]);
		// we dont need this anymore
		pickRecentProgID = null;
	}
	
}

ajaxCommunicator.prototype.getXMLHTTP = function(){
	var xmlhttp=null;
	//deba(this);
	if(pool.length>0){
		xmlhttp = pool.shift();
	}
	else{
		xmlhttp = this.createXMLHTTP();
	}
	return xmlhttp;
}
	
ajaxCommunicator.prototype.createXMLHTTP = function(){
	var A = null;
	if(typeof XMLHttpRequest != "undefined") {
		// Mozilla
		A=new XMLHttpRequest();
	}
	if (!A && document.all) {
		// IE
		A=new ActiveXObject(this._PIM_XMLHTTP_PROGID);
	}
	return A;
}
	
ajaxCommunicator.prototype.getData = function(url,callback){
	var xmlhttp = this.getXMLHTTP();
	if (xmlhttp) {
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4) {
				callback(xmlhttp);
				pool[pool.length]=xmlhttp;
//				deba(pool);
			}
		}
		xmlhttp.send(null);
	}
	else{
		throw('No XmlHTTPRequest Object!');
	}
}

ajaxCommunicator.prototype.postData = function(url,data,callback){
	var xmlhttp = this.getXMLHTTP();
	if (xmlhttp) {
		var postdata = new Array;
		for(name in data){
			postdata[postdata.length] = name + '=' + data[name];
		}
		//deba(postdata);
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4) {
				callback(xmlhttp);
				pool[pool.length]=xmlhttp;
//				deba(pool);
			}
		}
		xmlhttp.send(postdata.join("&"));
	}
	else{
		throw('No XmlHTTPRequest Object!');
	}
}

var ajax = new ajaxCommunicator();
var blockSeparator="#-|-#";
var itemSeparator="#|#";

function comget(id){
	ajax.getData("requests.php?act=comget&id="+id,function(xmlhttp) {
				if (xmlhttp.status == 200) {
					if (document.getElementById('com')) {
						var respT = xmlhttp.responseText;
						//Clear empty spaces in response string inData
						respT = respT.replace(/^\s+/,'');
						respT = respT.replace(/\s+$/,'');
						var eL = document.getElementById('com');
						var citE = document.getElementById('cit');
						//deba(respT);
						
						if(respT==''){
							eL.length="";
							var optM = new Option('Моля изберете', '');
							eL.options[eL.options.length] = optM;
				
							citE.length="";
							var optT = new Option('Моля изберете', '');
							citE.options[citE.options.length] = optT;
						} else {
							var respArr = new Array();
							respArr = respT.split(itemSeparator);
							eL.selectedIndex = 0;
							//eL.disabled = false;
							eL.length="";

							citE.length="";
							var optT = new Option('Моля изберете', '');
							citE.options[citE.options.length] = optT;
							//document.getElementById('cit').disabled = true;
							
							for(i=0;i<respArr.length-1;i++){
								var opt = new Option(respArr[i], respArr[i+1]);
								eL.options[eL.options.length] = opt;
								i=i+1;
							}
						}
					}
				}
				else{
					document.getElementById('err').innerHTML = "Server returned error, HTTP status " + xmlhttp.status;
				}
			}
		);
}

function citget(id){
	ajax.getData("requests.php?act=citget&id="+id,function(xmlhttp) {
				if (xmlhttp.status == 200) {
					if (document.getElementById('cit')) {
						var respT = xmlhttp.responseText;
						//Clear empty spaces in response string inData
						respT = respT.replace(/^\s+/,'');
						respT = respT.replace(/\s+$/,'');
						
						var eL = document.getElementById('cit');
						if(respT==''){
							eL.length="";
							var opt = new Option('Моля изберете', '');
							eL.options[eL.options.length] = opt;
						} else {
							var respArr = new Array();
							respArr = respT.split(itemSeparator);
							eL.selectedIndex = 0;
							//eL.disabled = false;
							eL.length="";
		
							for(i=0;i<respArr.length-1;i++){
								var opt = new Option(respArr[i], respArr[i+1]);
								eL.options[eL.options.length] = opt;
								i=i+1;
							}
						}
					}
				}
				else{
					document.getElementById('err').innerHTML = "Server returned error, HTTP status " + xmlhttp.status;
				}
			}
		);
}
