var showLoginOnLoad = false;
var submitLoginForm = false;

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 initLogin() {
	// Initialize the temporary Panel to display while waiting for external content to load
	correctPNGBackground( 'loginPopupBg', '/custom/images/login-bg.png' );
	document.getElementById('loginPopup').style.display = '';
	loginBox = 
			new YAHOO.widget.Panel("loginPopup",  
											{ width:"455px", 
											  height:"222px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	loginBox.render(document.body);
	if( showLoginOnLoad )
		loginBox.show();
}

YAHOO.util.Event.addListener(window, "load", initLogin);

function hideLogin() {
	if( typeof loginBox != "undefined" )
		loginBox.hide();
	submitLoginForm = false;
}

function showLogin() {
	// clear the password field
	//document.getElementById('clientLogin_password').value = '';
	if( typeof loginBox != "undefined" ){
		loginBox.show();
	} else
		showLoginOnLoad = true
}

function onPopupSubmit() {
	// if we are submitting to form
	if( submitLoginForm ){
		// ensure we have username and password
		var validate = new validateForm();
		if( document.getElementById('clientLogin_username').value == ' - Email Address -' ){
			validate.addCustomError( 'Email Address' );	
		} else {
			validate.checkText( 'clientLogin_username', 'Email Address' );
		}
		validate.checkText( 'clientLogin_password', 'Password' );
		if( validate.numberOfErrors() > 0 ) {
			validate.displayErrors();
			// we have errors
			submitLoginForm = false;
		}
	}
	
	return submitLoginForm;
}

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 = '';
		} else {
		}
	}
}

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 showMinibasket(){
	var w = window.open( '/manage_basket.php?CMSCatalogueBasket[view_cart]=true', 'basket', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=500, height=420');
	w.focus();
}

function doSearch() {
	var search = document.getElementById('searchText').value;
	if( search != '' && search != ' - Type here -' ){
		document.location = '/products/%20search::' + search;
	} else {
		alert( 'Please enter a search term' );
	}
	return false;
}