function rollover( el )
{	
	if ( el.getAttribute( "vOver" ) != null && el.getAttribute( "vOver" ) != "" )
		el.src = el.getAttribute( "vOver" );
}
		
function rollout( el )
{
	if ( el.getAttribute( "vOut" ) != null && el.getAttribute( "vOut" ) != "" )
		el.src = el.getAttribute( "vOut" );
}

function rolloverDiv( el )
{	
	if ( el.getAttribute( "vOver" ) != null && el.getAttribute( "vOver" ) != "" )
		el.style.backgroundImage = "url(" + el.getAttribute( "vOver" ) + ")";
}
		
function rolloutDiv( el )
{	
	if ( el.getAttribute( "vOut" ) != null && el.getAttribute( "vOut" ) != "" )
		el.style.backgroundImage = "url(" + el.getAttribute( "vOut" ) + ")";
}
				
function exitDemoURL(eURL) 
{
	response = confirm('You will now be redirected to the LloydsTSB’s website.\nAre you sure that you wish to exit the demonstration?');
	if (response==true) {
		window.open(eURL);
		top.close();
	}
}

		
function setButton( sName, sFunction, sOver, sOut, alt )
{
	var el = document.getElementById( sName );
	if ( el )
	{
		var f = null;
		if ( sFunction != "" )
		{
			f = eval( "window." + sFunction );
			if ( !f || typeof f != "function" )
				f = null;
		}
				
		el.onclick = f;
		el.style.cursor = f ? "hand" : "default";
		el.src = sOut;
		el.setAttribute( "vOver", sOver );
		el.setAttribute( "vOut", sOut );
		el.setAttribute( "alt", alt );
								
	}
}

function displayWT()
{
	var html = '<img src="./images/vSIMPLE/whatsthis.gif" vOut="./images/vSIMPLE/whatsthis.gif" vOver="./images/vSIMPLE/whatsthiso.gif" onmouseover="rollover( this );" onmouseout="rollout( this );" id="wtButton" alt="Switch on What\'s this function" style="cursor:hand;">';
	return html;
}

function styleSheetDisabled() 
{
   var i, a, b = false;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
   {
     if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.disabled) 
	 {
	   b = true;
     }
   }
   return b;
}


function displayMin()
{
	var html = "";
	
	if (collapsed == "true")
	{
		html = '<div id="hide" title="Toggle the display of the help panel" vOver="./images/vSIMPLE/hideo.gif" vOut="./images/vSIMPLE/hide.gif" onmouseover="rolloverDiv( this );" onmouseout="rolloutDiv( this );" onclick="hidePanel();" style="cursor:hand;background:transparent url(./images/vSIMPLE/show.gif) no-repeat top left;"></div>'
	}
	else
	{
		html = '<div id="hide" title="Toggle the display of the help panel" vOver="./images/vSIMPLE/hideo.gif" vOut="./images/vSIMPLE/hide.gif" onmouseover="rolloverDiv( this );" onmouseout="rolloutDiv( this );" onclick="hidePanel();" style="cursor:hand;"></div>'
	}

	html += '<a href="howto.html" id="home_link" onClick="location.href=this.href+\'?collapsed=\'+(collapsed ? collapsed : \'false\'); return false;"><div id="gb_home" title="Return to the homepage of the simulation" vOver="./images/vSIMPLE/gb_homeo.gif" vOut="./images/vSIMPLE/gb_home.gif" onmouseover="rolloverDiv( this );" onmouseout="rolloutDiv( this );" style="cursor:hand;"></div></a>';	

	html += '<div id="gb_whatsthis" title="Toggle the What\'s This mode on and off" vOver="./images/vSIMPLE/gb_whatsthiso.gif" vOut="./images/vSIMPLE/gb_whatsthis.gif" onmouseover="rolloverDiv( this );" onmouseout="rolloutDiv( this );" style="cursor:hand;"></div>';	

	return html;
}

function backupClicks( el, bBackup )
{
	var elParent = el;
	var vDefault = false;
	while ( elParent.tagName.toUpperCase() != "BODY" )
	{			
		if ( bBackup )
			elParent.oldclick = elParent.onclick;
		
		if ( !vDefault || bBackup )
			elParent.onclick = null;
			
		elParent = ( elParent.parentElement ) ? elParent.parentElement : elParent.parentNode;
	}
}

function restoreClicks( el )
{
	var elParent = el;
	while ( elParent.tagName.toUpperCase() != "BODY" )
	{
		if ( elParent.getAttribute( "oldclick" ) != null && elParent.getAttribute( "oldclick" ) != "" )
			elParent.onclick = elParent.oldclick;
			
		elParent = ( elParent.parentElement ) ? elParent.parentElement : elParent.parentNode;
	}
}


function findParentElementByTagName( el, tagName )
{
	var elParent = el;

	if ( el.tagName.toUpperCase() == tagName.toUpperCase() )
	{
		return el;
	}

	while ( elParent.tagName.toUpperCase() != "HTML" && elParent.tagName.toUpperCase() != tagName.toUpperCase() )
	{

		elParent = ( elParent.parentElement ) ? elParent.parentElement : elParent.parentNode;
	}

	if ( elParent.tagName == tagName.toUpperCase() )
	{
		return elParent;
	}
	else
	{
		return;
	}
}

function getRealElement( el )
{
	if ( el == null )
	{
		return null;
	}
	
	if ( el.tagName )
	{
		return el;
	}
	
	var elParent = el;
	
	while ( !elParent.tagName )
	{
		if ( !elParent.parentElement && !elParent.parentNode )
		{
			return	null;
		}
		
		elParent = ( elParent.parentElement ) ? elParent.parentElement : elParent.parentNode;
	}
	return elParent;
}

function catchEvent(e)
{
	if ( !e ) var e = window.event;

	el = getRealElement( e.srcElement || e.target );
		
	var a = findParentElementByTagName( el, "A" );
	if ( a )
	{
		var theID = a.id;
	}
	else
	{
		var theID = el.id;
	}
	
	//alert(theID);
	if (theID == "wtButton" || theID == "gb_whatsthis")
	{
		document.displayWhatsThis = ( document.displayWhatsThis ? false : true );
		if (document.displayWhatsThis)
		{
			setButton( "wtButton", "doNothing", "./images/vSIMPLE/whatsthisstopo.gif", "./images/vSIMPLE/whatsthisstop.gif", "Switch off What's this function");	
			setButton( "gb_whatsthis", "doNothing", "./images/vSIMPLE/gb_whatsthisstop.gif", "./images/vSIMPLE/gb_whatsthisstopo.gif", "Switch off What's this function");	
		}
		else
		{
			setButton( "wtButton", "doNothing", "./images/vSIMPLE/whatsthiso.gif", "./images/vSIMPLE/whatsthis.gif", "Switch on What's this function");
			setButton( "gb_whatsthis", "doNothing", "./images/vSIMPLE/gb_whatsthiso.gif", "./images/vSIMPLE/gb_whatsthis.gif", "Switch on What's this function");
		}
		
	}
	else if (document.displayWhatsThis)
	{
		if (whatsThisText[theID]) 
		{
			alert(whatsThisText[theID]);
		}
		else
		{
			alert("Sorry, no help is available for this item.");
		}
		cancelEvent( e );
	}
}

function watchClick( e )
{
	if ( !e ) var e = window.event;
	if ( !e.srcElement && e.target ) e.srcElement = e.target;
	
	el = getRealElement( e.srcElement );
	if ( el == null )
	{
		cancelEvent( e );
		return false;
	}
	
	if (document.displayWhatsThis)
	{
		backupClicks( el, true );
	}
	else
	{
		restoreClicks( el );
	}
}

function cancelEvent( e )
{
	if ( !e ) {var e = window.event;}

	if ( e )
	{
		e.cancelBubble	= true;
		e.returnValue	= false;
		if ( e.preventDefault ) e.preventDefault();
		if ( e.stopPropagation ) e.stopPropagation();
	}
	
	return false;
}

function hidePanel()
{
	collapsed = true;
	var el = document.getElementById( "help_panel" );
	el.style.width = 30 + "px";
	el.style.left = -220 + "px";
		
	el = document.getElementById( "the_page" );
	el.style.left = 30 + "px";
	
	setButton( "hide", "showPanel", "./images/vSIMPLE/showo.gif", "./images/vSIMPLE/show.gif", "Toggle the display of the help panel");
	document.getElementById( "hide" ).oldclick = showPanel;
	//if (document.getElementById( "hide" ).oldclick)	alert("Hide function"+document.getElementById( "hide" ).oldclick);
	
	document.getElementById( "gb_home" ).style.visibility = "visible";
	document.getElementById( "gb_whatsthis" ).style.visibility = "visible";
}

function showPanel()
{
	collapsed = false;
	var el = document.getElementById( "help_panel" );
	el.style.width = 250 + "px";
	el.style.left =  0 + "px";
		
	el = document.getElementById( "the_page" );
	el.style.left = 250 + "px";
	
	setButton( "hide", "hidePanel", "./images/vSIMPLE/hideo.gif", "./images/vSIMPLE/hide.gif", "Toggle the display of the help panel");
	document.getElementById( "hide" ).oldclick = hidePanel;
	//if (document.getElementById( "hide" ).oldclick)	alert("Show function"+document.getElementById( "hide" ).oldclick);	
	
	document.getElementById( "gb_home" ).style.visibility = "hidden";
	document.getElementById( "gb_whatsthis" ).style.visibility = "hidden";
}

function parseQueryString(queryString) 
{
  	var queryObject = new Object();
  	queryString = queryString.replace(/^.*\?(.+)$/,'$1');

  	while ((pair = queryString.match(/([^=]+)=\'?([^\&\']*)\'?\&?/)) && pair[0].length) 
	{
    	queryString = queryString.substring( pair[0].length );

    	if (/^\-?\d+$/.test(pair[2])) pair[2] = parseInt(pair[2]);
    	queryObject[pair[1]] = pair[2];
  	}

  return queryObject;
}

function doNothing()
{
	;
}

warningMsg   = "The option or feature you have chosen is not part of the simulation.\nPlease click 'OK' to continue.";
externalMsg  = "This link will open the LloydsTSB site in a new window.";

queryObject = parseQueryString(location.search);

collapsed 				= (queryObject.collapsed ? queryObject.collapsed : false);
displayWhatsThis		= false;
activeHomePageLink 		= true;
activeWhatsThisLink 	= true;

document.onclick 		= catchEvent;
document.onmousedown 	= watchClick;
