/* ##########################################################################################
 *	HTTP REQUEST OBJECT by Koko, Jelc
  ########################################################################################## */
  
function HTTPRequestSend(URL,mode,caller,callBackFunction,method,postData) {
    //var method = 'GET';
    //var postData = '';
    var format = 'JSON';
    var reqHeader = [{typ:'Content-Type', content:'application/x-www-form-urlencoded' }];
    if(typeof(XMLHttpRequest) != 'undefined') {
		var XHR = new XMLHttpRequest();
	}
	else {
		try {
			/*- IE */
			var XHR = new ActiveXObject("Msxml2.XMLHTTP"); /*- "Microsoft.XMLHTTP*/
		}
		catch (e) {
			try { 
				var XHR = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			catch (e) {
				return 0;
			}
		}
	}
    try {
		XHR.open(method, URL, mode=='sync' ? false : true);
	} catch(e){
		return 0;
	}
    
	if(reqHeader){
		for(var i = 0; i < reqHeader.length; i++){
			XHR.setRequestHeader(reqHeader[i].typ,reqHeader[i].content)
		}
	}
    if(mode != 'sync') {
		function prdel() {
	        if( XHR.readyState == 4 ) {
	            if( XHR.status == 200 ) {
					if(format == 'xml'){
						var dom = XHR.responseXML;
						caller[callBackFunction](dom);
					} else {
						eval('var data = '+XHR.responseText+'');
						caller[callBackFunction]({status:XHR.status, data:data});
					}
					XHR = null;
	            } else {
                    caller[callBackFunction]({status:XHR.status});
                }
	        }
	    }
		XHR.onreadystatechange = prdel;
    }
	if(method == 'POST'){ 
	    if(typeof postData != 'undefined') {
			XHR.send(postData);
		} else {
			return 0;
		}
	} else {
		XHR.send(null);
	}
	
	if(mode == 'sync'){
		if(format == 'xml'){
			var data = XHR.responseXML;
		} else {
            eval('var data = '+XHR.responseText+'');
		}
		return { status : XHR.status, data : data };
		
	} else {
		return XHR;
	}
};

function HTTPRequestAbort(XHR) {
	if (typeof XHR == 'object') {
		XHR.abort();
	} else {
		return 0;
	}
};
