var cv_http_requests = 0;
var cv_http_loadmsg;

// get the width of an object in pixels
// (IE uses a different attribute than Mozilla)
function getWidth(obj) {
    width = obj.offsetWidth;
    if (width == 0)
        width = obj.style.pixelWidth;
    return width;
}

// get the height of an object in pixels
function getHeight(obj) {
    height = obj.offsetHeight;
    if (height == 0)
        height = obj.style.pixelHeight;
    return height;
}

function centerDiv(div) {
    dom = document.getElementById && !document.all
    px = (dom ? window.pageXOffset : document.body.scrollLeft) + Math.floor(((dom ? window.innerWidth : document.documentElement.clientWidth) - getWidth(div)) / 2) - parseInt(document.body.style.marginLeft);
    py = (dom ? window.pageYOffset : document.body.scrollTop) + Math.floor(((dom ? window.innerHeight : document.documentElement.clientHeight) - getHeight(div)) / 2);
    try {
        div.style.left = px + 'px';
        div.style.top = py + 'px';
    } catch (e) {}
}

function getHTTPObject() {
	var xmlhttp;

	// this part runs in IE
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
            xmlhttp.overrideMimeType('text/xml');
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function buildQueryString(attrs) {
	var q = '';
	for (var attrName in attrs) {
		q += attrName + '=' + escape(attrs[attrName]) + '&';
	}
	// Remove trailing separator
	return q.substr(0, q.length - 1);
}

function doHttpRequest(url, attrs, callback, showLoad, method) {
	if (typeof(showLoad) == 'undefined')
		showLoad = 1;
	if (typeof(method) == 'undefined')
		method = "GET";
	if (attrs != null)
		query = buildQueryString(attrs);
	if (method == 'GET' && attrs != null)
		url = (url.indexOf('?') == -1) ? (url + '?' + query) : (url + '&' + query);
	var http = getHTTPObject();
	if (http) {
		http.open(method, url, true);
		if (method == 'POST')
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		makeLoadIndicator(showLoad);
		http.onreadystatechange = function () {
            //A try catch was added in order deal with the problem where the page was being reloaded before the function could complete.
            //This change is most likly temporary till a more suitable solution can be found.
            try{
            if (http.readyState == 4) {
				switch (http.status) {
				case 404:
				case 500:
					showHTTPError(http.responseText);
					break;
				default:
					typeof callback == 'function' ? callback(http) : eval(callback + "(http);");
					break;
				}
				removeLoadIndicator();
            }
            }catch(e){}
		}
		http.send(method == "POST" ? query : null);
		return true;
	}
	else {
		var iframe = document.createElement('iframe');
		iframe.style.display = 'none';
		document.body.appendChild(iframe);
		makeLoadIndicator(showLoad);
		iframe.src = url;
		iframe.onload = function () {
			if (iframe.title.indexOf('Error 500') == -1 && iframe.title.indexOf('Error 404') == -1) {
				iframe.responseText = iframe.contentDocument ? iframe.responseXML = iframe.contentDocument.documentElement.firstChild.nextSibling.innerHTML : iframe.innerHTML;
				typeof callback == 'function' ? callback(iframe) : eval(callback + "(iframe);");
			} else {
				showHTTPError(iframe.contentDocument ? iframe.contentDocument.documentElement.firstChild.nextSibling.innerHTML : iframe.innerHTML);
			}
			removeLoadIndicator();
			document.body.removeChild(iframe);
		}	
	}
	return false;
}


function makeLoadIndicator(showLoad) {
	if (cv_http_requests == 0) {
		if (showLoad) {
			var i = document.createElement('div');
			i.className = "loadIndicator";
			i.appendChild(document.createTextNode('Loading...'));
			document.body.appendChild(i);
			centerDiv(i);
			cv_http_loadmsg = i;
		}
		document.body.style.cursor = 'progress';
	}
	cv_http_requests++;	
}

function removeLoadIndicator() {
	cv_http_requests--;
	if (cv_http_requests == 0) {
		document.body.style.cursor = 'default';
		document.body.removeChild(cv_http_loadmsg);
	}
}

function showHTTPError(str) {
	try {
		errorWin = popup('', 'error', 800, 400);
		str = '<base href="' + window.location + '" />' + str;
		errorWin.document.body.innerHTML = str;
	}
	// If pop-up gets blocked, inform user
	catch(e) {
		alert('An error occurred, but the error message cannot be' +
			' displayed because of your browser\'s pop-up blocker.\n' +
			'Please allow pop-ups from this web site.');
		}
}