function d(x) {
	return document.getElementById(x);
}
function findPos(el){
	x=0; y=0; var temp
	if(el.offsetParent) {
		temp = el
		while(temp.offsetParent) { //Looping parent elements to get the offset of them as well
			temp=temp.offsetParent;
			x+=temp.offsetLeft;
			y+=temp.offsetTop;
		}
	}
    x+=el.offsetLeft;
    y+=el.offsetTop;
	return [x,y]
}
function showPop(target, x, cp) {
	var targ; var tt;
	if (x == 0) {
		targ = d('popL');
		tt = d('txtL');
		pos = findPos(target);
		targ.style.left = pos[0] + "px";
		targ.style.top = (pos[1] - 171) + "px";
	}
	else {
		targ = d('popR');
		tt = d('txtR');
		pos = findPos(target);
		targ.style.left = (pos[0] - 170) + "px";
		targ.style.top = (pos[1] - 171) + "px";
	}
	switch (cp) {
		case 1: tt.innerHTML = "<strong>Cream Cracker 400g</strong><br /><br />\"Haven\'t we met somewhere before?\"<br /><br />Everyone knows these South African cream crackers. Snacks just don't get any easier - simply add your favourite sweet or savoury topping and off you go. And this bulk value pack gives you more of your favourite Pyotts Cream Crackers in 2 stay-fresh packs.";
				break;
		case 2: tt.innerHTML = "<strong>Wheatsworth</strong><br /><br />\"What\'s a classy girl like you doing in a place like this?\"<br /><br />With its distinctive wheat and honey taste, Wheatsworth is the perfect posh snack to serve to guests with cheese and preserves. Or simply top with a slice of cheese and tomato for a classy snack when you haven\'t got time for lunch.";
				break;
		case 3: tt.innerHTML = "<strong>Kips</strong><br /><br />\"Want to join me in a salsa?\"<br /><br />This flavoursome savoury snack is perfect on its own, but it also loves to take a dip. Open a dip, scoop it with a Kips and voila! you\'ve got the perfect party nibble. (Just between us, the Kips Spring Onion variant is a real corker, if you know what we mean.)";
				break;
		case 4: tt.innerHTML = "<strong>Salticrax</strong><br /><br />\"Do you come here often?\"<br /><br />This savoury snack is everybody\'s sweetheart. Salticrax is welcome at any occasion - from a formal dinner party to a picnic. Salticrax gets on equally well with exotic toppings like smoked salmon and cream cheese, or simple toppings like cheese and tomato. But it\'s equally delicious au naturel.";
				break;
		case 5: tt.innerHTML = "<strong>VitaSnack</strong><br /><br />\"You look really fit. Do you work out?\"<br /><br />A delicious savoury bite-sized triangular snack. You can eat VitaSnack by the handful (in fact, it\'s difficult not to). Or you can pair VitaSnack with a dip. VitaSnack is available in 3 delicious flavours: Basil Pesto and Sesame Seeds, Cream Cheese and Sweet Chilli and Sweet Pepper and Poppy Seeds.";
				break;
		case 6: tt.innerHTML = "<strong>Snacktime</strong><br /><br />\"Are you a party animal?\"<br /><br />Snacktime is the ultimate \"party in a box\". It\'s all your favourite South African savoury crackers - including Salticrax and Wheatsworth - in one box. Don\'t throw a party without inviting the Snacktime.";
				break;
	}
	targ.style.visibility = 'visible';
}
function hidePop(x) {
	if (x == 0) d('popL').style.visibility = 'hidden';
	else d('popR').style.visibility = 'hidden';
}
function validateRegistration() {
	if (d('fName').value == "" || d('lName').value == "") {
		alert("Please give us both your first and last names to register.");
		return;
	}
	if (d('ruName').value == "" || d('cruName').value == "") {
		alert("Please give us your email address and confirm it.");
		return;
	}
	if (d('ruName').value != d('cruName').value) {
		alert("Sorry, but your Email address does not match the one supplied as a confirmation.");
		return;
	}
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!d('ruName').value.match(filter)) {
		alert("Sorry, but your email address does not appear to be valid.");
		return;
	}
	if (d('rpWord').value == "" || d('crpWord').value == "") {
		alert("Please select a password and confirm it.");
		return;
	}
	if (d('rpWord').value != d('crpWord').value) {
		alert("Sorry, but your selected password does not match the one supplied as a confirmation.");
		return;
	}
	d('registerForm').submit();
}
function validateLogin() {
	if (d('uName').value == "" || d('pWord').value == "") {
		alert("Please supply both your email address and password to log in.");
		return;
	}
	d('loginForm').submit();
}
function validateEvent() {
	if (d('title').value == "") {
		alert("Please supply a title for your event.");
		return;
	}
	if (d('venue').value == "") {
		alert("Please supply the name of the venue where the event will be held.");
		return;
	}
	if (d('start').value == "") {
		alert("Please select the start date of your event.");
		return;
	}
	if (d('rsvp').value == "") {
		alert("Please select a latest RSVP date for this event.");
		return;
	}
	if (d('details').value == "") {
		alert("We rely on the event details that you supply to create a meaningful invitation mailer. Please supply some additional information.");
		return;
	}
	d('sceneForm').submit();
}
function validateRecipientAdd() {
	if (d('name').value == "" || d('email').value == "") {
		alert("Please supply both a name and email address for the recipient.");
		return;
	}
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!d('email').value.match(filter)) {
		alert("Sorry, but your recipient's email address does not appear to be valid.");
		return;
	}
	d('recipientForm').submit();
}
function cancel(x, y) {
	if (confirm("You are about to permanently remove the event '" + y + "' and send all invitees a cancellation notice.\n\nProceed?")) {
		document.location = "Cancel?pyottEiD=" + encodeURI(x);
	}
}
function doRSVP() {
	if (d('r1').checked == false && d('r2').checked == false) {
		alert("Please indicate whether you will accept or decline this invitation before submitting.");
		return;
	}
	d('rsvpForm').submit();
}
function lostPassword() {
	if (d('uName').value == "") {
		alert("Please give us your email address and we'll send you a new password.");
		return;
	}
	d('loginForm').action = "doRetrieve";
	d('loginForm').submit();
}
function fileUpload() {
	if (d('imageFile').value == "") {
		alert("Please supply an image or select one from our pre-defined themes.");
		return;
	}
	if (d('imageFile').value.substring(d('imageFile').value.length - 3, d('imageFile').value.length) != "jpg"
	&& d('imageFile').value.substring(d('imageFile').value.length - 3, d('imageFile').value.length) != "jpeg") {
		alert("Sorry, but this system only supports JPEG files. Please locate a file with a .jpg or .jpeg file extension.");
		return
	}
	d('uploadForm').submit();
}
function popOne() {
	d('popC').style.visibility = "visible";
}
function unpopOne() {
	d('popC').style.visibility = "hidden";
}
function sendMail() {
	var checks = document.getElementsByTagName("input");
	var gotone = false;
	var cbc = 0;
	for (var i = 0; i < checks.length; i++) {
		if(checks[i].type == "checkbox") {
			cbc++;
			if (checks[i].checked) gotone = true;
		}
	}
	if (gotone) {
		d('recipientForm').action="Send";
		d('recipientForm').submit();
	} else if (cbc == 0) {
		alert("You have not added an invitee to your guest list. Please do so and click ADD TO GUEST LIST first.");
		return;
	} else {
		alert("Please use the checkboxes to indicate which guests should receive the invitation before submitting.");
		return;
	}
}
function wipeOut(x,y) {
	if (confirm("You are about to remove " + y + " from your invites list. Proceed?")) {
		document.location="DeleteRecipient?id=" + x;
	}
}
function catchEnter(e, dec) {
	var key;
	var keychar;

	if (window.event) key = window.event.keyCode;
	else if (e) key = e.which;

	keychar = String.fromCharCode(key);
	if (key == 13) validateLogin();
	return true;
}
