﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

var MenuNiveau;

// moet niet in document.ready
Cufon.replace("h1,h2,h3");

$(document).ready(function() {

    $("input:checkbox").css("border", "none");

    //Focus op eerste text input
    $("textarea:enabled:visible, input:text:not(.datepicker):enabled:visible").filter(":first").focus();

    //Padding voor Content afhankelijk van menu
    $("#Content").css("paddingTop", 44 * (MenuNiveau + 1) + 15);

    $(".Alert").fadeOut().fadeIn().fadeOut().fadeIn();

    $(".Submit").hover(function() {
        $(this).toggleClass("Hover");
    }, function() {
        $(this).toggleClass("Hover");
    });

    //Tooltips
    $("input:image").each(function(i) {

        if ($(this).attr("title").length > 0) {

            $(this)
        .data("FsTitle", this.title)
        .removeAttr("title")

        .mouseover(function(e) {

            var div = $("<div/>", document)
            .prependTo("body")
            .addClass("PopupInfo")
            .html($(this).data("FsTitle"))
            .show()
            .css({ top: (e.pageY + 10), left: 0, right: "auto" })

            if (e.clientX + $(div).width() + 20 > $(document).width()) {
                $(div).css({ left: "auto", right: ($(document).width() - e.clientX) + "px" })
            } else {
                $(div).css({ right: "auto", left: (e.clientX + 10) + "px" })
            }

            if (jQuery.browser.msie && jQuery.browser.version <= "6.0") {
                $(div).css("width", "300px")
            } else {
                $(div).css("max-width", "300px");
            }
        })

        .mouseout(function() {
            $(".PopupInfo:first").remove();
        })

        }


    })

});

/*!
* Wijzig de waarden in de Inclusief- en Exclusief BTW textboxen.
*
* Hiervoor moet je 2 textboxen hebben: 
*   - 1 textbox met class = ".ExclBTW var1 CssSubmit". 
*   - 1 textbox met class = ".InclBTW var1 CssSubmit"
*
*   De tweede class (var1) is een willekeurig woord die voor beide hetzelfde moet zijn. 
*   Hierdoor weet je dat deze textboxen bij elkaar horen.
*   Hierna mogen nog meer classen komen, bijvoorbeeld voor de opmaak met css.
*
* Na de Inclusief BTW textbox moet een span volgen met daarin het BTW-percentage
*/
$(document).ready(function() {

    $(".ExclBTW").live("change", function(e) {
        ExclBTW(this);
    })

    $(".ExclBTW").live("keyup", function(e) {
        ExclBTW(this);
    })

    function ExclBTW(tb) {

        var InclBTW_TB
        var Percentage

        var ClassArray = $(tb).attr("class").split(" ")

        for (var i = 0; i <= ClassArray.length - 1; i++) {
            if (ClassArray[i] != "ExclBTW") {
                InclBTW_TB = $(".InclBTW." + ClassArray[i])
                break;
            }
        }

        Percentage = parseFloat($(InclBTW_TB).siblings("span:last").html())

        CorrigeerDouble(tb)

        var ExclBTW_Prijs = parseFloat($(tb).val().replace(",", "."));
        var InclBTW_Prijs = ExclBTW_Prijs * ((Percentage + 100) / 100);

        $(InclBTW_TB).val((Math.round(InclBTW_Prijs * 10000) / 10000).toString().replace(".", ","));
        $(tb).val($(tb).val().replace(".", ","))

    }

    $(".InclBTW").live("change", function(e) {
        InclBTW(this);
    })

    $(".InclBTW").live("keyup", function(e) {
        InclBTW(this);
    })

    function InclBTW(tb) {

        var ExclBTW_TB
        var Percentage

        var ClassArray = $(tb).attr("class").split(" ")

        for (var i = 0; i <= ClassArray.length - 1; i++) {
            if (ClassArray[i] != "InclBTW") {
                ExclBTW_TB = $(".ExclBTW." + ClassArray[i])
                break;
            }
        }

        Percentage = parseFloat($(tb).siblings("span:last").html())

        CorrigeerDouble(tb)

        var InclBTW_Prijs = parseFloat($(tb).val().replace(",", "."));
        var ExclBTW_Prijs = InclBTW_Prijs / ((Percentage + 100) / 100);

        $(ExclBTW_TB).val((Math.round(ExclBTW_Prijs * 10000) / 10000).toString().replace(".", ","));
        $(tb).val($(tb).val().replace(".", ","))

    }
})

function CorrigeerDouble(tb) {

    var AantalIngevuld = $(tb).val().replace(",", ".")

    if (AantalIngevuld == "") {
        $(tb).val(0);
    } else {

        if (isNaN(AantalIngevuld)) {
            if (!isNaN(parseFloat(AantalIngevuld))) {
                $(tb).val(parseFloat(AantalIngevuld).toString().replace(".", ","));
            } else {
                $(tb).val(1)
            }
        } else {
            $(tb).val(AantalIngevuld.replace(".", ","));
        }

    }

}

//nl resources moet worden geladen vóór de datepicker word geïnitialiseerd
$.datepicker.regional.nl = {
    closeText: 'Sluiten',
    prevText: '',
    nextText: '',
    currentText: 'Vandaag',
    monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
	    'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
    monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
	    'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
    dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
    dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
    dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
    dateFormat: 'dd/mm/yy', firstDay: 1,
    isRTL: false
};
$.datepicker.setDefaults($.datepicker.regional.nl);
//---

/*Guid genereren*/
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
/*---*/

/*======================================================================================*/


