﻿//Script dat altijd als eerste geladen wordt. Bewaakt dat snel aangeroepen functies ook al geladen zijn.
//Als een page een body_onload event heeft dan roept het altijd de enige functie van dit script secureOnLoad aan.
//Argumenten: [0]Functie die moet bestaan voordat we verder kunnen
//          [1]Functie die daarna aangeroepen moet worden
//          [2]Array met argumenten voor deze laatste functie

var interval=50;
var maxTries=20;
var tries=0;
var f2;//Snd funct
var a;//Args for snd funct
var t1,t2;

function testForScript() {document.getElementById('noscript').style.display='none';}

function secureOnLoad(funcToTest,funcToCall,args) {
    f2=funcToCall;
    a=args;
    waitUntilExists(funcToTest);
    return true;}
function waitUntilExists(f1) {
    if (tries>=maxTries)
        noSuccess(f1+' niet gevonden');
    else { 
        try  { 
            if (typeof eval(f1)=="function") {
                clearTimeout(t1);
                waitUntilExists2();}
            else {
                tries+=1;
                t1 = setTimeout("waitUntilExists("+f1+")",interval);}
        }
        catch(e) {
            tries += 1;
            t1 = setTimeout("waitUntilExists("+f1+")",interval);}}
}
function waitUntilExists2() {
    if (tries>=maxTries)
        noSuccess(f2+' niet gevonden');
    else { 
        try  {   
            if (typeof eval(f2)=="function") {
                clearTimeout(t2);
                callSecondFunc();}
            else    {
                tries+=1;
                t2=setTimeout("waitUntilExists2()", interval);}
        }
        catch(e) {
            tries+=1;
            t2=setTimeout("waitUntilExists2()", interval);}
    }
}
function callSecondFunc() {
    var args=''; 
    if (a) {   
        for (var i=0;i<a.length;i++) {
            if(i==0)
                args=a[i];
            else
                args+=','+a[i];
        }
    }
    eval(f2+'('+args+');');
    window.status='Klaar ('+tries+')';
}
function noSuccess(msg) {window.status='Sorry, functies niet beschikbaar '+msg+'.';}
