/*
 * $Id: xmlhttp.js,v 1.3 2010-05-31 17:02:58 alongwil Exp $
 *
 * Copyright (c) 2005 Orbis Technology Ltd. All rights reserved.
 *
 * OpenBet Office
 * getXMLHttpRequest
 *
 *
 * 
 * Original source, openbet office shared javascript: xmlhttp.js
 * Id: xmlhttp.js,v 1.11 2009-10-17 15:54:06
 */

__xmlhttp_id__ = '$Id: xmlhttp.js,v 1.3 2010-05-31 17:02:58 alongwil Exp $';

if(document.Package) {
	document.Package.provide('office', 'xmlhttp');
}

/**********************************************************************
 * getXMLHttpRequest
 *********************************************************************/

function getXMLHttpRequest()
{
	var httpReq = false;

	try {
		// non-IE and IE7
		httpReq = new XMLHttpRequest();

		// Some versions of Mozilla are reported as locking up when anything
		// other than XML is returned
		if (httpReq.overrideMimeType) {
			httpReq.overrideMimeType("text/xml");
		}
	}
	catch(e) {
		// IE (not IE7)
		var type = ['MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'],
			i = 0,
			len = type.length;

		for(; i < len; i++){
			try {
				httpReq = new ActiveXObject(type[i]);
				break;
			} catch(e) {}
		}

	} finally {
		return httpReq;
	}
}

var request_complete;
// AJAX (get|post) HTTP Request
function HttpRequest(_url, _method, _callback, _post, _varAsync)
{

	var req = getXMLHttpRequest();
	if(!req) {
		alert("Your browser does not support AJAX, please upgrade");
		return;
	}

	if(typeof _varAsync == 'undefined') {
		_varAsync = true;
	}

	req.open(_method, _url, _varAsync);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	req.setRequestHeader("Timeout", SA_REQUEST_TIMEOUT);

	// For special case callback set a timer.
	request_complete = 0;
	if (_callback == 'placedResultsCallback') {
		var thread_id = setTimeout(
			// Abort request and send message back to handler.
			function(req) {
				if (!request_complete) {
					req.abort();
					_callback('ERROR');
					return;
				}
			},SA_REQUEST_TIMEOUT);
	}

	// non-blocking
	if(_varAsync) {
		req.onreadystatechange = function() {
			// Bet placemenet request confirmation.
			if(req.readyState == 4) {
				if (_callback == 'placedResultsCallback') {
					request_complete = 1;
				}

				_callback(req);
			}
		};
	}

	// We now know the outcome.
	clearTimeout(thread_id);

	if(_method == "POST") req.send(_post);
	else req.send(null);

	// blocking
	if(!_varAsync) _callback(req);
}



// toggle AJAX image busy-indicator
// - _id should include the class http_indicator
function HttpIndicator(_id, _enable)
{
	var obj = getObject(_id);
	if(obj && obj.className.indexOf('http_indicator') > -1) {
		obj.style.backgroundImage = _enable ? 'url(' + document.ajaxIndicator.src + ')' : 'none';
	}
}

