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 = '';
		} else {
		}
	}
}

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;
		}
	}
}

var showLoginOnLoad = false;
var submitLoginForm = 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:"300px", 
											  height:"167px", 
											  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();
}

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

function hideLogin() {
	// Show 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;
}


var showCheckoutOnLoad = false;
var submitCheckoutLoginForm = false;

function initCheckout() {
	// Initialize the temporary Panel to display while waiting for external content to load
	
	checkoutBox = 
			new YAHOO.widget.Panel("checkoutPopup",  
											{ width:"750px", 
											  height:"180px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	checkoutBox.render(document.body);
	
	if( showCheckoutOnLoad )
		checkoutBox.show();
		
}

YAHOO.util.Event.addListener(window, "load", initCheckout);

function hideCheckout() {
	// Show the Panel
	if( typeof checkoutBox != "undefined" )
		checkoutBox.hide();
	submitCheckoutLoginForm = false;
}

function showCheckout() {
	document.getElementById('checkoutPopup').style.display = '';
	// clear the password field
	document.getElementById('popupCheckoutPassword').value = '';
	// Show the Panel
	if( typeof checkoutBox != "undefined" ){
		checkoutBox.show();
	} else{
		showCheckoutOnLoad = true
	}
}

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


function resizeSite() {
	var browseWidth;

	var mainBody = document.getElementById('body');

	browseWidth=window.outerWidth;
	if (document.all){
		browseWidth=document.body.clientWidth;
	}
	
	var minPageSize = 810;
	if ( browseWidth < minPageSize ) {
		mainBody.style.width = '800px';
	} else {
		mainBody.style.width = '100%';
	}

}

function doOnload() {
	resizeSite();
}

function subscribeUserInline(){
	var validateform = new validateForm();
	// check we have name, email
	var emailAddress = document.getElementById('cmsContentSubscribe-emailAddressInline');
	if(( emailAddress.value != '- Email Address -' ) && ( emailAddress.value != '' )){
		// check email address is an email address
		validateform.validateEmailAddress( emailAddress.value );
		
		if(validateform.numberOfErrors() > 0) {
			validateform.displayErrors();
		} else {
			// check for default '- Name -' in the name field
			if( document.getElementById('cmsContentSubscribe-nameInline').value == '- Name -' ){
				// set it to blank
				document.getElementById('cmsContentSubscribe-nameInline').value = '';
			}
			return true;
		}
	} else {
		validateform.addCustomError( 'Please enter your email address.' );
		validateform.displayErrors();
		emailAddress.focus();
		return false;
	}
	
}

window.onresize = resizeSite;
window.onload = doOnload;



$(document).ready(function(){
	/*Stock Check Javascript Dialog Box Start*/
		$('#stockReminder').dialog({
			autoOpen: false,
			width: 300,
			modal: true,
			resizable: false
			
		});

		$('#stockReminderClick').click(function(){

			if(loggedIn==true){
				postStockReminder();
			}else{
				$('#stockReminder').dialog('open');
			}
		})				
		
		$('#closeReminderForm').click(function(){
			$('#stockReminder').dialog('close');
		})
		
		$('#stockReminderForm').submit(function(){
			if(postStockReminder()){
				$('#stockReminder').dialog('close');				
			}
			return false;
		})
		
		function postStockReminder(){
			var validator = new validateForm();
			validator.checkText( 'stock_name', 'Your Name' );
			validator.validateEmailAddress( 'stock_email', 'Your Email Address' );
			if ( validator.numberOfErrors() > 0 ) {
				validator.displayErrors();
				return false;
			}else{
				$.ajax({
					url:"/xmlservice.php?service=stockReminder",
					type:"POST",
					data:$('#stockReminderForm').serialize(),
					success: function(data, status){
						alert('You will receive an e-mail when we have stock again!');
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						alert("Error failed to submit Request !"+ textStatus+' '+errorThrown);
					}
				});
				return true;
			}
		}
	/*Stock Check Javascript Dialog Box Stop*/
})
