// xAjax Stuff
function submitForm(e, additional_cf) {

	if (e && e.tagName == 'FORM') {

		if(!ValidateForm(e)) return false;

		var formElements = e.elements;
		var target_cf = formElements['target_cf'] ? formElements['target_cf'].value : additional_cf;
		var wait_button = formElements['wait_button'].value;

		if(target_cf) {

			var chksum = location.hostname;
			formElements['checksum'].setAttribute("value",chksum);

			if (xajax.$(wait_button)) {
				xajax.$(wait_button).disabled=true;
				xajax.$(wait_button).value="Even geduld aub...";
			}
			xajax.call(target_cf, new Array(xajax.getFormValues(e)), 1);

		} else {
			alert('submitForm: No valid arguments');
		}
	} else {
		alert('submitForm: No valid FORM object');
	}
	return false;
}


function ValidateForm(e) {
	var msg = '';
	var chk = '';
	var focusfld = '';
	var i,ii,x;
	var rclist = [];
	var val = '';
	for(i=0;i<e.length;i++) {
		if (chk = e[i].getAttribute('inpchk')) {
			e[i].className = e[i].className.replace('mandatory','');
			if ((chk == 'notempty')) {
				if (e[i].type == 'checkbox') {
					val = (e[i].checked) ? '1' : '';
				} else {
					val = e[i].value;
				}
				if (val == '') {
					if (focusfld == '') focusfld = e[i].name;
					msg += e[i].getAttribute('chkmsg')+"\n";
					e[i].className += ' mandatory';
				}
			}
			if ((chk == 'minmaxlen')) {
				var min = e[i].getAttribute('minlen');
				var max = e[i].getAttribute('maxlen');
				if (e[i].value.length < min) {
					msg += e[i].getAttribute('chkmsg')+"\n";
					if (focusfld == '') focusfld = e[i].name;
					e[i].className += ' mandatory';
				}
				if (e[i].value.length > max) {
					msg += e[i].getAttribute('maxmsg')+"\n";
					if (focusfld == '') focusfld = e[i].name;
					e[i].className += ' mandatory';
				}
			}
			if ((chk == 'radiovalue')) {
				if (rclist[e[i].name] == 1) continue;
				rclist[e[i].name] = 1;
				x = e[e[i].name];
				if (!!x[0]) {
					for(ii=0;ii<x.length;ii++) {
						x[ii].className = x[ii].className.replace('mandatory','');
					}
					val = false;
					for(ii=0;ii<x.length;ii++) {
						if (x[ii].checked) val = true;
					}
					if (!val) {
						for(ii=0;ii<x.length;ii++) {
							x[ii].className += ' mandatory';
						}
						if (focusfld == '') focusfld = i;
						msg += e[i].getAttribute('chkmsg')+"\n";
					}
				}
			}
		}
	}
	if (msg) {
		alert(msg);
		e[focusfld].scrollIntoView(false);
		e[focusfld].focus();
		return false;
	}
	return true;
}

var base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");
var base64inv = {};
for (var i = 0; i < base64chars.length; i++)
{
  base64inv[base64chars[i]] = i;
}

function base64_decode (s)
{
  // remove/ignore any characters not in the base64 characters list
  //  or the pad character -- particularly newlines
  s = s.replace(new RegExp('[^'+base64chars.join("")+'=]', 'g'), "");

  // replace any incoming padding with a zero pad (the 'A' character is zero)
  var p = (s.charAt(s.length-1) == '=' ?
          (s.charAt(s.length-2) == '=' ? 'AA' : 'A') : "");
  var r = "";
  s = s.substr(0, s.length - p.length) + p;

  // increment over the length of this encrypted string, four characters at a time
  for (var c = 0; c < s.length; c += 4) {

    // each of these four characters represents a 6-bit index in the base64 characters list
    //  which, when concatenated, will give the 24-bit number for the original 3 characters
    var n = (base64inv[s.charAt(c)] << 18) + (base64inv[s.charAt(c+1)] << 12) +
            (base64inv[s.charAt(c+2)] << 6) + base64inv[s.charAt(c+3)];

    // split the 24-bit number into the original three 8-bit (ASCII) characters
    r += String.fromCharCode((n >>> 16) & 255, (n >>> 8) & 255, n & 255);
  }
   // remove any zero pad that was added to make this a multiple of 24 bits
  return r.substring(0, r.length - p.length);
}

/* FdB - Image Slider */

function ImageSlider(name, container_id) {

	//Private vars and functions
	this.this_name = name;
	this.container = container_id;
	this.images = new Array();
	this.divs = new Array();
	this.image_count = 0;
	this.div_count = 0;
	this.image_current = 0;
	this.div_current = 0;
	this.is_rotating = 0;
	this.backdiv = null;
	this.frontdiv = null;
}


ImageSlider.prototype.Current = function () {
	return this.image_current;
}
ImageSlider.prototype.CurrentDiv = function () {
	return this.div_current;
}

ImageSlider.prototype._ImageCount = function () {
	return this.image_count;
}

ImageSlider.prototype.Add = function (url, title, callback) {
	this.images[this.image_count] = new Array();
	this.images[this.image_count]['url'] = url;
	this.images[this.image_count]['title'] = title;
	this.images[this.image_count]['callback'] = callback;
	this.image_count++;
}

ImageSlider.prototype.AddDiv = function (id) {
	this.divs[this.div_count] = document.getElementById(id);
	this.div_count++;
}

ImageSlider.prototype.Render = function () {
	var e = document.getElementById(this.container);
	if(e) {
		this.Set(0);
	}
}
ImageSlider.prototype.RenderDiv = function () {
	var e = document.getElementById(this.container);
	if(e) {
		this.SetDiv(0);
	}
}

ImageSlider.prototype.Set = function (image_index) {
	if( (image_index < 0) || (image_index > this.image_count)) return false;
	if(this.is_rotating) return false;
	this.is_rotating = 1;
	var bckpid = this.image_current;
	this.image_current = image_index;

	var backimage = document.getElementById('slider_back_image');
	var frontimage = document.getElementById('slider_front_image');

	if (!frontimage.src) {
		backimage.src = this.images[image_index]['url'];
	} else {
		backimage.src = frontimage.src;
	}
	this._changeOpac(0, 'slider_front_image');
	frontimage.src = this.images[image_index]['url'];
	frontimage.title = this.images[image_index]['title'];
	this._set_opacity('slider_front_image',0,100,1000);
	setTimeout(this.this_name + ".is_rotating=0", 1000);
}

ImageSlider.prototype.SetDiv = function (div_index) {
	if( (div_index < 0) || (div_index > this.div_count)) return false;
	if(this.is_rotating) return false;
	this.is_rotating = 1;
	var bckpid = this.div_current;
	this.div_current = div_index;

	if (this.backdiv == null) {
		this.backdiv = document.getElementById('slider_back_div');
		this.frontdiv = document.getElementById('slider_front_div');
		this.backdiv.style.display = 'none';
		this.frontdiv.innerHTML = this.divs[div_index].innerHTML;
		this.is_rotating = 0;
	} else {
		if (this.backdiv.style.display == 'none') {
			this._changeOpac(0, this.backdiv.id);
			this.backdiv.innerHTML = this.divs[div_index].innerHTML;
			//this._changeOpac(50, this.backdiv.id);
			//this._changeOpac(0, this.backdiv.id);
			setTimeout("Slider._set_opacity('"+this.backdiv.id+"',0,100,1000)",100)
			setTimeout("Slider._set_opacity('"+this.frontdiv.id+"',100,0,1000)",100);
		} else if (this.frontdiv.style.display == 'none') {
			this._changeOpac(0, this.frontdiv.id);
			this.frontdiv.innerHTML = this.divs[div_index].innerHTML;
			setTimeout("Slider._set_opacity('"+this.frontdiv.id+"',0,100,1000)",100);
			setTimeout("Slider._set_opacity('"+this.backdiv.id+"',100,0,1000)",100);
		} else {
			alert("bd:" + this.backdiv.style.display + 'fd:' + this.frontdiv.style.display);
		}
		setTimeout(this.this_name + ".is_rotating=0", 1000);
	}
	//alert("Met deze pauze ziet het er goed uit");


}

ImageSlider.prototype.Prev = function () {
	var bckpid = this.image_current;
	this.image_current--;
	if(this.image_current < 0) {
		this.image_current = this.image_count-1;
	}
	this.Set(this.image_current);
}

ImageSlider.prototype.Next = function () {
	var bckpid = this.image_current;
	this.image_current++;
	if(this.image_current > this.image_count-1) {
		this.image_current = 0;
	}
	this.Set(this.image_current);
}

ImageSlider.prototype.PrevDiv = function () {
	var bckpid = this.div_current;
	this.div_current--;
	if(this.div_current < 0) {
		this.div_current = this.div_count-1;
	}
	this.SetDiv(this.div_current);
}

ImageSlider.prototype.NextDiv = function () {
	var bckpid = this.div_current;
	this.div_current++;
	if(this.div_current > this.div_count-1) {
		this.div_current = 0;
	}
	this.SetDiv(this.div_current);
}

ImageSlider.prototype.Click = function () {
	eval(this.images[this.image_current]['callback']);
	return true;
}

ImageSlider.prototype._set_opacity = function (id, opacStart, opacEnd, millisec) {

	//speed for each frame
	var speed = Math.round(millisec * 0.01);
	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(this.this_name + "._changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout(this.this_name + "._changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
ImageSlider.prototype._changeOpac = function (opacity, id) {
	var object = document.getElementById(id).style;
	if(opacity < 5) {
		object.display = "none";
	} else {
		object.display = "block";
	}
	object.opacity = (opacity * 0.01);
	object.MozOpacity = (opacity * 0.01);
	object.KhtmlOpacity = (opacity * 0.01);
	object.filter = "alpha(opacity=" + opacity + ")";
}