﻿function ajaxDomainHandler(destDomain)
{
ajaxDivHandler(destDomain, "ajaxDiv", "/pubs" );
}
function ajaxDivHandler(destDomain, ajaxDiv, subDir)
{
var resultSet = document.getElementById( (ajaxDiv?ajaxDiv:"ajaxDiv") );
var destDom = (destDomain?destDomain.replace( new RegExp( "^.*http://([a-zA-Z0-9\.]*)/.*\.(htm|aspx|html|asp\cgi\pl\jsp).*$"), "$1" ):"stowlonga.org.uk");
/* This extracts the destination domain from the url that includes the agent script at the front and possibly parameters at the back */
/* in passing through the server, urls are seeded with the server url. so if it started as slash path slash page, it now has a url
*  that includes the local host - stowlonga.org.uk. We need to parse the element and replace the local host, with the destination host. */
//resultSet.innerHTML = parseScript( resultSet.innerHTML );
if (resultSet)
 {
 var iframes = resultSet.getElementsByTagName( "IFRAME" );
 var images = resultSet.getElementsByTagName( "IMG" );
 var anchors = resultSet.getElementsByTagName( "A" );
 var forms = resultSet.getElementsByTagName( "FORM" );
 var scripts = resultSet.getElementsByTagName("SCRIPT");
 var links = resultSet.getElementsByTagName("LINK");
 var link;
 var re=new RegExp( (subDir?location.hostname+subDir:location.hostname), "ig" );

 for ( var i=0; i< anchors.length; i++)
  {
	link = anchors[i].href.replace( re, destDom );
	anchors[i].href = link;
	anchors[i].target = "_blank";
  }

 for ( var i=0; i< images.length; i++)
  {
	link = images[i].src.replace( re, destDom );
	images[i].src = link;
  }
 /* finally, the elements that do not have http type urls need updating. */
 resultSet.innerHTML = resultSet.innerHTML.replace( /(action="|')\//gi, "$1http://"+destDom + "/");
 resultSet.innerHTML = resultSet.innerHTML.replace( /(src="|')\//gi, "$1http://"+destDom + "/");

 for ( var i=0; i < 2/*scripts.length*/; i++)
  {
	installScript( scripts[i] );
  }

 for ( var i=0; i< links.length; i++)
  {
	if (links[i].href.indexOf(".css")!=-1)
       { //If object is a css file
       var linkOb = document.createElement("link")
       linkOb.setAttribute("rel", "stylesheet");
       linkOb.setAttribute("type", "text/css");
       linkOb.setAttribute("href", links[i].href);
       }
   }
 }
}

/** Execute a script in the global context. This installs all functions
    defined in this script into the global scope, unless they are
    explicitly created in different scopes.

    @param script   the actual script DOM element
 **/
function installScript( script )
{
    if (!script)
        return;

    if (script.src)
    {
        var head = document.getElementsByTagName("head")[0];
        var scriptObj = document.createElement("script");

        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("src", script.src);  

        head.appendChild(scriptObj);

    }
    else if (script.innerHTML)
    {
        //  Internet Explorer has a funky execScript method that makes this easy
        if (window.execScript)
            window.execScript( script.innerHTML );
        else
            window.setTimeout( script.innerHTML, 0 );
    }
}

/*	function parseScript(_source) {
		var source = _source;
		var scripts = new Array();
		
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
			
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		
		// Return the cleaned source
		return source;
	}

var loadedobjects="";
function loadObjs()
{
if (!document.getElementById)
  return
for (i=0; i<arguments.length; i++)
  {
  var file=arguments[i]
  var fileref=""
  if (loadedobjects.indexOf(file)==-1)
    { //Check to see if this object has not already been added to page before proceeding
    if (file.indexOf(".js")!=-1)
      { //If object is a js file
      fileref=document.createElement('script')
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", file);
      }
    else
      if (file.indexOf(".css")!=-1)
       { //If object is a css file
       fileref=document.createElement("link")
       fileref.setAttribute("rel", "stylesheet");
       fileref.setAttribute("type", "text/css");
       fileref.setAttribute("href", file);
       }
    }
  if (fileref!="")
    {
    document.getElementsByTagName("head").item(0).appendChild(fileref)
    loadedobjects+=file+" " //Remember this object as being already added to page
    }
  }
}
*/