$(document).ready(function(){

	$("a.email").each(function(){ //Email address obfuscation
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		$(this).text(e);
	});

	$("a.external").click(function(){ //Open link in new window
		window.open(this.href);
		return false;
	});

	$(".rollover").hover( //Image rollovers
		function(){
			if($(this).attr("src").indexOf("-over")==-1) {
				var newSrc = $(this).attr("src").replace(".gif","-over.gif");
				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");
				oldSrc = oldSrc.replace("-over.png",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
	
	$("input.input-text").each ( //Define default text for each input element
		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;
		}
	});
	
	$("a.deals").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'overlayOpacity': 0.8,
			'overlayColor'	: '#000',
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600,
			'speedOut'		: 600,
			'width'			: 800,
			'height'		: 800,
			'href'			: '/daily-deals.php',
			'type'			: 'ajax'
		});

		return false;
	});
	
	$("#calculator-form").submit( //Value calculator
		function() {
			var $weight=$("#weight",this).val();
			var $carat=$("#carat",this).val();
			
			$("#result-container").html("calculating...");
			
			$.post("/ajax/_calculate.inc.php",{weight: $weight, carat: $carat}, function($xml) {
				$("#result-container").html($("response",$xml).text());
				$("#result-container").css("color","#cc0000");
				$("#result-container").animate({ color: "#ffffff" }, 500);
			});

			return false;
		}
	);
	
	$("#request-form").submit(
		function() {
			return validatePack();
		}
	);

});

//Validate pack request form
function validatePack() {
	$fname=$("#request-form #fname").val();
	$sname=$("#request-form #sname").val();
	$address=$("#request-form #address").val();
	$email=$("#request-form #email").val();
	
	var error=false;
	var response="There was an error filling out the form:\n\n";

	if (!isString($fname)) {
		error=true;
		response+="Please enter your first name\n";
	}
	
	if (!isString($sname)) {
		error=true;
		response+="Please enter your surname\n";
	}
	
	if (!isString($address)) {
		error=true;
		response+="Please enter your address\n";
	}
	
	if (!isEmail($email)) {
		error=true;
		response+="Please enter a valid email address\n";
	}

	if (error) {
		alert(response);
		return false;
	} else {
		return true;
	}
};

//Data type validation
function isString(str) {
	if (str.length!="") {
		return true;
	} else {
		return false;
	}
};

function isInteger(str) {
	return (str.toString().search(/^-?[0-9]+$/) == 0);
};

function isFloat(str) {
	var temp_value = str;

	if (temp_value == "") {
		return false;
	}
	var Chars = "0123456789.";
	for (var i = 0; i < temp_value.length; i++) {
		if (Chars.indexOf(temp_value.charAt(i)) == -1) {
			return false;
		}
	}

	return true;
};

function isEmail(str) { //Email address
	var emailRegExp="^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex=new RegExp(emailRegExp);
	return regex.test(str);
};
