﻿function ajax(array) {
    var url =  array[0][0];
    var elementId = array[0][1];
    var loadingIcon = array[0][2];
    var functionToActivate = array[0][3];
    if(functionToActivate != '')
    {
        eval(functionToActivate);
    }
    var http = false;
	if(navigator.appName == "Microsoft Internet Explorer")
	{
	    http = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else 
	{
	    http = new XMLHttpRequest();
	}

	http.open("GET", url, true);
    if(elementId != null && elementId != '')
    {
        if(loadingIcon == 'True' && document.getElementById(elementId) != null)
        {
          document.getElementById(elementId).innerHTML  = "<div style='width:50px;height:50px;background:url(../images/style/ajaxLoading.gif) center center no-repeat;'></div>";
        }    
    }   
    http.onreadystatechange=function() 
    {
        if(http.readyState == 4) {

            if(elementId != null && elementId != '' && document.getElementById(elementId) != null)
            {               
                document.getElementById(elementId).innerHTML = http.responseText; 
                try {

		            if(typeof thickBoxInit == 'function') {
                         thickBoxInit();
                    } 
                    AjaxReActivateJavascript(document.getElementById(elementId));
		        } catch(e) {}

            }

            if(array.length > 1)
            {
                
                var newArray = new Array();
                for(i=0;i<array.length-1;i++)
                {
                    newArray[i] = array[i+1];
                }
                ajax(newArray)
            }  
        }
    }
    http.send(null);
}
function redirectToUrl(url) {
    location.href = url;
}
// Reactivate javascripts
function AjaxReActivateJavascript(element)
{
    var scriptElements = new Array();
    var divElements = new Array();
    if(element.childNodes != null)
    {    
        var elementScript = null;
        for(var i=0;i<element.childNodes.length;i++)
        {
            if(element.childNodes[i].tagName == 'SCRIPT')
            {
                scriptElements.push(element.childNodes[i]);
                            
            }
            if(element.childNodes[i].tagName == 'DIV')
            {
                divElements.push(element.childNodes[i]);

            }
        }
        // Activate scripts
        for(var i=0;i<scriptElements.length;i++)
        {
            var newScript = document.createElement('SCRIPT');
            newScript.type = 'text/javascript';
            if(scriptElements[i].src == "")
            {
                newScript.text = scriptElements[i].innerHTML;
            }
            else
            {
                newScript.src =  scriptElements[i].src;
            }
            element.appendChild(newScript);
            element.removeChild(scriptElements[i]); 
        }
        // Search next DIV
        for(var i=0;i<divElements.length;i++)
        {
            AjaxReActivateJavascript(divElements[i]);
        }
    }
}


