var showTechReqOnLoad = false;
var showHelpOnLoad = false;
var showSendToFriendOnLoad = false;
var showReviewOnLoad = false;
var showSeminarQuestionsOnLoad = false;

function initSeminarQuestionsPanel() {
	// Initialize the temporary Panel to display while waiting for external content to load
	correctPNGBackground( 'seminarQuestionsPopupBg', '/custom/images/questionpanel.png' );
	document.getElementById('seminarQuestionsPopup').style.display = '';
	seminarQuestionsBox = 
			new YAHOO.widget.Panel("seminarQuestionsPopup",  
											{ width:"687px", 
											  height:"451px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	seminarQuestionsBox.render(document.body);
	if( showSeminarQuestionsOnLoad )
		seminarQuestionsBox.show();
}

function hideSeminarQuestionsPopup() {
	if( typeof seminarQuestionsBox != "undefined" )
		seminarQuestionsBox.hide();
}

function showSeminarQuestionsPopup() {
	if( typeof seminarQuestionsBox != "undefined" )
		seminarQuestionsBox.show();
	else
		showSeminarQuestionsOnLoad = true
}

function initReviewPanel() {
	// Initialize the temporary Panel to display while waiting for external content to load
	correctPNGBackground( 'reviewPopupBg', '/custom/images/popupbg.png' );
	document.getElementById('reviewPopup').style.display = '';
	reviewBox = 
			new YAHOO.widget.Panel("reviewPopup",  
											{ width:"389px", 
											  height:"492px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	reviewBox.render(document.body);
	if( showReviewOnLoad )
		reviewBox.show();
}

function hideReviewPopup() {
	if( typeof reviewBox != "undefined" )
		reviewBox.hide();
}

function showReviewPopup() {
	if( typeof reviewBox != "undefined" )
		reviewBox.show();
	else
		showReviewOnLoad = true
}

function initTechReqPanel() {
	// Initialize the temporary Panel to display while waiting for external content to load
	correctPNGBackground( 'techReqPopupBg', '/custom/images/popupbg.png' );
	document.getElementById('techReqPopup').style.display = '';
	techReqBox = 
			new YAHOO.widget.Panel("techReqPopup",  
											{ width:"389px", 
											  height:"492px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	techReqBox.render(document.body);
	if( showTechReqOnLoad )
		techReqBox.show();
}

function hideTechReqPopup() {
	if( typeof techReqBox != "undefined" )
		techReqBox.hide();
}

function showTechReqPopup() {
	if( typeof techReqBox != "undefined" )
		techReqBox.show();
	else
		showTechReqOnLoad = true
}

function initHelpPanel() {
	// Initialize the temporary Panel to display while waiting for external content to load
	correctPNGBackground( 'helpPopupBg', '/custom/images/helppanel.png' );
	document.getElementById('helpPopup').style.display = '';
	helpBox = 
			new YAHOO.widget.Panel("helpPopup",  
											{ width:"389px", 
											  height:"203px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	helpBox.render(document.body);
	if( showHelpOnLoad )
		helpBox.show();
}

function hideHelpPopup() {
	if( typeof helpBox != "undefined" )
		helpBox.hide();
}

function showHelpPopup( title, content ) {
	document.getElementById( 'helpPopupTitle' ).innerHTML = title;
	document.getElementById( 'helpPopupContent' ).innerHTML = content;
	if( typeof helpBox != "undefined" )
		helpBox.show();
	else
		showHelpOnLoad = true
}

function initSendToFriendPanel() {
	// Initialize the temporary Panel to display while waiting for external content to load
	correctPNGBackground( 'sendToFriendPopupBg', '/custom/images/popupbg.png' );
	document.getElementById('sendToFriendPopup').style.display = '';
	sendToFriendBox = 
			new YAHOO.widget.Panel("sendToFriendPopup",  
											{ width:"389px", 
											  height:"492px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	sendToFriendBox.render(document.body);
	if( showSendToFriendOnLoad )
		sendToFriendBox.show();
}

function hideSendToFriendPopup() {
	if( typeof sendToFriendBox != "undefined" )
		sendToFriendBox.hide();
}

function showSendToFriendPopup() {
	if( typeof sendToFriendBox != "undefined" )
		sendToFriendBox.show();
	else
		showSendToFriendOnLoad = true
}

function correctPNGBackground( divId, imgURL ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
		var div = document.getElementById( divId );
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgURL+"', sizingMethod='scale')";
		div.style.background = '';
	}
}



function addToBasket( productId, productName, qty, onProductPage ) {
	var handleSuccess = function(o){
		window.scrollTo( 0, 0 );
		document.getElementById( 'miniBasketRow' ).innerHTML = o.responseText;
		// show the "item added" bubble
		document.getElementById('itemAdded').style.display = '';
		document.getElementById('itemAddedName').innerHTML = qty + ' x ' + productName; 
		anim = new YAHOO.util.Anim('itemAdded', { opacity: { from: 0, to: 1 } }, 0.5, YAHOO.util.Easing.easeOut);
		anim.animate();
		setTimeout( 'fadeOutItemAdded()', 5000 )
	};
	var handleFailure = function(o){
		alert( 'error' );
	};
	var callback =
	{
		success:handleSuccess,
		failure:handleFailure
	};
	// argument formId can be the id or name attribute value of the
	// HTML form, or an HTML form object.
	if( onProductPage ) {
		var formObject = document.getElementById( 'productForm' );
	} else {
		// hide the select drop down list, we have layering issues with the fallout basket
		var formObject = document.getElementById( 'productForm_' + productId );
	}
	YAHOO.util.Connect.setForm(formObject);
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/manage_basket.php', callback);
}

function hideItemAdded() {
	document.getElementById('itemAdded').style.display = 'none';
}

function fadeOutItemAdded() {
	anim2 = new YAHOO.util.Anim('itemAdded', { opacity: { to: 0 } }, 0.5, YAHOO.util.Easing.easeOut);
	anim2.animate();
	anim2.onComplete.subscribe(hideItemAdded);
}


function goToJobStage( stage ) {
	document.getElementById('redirect').value = stage;
	document.getElementById('jobDetailForm').submit();
}


function addToWishList( itemID ) {
	var sUrl = "/xmlservice.php?service=addToWishList&itemType=1&itemID=" + itemID;
	var handleSuccess = function( result ){
		if ( result.responseText !== undefined ) {
			eval( 'var response = (' + result.responseText + ')' );
			if ( response.ResultSet.Result[0]['result'] == 'success' ) {
				document.getElementById( 'wishListAddButton' ).style.display = 'none';
				document.getElementById( 'wishListRemoveButton' ).style.display = '';
			} else {
				alert( 'Sorry, an error occured whilst adding this item to your wish list.' );
			}
		}
	}
	
	var handleFailure = function(o){
		if(o.responseText !== undefined){
			alert( 'Sorry, an error occured whilst adding this item to your wish list.' );
		}
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback); 
}

function removeFromWishList( itemID, inWishList ) {
	var sUrl = "/xmlservice.php?service=removeFromWishList&itemType=1&itemID=" + itemID;
	var handleSuccess = function( result ){
		if ( result.responseText !== undefined ) {
			eval( 'var response = (' + result.responseText + ')' );
			if ( response.ResultSet.Result[0]['result'] == 'success' ) {
				if ( !inWishList ) {
					document.getElementById( 'wishListAddButton' ).style.display = '';
					document.getElementById( 'wishListRemoveButton' ).style.display = 'none';
				} else {
					document.getElementById( 'wishListItem_' + itemID ).style.display = 'none';
					numberOfWishListItems--;	// global var on the /wishlist page
					if ( numberOfWishListItems == 0 ) {
						document.getElementById( 'noWishListItemsMessage' ).style.display = '';
						document.getElementById( 'sendAllWishListButton' ).style.display = 'none';
					}
				}
			} else {
				alert( 'Sorry, an error occured whilst removing this item from your wish list.' );
			}
		}
	}
	
	var handleFailure = function(o){
		if(o.responseText !== undefined){
			alert( 'Sorry, an error occured whilst removing this item from your wish list.' );
		}
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback); 
}

function saveJobSearch( title, callbackFunc ) {
	
	var sUrl = getCookie( 'jobSearchPath' );
	sUrl += '/%20save::' + title;
	
	// add our get params
	var getParams = getCookie( 'jobSearchGetParams' );
	sUrl += getParams;
	
	// add our flag so the template knows to return a status message rather than the whole page
	sUrl += ( ( getParams == '' ) ? '?' : '&' ) + 'savingSearch=true';
	
	var handleSuccess = function( result ){
		if ( result.responseText !== undefined ) {
			if ( result.responseText == 'success' ) {
				if ( document.getElementById('jobSearchNameArea') ) {
					document.getElementById('jobSearchNameArea').style.display = 'none';
				}
				alert( 'Your job alert has been successfully created!' );
				if ( typeof callbackFunc != 'undefined' ) {
					callbackFunc();
				}
			} else {
				alert( 'Sorry, an error occured saving your search, please try again.' );
				return false;
			}
		}
	}
	
	var handleFailure = function(o){
		if(o.responseText !== undefined){
			alert( 'Sorry, an error occured saving your search, please try again.' );
		}
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback); 
}


function setCookie( name, value ) {
	document.cookie = name + '=' + value + ';path=/;';
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) {
		return null;
	}
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) {
		end = document.cookie.length;
	}
	return unescape( document.cookie.substring( len, end ) );
}


function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
				input.value = '';
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}


function getPreviousPage() {
	return getCookie( 'currentPage' );
}

function setPreviousPage() {
	setCookie( 'currentPage', document.location );
}

function doSearch() {
	var validate = new validateForm();
	validate.checkText( 'searchText', 'Keywords' );
	validate.checkSelect( 'searchArea', '', 'Search Area' );
	if ( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
	} else {
		var keywords = document.getElementById('searchText').value;
		if ( document.getElementById('searchArea').value == 'jobs' ) {
			// jobs
			var path = '/ecommerce/Jobs/%20search::' + escape( keywords );
		} else if ( document.getElementById('searchArea').value == 'exammaterial' ) {
			// exam material
			var path = '/ecommerce/Exam+Material/%20search::' + escape( keywords );
		} else {
			// seminars
			var path = '/location_dates?keywords=' + escape( keywords );
		}
		document.location = path;
	}
	return false;
}

function showTab( num ) {
	var numberOfTabs = 3;
	// hide all tabs...
	for ( var i = 1; i <= numberOfTabs; i++ ) {
		document.getElementById( 'tab_' + i ).className = ( i == numberOfTabs ) ? 'tabOffLast' : 'tabOff';
		document.getElementById( 'tabContent_' + i ).style.display = 'none';
	}
	// show the relevant tab...
	document.getElementById( 'tab_' + num ).className = 'tabOn';
	document.getElementById( 'tabContent_' + num ).style.display = '';
	// hide the box browsers put round links...
	document.getElementById( 'tab_' + num ).blur();
}

function resizeExamNav() {
	var navHeight = parseInt( document.getElementById('examMaterialNav').offsetHeight, 10 );
	var contentHeight = parseInt( document.getElementById('examMaterialDetail').offsetHeight, 10 );
	if ( contentHeight < navHeight ) {
		YAHOO.util.Dom.setStyle('examMaterialNav', 'height', contentHeight + 'px' )
	}
}