﻿//structure Ajax Caller
function structureAjaxCaller()
{

 this.wsService = "/Batanga_WebServices/myRadio.asmx"; // default service
 this.wsNamespace = "http://webservices.batanga.com/MyRadio"; // default namespace
 this.strEnvelope = "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/'";

 this.maxCon = 10; // maximum connection for this object
 this.tailCon = 1; // initial tail
 this.HTTPCon = new Array; // array of connection

 // BEGIN Ajax Caller Helper
 
 this.lt = function(a,b) // less than helper. need this because of XML decode < into &lt;
 {
  if(a<b) { return true; } else { return false; }
 }
 
 this.lte = function(a,b) // less or equal than
 {
  if(a<=b) { return true; } else { return false; }
 }
  
 this.gt = function(a,b) // greater than
 {
  if(a>b) { return true; } else { return false; }
 } 

 this.gt = function(a,b) // greater or equal than 
 {
  if(a>=b) { return true; } else { return false; }
 } 
 
 // END Ajax Caller Helper
 
 // function to find one available connection
 // either find used empty or create new if available
 this.fine1Con  = function()
 {
   var i;

   // try to find 1 used empty (completed)
   for(i=1;i<this.tailCon;i++)
   {
    if(this.HTTPCon[i]) {
     if(this.HTTPCon[i].readyState==0 || this.HTTPCon[i].readyState==4)
      return i ; // return empty one
    }
   }

   // if can not 1 used empty, pick 1 new
   if(this.tailCon<=this.maxCon)
   {  

    // crate new object
    if (window.ActiveXObject) {
     this.HTTPCon[this.tailCon] = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
     this.HTTPCon[this.tailCon] = new XMLHttpRequest();
     this.HTTPCon[this.tailCon].overrideMimeType('text/xml');  // line from previous player    
    }   
          
    // if new object was created successfully
    if(this.HTTPCon[this.tailCon])
    {
     i=this.tailCon; 
     this.tailCon=this.tailCon+1;
     return i; // increase the tail of stack and exit
    } else {
     return false; // new object was failed (browser may not be ajax anable)
    }
   } else {
    return false; // maximum connection reached
   }

  }

 this.AjaxSend = function(strFName,strVars,willExecute,fnOnResult)
 {  
    var gotOne = this.fine1Con();
    if(gotOne)
    {
     if (this.HTTPCon[gotOne]) {
     
      this.HTTPCon[gotOne].abort();
      this.HTTPCon[gotOne].open("POST", this.wsService, true);
      this.HTTPCon[gotOne].setRequestHeader("SOAPAction",  this.wsNamespace + "/" + strFName);
      this.HTTPCon[gotOne].setRequestHeader("Content-Type", "text/xml; charset=utf-8");
      
      var soapText = "<?xml version='1.0' encoding='utf-8'?>" 
      + "<soap:Envelope " + this.strEnvelope + ">"  
      + "<soap:Body>" 
      + "<" + strFName + " xmlns='" + this.wsNamespace + "'>";
 
      var mySplitVar = strVars.split("#");
      var i;  
      if(mySplitVar.length>1)
      {
        for(i = 0; i < mySplitVar.length; i+=2){
	        if(mySplitVar[i+1]) {
	         soapText = soapText +  "<" + mySplitVar[i] + ">" + mySplitVar[i+1] + "</" + mySplitVar[i] + ">";
	        }
        }             
      }  
      
      soapText = soapText 
      + "</" + strFName + ">" 
      + "</soap:Body>" 
      + "</soap:Envelope>";
      
      //HTTPCon[gotOne].onreadystatechange = AjaxOnReturn;
      var savedObject = this;
      this.HTTPCon[gotOne].onreadystatechange = function () {
       if (savedObject.HTTPCon[gotOne].readyState == 4 && savedObject.HTTPCon[gotOne].status == 200) {
        savedObject.AjaxOnReturn(gotOne,strFName);
        var strJS=savedObject.getTheResult(strFName,savedObject.HTTPCon[gotOne].responseText);
        if(willExecute) savedObject.executeJS(strJS); // execute JS
        if(fnOnResult)  fnOnResult(strJS); // if there is a function need to run after done
       }
      }
      this.HTTPCon[gotOne].send(soapText);
     }
    } 
}  

 this.executeJS = function(strJS)
 {
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.text = strJS;
   script.id = 'JSOnTheFly';
   document.body.appendChild(script);
   var sc = document.getElementById('JSOnTheFly');
   document.body.removeChild(sc);
 } 
 
 // to get the only result from web service XML
 this.getTheResult = function(strFName,responseText)
 {
  var t1 = responseText.indexOf("<" + strFName + "Result>");
  var t2 = responseText.indexOf("</" + strFName + "Result>");
  if((t1>-1)&&(t2>-1)&&(t2>t1))
  {
   return responseText.substring(t1+8+strFName.length,t2);
  } else 
  {
   return "";
  }
 }
 
 this.AjaxOnReturn = function(gotOne,strName)
 {
   //alert('gotOne=' + gotOne + ' ' + strName + this.HTTPCon[gotOne].responseText); 
 }

 
 // init connection array
 var i;
 for(i=1;i<=this.maxCon;i++)
 {
  this.HTTPCon[i]=false;
 }

 
 
}



