var isIE = (navigator.appName == "Microsoft Internet Explorer");
var isLoaded = 0;
var isNetscape = (navigator.appName == "Netscape");
var last_active_element = "";
var last_element = new Array(""); //content in usp table
var last_extra_element = "";
var prevHeight = 0;
var prevWidth = 0;
var usp_text0 = new Array(""); //default text in usp table
function afterLoad() {
//records isLoaded and does onResize()
isLoaded = 1;
onLoadFunctions();
onResize();
}
function bodyHeight(subtract) {
// do at changing size also on load (through onResizeFunctions)
// expecting a div with id "body0" setting at height minus "subtract":
// or a "leftpart" (only IE); with Netscape 15 more
    var height = 0;
    var dh = document.body.clientHeight;
    if (subtract > 0 && dh > subtract) {
    var height = dh - subtract;
    if (isNetscape)
        height += 15;
    }
    else
    var d = null;
    if (isIE)
    var d = document.getElementById("leftpart");
    if (d == null)
    d = document.getElementById("body0");
    if (d == null) {
    alert("no such id: leftpart or body0");
    return;
    }
    d.style.height = height;
}
function changeSize(frameName, Width, Height, minWPadding, minHPadding, scale) {
// do at changing size also on load
// expecting a div with id "frameName":
var dw = parseInt(document.body.clientWidth*scale);
var d = document.getElementById(frameName);
if (d == undefined) {
    alert("no such id: frame");
    return;
}
var p = String(minWPadding);
var q = String(minHPadding);
if (Width > 0 && dw > Width)
   var p = String(Math.max(minWPadding, parseInt((dw-Width)/2)));
var dh = document.body.clientHeight;
if (Height > 0 && dh > Height)
   var q= String(Math.max(minHPadding, parseInt((dh-Height)/2)));
//style_node.appendChild(document.createTextNode(
var selector = "#" + frameName;
var declaration = "padding: " + q + " " + p;
setCssStyleScreenOnly(selector, declaration);
}
function copyArrayToElement(fromArray, toElement) {
    //first empty element
    while (toElement.hasChildNodes()) {
// alert("atoe, remove child");
        toElement.removeChild(toElement.lastChild);
    }
    for (var i = 0; i<fromArray.length; i++)
        if (fromArray[i] != "") {
           toElement.appendChild(fromArray[i]);
        }
}
function copyElementToArray(fromElement, toArray) {
    var kids = fromElement.childNodes;
    var numkids = kids.length;
// alert("etoa start: length array:"+toArray.length);
    for (var i = 0; i<toArray.length; i++)
        toArray[i] = "";
    for (var i=0; i<numkids; i++) {
        var clone = kids[i].cloneNode(true);
        toArray[i] = clone;
        }
}
function decreaseCols(items, cols) {
//try if cols can be lowered
if (cols == 1)
    return 1;
var nRows = Math.floor(items/cols);
if (items%cols)
    nRows++;
do {
    cols--;
    var newRows = Math.floor(items/cols);
    if (items%cols)
        newRows++;
} while (newRows == nRows);
return cols+1;
}
function doReload() {
    //reloads if height or width changed more than 10 pixels
var dw = document.body.clientWidth;
var dh = document.body.clientHeight;
var deltax = Math.abs(prevWidth-dw);
var deltay = Math.abs(prevHeight-dh);
if (deltax < 10 && deltay < 50)
    return;
document.location.href = document.location.href;
}
function getPosition(w) {
  var myX = 0, myY = 0;
  if( typeof( w.pageXOffset ) == 'number' ) {
    //Non-IE
    myX = w.pageXOffset;
    myY = w.pageYOffset;
  } else {
    myX = document.body.scrollLeft ;
    myY = document.body.scrollTop ;
  }
  return Array(myX, myY);
}
function getSize(w) {
  var myWidth = 0, myHeight = 0;
  if( typeof( w.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = w.innerWidth;
    myHeight = w.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return Array(myWidth, myHeight);
}
function indexmenu(Pic, Width, Height, Cl, Link, Text, Additional, Scale) {
    var L = Pic.length;
    var mw = 0;
    var paddingFactor = 0.98;
    for (var i=0; i<L; i++) {
        mw = Math.max(mw, Width[i]);
    }
    //width times Scale, that accounts for leftMenuWidth times
    //paddingFactor (above) which accounts for padding.
    var dw = document.body.clientWidth*Scale*paddingFactor;
    var nCol = Math.min(parseInt(dw/(mw+10)), L);
    nCol = decreaseCols(L, nCol);
    var WidthMin = Math.floor(dw/nCol);
    var widthActual = 0
    e = '<table class="indexmenu" border="0" cellpadding="0" cellspacing="0" width="100%">';
    w(e);
    w('<tr><td height="10"><td></tr><tr>');
    var al = "center";
    var im = '';
    var li = '';
    for (var i=0; i<L; i++) {
        if (i%nCol == 0 && i > 0) {
        w('</tr><tr><td height="10"><td></tr><tr>');
        }
        if (Link[i] != "")
            li = '<a href="'+ Link[i] + '">' + Text[i] + '</a><br>';
        else
            li = Text[i];
        if (Pic[i] != "") {
             widthActual = Math.max(WidthMin, Width[i]);
             im = '<img src="'+Pic[i]+'" width="' + Width[i] +
                      '" height="'+Height[i]+'" alt="' + Text[i] +
                    '" class="indexim">';
             if (Link[i] != "") {
                im = '<a href="'+ Link[i] + '">' + im + '</a>';
             }
        }
        else {
            widthActual = WidthMin;
            im = "";
        }
        if (Pic[i] == "" && Additional[i] != "")
            li = li + "<br><br>"+Additional[i];
        w('<td class="' + Cl[i] + '" align="center" width="' +
                widthActual + '">' + li + im + '</td>');
    }
    w('</tr><tr><td height="10"><td></tr></table>');
}
function m(a, b, s, t) {
var mm;mm='<a href="';mm=mm+'mailto';mm+=":"+b;mm+="@"+a;
if(s){mm+="?subj";mm+="ect="+s};mm+='">';
if(t)mm=mm+t;else mm=mm+b+"@"+a;mm+="</a>";w(mm);
}
function onResize() {
    //acts if height or width changed more than 10 pixels
    if (!isLoaded) return;
    var dw = document.body.clientWidth;
    var dh = document.body.clientHeight;
    var deltax = Math.abs(prevWidth-dw);
    var deltay = Math.abs(prevHeight-dh);
    if (deltax < 10 && deltay < 50)
    return;
    prevHeight=dh;
    prevWidth=dw;
    onResizeFunctions();
}
function onResizeReload() {
    //reloads if height or width changed more than 10 pixels
    if (!isLoaded) return;
    var dw = document.body.clientWidth;
    var dh = document.body.clientHeight;
    var deltax = Math.abs(prevWidth-dw);
    var deltay = Math.abs(prevHeight-dh);
    if (deltax < 25 && deltay < 50)
    return;
    document.location.href = document.location.href;
}
function photopage(Text, Pic, Width, Height, AltText, Scale, SplitAt) {
    // with table colspan if portrait picture and padding space
    var L = Pic.length;
    var mw = 0;
    var padding = 20;
    var wi = 0;
    var he = 0;
    for (var i=0; i<L; i++) {
        mw = Math.max(mw, Width[i]);
    }
    //width times Scale, that accounts for leftMenuWidth times
    //paddingFactor (above) which accounts for padding.
    var available = document.body.clientWidth*Scale - 2*padding;
    var dw = available*(1.0-SplitAt);
    var wide = 0;
    var leftandright = 0;
    if (dw > mw) wide = 1;
    else {
        if (available > mw)
            leftandright = (available - mw)/2;
    }
   // comment = 'wide; ' + wide + ' available: '+available + ' mw: '+ mw + ' leftandright:' + leftandright;
   // w('<p>'+comment+'</p>');
    w('<table class="inbody" border="0" cellpadding="0" cellspacing="0" width="100%">');
    for (var i=0; i<L; i++) {
        w('<tr><td height=10></td></tr>')
        if (wide == 0) {
            // onder elkaar!!
            wi = Width[i];
            he = Height[i];
            if (Text[i] != "") {
                w('<tr><td height=10></td></tr>');
                w('<tr>');
                // extra cells is landscape and padding is wished:
                if (leftandright > 0 && wi > he)
                     w('<td width="'+leftandright+'"></td>');
                // the td cell for the text:
                w('<td class="ibtop" align="left"');
                // span for portrait pictures:
                if (leftandright > 0 && wi <= he)
                    w(' colspan=3');
                w('>' + Text[i]+ '</td>');
                if (leftandright > 0 && wi > he)
                     w('<td width="'+leftandright+'"></td>');
                w('</tr><tr><td height=10></td></tr>')
            }
            if (Pic[i] != "") {
                w('<tr>');
                 if (leftandright > 0)
                     w('<td width="'+leftandright+'"></td>');
                w('<td class="ibpbottom" align="left">');
                w('<img src="'+Pic[i]+'" width="' + Width[i]+
                      '" height="'+Height[i]+'" alt="' + AltText[i]+
                     '">');
                w('</td>');
                 if (leftandright > 0)
                     w('<td width="'+leftandright+'"></td>');
                 w('</tr>');
            }
        }
        else {
            // naast elkaar!!
            // altijd (eventueel lege) tekst:
            // eventueel test:' dw:' + dw + ' mw: ' + mw + ' wide: ' + wide +
            w('<tr><td class="ibleft" width="50%">' + Text[i]+'</td>');
            if (Pic[i] != "") {
                 w('<td class="ibpright" align="left">');
                 w('<img src="'+Pic[i]+'" width="' + Width[i]+
                      '" height="'+Height[i]+'" alt="' + AltText[i]+
                     '">');
                 w('</td></tr>');
            }
            else {
                w('<td></td></tr>');
            }
        }
    }
    w('<tr><td height=10></td></tr>')
    w('</table>');
}
function picmenu(Pic, Width, Height, Cl, Link, Text, Scale) {
    var L = Pic.length;
    var mw = 0;
    var paddingFactor = 0.95;
    for (var i=0; i<L; i++) {
        mw = Math.max(mw, Width[i]);
    }
    //width times Scale, that accounts for leftMenuWidth times
    //paddingFactor (above) which accounts for padding.
    var dw = document.body.clientWidth*Scale*paddingFactor;
    var nCol = Math.min(parseInt(dw/(mw+10)), L);
    nCol = decreaseCols(L, nCol);
    e = '<table class="picmenu" border="0" cellpadding="0" cellspacing="0" width="100%">';
    w(e);
    w('<tr><td height="10"><td></tr><tr>');
    var al = "center";
    var im = '';
    var li = '';
    for (var i=0; i<L; i++) {
        if (i%nCol == 0 && i > 0) {
        w('</tr><tr><td height="10"><td></tr><tr>');
        }
        if (Pic[i] != "") {
             im = '<img src="'+Pic[i]+'" width="' + Width[i]+
                      '" height="'+Height[i]+'" alt="' + Text[i]+
                     '">';
        }
        else {
            im = Text[i];
        }
        if (Link[i] != "") {
            if (Pic[i] != "") {
            li = '<p class="picmenu"><a href="'+ Link[i] + '">' + im + '</a><br>'+Text[i]+'</p>';
            } else {
            li = '<p class="picmenu"><a href="'+ Link[i] + '">' + im + '</a></p>';
        }
        }
        else {
            li = im;
        }
        w('<td class="' + Cl[i] + '" align="' +
                        'center' + '">' + li + '</td>');
    }
    w('</tr><tr><td height="10"><td></tr></table>');
}
function prelima(Name, Width, Height) {
    var L = Name.length;
    for (var i=0; i<L; i++) {
        (new Image(Width[i], Height[i])).src = Name[i];
    }
}
function resizeprint(doResize, language) {
//sizes to convenient format for printing
var toX = 800;
var toY = 600;
var posX = 100;
var posY = 100;
alert ('doResize: '+doResize);
if (doResize != 1) {
    window.print();
    return;
}
var oldSize = getSize(window);
var oldPosition = getPosition (window);
alert('position: ' + oldPosition);
window.resizeTo(toX, toY);
window.moveTo(posX, posY);
var newPosition = getPosition (window);
alert('position: ' + newPosition);
var newSize = getSize(window);
var oldX = oldSize[0];
var oldY = oldSize[1];
var newX = newSize[0];
var newY = newSize[1];
window.print();
if (language == "nl")
    var T = "terug naar het vorige venster formaat";
else
    var T = "back to the previous window size" ;
alert(T);
window.resizeTo(toX + oldX-newX, toY+oldY-newY);
newSize = getSize(window);
//alert('old: '+ oldSize +' new:' + newSize);
}
function setCssStyleScreenOnly(selector, declaration) {
// set style for element screen only. declaration WITHOUT braces!!
// example: see changeSize.
if (!isIE) { // firefox:
    var style_node = document.createElement("style");
    style_node.setAttribute("type", "text/css");
    style_node.setAttribute("media", "screen");
    var text = selector + "{" + declaration + "}";
    style_node.appendChild(document.createTextNode(text));
    // append the style node:
    document.getElementsByTagName("head")[0].appendChild(style_node);
}
else { //IE
   if (declaration.indexOf("url(../") >= 0){
    declaration = declaration.replace(/..\//g, "");
   }
   if (document.styleSheets && document.styleSheets.length > 0) {
         var last_style_node = document.styleSheets[document.styleSheets.length - 1];
         if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
        }
}
}
function tdhide(idnum) {
//do a (closing) hide by leaving a table usp cell, for tableusp and tableusp2
var tfield = document.getElementById(idnum+'t');
tfield.className = "tduspsb";
var e=document.getElementById(idnum+'h');
var f=document.getElementById('tduspe');
copyArrayToElement(usp_text0, f);
}
function tdshow(idnum) {
//do show in usp table (from tableusp and tableusp2)
last_extra_element = ""
if (last_active_element != "")
    tdhide(last_active_element);
last_active_element = idnum;
var tfield = document.getElementById(idnum+'t');
tfield.className = "tduspsa";
var e=document.getElementById(idnum+'h');
var f=document.getElementById('tduspe');
var toChild = f.lastChild;
// first time copy contents to array:
if (usp_text0.length == 1 && usp_text0[0] == "" && f.hasChildNodes())
    copyElementToArray(f, usp_text0);
//copy contents to last_array:
copyElementToArray(e, last_element);
//put element in e cell:
copyArrayToElement(last_element, f);
}
function tdshowextra() {
//do show last content in extra cell
var efield = document.getElementById('tduspe');
//put element in e cell:
if (last_element == last_extra_element)
    return;
last_extra_element = last_element;
copyArrayToElement(last_element, efield);
//alert("extra"+efield);
if (last_active_element != "") {
    var tfield = document.getElementById(last_active_element+'t');
    tfield.className = "tduspsa";
    }
}
//helper function:
function validateForm(f, lang) {
    // hier zijn de bedragen al keer 100 gedaan, dus in eurocenten
    var ra = f.amount.value;
      var a = parseInt(ra);
        if (isNaN(a)) {
              if (lang=='nl')
        alert("Geef s.v.p. een bedrag op");
                else
        alert("Please give an number (amount)");
                return false;
    }
    if (a < 100) {
                if (lang=='nl')
            alert("Geef s.v.p. een bedrag van minstens 1 euro op");
        else
            alert("Please enter a minimum amount of 1 EUR");
        return false;
    }
    if (a > 100000) {
                if (lang=='nl')
            alert("Het maximum bedrag is 1000 euro. Wilt u meer geven, neem dan s.v.p. contact op met Stichting Nimba");
        else
              alert("The maximum amount is 1000 euro. If you want to donate more, please contact Foundation Nimba");
        return false;
    }
  // alert ("amount: "+f.amount.value + " itemPrice1: "+f.itemPrice1.value);
    return true;
}
function w(t) {
    // helper function
    document.write(t);
}
