$(document).ready(function(){

	/***********************************************************************
	Document enhancement
	***********************************************************************/
	$("img").pngfix();

	$("a#zoom").fancybox();

	$("a.external").click(function() { //Open link in new window
		window.open(this.href);
		return false;
	});

	$("a.email").each(function() { //Email address obfuscation
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
		$(this).text(e);
	});

	$(".rollover").hover( //Image rollovers
		function() {
			if($(this).attr("src").indexOf("-over.") == -1) {
				var newSrc = $(this).attr("src").replace(".gif","-over.gif");
				var newSrc = newSrc.replace(".jpg","-over.jpg");
				var newSrc = newSrc.replace(".png","-over.png");
				$(this).attr("src",newSrc);
			}
		},
		function() {
			if($(this).attr("src").indexOf("-over.") != -1) {
				var oldSrc = $(this).attr("src").replace("-over.gif",".gif");
				var oldSrc = oldSrc.replace("-over.jpg",".jpg");
				var oldSrc = oldSrc.replace("-over.png",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);

	$("#header_basket").hover(function() { //Basket link rollover
		$(this).addClass("over");
	}, function() {
		$(this).removeClass();
	});

	$("#display").change(function() { //Product listings set display
		if($.query.get("pageno") || $.query.get("category")) {
			window.location=window.location.href+"&display="+$(this).val();
		} else {
			window.location=window.location.href+"?pageno=1&display="+$(this).val();
		}
	});
	
	$("#delivery_same").change(function() { //Set delivery detail same as billing detail values
		if ($(this).attr("checked")) {
			$("#delivery_title").attr("selectedIndex",$("#billing_title").attr("selectedIndex"));
			$("#delivery_fname").attr("value",$("#billing_fname").val());
			$("#delivery_sname").attr("value",$("#billing_sname").val());
			$("#delivery_address_1").attr("value",$("#billing_address_1").val());
			$("#delivery_address_2").attr("value",$("#billing_address_2").val());
			$("#delivery_town").attr("value",$("#billing_town").val());
			$("#delivery_county").attr("selectedIndex",$("#billing_county").attr("selectedIndex"));
			$("#delivery_postcode").attr("value",$("#billing_postcode").val());
		}
	});

	$("#step_3_table td:eq(0)").css("width",53); //Step 3 basket table widths
	$("#step_3_table td:eq(1)").css("width",449);
	$("#step_3_table td:eq(2)").css("width",75);
	$("#step_3_table td:eq(3)").css("width",50);
	$("#step_3_table td:eq(4)").css("width",75);
	
	$("input.input-text").each ( //Define default text for form fields
		function() {
			this.rel=this.value;
		}
	);

	$("input.input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$("input.input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});
	
	/***********************************************************************
	Form validation
	***********************************************************************/
	$("#add_form").validate({
		rules: {
			quantity: {
				required: true,
				minlength: 1,
				maxlength: 3,
				digits: true
			}
		},
		
		messages: {
			quantity: "Please enter a valid quantity"
		},

		errorPlacement: function(error, element) {
			$("#error").attr("innerHTML","");
			error.appendTo("#error");
		},

		submitHandler: function(form) {
			form.submit();
		},

		onfocusout: false,
		onkeyup: false
	});
	
	$("#details_form").validate({
		debug: true,

		rules: {
			billing_fname: {
				required: true,
				minlength: 1,
				maxlength: 30
			},
			billing_sname: {
				required: true,
				minlength: 1,
				maxlength: 30
			},
			billing_fname: {
				required: true,
				minlength: 1,
				maxlength: 30
			},
			billing_address_1: {
				required: true,
				minlength: 1,
				maxlength: 100
			},
			billing_town: {
				required: true,
				minlength: 1,
				maxlength: 40
			},
			billing_postcode: {
				required: function() {
					return $("#billing_county").val()=="Co. Antrim" || $("#billing_county").val()=="Co. Armagh" || $("#billing_county").val()=="Co. Down" || $("#billing_county").val()=="Co. Londonderry" || $("#billing_county").val()=="Co. Fermanagh" || $("#billing_county").val()=="Co. Tyrone";
				},
				minlength: 1,
				maxlength: 10
			},
			billing_email: {
				required: true,
				minlength: 1,
				maxlength: 60,
				email: true
			},
			billing_email_confirm: {
				required: true,
				minlength: 1,
				maxlength: 60,
				email: true,
				equalTo: "#billing_email"
			},
			delivery_fname: {
				required: true,
				minlength: 1,
				maxlength: 30
			},
			delivery_sname: {
				required: true,
				minlength: 1,
				maxlength: 30
			},
			delivery_fname: {
				required: true,
				minlength: 1,
				maxlength: 30
			},
			delivery_address_1: {
				required: true,
				minlength: 1,
				maxlength: 100
			},
			delivery_town: {
				required: true,
				minlength: 1,
				maxlength: 40
			},
			delivery_postcode: {
				required: function() {
					return $("#delivery_county").val()=="Co. Antrim" || $("#delivery_county").val()=="Co. Armagh" || $("#delivery_county").val()=="Co. Down" || $("#delivery_county").val()=="Co. Londonderry" || $("#delivery_county").val()=="Co. Fermanagh" || $("#delivery_county").val()=="Co. Tyrone";
				},
				minlength: 1,
				maxlength: 10
			},
			cc_name: {
				required: true,
				minlength: 1,
				maxlength: 60
			},
			cc_number: {
				required: true,
				minlength: 1,
				maxlength: 22,
				creditcard: true
			}
		},
		
		errorPlacement: function(error, element) {
			error.appendTo(element.prev("div.error") );
		},

		submitHandler: function(form) {
			form.submit();
		},

		onfocusout: false,
		onkeyup: false
	});
	
	$("#confirmation_form").validate({
		rules: {
			terms: {
				required: true
			}
		},
		
		messages: {
			terms: "You must agree to the Terms &amp; Conditions"
		},

		submitHandler: function(form) {
			$("#submit_button").css("visibility","hidden");
			form.submit();
		},
		
		errorPlacement: function(error, element) {
			error.appendTo("div.error");
		},

		onfocusout: false,
		onkeyup: false
	});

});

function popup(doc,w,h) {
	var top = (screen.availHeight/2)-(h/2);
	var left = (screen.availWidth/2)-(w/2);
	window.open(doc,"jukebook_popup","location=0,scrollbars=auto,menubar=no,width="+w+",height="+h+",top="+top+",left="+left+",toolbar=no");
	return false;
}

function clearSearchField() {
	var searchField=document.getElementById("s");
	if (searchField.value=="Search keyword or phrase...") {
		searchField.value="";
	}
}

function populateSearchField() {
	var searchField=document.getElementById("s");
	if (searchField.value=="") {
		searchField.value="Search keyword or phrase...";
	}
}

function validateRequest(form) {
	var error=false;
	var response="There was a problem with the form\n\n";

	var name=form.name.value;
	var email=form.email.value;
	var request=form.request.value;
	
	if (!isString(name)) {
		error=true;
		response+="Please enter your name\n";
	}

	if (!isEmail(email)) {
		error=true;
		response+="Please enter a valid email address\n";
	}

	if (!isString(request)) {
		error=true;
		response+="Please enter a request/comment\n";
	}
	
	if (error) {
		alert(response);
		return false;
	} else {
		return true;
	}
}

function validateComment(form) {
	var error=false;
	var response="There was a problem with the form\n\n";

	if (document.getElementById("author")) {
		var author=form.author.value;
		var email=form.email.value;
	
		if (!isString(author)) {
			error=true;
			response+="Please enter your name\n";
		}

		if (!isEmail(email)) {
			error=true;
			response+="Please enter a valid email address\n";
		}
	}
	
	var comment=form.comment.value;
	
	if (!isString(comment)) {
		error=true;
		response+="Please enter a comment\n";
	}
	
	if (error) {
		alert(response);
		return false;
	} else {
		return true;
	}
}

/* Data type validation */
function isString(str) { //String
	if (str.length!="") {
		return true;
	} else {
		return false;
	}
}

function isEmail(str) { //Email address
	var emailRegExp="^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex=new RegExp(emailRegExp);
	return regex.test(str);
}