var isIE = (navigator.appName == "Microsoft Internet Explorer");
var isLoaded = 0;
var isNetscape = (navigator.appName == "Netscape");
var prevHeight = 0;
var prevWidth = 0;
function afterLoad() {
//records isLoaded and does onResize()
isLoaded = 1;
onLoadFunctions();
onResize();
}
function $id(x){return document.getElementById(x)};
function $name(x){return document.getElementByName(x)};
function $tag(x){return document.getElementByTagName(x)};
function bestellen_calc() {
    var nelts = ["nlegering", "nmateriaal", "nbeide"];
    var prices = [50, 80, 110];
    var f = document.forms[0];
    var s ="";
    //for (var i=0; i < f.elements.length; i++)
    //    s += "\n" + f.elements[i].id + ": " + f.elements[i].name;
    var excl = 10;
    for (var i = 0; i<nelts.length; i++) {
            var val = 0 + f.elements[nelts[i]].value;
            var price = prices[i];
            if (val && val > 0 && val <12)
                excl += val*price;
            else if (val != 0) {
                alert("Zo'n bestelling is waarschijnlijk een tikfout: " + val);
                f.elements[nelts[i]].value = "";
            }
    }
    if (excl <= 10)
        excl = 0;
    if (excl) {
        var btw = excl*0.19;
        var incl = excl + btw;
    }
    else {
        var excl = 0;
        var btw = 0;
        var incl = 0;
    }
    replaceEltAmountAndStyle("totexbtw", excl);
    replaceEltAmountAndStyle("btw", btw);
    replaceEltAmountAndStyle("totinclbtw", incl);
}
function replaceEltAmountAndStyle(id, value) {
    var elt = $id(id);
    if (!elt) {
        alert("cannot find element with id" + id);
        return;
    }
    if (value) {
        replaceElt(id, returnMoneyString(value));
        elt.style.color = "red";
    }
    else    
        replaceElt(id, "");
        elt.style.color = "black";
    elt.style.textAlign = "right";
    
    
}
function returnMoneyString(Amount) {

var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1);
var AmountWithCommas = Amount.toLocaleString();
var arParts = String(AmountWithCommas).split(DecimalSeparator);
var intPart = arParts[0];
var decPart = (arParts.length > 1 ? arParts[1] : '');
decPart = (decPart + '00').substr(0,2);

return intPart + DecimalSeparator + decPart;

}
function isblank(s) {
    for (var i=0;i<s.length;i++) {
        var c = s.charAt(i);
        if ((c != " " && c != '\n' && c != '')) return false;
    }
    return true;
    
}
function validate_smz_form(formName) {
    var f = document.forms[formName];
    var textfields = ["naam", "email", "adres", "postcode", "plaats"];
    for (var i=0;i<textfields.length;i++) {
        var label = textfields[i];
        var formelement = f.elements[label];
        var value = formelement.value;
        if (isblank(value)) {
            var message = 'Vul s.v.p. veld "' + label + '" in.';
            alert(message);
            //setTimeout('alert("hllo")',2000);
            return;
        }
    }
    var numberfields = ["nlegering", "nmateriaal", "nbeide"];
    var ntotal = 0;
    for (var i=0;i<numberfields.length;i++) {
        var label = numberfields[i];
        var formelement = f.elements[label];
        var value = formelement.value;
        var nvalue = parseInt(value);
        if (!isNaN(nvalue))
            ntotal += parseInt(value);
    }
    if (ntotal <= 0) {
        var message = 'Kies een programma s.v.p.';
        alert(message);
            //setTimeout('alert("hllo")',2000);
        return;
        
    }
    var label = 'confirmbox';
    var formelement = f.elements[label];
    if (!formelement.checked) {
        var message = 'Bevestig s.v.p. dat u akkoord gaat met de voorwaarden';
        alert(message);
        //setTimeout('alert("hllo")',2000);
        return;
        
    }
    // all succeeded:
    alert("bestelling ok");
    return 1;
}
function focusElement(formName, elemName) {
	    var elem = document.forms[formName].elements[elemName];
	    elem.focus();
	    elem.select();
            alert("did focuselement for "+formName+", "+elemName);
	}

function getelement(form, label) {
    for (var i=0;i<form.elements.length;i++) {
        var felt = form.elements[i];
        //alert("name: "+felt.name+" id: "+felt.id + ", value: "+felt.value);
        if (felt.name == label)
            return felt;
    }
    alert ('form element "' + label + '" niet aanwezig, controleer het formulier en de javascript code');
    return false;
}
function replaceElt(id, value) {
    var elt = $id(id);
    if (!elt) {
        alert("cannot find element with id" + id);
        return;
    }
    var tn = document.createTextNode(value);
    if (elt.hasChildNodes())
        elt.replaceChild(tn, elt.firstChild);
    else
        elt.appendChild(tn);
        
    
}
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 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 < 25 && deltay < 50)
    return;
document.location.href = document.location.href;
}
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 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 < 25)
    return;
    document.location.href = document.location.href;
}
function photopage(Text, Pic, Width, Height, AltText, Scale, SplitAt) {
    var L = Pic.length;
    var mw = 0;
    var padding = 20;
    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;
// document.write('<p>'+comment+'</p>');
    document.write('<table class="inbody" border="0" cellpadding="0" cellspacing="0" width="100%">');
    for (var i=0; i<L; i++) {
        document.write('<tr><td height=10></td></tr>')
        if (wide == 0) {
            // onder elkaar!!
            if (Text[i] != "") {
                document.write('<tr><td height=10></td></tr>');
                document.write('<tr>');
                if (leftandright > 0)
                     document.write('<td width="'+leftandright+'"></td>');
                document.write('<td class="ibtop" align="left">' + Text[i]+ '</td></tr>');
                if (leftandright > 0)
                     document.write('<td width="'+leftandright+'"></td>');
                document.write('<tr><td height=10></td></tr>')
            }
            if (Pic[i] != "") {
                document.write('<tr>');
                 if (leftandright > 0)
                     document.write('<td width="'+leftandright+'"></td>');
                document.write('<td class="ibpbottom" align="left">');
                document.write('<img src="'+Pic[i]+'" width="' + Width[i]+
                      '" height="'+Height[i]+'" alt="' + AltText[i]+
                     '">');
                document.write('</td>');
                 if (leftandright > 0)
                     document.write('<td width="'+leftandright+'"></td>');
                 document.write('</tr>');
            }
        }
        else {
            // naast elkaar!!
            // altijd (eventueel lege) tekst:
            // eventueel test:' dw:' + dw + ' mw: ' + mw + ' wide: ' + wide +
            document.write('<tr><td class="ibleft" width="50%">' + Text[i]+'</td>');
            if (Pic[i] != "") {
                 document.write('<td class="ibpright" align="left">');
                 document.write('<img src="'+Pic[i]+'" width="' + Width[i]+
                      '" height="'+Height[i]+'" alt="' + AltText[i]+
                     '">');
                 document.write('</td></tr>');
            }
            else {
                document.write('<td></td></tr>');
            }
        }
    }
    document.write('<tr><td height=10></td></tr>')
    document.write('</table>');
}
function picmenu(Pic, Width, Height, Cl, Link, Text, Scale) {
    var L = Pic.length;
    var mw = 0;
    var paddingFactor = 0.85;
    var rowHeight = 20;
    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" width="100%">';
    document.write(e);
    document.write('<tr><td height="'+ rowHeight + '"><td></tr><tr>');
    var al = "center";
    var im = '';
    var li = '';
    for (var i=0; i<L; i++) {
        if (i%nCol == 0 && i > 0) {
        document.write('</tr><tr><td height="'+ rowHeight + '"><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] != "") {
            li = '<a href="'+ Link[i] + '">' + im + '</a>';
        }
        else {
            li = im;
        }
        document.write('<td class="' + Cl[i] + '" align="' +
                        'center' + '">' + li + '</td>');
    }
    document.write('</tr><tr><td height="'+ rowHeight + '"><td></tr></table>');
}
function resizeprint(doResize, language) {
//sizes to convenient format for printing
alert ('doResize: '+doResize);
if (doResize != 1) {
    window.print();
    return;
}
alert('href:'+window.location.href);
var w = window.open(window.location.href, 'Sample', 'width=800,height=600');
w.print();
alert('close againsize:');
w.close();
}
function w(t) {
    // helper function
    document.write(t);
}

