var basketOpen = false;
var vatState = 'exVat';
function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		$('#' + passwordField ).show();
		$('#' + passwordField ).focus();
		$('#' + passwordField + '_text' ).hide();
	} else {
		if( input.value == defaultText ) {
			input.value = '';
		}
	}
}
function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			$('#' + passwordField ).hide();
			$('#' + passwordField + '_text' ).show();
		} else {
			input.value = defaultText;
		}
	}
}
function doSearch( ) {
	var searchText = $('#searchText').val();
	if ( searchText != '' && searchText != '- Search...' ) {
		document.location = '/catering_equipment/%20search::' + searchText;
	} else {
		alert( 'Please enter a search term' );
	}
	return false;
}
function showDocument( document ){
	window.open( '/popup/' + document , 'mywindow', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=500, height=500');
}
function doFilter( base, element ) {
	document.location = base + element.value;
}
function showMinibasket() {
	basketOpen = true;
	$('#miniBasketOpen').show();
	$('#minibasketShow').attr( 'src', '/custom/images/minibasket-contract.gif' );
	$('#headExtrasMinibasket').addClass( 'headExtrasMinibasketOn' );
}
function hideMinibasket() {
	basketOpen = false;
	$('#miniBasketOpen').hide();
	$('#minibasketShow').attr( 'src', '/custom/images/minibasket-expand.gif' );
	$('#headExtrasMinibasket').removeClass( 'headExtrasMinibasketOn' );
}
function validateLoginPopup() {
	var validate = new validateForm();
	if( $('#clientLogin_username').val() == '- Email Address -' ) {
		validate.addCustomError( 'Email Address' );
	} else {
		validate.validateEmailAddress( 'clientLogin_username', 'Email Address' );
	}
	validate.checkText( 'clientLogin_password', 'Password' );
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	return true;
}
function validateLoginCheckout() {
	var validate = new validateForm();
	if( $('#checkoutLogin_username').val() == '- Email Address -' ) {
		validate.addCustomError( 'Email Address' );
	} else {
		validate.validateEmailAddress( 'checkoutLogin_username', 'Email Address' );
	}
	validate.checkText( 'checkoutLogin_password', 'Password' );
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	return true;
}
function checkCallMeBack() {
	
	var validate = new validateForm();
	validate.checkText('call_name','Your Name', '- Your Name');	
	validate.checkText('call_telephone','Telephone Number', '- Your Telephone Number');
	validate.checkText('call_enquiry','Brief description of enquiry', '- Brief description of enquiry...');
	
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	// send the data
	$.get( '/email/callMeBack', $("#callMeBackForm").serialize(),
		function( data ){
			if ( data.substr( 0, 6 ) == 'ERROR:' ) {
				alert( data.substr( 6 ) );
				return false;
			}
			// tell the user something happened
			alert( "Call me back request received, we will be in touch as soon as possible" );
			// hide it in a bit
			hideCallMeBack();
			// dont redirect maybe
			return false;
		}
		, 'html'
	);
}
function hideCallMeBack() {
	$('#callMeBackContainer').hide();
}
function showLogin() {
	$('#popupLoginPanel').dialog("open");
}

function hideLogin() {
	$('#popupLoginPanel').dialog("close");
}
function logout() {
	document.getElementById('logoutForm').submit();
}
function changeVat( newVatState ) {
	$('.' + vatState ).hide();
	vatState = newVatState;
	setCookie( 'vatState', vatState );
	$('.' + vatState ).show();
}
function getCurrentVat() {
	var vat = getCookie( 'vatState' );
	if ( !vat ) {
		setCookie( 'vatState', 'exVat' );
		return 'exVat'; // show exVat by default
	} else {
		return vat;
	}
}
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 checkVatCookie() {
	var cookieVal = getCurrentVat();
	// if val is true or not previously been set...
	if ( cookieVal != 'false' || cookieVal == 'true' || typeof cookieVal == 'undefined' || cookieVal != 'exVat' ) {
		changeVat( cookieVal );
	}
}
$(document).ready(function(){
	Cufon.replace('h5');
	Cufon.replace('h6');
	Cufon.now();	// required for ie
	checkVatCookie();
	$('.cssCheckbox').checkBox();
	$('#minibasketHide').live( 'click', function() {
		hideMinibasket();
	});
	$('#callMeBackCancel').click(function(){
		hideCallMeBack();
	});
	$('#lhsCallMeBack').click(function(){
		$('#callMeBackContainer').show();
	});
	/*
	$('#minibasketShow').live( 'click', function() {
		if( basketOpen ) {
			hideMinibasket();
		} else {
			showMinibasket();	
		}
	});
	*/
	$('#popupLoginPanel').dialog({
			modal:		true,
			resizable:	false,
			width:		237,
			height:		191,
			draggable:	false,
			autoOpen:   false,
			focusOnFirstElement: false
	});
});
function sharePage( type, title ) {
	var networkLocation = '';
	switch( type ) {
		case 'digg':
			networkLocation = 'http://digg.com/submit?phase=2&url='+location.href+'&title=' + title;
			break;
		case 'facebook':
			networkLocation = 'http://www.facebook.com/sharer.php?u='+location.href+'&t=' + title;
			break;
		case 'myspace':
			networkLocation = 'http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(location.href)+'&t=' + title;
			break;
		case 'stumbleupon':
			networkLocation = 'http://www.stumbleupon.com/submit?url='+location.href+'&title=' + title;
			break;
		case 'twitter':
			networkLocation = 'http://twitter.com/?status=' + title + ' - ' + encodeURIComponent(location.href);
			break;
	}
	// now open the link in a new window
	window.open( networkLocation );
}
function showImage( image ) {
	imgWin=window.open('about:blank','','scrollbars=no,width=500,height=500,left=100,top=100');
	with (imgWin.document) {
		writeln('<html><head><title></title><scr'+''+'ipt language="javascript">var arrTemp=self.location.href.split("?");var NS = (navigator.appName=="Netscape")?true:false;function FitPic() { iWidth = (NS)?window.innerWidth:document.body.clientWidth; iHeight = (NS)?window.innerHeight:document.body.clientHeight; iWidth = document.images[0].width - iWidth; iHeight = document.images[0].height - iHeight; window.resizeBy(iWidth, iHeight); self.focus(); }; </scr'+''+'ipt> </head> <body bgcolor="#ffffff" onload="FitPic();" topmargin="0" marginheight="0" leftmargin="0" marginwidth="0"> <img src="' + image + '" border="0" /></body> </html>');
		close();	
	}
}