var showLoginOnLoad = false;
var showLinkNotLoggedInBoxOnLoad = false;
var submitLoginForm = false;

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 doSearch() {
	if ( document.getElementById( 'searchText' ).value != '' ) {
		document.location = '/compare-products&search::' + document.getElementById( 'searchText' ).value;
	} else {
		alert( "Please enter search text" );
	}
	return false;
}

function initLogin() {
	// Initialize the temporary Panel to display while waiting for external content to load
	document.getElementById('loginPopup').style.display = '';
	loginBox = 
			new YAHOO.widget.Panel("loginPopup",  
											{ width:"360px", 
											  height:"220px", 
											  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();
}

var buyNowTrackLink = '';
function buyNow( shopPrice, cashbackPrice, params) {
	
	buyNowTrackLink = '/link/?url='+params;
	if( !loggedin && shopPrice > cashbackPrice ) {
		document.getElementById( 'shopPricePopup' ).innerHTML = '£'+shopPrice;
		document.getElementById( 'cashbackPricePopup' ).innerHTML = '£'+cashbackPrice;
		
		showLinkNotLoggedInPopup();
	} else {
		buyNowGo();
	}
	
}

function buyNowGo( ) {
	hideLinkNotLoggedInPopup();
	//try to do a popup
	imgWin=window.open(buyNowTrackLink,'_blank','width=1020,height=700,left=0,top=0,location=1,status=1,scrollbars=1,toolbar=1,menubar=1,resizable=1');
	if( !imgWin ) {
		//error opening popup
		document.location = buyNowTrackLink;
	}
}

function initLinkNotLoggedInPopup() {
	// Initialize the temporary Panel to display while waiting for external content to load
	document.getElementById('linkNotLoggedInPopup').style.display = '';
	linkNotLoggedInBox = 
		new YAHOO.widget.Panel("linkNotLoggedInPopup",  
				{ width:"400px", 
			height:"360px", 
			fixedcenter:true, 
			close:false, 
			draggable:false, 
			modal:true,
			visible:false,
			underlay:"none",
			effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
				} 
		);
	
	linkNotLoggedInBox.render(document.body);
	if( showLinkNotLoggedInBoxOnLoad )
		linkNotLoggedInBox.show();
}

function hideLinkNotLoggedInPopup() {
	// Hide the Panel
	if( typeof linkNotLoggedInBox != "undefined" )
		linkNotLoggedInBox.hide();
	submitLoginForm = false;
}

function showLinkNotLoggedInPopup() {
	// clear the password field
	document.getElementById('popupPassword').value = '';
	// Show the Panel
	if( typeof linkNotLoggedInBox != "undefined" )
		linkNotLoggedInBox.show();
	else
		showLoginOnLoad = true
}

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

function showLogin() {
	// clear the password field
	document.getElementById('popupPassword').value = '';
	// Show the Panel
	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();
		validate.checkText( 'popupUsername', 'Username' );
		validate.checkText( 'popupPassword', 'Password' );
		if( validate.numberOfErrors() > 0 ) {
			validate.displayErrors();
			// we have errors
			submitLoginForm = false;
		}
	}
	
	return submitLoginForm;
}

function initToolTips() {
	
	var cashbackPriceHelp = YAHOO.util.Dom.getElementsByClassName( 'cashbackPriceHelp' );
	
	if( cashbackPriceHelp.length ) {
		var ttCashbackPrice = new YAHOO.widget.Tooltip("ttCashbackPrice", { 
			context:cashbackPriceHelp,
			autodismissdelay: 120000,
			text: '<p>This price includes the estimated amount<br/>of cashback you could recive when taking<br/>part in our cashback scheme.</p><p>The price you see when you purchase the<br/>product from the retailer should be the<br/>price we show excluding cashback, although<br/>from time to time our data may be out of<br/>date.</p><p>The cashback is part of a reward scheme we<br/>provide and is not provided directly by<br/>the retailer</p><p>To be eligable to recive cashback you need<br/>to be logged in when you click our Buy Now<br/>button. Please read our Terms and<br/>Conditions for full details.</p>'
		});
	}
}

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