// JavaScript Document
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}
	function FixBackAndBookmarking() {
		initPage();
		if(!document.getElementById || !document.getElementsByTagName) return;
		if(document.iframesfix) {
			fix = new AjaxIframesFixer('ajaxnav');
		} else {
			fix = new AjaxUrlFixer();
		}
	}
	
function getContent(url)
{
	xmlGet(url, initializeAJAX);
}

function insertContent(strContent)
{
	var swappableSection = document.getElementById("content");
	swappableSection.innerHTML = strContent;
}

function insertPage(strContent)
{
	var swappableSection = document.getElementById("content");
	swappableSection.innerHTML = strContent;
	var strTitle = document.getElementById("page_title");
	if (strTitle) {
		var breadCrumb = document.getElementById("breadcrumb_title");
		breadCrumb.innerHTML = strTitle.innerHTML;
	}
	// Fix our Back and Forward buttons (located in menu.js)
	hijaxBackNext();

	// Highlight our menue where we are at (located in menu.js)
	highlightLocation();
}

function initializeAJAX()
{
     // Make sure the request is loaded (readyState = 4)
	 if (req.readyState == 1) { insertContent("");}
	 if (req.readyState == 2) { insertContent("Loading");}
	 if (req.readyState == 3) { insertContent("Loading...");}
     if (req.readyState == 4)
     {
         // Make sure the status is "OK"
         if (req.status == 200)
         {
			// Transform XML Document and push HTML to browser
			insertPage(req.responseText);
			findPos();
         }
         else
         {
             alert("There was a problem retrieving the data:\n" +
                 req.statusText);
         }
     }
 }

 
function xmlGet(url, responseHandler)
{
    xmlOpen("GET", url, null, responseHandler);
}

function xmlOpen(method, url, toSend, responseHandler)
{
    if (window.XMLHttpRequest)
    {
        // browser has native support for XMLHttpRequest object
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // try XMLHTTP ActiveX (Internet Explorer) version
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(req)
    {
        req.onreadystatechange = responseHandler;
        req.open(method, url, true);
        req.setRequestHeader("content-type","application/x-www-form-urlencoded");
        req.send(toSend);
    }
    else
    {
        alert('Your browser does not seem to support XMLHttpRequest.');
    }
}

// ******************************************************************************************
//								Enabling Back Button Control
//				thanks to: www.contentwithstyle.co.uk/Articles/38/

var defaultQS = 1;


function PageLocator(propertyToUse, dividingCharacter) {
    this.propertyToUse = propertyToUse;
    this.defaultQS = 1;
    this.dividingCharacter = dividingCharacter;
	this.fullHash = 0;
}
PageLocator.prototype.getLocation = function() {
    return eval(this.propertyToUse);	
}
PageLocator.prototype.getHash = function() {
    var url = this.getLocation();
    if(url.indexOf(this.dividingCharacter) > -1) {
        var url_elements = url.split(this.dividingCharacter);
        this.fullHash = url_elements[url_elements.length-1];
		
		// Check for anchor links
		if(this.fullHash.indexOf('a') > -1) {
			var anchor_elements = this.fullHash.split('a');
			// Here we use logic to determine if we are on the same page, and
			// a new anchor has been selected
			if (anchor_elements[0] == hash_anchor) {
				// we have remained on the same page
				if (tag_anchor != anchor_elements[anchor_elements.length-1]) {
					tag_anchor = anchor_elements[anchor_elements.length-1];
					findPos();
				}
			}	
			// found in anchor.js	
			tag_anchor = anchor_elements[anchor_elements.length-1];
			hash_anchor = anchor_elements[0];
			return anchor_elements[0];
		} else {
			tag_anchor = 0;
			hash_anchor = this.fullHash;
			return this.fullHash
		}
    } else {
        return this.defaultQS;
    }
}
PageLocator.prototype.getHref = function() {
    var url = this.getLocation();
    var url_elements = url.split(this.dividingCharacter);
    return url_elements[0];
}
PageLocator.prototype.makeNewLocation = function(new_qs) {
    return this.getHref() + this.dividingCharacter + new_qs;
}
PageLocator.prototype.getAnchor = function() {

}



function AjaxUrlFixer() {
    this.fixLinks(); 
    this.locator = new PageLocator("window.location.href", "#");
    this.timer = new Timer(this);
    this.checkWhetherChanged(0);
}
AjaxUrlFixer.prototype.fixLinks = function () {
    var links = document.getElementsByTagName("A");
    for(var i=0; i<links.length; i++) {
        var href = links[i].getAttribute("href");
		if (href)
		{
			if (href.indexOf("hash=") > -1)
			{
				var hash = href.substr(href.indexOf("hash=")+5);
				hash = hash.replace('#', 'a');
				links[i].setAttribute("href","#"+hash);
			}
		}
    }
}
AjaxUrlFixer.prototype.checkWhetherChanged = function(location){
    if(this.locator.getHash() != location) {
        doGetPage(this.locator.getHash());
    }
    this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
}

function doGetPage(i) {
	if(i) {
		if (i != 0)
		{
			getContent("content" + i + ".htm");
		}
	}
}

/* AjaxIframesFixer */
function AjaxIframesFixer(iframeid) {
	this.iframeid = iframeid;
	if (document.getElementById('ajaxnav')) {
		this.fixLinks();
		
		this.locator = new PageLocator("document.frames['"+this.iframeid+"'].getLocation()", "?hash=");
		this.windowlocator = new PageLocator("window.location.href", "#");
		this.timer = new Timer(this);
				
		this.delayInit(); // required or IE doesn't fire
				
	}
}
AjaxIframesFixer.prototype.fixLinks = function (iframeid) {
	var links = document.getElementsByTagName("A");
	for(var i=0; i<links.length; i++) {
		var href = links[i].getAttribute("href");
		if (href)
		{
			if (href.indexOf("hash=") > -1)
			{
				var hash = href.substr(href.indexOf("hash=")+5);
				hash = hash.replace('#', 'a');
				links[i].setAttribute("href","javascript:document.getElementById('"+this.iframeid+"').setAttribute('src', 'mock-page.asp?hash="+hash+"');");
			}
		}
	}
}
AjaxIframesFixer.prototype.delayInit = function(){
	this.timer.setTimeout("checkBookmark", 100, "");
}
AjaxIframesFixer.prototype.checkBookmark = function(){
	window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
	this.checkWhetherChanged(0);
}
AjaxIframesFixer.prototype.checkWhetherChanged = function(location){
	if(this.locator.getHash() != location) {
		
		doGetPage(this.locator.getHash());
		window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
	}
	this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
}
	/* END AjaxIframesFixer */