// functions intranet
function Update_Cookie ( name,action, value ) {
   
   var new_value=''
   var cookie_value = Get_Cookie(name);
   if (cookie_value!=null) {
      
      if (action=='add') {
         // check of de waarde al bestaat in cookie
         var the_cookie = Get_Cookie(name);

         //if(the_cookie.indexOf(value)<0){
		if (cookie_value==''||cookie_value==null||cookie_value.lenght>0){
         	cookie_value=value;

		}
		else{
		cookie_value=cookie_value+','+value;
		}
          	Set_Cookie(name,cookie_value,'2','/');
         //}
      } else {
           //delete
           var array_cookie = cookie_value.split(',');
           for (var i=0; i<array_cookie.length; i++) {
             if (array_cookie[i]!=value) {
                new_value = new_value+array_cookie[i]+',';
             }
           }
           var mLen = new_value.length;
           mLen=mLen-1
           new_value=new_value.substring(0,mLen);
           Set_Cookie(name,new_value,'2','/');
      }
   } else {
      if (action=='add') {
    	   Set_Cookie(name,value,'2','/');
      }
   }
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24; //days
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
         return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}


function openwindowcenter(url,name,w,h,opts){
	if(w==undefined) w=640;
	if(h==undefined) h=480;
	var x = (screen.width/2)-(w/2);
	var y = (screen.height/2)-(h/2);
	var win;
 	if(opts==null){
 		opts='toolbar=no,location=no,resizable=yes,status=yes,scrollbars=no,menubar=no';
 	}
 	win=window.open(url,name,opts + ',left='+x+',top='+y+',width='+w+',height='+h);
}

// Extra functions for ps5forms

function sofielfproef(v, channelcode)
{
 if ((channelcode=='INT' || channelcode=='int') && v=='') return true;
 var r=/^[0-9]{9}$/;
 if (!r.test(v)) return false;
 sum=0;
 for (i=1; i<9; i++)
  sum += eval(v.charAt(i-1)) * (10-i);
 return (sum % 11)==eval(v.charAt(8));
}

function bankelfproef(v)
{
 var r=/^[0-9]+$/;
 if (v=='0' || v.length>10 || !r.test(v)) return false;
 if (v.length>=9)
 {
  sum=0;
  for (i=1; i<=v.length; i++)
   sum += eval(v.charAt(v.length-i)) * i;
  return (sum % 11)==0;
 }
 else
  return true;
}

function setVisibility(elements, value)
{
 a=elements.split(',');
 for (i=0; i<a.length; i++)
 {
  e=document.getElementById(a[i]);
  if (e)
  {
   e.disabled = !value;
   e.style.visibility = value ? 'visible' : 'hidden';
  }
 }
}

function setDisable(elements, value)
{
 a=elements.split(',');
 for (i=0; i<a.length; i++)
 {
  e=document.getElementById(a[i]);
  if (e)
  {
   e.disabled = value;
   enableControl(e, !value);
   if (e.tagName == 'INPUT')
   {
    e.style.backgroundColor = (!value ? 'white' : '#E6E6E6');
    e.style.borderColor = (!value ? '#999999' : 'black');
   }
  }
 }
}


//fix voor 5.1d let op deze wordt enkel voor aanvullende schermen gebruikt !
function setDisableAll(elements, value)
{
 a=elements.split(',');
 for (i=0; i<a.length; i++)
 {
  e=document.getElementById(a[i]);
  if (e)
  {
   e.disabled = value;
   enableControl(e,false); // vince toegevoegd 5.1d
   var x=e.removeAttribute('onValidate',true);
   e.setAttribute('onValidate','');
   if (e.tagName == 'INPUT')
   {
    e.style.backgroundColor = (!value ? 'white' : '#E6E6E6');
    e.style.borderColor = (!value ? '#999999' : 'black');
   }
  }
 }
}


function setReadonly(elements, value)
{
 a=elements.split(',');
 for (i=0; i<a.length; i++)
 {
  e=document.getElementById(a[i]);
  if (e)
  {
   e.readOnly = value;
   if (e.tagName == 'INPUT')
   {
    e.style.backgroundColor = (!value ? 'white' : '#E6E6E6');
    e.style.borderColor = (!value ? '#999999' : 'black');
   }
  }
 }
}

function trim(s)
{
 while (c = s.substring(0, 1), c==' ' || c=='\n' || c=='\r')
  s = s.substring(1, s.length);
 while (c = s.substring(s.length-1, s.length), c==' ' || c=='\n' || c=='\r')
  s = s.substring(0, s.length-1);
 return s;
}

function regexpToUpper(r, control)
{
 control.value = trim(control.value.toUpperCase());
 return r.test(control.value);
}

function regexpInitUpper(r, control)
{
 control.value = trim(control.value);
 if (control.value.length>0)
  control.value = control.value.substr(0,1).toUpperCase() + control.value.substr(1);
 return r.test(control.value);
}

function regexpPostcodeUpper(control)
{
 r=/^([0-9]{4})\s*([A-Z]{2})$/i;
 control.value = trim(control.value);
 if (r.test(control.value))
  control.value = r.exec(control.value)[1] + ' ' + r.exec(control.value)[2].toUpperCase();
 return r.test(control.value);
}

function regexpPartialPostcodeUpper(control)
{
 r=/^(([0-9]{2,4})\s*([A-Z]{1,2})?)?$/i;
 control.value = trim(control.value).toUpperCase();
 x = r.exec(control.value);
 if (x)
  if (x[3].length > 0)
   control.value = x[2] + ' ' + x[3].toUpperCase();
 return r.test(control.value);
}


function genDateCheck(d,m,y, channelcode)
{
 emsg = 'Validatie voor datum '+  d + '-' +  m + '-' +  y + ' * is mislukt';
 if (d == 0 || m == 0 || y == 9999) return;
 birth = new Date(y, m - 1, d);
 if (birth.getDate() != d)
  return emsg + ': Datum bestaat niet';
}


function dateCheck(control, channelcode)
{
 emsg = 'Validatie voor Geboortedatum * is mislukt';
 d = parseInt(control.form.Geboortedag.value);
 m = parseInt(control.form.Geboortemaand.value);
 y = parseInt(control.form.Geboortejaar.value);
 if (d == 0 || m == 0 || y == 9999) return;
 birth = new Date(y, m - 1, d);
 if (birth.getDate() != d)
  return emsg + ': Datum bestaat niet';
 if (!(channelcode == 'INT' || channelcode == 'int'))
 {
  minimumage = 18;
  today = new Date();
  birth.setYear(y + minimumage);
  if (birth>today)
   return emsg + ': Minimale leeftijd is ' + minimumage;
 }
}

function dateCheckEx(control, channelcode, suffix, minAge)
{
 emsg = 'Validatie voor Geboortedatum * is mislukt';
 d = parseInt(control.value);
 m = parseInt(document.getElementById('Geboortemaand' + suffix).value);
 y = parseInt(document.getElementById('Geboortejaar' + suffix).value);
 if (d == 0 || m == 0 || y == 9999) return;
 birth = new Date(y, m - 1, d);
 if (birth.getDate() != d)
  return emsg + ': Datum bestaat niet';
 if (!(channelcode == 'INT' || channelcode == 'int') && minAge>0)
 {
  today = new Date();
  birth.setYear(y + minAge);
  if (birth>today)
   return emsg + ': Minimale leeftijd is ' + minAge;
 }
}

function cmpDatepicker(obj1, obj2)
{
 return 0;
}

function hasClass(obj) 
{
  var result = false;
  if (obj.getAttributeNode("class") != null) 
  {
    result = obj.getAttributeNode("class").value;
  }
  return result;
}   

function stripeTable(id) 
{
  if (id==undefined) id = 'tb_Form';
  var even = false;
  var table = document.getElementById(id);
  if (!table) return;

  table.cellPadding = 0;
  table.cellSpacing = 0;
  if (id == 'tb_Form') table.parentNode.parentNode.parentNode.parentNode.cellSpacing = 0;

  var trs = table.getElementsByTagName('tr');
  for (var i = 0; i < trs.length; i++)
    if (trs[i].parentNode.parentNode.id == id)
    {
      trs[i].className = even ? 'even' : 'odd';
      even = !even;
    }
}





/* voer een submit uit bij enter in een formulier */
/* voeg onKeyPress="return submitenter(this,event)" toe aan input tag om dit werkend te krijgen */
/* Jan-Loek 22-03-2007 */
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}


/* Opent een popup met informatie */
function popupzw(control) {
  if (control.value==1){
	newwindowzw=window.open('','name','height=150,width=400');
	var tmp = newwindowzw.document;
	tmp.write('<html><head><title>Herinnering</title>');
	tmp.write('</head><body><p align="center">Denkt u eraan om langs een van de aangesloten corporaties te gaan om uw woonwaarde te verzilveren.</p>');
	tmp.write('<p align="center"><a target="_blank" href="https://www.woonzicht.nl/smartsite.dws?ch=DEF,dD9&id=66">Klik hier voor meer uitleg.</a></p>');
	tmp.write('<p align="center"><a href="javascript:self.close()">Sluiten</a></p>');
	tmp.write('</body></html>');
	tmp.close();
  }
}

function is_valid_email_address(control){
var filter=/^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/;
control.value = trim(control.value);
 if (control.value.length>0) {
	return filter.test(control.value);
} else {
	return true;
}
}