function popup(url) {
        var w = 650;
        var h = 600;
        popupwindow(url,"ANWB_popup",w,h);
        return false;
}
function popupwindow(url,name,w,h) {
        LeftPosition = (screen.width) ? (screen.width- w)/2 : 0;
        TopPosition = (screen.height) ? (screen.height- h)/2 : 0;

   var popup = window.open(url,name,"scrollbars=yes,width="+w+",height="+h+",menubar=no,toolbar=no,top="+TopPosition+",left="+LeftPosition+",location=no,directories=no,resizable=no");

        if (popup.focus) {
                popup.focus();
        }
}
function closeopen() {
        if (window.opener && !window.opener.closed) {
                window.opener.location.href=this.href; window.close();
        }
}
var maxlimit = 1000;
function textCounter(field) {
        if (field.value.length > maxlimit) {// if too long...trim it!
                field.value = field.value.substring(0, maxlimit);
                alert("maxium veld lengte bereikt");
        }
}
function displayImage(image) {
  image.style.display = "block";
}
//
// all functions below are for displaying foto(s).
//
// addImages is the one that should be called from the html page as onload on the body.
function addImages(htmlId, url,fotoId,multiple,beginjaar,eindjaar) {
// @yomog: added logical OR clause to if-statement
    if (eindjaar == null || eindjaar == '') {
        //alert('eindjaar is null');
        eindjaar = new Date().getFullYear();
    }
    //alert('beginjaar: ' + beginjaar); 
    //alert('eindjaar: ' + eindjaar); 
    if (beginjaar == '' || beginjaar == null || beginjaar == '0')
        // We only carry fotos starting in the year 1995-1(=1994)
        var beginjaar = '1995';
    addImagesRecursive(htmlId, url, fotoId, multiple,beginjaar,eindjaar);
}
// we loop recursively through all the years untill we found the picture
// if we don't find it we use the default location without a year.
function addImagesRecursive(htmlId, url, fotoId, multiple,beginjaar,year) {
   //alert('year_param: ' + year);
   var m_imgPreloaded = document.createElement("IMG");
   m_imgPreloaded.src = url + year + "/" + fotoId + "a.jpg";
   //alert('year: ' + year); alert('beginjaar: ' + beginjaar);
   //IE reloads the image from cache and so image onload is not fired everytime. Hence added the below check. Defect Fix TT10471
   if(!m_imgPreloaded.complete){
		m_imgPreloaded.onload= function () {done(htmlId, m_imgPreloaded, url + year, fotoId, multiple);};		
  }else{
			// Once the document is loaded append the image to the id
			jQuery(document).ready( function(){  
				done(htmlId, m_imgPreloaded, url + year, fotoId, multiple);				
			});		 
  }	
   m_imgPreloaded.onerror=function () {if (year >= beginjaar) addImagesRecursive(htmlId, url, fotoId, multiple, beginjaar, year-1); else fallbackImage(htmlId, url, fotoId, multiple);};
}
// displays the picture from the fallback place.
function fallbackImage(htmlId, url, fotoId, multiple) {
    var m_imgPreloaded = document.createElement("IMG");
    m_imgPreloaded.src = url + "/" + fotoId + "a.jpg";
    m_imgPreloaded.style.display = "none";
    m_imgPreloaded.onload=function() {displayImage(m_imgPreloaded)};
    done(htmlId, m_imgPreloaded, url, fotoId, multiple);
}
// the endpoint: displays the preloaded image
// if multiple = true all other images are also fetched and displayed.
function done(htmlId, m_imgPreloaded, url, fotoId, multiple) {
    document.getElementById(htmlId).appendChild(m_imgPreloaded);

    if (multiple == true) {
       var letters = ['b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
        for (i = 0; i <= 24; i++) {
            addOneImage(htmlId, url + "/" + fotoId + letters[i] + ".jpg");
        }
    }
}
// adds one image to the html.
function addOneImage(htmlId, fullUrl) {
     var anImage = document.createElement("IMG");
     anImage.src = fullUrl;
     anImage.onload= function () {document.getElementById(htmlId).appendChild(anImage);};
}
// end of code for displaying foto's
//enter as tab
enterAsTab = function(){
    function next(e){
        var l, i, f, j, o = e.target;
        if(e.key == 13 && !/textarea|select/i.test(o.type)){
            for(i = l = (f = o.form.elements).length; f[--i] != o;);
            for(j = i; (j = (j + 1) % l) != i && (!f[j].type || f[j].disabled || f[j].readOnly || f[j].type.toLowerCase() == "hidden"););
            e.preventDefault(), j != i && f[j].focus();
        }
    }
    for(var f, i = (f = document.forms).length; i; addEvent(f[--i], "keypress", next));
}
addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
}

