﻿function ValidateNRN(sender, e) {
    var nrn = e.Value;
    var cleanNrn = nrn.replace(/\./g, '').replace(/-/g, '').replace(' ', '');
    if (cleanNrn.length != 11) {
        e.IsValid = false;
        return;
    }

    var year = cleanNrn.substring(0, 2);        
    var core = cleanNrn.substring(0, 9);
    var checkDigit = cleanNrn.substring(9, 11);
//  if (year < 30)
        core2 = "2" + core;

    if (97 - (core % 97) != checkDigit && 97 - (core2 % 97) != checkDigit ) {
        e.IsValid = false;
        return;
    }
    e.IsValid = true;
}
function NormalizePhoneNumber(phoneNumber) {
    if (phoneNumber.startsWith("00"))
        return phoneNumber;
    var i = 0;
    while (i < phoneNumber.length && phoneNumber.substr(i, 1) == "0")
        i++;
    phoneNumber = phoneNumber.substr(i, phoneNumber.length - i);
    return "0032" + phoneNumber;
}
function ExpandCollapse(bulletId, childTableId) {
    var bullet = document.getElementById(bulletId);
    var childTable = document.getElementById(childTableId);

    if (childTable.style.display == "none") {
        childTable.style.display = ""
        bullet.src = "/images/icons/bullet_toggle_minus.png";
    }
    else {
        childTable.style.display = "none"
        bullet.src = "/images/icons/bullet_toggle_plus.png";
    }
}
function GetNrnFromCard(textBoxId) {
    CreateApplet();
    var oldClassName = document.getElementById(textBoxId).className;
    document.getElementById(textBoxId).value = "";
    document.getElementById(textBoxId).className = "busyTextBox";
    setNrnAsSoonAsAvailable(textBoxId);
}

function setNrnAsSoonAsAvailable(textBoxId, oldClassName) {
    var applet = getBEIDApplet();
    if (applet == null) {
        window.setTimeout("setNrnAsSoonAsAvailable('" + textBoxId + "')", 100);
        return;
    }
    else {
        applet.InitLib(null);

        if (applet.readerCount() == 0) {
            document.getElementById(textBoxId).className = oldClassName;
            alert("No Card Reader found");
            return;
        }

        if (!applet.isCardPresent(applet.getReaderByNum(0))) {
            document.getElementById(textBoxId).className = oldClassName;
            alert("Insert Card");
            return;
        }

        document.getElementById(textBoxId).value = applet.getNationalNumber();
        document.getElementById(textBoxId).className = oldClassName;
        applet.ExitLib();
    }
}

var appletCreated = false;
function CreateApplet() {
    if (appletCreated)
        return;
    var appletPh = document.getElementById("AppletPlaceholder");

    var html = '<applet code="org.jdesktop.applet.util.JNLPAppletLauncher" codebase="/jar" name="BEIDAppletLauncher" width="0" height="0" archive="applet-launcher.jar,beid35libJava.jar,BEID_Applet.jar">';
    html += '<param name="codebase_lookup" value="false"/>';
    html += '<param name="subapplet.classname" value="be.belgium.beid.BEID_Applet"/>';
    html += '<param name="progressbar" value="true"/>';
    html += '<param name="jnlpNumExtensions" value="1"/>';
    html += '<param name="jnlpExtension1" value="/jar/beid.xml"/>';
    html += '<param name="debug" value="false"/>';
    html += '<param name="Reader" value=""/>';
    html += '<param name="OCSP" value="-1"/>';
    html += '<param name="CRL" value="-1"/>';
    html += '</applet>';
    appletPh.innerHTML = html;
    appletCreated = true;
}

function getBEIDApplet() {
    return document.BEIDAppletLauncher.getSubApplet();
}