﻿// ajax InfoTabs
    var req;
    var responseContent;
    var start;
    var timeStart;
    var currentInfoTabId;
    var nextInfoTabId;
    var originalInfoTabId = 0;
    var prevInfoTabId;
    var tempNum = 0;
    
    var loadingImage = new Image();
    loadingImage.src = "/modules/infotabs/images/loadingsmall.gif";

    function loadNewContainerInfoTab(recordId) {
        currentInfoTabId = recordId;  
        
        //alert(recordId);
        
        // capture orignal InfoTab id for resets
        if (originalInfoTabId == 0) {
        	originalInfoTabId = currentInfoTabId;
        }
        
        //if the recordId is 0, reset to start
        if (currentInfoTabId == 0) {
        	currentInfoTabId = originalInfoTabId;
        }
        
	    var sURL = "/modules/infotabs/getinfotab.aspx?h=" + recordId;
       //alert(sURL);
        
        //# 2/3/2006 [sef] set our start time
        start = new Date();
        timeStart = start.getTime(); 

	    //# 2/3/2006 [sef] setup xml http request
	    req = GetXMLHttpObject();
   	    req.onreadystatechange = handleRequestInfoTab;	
        req.open("GET", sURL, true);
        req.send(null);  

    }
    

    
     function loadInfoTab(t, tabIndex, numTabs) {
        //alert('current InfoTab id = ' + currentInfoTabId + ', loading next InfoTab id ' + nextInfoTabId);
        loadNewContainerInfoTab(t);
        tempNum = 1;      
        selectTab(tabIndex, numTabs) 
    }
    
    function selectTab(tabIndex, numTabs) {
        for (i=1; i<=numTabs; i++) {
            if (tabIndex == i) {
               document.getElementById("tabLink" + i).className = "selected";
            } else {
               document.getElementById("tabLink" + i).className = "";
            }
        }
    }
    

    
    function handleRequestInfoTab() {
	     //# 2/3/2006 [sef] if the response has made it's way back from the server then validate it and take action
	 //alert(req.responseText);
         if (req.readyState == 4) {
              // only if "OK"
               if (req.status == 200) {
                   // ...processing statements go here...
                   //alert(req.responseText);
		           responseContent = req.responseText;
               } 
               else {
                    responseContent = "We apologize but there was an error processing your request.  Please <a href='javascript:window.location.reload()'>Try Again<\/a>.<p>Error Detail: There was a problem retrieving the XML data:\n" + req.statusText;
                    fillContainerInfoTab(responseContent);
                    
               }
            //# 2/3/2006 [sef] calculate how long the xml http operation took, make the response take 2 seconds so you get to see the gears ;)
            var end = new Date();
    	    var timeEnd = end.getTime();  
    	    var diff = timeEnd - timeStart;
    	    var waitTime = 0 - diff;
    			 //alert("record id " + currentInfoTabId + " loaded in " + diff + "ms");
        	
    		    if (waitTime > 0) {
    			    //document.getElementById("ajaxStat").innerHTML = "Execution Time: " + diff + "ms";
			        window.setTimeout("processResponseInfoTab();", waitTime);
    		    }
    		    else {
    			    processResponseInfoTab();
    		    }
          }
          else
          {
      	    setLoadingStatusInfoTab();
          }
    }
    
    function processResponseInfoTab() {
          var responseString = responseContent;
          var contentResults;
          var content;
    
          //alert(responseString);
          contentResults = responseString.split("$$$;$$$");
          //alert(contentResults.length);
          
         // alert('next id length = ' + contentResults[0].length);
         // check for no more InfoTabs
         if (contentResults[1].length == 0) {
            //document.getElementById("InfoTabsNext").className = "InfoTabsButton disabled";
            clearLoadingStatusInfoTab();
            //alert("empty InfoTab");
         }
         else {
            nextInfoTabId = contentResults[0];
            prevInfoTabId = currentInfoTabId;         
            content = contentResults[1];
            fillContainerInfoTab(content);                                                           
         }
    }
    
    function fillContainerInfoTab(txt) {
	    var container = document.getElementById("infotabbox");
	    container.innerHTML = txt;
	    clearLoadingStatusInfoTab();
    }
    
    function setLoadingStatusInfoTab() {
        document.getElementById("infotabbox").style.background = "url(/modules/infotabs/images/loadingsmall.gif) no-repeat 100% 0%";
        document.getElementById("infotabbox").innerHTML = "";
    }
    
    function clearLoadingStatusInfoTab() {
        document.getElementById("infotabbox").style.background = "";
    }
    
    
   


function GetXMLHttpObject()
{
    var oXMLHttp = null;

    try
    {
        oXMLHttp = new ActiveXObject("MSXML2.XMLHTTP");
    }
    catch (E)
    {
        try
        {
            oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
          oXMLHttp = null;
        }
    }

    if ((oXMLHttp == null) && (typeof(XMLHttpRequest) != 'undefined'))
    {
        oXMLHttp = new XMLHttpRequest();
    }

    return oXMLHttp;
}

