
 table_goto_page = function(table_id,go, callback)
{
	if (data.get('busy'))
		return false;

	var next_page = data.get(table_id+'_current')+go;

	if (next_page < 1)
		return false;

	if (next_page > data.get(table_id+'_total'))
		return false;

	data.set('busy',true);
	if ($(table_id+'_page_'+next_page))
		return table_goto_page_scroll(table_id,go);
	url = data.get(table_id+'_baseurl').replace('XPAGEX', next_page);
	new Ajax.Request(url,{
		parameters: { ajax: true, page: next_page },
		method: 'get',
		onSuccess: function(transport, json){
			if (go>0) // insert after
				$(table_id+'_content').innerHTML += transport.responseText;
			else // insert before
			{
				var jumpamount = parseInt($(table_id+'_container').style.height) * go; // move div up by 'jumpamount' ..
				var offset = (parseInt($(table_id + '_content').style.top) + jumpamount) + 'px';  // .. pixels ..

				$(table_id + '_content').style.top = offset; // .. by setting the style->top of the inner container div ..

				new Insertion.Top(table_id + '_content',transport.responseText); // and insert the content in the invisible (upper) space (in the inner container div)

			}
			callback();
			table_goto_page_scroll(table_id,go, callback);



		}
	});
	return false;
}
table_goto_page_scroll = function(table_id,go)
{
	var next_page = data.get(table_id+'_current')+go;
	var current_page = table_id+'_page_'+data.get(table_id+'_current');
	//new Effect.Opacity(current_page, { from: 1.0, to: 0.3, duration: 0.5 });
	//new Effect.Opacity(next_page, { from: 0.1, to: 1.0, duration: 1 });
	var scrollamount = parseInt($(table_id+'_container').style.height) * go * -1;

	data.set(table_id+'_current', data.get(table_id+'_current')+go);

	new Effect.Move(table_id+'_content', { x: 0, y: scrollamount, mode: 'relative', transition: Effect.Transitions.sinoidal, duration: 1.5,
		afterFinish: function()
		{
			setTimeout("table_remove_excess_pages('"+table_id+"',"+ go +");",690);
			setTimeout("data.set('busy',false);",700);
		}
	});
	return false;
}

table_remove_excess_pages = function (table_id, go)
{
	var current_page = data.get(table_id+'_current');
	var jumpamount = parseInt($(table_id+'_container').style.height) * go;
	var element_to_remove = table_id+'_page_'+(current_page - go*3);

	if ($(element_to_remove))
	{
		//alert("remove: "+element_to_remove);
		if (go > 0) // no need to jump if content is removed from bottom
		{

			var top = (parseInt($(table_id + '_content').style.top) + jumpamount) + 'px';
			$(table_id + '_content').style.top = top;
		}
		Element.remove(element_to_remove);
	}
}
/* ***************************************************************************** */

function ajaxlink (url,params)
{
	var level = params.level;
	var target = 'page_content_'+level;
	var effects = params.effects;

	if (effects)
	{

		new Effect.Fade(target,{ duration: 0.3,
		afterFinish: function() {
			new Ajax.Updater(target, url, { method: 'get', parameters: { xmlhttpreq: true, level: level }, evalScripts: true,
				onComplete: function()
				{
					new Effect.Appear(target,{ duration: 0.8 });

				}
			});
		}});
	}
	else
	{
		new Ajax.Updater(target, url, {
				method: 'get',
				parameters: { xmlhttpreq: true, level: level },
				evalScripts: true
				});

	}
	return false;
}
function ajaxsubmit (ref,params)
{
	var level = params.level;
	var target = 'page_content_'+level;
	var effects = params.effects;
	var currenturl = params.currenturl;

	if (effects)
	{

		new Effect.Fade(target,{ duration: 0.5,
		afterFinish: function() {
			new $(ref.form).request({ parameters: { xmlhttpreq: true, level: level }, evalScripts: true,
				onComplete: function()
				{

					new Ajax.Updater(target, currenturl, { method: 'get', parameters: { xmlhttpreq: true, level: level }, evalScripts: true,
						onComplete: function()
						{
							new Effect.Appear(target);

						}
					});

				}
			});
		}});
	}
	else
	{
		new $(ref.form).request({ parameters: { xmlhttpreq: true, level: level }, evalScripts: true,
		onComplete: function()
		{
			new Ajax.Updater(target, currenturl, { method: 'get', parameters: { xmlhttpreq: true, level: level }, evalScripts: true,

			});

		}
		});

	}
	return false;
}


var $search_timeout = 0;
function search_list ()
{

        if ($search_timeout)
                clearTimeout($search_timeout);

        $search_timeout = setTimeout(function(){
                url = search_baseurl.replace('XXXXX',$('search_field').value);
                ajaxlink(url,{level: 3, effects: false});
        },500);
}

var StarRating = Class.create({
  initialize: function(id,container,params) {

  	// settings

    this.rating = params['rating'] || 0;
    this.id = id;
    this.container = container;
	this.posturl = params['posturl'];

    this.imgdir = params['imgdir'] || '/images/rating/';
    this.imgname = params['imgname'] || 'star';
    this.imgext = params['imgext'] || '.png';
    this.disabled = params['disabled'] || false;
	
    this.showRateBox();

  },
  rate: function(rating) {
  	alert(this.rating + ' --> ' + rating);
    this.rating(rating);
  },
  showRateBox: function()
  {

	for (var i = 1; i <= 5; ++i)
	{
		var img = new Element('img', {'class': 'ratingstar',title: 'Klik om de nieuwe beoordelingsscore op te slaan.', src: this.getImage({nr:i,rating: this.rating}), alt: i, id: 'star_'+this.id+'_'+i });
		$(this.container).insert(img);

		if (!this.disabled)
		{
			Event.observe(img,'mouseover', this.starMouseOver.bindAsEventListener(this) );
			Event.observe(img,'click',this.starClick.bindAsEventListener(this) );
		}
	}

	if (!this.disabled)
	{

		$(this.container).style.cursor = 'pointer';

		Event.observe($(this.container),'mouseover',this.rateBoxMouseOver.bindAsEventListener(this) );
		Event.observe($(this.container),'mouseout',this.rateBoxMouseOut.bindAsEventListener(this) );

		if (!this.posturl)
		{
			var hiddeninput = new Element('input', { 'name' : this.id, id: 'rating_'+this.id });
			hiddeninput.type = 'hidden';
			$(this.container).insert(hiddeninput);
		}
	}


  },
  blabla: function()
  {
  	alert(this.posturl);

  },
  updateRateBox: function(rating,params)
  {

  	this.disabled = true;
  	var effect = false;
  	var delay = 50;

  	if (typeof(params) != 'undefined')
  	{
  		effect = params.effect || effect;
  		delay = params.delay || delay;

  	}
  	if (effect !== false)
  	{
		for (var i = 1; i <= 5; ++i)
		{
			var e = $('star_'+this.id+'_'+i);
			var d = delay * (5 - i) + 1;
  			setTimeout( '$(\''+e.id+'\').src = \''+ this.getInactiveStarImg() +'\';', d );
		};

  	}

	for (var i = 1; i <= 5; ++i)
	{
//		alert('star_'+this.id+'_'+i);
		var e = $('star_'+this.id+'_'+i);


  		if (effect !== false)
  		{
  			var d = delay * (6 + i);
  			setTimeout( '$(\''+e.id+'\').src = \''+ this.getImage({ nr: i, rating: rating }) +'\';', d );

  		}
  		else
  		{
//  			this.blabla();

  			e.src = this.getImage({nr: i, rating: rating});

  		}
	}
	if (effect === false)
		this.enable(this);
	else
	{
//		this.blabla.delay(2);
		this.enable.delay(1,this);
	}
  },
  enable: function(ref)
  {
  	ref.disabled = false;
  },
  starClick: function(e)
  {
  	this.saveRating(e.element().alt);
  },
  saveRating: function (rating)
  {
//  	alert('clicked : ' + this.rating + ' -> ' + rating);



	this.rating = rating;
	this.updateRateBox(rating, { effect: true });

	// save in hidden field
	if (!this.posturl)
		$('rating_'+this.id).value = rating;
	// send to server with ajax
	else
	{
  	new Ajax.Request(this.posturl,
	  {
	    method:'post',
	    parameters: { id: this.id, rating: rating },
	    onSuccess: function(transport,json){
//	 	  alert(json.rating);
//	      ref.rating = (json ? json.rating : ref.rating);
//
//	      ref.updateRateBox(ref,ref.rating);
//			new Effect.Highlight(ref.container);


	    },
	    onFailure: function(){ alert('Door een technisch probleem is de score niet opgeslagen. Onze excuses voor het ongemak.'); }
	  });
	}
  },
  starMouseOver: function(e)
  {
//  	alert('mouseover(star): ' + e.element().id);
	if (!this.disabled)
	  	this.updateRateBox(e.element().alt);
  	// highlight 1 tm 'id'
  },
  rateBoxMouseOver: function(e)
  {
  	// enable ratingbox

  },
  rateBoxMouseOut: function(e)
  {
  	// restore ratingbox
  	if (!this.disabled)
  		this.updateRateBox(this.rating);

  },
  getImage: function(params)
  {
  	var nr = params.nr;
  	var rating = params.rating || this.rating;

  	if (nr <= rating)
  		return this.getActiveStarImg();
  	else
  		return this.getInactiveStarImg();
  },
  getInactiveStarImg: function()
  {
	return this.imgdir+this.imgname+'0'+this.imgext;
  },
  getActiveStarImg: function()
  {
  	return this.imgdir+this.imgname+'1'+this.imgext;
  }

});






var Popup = Class.create();
Popup.prototype =
{
  initialize: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 400
    }
    Object.extend(this.options, options || {});
    window.open(this.options.url, '', 'width='+this.options.width+',height='+this.options.height);
  }
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function serialize (theForm)
{
	       var queryString = '';

        var foutmeldingen = [];
        for(i=0; i<theForm.elements.length; i++){
                var alertText = ""
                alertText += "Element Type: " + theForm.elements[i].type + "\n"



                    if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
                            alertText += "Element Value: " + theForm.elements[i].value + "\n";
                            //alert(theForm.elements[i].name+': '+theForm.elements[i].value +' => '+urlencode( theForm.elements[i].value ))
                            queryString += '&'+Url.encode( theForm.elements[i].name )+'='+Url.encode( theForm.elements[i].value );

                    }
                    else if(theForm.elements[i].type == "checkbox"){
                            alertText += "Element Checked? " + theForm.elements[i].checked + "\n";
                            queryString += '&'+Url.encode( theForm.elements[i].name)+'='+Url.encode( theForm.elements[i].value );

                    }
                    else if(theForm.elements[i].type == "radio"){
                            alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
                            if (theForm.elements[i].checked)
                            {
                                    queryString += '&'+Url.encode( theForm.elements[i].name )+'='+Url.encode( theForm.elements[i].value);
                            }
                    }
                    else if(theForm.elements[i].type == "select-one"){
                            alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
                            queryString += '&'+Url.encode( theForm.elements[i].name )+'='+ Url.encode( theForm.elements[i].value );
                    }

        }
        return queryString;
}



// global xmlHttp var
var xmlHttp;
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
	try
	{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return xmlHttp;
}


function hrefClick (url)
{
	// ajax request hier

	document.getElementById('main').style.border = '1px solid #c0c0c0';
//	document.getElementById('main').innerHTML = '<div class="loading">Loading ...</div>';
//	return false;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		return;
	}

	 url+="&xmlhttpreq=true";

	xmlHttp.onreadystatechange=hrefClickHtmlResponse;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function hrefClickHtmlResponse()
{
	if(xmlHttp.readyState==4)
	{
		document.getElementById('main').innerHTML=xmlHttp.responseText;
		document.getElementById('main').style.border = '';
	}
}


function hrefMouseOut (ref)
{
	window.status='';
	ref.style.cursor='';
//	ref.removeClass('block_hover');
}

function hrefMouseOver (ref,url)
{

	ref.style.cursor='pointer';
	window.status=url; return true;

}



function formGotoUrl (url,ref)
{
//	document.getElementById('testdiv').innerHTML=ref.name+'='+ref.value;
	if (useXmlHttpReqs == true)
	{
//		alert('ajax');
		hrefClick(url+'&'+urlEncode(ref.name)+'='+urlEncode(ref.value));

	}
	else
	{
//		alert('geen ajax');
		document.location=url+'&'+urlEncode(ref.name)+'='+urlEncode(ref.value);
	}




}

function urlEncode (string)
{
        string = encodeURI(string);
        string = string.replace('&','%26');
        return string;
}







function showBookingPeriodDetails (idxPeriod)
{
	document.getElementById('bookingPeriod_details').innerHTML=idxPeriod;

}





function showDivFade(id)
{
	// placeholder
	changeOpac(0, id);
	showDiv(id);
	opacity(id,0,100,500);
}
function hideDivFade(id)
{
	// placeholder
	opacity(id,100,0,500);
	setTimeout(function(){hideDiv(id);},501);
}



function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

















function showDiv(id)
{
//	document.getElementById('testdiv').innerHTML = id;
	document.getElementById(id).style.display = '';

}

function hideDiv(id)
{
	document.getElementById(id).style.display = 'none';
}

//function showHint(id)
//{
//	document.getElementById(id).style.display = '';
//}
//function hideHint(id)
//{
//	document.getElementById(id).style.display = 'none';
//}


function toggleDiv(id)
{
//	alert(id.name);
	if (document.getElementById(id).style.height != '30px')
		document.getElementById(id).style.height = '30px';
	else
		document.getElementById(id).style.height = '';
}
