var req;

//AJAX functions
function loadXMLDoc(url,div,image,method,position)
{
	method = method || 'GET';
	//alert(url);
	//graphic = window.parent.document.getElementById('loading');
	if(image)	image.style.display = "block";
	//branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = function () {processReqChange(div,image,position);};
		if(method=='POST' || method=='post')
		{
			var pos = url.indexOf('?');
			var url_root = url.substr(0,pos);
            var hashPos = url_root.indexOf('#');
            if(hashPos!=-1)
            {
                url_root = url_root.substring(0,hashPos);
            }
            var url_args = url.substr(pos+1);
			req.open(method,url_root,true);
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			req.send(url_args);
		}
		else
		{
			req.open(method,url,true);
			req.send(null);
		}	
	}	//branch for IE/Windows ActiveX version
	else if(window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if(req) {
			req.onreadystatechange = function () {processReqChange(div,image,position);};
			if(method=='POST' || method=='post')
			{
				var pos = url.indexOf('?');
				var url_root = url.substr(0,pos);
                var hashPos = url_root.indexOf('#');
                if(hashPos!=-1)
                {
                    url_root = url_root.substring(0,hashPos);
                }
                var url_args = url.substr(pos+1);

				req.open(method,url_root,true);
				req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				req.send(url_args);
			}
			else
			{
				req.open(method,url,true);
				req.send();
			}
		}
	}
}

function processReqChange(div,image,position)
{
	//only if req shows "complete"
	if(req.readyState==4) {
		if(image)	image.style.display = "none";
		//only if OK
		if(req.status == 200)
		{
//alert(req.getAllResponseHeaders());
			//alert(req.responseXML);
			xmlresponse = req.responseXML;
			if(xmlresponse && xmlresponse.documentElement!=undefined)
			{
			  response = xmlresponse.documentElement;
			  method = response.getElementsByTagName('method')[0].firstChild.data;
			  result = response.getElementsByTagName('result')[0];
			    if(response.getElementsByTagName('other').length==0)
			    {
			      eval(method+'(div,result,position)');
			    }
			    else
			    {
			      other = response.getElementsByTagName('other')[0];
			      eval(method+'(div,result,other,position)');
			    }
			}
			else
			{
			  response = eval('('+req.responseText+')');
			  method = response.method;
			  eval(method+'(div,response,position)');
			}
		}
		else {
			alert("there was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function ajaxLink(el)
{
    var href = el.getAttribute('href');
    var vars = getUrlVars(href);
    if(vars['check'])
    {
        if(!confirm('Are you sure?'))
        {
            return false;
        }
    }

    var target;
    if(vars['ajaxview'])
    {
        href = href + '&view='+vars['ajaxview'];
        target = document.getElementById(vars['ajaxview']);
    }
    
    href = href + '&format=json';
    loadXMLDoc(href,target,loadingImg,'GET');
    return false;
}
