/*
ALGEMENE JAVASCRIPT FUNCTIES DIE NODIG ZIJN OP DE SITE
*/

function window_popup(url,naam,width,height) {

	var scr_w = get_browser_width();
	var scr_h = get_browser_height();
	var scr_top = 0;
	var scr_left = 0;
	
	var top = scr_top + ((scr_h - height) / 2);
	var left = scr_left + ((scr_w - width) / 2);
	
	var status = 'no';
	var scrollbars = 'no';
	var toolbar = 'no';
	
	var new_window = window.open(url, naam, 'width='+width+',height='+height+',status='+status+',scrollbars='+scrollbars+',toolbar='+toolbar+',top='+top+',left='+left);
	new_window.focus();
	return new_window;
}

function window_popup_scroll(url,naam,width,height) {
	var scr_w = get_browser_width();
	var scr_h = get_browser_height();
	var scr_top = 0;
	var scr_left = 0;
	
	var top = scr_top + ((scr_h - height) / 2);
	var left = scr_left + ((scr_w - width) / 2);
	
	var status = 'no';
	var scrollbars = 'yes';
	var toolbar = 'no';
	
	var new_window = window.open(url, naam, 'width='+width+',height='+height+',status='+status+',scrollbars='+scrollbars+',toolbar='+toolbar+',top='+top+',left='+left);
	new_window.focus();
	return new_window;
}

function check_email(email_val) {
	var result = false;
	if(email_val.indexOf('@',0) == -1 || email_val.indexOf('.',0)==-1) {
		result = false;
	}
	else {
		result = true;
	}
	return result;
}

function check_empty_text(text_val) {
	var result = false;
	if(text_val == null || text_val == "" || text_val == " ") {
		result = false;
	}
	else {
		result = true;
	}
	return result;
}

function get_browser_width() {
	var width=0;
	/*
	if (navigator.userAgent.indexOf("MSIE") > 0)
		{
			width = document.body.clientWidth;
        }
	else
		{
			width =  window.outerWidth;
		}
		*/
		width = window.screen.width;
	return width;
}

function get_browser_height() {
	var height=0;
	/*if (navigator.userAgent.indexOf("MSIE") > 0)
		{
			height=document.body.clientHeight;
		} 
	else
		{
			height=window.outerHeight;
		}*/
		height = window.screen.height;
	return height;
}

function str_replace(oldText,newText,inString) {
	return (inString.split(oldText).join(newText));
}

function ltrim(str)
{
	var charlist = "";
	if(arguments.length == 1) {
		charlist = "";
	}
	else {
		charlist = arguments[1];
	   
	}
   var whitespace = new String(" \t\n\r"+charlist);

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function rtrim(str)
{
	var charlist = "";
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
  if(arguments.length == 1) {
		charlist = "";
	}
	else {
		charlist = arguments[1];
	}
   var whitespace = new String(" \t\n\r"+charlist);
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

function trim(str)
{
	var result = "";
	if(arguments.length == 1) {
		 result = rtrim(ltrim(str));
	}
	else {
		var charlist = arguments[1];
		result = rtrim(ltrim(str,charlist),charlist);   
	}
  return result;
}

function tel_aantal_karakters (f_form, f_element, tel_veld, maximum) {
	var tel_element = f_form.elements[tel_veld];
	if(f_element.value.length > maximum) {
		 f_element.value =f_element.value.substring(0, maximum);
	}
	else {
		tel_element.value = maximum - f_element.value.length;
	}
}

//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

//kinda important
var AutoClose = true;

//don't touch
function popImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
    'window.doneAlready = true;\n'+ //for Safari and Opera
    'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
    'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
    'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
    '//var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
    '//var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
    '//if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
    '}\n'+
    '<\/script>'+
    '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
    '<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}


