var showShippingAddressPopupOnLoad = false;
var showLoginOnLoad = false;
var submitLoginForm = false;
var showTrackingOnLoad = false;
var bannerManuallyChanged = false;

var shipToCountry = 'GB';

var currentQuoteRowId = -1;
var currentQuoteRowButton;

var currentPricingTableDestinationId = 1;
var currentPricingTableWeightId = 1;

function logout() {
	document.getElementById('logout_form').submit();
}

function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

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='image')";
		div.style.background = '';
	}    
}

function initLogin() {
	document.getElementById('loginPopup').style.display = '';
	loginBox = 
			new YAHOO.widget.Panel("loginPopup",  
											{
											  width:"282px",
											  height:"196px",
											  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();
}
function hideLogin() {
	if( typeof loginBox != "undefined" ) {
		loginBox.hide();
	}
	submitLoginForm = false;
}
function showLogin( message ) {
	if ( typeof message != 'undefined' ) {
		document.getElementById('loginPopupText').innerHTML = message;
	}
	if( typeof loginBox != "undefined" ) {
		loginBox.show();
	} else {
		showLoginOnLoad = true;
	}
}

function initTracking() {
	// Initialize the temporary Panel to display while waiting for external content to load
	document.getElementById('trackingPopup').style.display = '';
	trackingBox = 
			new YAHOO.widget.Panel("trackingPopup",  
											{
											  width:"282px",
											  height:"196px",
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	trackingBox.render(document.body);
	if( showTrackingOnLoad )
		trackingBox.show();
}

function hideTracking() {
	if( typeof trackingBox != "undefined" )
		trackingBox.hide();
}

function showTracking() {
	if( typeof trackingBox != "undefined" )
		trackingBox.show();
	else
		showTrackingOnLoad = true
}

function onPopupSubmit() {
	// if we are submitting to form
	if( submitLoginForm ){
		// ensure we have username and password
		var validate = new validateForm();
		validate.checkText( 'popupUsername', 'Username' );
		validate.checkText( 'popupPassword', 'Password' );
		if( validate.numberOfErrors() > 0 ) {
			validate.displayErrors();
			// we have errors
			submitLoginForm = false;
		}
	}
	return submitLoginForm;
}


function checkLogin() {
	var validate = new validateForm();
	
	if( document.getElementById( 'clientLogin_username' ).value == 'Username' ){
		validate.addCustomError( 'Username' );
	} else {
		validate.checkText( 'clientLogin_username', 'Username' );
	}
	
	validate.checkText( 'clientLogin_password', 'Password' );
	
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
}

function openWindow( url, width, height ) {
	window.open( url, 'popupWindow','menubar=0,resizable=1,scrollbars=1,width='+ width +',height=' + height);
}


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 removeNonNumeric( val ) {
	return val.replace( /[^0-9]/g, '' );
}

function checkWhole( units, input ) {
	var numericValue = removeNonNumeric( input.value );
	if ( input.value != numericValue ) {
		input.value = numericValue;
		alert('Please only enter a whole number (ie 5.3' + units + ' would be 6' + units + ')');
	}
}

function mouseOverNavItem( navId ) {
	document.getElementById('navLink_' + navId).className = 'navLinkOn';
}
function mouseOutNavItem( navId ) {
	document.getElementById('navLink_' + navId).className = 'navLink';
}

function goToCheckout() {
	if ( document.getElementById('quoteTableCheckoutButton') ) {
		// we are on a page with the quote table, validate the form
		prebookShipments();
	} else {
		// no quote table, just proceed to the checkout
		document.location = '/checkout';
	}
}

/**
The keepSessionAlive function runs every 10 minutes to 
ensure that our session does not time out, allowing the 
user to keep items in their basket for as long as required
**/
function keepSessionAlive() {
	
	var sUrl = "/keepsessionalive.php";
	
	var handleSuccess = function( result ){
	}
	
	var handleFailure = function(o) {
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback); 
}

setInterval( 'keepSessionAlive()', 600000 );

function changePricingTableDestination( num ) {
	document.getElementById('pricingStructureTabContent_' + currentPricingTableWeightId + '_' + currentPricingTableDestinationId ).style.display = 'none';
	document.getElementById('pricingStructureTab_' + currentPricingTableDestinationId ).className = 'pricingStructureTab';
	currentPricingTableDestinationId = num;
	document.getElementById('pricingStructureTabContent_' + currentPricingTableWeightId + '_' + currentPricingTableDestinationId ).style.display = 'block';
	document.getElementById('pricingStructureTab_' + currentPricingTableDestinationId ).className = 'pricingStructureTab pricingStructureTabOn';
}
function changePricingTableWeight( num ) {
	document.getElementById('pricingStructureTabContent_' + currentPricingTableWeightId + '_' + currentPricingTableDestinationId ).style.display = 'none';
	document.getElementById('pricingStructureLeftTab_' + currentPricingTableWeightId ).className = 'pricingStructureTab';
	currentPricingTableWeightId = num;
	document.getElementById('pricingStructureTabContent_' + currentPricingTableWeightId + '_' + currentPricingTableDestinationId ).style.display = 'block';
	document.getElementById('pricingStructureLeftTab_' + currentPricingTableWeightId ).className = 'pricingStructureTab pricingStructureTabOn';
}

var currentBannerItem = 1;
var numberOfBanners = 2;

function viewNextBanner( dir ) {
	if ( dir == '+' ) {
		var nextBannerItem = currentBannerItem + 1;
		if ( nextBannerItem > numberOfBanners ) {
			nextBannerItem = 1;
		}
	} else {
		var nextBannerItem = currentBannerItem - 1;
		if ( nextBannerItem == 0 ) {
			nextBannerItem = numberOfBanners;
		}
	}
	var animImgIn = new YAHOO.util.Anim( 'slideyBannerImage' + nextBannerItem, { opacity: { to: 1 } }, 0.5, YAHOO.util.Easing.easeOut );
	var animTextIn = new YAHOO.util.Anim( 'sliderBannerText' + nextBannerItem, { opacity: { to: 1 } }, 0.5, YAHOO.util.Easing.easeOut );
	var animImgOut = new YAHOO.util.Anim( 'slideyBannerImage' + currentBannerItem, { opacity: { to: 0 } }, 0.5, YAHOO.util.Easing.easeIn );
	var animTextOut = new YAHOO.util.Anim( 'sliderBannerText' + currentBannerItem, { opacity: { to: 0 } }, 0.5, YAHOO.util.Easing.easeOut );
	animImgIn.animate();
	animTextIn.animate();
	animImgOut.animate();
	animTextOut.animate();
	currentBannerItem = nextBannerItem;
}

function initBanner() {
	viewNextBanner('+');
}

function showConverter() {
	openWindow( '/converter', 460, 140 );
}

function validateTrackingInline() {
	var validator = new validateForm();
	validator.checkText( 'trackingNumberInline', 'Tracking Number' );
	if( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

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