var anim;
var anim2;
var showLoginOnLoad = false;
var showProductDetailOnLoad = false;
var productDetailsPopupDisplayed = false;

function doLogout() {
	document.getElementById('logoutForm').submit();
}

function addToBasket( productId, formNumber, productName, price, img ) {
	var validate = new formValidator();
	validate.checkNumeric( 'product_quantity_' + formNumber, 'Quantity' );
	if ( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	
	// check all customer options
	var selects = document.getElementById('productOptions').getElementsByTagName('select');
	for ( var i = 0; i < selects.length; i++ ) {
		if ( selects[i].selectedIndex == 0 ) {
			alert('Please select all options');
			return false;
		}
	}
	
	var doAddToBasket = function(o) {
		document.getElementById( 'minibasketValues' ).innerHTML = o.responseText;
		// show the "item added" bubble
		document.getElementById('itemAdded').style.display = '';
		document.getElementById('itemAddedImage').innerHTML = '<img src="' + img + '" width="68" height="68" />';
		document.getElementById('itemAddedName').innerHTML = productName;
		document.getElementById('itemAddedPrice').innerHTML = '&pound;' + price;
		anim = new YAHOO.util.Anim('itemAdded', { opacity: { from: 0, to: 1 } }, 0.5, YAHOO.util.Easing.easeOut);
		anim.animate();
		setTimeout( 'fadeOutItemAdded()', 5000 );
	};
	
	var handleSuccess = function(o){
		window.scrollTo( 0, 0 );
		if( productDetailsPopupDisplayed ) {
			productDetailBox.hideMaskEvent.subscribe(function() {
				doAddToBasket(o);
			}, this, true);
			hideProductDetailPopup();
		} else {
			doAddToBasket(o);
		}
	};
	var handleFailure = function(o){
		alert( 'error' );
	};
	var callback =
	{
		success:handleSuccess,
		failure:handleFailure
	};
	var formObject = document.getElementById( 'productForm_' + formNumber );
	YAHOO.util.Connect.setForm(formObject);
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/submit.php', callback);
	return false;
}

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 initProductDetailsPopup() {
	document.getElementById('productDetailPopup').style.display = '';
	productDetailBox = new YAHOO.widget.Panel("productDetailPopup",  
											{ width:"982px", 
											  height:"523px", 
											  close:false, 
											  fixedcenter:false,
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  context:['lhs','br','tl'],
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);
	productDetailBox.render(document.body);
	if( showProductDetailOnLoad ) {
		productDetailsPopupDisplayed = true;
		productDetailBox.show();
	}
}

function hideProductDetailPopup() {
	if( typeof productDetailBox != "undefined" ) {
		productDetailsPopupDisplayed = false;
		productDetailBox.hide();
	}
}

function showProductDetailPopup() {
	if( typeof productDetailBox != "undefined" ) {
		productDetailsPopupDisplayed = true;
		productDetailBox.show();
	} else {
		showProductDetailOnLoad = true;
	}
}

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 inputFocus( input, passwordField ) {
	if ( typeof passwordField == 'undefined' ) {
		if ( input.defaultValue == input.value ) {
			input.value = '';
		}
	} else {
		input.style.display = 'none';
		var passwordInput = document.getElementById( passwordField );
		passwordInput.style.display = '';
		passwordInput.focus();
	}
}
function inputBlur( input, textField ) {
	if ( typeof textField == 'undefined' ) {
		if ( input.value == '' ) {
			input.value = input.defaultValue;
		}
	} else {
		if ( input.value == '' ) {
			input.style.display = 'none';
			var textInput = document.getElementById( textField );
			textInput.style.display = '';
		}
	}
}