//Copyright  2002-2007 Machineware, LLC

gbal = {};
gbal['httpR'] = {};
gbal['httpURL'] = {};
gbal['eMode'] = false;

function getReqData(oname){
	if(gbal['eMode']){
		var tframe = getObjByName('iframe_' + oname);
		httpFinish(oname);
		return(tframe.contentWindow.document.body.innerHTML);
	} else {
		try{
			var temp = gbal['httpR'][oname].responseText;
			httpFinish(oname);
			return(temp);
		} catch(e){
			alert('Error with http request data (' + oname + ')');
		}
	}
}

function examineReqData(oname){
	if(gbal['eMode']){
		var tframe = getObjByName('iframe_' + oname);
		return(tframe.contentWindow.document.body.innerHTML);
	} else {
		try{
			var temp = gbal['httpR'][oname].responseText;
			return(temp);
		} catch(e){
			return(null);
			//alert('Error with http request data (' + oname + ')');
		}
	}
}

function httpStatus(oname){
	if(gbal['eMode']){
		return('4');
	} else {
		if(gbal['httpR'][oname] != null && gbal['httpR'][oname].readyState == 4){
		}
		if(gbal['httpR'][oname] != null){
			return(gbal['httpR'][oname].readyState);
		} else {
			return(null);
		}
	}
}

//http status code
function httpResult(oname){
	if(gbal['eMode']){
		return(200);
	} else{
		try{
			if(gbal['httpR'][oname].status == 200){

			}
			return(gbal['httpR'][oname].status);
		} catch(e){
			return(null);
		}
	}
}

function getHTTPHeader(o,h){
	try{
		if(gbal['httpR'][o].getResponseHeader(h) == ''){
			return(null);
		} else {
			return(gbal['httpR'][o].getResponseHeader(h));
		}
	} catch(e){
		return(null);
	}
}

function httpPostRequest(oname,target_url,callback,fix_info,params){

	if(gbal['httpR'][oname] != null){
		if(gbal['httpR'][oname].readyState != '4'){ // this process
			gbal['httpR'][oname].onreadystatechange = null;
			gbal['httpR'][oname].abort();
		}
	}

	if(window.XMLHttpRequest){
		gbal['httpR'][oname] = new XMLHttpRequest();
	} else {
		gbal['httpR'][oname] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var sObj = new Object;
	sObj.oname = oname;
	sObj.url = target_url;
	if(callback == null){
		callback = httpMonitor;
	}
	sObj.callback = callback;
	sObj.fix = fix_info;
	sObj.params = params;
	sObj.isPost = true;
	sObj.startTime = new Date;

	gbal['httpR'][oname].sObj = sObj;
	if(callback){
		gbal['httpR'][oname].onreadystatechange = callback;
	}
	if(callback){
		var thisFunc = function(){this.callback(this.oname,this.fix)};
		thisFunc.callback =callback;
		thisFunc.oname = oname;
		thisFunc.fix = fix_info;
		gbal['httpR'][oname].onreadystatechange = thisFunc;
	}

	gbal['httpR'][oname].open("POST", target_url, true);
	gbal['httpR'][oname].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	gbal['httpR'][oname].setRequestHeader("Connection", "close");

	var postp;
	for(var this_post_item=0;this_post_item<params.length;this_post_item++){
		postp = postp + '&' + params[this_post_item].name   + '=' + params[this_post_item].data;
	}
	gbal['httpR'][oname].send(postp);
}


function httpFinish(oname,badResult){
		
	gbal['httpURL'][oname] = null;
	gbal['httpR'][oname] = null;

}

function httpRetry(oname){
	if(gbal['httpURL'][oname] != null){
		var sObj = gbal['httpURL'][oname];
		gbal['httpURL'][oname] = null;
		gbal['httpR'][oname] = null;
		if(sObj.isPost){
			httpPostRequest(sObj.oname,sObj.url,sObj.callback,sObj.fix,sObj.params);
		} else {
			httpRequest(sObj.oname,sObj.url,sObj.callback,sObj.fix);
		}
	}
}


//monitor and dispose of http links
function httpMonitor(oname){
	if(httpStatus(oname) == '4'){		
		if(httpResult(oname) != 200){ //bad result...server down, etc
			httpFinish(oname,true);
		} else {
			httpFinish(oname);
		}
	}
}

function httpRequest(oname,url,callback,fix){
	
	var sObj = new Object;
	sObj.oname = oname;
	sObj.url = url;
	if(callback == null){
		callback = httpMonitor;
	}
	sObj.callback = callback;
	sObj.fix = fix;
	sObj.isPost = false;
	sObj.startTime = new Date;

	if(fix != null){
		oname = oname+'_' + fix;
	}
	if(gbal['httpR'][oname] != null){
		if(gbal['httpR'][oname].readyState != '4'){ // this process
			gbal['httpR'][oname].onreadystatechange = null;
			gbal['httpR'][oname].abort();

		}
	}
	gbal['httpURL'][oname] = sObj;
	try{
		if(window.XMLHttpRequest){
			gbal['httpR'][oname] = new XMLHttpRequest();
		} else {
			gbal['httpR'][oname] = new ActiveXObject("Microsoft.XMLHTTP");
		}
		gbal['eMode']=false;
		if(callback){
			var thisFunc = function(){callback(oname,fix)};
			gbal['httpR'][oname].onreadystatechange = thisFunc;
		}
		gbal['httpR'][oname].open("GET", url, true);
		gbal['httpR'][oname].setRequestHeader("Connection", "close");
		gbal['httpR'][oname].send();
	} catch(e){ //high securty setting?
		
	}
}

function getScreenWidth(){
	return document.documentElement.offsetWidth;
}

function getScreenHeight(){
	return document.documentElement.offsetHeight;
}

function getOffsetTop(the_object){
	var the_offset = 0;
	while (the_object != null) {
		the_offset += the_object.offsetTop;
		the_object = the_object.offsetParent;
	}
	return the_offset;
}

function getObjTop(target_obj){
	return parseInt(target_obj.offsetTop,10);
}

function getObjLeft(target_obj){
	return parseInt(target_obj.offsetLeft,10);
}
