function callAJAX(url, respHdlr, bPageView, failHdlr, callHdlr, bAsync) {

	var bAsync = (bAsync == null ? true : bAsync);
	var req;
	
	try {
		req = new XMLHttpRequest();													/* e.g. Firefox */
	} catch(e) {
		try {
	 		req = new ActiveXObject("Msxml2.XMLHTTP");				/* some versions IE */
	 	} catch (e) {
	  	try {
	  		req = new ActiveXObject("Microsoft.XMLHTTP");		/* other versions IE */
	  	} catch (e) {
				return false;
	 		}
		}
	}
	
	if (callHdlr) {
		callHdlr();
	}
	req.onreadystatechange = function() {
		AJAXResp(req, url, respHdlr, bPageView, failHdlr);
	};
	req.open("GET", url, bAsync);
	req.send(null);
}
	
function AJAXResp(req, url, respHdlr, bPageView, failHdlr) {
  
        bPageView = (bPageView == null ? false : bPageView);
	if (req.readyState == 4) {
		if (req.status == 200) {
		  if (bPageView) {
		    // console.log('Urchin called');
        // pageTracker._trackPageview(url);
  			urchinTracker(url);
  		}
  		if (respHdlr != null) {
  			respObj = eval('(' + req.responseText + ')');
	  		respHdlr(req, respObj);
	  	  }
		} else if (failHdlr) {
			failHdlr(req);
		}
	}
}

var aAJAXStack = new Array();

function AJAXPush(ajxCall, bSession) {
 	aAJAXStack[aAJAXStack.length] = ajxCall;
  bSession = (bSession == null ? false : bSession);
  if (bSession) {
    callAJAX(WS_AJAX + "stackSession.php?move=" + ajs2aphp(aAJAXStack), null, false, null, null, false);
  }
}

function AJAXPop(respObj) {
	
	if (respObj.SUCCESS == true) {
	  if (aAJAXStack.length > 0) {
		  eval(aAJAXStack[0]);
		  aAJAXStack.splice(0, 1);
		}
	}
}

function AJAXReset() {
	
	aAJAXStack = [];
}

// adapted from http://aspn.activestate.com/ASPN/Cookbook/PHP/Recipe/414334
function ajs2aphp(a)
// This converts a javascript array to a string in PHP serialized format.
// This is useful for passing arrays to PHP. On the PHP side you can 
// unserialize this string from a cookie or request variable. For example,
// assuming you used javascript to set a cookie called "php_array"
// to the value of a javascript array then you can restore the cookie 
// from PHP like this:
//    <?php
//    session_start();
//    $my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
//    print_r ($my_array);
//    ?>
// /* This automatically converts both keys and values to strings.
// The return string is not URL escaped, so you must call the
// Javascript "escape()" function before you pass this string to PHP. */
{
    var a_php = "";
    var total = 0;
    for (var key in a) {
        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
    }
    a_php = escape("a:" + total + ":{" + a_php + "}");
    return a_php;
}
