var currentDiv2 = "";
var topHeading = "";
var queAllow = true;
var queBool = new Array(10);
var queUrl = new Array(10);
var queDivID = new Array(10);
var xmlHttp;
for (var i = 0; i < 10; i++) {
  queBool[i] = false;
  queUrl[i] = "";
  queDivID[i] = "";
}
 function loadPage(url,divID) {

   //Add a random variables to the url, this is a hack to prevent caching as IE tends to be unpredictable. The url is made unique by adding a random variable.
  if( url.indexOf("?") == -1){
   url+="?RandomVariable="+Math.floor(Math.random()*9999);
  }else{
   url+="&RandomVariable="+Math.floor(Math.random()*9999);
  }

  if (queAllow == true) {
    queAllow = false

    currentDiv = divID

    xmlHttp = new GetXmlHttpObject()
    if (xmlHttp==null) {
      alert ("Browser does not support HTTP Request")
      return
    }
    xmlHttp.onreadystatechange=stateChanged

    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
  } else {
    i = 0;
    while (queBool[i] == true) {
      i++;
    }
    queAllow = false
    queBool[i] = true
    queUrl[i] = url
    queDivID[i] = divID
  }
}
function stateChanged() {
  if (xmlHttp.readyState==1) {
  }
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

   if( currentDiv  ){
            var StrHtml = xmlHttp.responseText;
            Process_Data(StrHtml,currentDiv);



   }

    queAllow = true
    i = 0;
    while (queBool[i] == false) {
      i++;
    }
    if (queBool[i]==true) {
      queBool[i] = false

      loadPage(queUrl[i],queDivID[i])
      queUrl[i] = ""
      queDivID[i] = ""
    }
  }

}

function Process_Data(StrHtml,currentDiv){
            Process_Tags(StrHtml);
            Load_Code(StrHtml,currentDiv);
}

function GetXmlHttpObject(handler) {
  var objXMLHttp = null
  if (window.XMLHttpRequest) {
    objXMLHttp = new XMLHttpRequest()
  }
  else if (window.ActiveXObject) {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
  }
  return objXMLHttp
}

function Load_Code(StrHtml,currentDiv){

   //Append new Javascript
var elem = currentDiv;
var newSpan = document.createElement('span');
newSpan.id = elem.id;

newSpan.innerHTML = StrHtml;
if( currentDiv.childNodes ){
 for (var i = 0; i < currentDiv.childNodes.length; i++) {
   var n = currentDiv.childNodes[i]
   n.parentNode.removeChild(n)
 }
}
currentDiv.innerHTML = '';
currentDiv.appendChild(newSpan);


    //Append new Javascript

   //if( frames.parent ){ //If it's in an Iframe then add the javascript to the parent window so that it will fire
   //   var h = frames.parent.document.getElementsByTagName('head')[0];
  // }else{
    var h = document.getElementsByTagName('head')[0];

  // }


    var scripts = currentDiv.getElementsByTagName('script');

            for(var i=0;i<scripts.length;i++){

                            if( scripts[i] != '' ){

                              var x = document.createElement('script');

                             if( scripts[i].text != '' ){
                              x.text = scripts[i].text;
                               h.appendChild(x);
                              h.removeChild(x);
                             }


                            }
            }

}

function Process_Tags(StrHtml){
  // if( frames.parent ){ //If it's in an Iframe then add the javascript to the parent window so that it will fire
  //    var h = frames.parent.document.getElementsByTagName('head')[0];
 // }else{
    var h = document.getElementsByTagName('head')[0];

  // }

    /*var scripts = StrHtml.split('<script');
    for(var i=0; i < scripts.length;i++){
            if(scripts[i].indexOf('src')>-1 && scripts[i].indexOf('.src')==-1){

                var tmpARY = scripts[i].split('src="');

                tmpARY[1] = tmpARY[1].substr(0,tmpARY[1].indexOf('"'));

                                 var x = document.createElement('script');
                                 x.type = "text/javascript";
                                 x.src = tmpARY[1];

                              h.appendChild(x);
                            //  h.removeChild(x);
            }
    } */


    var stylesheets = StrHtml.split('<link');

            for(var i=0;i<stylesheets.length;i++){
             if(stylesheets[i].indexOf('href')>-1){

                var tmpARY = stylesheets[i].split('href="');
             if( tmpARY.length > 1 ){
                tmpARY[1] = tmpARY[1].substr(0,tmpARY[1].indexOf('"'));

                            if( stylesheets[i] != '' ){

                              var x = document.createElement('link');

                                 x.type = "text/javascript";
                                 x.href = tmpARY[1];
                                 x.rel = 'stylesheet';
                                 x.type = 'text/css';
                                 x.media='screen';
                              h.appendChild(x);
                             // h.removeChild(x);
                            }
                            
              }
            }
           }
}


