/*
Herbies Head Shop - Javascript File
Last Modified - 08/12/2009
Created by Danny Hayfield for West Midlands Media Ltd
*/

$(document).ready(function() {
	//Firstly, hide the magic
	$('.magic').hide();
	$('#seolinks').hide();
	if($('#shoplist')) {
		$('#shoplist').hide();	
	}
	
	//Cufon
	Cufon.replace('.replace', {hover: 'true', fontFamily: 'UnitRoundedOT-Bold'});
	
	//Finally, let the replaced be seen (ie)
	$('.replace').css('visibility','visible');
	
	//Flash Header
	var d = new Date();
	var monthName = d.getMonth(); //Numberic value of month
	if(monthName == "11") {
		var headerName = "headerXmas.swf";	
	} else {
		var headerName = "header.swf";	
	}
	
	//var timestamp = new Date(); (timestamp.getTime()-timestamp.getMilliseconds())/1000);
	var flashvars = {};
	var params = {base: "images/flash/", menu: "false",wmode: "transparent", scale:"false"};
	var attributes = {}; 
	swfobject.embedSWF("/images/flash/"+headerName, "flash", "900", "190", "9.0.0", "", flashvars, params, attributes);
	
	//Large Image Preview
	$(function() {		
		$('.product-image-larger').live({
			mouseenter:
			   function() {
					$('.largepreview').show();
			   }
		});		
		$('.largepreview').live({
			mouseleave:
			   function() {
					$('.largepreview').hide();
			   }
		});		
	});
	
	//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.');
			}
		});
	}
	
	if(document.getElementById('fakeh1') && document.getElementById('trueh1')) {
		document.getElementById('trueh1').innerHTML = document.getElementById('fakeh1').innerHTML;
		document.getElementById('fakeh1').innerHTML = '';
		$('#fakeh1').hide();
	}
});

/* add this bookmark */
var bookmarkURL = encodeURIComponent(window.location);
var bookmarkTitle = encodeURIComponent(document.title);
var bookmarkUser = "herbiesheadshop";
var bookmarkLink = "http://addthis.com/bookmark.php?pub="+bookmarkUser+"&url="+bookmarkURL+"&title="+bookmarkTitle+"&s=";
function bookmark(site){		
	var theWindow = window.open(bookmarkLink+site, "_blank", "location=0, statusbar=0, menubar=0, width=770, height=480, scrollbars=1");
	theWindow.focus();
	return false;	
}

/* price match promise hover */
$(function() {
	$('div.price-match-tooltip').hide();
	$('.price-match').hover(function() {
		$(this).children('div').html('<span class="tooltip-title">Price Match<br /> Promise!</span>All my products come under my price match<br />guarantee. Find the same product cheaper<br />on a different site, contact me and I\'ll try to<br /> beat that price.<br /><br />(only genuine seeds/sites please!)');
		$(this).children('div').show();
	},
	function() {
		$(this).children('div').hide();
		$(this).children('div').html('');
	});
});

/* view all manufacturers */
$(function() {
	/*$('.viewall').click(function() {
		$('.othercategories').show();
		$('.viewall').fadeOut(500);
	});*/
});


/* drop footer toggle */
$(function() {
	$('.subnav').click(function(event) {
		if($('#seolinks').css('display') == "none") {
			$('#seolinks').css('display','block');
		} else {
			$('#seolinks').css('display','none');
		}
		return false;
	});
});

function checkterms(name) {
	if($('input[name='+name+']').is(':checked')) {
		return true;	
	} else {
		alert('Please agree to the terms and conditions.');
		return false;	
	}
}

/*******************************************/

/* checkout country delivery options */
$(function() {
	$('select.checkout_country').change(function() {
		var country = $("select.checkout_country option:selected").val();
		
		if(country == "") {
			return false;
		} else if(country == "United Kingdom") {
			$('select.delivery_method').empty().append($('#uk_del_options').html()).find('option:first').attr('selected','selected');
		} else {
			$('select.delivery_method').empty().append($('#int_del_options').html()).find('option:first').attr('selected','selected');
		}
	});
});

/* validation functions */
function isValidEmail(str) {
    email = str;
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	
	if (AtPos == -1 || StopPos == -1) {
		return false;
	}
}
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;
}


/* contact form check */
$(function() {
	$('#contact_form').submit(function() {		
		if(this.contact_name.value == "" || this.contact_email.value == "" || this.contact_query.value == "" || isValidEmail(this.contact_email.value) == false) {
			alert("Please enter your name, email address and enquiry.");
			return false;
		}
		return true;
	});
});

/* process checkout */ 
$(function() {
	$('#checkout_delivery').submit(function() {
		var error = "0";
		var errors = "The following errors occured:\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 address\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_confirm)) {
			error = "1";
			errors += "- Please confirm your email address\n";
		}
		if (!IsEmpty(this.delivery_email)) {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if (reg.test(this.delivery_email.value) == false) {
				error = "1";
				errors += "- Your email address is not valid.\n";
			}
			if (this.delivery_email.value !== this.delivery_email_confirm.value) {
				error = "1";
				errors += "- Your confirmed email address does not match.\n";
			}
		}

		if (error == "1") {
			alert(errors);
			return false;
		}
		return true;
	});
});

$(function() {
	$('#paynow').click(function() {
		var form = document.checkout_payment;
		var error = "0";
		var errors = "The following errors occured:\n\n";	
		if (IsEmpty(form.card_name)) {
			error = "1";
			errors += "- Please enter the card holders name (e.g MR J SMITH)\n";
		}
		if (IsEmpty(form.card_number)) {
			error = "1";
			errors += "- Please enter the card number (digits only)\n";
		}
		if (!IsNumeric(form.card_number.value)) { 
		  error = "1";
		  errors += "- Please enter the card number (digits only)\n";
		}
		if (IsEmpty(form.card_ccv)) {
			error = "1";
			errors += "- Please enter the CCV (security number)\n";
		}
		if (!IsNumeric(form.card_ccv.value)) { 
		  error = "1";
		  errors += "- Please enter the CCV (security number)\n";
		}
		if (form.card_expires_month.value == "") {
			error = "1";
			errors += "- Please select the expires end month\n";
		}
		if (form.card_expires_year.value == "") {
			error = "1";
			errors += "- Please select the expires end year\n";
		}
		if (error == "1") {
			alert(errors);
			return false;
		}
		return true;
	});
});
