topical media & game development

talk show tell print

mashup-amazon-10-10-02-Wishlist-js-request.js / js



  // JScript File
  var gRequest;
  var gXsl = new Array;
  var gCurrentXsl = 0;
  var gTargetDiv = "results";
  var gXml;
  
  function Lookup(ItemId){
      // Set the Xsl file to use when the data comes back
          GetData(2, "details", "api=amazon&operation=ItemLookup&ItemId=" + ItemId);
  }
  
  function ListSearch(){
      // Set the Xsl file to use when the data comes back    
      var FirstName = document.getElementById('txtFirstName').value;
          var LastName = document.getElementById('txtLastName').value;
          GetData(0, "results", "api=amazon&operation=ListSearch&ResponseGroup=ListInfo&ListType=WishList&FirstName="+ FirstName + "&LastName=" + LastName);
  }
  
  function ListLookup(ListId){
      // Set the Xsl file to use when the data comes back
          GetData(1, "lists", "api=amazon&operation=ListLookup&ResponseGroup=Large&ListType=WishList&ListId=" + ListId);
  }
  
  function DirectorSearch(keywords){
      // Set the Xsl file to use when the data comes back
      GetData(2, "results", "api=amazon&operation=ItemSearch&Director=" + keywords + "&SearchIndex=DVD");
  }
  
  function YouTube(keywords){
      // Set the Xsl file to use when the data comes back
      GetData(2, "results", "api=youtube&tag=" + keywords );
  }
  
  function EbaySearch(keywords){
      // Set the Xsl file to use when the data comes back
      GetData(5, "results", "api=ebay&query=" + keywords );
  }
  
  function ActorSearch(keywords){
      // Set the Xsl file to use when the data comes back    
          GetData(2, "results", "api=amazon&operation=ItemSearch&Actor=" + keywords + "&SearchIndex=DVD");
  }
  
  // this function actually performs the request for data. 
  function GetData(myXslIndex, myTarget, myOperation) {
  
      // Set some variables used in the response handler   
      gCurrentXsl = myXslIndex; 
      gTargetDiv = myTarget;
      
      // Show progress
      document.getElementById(gTargetDiv).innerHTML = "<h2>Loading...</h2>";
  
      var myCacheBreaker =Math.floor(Math.random()*10000);
      // Break the caching by attaching a random number. 
          var myUrl="getdata.aspx?" + myOperation + "&cacheBreaker=" + myCacheBreaker;
          
          // Is this a Microsoft browser?
          if (window.ActiveXObject) {
              // Create a new request
          myRequest = new ActiveXObject("Microsoft.XMLHTTP");
          if (myRequest) {
              myRequest.onreadystatechange = HandleResponse;
              myRequest.open("GET", myUrl, true);
              myRequest.send();
              
          }  
      } else if (window.XMLHttpRequest) {
          // If this is Firefox or Safari...        
          myRequest = new XMLHttpRequest();
          myRequest.onreadystatechange = HandleResponse;
          myRequest.open("GET", myUrl, true);
          myRequest.send(null);             
      }
      myPrint(myUrl);
  }
  
  function myPrint(msg){
    document.getElementById('debug').innerHTML += "<br/>" + msg;
  }
  
  function HandleResponse() {
  
      // Readystate 4 means we're done
      if (myRequest.readyState == 4) {
      
          // If the server returned OK
          if (myRequest.status == 200) {                         
              gXml = myRequest.responseXML;
              TransformResponse();
          } else {
              myDiv.innerHTML = "<br/>Oops there was a problem, " + myRequest.statusText;
              myDiv.innerHTML += "<br/>Try again later";
          }
      }
  }
  function Init(){
      // Branch the code to support either IE or Firefox
          if (window.ActiveXObject)
          {
          InitXslIE();
          }
          else
          {
          InitXslFF();
      }
      // Load all the stylesheets.
      LoadXsl();
  }
  
  // Load an array of XSL sheets
  function LoadXsl(){
      gXsl[0].load('xml/AmazonListSearch.xsl');
      gXsl[1].load('xml/AmazonListLookup.xsl');   
      gXsl[2].load('xml/AmazonYouTube.xsl');
      gXsl[3].load('xml/AmazonCartCreate.xsl');
      gXsl[4].load('xml/AmazonCartGet.xsl');
      gXsl[5].load('xml/ebaySearch.xsl');
  }
  // Initialize the XSL objects in Firefox
  function InitXslFF() {
     for (var i = 0; i< 6 ; i++ ){
        gXsl[i] = document.implementation.createDocument("","xsl",null);
     }
     
      // Finally the XML object
     gXml = document.implementation.createDocument("","xml",null);        
     gXml.async=false;
  }
  
  // Initialize the XSL in IE
  function InitXslIE() {    
     for (var i = 0; i< 6 ; i++ ){   
         gXsl[i] = new ActiveXObject("Msxml2.DomDocument"); 
     }   
     // setup the xml object. 
     gXml = new ActiveXObject("Msxml2.DomDocument");
     gXml.async=false; 
  }
  
  // Transform the XML data
  function TransformResponse()
  {        
      var myXsl = gXsl[gCurrentXsl];
      var myXml=gXml;
      var myResults;
      var myProcessor;     
      var myTarget = document.getElementById(gTargetDiv); 
           
      if (window.ActiveXObject)
      {               
         // IE specific transformation.    
         myResults = myXml.transformNode(myXsl); 
         myTarget.innerHTML = myResults;               
      }
      else
      {
          // Create Processor
          myProcessor = new XSLTProcessor();
          myProcessor.importStylesheet(myXsl);
                        
          myResults = myProcessor.transformToFragment(myXml, document);
  
          // Finally display the response
          myTarget.innerHTML = "";
          myTarget.appendChild(myResults);
      }       
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.