// <xscript src='json.js'></xscript>

// ENV SETTINGS /////////////////////////////////////////
//
   var varicom_call_url = url_root+'/varicom.php';
   var varicom = new varicom_object();
// var varicom_debug_func = false;
//
/////////////////////////////////////////////////////////

function varicom_object()
{
   this.com = new Array(); // comunicators
   
   this.set = function(t,v,e)
     {
     //alert('set: '+t+ " [" + v + "] " + e)
     var c = this.selectCom();
     c["t"] = t;
     c["v"] = v;
     c["e"] = e;
     c["mode"] = 'set';
  
     c.c.open('POST',varicom_call_url,true);
     c.c.onreadystatechange = c.func;   
     c.c.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
     var ts = "t="+encodeURIComponent(t)+"&v="+encodeURIComponent(v.toJSONString());
     c.c.send(ts);
     //alert(ts)
     }//func
   
   
   this.get = function(t,e)
     {
     var c = this.selectCom();
     c["t"] = t;
     c["v"] = false;
     c["e"] = e;
     c["mode"] = 'get';
  
     c.c.open('POST',varicom_call_url,true);
     c.c.onreadystatechange = c.func;   
     c.c.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
     var ts = "m=get&t="+encodeURIComponent(t);
     c.c.send(ts);
     }
   
   this.selectCom = function()
   {// figure a com handler to use (create one or resuse old one)
    // - need some work. (see how to reuse)
   var res = false;
   var func = false;
   var c = false;
   for(i in this.com)
      {
      var c = this.com[i];
      //alert(i + " " + listProps(this.com[i]));
      //if(c.free)   /// figure out how to reuse these
         //res = c;
      }//for
   if(res === false)
     {//make new one
     c = this.xmlHttpReqObj();
     func = new Function("varicomDeltaCom('"+this.com.length+"')");
     this.com.push({"free" : false, "c" : c,"func" : func});
     res = this.com[this.com.length-1]
     }//if
     
   return res;
   }//func
   
this.xmlHttpReqObj = 
   function()
    {
    var xhr;
    if(window.ActiveXObject)
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      else
        xhr = new XMLHttpRequest();
    return xhr;
    }//func   
   
}//func

function varicomDeltaCom(i)
{ // assumes global varicom object instance
var c = varicom.com[i];
var xcom = c.c;
var s = xcom.readyState;
var r = xcom.responseText;
var res = null;
//alert(i + " s:"+s);

if(s == 4)
    {
    r = xcom.responseText;
    //alert("varicomDelta: r= ["+r+"]")
   
    if(r.length == 0 || false) //should check here if valid json object.
           res = null;
        else
           res = eval("("+r+")");
           
    if(c.mode == 'set')
        {
        if(c.e)
          c.e(c.t,res,r);
        }//if
    else
       if(c.mode = 'get')
         {
           if(c.e)
             c.e(c.t,res,r);
         }//if
        
    c.free = true;    
    }//if
    
 
    
}//func

// function eSet(t,r,txt)
// {// on set complete event
//  //t = original tag requested (direct string)
//  //r = result object
//  //txt (opt) = raw text output of php call (good for debugging)
// }

// function eGet(t,v,txt)
// {//t = original tag requested
//  //v = value object (most recent)
//  //txt (opt) = raw text output of php call (good for debugging)
// 

// function eRes(t,r,txt)
// {//t = original tag requested
//  //r = result object (most recent)
// }


// varicom.set("tag",{"a" : 5, "n" : 22},eRes);



// xmlHttpReqObj = 
//   function()
//    {
//    var xhr;
//    if(window.ActiveXObject)
//        xhr = new ActiveXObject("Microsoft.XMLHTTP");
//      else
//        xhr = new XMLHttpRequest();
//    return xhr;
//    }//func   
