/**
 * DOM Ready Functions
 */
$(function(){
	enableReservations();
});

function enableReservations(){
	
	/* Need to add a click event to the reservation links */
	$('a.jsReserveMyCopy').click(function(e){
		
		/* Stop the default action */
		e.preventDefault();
		
		/* Pull put the productId */
		var $productId = $(this).attr('data-product');
		
		/* Get the reserve form */
		$.get('/reserve-form/',{
			productId:$productId
		},function(data){
			
			/* If the data isn't blank */
			if(data!=""){
				/* Append this into the body */
				$('body').append(data);
				
				$('span#jsCloseReserveForm').click(function(){
					
					/* Fade them out */
					$('div#jsModalMask, div#jsReserveForm').fadeOut('normal',function(){
						
						/* Remove from dom */
						$('div#jsModalMask, div#jsReserveForm').remove().detach();
					});
				});
				
				/* Now setup the form */
				$('form#reservation-form').submit(function(){
					var ready = true;
					
					// Validate the form
					if($('#reserveName').val() == ""){
						ready = false;
						alert("Please enter your name.");
					}
					
					if($('#reserveEmail').val() == "" && ready == true){
						ready = false;
						alert("Please enter your email address.");
					}
					
					if( ready == true  && checkValidEmail($('#reserveEmail').val()) == false){
						ready = false;
					}
					
					if($('#reservePhone').val() == "" && ready == true){
						ready = false;
						alert("Please enter your telephone number.");
					}
					
					if(ready){
						/* we're ready, serialize the form */
						var formData = $('form#reservation-form').serialize();
						
						$('div#jsReserveForm p:first').html("Please wait while we send your reservation request, this should only take a few seconds...");
						
						$('form#reservation-form').fadeOut('normal');
						
						$('div#jsReserveForm').animate({
								height:"60px"
							},'500',function(){
							
							/* Post to the reservation system */
							$.post('/reserve-my-copy/',formData,function(data){
								
								/* If data == success */
								if(data == "Success"){
									
									/* need to clear these out of the system */
									$('div#jsReserveForm p:first').html("Thank you, we have received your reservation, you will be notified when this item is available.");
									
									/* Fade them out after a delay and remove them */
									$('div#jsModalMask, div#jsReserveForm').delay('5000').fadeOut('normal',function(){
										
										/* Remove from dom */
										$('div#jsModalMask, div#jsReserveForm').remove().detach();
									});
								}
							});
							
						});
						
						return false;
						
					}else{
						/* Return false */
						return false;
					}
				});
			}
		});
		
		/* return false */
		return false;
		
	});
}


/**
 * Reusable Function for centering items,
 * container must be relative
 * item must be absolute
 * 
 * @param item
 */
function centreMe(item){
	$(item).css({
		left:($(item).parent().outerWidth()/2) - ($(item).width()/2),
		top:($(item).parent().outerHeight()/2) - ($(item).height()/2)
	});
	// Fade out the mask
	$(item).next('.mask').fadeOut('normal');
	$(item).parent().next('.mask').fadeOut('normal');
}


/** THIS HANDLES THE PRODUCT PAGE TABS **/
$(function(){
	
	$(window).load(function(){
		
		if($('a.product_image img').length > 0){
			$('a.product_image img').each(function(index){
				centreMe($(this));
			});
		}
		
		
		if($('a#largeImage img').length > 0){
			$('a#largeImage img').each(function(index){
				centreMe($(this));
			});
		}
		
		
		// Check for product images first
		if($('img','.product-item-image a').length > 0){
			// Loop through all product images
			$('img','.product-item-image a').each(function(index){
				// Run the centreme function
				centreMe($(this));
			});
		}
	});
	
	// Show the panel tabs
	// They would be hidden normally since we are gracefully degrading
	$('.infoTab').show();
	
	// Each infopanel
	$('.infoPanel').each(function(index){
		// Does it have a class of selected
		if($(this).hasClass('selected')){
			// yes, do nothing
		}else{
			// no hide it
			$(this).hide();
		}
	});
	
	// When click on an infotab
	$('.infoTab').click(function(){
		// Remove all infotab selected states
		$('.infoTab').removeClass('selected');
		// Add selected to the clicked item
		$(this).addClass('selected');
		
		// Get the id from this attr
		var id = $(this).attr('id');
		// Replace tab with panel in the id
		id = id.replace("tab","panel");
		// Hide the infopanle
		$('.infoPanel').hide();
		// Now show the panel with the correct name
		$('#'+id).show();
		// And focus
		$(this).focus();
		
	});
	
	
	// Create something that will fire tab 2 off should the review link be clicked.
	$('p.review-link').click(function(){
		
		// Fire a click on tab 2
		$('#tab2').click();
		
		// Scroll down to it in 500ms
		$('html, body').animate({scrollTop: $('#panel2').offset().top}, 500);
	});
});

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

/**
 * Product Image Click Handler
 */
function newImageHandler(){
	
	//lightbox link handler
	$('a.product_image').lightBox({fixedNavigation:true});
	
	/*$('p.jsProductImage').click(function(){						// Each a tag with class jsProductImage
		$('#productImageImg').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
		
		$('#cloud-zoom').attr('href', $(this).attr('href')).attr('title', $(this).attr('title'));
		$('#cloud-zoom').data('zoom').destroy();
		$('#cloud-zoom').CloudZoom();*/
		
		
		/* we also need to check the value of this item against our product images */
		/* var groupVal = $(this).attr('rel2');
		
		// Replace spaces with slashes
		groupVal = groupVal.replace("group-","");
		groupVal = groupVal.replace("-"," ");
		groupVal = groupVal.replace("-"," ");
		groupVal = groupVal.replace("-"," ");
		groupVal = groupVal.replace("-"," ");
		groupVal = groupVal.replace("-"," ");
		//alert(groupVal);
		
		/* Change the selected item in the dropdown 
		$('#ProductOptions1Wrapper select').val(groupVal).attr('selected',true);
		
		counter = $('#ProductOptions1Wrapper select').attr("counter");
		
		// Hack attack
		if($('#ProductOptions1Wrapper select').attr("attributeId" == 2)){
			// We need to use the opposite attribute to what we have already got
			attributeId = 3;
		}
		else{
			attributeId = 2;
		}
		productId = $('#ProductOptions1Wrapper select').attr("productId");
		parentValue = $('#ProductOptions1Wrapper select').val();
		
		loadProductMenu2(counter, attributeId, productId, parentValue);
		
		// Get the value of this option
		var option1 = $('[name=attribute3] option:selected').val();
		
		// We want the product id
		var productId = $('[name=attribute3]').attr("productId");
		
		// Chuck this to the updater only if we have a product Id
		if(productId){
			updatePrice(option1, productId);
		}
		
		return false;
	});*/
}

$(function() {
	// Add on change to product option one.
	$("#ProductOptions1").change(function(){
			counter = $(this).attr("counter");
			
			// Hack attack
			if($(this).attr("attributeId" == 2)){
				// We need to use the opposite attribute to what we have already got
				attributeId = 3;
			}
			else{
				attributeId = 2;
			}
			productId = $(this).attr("productId");
			parentValue = $(this).val();
			
			loadProductMenu2(counter, attributeId, productId, parentValue);
			
			/* we also need to check the value of this item against our product images */
			var groupVal = $(this).val();
			
			/* Replace spaces with slashes */
			groupVal = groupVal.replace(" ","-");
			groupVal = groupVal.replace(" ","-");
			groupVal = groupVal.replace(" ","-");
			groupVal = groupVal.replace(" ","-");
			groupVal = groupVal.replace(" ","-");
			
			// alert(groupVal);

			/* Make this item click (it can only be an image) */
			$('p.jsProductImage[rel2=group-'+groupVal+']').click();
		}
	);
});


function loadProductMenu2(counter, attributeId, productId, parentValue){
	// Load 
	$.ajax({url: "/ajax/options/?counter="+counter+"&attributeId="+attributeId+"&productId="+productId+"&parentValue="+parentValue,
		  	success: function(data){
		    			$('#ProductOptions2Wrapper').html(data);
		    			setupSecondAttributes();
		    			loadPrice();
		  			}
			});
}

$(function(){
	
	setupSecondAttributes();
	
	// If the form item name attribute 1 changes run a function
	$('[name=attribute3]').change(function(){
		// Get the value of this option
		var option1 = $('[name=attribute3] option:selected').val();
		
		// We want the product id
		productId = $(this).attr("productId");
		
		// Chuck this to the updater only if we have a product Id
		if(productId){
			updatePrice(option1, productId);
		}

	});

});

function setupSecondAttributes(){
	// If the form item name attribute 2 changes run a function
	$('[name=attribute2]').change(function(){
		// Get the value of this option
		var option2 = $('[name=attribute2] option:selected').val();
		
		// We want the product id
		productId = $(this).attr("productId");
		
		// Chuck this to the updater only if we have a product Id
		if(productId){
			updatePrice(option2, productId);
		}
		
	});
}

// This will call in the ajax required to update the price shown on the site
function updatePrice(option, productId){
	/* REWRITING THE WAY THIS WORKS PLEASE */
	
	var option1 = "";
	var option2 = "";
	
	/* First do we have an attribute 3? */
	if($('[name=attribute3]').length > 0){
		// Is it nto blank, and not please select?
		if(($('[name=attribute3]').val() != "Please select ->") && ($('[name=attribute3]').val() != "")){
			// Set value of option 1
			option1 = $('[name=attribute3]').val();
		}
	}
	
	/* Now do we have an attribute 2? */
	if($('[name=attribute2]').length > 0){
		// Is it nto blank, and not please select?
		if(($('[name=attribute2]').val() != "Please select ->") && ($('[name=attribute2]').val() != "")){
			// Set value of option 2
			option2 = $('[name=attribute2]').val();
		}
	}
	
	/* Now run our get request */
	$.get("/ajax/prices/",{
		skuOption1:option1,
		skuOption2:option2,
		productId:productId
	},function(data){
		/* Put the data into the productPrices area */
		$('div#productPrices').html(data);
	});

}

/**
 * Will run the updater code on load
 * 
 * 
 */
$(function(){
	counter = $("#ProductOptions1").attr("counter");
	
	// Hack attack
	if($("#ProductOptions1").attr("attributeId" == 2)){
		// We need to use the opposite attribute to what we have already got
		attributeId = 3;
	}
	else{
		attributeId = 2;
	}
	productId = $("#ProductOptions1").attr("productId");
	parentValue = $("#ProductOptions1").val();

	loadProductMenu2(counter, attributeId, productId, parentValue);
});

/**
 * Will run the price call on load
 * 
 */
function loadPrice()
{
	// Check for an option 1
	var option1 = $('[name=attribute3] option:selected').val();
	
	// Check for a product Id
	var productId1 = $('[name=attribute3]').attr("productId");
	
	if(productId1){
		updatePrice(option1, productId1);
	}
	
	// Check for an option 2
	var option2 = $('[name=attribute2] option:selected').val();
	
	// Check for product Id
	var productId2 = $('[name=attribute2]').attr("productId");
	
	if(productId2){
		updatePrice(option2, productId2);
	}
}

/**
 * Handles clicks for the add to basket button
 * 
 */
$(function(){
	// On out basket button click
	$('#product-add-basket').click(function(){
		// Check the value of attribute 1
		if($('[name=attribute3]').val() == "Please select ->"){
			// Alert an error
			alert("Please select an option from the drop down menus to add an item to your basket.");
			
			// Return false so we can't post a nothing
			return false;
		}
		// Now check attribute 2
		else if($('[name=attribute2]').val() == "Please select ->"){
			// Alert an error
			alert("Please select an option from the drop down menus to add an item to your basket.");
			
			// Return false so we can't post a nothing
			return false;
		}
		else{
			// Else return true and good to go
			return true;
		}
		
	});
});


$(function(){
	$('#basket-show, #alt-basket-show').click(showMiniBasket);
});

var basketMoving = false;

/**
 * Basket show handler
 * @returns {Boolean}
 */
function showMiniBasket(){
	
	// Is the basket moving now?
	if(basketMoving == false){
		// it is now!
		basketMoving = true;
		
		// Run an unbind
		$('#basket-show').unbind('click');
		
		// Hide Select boxes that are underneath if there are any
		$('#product-options').find('select').each(function(){
			$(this).hide();
		});
		
		// Display the hidden basket
		$('#hidden-basket').slideDown('400',function(){
			// Set moving to false
			basketMoving = false;
			// Now add a new click event
			$('#basket-show').click(hideMiniBasket);
			// Add a click to the body that will hide the basket
			$('body, html').one('click',hideMiniBasket);
			// Create a none-propagation link so that if we click the basket it won't dissapear!
			$('#small-basket').click(function(event){
				// Stop click propagation (to the body or html)
				event.stopPropagation();
			});
		});
	}
	// Return false
	return false;
}

/**
 * Basket hide handler
 * @returns {Boolean}
 */
function hideMiniBasket(){
	
	// Is the basket moving now?
	if(basketMoving == false){
		
		// it is now
		basketMoving = true;
		
		// Run an unbind
		$('body, html, #basket-show, #small-basket').unbind('click');
		// Slide up
		$('#hidden-basket').slideUp('400',function(){
			// Set moving to false
			basketMoving = false;
			// Now add new click event
			$('#basket-show').click(showMiniBasket);
			// Show Select boxes that are underneath if there are any
			$('#product-options').find('select').each(function(){
				$(this).show();
			});
		});
	}
	// Return false
	return false;
}

/**
 * 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;
	});
});

/**
 * 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');
}



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



/* 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($('input#telephone').val() == "")
	{
		$('input#telephone').addClass('error');
		valid = false;
		alert("Please enter your telephone number");
		$('html, body').animate({scrollTop: $('input#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;				
}


/**
 * NEW CUSTOMER SCRIPTS in accounts section
 * 
 * @return
 */
//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($('input[name=telephone]').val() == "")
		{
			$('input[name=telephone]').addClass('error');
			
			valid = false;
			alert("Please enter your telephone number");
			$('html, body').animate({scrollTop: $('input[name=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
$(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($('input[name=telephone]').val() == "")
		{
			$('input[name=telephone]').addClass('error');
			
			valid = false;
			alert("Please enter your telephone number");
			$('html, body').animate({scrollTop: $('input[name=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;
		}
			
	});
});


$(function(){
	
	/* Get the height of the left div */
	var $leftHeight = $('#productLeft').outerHeight();
	
	var $rightHeight = $('#productHeading').outerHeight() + $('#productSummary').outerHeight() + $('#product-buying-section').outerHeight();
	
	var $dif = $leftHeight - $rightHeight;
	
	if($dif > 200){
		$dif = $dif - $('#productTabs').outerHeight();
		$dif = $dif - 76;
		$('.infoPanel').css({height:$dif});
	}
});


