
function kw_invitation_next_recipient(name,email,useract)
{
	if (name == "" || email == "") {
		return alert("Vul eerst een naam en e-mailadres van een vriendje of vriendinnetje in, voordat je nog iemand kan uitnodigen.");
	}
	
	if (!validate_email(email)) {
		return alert("Het e-mailadres is ongeldig.");
	}
	
	if ($("p.kw_invitation_list").length > 0) {
		$("p.kw_invitation_list").replaceWith("<ol id=\"kw_invitation_list\"></ol>");
	}
			
	kw_list_container = $("ol#kw_invitation_list");
	
	li = "<li>\n";
	li+= "\t<input type=\"hidden\" name=\"names[]\" value=\""+name+"\"/>\n";
	li+= "\t<input type=\"hidden\" name=\"emails[]\" value=\""+email+"\"/>\n";
	li+= "\t"+name + ", " + email+" (<a href=\"#\" onclick=\"return kw_invitation_del_recipient(this)\">verwijder</a>)\n";
	li+= "</li>\n";

	$(kw_list_container).append(li);
	
	if (useract) {
		$("#in_name_id, #in_email_id").val("");
		$("#in_name_id").blur().focus();
	}
}

function kw_invitation_del_recipient(a_el)
{
	$(a_el).parents("li").eq(0).remove();
	
	return false;
}

function flickr_replace_picture(img_id,img_index)
{
	var picture = document.getElementById(img_id);

	if (picture) {
		picture.src = flickr_photos[img_index];
	}
	
	next_btn = document.getElementById('flickr_detail_next');
	if (!flickr_photos[img_index+1]) {
		next_btn.style.visibility = 'hidden';
	}
	else {
		next_btn.style.visibility = 'visible';
	}
	
	previous_btn = document.getElementById('flickr_detail_previous');
	if (!flickr_photos[img_index-1]) {
		previous_btn.style.visibility = 'hidden';
	}
	else {
		previous_btn.style.visibility = 'visible';
	}
	
	flickr_current = img_index;
	
}

function flickr_next_picture(img_id) {
	if (!flickr_current) {
		flickr_current = 0;
	}

	flickr_next = 0;
	if (flickr_photos[flickr_current+1]) {
		flickr_next = flickr_current+1;
		flickr_replace_picture(img_id,flickr_next);
	}
	
}

function flickr_previous_picture(img_id) {
	if (!flickr_current) {
		flickr_current = 0;
	}

	flickr_next = 0;
	if (flickr_photos[flickr_current-1]) {
		flickr_next = flickr_current-1;
		flickr_replace_picture(img_id,flickr_next);
	}
	
}

function help_open(helpid)
{
	el = document.getElementById(helpid);
	el.style.display = "block";
}

function help_close(helpid)
{
	el = document.getElementById(helpid);
	el.style.display = "none";
}

function help_move(event,helpid)
{
	if (event.pageX) {
		// !msie
		xPos = event.pageX;
		yPos = event.pageY;
	}
	else {
		// msie
		xPos = window.event.x;
		yPos = window.event.y;

		xPos += document.body.scrollLeft;
		yPos += document.body.scrollTop;

		if ((document.documentElement) && (document.documentElement.scrollLeft)) {
			xPos += document.documentElement.scrollLeft;
		}
		if ((document.documentElement) && (document.documentElement.scrollTop)) {
			yPos += document.documentElement.scrollTop;
		}
	}

	el = document.getElementById(helpid);
	el.style.left = xPos + 20 + "px";
	el.style.top  = yPos + 10 + "px";
}

function init_language_tabs() {
	$("div.language_tabs span a").each(function(i) {
		$(this).click(function() {
			$(this).parents("form").eq(0).find("fieldset.language_set").each(function(y) {
				if (i == y) {
					$(this).addClass("active");
				}
				else {
					$(this).removeClass("active");
				}	
			}); 
			return false;
		});
	});
}

var rotate_iterator = 0;
var rotate_interval = 5000;

function start_rotate_calendar()
{
	rotate_iterator = 0;
	rotate_calendar();
}

function rotate_calendar()
{
	$("div#calendar div#calendar-content div.calendar-item").hide();
	$("div#calendar div#calendar-content div.calendar-item").eq(rotate_iterator).fadeIn();
	rotate_iterator++;
	
	if (rotate_iterator >= $("div#calendar div#calendar-content div.calendar-item").length) {
		rotate_iterator = 0;
	}
	
	setTimeout("rotate_calendar()", rotate_interval);
}

/**
 * Validate an emailaddress
 *
 * uses http://en.wikipedia.org/wiki/E-mail_address as reference
 */
function validate_email(email)
{
	var parts = email.split(/@/);

	if (parts.length != 2) {
		// not composed of local part and domain part
		return false;
	}

	var local_part = parts[0];
	var domain_part = parts[1];


	// check the local part

	var reg = /^([A-Za-z0-9!#\$%\*\/\?\|\^\{\}`~&'+\-=_.]+)$/;

	if (reg.test(local_part)) {
		if (local_part.indexOf("..") >= 0) {
			// double dot in local part
			return false;
		}
		if (local_part.indexOf(".") == 0) {
			// dot at start of local part
			return false;
		}
		if (local_part.lastIndexOf(".") == local_part.length-1) {
			// dot at end of local part
			return false;
		}
	}
	else {
		reg = /^\"([^\"]+)\"$/;

		if (!reg.test(local_part)) {
			// failed escaped local part
			return false;
		}
	}


	// check the domain part

	reg = /^[[]([0-9:.]+)[]]$/;
	
	if ((domain_part == "") || (domain_part == "[]")) {
		// invalid domain part
		return false;
	}
	else if (reg.test(domain_part)) {
		// check the ip address

		// (still missing)
	}
	else {
		// check the labels

		var labels = domain_part.split(/[.]/);
		reg = /^([A-Za-z0-9-]+)$/;

		var i;
		for(i=0; i < labels.length; i++) {
			var label = labels[i];

			if (label == "") {
				// empty label
				return false;
			}
			if (!reg.test(label)) {
				// invalid characters
				return false;
			}
			if (label.indexOf("-") == 0) {
				// hyphen at start of label
				return false;
			}
			if (label.lastIndexOf("-") == label.length-1) {
				// hyphen at end of label
				return false;
			}
		}
	}

	return true;
}

function toggle_duclub_fields(checkbox_el) 
{
	if (checkbox_el.checked) {
		$("fieldset[class^=duclub]").attr("style", "display:block;");
	}
	else {
		$("fieldset[class^=duclub]").attr("style", "display:none;");
	}
}