﻿var theIP = new IpAddress();
theIP.setTextValue('192.168.0.1');

var theMask = new NetMask();
theMask.setTextValue('255.255.255.0');

var testIP = new IpAddress();

function SetIpCalculations() {
    var i, by, bit, netclass;
    var sml = theMask.getMaskLength();
    var invalidMask = '<b>שגיאת חישוב</b>';

    $("#calcIpClassic").html(theIP.getIpText() + '/' + (sml ? theMask.getIpText() : invalidMask));
    $("#calcIpCisco").html(theIP.getIpText() + '/' + (sml ? sml : invalidMask));
    $("#calcIpBinary").html(theIP.getBinaryText());
    $("#calcSubnetBinary").html(sml ? theMask.getBinaryText() : invalidMask);
    
    testIP.setTextValue(theIP.getIpText());
    if ((sml) && (sml < 31)) {
        for (i = sml; i < 32; i++) {
            bit = i % 8;
            by = (i - bit) / 8;
            testIP.bytes[3 - by].bits[7 - bit].setValue(0);
        }
        $("#calcNetwork").html(testIP.getIpText() + '/' + (sml ? sml : invalidMask));
        $("#calcNetClass").html(sml ? testIP.getIpText() : invalidMask);
        
        var binaryIP = theIP.getBinaryText().split(".");
        var binarySubnet;
        binarySubnet = theMask.getBinaryText().split(".");
        for (var i = 0; i < 4; i++) {
            var bIP = binaryIP[i].split('');
            var sIP;
            sIP = binarySubnet[i].split('');
            for (var j = 0; j < 8; j++) {
                $("#binaryIP_Frag" + (i + 1) + "_" + (j + 1)).text(bIP[j]);
                $("#binarySubnet_Frag" + (i + 1) + "_" + (j + 1)).text(sIP[j]);
            }
        }
        
        for (i = sml; i < 32; i++) {
            bit = i % 8;
            by = (i - bit) / 8;
            testIP.bytes[3 - by].bits[7 - bit].setValue(1);
        }
        $("#calcBroadcastAddress").html((sml ? testIP.getIpText() : invalidMask));
    } else {
        for (var i = 0; i < 4; i++) {
            for (var j = 0; j < 8; j++) {
                $("#binaryIP_Frag" + (i + 1) + "_" + (j + 1)).text("-");
                $("#binarySubnet_Frag" + (i + 1) + "_" + (j + 1)).text("-");
            }
        }
    
        $("#calcNetwork").html("");
        $("#calcNetClass").html("");
        $("#calcBroadcastAddress").html("");
    }
    $("#calcNetClass").html(getNetClass(sml));

    if (sml) {
        by = 1;
        for (i = 0; i < (32 - sml); i++) by = by * 2;
        $("#calcNumberOfIP").html(by);
    } else {
        $("#calcNumberOfIP").html("");
    }
}

function getNetClass(x) {
    if (x) {
        if (x > 28) return '';
        if (x == 28) return 'D';
        if (x > 24) return 'subnet of C';
        if (x == 24) return 'C';
        if (x > 16) return 'subnet of B';
        if (x == 16) return 'B';
        if (x > 8) return 'subnet of A';
        if (x == 8) return 'A';
        if (x > 0) return 'unknown';
    } else {
        return '';
    }
}

function showHideInfo() {
    var i = getObj('otherinfo');
    if (i) if (i.style) i.style.display = i.style.display == 'block' ? 'none' : 'block';
    return void (0);
}

$(function() {
    $("input[id^=txtIpCalc]").bind("keyup", function() {
        var calcIP = $("#txtIpCalcIpFragment1").val() + "." + $("#txtIpCalcIpFragment2").val() + "." + $("#txtIpCalcIpFragment3").val() + "." + $("#txtIpCalcIpFragment4").val();
        var calcSubnet = $("#txtIpCalcSubnetFragment1").val() + "." + $("#txtIpCalcSubnetFragment2").val() + "." + $("#txtIpCalcSubnetFragment3").val() + "." + $("#txtIpCalcSubnetFragment4").val();

        theIP.setTextValue(calcIP);
        theMask.setTextValue(calcSubnet);

        SetIpCalculations();
    });

    SetIpCalculations();
});
