function popup($url){
	var $day	= new Date();
	var $id	= $day.getTime();

	eval("page" + $id + " = window.open('" + $url + "' , '" + $id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=600,height=400,left = 400,top = 325');");
}

/**
* get HTML element by ID
*/
function g(id)
{
	if(document.getElementById(id))
	{
	   return document.getElementById(id);
	}
	else
	{
	   return false;
	}
}
function html_entity_decode(str) 
{
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

function h($id , $effect)
{
	try
	{
		switch ($effect)
		{
			case 'BlindUp':

				Effect.BlindUp($id);

				break;

			case 'Fade':

				Effect.Fade($id);

				break;

			default:
				if($($id) !== null)
				{
					$($id).style.display = 'none';
				}
				
		}
	}
	catch($e)
	{		
		$($id).style.display = 'none';
	}
}


function s($id , $effect)
{
	try
	{
		switch ($effect)
		{
			case 'Appear':

				Effect.Appear($id);

				break;

			case 'BlindDown':

				Effect.BlindDown($id);

				break;

			default:

				$($id).style.display = 'block';
		}
	}
	catch($e)
	{
		try
		{
			$($id).style.display = 'block';
		}
		catch(e){
			//console.log(e)
		};
	}
}


function tog($id , $hide_effect , $show_effect)
{
	var $element = $($id);

	if (!$hide_effect)
	{
		var $hide_effect = '';
	}

	if (!$show_effect)
	{
		var $show_effect = '';
	}

	if ($element.style.display == 'none')
	{
		s($id , $show_effect);
	}
	else
	{
		h($id , $hide_effect);
	}
}


/**
 * @desc: Checks if javascript is enabled
 */
function check_js(element)
{
	if ($(element))
	{
		h(element + '_nojs');
		s(element);
	}
}


/**
 * goes to page
 */
function goTo(page)
{
	location.href=page;
}

/**
 * checks if value is in array
 */
function in_array(needle, haystack)
{
	for(var i=0;i<haystack.length;i++)
	{
	   if(haystack[i] == needle)
	   {
	      return true;
	   }
	}

	return false;
}

/**
 * validates URL
 */
function isUrl(s)
{
	var v = new RegExp();

	v.compile("^[^:/?#]+://[^/?#]+\\.[a-z]{2,}[^?#]*(\\?[^#]*)?(#.*)?"    , "i");
	//          [scheme:][  //authority.tld  ][path][ ?query ] [#fragment]
	
	return v.test(s);
}

/**
 * filters out bad chars
 */
function safeName(name)
{
   name = name.replace(/ /g , '_');

   noalpha = '�����������������������������������������������������@���';
   alpha   = 'AEIOUYaeiouyAEIOUaeiouAEIOUaeiouAEIOUaeiouyAaOoAaNnCcaooa';

   for(i=0; i<alpha.length; i++)
   {
   	name = name.replace(noalpha[i], alpha[i]);
   }

   return name;
}

/**
 * creates popup page
 */
function popup(page, width, height)
{
	popupframe=window.open(page, 'popupframe', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + width + ',height=' + height + ',left=100,top=60');
	popupframe.focus();
}

/**
 * select award and close popup
 */
function selectaward(date)
{
	var txtarea = window.opener.document.forms[0]["award_date"];

	txtarea.value = date;

	txtarea.focus();

	self.close();
}

/**
 * adds a contribution to favorites
 */
function addContributionToFavorites(objectId)
{
   if(confirm(lang.addContributionToFavorites))
   {
      alert('nog te doen');
   }
}

/**
 * change height of HTML element
 */
function changeSize(id, offset, minSize)
{
   // get element
   el = id;

   // if element is an id and not a reference: get reference
   if ($(id))
   {
      el = $(id);
   }

   // set minimum size
   if (minSize === undefined)
   {
      minSize = 77;
   }

   // get height
   height = parseInt(el.style.height);

   if(!height && el.offsetHeight)
	{
		height = el.offsetHeight;
	}

	// calculate new height
	height += offset;

	if(height <= minSize)
	{
	   height = minSize;
	}

	// set new height
   el.style.height = height+"px";
}

/**
 * change TinyMCE textarea
 */
function changeTinyMCESize(textareaId, offset)
{
   var el = $(textareaId);
   var parent = el.parentNode;
   var set = false;
   var nEditor = 0;

   // expand table
   for(var i in parent.childNodes)
   {
      // if this is first element of tiny_mce
      if (set)
      {
         // table from tiny_mce
         table = parent.childNodes[i].firstChild;

         // get id for iframe
         nEditor = parent.childNodes[i].id;
         nEditor = nEditor.substr(11, 1);
         nEditor = 'mce_editor_' + nEditor;

         // set sizes
         changeSize(table, offset);
         changeSize(nEditor, offset);

         // sizes are set
         set = false;
      }
      // this is the textarea we made in a .tpl
      if (parent.childNodes[i].id == el.id)
      {
         // set = true (so next item is a span of tiny_mce)
         set = true;
      }
   }
}
function getCheckedValue(radioObj) 
{	
	if(!radioObj){
		return "";
	}
	var radioLength = radioObj.length;
	if(radioLength === undefined){
		if(radioObj.checked){
			return radioObj.value;
		}else{
			return "";
		}
	}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function lang_text(key)
{
	if(typeof lang_strings == 'undefined' || typeof lang_strings[key] == 'undefined')
	{
		return '[='+key+'=]';
	}
	else if(lang_strings[key] == '')
	{
		return '[~'+key+'~]';
	}
	else
	{
		return lang_strings[key];
	}
}

function DialogOverlay(content, contentHeader, container) {
	 
	// Manage arguments and assign defaults,
	if (typeof container == 'undefined' ) {
		container = document.body;
	}
	if (null == (this.container = $(container))) {
		throw("container is not valid");
	}
 
	// Assign instance variables
	this.content = content;
	this.overlay = new Element('div', { 'class': 'overlay' }).hide();
	this.header = new Element('div', {'class': 'dialogHeader'}).hide();
	this.dialog = new Element('div', { 'class': 'dialog' }).hide();
 
	// Hide the overlay when clicked. Ignore clicks on the dialog.
	Event.observe(this.overlay, 'click', this.hide.bindAsEventListener(this));
	Event.observe(this.dialog, 'click',  function(event) { Event.stop(event); });
 
	// Insert the elements into the DOM
	this.dialog.insert(this.content);
	this.header.insert(contentHeader);
	this.container.insert(this.overlay);
	this.container.insert(this.header);
	this.container.insert(this.dialog);
 
	// Content may have been hidden if it is embedded in the page
	content.show();
	this.dialog.hide();
}
 
DialogOverlay.prototype.show = function(url) {
	if(url !== undefined){
		var iframe = this.dialog.firstChild.lastChild;
		iframe.setAttribute('src',url);
	}
	Effect.Appear(this.overlay, { duration: 0.5,  to: 0.8 });
	this.dialog.show();
	this.dialog.firstChild.show();
	this.header.show();
	this.header.firstChild.show();
	return this;
};
DialogOverlay.prototype.hide = function(event) {
	this.dialog.hide();
	this.overlay.hide();
	this.header.hide();
	return this;
};

function compare_dates(date_start,date_end){
	calendar_from = date_start;
	calendar_until = date_end;
	calendar_from_format = calendar_from.substr(6,4) + "." +  calendar_from.substr(3,2) + "." +  calendar_from.substr(0,2);
	calendar_until_format = calendar_until.substr(6,4) + "." + calendar_until.substr(3,2) + "." +  calendar_until.substr(0,2);
	calendar_from = Date.parse(calendar_from_format.replace(/\./g, "/"));
	calendar_until = Date.parse(calendar_until_format.replace(/\./g, "/"));
	if (calendar_from < calendar_until){
		return true;
	}else{
		return false;	
	}	
}

/**
 * validate email
 */
function emailcheck(str)
{
	return /^[^@]+@[^@.]+\.[^@]*\w\w$/.test(str);
}

/* custom slectbox */ 
if (window.attachEvent){
	window.attachEvent('onload',	function(){
		var sites_items = [
			{value : "9"	, label : 'NL'	, selected : iso_lang.toUpperCase() == 'NL' ? true : false},
			{value : "4"	, label : 'EN'	, selected : iso_lang.toUpperCase() == 'EN' ? true : false},
		]; 
		selectBoxes(sites_items);
	}, false);
}
if(window.addEventListener){
	window.addEventListener('load',	function(){
		var sites_items = [
			{value : "9"	, label : 'NL'	, selected : iso_lang.toUpperCase() == 'NL' ? true : false},
			{value : "4"	, label : 'EN'	, selected : iso_lang.toUpperCase() == 'EN' ? true : false},
		]; 
		selectBoxes(sites_items);
	}, false);
}
function selectBoxes(items){
	
	$$('#langswitch .items').each(function(itemsBox){
		items.each(function(item){
			if(item != undefined){
				var div = Builder.node('div', {value : item.value});
				if(item.selected !== undefined && item.selected === true){
					div.setAttribute("selected", "selected");
				}
					div.appendChild(Builder._text(item.label));
				itemsBox.appendChild(div);
			}
		});
	});
		
	
	$$('#langswitch').each(function(box){
		if(box != undefined){
			var width = box.offsetLeft-37;
			$$('#'+box.id+' .label').each(function(label){
				width = label.getWidth();
			});
			$$('#'+box.id+' .value').each(function(value){
				Event.observe(value,'click',function(e){
					$$('#'+box.id+' .items').each(function(items){
						var width = value.getWidth()-5;
						items.setStyle({minWidth : width+"px"});
						if(items.getStyle('display') != 'none'){
							items.setStyle({display : 'none'});
						}else{
							items.setStyle({display : 'block'});
						}
					});
				});
			});
			$$('#'+box.id+' .items').each(function(items){
				items.setStyle({left : (width)+"px"});
			});
			$$('#'+box.id+' .items div').each(function(item){
				var value;
				$$('#'+box.id+' .value').each(function(v){
					value = v;
				});
				Event.observe(item,'click',selectBoxSelectItem.bindAsEventListener(this,value));
				if(item.getAttribute('selected') == "selected"){
					value.innerHTML = item.innerHTML;
					value.setStyle({display : 'inline'});
					item.parentNode.setStyle({display : 'none'});
				}
			});
		}
	});
}

function selectBoxSelectItem(e,value){
	var el = Event.findElement(e);
	var val = el.getAttribute("value");
	var path = location.pathname+'?set_language='+val;
	location.href = path;
}