function sendMail(emailAdresse, subject) {
    location.href = "mailto:"+emailAdresse+"?subject=" + subject;
}

function ModalPopUpOK(customerID, currentTVA, controlTva) {
    var proposedTva = document.getElementById(controlTva).value;

    var langue = GetLanguage();    

    var cleanedTva = CleanTVA(proposedTva);
    var NumEnt = 'BE' + cleanedTva;
    if ( NumEnt == currentTVA ) {
        window.location.href = 'ModifierFiche.aspx?CustomerID=' + customerID;
    }
    else {
        if (ValidateVat(proposedTva) == true) {
            /*alert(proposedTva);*/
            window.location.href = 'ModifierFiche.aspx?CustomerID=' + customerID + '&TVA=' + NumEnt;
        }
        else {
            if (langue == 'FR') {
                alert("Ce Num\351ro d'entreprise est \351rron\351.");
            }
            else {
                alert("Deze BTW is vals.");
            }
        }
    }
}

function ValidateTVA(source, value) {
    var ControlName = source.controltovalidate;

    //var vta = document.getElementById(ControlName).value.substr(0, 2);

    if (/*vta == 'BE' &&*/ ValidateVat(document.getElementById(ControlName).value) == true) {
        value.IsValid = true;
        return true;
    }
    else {
        value.IsValid = false;
        return false;
    }
}

function ValidateVat(tva) {

    //var vta = tva.substr(2, 10);

    var vta = CleanTVA(tva);

    /*if (isNaN(vta)) {
        return false;
    }*/

    if (vta.length > 10 || vta.length < 9) {
        return false;
    }

    var firstPart = vta.substr(0, 8);
    var secondPart = vta.substr(8, 2);

    var reste = firstPart % 97

    if (97 - reste != secondPart) {
        return false;
    }
    else {
        return true;
    }
}

function CleanTVA(vat) {

    var myTVA = '';

    var i;
    for (i = 0; i < vat.length; i++) {
        if(!(isNaN(vat.substr(i,1))) && vat.substr(i,1)!= ' '){
            myTVA += vat.substr(i, 1);
        }
    }

    if (myTVA.length < 10 && myTVA.length > 8) {
        myTVA = '0' + myTVA;
    }

    return myTVA;
}

function GetLanguage() {

    var langue;

    try {
        if (document.getElementById("searchLinkButton").innerText == "Rechercher") {
            langue = 'FR';
        }
        else {
            langue = 'NL';
        }
    }
    catch (e) { }

    try {
        if (document.getElementById("advancedsearchLinkButton").innerText == "Rechercher") {
            langue = 'FR';
        }
        else {
            langue = 'NL';
        }
    }
    catch (e) { }

    return langue;
}