$(function() {
	// Inputs
	$('input[type="text"], input[type="password"], textarea').focus(function() {
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"], input[type="password"], textarea').blur(function() {
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	// Accreditations
	var original = $("#accreditations li p").text();
	$("#accreditations .lloyds").hover(function() {
		$(this).siblings().children("p").text("Cactus is quality accredited to ISO 9001:2008");
	}, 
	function() {
		$(this).siblings().children("p").text(original);
	});
	// Product Hovers
	$("#products li").hover(function() {
		$("#products li").not(this).addClass("dim");
	}, 
	function() {
		$("#products li").not(this).removeClass("dim");
	});
	// Submit Hover
	$("#submit").hover(function() {
		$(this).addClass("underline");
	}, 
	function() {
		$(this).removeClass("underline");
	});
	// Toggle Signup
	$("#button").toggle(function() {
		$(this).blur();
		$("#newsletter").slideDown(600, function() {
			$(this).children().children("#fname").focus();
		});
		$("#email").val($("#address").val());
		return false;
	}, 
	function() {
		$(this).blur();
		$("#newsletter").slideUp(600, function() {
			$(this).children().children("#fname").blur();
			$("#address").focus();
		});
		$("#email").val("");
		return false;
	});
	// Contact Form Validation
	function validateField(field) {
		var error = false;
		if ($(field).attr("class").indexOf("required") != -1) {
			if (!$(field).val().length)
				error = true;
		}
		if ($(field).attr("default_value") != "" && $(field).attr("default_value") == $(field).val() ) {
                error = true;
        }
		if ($(field).attr("class").indexOf("numeric") != -1) {
			if (!/^[0-9]*$/.test($(field).val()))
				error = true;
		}
		if ($(field).attr("class").indexOf("robot") != -1) {
			if (!/^[4]*$/.test($(field).val()))
				error = true;
		}
		if ($(field).attr("class").indexOf("character") != -1) {
			if (!/^[a-zA-ZöÖäÄåÅ]*$/.test($(field).val()))
				error = true;
		}
		if ($(field).attr("class").indexOf("email") != -1) {
			if (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test($(field).val()))
				error = true;
			if ($(field).val() == $(this).val('default_value'))
				error = true;
		}
		if (error) {
			$(field).addClass("error");
		} else {
			$(field).removeClass("error");
		}
		return !error;
	}
	$("form").each( function() {
		$(this).submit(function () {
			var validationError = false;
			$("input, select, textarea", this).each( function() {
				if ($(this).attr("class")) {
					if (!validateField(this))
						validationError = true;
				}
			});
			return !validationError;
		});
		$("input, select, textarea", this).each( function() {
			if ($(this).attr("class")) {
				$(this).attr('default_value',$(this).val());
				$(this).blur( function() { validateField(this) } );
    			}
		});
	});
});
