﻿// JScript File
var IUI_Boxes = new Array()

function IUI_Box(ID, URL, Location)
{
    this.ID = ID;
    this.URL = URL;
    this.Location = Location;
    this.CurContent = 0;
    this.LoopNum = 0;
}

IUI_Request.prototype.ID;
IUI_Request.prototype.Value;
IUI_Request.prototype.Location;
IUI_Request.prototype.Box;
IUI_Request.prototype.CurContent;

// Load up boxes
// 2/7/08 - I don't belive this is in use. The call come in with IUI_AddBox_Direct
function IUI_AddBox(Location, BoxID)
{ 
 var url = "http://" + location.host + "/ws/Box/Box.aspx?ID=" + BoxID; //"http://www.iupdateit.com/ws/Box/Box.aspx?ID=" + BoxID;
 //alert(url);
 var newBox = new IUI_Box(BoxID, url, Location);
 var req = new IUI_Request(newBox);
 
 IUI_Boxes[IUI_Boxes.length] = newBox;
 
 //alert(dumpObj1(IUI_Boxes, "boxes"))
 
// var fname = "debugdiv";
//    document.getElementById(Location).InnerHtml = "erp"; 

/*
    var el = document.getElementById(Location)
    if (el != null)
        el.InnerHtml = "Loading...";
 */
     

 //alert(req.URL);
 req.Run();
}

function IUI_AddBox_Direct(BoxID, Location, BoxContents)
{ 
    var newBox = new IUI_Box(BoxID, "", Location);
    
//            el = document.getElementById(Location)
//            el.innerHTML = "[" + BoxContents + "]"    
            
    newBox.Box = eval("(" + BoxContents + ")");
    IUI_Boxes[IUI_Boxes.length] = newBox;
    IUI_DisplayBox(newBox.ID)
}

function IUI_Request(Box)
{
    this.Request = new ajax();
    this.Box = Box;
    
//    this.Run();
   
}

IUI_Request.prototype.Box;
IUI_Request.prototype.Request;

IUI_Request.prototype.Run = function()
{
    // Need to pull these down to get this to work
    var req = this.Request;
    var box = this.Box;
    
    // Do the request
//    alert("url: " + this.Box.URL);
    req.open("GET", this.Box.URL, true);
    req.onreadystatechange=function() 
    {
        // Process the request
        if (req.readyState==4) 
        {
//        alert("4: " +  req.responseText);
        
        
//            el = document.getElementById(box.Location)
//            el.innerHTML = "[" + req.responseText + "]"
            box.Box = eval("(" + req.responseText + ")");
            IUI_DisplayBox(box.ID)
//            el.innerHTML = "<HR/>" + dumpObj1(box, "test2"); // + "\n\n" + req.responseText;            


//   var myObj = eval("({'one' : '1', 'two' : '2', 'three' : '3', 'four' : '4', 'five' : '5'})");

        }
    }
    
    //alert(this.Location)
  
    req.send(null);
}

function IUI_DisplayBox(ID)
{
    var el2 = document.getElementById("IUI_debugdiv");
    
    for (num in IUI_Boxes)
    {
        var box = IUI_Boxes[num];
        
        if (box.ID == ID)
        {
            // Get the control
            var el = document.getElementById(box.Location)
                          
            if (el == null && document.all)
                el = document.all(box.Location)                
             
            // Get the proper content    
            if (el != null)        
                el.innerHTML = box.Box.Content[box.CurContent].Contents;
            else
                alert("Could not find: " + box.Location);
                
            // debug display    
            if (el2 != null)
                el2.innerHTML = el2.innerHTML + dumpObj1(box, "box");                
                
                
            var duration = box.Box.Content[box.CurContent].Duration                       
     
            box.CurContent = box.CurContent + 1;
            if (box.CurContent > box.Box.Content.length - 1)
            {
                box.CurContent = 0;
                box.LoopNum = box.LoopNum + 1;
            }
                
            if (box.Box.Content.length == 1)
                // Only one, don't need a timer
                return;
                
            if (box.Box.ExecutionType > 1 && box.LoopNum > box.Box.ExecutionType - 2)
                // Limited # of loops, we are done
                return;                            
                
            // Set up next one
            setTimeout("IUI_DisplayBox('" + box.ID + "')", duration * 1000)
            return;
        }
    }
    
    alert("BoxID " + ID + ", not found")
}




function IUI_AddBox_old(Location, BoxID)
{
 var xmlhttp = new ajax();
 // HEAD request
 xmlhttp.open("GET", "BoxItT2.aspx?ID=" + BoxID + "&OP=1", true);
 
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   //alert(xmlhttp.responseText)
   el = document.getElementById(Location)
   
   var myObj = eval("(" + xmlhttp.responseText + ")");
   el.innerHTML = dumpObj(myObj, "test2")
  }
  }
 

 xmlhttp.send(null)
}


// xmlhttp
function ajax()
{
    var xmlhttp;
    
    if (typeof XMLHttpRequest!='undefined') 
    {
	    try 
	    {
		    xmlhttp = new XMLHttpRequest();
		    return xmlhttp;
	    } 
	    catch (e) 
	    {
		    xmlhttp=false;
	    }
    }
    
    if (window.createRequest) 
    {
	    try 
	    {
		    xmlhttp = window.createRequest();
		    return xmlhttp;
	    } 
	    catch (e) 
	    {
		    xmlhttp=false;
	    }
    }

    try
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        return xmlhttp;
    }
	catch (e) 
	{
	    xmlhttp=false;
	}  
    
    return xmlhttp
}


// Debug

var MAX_DUMP_DEPTH = 10;

       function dumpObj1(obj, name)
       {
        return dumpObj(obj, name, "", 0)
        }

       function dumpObj(obj, name, indent, depth) {
              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + "[" + name + "]<BR>";

                     indent += "&nbsp;&nbsp;&nbsp;&nbsp;";

                     for (var item in obj)

                     {
                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "<BR>";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }
