$(document).ready(function() {
	//Currency
	$('div.currency').hover(function() {
		$('span.currency-choice').show();
	}, function() {
		$('span.currency-choice').hide();
	});
	
	//Basket
	$('a.basket').hover(function() {
		$('span.basket-more').show();
	}, function() {
		$('span.basket-more').hide();
	});
	
	//Font Replace
	Cufon.replace('.rep', {fontFamily: 'Century Gothic'});
	Cufon.replace('.pri, .trep', {fontFamily: 'Myriad Pro'});
	
	//Bookmark
	if(!window.opera) {
		$("a.bookmark").click(function(event){
			event.preventDefault();
			var url = this.href;
			var title = this.title.replace(/Bookmark /, '');
			var ua=navigator.userAgent.toLowerCase();
			var isKonq=(ua.indexOf('konqueror')!=-1);
			var isSafari=(ua.indexOf('webkit')!=-1);
			var isMac=(ua.indexOf('mac')!=-1);
			var buttonStr=isMac?'Command/Cmd':'CTRL';
		
			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} else if(window.external) { // IE Favorite
				window.external.AddFavorite(url, title);
			} else if(window.home || isSafari) { // Firefox, Netscape, Safari, iCab
			  alert('You need to press '+buttonStr+' + D to bookmark our site.');
			} else if(!window.print || isMac) { // IE5/Mac and Safari 1.0
			  alert('You need to press Command/Cmd + D to bookmark our site.');    
			} else {
			  alert('In order to bookmark this site you need to do so manually through your browser.');
			}
		});
	}
	
	//Lightbox
 	$("a.lightbox").fancybox({
		'overlayShow'			: false,
		'zoomSpeedIn'			: 600,
		'zoomSpeedOut'			: 500,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack'
	});
	
	//Newsletter Form
	$('form#newsletterForm').submit(function() {
		var theEmail = this.newsletterEmail.value;
		theEmail = theEmail.split(' ').join('');
		if(theEmail == "" || theEmail == "email address" || theEmail == "emailaddress") {
			alert('Please enter your email address.');
			return false;
		}
		if(theEmail !== "" && theEmail !== "email address") {
			validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
			if (theEmail.search(validRegExp) == -1) {
				alert('Please enter a valid email address.');
				return false;
			}
		}
		return true;
	});
	
	//Callback
	$('#callbackform').ajaxInit();
	
	//Contact Form
	$('form#contactForm').submit(function() {
		var contactName = this.contactName.value;
		var contactEmail = this.contactEmail.value;
		var contactQuery = this.contactQuery.value;
		var setError = 0;
		var setErrorLog = "";
		
		if(contactName == "") {
			setError = 1;
			setErrorLog += "+ Please enter your name.\n";
		}
		if(contactEmail == "") {
			setError = 1;
			setErrorLog += "+ Please enter your email address.\n";
		}
		if(contactQuery == "") {
			setError = 1;
			setErrorLog += "+ Please enter your enquiry.\n";
		}
		if(setError == 1) {
			alert(setErrorLog);
			return false;
		}		
		return true;
	});
	
	//Education Form
	$('form#educationForm').submit(function() {
		var educationName = this.educationName.value;
		var educationSchool = this.educationSchool.value;
		var educationEmail = this.educationEmail.value;
		var setError = 0;
		var setErrorLog = "";
		
		if(educationName == "") {
			setError = 1;
			setErrorLog += "+ Please enter your name.\n";
		}
		if(educationSchool == "") {
			setError = 1;
			setErrorLog += "+ Please enter the name of your school/college.\n";
		}
		if(educationEmail == "") {
			setError = 1;
			setErrorLog += "+ Please enter your email address.\n";
		}
		if(setError == 1) {
			alert(setErrorLog);
			return false;
		}		
		return true;
	});
	
	//Checkout
	$('#invaddr').hide();
	
	function IsEmpty(aTextField) {
	   if ((aTextField.value.length==0) || (aTextField.value==null)) {
		  return true;
	   } else {return false;}
	 }
	function IsNumeric(sText) {
	   var ValidChars = "0123456789.-";
	   var IsNumber=true;
	   var Char;
	 
	   for (i = 0; i < sText.length && IsNumber == true; i++) { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		  }
	   }
	   return IsNumber;
	}
	function numberonly(evt) {
	  var charCode = (evt.which) ? evt.which : event.keyCode
	  if (charCode > 31 && (charCode < 48 || charCode > 57))
	  return false;
	  return true;
	}
	$('form#checkoutDelivery').submit(function() {
		if(this.step.value == '3') {
			var error = "0";
			var errors = "The following errors were found:\n\n";	
			if (IsEmpty(this.delivery_name)) {
				error = "1";
				errors += "+ Please enter your full name (e.g Mr John Smith)\n";
			}
			/*if (IsEmpty(this.delivery_housenumber)) {
				error = "1";
				errors += "+ Please enter your house/flat number\n";
			}*/
			if (IsEmpty(this.delivery_street)) {
				error = "1";
				errors += "+ Please enter your street\n";
			}
			if (IsEmpty(this.delivery_town)) {
				error = "1";
				errors += "+ Please enter your town/city\n";
			}
			if (IsEmpty(this.delivery_county)) {
				error = "1";
				errors += "+ Please enter your county/region\n";
			}
			if (IsEmpty(this.delivery_postcode)) {
				error = "1";
				errors += "+ Please enter your postal code\n";
			}
			if (IsEmpty(this.delivery_telephone)) {
				error = "1";
				errors += "+ Please enter your telephone number\n";
			}
			if (IsEmpty(this.delivery_email)) {
				error = "1";
				errors += "+ Please enter your email address\n";
			}
			if (!IsEmpty(this.delivery_email)) {
				validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
				if (this.delivery_email.value.search(validRegExp) == -1) {
					error = "1";
					errors += "+ Your email address is not valid.\n";
				}
			}
			if (error == "1") {
				alert(errors);
				return false;
			}
		}
		return true;
	});
	$('form#checkoutPayment').submit(function() {
		if(this.step.value == '4') {
			var error = "0";
			var errors = "The following errors were found:\n\n";	
			if (IsEmpty(this.card_name)) {
				error = "1";
				errors += "+ Please enter the card holders name (e.g Mr John Smith)\n";
			}
			if (IsEmpty(this.card_number)) {
				error = "1";
				errors += "+ Please enter the card number (do not use hyphens)\n";
			}
			if (!IsNumeric(this.card_number.value)) { 
				error = "1";
				errors += "+ Please enter the card number (do not use hyphens)\n";
			}
			if (IsEmpty(this.card_ccv)) {
				error = "1";
				errors += "+ Please enter the CCV (security number)\n";
			}
			if (!IsNumeric(this.card_ccv.value)) { 
				error = "1";
				errors += "+ Please enter the CCV (security number)\n";
			}
			if (this.card_expires_month.value == "") {
				error = "1";
				errors += "+ Please select the month the card expires\n";
			}
			if (this.card_expires_year.value == "") {
				error = "1";
				errors += "+ Please select the year the card expires\n";
			}
			if (error == "1") {
				alert(errors);
				return false;
			}
		}
		return true;
	});
});

//Switch Tab
function switchTab(id) {
	for(var i=1; i<7; i++) {
		$('#tab'+i).hide();
	}
	$('#tab'+id).show();	
	
	for(var i=1; i<7; i++) {
		$('.t'+i).removeClass('active');
	}
	$('.t'+id).addClass('active');
}

//Callback AJAX
$.fn.ajaxInit = function(e) {
	this.submit(function(){
	  var cb_name = $("#cb_name").val();
	  var cb_telephone = $("#cb_telephone").val();
	  var cb_when = $("#cb_when").val();
	  if(cb_name == "" || cb_name == "Name" || cb_telephone == "" || cb_telephone == "Phone Number") {
		alert("For us to call you back please leave us your name, telephone number and when you'd like us to give you a call.");
		return false;
	  } else {
		var dataString="type=callback&name="+cb_name+"&telephone="+cb_telephone+"&when="+cb_when
		$.ajax({
		  type: "POST", 
		  url: "/includes/ajax.inc.php", 
		  data: dataString, 
		  timeout: 3000,
		  error: function() { 
		  	alert('Please try again later...');
			return false;
		  },
		  success: function() {
			  	$('#callback-loading').html('<img src="/images/icons/loading.gif" alt="" style="padding-top: 42px; display:block; margin: 0 auto" />');
			  	$('#callback-show').fadeOut('fast', function() {
					$('#callback-loading').fadeIn('slow');
				});
		  },
		  complete: function() {
			  	setTimeout("$('#callback-loading').fadeOut('fast', function() {$('#callback-loading').html('Thank you!<br /><br />The callback request has been sent to our team, you can expect a callback shortly, please remember we will call you back during office hours.');$('#callback-loading').fadeIn('fast');});", [3200]);
		  }
		});
		return false;
	  }
	});
}
