// Have set a left variable here
var left = 0;

$(function() {
	// Turn off the top right link 
	$.Lightbox.construct({"show_linkback":false});
	
	// Activate the gallery lightbox
	$('.trialImages a').lightbox({fixedNavigation:true});
	zIndexHandler();
	
});

function zIndexHandler(){
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 1;
	});
}

/**
 * New Tozer Scripts v1.00	10/01/11 
 */
$(function(){
	// Enable the mini basket
	enableByLetterNav();
});


function smallBasketActions(){
	// When we click a 'removefrom basket' button
	$('.jsRemoveFromBasket').click(function(){
		
		// First remove the regular class and add a 'waiting' class
		$(this).removeClass('jsRemoveFromBasket').addClass('jsRemoveFromBasketWait');
		
		// Define 'item' as the parent div of this button
		var item = $(this).parents('.jsItem');
		
		var basketItem = $(this).attr('id');
		// Set basketItem as the id minus the text
		basketItem = basketItem.replace('basketItem','');
		
		// After a short delay animate the row out, then callback
		$(item).delay('500').animate({left:'-300px', opacity:'hide', height:'0px', padding:'0px'},'normal',function(){
			// All following siblings of this item, need to have their classes toggled
			$(item).nextAll('.jsItem').toggleClass('smallBasketOdd').toggleClass('smallBasketEven');
			// Now detach the item from the DOM completely
			$(item).detach();
			// We need to fire an ajax here and get the basket totals back
			$.get('/ajax/remove-from-basket	/',{'basketItemId':basketItem},function(data){
				// If we have returned data then we don't have a completely empty basket (which is good)
				if(data!=""){
					// Replace the basket totals with the div we have here!
					$('.smallBasketTotals').replaceWith(data);
					// Update the basket item total by fetching it from the counter
					var total = parseFloat($('#miniBasketCounter').html());
					// Take one off
					total--;
					if(total==3){
						$('.jsItem').unwrap();
					}
					// And re-insert it
					$('#miniBasketCounter').html(total);
				} else {
					// Create an 'empty basket' set
					var html = '<div class="smallBasketTotals">';
						html +='<div class="smallBasketLabel">Subtotal</div>';
						html +='<div class="smallBasketAmount">&pound;0.00</div>';
						html +='<div class="smallBasketLabel">Carriage</div>';
						html +='<div class="smallBasketAmount">FREE</div>';
						html +='<div class="smallBasketLabel">VAT(inc)</div>';
						html +='<div class="smallBasketAmount">&pound;0.00</div>';
						html +='<div class="smallBasketLabel">Total</div>';
						html +='<div class="smallBasketAmount"><strong>&pound;0.00</strong></div>';
						html +='</div>';
						
					// Replace the current totals with this
					$('.smallBasketTotals').replaceWith(html);
					// Detach the counter since we're at 0
					$('#miniBasketCounter').detach();
					// Fire a click on the body
					$('body').click();
				}
			});
		});
		
		
		// Override any default behaviour
		return false;
	});
}


/**
 * Enables the mini basket expander
 */
function enableMiniBasket(){
	// Hook onto the jsMiniBasket Button
	$('#jsMiniBasketBtn').click(function(){
		// Hide the counter
		$('#miniBasketCounter').fadeOut('500');
		// Animate the small basket
		$('#smallBasketOuter').fadeIn('200',function(){
			// Switch class to open
			$(this).removeClass('closed').addClass('open');
			$('#smallBasket').slideDown('1000', function(){
				dockItem('#smallBasketOuter');
			});
			
			// First we need an event handler to stop propagation to the body
			$('#smallBasketOuter').click(function(event){
				// Stop event propagation
				event.stopPropagation();
			});
			
			// Add an event click to the body that will only run once
			$('body').one('click',function(){
				// Unbind the scroll event on the window
				$(window).unbind('scroll');
				// Roll up the small basket and then callback
				$('#smallBasket').slideUp('500',function(){
					// Fadeout the wrapper
					$('#smallBasketOuter').fadeOut('250',function(){
						// Switch the class
						$(this).removeClass('open').addClass('closed');
						// Re-show the counter
						$('#miniBasketCounter').fadeIn('250');
						$('#smallBasketOuter').removeClass('fixed').css({"left":"-123px"});
					});
				});
			});
			
		});
		return false;
	});
	
	// Hook into the close button
	$('#smallBasketTitle h2').click(function(){
		// Unbind the window scroll
		$(window).unbind('scroll');
		// Roll up the small basket and then callback
		$('#smallBasket').slideUp('500',function(){
			// Fadeout the wrapper
			$('#smallBasketOuter').fadeOut('250',function(){
				// Switch the class
				$(this).removeClass('open').addClass('closed');
				// Re-show the counter
				$('#miniBasketCounter').fadeIn('250');
				$('#smallBasketOuter').removeClass('fixed').css({"left":"-123px"});;
			});
		});
	});
}


function enableByLetterNav(){
	
	// First add a click event to the level 1 items
	$('.letterLevel1').click(function(){

		// Make sure this wasn't the last item that was clicked here
		if($(this).hasClass('last')){
						
		} else {
			// Clear other items
			$('.letterLevel1').removeClass('last');
			
			// Make this the last clicked item
			$(this).addClass('last');
			
			// Slide other options up
			$('.letterLevel2, .letterLevel2selected').hide();
			
			// Get the next li items until we get to level 1 again
			$(this).nextUntil('.letterLevel1').show();
		}
		
		// Don't follow any links
		return false;
	});
	
	
}

$(function(){
	// enableTooltips()		//TODO Tooltips turned off until categories have text
});

/**
 * Enables the tooltippyness where ever one is!
 * @return
 */
function enableTooltips(){
	$('.tooltip').parent().hoverIntent(function(){
		/* doing a fade in instead */
		$(this).find('.tooltip').fadeIn('300');
	},function(){
		/* doing fadeout */
		$(this).find('.tooltip').fadeOut('200');
	});
}

$(function(){
	// When we focus on the header search input
	$('#header-search-input').focus(function(){
		// If it's value is the default value
		if($(this).val() == "Enter Your Search..."){
			// Clear it
			$(this).val("");
		}else{
			// Don't clear it
		}
	});

	// When we focus out of the header search input
	$('#header-search-input').blur(function(){
		// If it's value is empty
		if($(this).val() == ""){
			// add the default value
			$(this).val("Enter Your Search...");
		}else{
			// Don't clear it
		}
	});

	// Now when we want to do a search lets check for the default value
	// When form is submitted
	$('#search_form').submit(function(){
		// If the search input = the default term
		if($('#header-search-input').val()=="Enter Your Search..."){
			// Alert and return false
			alert("Please enter a search term");
			$('#header-search-input').focus();
			return false;
		}else{
			// Just return true for now
			return true;
		}
	});	
});


/**
 * This function enables us to dock
 * an item to the top of the browser
 * when it reaches the top of the screen
 * (useful for shopping basket?)
 * @param item
 */
function dockItem(item){
	// Set top as the miniBasket Outer
	var top = $('#miniBasketOuter').offset().top - parseFloat($(item).css('marginTop').replace(/auto/,0));
	// Pull in left from the global variable
	if(left==0){
		// If it's unset set it from the items offest left
		left = $(item).offset().left;
	} else {
		// If its set we need to use the miniBasket Outer's offset minus it's left value
		left = $('#miniBasketOuter').offset().left - 123;
	}
	// Add a listener that will reset this item on window resize
	$(window).resize(function(){
		// If the item has a class of fixed
		if($(item).hasClass('fixed')){
			// Stop all animation and then animate the left of the item to where it should be
			$(item).stop().animate({"left":left},'fast');
		}
		// Unbind the scroll event
		$(window).unbind('scroll');
		// Unbind the resize event
		$(window).unbind('resize');
		// Reinstantiate the dockItem!
		dockItem(item);
	});
	// Add a handler to the scroll
	$(window).scroll(function(){
		// what the y position of the scroll is
	    var y = $(this).scrollTop();
	    // whether that's below the form
	    if (y >= top){
	    	// if so, add the fixed class
		    $(item).addClass('fixed').css({"left":left});
	    } else {
	    	// otherwise remove it
	      	$(item).removeClass('fixed').css({"left":"-123px"});
	    }
	});
}

/**
 * jQuery to call in supersleight (png fix for IE6)
 * 
 * DOES NOT BREAK OTHER BROWSERS!!
 * 
 */
$(function(){
	$('body').supersleight({shim: '/images/blank.gif'});
});

/**
 * jQuery function to show and then hide the basket feedback
 * 
 */
$(function(){
	// This will fade the feedback in and out if no mouse interaction
	$('#feedback-container').delay(500).fadeIn(500, enableMouseStop).delay(4000).fadeIn(1,disableMouseStop).fadeOut(500);
	// $('#feedback-container').delay(1000).fadeIn(1000); << USE THIS LINE TO STYLE UP THE BASKET FEEDBACK (won't dissapear)
	
	// Once we have rolled over this will handle the click on continue button
	$('#continue-button').click(function(){
		// FIRST unbind the mouseout (so it doesn't stop again)
		$('#mini-basket-feedback').unbind('mouseover');
		
		// Now fade out
		$('#feedback-container').fadeOut(200);
		
		// And return false so that our fallback link isn't followed!
		return false;
	});
});

$(function(){
	newImageHandler();
});

/**
 * Product Image Click Handler
 */
function newImageHandler(){
	$('p.jsProductImage').click(function(){							// Each a tag with class jsProductImage
		$('#product-image-img').attr('src',$(this).attr('rel'))		// Set the main image src to this images href
								.attr('alt',$(this).attr('title'));	// and set it's alt to this ones title
		return false;
	});
}

/**
 * Enables the mouseover event on the Basket Feedback
 *  
 * @return
 */
function enableMouseStop(){
	// Create a listener for the feedback on mouseover
	$('#mini-basket-feedback').mouseover(function(){
		// Stop all animation, clear queue and do not jump to end
		$('#feedback-container').stop(true,false);
	});
}

/**
 * Disables the mouseoever event on the basket feedback
 * 
 * @return
 */
function disableMouseStop(){
	$('#mini-basket-feedback').unbind('mouseover');
}

/**
 * jQuery function to clear searchbox on click and focus
 * Will automatically add 'search' to the box if it's clear on focusout
 * 
 */
$(function(){
	// When we focus on the searchterm
	$('#searchTerm').focusin(function(){
		// Store the search term
		var search = $('#searchTerm').val();
		
		// Is it search?
		if(search == "Search the site"){
			// Yes so blank it
			$('#searchTerm').val("");
		}
	});
	
	// When the search box loses focus
	$('#searchTerm').focusout(function(){
		// Store the search term
		var search = $('#searchTerm').val();
		
		// Is it blank
		if(search == ""){
			// Yes it is so stick search in it
			$('#searchTerm').val("Search the site");
		}
	});
	
	// When we click on the search box
	$('#searchTerm').click(function(){
		// Store the search value
		var search = $('#searchTerm').val();
		
		// Is it search?
		if(search == "Search the site"){
			// Yes so blank it
			$('#searchTerm').val("");
		}
	});
});

/* Product Section Functions */


/**
 * Enable the scroll down Javascript basket
 * 
 */
$(function(){
	// On click on the shopping basket button
	$('#basket-show').toggle(function(){
		/* DANGER IE BUG HACK */
		// Hide Select boxes that are underneath
		$('#product-options').find('select').each(function(){
			$(this).hide();
		});
		/* /DANGER IE BUG HACK */
		
		
		// Display the hidden basket
		$('#hidden-basket').slideDown('400');
		
		// Return false so we don't follow the link
		return false
	},function(){
		/* DANGER IE BUG HACK */
		// Hide Select boxes that are underneath
		$('#product-options').find('select').each(function(){
			$(this).show();
		});
		/* /DANGER IE BUG HACK */
		
		
		// $('#hidden-basket').stop(true,false);
		$('#hidden-basket').slideUp('200');
		
		// Do not follow the link
		return false;
	});
});


/* NEW CUSTOMER SCRIPTS */

// Ok add a listener to the form submit for new customers
$(function(){
	// When the document is ready
	$('#checkout').submit(function(){
		$('input').removeClass('error');
		
		// Assume all is good to go
		var valid = true;
		
		// Run through all fields and check
		if($('#firstname').val() == ""){			
			$('#firstname').addClass('error');
			
			valid = false;
			alert("Please enter your first name");
			$('html, body').animate({scrollTop: $('#firstname').offset().top}, 500);
			return false;
		}
		
		if($('#surname').val() == "")
		{
			$('#surname').addClass('error');
			
			valid = false;
			alert("Please enter your surname");
			$('html, body').animate({scrollTop: $('#surname').offset().top}, 500);
			return false;
		}
		
		if($('#telephone').val() == "")
		{
			$('#telephone').addClass('error');
			
			valid = false;
			alert("Please enter your telephone number");
			$('html, body').animate({scrollTop: $('#telephone').offset().top}, 500);
			return false;
		}
		
		if(($('#jsEmail1').val() == "") || ($('#jsEmail2').val() == ""))
		{
			$('#jsEmail1').addClass('error');
			$('#jsEmail2').addClass('error');
			
			valid = false;
			alert("Please enter your email address into both boxes");
			$('html, body').animate({scrollTop: $('#jsEmail1').offset().top}, 500);
			return false;
		}
		
		if($('#jsEmail1').val() != $('#jsEmail2').val())
		{
			$('#jsEmail1').addClass('error');
			$('#jsEmail2').addClass('error');
			
			valid = false;
			alert("Please enter the same email address into both boxes");
			$('html, body').animate({scrollTop: $('#jsEmail1').offset().top}, 500);
			return false;
		}
		
		if(checkValidEmail($('#jsEmail1').val()) == false)
		{
			$('#jsEmail1').addClass('error');
			$('html, body').animate({scrollTop: $('#jsEmail1').offset().top}, 500);
			valid = false;
			return false;
		}
				
		if(($('#password1').val() == "") || ($('#password2').val() == ""))
		{
			$('#password1').addClass('error');
			$('#password2').addClass('error');
			
			valid = false;
			alert("Please enter a password into both boxes");
			$('html, body').animate({scrollTop: $('#password1').offset().top}, 500);
			return false;
		}

		var password = $('#password1').val();
		
		if(password.length < 6)
		{
			$('#password1').addClass('error');
			$('#password2').addClass('error');
			
			valid = false;
			alert("Please enter a password which is as least 6 characters long");
			$('html, body').animate({scrollTop: $('#password1').offset().top}, 500);
			return false;
		}
		
		if($('#password1').val() != $('#password2').val())
		{
			$('#password1').addClass('error');
			$('#password2').addClass('error');
			
			valid = false;
			alert("Please enter the same password into both boxes");
			$('html, body').animate({scrollTop: $('#password1').offset().top}, 500);
			return false;
		}
		
		// Now on to the addresses!
		if($('#billing1').val() == "")
		{
			$('#billing1').addClass('error');
			
			valid = false;
			alert("Please enter the first line of your billing address");
			$('html, body').animate({scrollTop: $('#billing1').offset().top}, 500);
			return false;
		}
		
		if($('#billingTown').val() == "")
		{
			$('#billingTown').addClass('error');
			
			valid = false;
			alert("Please enter the town of your billing address");
			$('html, body').animate({scrollTop: $('#billingTown').offset().top}, 500);
			return false;
		}
		
		if($('#billingCounty').val() == "")
		{
			$('#billingCounty').addClass('error');
			
			valid = false;
			alert("Please enter the county of your billing address");
			$('html, body').animate({scrollTop: $('#billingCounty').offset().top}, 500);
			return false;
		}
		
		if($('#billingPostcode').val() == "")
		{
			$('#billingPostcode').addClass('error');
			
			valid = false;
			alert("Please enter the postcode of your billing address");
			$('html, body').animate({scrollTop: $('#billingPostcode').offset().top}, 500);
			return false;
		}
		
		if($('#delivery1').val() == "")
		{
			$('#delivery1').addClass('error');
			
			valid = false;
			alert("Please enter the first line of your delivery address");
			$('html, body').animate({scrollTop: $('#delivery1').offset().top}, 500);
			return false;
		}
		
		if($('#deliveryTown').val() == "")
		{
			$('#deliveryTown').addClass('error');
			
			valid = false;
			alert("Please enter the town of your delivery address");
			$('html, body').animate({scrollTop: $('#deliveryTown').offset().top}, 500);
			return false;
		}
		
		if($('#deliveryCounty').val() == "")
		{
			$('#deliveryCounty').addClass('error');
			
			valid = false;
			alert("Please enter the county of your delivery address");
			$('html, body').animate({scrollTop: $('#deliveryCounty').offset().top}, 500);
			return false;
		}
		
		if($('#deliveryPostcode').val() == "")
		{
			$('#deliveryPostcode').addClass('error');
			
			valid = false;
			alert("Please enter the postcode of your delivery address");
			$('html, body').animate({scrollTop: $('#deliveryPostcode').offset().top}, 500);
			return false;
		}
		
		// Now run a double check
		if(valid == true)
		{
			return true;
		}
		else
		{
			return false;
		}
			
	});
});

// Add event listener to the edit customer form
$(document).ready(function()
{
	// When the document is ready
	$('#edit-customer').submit(function()
	{
		$('input').removeClass('error');
		
		// Assume all is good to go
		var valid = true;
		
		// Run through all fields and check
		if($('#firstname').val() == "")
		{			
			$('#firstname').addClass('error');
			
			valid = false;
			alert("Please enter your first name");
			$('html, body').animate({scrollTop: $('#firstname').offset().top}, 500);
			return false;
		}
		
		if($('#surname').val() == "")
		{
			$('#surname').addClass('error');
			
			valid = false;
			alert("Please enter your surname");
			$('html, body').animate({scrollTop: $('#surname').offset().top}, 500);
			return false;
		}
		
		if($('#telephone').val() == "")
		{
			$('#telephone').addClass('error');
			
			valid = false;
			alert("Please enter your telephone number");
			$('html, body').animate({scrollTop: $('#telephone').offset().top}, 500);
			return false;
		}
		
		if($('#jsEmail').val() == "")
		{
			$('#jsEmail').addClass('error');
			
			valid = false;
			alert("Please enter your email address");
			$('html, body').animate({scrollTop: $('#jsEmail').offset().top}, 500);
			return false;
		}
		
		if(checkValidEmail($('#jsEmail').val()) == false)
		{
			$('#jsEmail').addClass('error');
			$('html, body').animate({scrollTop: $('#jsEmail').offset().top}, 500);
			valid = false;
			return false;
		}
						
		// Now on to the addresses!
		if($('#billing1').val() == "")
		{
			$('#billing1').addClass('error');
			
			valid = false;
			alert("Please enter the first line of your billing address");
			$('html, body').animate({scrollTop: $('#billing1').offset().top}, 500);
			return false;
		}
		
		if($('#billingTown').val() == "")
		{
			$('#billingTown').addClass('error');
			
			valid = false;
			alert("Please enter the town of your billing address");
			$('html, body').animate({scrollTop: $('#billingTown').offset().top}, 500);
			return false;
		}
		
		if($('#billingCounty').val() == "")
		{
			$('#billingCounty').addClass('error');
			
			valid = false;
			alert("Please enter the county of your billing address");
			$('html, body').animate({scrollTop: $('#billingCounty').offset().top}, 500);
			return false;
		}
		
		if($('#billingPostcode').val() == "")
		{
			$('#billingPostcode').addClass('error');
			
			valid = false;
			alert("Please enter the postcode of your billing address");
			$('html, body').animate({scrollTop: $('#billingPostcode').offset().top}, 500);
			return false;
		}
		
		if($('#delivery1').val() == "")
		{
			$('#delivery1').addClass('error');
			
			valid = false;
			alert("Please enter the first line of your delivery address");
			$('html, body').animate({scrollTop: $('#delivery1').offset().top}, 500);
			return false;
		}
		
		if($('#deliveryTown').val() == "")
		{
			$('#deliveryTown').addClass('error');
			
			valid = false;
			alert("Please enter the town of your delivery address");
			$('html, body').animate({scrollTop: $('#deliveryTown').offset().top}, 500);
			return false;
		}
		
		if($('#deliveryCounty').val() == "")
		{
			$('#deliveryCounty').addClass('error');
			
			valid = false;
			alert("Please enter the county of your delivery address");
			$('html, body').animate({scrollTop: $('#deliveryCounty').offset().top}, 500);
			return false;
		}
		
		if($('#deliveryPostcode').val() == "")
		{
			$('#deliveryPostcode').addClass('error');
			
			valid = false;
			alert("Please enter the postcode of your delivery address");
			$('html, body').animate({scrollTop: $('#deliveryPostcode').offset().top}, 500);
			return false;
		}
		
		// Now run a double check
		if(valid == true)
		{
			return true;
		}
		else
		{
			return false;
		}
			
	});
});



//------------------------
//Billing and Delivery Address are the same so lets auto complete
//------------------------

$(function()
{
	$('#jsAutoComplete').click(autoCompleteAddress);
});

/**
 * Auto Complete for the customer address fields
 * 
 * @return
 */
function autoCompleteAddress() 
{	
	$('#delivery1').val($('#billing1').val());
	$('#delivery2').val($('#billing2').val());
	$('#delivery3').val($('#billing3').val());
	$('#deliveryTown').val($('#billingTown').val());
	$('#deliveryCounty').val($('#billingCounty').val());
	$('#deliveryPostcode').val($('#billingPostcode').val());
	$('html, body').animate({scrollTop: $('#delivery1').offset().top}, 500);
	// Country Menu
	document.getElementById("form-deliveryCountry").selectedIndex = document.getElementById("form-billingCountry").selectedIndex;
	
	return false;
}

/* End of Customer Functions */


/* Basket Function */

// Update postage cookie
function submitBasket()
{
	$('#full-basket').submit();
}

// Remove code and submit the basket
function removeBasketPromo()
{
	document.basket.promoCode.value = "";
	$('#full-basket').submit();
}

// Update postage details on order confirmation page
function submitPostageChange()
{
	document.postage.submit();
}


$(function(){
	$('#postageRegion').change(function(){
		$('#full-basket').submit();
	});
	
	$('#postageUpgrade').change(function(){
		$('#full-basket').submit();
	});
})

/* End of Basket Functions */


/* Account Functions */

/**
 * Validates the account profile form
 * 
 * @return
 */
function validateContactForm()
{
	// Assume we are okay unless otherwise
	var valid = true;
	
	$('input').removeClass('error');
	
	// $('#deliveryCounty').addClass('error');
	
	// valid = false;
	// alert("Please enter the county of your delivery address");
	// $('html, body').animate({scrollTop: $('#deliveryCounty').offset().top}, 500);
	// return false;
	
	if($('#firstname').val() == "")
	{
		$('#firstname').addClass('error');
		valid = false;
		alert("Please enter your firstname");
		$('html, body').animate({scrollTop: $('#firstname').offset().top}, 500);
		return false;
	}
	
	if($('#surname').val() == "")
	{
		$('#surname').addClass('error');
		valid = false;
		alert("Please enter your surname");
		$('html, body').animate({scrollTop: $('#surname').offset().top}, 500);
		return false;
	}
	
	if($('#telephone').val() == "")
	{
		$('#telephone').addClass('error');
		valid = false;
		alert("Please enter your telephone number");
		$('html, body').animate({scrollTop: $('#telephone').offset().top}, 500);
		return false;
	}
	
	if(valid == true)
	{
		$('#contact').submit();
	}
	else
	{
		return false;
	}
}

/**
 * Validates the new address form in accounts section
 * 
 * @return
 */
function validateNewAddress()
{
	// Assume we are okay unless otherwise
	var valid 	= true;
	
	$('input').removeClass('error');
	
	if($('#address1').val() == "")
	{
		$('#address1').addClass('error');
		valid = false;
		alert("Please enter your address");
		$('html, body').animate({scrollTop: $('#address1').offset().top}, 500);
		return false;
	}
	
	if($('#town').val() == "")
	{
		$('#town').addClass('error');
		valid = false;
		alert("Please enter your town");
		$('html, body').animate({scrollTop: $('#town').offset().top}, 500);
		return false;
	}
		
	if($('#county').val() == "")
	{
		$('#county').addClass('error');
		valid = false;
		alert("Please enter your county");
		$('html, body').animate({scrollTop: $('#county').offset().top}, 500);
		return false;
	}
	
	if($('#postcode').val() == "")
	{
		$('#postcode').addClass('error');
		valid = false;
		alert("Please enter your postcode");
		$('html, body').animate({scrollTop: $('#postcode').offset().top}, 500);
		return false;
	}
	
	if(valid == true)
	{
		$('#addressform').submit();
	}
	else
	{
		return false;
	}
}

/**
 * Checks the password values for the edit password part of accounts
 * 
 * @return
 */
function checkPassword()
{
	var valid   = true;
	var message = "";
	
	// Go through the password fields to ensure all is working
	if(document.security.password1.value == "")
	{
		valid = false;
		message = "Please enter a new password into both boxes";
		alert(message);
		return;
	}
	
	if(document.security.password2.value == "")
	{
		valid = false;
		message = "Please enter a new password into both boxes";
		alert(message);
		return;
	}
	
	if(document.security.password1.value.length < 6)
	{
		valid = false;
		message = "Please enter a new password which is at least 6 characters long";
		alert(message);
		return;
	}
	
	if(document.security.password2.value.length < 6)
	{
		valid = false;
		message = "Please enter a new password which is at least 6 characters long";
		alert(message);
		return;
	}
	
	if(document.security.password1.value != document.security.password2.value)
	{
		valid = false;
		message = "Please check that you have entered the same new password into both boxes";
		alert(message);
		return;
	}
	
	if(valid == true)
	{
		document.security.submit();
	}
	else
	{
		return false;
	}
}

/* End of Account Functions */

// GLOBALLY USED FUNCTIONS

/**
 * Email Validator Function
 * 
 * @param 	str
 * @return	boolean
 */
function checkValidEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

 	return true;				
}


/**
 * Email Validator Function
 * 
 * @param 	str
 * @return	boolean
 */
function checkValidEmailWithoutMessage(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		return false;
	}

 	return true;				
}

/*
 * Run this when the Dom is ready
 */
$(function(){
	
	// Call out setupImageSwap
	setupImageSwap('#scrollWrapper',590,600);
});

// Set a global variable here
var moving = false;
var timer = 0;
var firstItem = "";

/**
 * Sets Up our Image Animations
 * 
 * 
 * @param parent	The item containing the items to be moved
 * @param distance	The width of a single item so we know how far to animate
 */
function setupImageSwap(parent,distance,speed){
	
	/* We need to find the first and last items to begin with 
	by using direct descenders and first and last sudo classes
	*/
	
	firstItem 	= $(parent).find('>:first');
	var lastItem 	= $(parent).find('>:last');
	
	// Okay great now we have those...
	
	// First the move left (right arrow)
	$(firstItem).unbind('click').one('click',function(){
		// First clear the timer
		clearTimeout(timer);
		// ONLY ACTIVATE ONCE WE HAVE DONE MOVING
		if(moving == false){
			
			// We're moving, set it to true
			moving = true;
			
			// Animate the first item
			$(firstItem).css({'marginleft':'0px'})			// First set margin to 0
							 .animate({'marginLeft': '-'+distance+'px'}, 
							 speed,	
							 function(){
								 
								 // Okay animation complete set moving to false
								 moving = false;
								 
								 // Reset margin and detach
								 $(firstItem).css({'marginLeft':''}).detach();
								 
								 // Now insert it after the last item
								 $(firstItem).insertAfter(lastItem);
								 
								 // Recall ourselves
								 setupImageSwap(parent,distance,speed);
								 
							});
		} else {
			// DO NOTHING WE'RE STILL ANIMATING
		}
		
	});
	
	// First clear the timer
	clearTimeout(timer);
	// Run a timer
	timer = setTimeout("$(firstItem).click()", 4000);
}


/**
 * Ready Feedback for sending
 * 
 */
$(function(){
	$('#feedback-form').submit(function(){
		// Assume we are ready
		var ready = true;

			// We need to check the textarea
			if($('#comment').val() == ""){
				// If we are blank set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('#comment').addClass('error');

				
			}else{
				// we don't need to change anything
				
				// Make sure we remove any error classes
				$('#comment').removeClass('error');
			}
		
		// Okay we only have 2 input types so if we are ready we can continue
		if(ready == true){
			// Allow the form to submit
			return true;
		}else{
			// Alert user and don't let the form submit
			alert('Please provide a comment to submit your feedback');
			// Return false
			return false;
		}
	});
});

$(function(){
	$('#catalogue-download').submit(function(){
		// Assume we are ready
		var ready = true;
		
		// Cycle through each of the input fields
		$('.formrow-required input').each(function(index){
			
			// If this input is blank
			if($(this).val() == ""){
				// Set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$(this).addClass('error');

				
			}else{
				// Otherwise do nothing as we are assuming ready and don't want to reset it
				
				// Make sure we remove any error classes
				$(this).removeClass('error');
			}
		});
		
		// We need to check for a valid email address
		if(ready == true){
			
			// Pass the email address to the valid email checking thingy
			if(checkValidEmailWithoutMessage($('input#email').val()) == true){
				// Has passed don't do anything
				
				// Make sure we remove any error classes
				$('input#email').removeClass('error');
				
			}else{
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('input#email').addClass('error');
			}
		}
		
		// Okay we only have 2 input types so if we are ready we can continue
		if(ready == true){
			
			$('#catalogue-download').hide('fast',function(){
				$(this).parent().append('<p><strong>Thank you for downloading the Tozer Seeds Catalogue 2011-2012</strong></p><p>If you have any download problems or questions please contact us on <strong>01932 862059</strong>.</p><p><a href="/" alt="Back to Homepage">Back to Homepage</a></p>');
			});
			
			// Allow the form to submit
			return true;
			
		}else{
			// Alert user and don't let the form submit
			alert('Please fill in all the required fields, and ensure you have entered a valid email address');
			// Return false
			return false;
		}
	});
});


