var ERRORMSG_DATE = 'Datum foutief';

function UnCryptMailto(s) {
  var n=0;
  var r="";
  for(var i=0; i < s.length; i++) {
    n=s.charCodeAt(i);
    if (n>=8364) {n = 128;}
    r += String.fromCharCode(n-(1));
  }
  return r;
}
  // JS function for uncrypting spam-protected emails:
function linkTo_UnCryptMailto(s)  {
  location.href=UnCryptMailto(s);
}

function setZoom(img, dir, width, height, margin, zIndex, delay) {
  setTimeout(function() {
    if (img.dir==dir) {
      img.style.width=width;
      img.style.height=height;
      img.style.margin=margin;
      img.style.zIndex=zIndex;
      img.parentNode.parentNode.style.zIndex=zIndex;
    }
  }, delay);
}

function larger(img, width, height) {
  img.dir='rtl';
  now=parseInt(img.style.zIndex);
  for (i=now+1; i<=10; i++) {
    w=(width*(10+i))/20+'px';
    h=(height*(10+i))/20+'px';
    m=(-i)+'px 0 0 '+(-width*i/40)+'px';
    setZoom(img, 'rtl', w, h, m, i, 20*(i-now));
  }
}

function smaller(img, width, height) {
  img.dir='ltr';
  now=parseInt(img.style.zIndex);
  for (i=now-1; i>=0; i--) {
    w=(width*(10+i))/20+'px';
    h=(height*(10+i))/20+'px';
    m=(-i)+'px 0 0 '+(-width*i/40)+'px';
    setZoom(img, 'ltr', w, h, m, i, 20*(now-i));
  }
}

//<img src="hier de url" style="width: 80px; height: 60px; z-index: 0;" onmouseover="larger(this,160,120)" onmouseout="smaller(this,160,120)">

function showErrorMessage(obj, msg)
{
  if (msg != '') {
    alert(msg);
  }
}

function CheckDate(obj, eeuwwissel, hideerror)
{
  var Result = true;

  var mandatory = obj.getAttribute('mandatory');
  if (mandatory == null) {
    mandatory = false;
  }

  if (!eeuwwissel) {
    eeuwwissel = 30;
  }

  var v = obj.value;

  // vervang spatie, punt en slash door streepje
  var re;
  re = new RegExp(' ', 'g');
  v = v.replace(re, '-');
  re = new RegExp('/', 'g');
  v = v.replace(re, '-');
  re = new RegExp('\\.', 'g');
  v = v.replace(re, '-');

  // splits op streepje
  arr = v.split('-');

  if (arr.length == 3) {
    d = arr[0];
    m = arr[1];
    y = arr[2];
  } else {
    switch (v.length) {
      case 4: { // DDMM
        d = v.substr(0, 2);
        m = v.substr(2, 2);
        var TodaysDate = new Date();
        y = TodaysDate.getYear();
        break;
      }
      case 6: { // DDMMYY
        d = v.substr(0, 2);
        m = v.substr(2, 2);
        y = v.substr(4, 2);
        break;
      }
      case 8: { // DDMMYYYY
        d = v.substr(0, 2);
        m = v.substr(2, 2);
        y = v.substr(4, 4);
        break;
      }
      default: Result = false;
    }
  }

  if (Result) {
    if (d.length == 1) {
      d = '0' + d;
    }
    if (m.length == 1) {
      m = '0' + m;
    }
    if (y.length == 1) {
      y = '0' + y;
    }
    if (y.length == 2) {
      if (y < eeuwwissel) {
        y = '20' + y;
      } else if (y < 100) {
        y = '19' + y;
      }
    }
    s = m + "/" + d + "/" + y;
    var testDate = new Date(s);
    if (testDate.getMonth()+1 != m){
      Result = false;
    }
  }

  if ((v.length == 0) &&
      (!mandatory)) {
    Result = true;
  } else if (Result) {
    v = d + "-" + m + "-" + y;

    obj.value = v;
  }
  if (Result || hideerror) {
    showErrorMessage(obj, '');
  } else {
    showErrorMessage(obj, ERRORMSG_DATE);
    obj.focus();
  }
  return Result;
 }

 function CheckSofinummer(obj)
 {
   var Result = true;

   var mandatory = obj.getAttribute('mandatory');
   if (mandatory == null) {
     mandatory = false;
   }

   var v = obj.value;
   var reSofi = /(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/;
   var array;
   var lSom = 0;
   if ((obj.value.length == 0) ||
       (obj.value == null)) {
     Result = !mandatory;
   } else {
     Result = (reSofi.test(obj.value));
     if (Result) {
       // 11 proef
       array = (obj.value.match(reSofi));
       for (i=1;i<9;i+=1)
         lSom += (array[i] * (9-(i-1)));
       Result = (((lSom -= array[9]) % 11) == 0)
     }
   }

   if (Result) {
     showErrorMessage(obj, '');
   } else {
     showErrorMessage(obj, ERRORMSG_SOFI);
     obj.focus();
   }
   return Result;
 }

var minFontSize =  8;
var maxFontSize = 18;

function increaseFontSize(aTag) {
  if (aTag == '') aTag = 'p';
  var p = document.getElementsByTagName(aTag);
  for (i=0;i<p.length;i++) {
    if(p[i].style.fontSize) {
      var s = parseInt(p[i].style.fontSize.replace("px",""));
    } else {
      var s = 11;
    }
    if (s!=maxFontSize) {
      s += 1;
    }
    p[i].style.fontSize = s+"px"
  }
}

function decreaseFontSize(aTag) {
  if (aTag == '') aTag = 'p';
  var p = document.getElementsByTagName(aTag);
  for (i=0;i<p.length;i++) {
    if (p[i].style.fontSize) {
      var s = parseInt(p[i].style.fontSize.replace("px",""));
    } else {
      var s = 11;
    }
    if (s!=minFontSize) {
      s -= 1;
    }
    p[i].style.fontSize = s+"px"
  }   
}

function GeneratePassword(aInput) {
    if (parseInt(navigator.appVersion) <= 3) { 
        alert("Sorry this only works in 4.0 browsers"); 
        return true; 
    }
    
    var length=8;
    var sPassword = "";
    
    var noPunction = true;
    var randomLength = false;
    
    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);
    }
    aInput.value = sPassword
    
    return true;
}

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;
}

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;
}
