﻿function setfocus(a_field_id) {
	var thefield = document.getElementById(a_field_id);
	if(thefield)
	{	thefield.focus();
	}
}
function checkmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {			
		return false;	
	}	
	else return true;
}
//(key==46) || (key==45) || er uit gehaald .
function numeric(keyP){
	var key;	
	if (document.all) {key = window.event.keyCode;}
	else {key = keyP.which;}	
	
	if ((key>47 && key<58) || (key < 32 || key==127) || (key==44) || (key==46)) {return true;}
	else {return false;}
}
function integer(keyP){
	var key;	
	if (document.all) {key = window.event.keyCode;}
	else {key = keyP.which;}	
	
	if ((key>47 && key<58) || (key < 32 || key==127)) {return true;}
	else {return false;}
}
function local_number_format(num, lang) {
	if(num == '')
	{	num = 0
	}
	num = parseFloat(num); 
	num = num.toFixed(2); 	
	num += ''; //make sure we have a string
	if(lang=="en")
	{	num = num.replace(',','.');
	}
	else
	{	num = num.replace('.',',');
	}
	return num;
}
function internal_number(num) {
	if(num == '')
	{	num = 0
	}
	num = num + '';
	num = num.replace(',','.');
	num = parseFloat(num); 	
	return num;
}
function isimage(filename)
{	if(filename.toLowerCase().lastIndexOf(".jpg")==-1) 
	{	return false;
	}
	else
	{	return true;
	}
}
function radio_checked(fld)
{	var selectedval = "";
	for (var i=0; i < fld.length; i++)
	{	if (fld[i].checked)
		{	 return fld[i].value;			
		}
	}	
	return "";
}
function radio_set_checked(radioObj, newValue) 
{	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
function list_selected(fld)
{	var selectedval = "";
	for (var i=0; i < fld.options.length; i++)
	{	if (fld.options[i].selected)
		{	 return fld.options[i].value;			
		}
	}	
	return "";
}

function fGetSDims () {
	var x, y;
	if (self.pageYOffset) { // all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6/7 Strict Mode
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) { // all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [x, y];
}
function scrollto(elementId, hideEl, space_on_top) {
	var coords = findPos(document.getElementById(elementId));
	if(space_on_top > 0)
	{	if((coords[1] - space_on_top) > 0 )
		{	coords[1] = coords[1] - space_on_top;
		}		
	}	
	var dist;
	var coef = 1;
	var y;
	var diff;	
	var slideStep = 30;
    var slideTime = 30;	
	var dimens = fGetSDims();
	y = dimens[1];	
	dist = coords[1] - y;
	if (dist < 0) {
		coef = -1;
	}	
	var timer = setInterval(function () {
		dimens = fGetSDims();
		y = dimens[1];
	
		if ((coords[1] - y)*coef < ((dist*coef)/slideStep)) {
			window.scrollTo(0, coords[1]);
			clearInterval(timer);
			if (hideEl) {
				hideEl.style.display = "none";
			}
		} else {
			window.scrollBy(0, (dist/slideStep));
			dimens = fGetSDims();		
			diff = dimens[1];			
			if (y === diff) { //scroll bottom reached
				clearInterval(timer);
				if (hideEl) {
					hideEl.style.display = "none";
				}
			}
		}
	}, slideTime);
}
function goto(elementId, space_on_top)
{	
	var coords = findPos(document.getElementById(elementId));
	var dimens = fGetSDims();
	if(space_on_top > 0)
	{	if((coords[1] - space_on_top) > 0 )
		{	coords[1] = coords[1] - space_on_top;
		}		
	}
	window.scrollTo(0, coords[1]);
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

/* ----------------------------------- PASSWORD GEN ---------------------------- */
function gen_pass(fld)
{	var passfield = fld;
	if(passfield)
	{	passfield.value = GeneratePassword(10, true, false);
	}
}
// generieke hulpfuncties
function checkPunc(num) 
{	if ((num >=33) && (num <=47)) { return true; }
	if ((num >=58) && (num <=64)) { return true; }
	if ((num >=91) && (num <=96)) { return true; }
	if ((num >=123) && (num <=126)) { return true; }
	return false;
}
function GeneratePassword(length, noPunction, randomLength) 
{	var sPassword = "";    
	if (randomLength) {
		length = Math.random();
		length = parseInt(length * 100);
		length = (length % 7) + 6
	}
	for (i=0; i < length; i++) {

		numI = getRandomNum();
		if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

		sPassword = sPassword + String.fromCharCode(numI);
	}
	return sPassword;
}
function getRandomNum() 
{	// between 0 - 1
	var rndNum = Math.random()
	// rndNum from 0 - 1000
	rndNum = parseInt(rndNum * 1000);
	// rndNum from 33 - 127
	rndNum = (rndNum % 94) + 33;
	return rndNum;
}		
	
/* ------------------------------------ LIGHTBOX ------------------------------- */
function ShowLightBox(name, focus_to){											
	//vergeet geen div aan te maken met id edit_block_background	
	var objOverlay = document.getElementById('lightbox_block_background');
	var objLightbox = document.getElementById(name);
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 ) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 ) / 2);
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	if (document.body.scrollWidth > document.body.offsetWidth) // all but Explorer Mac											
	{	x = document.body.scrollWidth;
	}											
	else // Explorer Mac would also work in Explorer 6 Strict, Mozilla and Safari
	{	x = document.body.offsetWidth;
	}
	objOverlay.style.display="";
	objOverlay.style.width = x + "px";
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objLightbox.style.display = "";	
	// probeer cursor in focus veld te plaatsen
	if(focus_to != "")
	{	setfocus(focus_to);
	}	
}
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}		
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function CloseLightBox(name){
	var thebox = document.getElementById(name);
	thebox.style.display = "none";
	document.getElementById("lightbox_block_background").style.display="none";	
}

/* ------------------------------------ AJAX --------------------------------- */
var ajaxDirectory = "/ajax/";
var errorText = "error";
var generalErrorMessage = "Er heeft zich een fout voorgedaan. Probeer het opnieuw aub."
var noBrowserSupport = "Sorry, uw browser ondersteunt geen AJAX technologie."
var xmlHttp;
var responseText;
var responseXml;
var ajaxasync = true;
// geef de pagina (met parameters die uitgevoerd moet worden) en de js functie die na de call moet uitgevoerd worden
function fdoAjaxPostText(page, funcname)
{	fdoAjax(page, funcname,"text", "post")
}
function fdoAjaxText(page, funcname)
{	fdoAjax(page, funcname,"text", "get")
}
function fdoAjaxXml(page, funcname)
{	fdoAjax(page, funcname,"xml", "get");	
}
function fdoAjax(pagina, funcname,method, post_or_get) 
{	responseText = '';
	responseXml = '';
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert (noBrowserSupport);
		return;
	}	
	if(post_or_get=="get")
	{	var url=ajaxDirectory + pagina+"&sid="+Math.random();		
		xmlHttp.onreadystatechange=Function("fEndAjax(" + funcname + ",'"+ method+"')");
		xmlHttp.open("GET",url,ajaxasync);
		xmlHttp.send(null);
	}
	else
	{	var url= ajaxDirectory + pagina.split("?")[0];
		var query= pagina.split("?")[1];
		xmlHttp.open("POST", url, true);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", query.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = Function("fEndAjax(" + funcname + ",'"+ method+"')");
		xmlHttp.send(query);
	}
}
function fEndAjax(funcname,method)
{	document.body.style.cursor = 'wait';
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{ 	if ((xmlHttp.status != '200') && (xmlHttp.status != '0'))
		{	alert('Error ' + xmlHttp.status + "-" + xmlHttp.responseText);
		}
		if (xmlHttp.status == '200')
		{	
			if (method=="text")
			{					
				if (xmlHttp.responseText != errorText) 
				{
					// processed OK					
					responseText= xmlHttp.responseText;
					funcname();
				}
				else 
				{	alert(generalErrorMessage + " (code 1)")					
				}
			} 
			else 
			{	if (xmlHttp.responseText != errorText) {
					// processed OK
					if (window.ActiveXObject)
					{	responseXml=new ActiveXObject("Microsoft.XMLDOM");
					}
					// code for Mozilla, Firefox, Opera, etc.
					else if (document.implementation && document.implementation.createDocument)
					{
						responseXml=document.implementation.createDocument("","",null);
					}					
					responseXml = xmlHttp.responseXML;				
				
					funcname();
					
				} else { //an error occurred
					alert(generalErrorMessage + " (code 2)")
				}			
			}
		} 
		else 
		{	alert(generalErrorMessage + " (code 3)")
		}
	}	
	document.body.style.cursor = 'default';
}
function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}
function fillbox(box, list){
	var rec	= list.split("-|-");
	var ret = "";
	box.options.length = 0;
	for (i in rec){
		if (rec[i]!=''){
			var dd = rec[i].split('-,-')[0].indexOf('*');
			var ee = dd != -1 ? true:false;
			box[i] = new Option(rec[i].split('-,-')[1], rec[i].split('-,-')[0].replace('*', ''),false,ee);
			if (ee) {ret = rec[i].split('-,-')[0].replace('*', '');}
		}
	}
	return ret;	
}
function URLencode(sStr) {
    return escape(sStr).
             replace(/\+/g, '%2B').
                replace(/\"/g,'%22').
                   replace(/\'/g, '%27').
                     replace(/\//g,'%2F');
}
function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}
