$(document).ready(function(){

	$("a.email").each(function(){ //Email address obfuscation
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		$(this).text(e);
	});

	$("a.external,area.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-text").each ( //Define default text for each input element
		function() {
			this.rel=this.value;
		}
	);

	$(".input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$(".input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});
});


function getPivot(year) {
	if (year < 10) return (2000 + year);
	if (year < 1900) return (1900 + year);
	return year;
}

// Return number of days in a month
function daysInMonth(month, year) {
	return new Date(year ? year : 1970, month, 0).getDate();
}

function toInt(n) {
	return parseInt(n.replace(/^0/,''));
}
//Validate gateway page form
function validateGateway(form) {
	
	var error=false;
	var response="";

	var today=new Date();

	var min_age=18;
	var dob_day=toInt(document.getElementById("dob_day").value);
	var dob_month=toInt(document.getElementById("dob_month").value);
	var dob_year=getPivot(toInt(document.getElementById("dob_year").value));
	var dob_date=new Date((dob_year+min_age),dob_month-1,dob_day);
	
	//alert(dob_month + ":" + dob_year + ":" + document.getElementById("dob_month").value);
	//alert(dob_date.toGMTString());
	//alert(today.toGMTString() + ":"+ today.getTime() +":" + dob_date.getTime() + ":" + (today.getTime()-dob_date.getTime()));
	if (isNaN(dob_day)) {
		error=true;
		response+="Please enter a valid Day\n";
	}
	else {
		var maxDays = daysInMonth(dob_month, dob_year);
		
		if (dob_year > 0 && (dob_day < 1 || dob_day > maxDays)) {
			error=true;
			response+="Day must be between 1 and " + maxDays + "\n";
		}
	}
	
	if (isNaN(dob_month) || dob_month < 1 || dob_month > 12) {
		error=true;
		response+="Please enter a valid Month\n";
	}
	if (isNaN(dob_year)) {
		error=true;
		response+="Please enter a Year\n";
	}
	
	if (response == '' && (today.getTime()-dob_date.getTime())<0) {
		this.location.href = "http://www.drinkaware.ie/";
		return false;
	}
		
	if (error) {
		alert(response);
		return false;
	} else {
		return true;
	}
	
};

//Validate competition form
function validateCompetition(form) {
	for (var i=0; i < form.product.length; i++) {
		if (form.product[i].checked) {
			var pre = form.product[i].value;
		}
	}

	var artist=document.getElementById("artist").value;
	var song=document.getElementById("song").value;
	var name=document.getElementById("name").value;
	var email=document.getElementById("email").value;
	var terms=document.getElementById("terms").checked;

	var error=false;
	var response="";
	
	if (!pre) {
		error=true;
		response+="Please answer the pre-qualifier question\n";
	}

	if (isString(artist)==false || artist=="Artist Name") {
		error=true;
		response+="Please enter an artist name\n";
	}

	if (isString(song)==false || song=="Song Title") {
		error=true;
		response+="Please enter an song title\n";
	}

	if (isString(name)==false || name=="Your Name") {
		error=true;
		response+="Please enter your name\n";
	}

	if (!isEmail(email)) {
		error=true;
		response+="Please enter your email address\n";
	}
	
	if (terms==false) {
		error=true;
		response+="You must agree to our Terms & Conditions";
	}

	if (error) {
		alert(response);
		return false;
	} else {
		return true;
	}
};

//Date 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);
}