var xmlHttp = createXmlHttpRequestObject();

var feedGridUrl = si.baseUrl + "grid.php";
var gridDivId = "gridDiv";
var statusDivId = "statusDiv";
var tempRow;
var editableId = null;
var stylesheetDoc;
var xsltFileUrl;

//
// Firefox XSLTProcessor fix
//

XSLTProcessor._styleSheetFix = null;
XSLTProcessor.prototype._importStylesheet = XSLTProcessor.prototype.importStylesheet;
XSLTProcessor.prototype.importStylesheet = function(oStylesheet)
{
	// preload empty stylesheet document "factory"
	if (!this.constructor._styleSheetFix){
			var oXMLHttpRequest = new XMLHttpRequest();
			oXMLHttpRequest.open('GET', si.baseUrl + 'xsl/stylesheet.xsl', false);
			oXMLHttpRequest.send(null);
			this.constructor._styleSheetFix = oXMLHttpRequest.responseXML;
	}
	// replace documentElement in "factory" document with new stylesheet
   
	this.constructor._styleSheetFix.replaceChild(oStylesheet.documentElement.cloneNode(true), this.constructor._styleSheetFix.documentElement);
	// import stylesheet to processor
	return this._importStylesheet(this.constructor._styleSheetFix);
}

function init(xsltFile, ipageNo, isortBy, isortOrder)
{

  if(!xsltFile) {
	  xsltFileUrl = si.baseUrl + "xsl/grid.xsl";
  } else {
	  xsltFileUrl = xsltFile;
  }
  if(!ipageNo) {
	  ipageNo = 1;
	  alert('no 1');
  }
  if(!isortBy) {
	  isortBy = 'flash_id';
	  alert('no 2');
  }
  if(!isortOrder) {
	  isortOrder = 'ASC';
	  alert('no 3');
  }
  
  if(window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser)
  {
	  
    loadStylesheet();
    loadGridPage(ipageNo, isortBy, isortOrder);
	
    return;
  }
  
  if (window.ActiveXObject && createMsxml2DOMDocumentObject())
  {
	  
    loadStylesheet();
    loadGridPage(ipageNo, isortBy, isortOrder);
	
    return;  
  }
  
  alert("Your browser doesn't support the necessary functionality.");
}

function createMsxml2DOMDocumentObject()
{
	
  var msxml2DOM;
  
  var msxml2DOMDocumentVersions = new Array("Msxml2.DOMDocument.6.0",
                                            "Msxml2.DOMDocument.5.0",
                                            "Msxml2.DOMDocument.4.0");
  
  for (var i=0; i<msxml2DOMDocumentVersions.length && !msxml2DOM; i++) 
  {
    try 
    { 
	
      msxml2DOM = new ActiveXObject(msxml2DOMDocumentVersions[i]);
    } 
    catch (e) {}
  }
  
  if (!msxml2DOM)
    alert("Please upgrade your MSXML version from \n" + 
          "http://msdn.microsoft.com/XML/XMLDownloads/default.aspx");
  else 
    return msxml2DOM;
}


function createXmlHttpRequestObject() 
{
	
  var xmlHttp;
 
 
  try
  {
	  
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
	  
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
	
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
	  
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function loadStylesheet()
{
	
  xmlHttp.open("GET", xsltFileUrl, false);        
  xmlHttp.send(null);
  
  if (this.DOMParser)
  {
    var dp = new DOMParser();
    stylesheetDoc = dp.parseFromString(xmlHttp.responseText, "text/xml");
  } 
  else if (window.ActiveXObject)
  {
    stylesheetDoc = createMsxml2DOMDocumentObject();         
    stylesheetDoc.async = false;         
    stylesheetDoc.load(xmlHttp.responseXML);
  }
}

function loadGridPage(pageNo, sortBy, sortOrder)
{
	
  editableId = false;
  
  if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0))
  {
	if(sortBy == undefined) {
		sortBy = 'flash_id';
	}
	if(sortOrder == undefined) {
		sortOrder = 'ASC';
	}
	if(pageNo == undefined) {
		pageNo = 1;
	}
	
	var query = feedGridUrl + "?action=FEED_GRID_PAGE&sortby=" + sortBy + "&sortorder=" + sortOrder + "&page=" + pageNo;
	
    xmlHttp.open("GET", query, true);
    xmlHttp.onreadystatechange = handleGridPageLoad;
    xmlHttp.send(null);
	
  }  
}

function handleGridPageLoad()
{
	
  if (xmlHttp.readyState == 4)
  {
	  
    if (xmlHttp.status == 200)
    {    
	
      response = xmlHttp.responseText;
	  
      if (response.indexOf("ERRNO") >= 0 
          || response.indexOf("error") >= 0
          || response.length == 0)
      {
		  
        alert(response.length == 0 ? "Server error." : response);
		
        return;
      }
	  
      xmlResponse = xmlHttp.responseXML;
	  
      if (window.XMLHttpRequest && window.XSLTProcessor && 
          window.DOMParser)
      {
		var xsltProcessor = new XSLTProcessor();
		
		xsltProcessor.importStylesheet(stylesheetDoc);
		
		page = xsltProcessor.transformToFragment(xmlResponse, document);
		
		var gridDiv = document.getElementById(gridDivId);
		gridDiv.innerHTML = "";
		gridDiv.appendChild(page);
        
      }
      else if (window.ActiveXObject) 
      {
		  
        var theDocument = createMsxml2DOMDocumentObject();
        theDocument.async = false;
        theDocument.load(xmlResponse);
		
        var gridDiv = document.getElementById(gridDivId);
        gridDiv.innerHTML = theDocument.transformNode(stylesheetDoc);
      }
    } 
    else 
    {          
      alert("Error reading server response.")
    }
  }
}
