//some code based on ideas from Apple developer code (developer.apple.com) and nevow 0.4.1 (www.nevow.org)

// refactoring of existing ajax functionality to allow server-initiated data to be processed client side
// and to prevent server timeouts

var server_response;
var dying = false;
var request_count = 0;

function ajaxRequest(url, responseHandler) {
    var req;
    var response_args = [];
    if (arguments.length > 2) {
        for (var i = 2; i < arguments.length; i++) {
            response_args += arguments[i];
        }
    }
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req) {
	req.onreadystatechange = function() {
	    if (req.readyState==4) {
		if (req.status == 200) {
		    try {
                        if (typeof responseHandler == "undefined")
                        {
                            eval(req.responseText);
                        } else {
			    responseHandler(req.responseText, response_args);
                        }
		    } catch (e) {
			alert(e); //ignore errors
		    }
		}
	    }
	};
	req.open("GET", url, true);
	req.send(null);
    }
}

function shutdown()
{
    if (server_response) {
	server_response.abort();
    }
    dying = true;
}

var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("msie") != -1) {
    /* IE specific stuff */
    /* Abort last request so we don't 'leak' connections */
    window.attachEvent("onbeforeunload", shutdown )
    /* Set unload flag */
} else if (document.implementation && document.implementation.createDocument) {
    /* Mozilla specific stuff (onbeforeunload is in v1.7+ only) */
    window.addEventListener("beforeunload", shutdown, false);
}

//generic ajax call
function generic_ajax(pre_action,func,qsa) {
    // pre_action: some javascript to execute before calling the function passed as
    // func: a function object
    // qsa: a query string slice (including leading '&') to be appended to the
    // url for the xmlhttprequest
    if (pre_action) {
	eval(pre_action);
    }
    var additionalArguments = qsa;
    for (i = 3; i<arguments.length; i++) {
	additionalArguments += '&a=';
	additionalArguments += encodeURIComponent(arguments[i]);
    }
    request_count += 1;
    additionalArguments += '&a=' + request_count;
    var loc = document.location.toString();
    if (loc.indexOf('?') != -1) {
	loc = loc.substr(0,loc.indexOf('?'));
    }
    ajaxRequest(loc+"?_action_ajax_controller=&f="+func+additionalArguments);
}

function unapiRequest(type, id, format, display) {
  ajaxRequest('/'+type+'/unapi?format='+format+'&id='+id, unapiResponse, display);
}

function unapiResponse(text, display) {
  var e = document.getElementById(display);
  e.innerHTML = '<textarea class="unapiResponse" cols="60" rows="7">'+text+'</textarea>';
  e.focus();
  var c = document.getElementById('close'+display); 
  c.style.display = 'inline';
}

function unapiClear(display) {
  var e = document.getElementById(display); 
  e.innerHTML = ''; 
  var c = document.getElementById('close'+display); 
  c.style.display = 'none';
}

