﻿function CreateXmlHttpRequest()
{
 var xmlhttp=false;
 // JScript gives us Conditional compilation, we can cope with old IE versions.
 // and security blocked creation of the objects.
  try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }

 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  try {
   xmlhttp = new XMLHttpRequest();
  } catch (e) {
   xmlhttp=false;
  }
 }
 if (!xmlhttp && window.createRequest) {
  try {
   xmlhttp = window.createRequest();
  } catch (e) {
   xmlhttp=false;
  }
 }
 return xmlhttp;
}

function getListItems(siteUrl, listName, container_id, xsl_path) {
 
var xmlhttp = CreateXmlHttpRequest();
if (xmlhttp)
{
 var request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
 request    += "  <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >";
 request    += "  <soap:Body>";
 request    += "    <GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">";
 request    += "      <listName>" + listName + "</listName>";
 request    += "      </GetListItems>";
 request    += "  </soap:Body>";
 request    += "</soap:Envelope>";

 xmlhttp.open("POST", siteUrl + "/_vti_bin/Lists.asmx?op=GetListItems", true);
 xmlhttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
 xmlhttp.setRequestHeader("SOAPAction","http://schemas.microsoft.com/sharepoint/soap/GetListItems");
 xmlhttp.setRequestHeader("Content-Length", request.length);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   RenderHtmlOutput(xmlhttp.responseText, container_id, xsl_path);
  }
 }
 xmlhttp.send(request);
 }
}

function RenderHtmlOutput(responseXML, container_id, xsl_path)
{
   var output = '';
   // code for IE
   if (window.ActiveXObject)
   {
   		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  		xmlDoc.async="false";
  		xmlDoc.loadXML(responseXML.replace('<?xml version="1.0" encoding="utf-8"?>',''));

  		var xmlXsl=new ActiveXObject("Microsoft.XMLDOM");
  		xmlXsl.async="false";
  		xmlXsl.load(xsl_path);
  
     	output = xmlDoc.transformNode(xmlXsl);
     	
     	var el = document.getElementById(container_id)
   		if (el)
  	 		el.innerHTML = output.replace('<?xml version="1.0" encoding="utf-16"?>','');
   }
   // code for Mozilla, Firefox, Opera, etc.
   else if (document.implementation && document.implementation.createDocument)
   {
   		parser = new DOMParser();
   		
   		var xmlDoc = document.implementation.createDocument("","",null);
		xmlDoc = parser.parseFromString(responseXML, "text/xml");

   		var xmlXsl = document.implementation.createDocument("","",null);
   		xmlXsl.async = false;
		xmlXsl.load(xsl_path);
		
		xsltProcessor=new XSLTProcessor();
     	xsltProcessor.importStylesheet(xmlXsl);

     	output = xsltProcessor.transformToFragment(xmlDoc,document);

		document.getElementById(container_id).appendChild(output);
   }
}
