//Variables
var showVAT = false;
var activeBanner = 1;
var timeoutId = 0;

//Common functions
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;
		}
	}
}

//Custom functions
function doSearch() {
	if (( document.getElementById( 'searchText' ).value != '' )) {
		document.location = '/catering_equipment/%20search::' + document.getElementById( 'searchText' ).value;
	} else {
		alert( "Please enter search text" );
	}
	return false;
}
function initVAT() {
	var cookieVal = getCookie( 'showVAT' );
	if ( cookieVal == 'true' ) {
		$('#show_incVAT').click();
		updateVAT(true);
	} else {
		$('#show_exVAT').click();
		updateVAT(showVAT);
	}
}
function updateVAT(vat) {
	showVAT = vat;
	if(showVAT) {
		$('.price_exVAT').hide();
		$('.price_exVAT_pounds').hide();
		$('.price_exVAT_pounds').attr('disabled', 'disabled');;
		$('.price_incVAT').show();
		$('.price_incVAT_pounds').show();
		$('.price_incVAT_pounds').removeAttr('disabled');
	}
	else {
		$('.price_incVAT').hide();
		$('.price_incVAT_pounds').hide();
		$('.price_incVAT_pounds').attr('disabled', 'disabled');;
		$('.price_exVAT').show();
		$('.price_exVAT_pounds').show();
		$('.price_exVAT_pounds').removeAttr('disabled');
	}
	setCookie('showVAT',vat);
}
function showTab(element,tabId) {
	$('#productTabs').find('.menuActive').removeClass('menuActive');
	$(element).parent().addClass('menuActive');
	$(element).blur();
	$('.productTabsSection').hide();
	$('#'+tabId).show();
	return false;
}
function validateBasket() {
	var IsNumber = true;
	var ValidChars = "0123456789";
	var Char;
	$('.checkoutPrice').each(function(){
		var data = $(this).val();
		if( data.length > 0 ) {
			for (i = 0; i < data.length && IsNumber == true; i++){ 
				Char = data.charAt(i); 
				if (ValidChars.indexOf(Char) == -1){
					IsNumber = false;
				}
			}
		}
	});
	if( !IsNumber ){
		alert('Invalid quantity!');
	}
	else {
		$('.checkoutPrice').each(function(){
			if($(this).hasClass('toRemove'))
			{
				$(this).val(0);
			}
		});
		$('#checkoutForm').submit();
	}
	return false;
}
function removeItem(checkbox,itemId) {
	var item = document.getElementById('CMSCatalogueBasket_basketQuantity_'+itemId);
	if(checkbox.checked == true) {
		$(item).addClass('toRemove');
	}
	else {
		$(item).removeClass('toRemove');
	}
}
function validateCheckoutLogin() {
	var validate = new validateForm();
	validate.validateEmailAddress( 'client_login_email', 'Email Address', 'inputfieldmandError' );
	validate.checkText( 'client_login_password', 'Password', '', 'inputfieldmandError' );
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	return true;
}
function showLogin() {
	$('#popupLoginPanel').dialog("open");
}
function validatePopupLogin() {
	var validate = new validateForm();
	validate.validateEmailAddress( 'popup_login_email', 'Email Address', 'inputfieldmandError' );
	validate.checkText( 'popup_login_password', 'Password', '', 'inputfieldmandError' );
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	return true;
}
function subscribeUser(){
	var validateform = new validateForm();
	// check we have name, email
	var emailAddress = document.getElementById('cmsContentSubscribe-emailAddress');
	if(( emailAddress.value != 'Enter your email...' ) && ( emailAddress.value != '' )){
		// check email address is an email address
		validateform.validateEmailAddress( emailAddress.value );
		
		if(validateform.numberOfErrors() > 0) {
			validateform.displayErrors();
		} else {
			document.forms['subscribeForm'].submit();
		}
	} else {
		validateform.addCustomError( 'Please enter your email address.' );
		validateform.displayErrors();
		emailAddress.focus();
	}
}
function validatePopupLogin() {
	var validate = new validateForm();
	validate.validateEmailAddress( 'popup_login_email', 'Email Address', 'inputfieldmandError' );
	validate.checkText( 'popup_login_password', 'Password', '', 'inputfieldmandError' );
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	return true;
}
function showBanner(bannerNum) {
	activeBanner = bannerNum;
	var bannerName = '#homeBanner'+activeBanner;
	var bannerTitle = '#bannerTitle'+activeBanner;
	$('.homeBannerBanner').css('display','none');
	$(bannerName).css('display','');
	$('.homeBannerTitle').removeClass('homeBannerTitleActive');
	$(bannerTitle).addClass('homeBannerTitleActive');
	clearTimeout(timeoutId);
	timeoutId = setTimeout('startBanner()',20000);
}
function startBanner() {
	clearTimeout(timeoutId);
	timeoutId = setTimeout('animateBanner()',5000);
}
function animateBanner() {
	if(activeBanner < 3) {
		activeBanner++;
	}
	else {
		activeBanner = 1;
	}
	var bannerName = '#homeBanner'+activeBanner;
	var bannerTitle = '#bannerTitle'+activeBanner;
	$('.homeBannerBanner').css('display','none');
	$(bannerName).css('display','');
	$('.homeBannerTitle').removeClass('homeBannerTitleActive');
	$(bannerTitle).addClass('homeBannerTitleActive');
	clearTimeout(timeoutId);
	timeoutId = setTimeout('animateBanner()',5000);
}
//Document ready section
$(document).ready( function() {
	initVAT();
	startBanner();
	$('#popupLoginPanel').dialog({
			modal:		true,
			resizable:	false,
			width:		293,
			height:		197,
			draggable:	false,
			autoOpen:   false,
			title:		'Customer Login'
	});
});
