var old = null;
function goToHome(){
    window.location = "/";
}
function clearField(field, text) {
    if (field.value == text) {
        field.style.color = "#000000";
        field.value = "";
    } else if (field.value == "") {
        field.style.color = "#AAAAAA";
        field.value = text;
    }
}
function submitenter(field,e){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13){
        field.form.submit();
        return false;
    }
    else
        return true;
}
function mouseOverWithDisplayNone(id){
    if(old){
        document.getElementById(old).style.display = "none";
    }
    document.getElementById(id).style.display = "block";
    old = id;
}
function mouseOutWithDisplayNone(id){
    document.getElementById(id).style.display = "none";
}
function layerOpen(layer){
    var oldStyle = document.getElementById(layer).style.display.toString();

    if(old!=null){
        document.getElementById(old).style.display = "none";
        document.getElementById("link"+old).className = "";
    }
    
    if(old==null || (document.getElementById(layer) && oldStyle != "block")){
        document.getElementById(layer).style.display = "block";
        document.getElementById("link"+layer).className = "active";
        old=layer;
    }
}
function sameHeight(){
    var hoogte  = null;
    var lis     = document.getElementsByClassName("itemcell");
    
    for(var i=0;i<lis.length;i = i+2){
        if(lis[i+1]){
            hoogte  = lis[i].clientHeight>hoogte?lis[i].clientHeight:hoogte;
            hoogte  = lis[i+1].clientHeight>hoogte?lis[i+1].clientHeight:hoogte;

            lis[i].style.height = hoogte+"px";
            lis[i+1].style.height = hoogte+"px";
            hoogte  = null;
        }
        else{
            hoogte  = lis[i].clientHeight>hoogte?lis[i].clientHeight:hoogte;
            lis[i].style.height = hoogte+"px";
        }
    }
    
}
function validOrder(){
    var total = 0;
    $(".bestelInput").each(function(){
        var value = this.value==""?0:this.value;
        var valid = this.className.toString().replace("bestelInput","").replace(/ valid(.*)$/,"$1");
        if(valid && value > 0 && value < valid){
            alert("De minimale afname van \""+this.name.toString().replace(/bestel\[(.*)\]/,"$1")+"\" is "+valid);
            return false;
        }
        total += value;
    });
    if(total > 0){
        return true;
    }
    else{
        alert("U dient een geldig aantal in te voeren.")
        return false;
    }
}

document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName){
                    retVal.push(elements[i]);
                }
            }
        } else if(elements[i].className == clsName){
            retVal.push(elements[i]);
        }
    }
    return retVal;
}

$(document).ready(function() {

    var colheight = 0;
    $(".sameheight").each(function(){
        if ($(this).height()>colheight) colheight=$(this).height();
    }).css({height:colheight});

    var num = $(".section .rij").length;
    var colheight2 = 0;
    for (var i = 1; i <= num; i++) {
        $(".sameheight"+i).each(function(){
            if ($(this).height()>colheight2) colheight2=$(this).height();
        }).css({height:colheight2});        
    }
//    var colheight2 = 0;
//    $(".sameheight1").each(function(){
//        if ($(this).height()>colheight2) colheight2=$(this).height();
//    }).css({height:colheight2});
//    
//    $(".sameheight2").each(function(){
//        if ($(this).height()>colheight2) colheight2=$(this).height();
//    }).css({height:colheight2});
//    
//    $(".sameheight3").each(function(){
//        if ($(this).height()>colheight2) colheight2=$(this).height();
//    }).css({height:colheight2});
//    
//    $(".sameheight4").each(function(){
//        if ($(this).height()>colheight2) colheight2=$(this).height();
//    }).css({height:colheight2});
//    
//    $(".sameheight5").each(function(){
//        if ($(this).height()>colheight2) colheight2=$(this).height();
//    }).css({height:colheight2});
    
    var colheight3 = 0;
    $(".sameheightH2").each(function(){
        if ($(this).height()>colheight3) colheight3=$(this).height();
    }).css({height:colheight3});
    
     var colheight4 = 0;
    $(".sameheight-li").each(function(){
        if ($(this).height()>colheight4) colheight4=$(this).height();
    }).css({height:colheight4});

    $("select.refines").change(function () {
        document.location = $(this).val();
    });
    
    $("#btnAdd, #btnAdd2").click(function(event) {
        if ($("#aantal").val() == "" || !is_numeric($("#aantal").val())) {
            alert("U dient het aantal op te geven.");
            event.preventDefault();
        }
    });

});
