// A generic callback function to write the result of an AJAX request to a named element.
function _writeAjaxResponseToElement(req, paramv, contextv) {
    var el = document.getElementById(contextv.targetid);
    // window.alert(el); return;
    el.innerHTML = req.responseText;
    el.style.display = 'block';
}

/**
 * Accepts the name of an element attribute to fetch and a list of
 * element IDs in which to seek it.  Would most often be used to get
 * the value attribute of several form elements.  Returns an
 * associative array of values indexed by element ID.
 *
 * array = GetElementAttributes('attributeName', 'id1', 'id2', id3',...);
 *
 */
function GetElementAttributes(attrName) {
    var attrv, el;
    attrv = new Array();
    l = arguments.length;
    for(i=1; i<l; i++) {
	// alert(arguments[i]);	
	el = document.getElementById(arguments[i]);
	attrv[arguments[i]] = el.value;
	// In FF, IE and Safari, el.value was correct even for a SELECT element.
	// alert(arguments[i] + "\n" + el.value);
    }

    return attrv;
}

/** Sets the value attribute of the element of eleId, most typically an input element obviously. */
function SetValueById(eleId, val) {
  el = document.getElementById(eleId);
  el.value = val;
}

function hideElement(id) {
    document.getElementById(id).style.display = 'none'; 
}
function showElement(id) {
    document.getElementById(id).style.display = 'block'; 
}

function displayToggle(id) {
    var el = document.getElementById(id).style; 
    if(el.display == "none") {
	el.display = "block";  
    }
    else if(el.display == "block") {
	el.display = "none";
    }
}

function disableSubmitById(id) {
    el = document.getElementById(id);
    el.disabled = true;
}

var Ajax = {


    tryFunctions : function (funclist) {
	for(var a=0,b=arguments.length;a<b;++a) {
	    try { return (arguments[a])(); } catch(e) {}
	}
    },

    createXmlHttp : function () {
	return Ajax.tryFunctions(
	    function() {return new XMLHttpRequest()},
	    function() {return new ActiveXObject('Msxml2.XMLHTTP')},
	    function() {return new ActiveXObject('Microsoft.XMLHTTP')},
	    function() {return window.createRequest()}
	    ) || false;
    },

    buildRequestBody : function (params) {
	var p, body='';
	for(p in params) {
	    if(body != '') {
		body += '&';
	    }
	    // body += p+'='+ params[p];
	    body += p + '=' + encodeURIComponent(params[p]);
	}
	return body;
    },

    /* 
    // KH: I want to see if I can modify this to pass full context through to the callback functions.
    
    sendRequest : function (apiUrl, params, okCallback, errorCallback) {
	var req = Ajax.createXmlHttp();
	req.open('POST', apiUrl, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function () {
	    if (req.readyState==4) { okCallback(req); }
       	};
	if(req.onerror)
	    req.onerror = function() {
	       	errorCallback(req);
	    };
	req.send(Ajax.buildRequestBody(params));
    }
    */

    sendRequest : function (apiUrl, paramv, okCallback, errorCallback, contextv) {
       var req = Ajax.createXmlHttp();
       req.open('POST', apiUrl, true);
       req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       req.onreadystatechange = function () {
	   if (req.readyState==4) { okCallback(req, paramv, contextv); }
       };
       if(req.onerror)
	   req.onerror = function() {
	       errorCallback(req, paramv, contextv);
	   };
       req.send(Ajax.buildRequestBody(paramv));
   }

}
