<!--
// JavaScript (C) Holger Steinacker www.datahelp.de 
// Alle Rechte vorbehalten.
function berechnung (e) {
    if (e.name == "cidr") {
        var cidr = parseInt(e.value);
        cidr = Math.max(0, Math.min(32, cidr));
        if (! isNaN(cidr))
            document.f1.cidr.value = cidr;
		var netzmask = cidr_to_netzmask(cidr);
        document.f1.netzmask.value = netzmask;
        document.getElementById('invers').firstChild.data =
			addr_to_string(~ string_to_addr(netzmask));
        aktualisieren(cidr);
        set_hosts(cidr);
    } else if (e.name == "netzmask") {
        var netzmask = e.value;
        var num = string_to_addr(netzmask);
		var invers_nm = addr_to_string(~ num);
        var zerobits = 0;
        while ((num & 1) == 0) {
            zerobits++;
            num >>>= 1;
        }
        var cidr = 32-zerobits;
        document.f1.cidr.value = cidr;
        aktualisieren(cidr);
        set_hosts(cidr);
        var netzmask_ok = cidr_to_netzmask(cidr);
		document.getElementById('netzmask_ok').firstChild.data =
          (netzmask_ok == netzmask)
            ? 'OK'
            : '(Vorschlag: '+ netzmask_ok +')';
		document.getElementById('invers').firstChild.data = invers_nm;           
    } else if (e.name == "ip") {
        aktualisieren(document.f1.cidr.value);
    } else if (e.name == "hosts") {
        var hosts = Math.max(0,parseInt(e.value));
        if (hosts > 0)
            hosts++;
        var bits = 0;
        while ((hosts >>> bits) > 0) {
            bits++;
        }
        var cidr = 32 - bits;
        document.f1.cidr.value = cidr;
        document.f1.netzmask.value = cidr_to_netzmask(cidr);
        document.getElementById('netzmask_ok').firstChild.data = '.';
        var hosts_ok = Math.max(0, (2 << (bits-1))-2);
        document.getElementById('hosts_ok').firstChild.data = 
          (hosts_ok == e.value)
            ? ' '
            : '(Max: ' + hosts_ok + ')';
        aktualisieren(cidr);     
    } else {
        alert("??? e.name="+e.name);
    }
    return false;
}
function addr_to_string (addr) {
    var byte1 = (addr >>> 24);
    var byte2 = (addr >>> 16) & 255;
    var byte3 = (addr >>>  8) & 255;
    var byte4 = addr & 255;
    return(byte1 + '.' + byte2 + '.' + byte3 + '.' + byte4);
}
function string_to_addr (s) {
    var masks = s.split('.', 4);
    var num = ((masks[0] & 255) << 24)
            + ((masks[1] & 255) << 16)
            + ((masks[2] & 255) <<  8)
            +  (masks[3] & 255);
    return num;
}
function cidr_to_netzmask (cidr, num_flag) {
    cidr = Math.max(0, Math.min(32, parseInt(cidr)));
    var count = cidr;
    var bits = '';
    while (count--) bits += '1';
    count = 32 - cidr;
    while (count--) bits += '0';
    var netzmaske = parseInt(bits,2);
    if (num_flag)
        return netzmaske;
    else 
        return addr_to_string(netzmaske);
}
function aktualisieren (cidr) {
    cidr = Math.max(0, Math.min(32, parseInt(cidr)));
    var netzmaske = cidr_to_netzmask(cidr, 1);
    var ip = string_to_addr(document.f1.ip.value);
    var netzaddr = ip & netzmaske;
    var broadcast = netzaddr | (~ netzmaske);
	var ip_von = cidr == 31 ? '-' : cidr == 32 ? netzaddr : netzaddr + 1;
	var ip_bis = cidr == 31 ? '-' : cidr == 32 ? netzaddr : broadcast - 1;
    document.getElementById('netz').firstChild.data      = addr_to_string(netzaddr);
    document.getElementById('broadcast').firstChild.data = addr_to_string(broadcast);
    document.getElementById('erste_ip').firstChild.data  = isNaN(ip_von) ? ip_von : addr_to_string(ip_von);
    document.getElementById('letzte_ip').firstChild.data = isNaN(ip_bis) ? ip_bis : addr_to_string(ip_bis);
}
function set_hosts(cidr) {
    var count = 32 - cidr;
    var hosts = Math.pow(2, count) - 2;
    hosts = Math.max(0, hosts);
    document.f1.hosts.value = hosts;
    document.getElementById('hosts_ok').firstChild.data = ' ';
}
function common_net () {
    var ip1 = string_to_addr(document.f2.ip1.value);
    var ip2 = string_to_addr(document.f2.ip2.value);
    var ip_common = ip1 ^ ip2;
    var bit = 31;
    while ((ip_common & (2<<bit)) == 0 && bit > 0) {
        bit--;
    }
    var cidr = 32-bit-2;
    var netzmaske = cidr_to_netzmask(cidr, 1);
    var ip = Math.min(ip1, ip2);
    var netzaddr = ip & netzmaske;
    var broadcast = netzaddr | (~ netzmaske);
    document.getElementById('c_net').firstChild.data        = addr_to_string(netzaddr);
    document.getElementById('c_netzmask').firstChild.data    = addr_to_string(netzmaske);
    document.getElementById('c_cidr').firstChild.data       = cidr;
    document.getElementById('c_broadcast').firstChild.data  = addr_to_string(broadcast);
}
function debug (t) {
    document.getElementById("debug").innerHTML += t + '<br>';
}
// -->