/* ---------------------------------------------*\
|
| some functions for  httprequests & XML handling
|
| (c) Jody Weissmann jody.xha@gmail.com
\*----------------------------------------------*/

var http;
var g_cbFunc;
var g_bVerbose=false;

//-----------------------------------------------
// doRequest
//   initiate the request for image data
//   imgid:        id of image to display
//   callbackFunc: function object to be called with image data array
//
function doRequest(action, imgid, callbackFunc) {
    g_cbFunc = callbackFunc; 


    http = createRequestObject();
    http.open('get', 'internal_request.php?action=' + action +'&id=' + imgid);
    http.onreadystatechange = handleResponse;
    http.send(null);

}
//-----------------------------------------------
// doRequestS
//   initiate the request for image data
//   imgid:        id of image to display
//   callbackFunc: function object to be called with image data array
//
function doRequestS(sRequest, callbackFunc) {
    g_cbFunc = callbackFunc; 
    //    alert("request is : "+sRequest);

    http = createRequestObject();
    http.open('get', 'internal_request.php?'+sRequest);
    http.onreadystatechange = handleResponse;
    http.send(null);

}

//-----------------------------------------------
// handleResponse
//  extract image data array from XML response
//  and pass it to g_cbFunc
//
//  The expected format:
//  <imginfo>
//    <item id="name1">value1</item>
//    <item id="name2">value2</item>
//    ...
//  </imginfo>
//
function handleResponse() {
    if(http.readyState == 4){ 
        var response = http.responseText;

	var doc = getXMLDocument(response);  
	
	if (g_bVerbose) {
	  alert("got response:"+response);
	}

        var x = doc.documentElement;	
	var aItems = x.getElementsByTagName("item");

	aData = new Array();
        iN = aItems.length;
	if (iN > 0) {
	  for (ii = 0; ii < iN; ii++) {

 	    sName = aItems[ii].getAttribute("id");
            if (aItems[ii].hasChildNodes()) { 
	      sVal = aItems[ii].firstChild.nodeValue;
            } else {
	      sVal = "";
            }
	    //   alert("aData[" + sName + "] : "+ sVal);
	    aData[sName] = sVal;    
	  }
	} else {
	  var aErrs = x.getElementsByTagName("error");
	  if (aErrs.length > 0) {
	    aData[0] = aErrs[0].firstChild.nodeValue;
	  } else {
	    var aOKs = x.getElementsByTagName("ok");
	    if (aOKs.length > 0) {
	      aData[0] = "OK";
	    }
	  }
	}
	  
	g_cbFunc(aData);

    }
}


//-----------------------------------------------
// createRequestObject
//   acquire and return a xmlhttprequest object
//
function createRequestObject(){
    var request_o; //declare the variable to hold the object.
    var browser = navigator.appName; //find the browser name
 
    if(browser == "Microsoft Internet Explorer"){
	// code for IE
	request_o = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
	// normal browsers
	request_o = new XMLHttpRequest();
    }
    return request_o; //return the object
}

//-----------------------------------------------
// getXMLDocument
//   extract and return a xml document from response
//
function getXMLDocument(response){
    // code for IE
    if (window.ActiveXObject) {
        var doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(response);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser=new DOMParser();
        var doc=parser.parseFromString(response,"text/xml");
    }
    return doc;
}

