var xmlurl = "";
var xslurl = "";
var xmlcontent = "";
var xslcontent = "";
var xmldom = null;

function trim(str){
   var v1 = str.replace(/^(\s*)/, '');
   return v1.replace(/\s*$/, '');         
}
function toDOM(str) {
    str = trim(str);
	if (document.implementation.createDocument){
		var parser = new DOMParser();
		return parser.parseFromString(str, "text/xml");
	} else if (window.ActiveXObject){
		var dom = new ActiveXObject("Msxml2.DOMDocument")
		dom.async="false";
		dom.loadXML(str);
		return dom;
	}

}
function filterResult(){
   // TODO: get the XSL stylesheet as a string
var myXslStylesheet;

// TODO: get the XML DOM data document
var myXmlData;

// init a processor
var myXslProc;

// init the final HTML
var finishedHTML = "";

// create a XSLT processor
if(document.implementation.createDocument) {
    // Mozilla has a very nice processor object
    
    myXslProc = new XSLTProcessor();
    
    // convert the XSL to a DOM object first
    var parser = new DOMParser();
    myXslStylesheet = parser.parseFromString(xslcontent, "text/xml");
    
    
    // attach the stylesheet; the required format is a DOM object, and not a string
    myXslProc.importStylesheet(myXslStylesheet);
    
    // do the transform (domDocument is the current HTML page you're on)
    var fragment = myXslProc.transformToFragment(xmldom, document);
    // create a DOM container and insert offline
    var tmpBox = document.createElement("div");
    tmpBox.appendChild(fragment);
    
    // grab the innerHTML and write to output, and insert into HTML document
    finishedHTML = tmpBox.innerHTML;
    
    
} else {
    // IE requires a couple more hoops to jump through
   
    // first create a DOM document
    var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
   
    // then create an XSLT template document
    var xslTemplate = new ActiveXObject("Msxml2.XSLTemplate");
    xslDoc.async = false;
    // load the stylesheet into the DOM document
    xslDoc.loadXML(trim(xslcontent));
   
    // attach that DOM document to the XSLT template
    xslTemplate.stylesheet = xslDoc;
    
    // get the rental-model XSLT processor object
    
       myXslProc = xslTemplate.createProcessor();
    
    // feed it the XML data
     myXslProc.input = xmldom;
     var c = myXslProc.transform();
    // grab the output, and insert into HTML document
    finishedHTML = myXslProc.output;    
    
}	

$('result').innerHTML = finishedHTML;
}

function getXslContent(){	
 new Ajax.Request(xslurl,
  {
    method:'get',
    onSuccess: function(transport){
      xslcontent = transport.responseText || "no response text";
      
      if(xslcontent.length > 50){
      	
      	filterResult();
      } else {

      }
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}  

function getXmlContent(){
    new Ajax.Request(xmlurl,
  {
    method:'get',
    onSuccess: function(transport){
      xmlcontent = transport.responseText || "no response text";
      xmldom = toDOM(xmlcontent);
      if(xmlcontent.length > 50){
      	getXslContent();
      } else {
      }
    },
    onFailure: function(){ alert('Something went wrong...') }
  });

}


function showResult(){
   if(xslurl.length > 0 && xmlurl.length > 0){
   	getXmlContent();
   }	 	
}