// the id of the grid div
var DivId = false;
var ai_co = 0;
// url s XML
var feedMethod = "GET";

// url s XML
var feedUrl = "";

// url s XML
var feedParams = "";

// the XSLT document
var stylesheetDoc;

// the XSLT document
var callbackFX;


// eveything starts here
function ajax_post(elementName,url,params,xsltFileUrl,callbackFunction)
{
    if (DivId != false) {
        setTimeout(function() { ajax_post(elementName,url,xsltFileUrl,callbackFunction); }, 50);
        return;
    }
    callbackFX = callbackFunction;
    DivId = elementName;
    feedUrl = url;
    feedParams = params;
    feedMethod = "POST";

    // test if user has browser that supports native XSLT functionality
    if(window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser)
    {
        // load the grid
        loadStylesheet(xsltFileUrl);
        loadPageUN();
        return;
    }
    // test if user has Internet Explorer with proper XSLT support
    if (window.ActiveXObject && createMsxml2DOMDocumentObject())
    {
        // load the grid
        loadStylesheet(xsltFileUrl);
        loadPageUN();
        // exit the function
        return;    
    }
    // if browser functionality testing failed, alert the user
    alert("Your browser doesn't support the neccessary functionality.");
}


// eveything starts here
function ajax_init(elementName,url,xsltFileUrl,callbackFunction)
{
    if (DivId != false) {
        setTimeout(function() { ajax_init(elementName,url,xsltFileUrl,callbackFunction); }, 50);
        return;
    }
    
    ts_now = new Date(); 
    callbackFX = callbackFunction;
    DivId = elementName;
    feedUrl = url+'&ts='+ts_now.getTime();
    feedParams = null;
    feedMethod = "GET";

    // test if user has browser that supports native XSLT functionality
    if(window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser)
    {
        // load the grid
        loadStylesheet(xsltFileUrl);
        loadPageUN();
        return;
    }
    // test if user has Internet Explorer with proper XSLT support
    if (window.ActiveXObject && createMsxml2DOMDocumentObject())
    {
        // load the grid
        loadStylesheet(xsltFileUrl);
        loadPageUN();
        // exit the function
        return;    
    }
    // if browser functionality testing failed, alert the user
    alert("Your browser doesn't support the neccessary functionality.");
}


// makes asynchronous request to load a new universal page
function loadPageUN()
{
  // continue only if the XMLHttpRequest object isn't busy
  if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0))
  {
    var query = feedUrl;// + "?action=FEED_GRID_PAGE&page=" + pageNo;
    //InfoMessageAdd('q '+query);
    xmlHttp.open(feedMethod, query, true);
    xmlHttp.onreadystatechange = handlePageLoadUN;
    if (feedMethod == "POST") {
        xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
        xmlHttp.setRequestHeader("Content-length", feedParams.length);
    }
    xmlHttp.send(feedParams);
  }  
}

// handle receiving the server response with a new page of products
function handlePageLoadUN()
{ 
  // when readyState is 4, we read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      // read the response
      response = xmlHttp.responseText;
      // server error?
      if (response.indexOf("ERRNO") >= 0 
          || response.indexOf("error") >= 0
          || response.length == 0)
      {
	//InfoMessageAdd(response);
        // display error message
        //alert(response.length == 0 ? "Server serror." : response);
        // exit function
        return;
      }
      // the server response in XML format
      xmlResponse = xmlHttp.responseXML;        
      // browser with native functionality?    
      if (window.XMLHttpRequest && window.XSLTProcessor && 
          window.DOMParser)
      {      

        // load the XSLT document
        var xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(stylesheetDoc);
        // generate the HTML code for the new page of products
        page = xsltProcessor.transformToFragment(xmlResponse, document);
        // display the page of products
        
        if (DivId.nodeType == 1)
            var Div = DivId;
        else
            var Div = document.getElementById(DivId);
        if (Div) {
            Div.innerHTML = "";
            Div.appendChild(page);
        }
        DivId = false;
        if (callbackFX)
            callbackFX();
      } 
      // Internet Explorer code
      else if (window.ActiveXObject) 
      {
        // load the XSLT document
        var theDocument = createMsxml2DOMDocumentObject();
        theDocument.async = false;
        theDocument.load(xmlResponse);
        // display the page of products
        if (DivId.nodeType == 1)
            var Div = DivId;
        else
            var Div = document.getElementById(DivId);
        if (Div) {
            Div.innerHTML = theDocument.transformNode(stylesheetDoc);
        }
        else { alert('hhh'); }
        //InfoMessageAdd(ai_co+'axsl');ai_co++;
        DivId = false;
        if (callbackFX)
            callbackFX();
      }
    } 
    else 
    {          
      //alert("Error reading server response.")
    }
  }
}


