/*
	$HeadURL: svn://hebe/Customers/WyeUsk/trunk/includes/tinyajax/ajax_functions.js $
	$Id: ajax_functions.js 1984 2008-01-24 16:16:24Z qbnut $
*/

/* ---------- VARS ---------- */

var lasthash = '';
var delay = 750;

/* ---------- INIT ---------- */

window.onload = function() {

	if (getbrowser() == "Microsoft Internet Explorer") {
		/* -- START IE IFRAME HISTORY HACK -- */
		/*
			The IFrame needs something in to start with, otherwise
			there is no "History" generated for the Process Entry page...
		*/
		var ihack = document.getElementById("iframe-hack");
		var ihdoc = ihack.contentWindow.document;
		var ihash = ihdoc.body.innerHTML;
		ihdoc.open();
		ihdoc.write("<html><body>#ps_0</body></html>");
		ihdoc.close();
		lasthash = '#ps_0';
		/* -- END IE IFRAME HISTORY HACK -- */
	}
	
	set_stage();
	setInterval("set_stage()", 1000);
}

/* ---------- FUNCTIONS ---------- */

function aj_post_call(func_name) {

	/*
		This call has been hooked in to run JS code post-AJAX.
		This is good for switching elements that should be hidden by default off (but
		only after the page it affects actually exists!), and repositioning window
		only after the page has loaded, not before.
	*/

	/*
		Hide Popup on page change...
	*/

	element = document.getElementById("popup");
	if (element != null) {
		element.style.visibility = 'hidden';
		element.style.display = 'none';
	}

	/*
		IE only lets you set a Hash to an anchor that actually exists. 

		Discovered on: http://www.unfocus.com/Projects/FlashSuite/:

		"IE 5.0 Windows: Setting the hash (window.location.hash) will generate a history if
		there is a matching anchor in the html (which means you have to generate one first)."

		DON'T USE document.write() to "generate" an anchor - Mozilla blows up if you do.
		Make sure they are in the Templates.
	*/

	var process_number = Number(func_name.substr(func_name.lastIndexOf("_") + 1));
	if (process_number != NaN) {
		
		if (process_number > 0) {
			window.location.hash = func_name;
		}

		if (getbrowser() == "Microsoft Internet Explorer") {

			/* -- START IE IFRAME HISTORY HACK -- */
			/* See Brad Neuberg's Weblog for the inspiration: http://codinginparadise.org/ */

			/* alert("Writing " + newhash + " to IFrame"); */
			var ihack = document.getElementById("iframe-hack");
			var ihdoc = ihack.contentWindow.document;
			var ihash = ihdoc.body.innerHTML;
			/*
				Check if the new page is greater than the old one. Only write
				to the IFrame if this is the case to prevent double entries...
			*/
			var prev_process_number = ihash.substr(ihash.lastIndexOf("_") + 1);
			if (Number(prev_process_number) != NaN && Number(process_number) != NaN) {
				if (process_number > prev_process_number) { 
					ihdoc.open();
					ihdoc.write("<html><body>#ps_" + process_number + "</body></html>");
					ihdoc.close();
				}
			}
			/* -- END IE IFRAME HISTORY HACK -- */
		} 
	}
}

function set_stage(stage) {

	var newhash = '';

	// --------------------------------------------------------------

	// TRIGGER 1: Manually Set by Parameter
	if (stage != undefined && stage != NaN) {

		newhash = '#ps_' + stage;
		// Always trigger if the stage is set
		var trigger = '1';

	} 
	// --------------------------------------------------------------

	// TRIGGER 2: Page change detection by Standard "window.location.hash" method
	if (newhash == '') {
		if (window.location.hash != lasthash && lasthash != '#ps_0') {

			newhash = window.location.hash;

			if (getbrowser() == "Microsoft Internet Explorer") {
				/* -- START IE IFRAME HISTORY HACK -- */
				var ihack = document.getElementById("iframe-hack");
				var ihdoc = ihack.contentWindow.document;
				if (newhash != '') {
					/*
						Update the IFrame to prevent page "bouncing", but don't create
						a new history entry using open/write/close...
					*/
					ihdoc.body.innerHTML = newhash;
				}
				/* -- END IE IFRAME HISTORY HACK -- */
			}

			if (newhash != lasthash) {
				var trigger = '2';
			}
		}
	}
	// --------------------------------------------------------------

	// TRIGGER 3: Page change detection by IE IFrame Hack 
	if (getbrowser() == "Microsoft Internet Explorer" && newhash == '') {

		/* -- START IE IFRAME HISTORY HACK -- */
		/* See Brad Neuberg's Weblog for the inspiration: http://codinginparadise.org/ */
		/*
			window.location does not get reset if the Back/Fwd button is pressed in IE
			so the DOM never matches what is actually being displayed in the browser window 
			if the Back/Fwd navigation buttons are used. This bug in IE prevents Trigger #2
			from firing correctly. Thus, we have to to use this ugly hack to work around this fact.
		*/
		var ihack = document.getElementById("iframe-hack");
		var ihdoc = ihack.contentWindow.document;
		var ihash = ihdoc.body.innerHTML;

		newhash = ihash;

		if (newhash != lasthash) {
			var trigger = '3';
		}

		/* -- END IE IFRAME HISTORY HACK -- */
	}
	// --------------------------------------------------------------
	
	// Enable for Debug ONLY!
	// window.status = "newhash = " + newhash + ", lasthash = " + lasthash + ", ihash = " + ihash + ", trigger = " + trigger;

	if (trigger != undefined) {

		/* alert("hash changed from '" + lasthash + "' to '" + newhash + "'"); */
		
		if (newhash != '') {
			lasthash = newhash;
			switch(newhash) {
				case '#ps_0':
					return process_beat();
					break;
				case '#ps_1':
					return process_bookingdates();
					break;
				case '#ps_2':
					return process_rodtypes();
					break;
				case '#ps_3':
					return process_confirm();
					break;
			}
		} else if (newhash == '' && lasthash != '') {
			lasthash = newhash;
			// return process_beat();
		} else {
			lasthash = newhash;
		}
	} 
}

