//Document enhancement
function enhancePage() {
	if (!document.getElementsByTagName) return;

	var anchors=document.getElementsByTagName("a"); //Open external links in a new window
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
	
	$('a.email').each(function(){ //Email address obfuscation (required JQuery)
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
		$(this).text(this.title);
	});
}

window.onload=enhancePage;

//Show/hide content
function initialiseContent() {
	var qs = new Querystring();

	if (qs.get("content")) {
		var activeContent=qs.get("content");
	} else {
		var activeContent="";
	}

	if (!qs.get("expand")) {
		var plinths=document.getElementsByClassName("content-plinth");
		for (i=0; i<plinths.length; i++) {
			if (plinths[i].id!=activeContent) {
				plinths[i].style.display="none";
			}
		}
	}
}

function toggleContent(plinth,link) { //Show/hide partner and toggle link activation
	$("#"+plinth).slideToggle("slow");
	if (link.className=="active") {
		link.className="";
	} else {
		link.className="active";
	}
	
	return false;
}

//Home page banner text toggle
function toggleBannerText(direction) {
	var text=document.getElementById("caption");

	if (direction=="show") {
		text.style.visibility="visible";
	} else {
		text.style.visibility="hidden";
	}
}

//Validate donation form
function validateDonation(form) {
	amount=form.amount.value;
	name=form.name.value;
	address=form.address.value;
	email=form.email.value;
	telephone=form.telephone.value;
	
	var error=false;
	var response="There was an error filling out the form:\n\n";
	
	if (!isFloat(amount)) {
		error=true;
		response+="Please enter a valid donation amount\n";
	}
	
	if (!isString(name)) {
		error=true;
		response+="Please enter your name\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 (!isString(telephone)) {
		error=true;
		response+="Please enter your telephone number\n";
	}
	
	if (error) {
		alert(response);
		return false;
	} else {
		return true;
	}
}

//Validate purchase gift wrap form
function validateGiftwrap(form) {
	name=form.name.value;
	address=form.address.value;
	email=form.email.value;
	telephone=form.telephone.value;
	
	var error=false;
	var response="There was an error filling out the form:\n\n";

	if (!isString(name)) {
		error=true;
		response+="Please enter your name\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 (!isString(telephone)) {
		error=true;
		response+="Please enter your telephone number\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 isFloat(str) { //Integer
	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);
}

//Popup window
function popup(doc,w,h) {
	var top = (screen.availHeight/2)-(h/2);
	var left = (screen.availWidth/2)-(w/2);
	window.open(doc,"jukebook_popup","menubar=no,width="+w+",height="+h+",top="+top+",left="+left+",toolbar=no,location=0,scrollbars=yes");
	return false;
}

document.getElementsByClassName = function(className, parentElement) {
	if (typeof parentElement == 'string'){
		parentElement = document.getElementById(parentElement);
	} else if (typeof parentElement != 'object' || typeof parentElement.tagName != 'string') {
		parentElement = document.body;
	}
	var children = parentElement.getElementsByTagName('*');
	var re = new RegExp('\\b' + className + '\\b');
	var el, elements = [];
	var i = 0;
	while ( (el = children[i++]) ){
		if ( el.className && re.test(el.className)){
			elements.push(el);
		}
	}
  	return elements;
}