/*
	Ophalen van sessie cookies die aangemaakt zijn doormiddel
	van javascript
*/

function Get_Cookie(Name)
{
	var search = Name + "="
	var returnvalue = "";

	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search)

		if (offset != -1)
		{
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value

			if (end == -1)
				end = document.cookie.length;

			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}


/*
	TaalSwitch
*/
function switchTaal(taalcode)
{
	var url = document.location.href;
	var newurl = '';

	//Controleer of er al een lang in de url staat
	var search_lang = 'lang'

	if(url.length > 0)
	{
		offset = url.indexOf(search_lang);

		if(offset != -1){
			//Strip de lang van de url
			newurl = url.substring(0, (offset-1));
		}
		else{
			newurl = url;
		}
	}

	var x = '?';
	var check = newurl.indexOf(x);

	if(check != -1)	{
		var addtaal = '&lang='+taalcode;
	}else{
		var addtaal = '?lang='+taalcode;
	}

    document.location.href=newurl+addtaal;
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""

	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}


function enterSubmit(e)
{
	var key = window.event ? e.keyCode : e.which;

	if (key == 13)
	{
		 document.letsForm.submit();

		 return false;
	}
}

function isnumeriek(obj, mode)
{
	var strString = obj.value;

  	if(mode == -1)
	{
  		var strValidChars = "0123456789+-()";
	}
	else if(mode == 'tel')
	{
		var strValidChars = "0123456789+-()";
	}
	else if(mode == 'cijfer')
	{
		var strValidChars = "0123456789";
	}
	else if(mode == 'letter')
	{
		var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	}


  	var strChar = '';
	var blnResult = true;
	var newstr = ''

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);

		if (strValidChars.indexOf(strChar) != -1)
		{
			newstr = newstr + strChar
		}
	}

	if(!newstr)
		obj.value = '';
	else
		obj.value = newstr;

}

function checkinput(obj, mode)
{
	var strString = obj.value;

  	if(mode == -1)
	{
  		var strValidChars = "0123456789+-()";
	}
	else if(mode == 'tel')
	{
		var strValidChars = "0123456789+-()";
	}
	else if(mode == 'cijfer')
	{
		var strValidChars = "0123456789";
	}
	else if(mode == 'letters')
	{
		var strValidChars = "0123456789~!@#$%^&*()_+|}{:?>,.</[]/\=-";
	}


  	var strChar = '';
	var blnResult = true;
	var newstr = ''

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);

		if(mode == 'letters')
		{
			if (strValidChars.indexOf(strChar) == -1)
			{
				newstr = newstr + strChar
			}
		}
		else if(mode == 'cijfer')
		{
			if (strValidChars.indexOf(strChar) != -1)
			{
				newstr = newstr + strChar
			}
		}
	}

	if(!newstr)
		obj.value = '';
	else
		obj.value = newstr;

}

function checkemail(obj)
{
	var email = obj.value;

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (filter.test(email)){
		document.letsForm.emailcheck.value=0;

		return true
	}
	else{
		document.letsForm.emailcheck.value=1;

		return false
	}
}

//--Plaats na div of in footer
//window.onload = function(){	HeightDiv('scroll', 102);}
//window.onresize = function(){ HeightDiv('scroll', 102);}
function HeightDiv (obj_id, height)
{
	var cHeight = height;

	if(obj_id == '')
		obj_id = 'scroller';

	if (self.innerHeight){
		wHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		wHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		wHeight = document.body.clientHeight;
	}

	nHeight = Math.round(wHeight-cHeight);

	if(document.getElementById(obj_id))
		document.getElementById(obj_id).style.height=nHeight;
	else
		return false;
}

var win = null;
function popUp(url, id, options) {

	day = new Date();

	if(!id){
		id = day.getTime();
	}

	if(!options){
		options="toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=350,height=200,left=300,top = 300";
	}

	if (win && win.open && !win.closed)
	{
		//win.focus();
		win.close();
		win = eval("page" + id + " = window.open(url, '" + id + "', '"+ options +"');");
	}
	else
		win = eval("page" + id + " = window.open(url, '" + id + "', '"+ options +"');");
}

function show_debug(val)
{
	if(document.getElementById('debug'))
	{
		var debug = document.getElementById('debug');	//--tmp debug div

		debug.innerHTML = debug.innerHTML + val + '<br />';
	}
}


/**** RETURNS THE SIZE OF THE VIEWPORT ****/
function getViewportSize() {
	var size = [0, 0];

	if (typeof window.innerWidth != 'undefined') {
		size = [window.innerWidth, window.innerHeight];
	} else if (typeof document.documentElement != 'undefined'
			   && typeof document.documentElement.clientWidth != 'undefined'
			   && document.documentElement.clientWidth != 0) {
		size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
	} else {
		size = [document.getElementsByTagName('body')[0].clientWidth,
				document.getElementsByTagName('body')[0].clientHeight];
	}
	return size;
}

/*** PRINT CONTENT ***/
/*
function printContent() {

	if(document.getElementById('content')) {

		var contentContainer = document.getElementById('content');
		var printContainer = document.getElementById('print_content');
		var contentText = contentContainer.innerHTML;
		var printText = contentText.replace(/\n/g, "");

		printContainer.innerHTML = contentText;

		window.print();
	}
}

*/

/*** EMAIL CONTENT ***/
function showEmailContentForm() {

	if (document.getElementById('email_content')) {

		var viewportSize = getViewportSize();
		var emailFormContainer = document.getElementById('email_content');
		var x = (viewportSize[0] / 2) - 150; // - breedte / 2
		var y = (viewportSize[1] / 2) - 100; // - hoogte / 2

		emailFormContainer.style.left = x + 'px';
		emailFormContainer.style.top = y + 'px';
		emailFormContainer.style.display = 'block';
	}
}

function hideEmailContentForm() {
	var emailFormContainer = document.getElementById('email_content');

	emailFormContainer.style.display = 'none';
}

function confirmMailSent(response_message) {
	//hideEmailContentForm();

	alert(response_message);
}

/**********************/
/*** FORM VALIDATOR ***/
/**********************/
// http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

function Validator(frmname) {
	this.formobj = document.forms[frmname];

	if(!this.formobj) {
		alert("BUG: couldnot get Form object "+frmname);
		return;
	}

	if(this.formobj.onsubmit) {
		this.formobj.old_onsubmit = this.formobj.onsubmit;
		this.formobj.onsubmit=null;
	} else {
		this.formobj.old_onsubmit = null;
	}

	this.formobj.onsubmit = form_submit_handler;
	this.addValidation = add_validation;
	this.setAddnlValidationFunction = set_addnl_vfunction;
	this.clearAllValidations = clear_all_validations;
}

function set_addnl_vfunction(functionname) {
	this.formobj.addnlvalidation = functionname;
}

function clear_all_validations() {
	for(var itr=0; itr < this.formobj.elements.length; itr++) {
		this.formobj.elements[itr].validationset = null;
	}
}

function form_submit_handler() {
	for(var itr=0; itr < this.elements.length;itr++) {
		if(this.elements[itr].validationset && !this.elements[itr].validationset.validate()) {
			return false;
		}
	}

	if(this.addnlvalidation) {
		str =" var ret = "  +this.addnlvalidation + "()";
		eval(str);

		if(!ret) return ret;
	}

	return true;
}

function add_validation(itemname, descriptor, errstr) {
	if(!this.formobj) {
		alert("BUG: the form object is not set properly");
		return;
	}

	var itemobj = this.formobj[itemname];

	if(!itemobj) {
		alert("BUG: Couldnot get the input object named: " + itemname);
		return;
	}

	if(!itemobj.validationset) {
		itemobj.validationset = new ValidationSet(itemobj);
	}

	itemobj.validationset.add(descriptor,errstr);
}

function ValidationDesc(inputitem, desc, error) {
	this.desc = desc;
	this.error = error;
	this.itemobj = inputitem;
	this.validate = vdesc_validate;
}

function vdesc_validate() {
	if(!V2validateData(this.desc, this.itemobj, this.error)) {
		this.itemobj.focus();
		return false;
	}

	return true;
}

function ValidationSet(inputitem) {
	this.vSet = new Array();
	this.add = add_validationdesc;
	this.validate = vset_validate;
	this.itemobj = inputitem;
}

function add_validationdesc(desc,error) {
	this.vSet[this.vSet.length]=
	new ValidationDesc(this.itemobj,desc,error);
}

function vset_validate() {
	for(var itr=0;itr<this.vSet.length;itr++) {
		if(!this.vSet[itr].validate()) {
			return false;
		}
	}

	return true;
}

// a very simple email validation checking.
// you can add more complex email checking if it helps
function validateEmailv2(email) {
	if(email.length <= 0) {
		return true;
	}

	var splitted = email.match("^(.+)@(.+)$");

	if(splitted == null) return false;

	if(splitted[1] != null ) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;

		if(splitted[1].match(regexp_user) == null) return false;
	}

	if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;

		if(splitted[2].match(regexp_domain) == null) {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;

			if(splitted[2].match(regexp_ip) == null) return false;
		}

		return true;
	}

	return false;
}

function validateDate(date) {
	//dd-mm-YYYY

	var regexp_date=/^([0-9]{1,2})[\-]([0-9]{1,2})[\-]([0-9]{1,4})$/;

	if (date.match(regexp_date))
		return true;

	return false;
}

function V2validateData(strValidateStr, objValue, strError) {
	var epos = strValidateStr.search("=");
	var command  = "";
	var cmdvalue = "";

	if(epos >= 0) {
		command = strValidateStr.substring(0,epos);
		cmdvalue = strValidateStr.substr(epos+1);
	} else {
		command = strValidateStr;
	}

	switch(command) {
		case "req":

		case "required": {
			if(eval(objValue.value.length) == 0) {
				if(!strError || strError.length == 0) {
					strError = objValue.name + " : Required Field";
				}
				alert(strError);

				return false;
			}
			break;
		}

		case "maxlength":

		case "maxlen": {
			if(eval(objValue.value.length) > eval(cmdvalue)) {
				if(!strError || strError.length == 0) {
					strError = objValue.name + " : "+cmdvalue+" characters maximum ";
				}
				alert(strError + "\n[Current length = " + objValue.value.length + " ]");

				return false;
			}
			break;
		}

		case "minlength":

		case "minlen": {
			if(eval(objValue.value.length) <  eval(cmdvalue)) {
				if(!strError || strError.length == 0) {
					strError = objValue.name + " : " + cmdvalue + " characters minimum  ";
				}

				alert(strError + "\n[Current length = " + objValue.value.length + " ]");
				return false;
			}
			break;
		}

		case "alnum":

		case "alphanumeric": {
			var charpos = objValue.value.search("[^A-Za-z0-9]");

			if(objValue.value.length > 0 &&  charpos >= 0) {
				if(!strError || strError.length ==0)  {
					strError = objValue.name+": Only alpha-numeric characters allowed ";
				}

				alert(strError + "\n [Error character position " + eval(charpos+1)+"]");

				return false;
			}
			break;
		}

		case "num":

		case "numeric": {
			var charpos = objValue.value.search("[^0-9]");

			if(objValue.value.length > 0 &&  charpos >= 0) {
				if(!strError || strError.length ==0) {
					strError = objValue.name+": Only digits allowed ";
				}

				alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
				return false;
			}
			break;
		}

		case "alphabetic":

		case "alpha": {
			var charpos = objValue.value.search("[^A-Za-z]");

			if(objValue.value.length > 0 &&  charpos >= 0) {
				if(!strError || strError.length ==0) {
					strError = objValue.name+": Only alphabetic characters allowed ";
				}

				alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
				return false;
			}
			break;
		}

		case "alnumhyphen": {
			var charpos = objValue.value.search("[^A-Za-z0-9\-_]");

			if(objValue.value.length > 0 &&  charpos >= 0) {
				if(!strError || strError.length ==0) {
					strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _";
				}

				alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
				return false;
			}
			break;
		}

		case "email": {
			if(!validateEmailv2(objValue.value)) {
				if(!strError || strError.length == 0) {
					strError = objValue.name + ": Enter a valid Email address ";
				}

				alert(strError);
				return false;
			}
			break;
		}

		case "date": {
			if (!validateDate(objValue.value)) {
				if(!strError || strError.length == 0) {
					strError = objValue.name + ": Enter a valid date";
				}

				alert(strError);
				return false;
			}
			break;
		}

		case "lt":

		case "lessthan": {
			if(isNaN(objValue.value)) {
				alert(objValue.name+": Should be a number ");
				return false;
			}
			if(eval(objValue.value) >=  eval(cmdvalue)) {
				if(!strError || strError.length == 0) {
					strError = objValue.name + " : value should be less than "+ cmdvalue;
				}

				alert(strError);
				return false;
			}

			break;
		}

		case "gt":

		case "greaterthan": {
			if(isNaN(objValue.value)) {

				alert(objValue.name+": Should be a number ");
				return false;
			}
			if(eval(objValue.value) <=  eval(cmdvalue)) {
				if(!strError || strError.length ==0) {
					strError = objValue.name + " : value should be greater than "+ cmdvalue;
				}

				alert(strError);
				return false;
			}

			break;
		}

		case "regexp": {
			if(objValue.value.length > 0) {
				if(!objValue.value.match(cmdvalue)) {
					if(!strError || strError.length == 0) {
						strError = objValue.name+": Invalid characters found ";
					}

					alert(strError);
					return false;
				}
			}
			break;
		}

		case "dontselect": {
			if(objValue.selectedIndex == null) {
				alert("BUG: dontselect command for non-select Item");
				return false;
			}
			if(objValue.selectedIndex == eval(cmdvalue)) {
				if(!strError || strError.length ==0) {
					strError = objValue.name+": Please Select one option ";
				}

				alert(strError);
				return false;
			}
			break;
		}
	}

	return true;
}


/*functie om een formulier te printen*/
function printForm(form, title, css){

  var disp_setting="toolbar=no,location=no,directories=no,menubar=no,";
      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";

  var content_vlue = document.getElementById("content").innerHTML;

  var docprint=window.open("","",disp_setting);
      docprint.document.open();

  //headers
  docprint.document.open();
  docprint.document.write('<html>\n');
  docprint.document.write('<head>\n');
  docprint.document.write('<title>' + title + '</title>\n');
  docprint.document.write('<link href="http://algemeen.kuiper.vdlp/templates/site/skins/algemeen/css/site.css" rel="stylesheet" type="text/css" media="all" />\n');
  docprint.document.write('<link href="http://algemeen.kuiper.vdlp/templates/site/skins/algemeen/css/site_print.css" rel="stylesheet" type="text/css" media="all" />\n');
  docprint.document.write('</head>\n');
  docprint.document.write('<body onLoad="self.print()">\n');
  docprint.document.write('<left>\n');
  docprint.document.write('<div id="content" class="story">\n');
  docprint.document.write('<h1>'+title+'</h1>');
  docprint.document.write('<table cellspacing=5 class="story" width=600>');

  //formuliervelden printen
  for(i=0; i < form.length; i++) {

    if(form[i].type!='hidden' && form[i].type!='button' && form[i].type!='submit') {

      if(form[i].type=='radio' || form[i].type=='checkbox') {
        //docprint.document.write(form[i].checked);
        if(form[i].checked==true) {

          var fieldname = form[i].name;

          var fieldvalue = form[i].value;
          docprint.document.write('<tr><td width=150 valign="top">'+fieldname+' :</td><td valign="top" width=450>'+fieldvalue+'</td></tr>');
        }
      }
      /*
      else if (form[i].name == 'Locatie') { // Verwijder het e-mail adres bij de locatie select

        var fieldname = form[i].name;
        var fieldvalue = form[i].value;

        var newval = fieldvalue.split("_");
        docprint.document.write('<tr><td width=150 valign="top">'+fieldname+' :</td><td valign="top" width=450>'+newval[0]+'</td></tr>');
      }
      */
      else {

        var fieldname = form[i].name;
        var fieldvalue = form[i].value;
        docprint.document.write('<tr><td valign="top"  width=150>'+fieldname+' :</td><td valign="top" width=450>'+fieldvalue+'</td></tr>');
      }
    }
  }

  docprint.document.write('</table></div></body></html>');
  docprint.document.close();
}

function printContent()
{
  if(document.getElementById('content')) {

    var disp_setting="toolbar=no,location=no,directories=no,menubar=no,";
        disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";

    var content_vlue = document.getElementById("content").innerHTML;

    var docprint=window.open("","",disp_setting);
    docprint.document.open();
    docprint.document.write('<html>\n');
    docprint.document.write('<head>\n');
    docprint.document.write('<title></title>\n');
    docprint.document.write('<link href="http://algemeen.letsmanage3.nl/templates/site/skins/algemeen/css/site.css" rel="stylesheet" type="text/css" media="all" />\n');
    docprint.document.write('<link href="http://algemeen.letsmanage3.nl/templates/site/skins/algemeen/css/site_print.css" rel="stylesheet" type="text/css" media="all" />\n');
    docprint.document.write('</head>\n');
    docprint.document.write('<body onLoad="self.print()">\n');
    docprint.document.write('<left>\n');
    docprint.document.write('<div id="content" class="story">\n');
    docprint.document.write(content_vlue);
    docprint.document.write('</div>\n');
    docprint.document.write('</left>\n');
    docprint.document.write('</body>\n');
    docprint.document.write('</html>\n');
    docprint.document.close();
    docprint.focus();
  }
}
