﻿// Copyright    :   True Potential LLP 2009
// Author       :   Paul Outterside
// Created      :   22/09/2009


// FUNCTION LIST :
//
// getBrowser()
// left(str, n)
// mid(str, start, len)
// inStr(str, charSearch)
// httpReq()
// setHtml(targetItem, content)
// changeContentProxy(paramString)
// changeContent(sourceFile, paramString, targetItem, useRandom, loadMessage, spinnerType, parseScript, errorMessage, fromStartMenu, parentCall)
// showSub(bufferHeight, paneWidth)
// hideSub()
// checkOverlay()
// showWhirl()

var winWidth;
var winHeight;

//  #####################
//  BROWSER DETECT
function getBrowser() {
	var browserString;
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		browserString = "other";
		} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		browserString = "ie6+";
		} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		browserString = "ie4"
		}
	return browserString;
}


// ###########################################
// LEFT STRING FUNCTION
function left(str, n) {
	// Invalid bound, return blank string
	if (n <= 0) {
		return "";
		} else if (n > String(str).length) {
			// Invalid bound, return
			return str;
			} else {
			// Valid bound, return appropriate substring
			return String(str).substring(0,n);
			}
}


// #################################
// mid(string, start(ZERO BASED!!), length)
//
// Returns a specified number of characters from a string
function mid(str, start, len) {
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) return "";
	
	var iEnd, iLen = String(str).length;
	
	if (start + len > iLen) {
		iEnd = iLen;
		} else {
			iEnd = start + len;
			}

	return String(str).substring(start,iEnd);
}


// #################################
// inStr(strSearch, charSearchFor)
// 
// InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
// was found in the string str.  (If the character is not found, -1 is returned.)
// Requires use of: Mid function
function inStr(str, charSearch) {
	for (i=0; i < str.length; i++) 	{
	    if (charSearch == mid(str, i, 1)) {
		return i;
	    }
	}
	return -1;
}




//  ##############################################
//  FUNCTION TO SET UP THE XMLHTTPREQUEST FOR AJAX
//
//  as different browsers use different XMLHTTP methods,
//  this function ensures the right one is used.
function httpReq() {

	var returnItem=null

	try {
		returnItem = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
				try {
				returnItem = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
				//
				}
			}

	if (returnItem==null) {
		returnItem = new XMLHttpRequest();
		}


	// Now do the actual thing
	if (returnItem==null) {
		alert('Sorry, your browser does not support the HTTP request method.');
		return
		} else {
		return returnItem
		}

}


// #####################################################
// PARSE THE RETURNED TEXT TO ALLOW SCRIPT BLOCKS TO RUN
// pass in the target div id and the AJAX returned text

function setHtml(targetItem, content) {
	var search = content;
	var script;

	var is_singleq = 0; var singleq = "'";
	var is_doubleq = 0; var doubleq = '"';
	var is_escaped = 0; var escap = "\\";
	var layer = 0;

	while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/)) {
		search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);
		if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;
		block = search.substr(0, search.indexOf(RegExp.$1));
		search = search.substring(block.length + RegExp.$1.length);

	while(func = block.match(/(function(.+?)\((.*?)\)\s*\{)/)) {
		eval(block.substr(0,block.indexOf(RegExp.$1)));
		// for evaluating non functions

	block = block.substr(block.indexOf(RegExp.$1) + RegExp.$1.length);
	name = RegExp.$2;
	param = RegExp.$3;

	is_singleq = 0;
	is_doubleq = 0;
	is_escaped = 0;
	layer = 0;


	for(i=0;i<block.length;i++) {
		c = block.substr(i,1);

		if ((is_singleq || is_doubleq) && is_escaped) {
			is_escaped = 0;
			} else if (!is_doubleq && (c==singleq)) {
			is_singleq = !is_singleq;
			} else if (!is_singleq && (c==doubleq)) {
			is_doubleq = !is_doubleq;
			} else if ((is_singleq || is_doubleq) && (c==escap)) {
			is_escaped = 1;
			} else if ( c=="{") {
			layer++;
			} else if ( c=="}") {
			if ( layer==0 ) {
			break;
			}
			layer--;
			}
		}

		code = block.substr(0,i-1);
		block = block.substr(i +1);

		code = code.replace(/\n/g, '\\n');
		code = code.replace(/\r/g, '\\r');
		code = code.replace(/'/g,"\\'");

		eval(name + " = new Function('"+param+"','"+code+"');");
	}
	eval(block); // for evaluating non functions
	}
	document.getElementById(targetItem).innerHTML = content;
}


//	#################################################################
//	AJAX CALL PROXY FUNCTION
//
//	Allows reduced parameter calls to the changeContent function
//	Call this proxy with all parameters on the QS
function changeContentProxy(paramString) {

	// example call : sourceFile=http://www.6point4.com/blog.aspx|qs=&param1=jijd&param2=odihf|targetItem=proxyTarget|useRandom=1|loadMessage=|spinnerType=small|parseScript=1|fromStartMenu=1|errorMessage=You messed up|parentCall=0

	var itemCount = 0;
	var sourceFile = "";
	var qs = "";
	var targetItem = "";
	var useRandom = 1;
	var loadMessage = "";
	var spinnerType = "small";
	var parseScript = 1;
	var errorMessage = "";
	var fromStartMenu = 0;
	var parentCall = 0;
	var topMargin = 0;
	
	var paramArray = paramString.split("|");
	
	while (itemCount < paramArray.length) {
		
		// sort the source file
		if (left(paramArray[itemCount], 10) == "sourceFile") {
			sourceFile = mid(paramArray[itemCount], 11, paramArray[itemCount].length);
			}
		
		// sort any QS parameters
		if (left(paramArray[itemCount], 2) == "qs") {
			qs = mid(paramArray[itemCount], 3, paramArray[itemCount].length);
			}
		
		// sort targetItem
		if (left(paramArray[itemCount], 10) == "targetItem") {
			targetItem = mid(paramArray[itemCount], 11, paramArray[itemCount].length);
			}
		
		// sort useRandom
		if (left(paramArray[itemCount], 9) == "useRandom") {
			useRandom = mid(paramArray[itemCount], 10, paramArray[itemCount].length);
			}
		
		// sort loadMessage
		if (left(paramArray[itemCount], 11) == "loadMessage") {
			loadMessage = mid(paramArray[itemCount], 12, paramArray[itemCount].length);
			}
		
		// sort spinnerType
		if (left(paramArray[itemCount], 11) == "spinnerType") {
			spinnerType = mid(paramArray[itemCount], 12, paramArray[itemCount].length);
			}
		
		// sort parseScript
		if (left(paramArray[itemCount], 11) == "parseScript") {
			parseScript = mid(paramArray[itemCount], 12, paramArray[itemCount].length);
			}
		
		// sort errorMessage
		if (left(paramArray[itemCount], 12) == "errorMessage") {
			errorMessage = mid(paramArray[itemCount], 13, paramArray[itemCount].length);
			}
		
		// sort fromStartMenu
		if (left(paramArray[itemCount], 13) == "fromStartMenu") {
			fromStartMenu = mid(paramArray[itemCount], 14, paramArray[itemCount].length);
			}
		
		// sort parentCall (for use when calling this function in parent from iFrame)
		if (left(paramArray[itemCount], 10) == "parentCall") {
			parentCall = mid(paramArray[itemCount], 11, paramArray[itemCount].length);
			}

		// sort manual top margin
		if (left(paramArray[itemCount], 9) == "topMargin") {
			topMargin = mid(paramArray[itemCount], 10, paramArray[itemCount].length);
			}
		
		itemCount++;
		}
		
		// alert out the info for debugging
		/*alert(sourceFile);
		alert(qs);
		alert(targetItem);
		alert(useRandom);
		alert(loadMessage);
		alert(spinnerType);
		alert(parseScript);
		alert(errorMessage);
		alert(fromStartMenu);
		alert(parentCall);
		alert(topMargin);*/

	// now finally call the function
	// changeContent(sourceFile, qs, targetItem, useRandom, loadMessage, spinnerType, parseScript, errorMessage, fromStartMenu, parentCall);
	setTimeout('changeContent(\'' + sourceFile + '\', \'' + qs + '\', \'' + targetItem + '\', ' + useRandom + ', \'' + loadMessage + '\', \'' + spinnerType + '\', ' + parseScript + ', \'' + errorMessage + '\', ' + fromStartMenu + ', ' + parentCall + ', ' + topMargin + ')', 100);	

}


//  #################################################################
//  FUNCTION TO PERFORM THE AJAX REQUEST TO CHANGE THE CONTENT WINDOW
//
//  pass in the file that is to be included, plus any parameter string
// format the parameters like this : &thing=blah&thing=blah...
function changeContent(sourceFile, paramString, targetItem, useRandom, loadMessage, spinnerType, parseScript, errorMessage, fromStartMenu, parentCall, topMargin) {

	// create http variable based on random number so multiple can be used
	var loadingItem;
	var ranNumber = Math.floor(Math.random() * 100000+1);
	var httpObj = eval['http' + ranNumber];

	// now set the top margin value for the spinner image
	if (topMargin == 0) {
		topMargin = winHeight-176;
		topMargin = topMargin/2;
		// deduct half the spinner.gif height!!
		topMargin = topMargin-16;
		}

	// set up the loading message
	if (loadMessage != "") {
		loadingItem = loadMessage;
		} else {
		if (spinnerType == "small") {
			loadingItem = "<center><img src='/resources/images/loaderSmall.gif' style='margin-top : " + topMargin + "px;' /></center>";
			} else {
			loadingItem = "<center><img src='/resources/images/loaderLarge.gif' style='margin-top : " + topMargin + "px;' /></center>";
			}
		}

	// check the browser type so we can position the spinner in the centre
	var winHeight = 0;
	if (getBrowser() == "other") {
		//Non-IE
		winHeight = window.innerHeight;
		} else if (getBrowser() == 'ie6+') {
		//IE 6+ in 'standards compliant mode'
		winHeight = document.documentElement.clientHeight;
		} else if (getBrowser() == 'ie4') {
		//IE 4 compatible
		winHeight = document.body.clientHeight;
		}
	
	// now set the thing to be the loading item
	try {
		document.getElementById(targetItem).innerHTML = loadingItem;
		} catch(e) {
		document.write('<br />' + e.message + ' - ' + targetItem + ' - ' + sourceFile);
		}

	// set up the XMLHttp variable
	httpObj=httpReq();
	
	// modify the path to the source file if parentCall = 1
	if (parentCall == 1) {
		if (getBrowser() == 'other') {
			sourceFile = "" + sourceFile;
			}
		}

	// call the page and add a random number to prevent using a cached one if reqd!
	var url;
	if (useRandom == 1) {
		url=sourceFile + "?ran=" + Math.random() + '&' + paramString;
		} else {
			url=sourceFile + "?" + paramString;
			}

	//  call the stateChanged function
	httpObj.onreadystatechange = function () {
		if (httpObj.readyState==4||httpObj.readyState=="complete") {
			// check the status of the return (200 should be ok)
			if (httpObj.status==200) {				
				try {
					//setHtml(targetItem, eval['http' + ranNumber].responseText);
					if (parseScript == 1) {
						setHtml(targetItem, httpObj.responseText);
						} else {
						document.getElementById(targetItem).innerHTML = httpObj.responseText.replace('\'', '\'');
						}
					} catch(e) {
					document.write('e : ' + e.message);
					}
				} else {
				if (errorMessage != "") {
					document.getElementById(targetItem).innerHTML = errorMessage;
					} else {
					document.getElementById(targetItem).innerHTML = "<img src='/resources/images/icons/cross.gif' />";
					}
				}
			}
		}
	httpObj.open("GET",url,true);
	httpObj.send(' ');

}


// ########################################
// FUNCTIONS TO SHOW/HIDE POPUP WINDOW ALTERNATIVES
function showSub(bufferHeight, paneWidth) {
	var winWidth;
	var winHeight;
	var xScroll;
	var yScroll;
	
	// get the display window dimensions
	if (getBrowser() == 'other') {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
		} else if (getBrowser() == 'ie6+') {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
		} else if (getBrowser() == 'ie4') {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
		}
	
	// now get the value of the scrolling dimensions
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			// all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			} else {
			// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
			}

	document.getElementById("popOverlay").style.display = "block";
	if (getBrowser() == 'other') {
		document.getElementById("popOverlay").style.width = winWidth-0 + "px";
		document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
		} else {
		document.getElementById("popOverlay").style.width = winWidth-0 + "px";
		document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
		}


	if (yScroll > winHeight) {
		document.getElementById("popOverlay").style.height = yScroll + "px";
		document.getElementById("popHideSelect").style.height = yScroll + "px";
		if (getBrowser() == 'other') {
			document.getElementById("popOverlay").style.width = winWidth-17 + "px";
			document.getElementById("popHideSelect").style.width = winWidth-17 + "px";
			} else {
			document.getElementById("popOverlay").style.width = winWidth-0 + "px";
			document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
			}
		} else {
		document.getElementById("popOverlay").style.height = winHeight + "px";
		document.getElementById("popHideSelect").style.height = winHeight + "px";
		if (getBrowser() == 'other') {
			document.getElementById("popOverlay").style.width = winWidth-0 + "px";
			document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
			} else {
			document.getElementById("popOverlay").style.width = winWidth-0 + "px";
			document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
			}
		}

	document.getElementById("popHideSelect").style.display = "block";
	document.getElementById("popWindow").style.display = "block";
	document.getElementById("popWindow").style.width = paneWidth + "px";
	document.getElementById("popWindow").style.height = bufferHeight + "px";
	document.getElementById("popWindow").style.top = (winHeight/2)-(bufferHeight/2) + "px";
	document.getElementById("popWindow").style.left = (winWidth/2)-(paneWidth/2) + "px";
}


// HIDE THE POPUP ALTERNATIVE
function hideSub() {
	document.getElementById("popWindow").innerHTML = '&nbsp;';
	document.getElementById("popOverlay").style.display = "none";
	document.getElementById("popHideSelect").style.display = "none";
	document.getElementById("popWindow").style.display = "none";
}


// CHECK OVERLAY
function checkOverlay() {
	var winWidth;
	var winHeight;
	var popWinHeight;
	popWinHeight = document.getElementById("popWindow").style.height;
	popWinHeight = parseFloat(popWinHeight.replace(/px/i, ""));
	if (isNaN(popWinHeight)) {
		popWinHeight = 0;
		}

	// get the display window dimensions
	if (getBrowser() == 'other') {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
		} else if (getBrowser() == 'ie6+') {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
		} else if (getBrowser() == 'ie4') {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
		}

	// now get the value of the scrolling dimensions
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			// all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			} else {
			// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
			}

	if (getBrowser() == 'other') {
		document.getElementById("popOverlay").style.width = winWidth-0 + "px";
		document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
		} else {
		document.getElementById("popOverlay").style.width = winWidth-0 + "px";
		document.getElementById("popHideSelect").style.width = winWidth-0 + "px";
		}
	if (yScroll > winHeight) {
		document.getElementById("popOverlay").style.height = yScroll + "px";
		document.getElementById("popHideSelect").style.height = yScroll + "px";
		} else {
		document.getElementById("popOverlay").style.height = winHeight + "px";
		document.getElementById("popHideSelect").style.height = winHeight + "px";
		}
	
	document.getElementById("popWindow").style.top = (winHeight/2)-(popWinHeight/2) + "px";
	document.getElementById("popWindow").style.left = (winWidth/2)-360 + "px";
}


// showWhirl
function showWhirl() {
	var overlay = document.getElementById("whirlOverlay");
	var hideSelect = document.getElementById("whirlHideSelect")

	// get the display window dimensions
	if (getBrowser() == 'other') {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
		} else if (getBrowser() == 'ie6+') {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
		} else if (getBrowser() == 'ie4') {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
		}

	// now get the value of the scrolling dimensions
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			// all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			} else {
			// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
			}

	overlay.style.display = "block";
	hideSelect.style.display = "block";	
	if (getBrowser() == 'other') {
		overlay.style.width = winWidth-0 + "px";
		hideSelect.style.width = winWidth-0 + "px";
		} else {
		overlay.style.width = winWidth-0 + "px";
		hideSelect.style.width = winWidth-0 + "px";
		}
	if (yScroll > winHeight) {
		overlay.style.height = yScroll + "px";
		hideSelect.style.height = yScroll + "px";
		} else {
		overlay.style.height = winHeight + "px";
		hideSelect.style.height = winHeight + "px";
		}

	}

























