/**
 * file: wifi.fixed.js
 *
 * Copyright Miller Electric Mfg. Co. as an unpublished work.
 * All rights reserved.
 *
 * The information contained herein is confidential property of
 * Miller Electric Mfg. Co.  The use of, copying, transfer or
 * disclosure of such information is prohibited except by express
 * written agreement with Miller Electric.
 */


function pageLoad() {
    for (var x = 0; x < WiFiEncryption.encryptionID.length; x++) {
        if (WiFiEncryption.encryptionSelected[x] === 1) {
            $('#encryptionTypes').val(WiFiEncryption.encryptionID[x]);
        }
    }
    for (var y = 0; y < WiFiAuthentication.authenticationID.length; y++) {
        if (WiFiAuthentication.authenticationSelected[y] === 1) {
            $('#authenticationTypes').val(WiFiAuthentication.authenticationID[y]);
        }
    }
    $('#license_key').text("License Key: " + InsightCoreInfo.Value[0]);

    runUpdates();
};

function checkSecurity() {
    $('#authenticationSelection').val($('#authenticationTypes').val());
    $('#encryptionSelection').val($('#encryptionTypes').val());
    $('#user_ssid').val($('#ssid').val());
    $('#user_pw').val($('#wifi_password').val());
    $('#securitysubmit').val(1);
    $('#ipsubmit').val(0);

    var numCheck = $('#spinEdit37').val();

    $("#iptype1").val($("#static_dynamic_b").val());

    $("#ipaddressfirst1").val($('#spinEdit17').val());
    $("#ipaddresssecond1").val($('#spinEdit18').val());
    $("#ipaddressthird1").val($('#spinEdit19').val());
    $("#ipaddressfourth1").val($('#spinEdit20').val());

    $("#subnetmaskfirst1").val($('#spinEdit21').val());
    $("#subnetmasksecond1").val($('#spinEdit22').val());
    $("#subnetmaskthird1").val($('#spinEdit23').val());
    $("#subnetmaskfourth1").val($('#spinEdit24').val());

    $("#gatewayfirst1").val($('#spinEdit25').val());
    $("#gatewaysecond1").val($('#spinEdit26').val());
    $("#gatewaythird1").val($('#spinEdit27').val());
    $("#gatewayfourth1").val($('#spinEdit28').val());

    if (numCheck != 'NaN') {
        $("#dnsfirst1").val($('#spinEdit37').val());
        $("#dnssecond1").val($('#spinEdit38').val());
        $("#dnsthird1").val($('#spinEdit39').val());
        $("#dnsfourth1").val($('#spinEdit40').val());
    }
    else {
        $("#dnsfirst1").val(127);
        $("#dnssecond1").val(0);
        $("#dnsthird1").val(0);
        $("#dnsfourth1").val(1);
    }

    $('#security_change').blur();
    form_submit();
};

function runUpdates() {
    var decimal_location = WIN_IP_wifi_info[1].indexOf(".", 0);
    var next = WIN_IP_wifi_info[1].indexOf(".", (decimal_location + 1));
    var last = WIN_IP_wifi_info[1].indexOf(".", (next + 1));

    var subnet_decimal_location = WIN_IP_wifi_info[2].indexOf(".", 0);
    var subnet_next = WIN_IP_wifi_info[2].indexOf(".", (subnet_decimal_location + 1));
    var subnet_last = WIN_IP_wifi_info[2].indexOf(".", (subnet_next + 1));

    var gateway_decimal_location = WIN_IP_wifi_info[3].indexOf(".", 0);
    var gateway_next = WIN_IP_wifi_info[3].indexOf(".", (gateway_decimal_location + 1));
    var gateway_last = WIN_IP_wifi_info[3].indexOf(".", (gateway_next + 1));

    var dns_decimal_location = WIN_IP_wifi_info[4].indexOf(".", 0);
    var dns_next = WIN_IP_wifi_info[4].indexOf(".", (dns_decimal_location + 1));
    var dns_last = WIN_IP_wifi_info[4].indexOf(".", (dns_next + 1));

    var dhcp_decimal_location = WIN_IP_wifi_info[5].indexOf(".", 0);
    var dhcp_next = WIN_IP_wifi_info[5].indexOf(".", (dhcp_decimal_location + 1));
    var dhcp_last = WIN_IP_wifi_info[5].indexOf(".", (dhcp_next + 1));

    $('#spinEdit17').spinedit();
    $('#spinEdit18').spinedit();
    $('#spinEdit19').spinedit();
    $('#spinEdit20').spinedit();
    $('#spinEdit21').spinedit();
    $('#spinEdit22').spinedit();
    $('#spinEdit23').spinedit();
    $('#spinEdit24').spinedit();
    $('#spinEdit25').spinedit();
    $('#spinEdit26').spinedit();
    $('#spinEdit27').spinedit();
    $('#spinEdit28').spinedit();
    $('#spinEdit29').spinedit();
    $('#spinEdit30').spinedit();
    $('#spinEdit31').spinedit();
    $('#spinEdit32').spinedit();

    $('#spinEdit37').spinedit();
    $('#spinEdit38').spinedit();
    $('#spinEdit39').spinedit();
    $('#spinEdit40').spinedit();

    $('#spinEdit17').spinedit('setValue', WIN_IP_wifi_info[1].slice(0, decimal_location));
    $('#spinEdit17').spinedit('setMinimum', 0);
    $('#spinEdit17').spinedit('setMaximum', 255);
    $('#spinEdit17').spinedit('setStep', 1);

    $('#spinEdit18').spinedit('setValue', WIN_IP_wifi_info[1].slice((decimal_location + 1), next));
    $('#spinEdit18').spinedit('setMinimum', 0);
    $('#spinEdit18').spinedit('setMaximum', 255);
    $('#spinEdit18').spinedit('setStep', 1);

    $('#spinEdit19').spinedit('setValue', WIN_IP_wifi_info[1].slice((next + 1), last));
    $('#spinEdit19').spinedit('setMinimum', 0);
    $('#spinEdit19').spinedit('setMaximum', 255);
    $('#spinEdit19').spinedit('setStep', 1);

    $('#spinEdit20').spinedit('setValue', WIN_IP_wifi_info[1].slice(last + 1));
    $('#spinEdit20').spinedit('setMinimum', 0);
    $('#spinEdit20').spinedit('setMaximum', 255);
    $('#spinEdit20').spinedit('setStep', 1);

    $('#spinEdit21').spinedit('setValue', WIN_IP_wifi_info[2].slice(0, subnet_decimal_location));
    $('#spinEdit21').spinedit('setMinimum', 0);
    $('#spinEdit21').spinedit('setMaximum', 255);
    $('#spinEdit21').spinedit('setStep', 1);

    $('#spinEdit22').spinedit('setValue', WIN_IP_wifi_info[2].slice((subnet_decimal_location + 1), subnet_next));
    $('#spinEdit22').spinedit('setMinimum', 0);
    $('#spinEdit22').spinedit('setMaximum', 255);
    $('#spinEdit22').spinedit('setStep', 1);

    $('#spinEdit23').spinedit('setValue', WIN_IP_wifi_info[2].slice((subnet_next + 1), subnet_last));
    $('#spinEdit23').spinedit('setMinimum', 0);
    $('#spinEdit23').spinedit('setMaximum', 255);
    $('#spinEdit23').spinedit('setStep', 1);

    $('#spinEdit24').spinedit('setValue', WIN_IP_wifi_info[2].slice(subnet_last + 1));
    $('#spinEdit24').spinedit('setMinimum', 0);
    $('#spinEdit24').spinedit('setMaximum', 255);
    $('#spinEdit24').spinedit('setStep', 1);

    $('#spinEdit25').spinedit('setValue', WIN_IP_wifi_info[3].slice(0, gateway_decimal_location));
    $('#spinEdit25').spinedit('setMinimum', 0);
    $('#spinEdit25').spinedit('setMaximum', 255);
    $('#spinEdit25').spinedit('setStep', 1);

    $('#spinEdit26').spinedit('setValue', WIN_IP_wifi_info[3].slice((gateway_decimal_location + 1), gateway_next));
    $('#spinEdit26').spinedit('setMinimum', 0);
    $('#spinEdit26').spinedit('setMaximum', 255);
    $('#spinEdit26').spinedit('setStep', 1);

    $('#spinEdit27').spinedit('setValue', WIN_IP_wifi_info[3].slice((gateway_next + 1), gateway_last));
    $('#spinEdit27').spinedit('setMinimum', 0);
    $('#spinEdit27').spinedit('setMaximum', 255);
    $('#spinEdit27').spinedit('setStep', 1);

    $('#spinEdit28').spinedit('setValue', WIN_IP_wifi_info[3].slice(gateway_last + 1));
    $('#spinEdit28').spinedit('setMinimum', 0);
    $('#spinEdit28').spinedit('setMaximum', 255);
    $('#spinEdit28').spinedit('setStep', 1);

    if (WIN_IP_wifi_info[5] == '') {
        $('#spinEdit29').spinedit('setValue', 0);
        $('#spinEdit29').spinedit('setMinimum', 0);
        $('#spinEdit29').spinedit('setMaximum', 255);
        $('#spinEdit29').spinedit('setStep', 1);

        $('#spinEdit30').spinedit('setValue', 0);
        $('#spinEdit30').spinedit('setMinimum', 0);
        $('#spinEdit30').spinedit('setMaximum', 255);
        $('#spinEdit30').spinedit('setStep', 1);

        $('#spinEdit31').spinedit('setValue', 0);
        $('#spinEdit31').spinedit('setMinimum', 0);
        $('#spinEdit31').spinedit('setMaximum', 255);
        $('#spinEdit31').spinedit('setStep', 1);

        $('#spinEdit32').spinedit('setValue', 0);
        $('#spinEdit32').spinedit('setMinimum', 0);
        $('#spinEdit32').spinedit('setMaximum', 255);
        $('#spinEdit32').spinedit('setStep', 1);
    }
    else {
        $('#spinEdit29').spinedit('setValue', WIN_IP_wifi_info[5].slice(0, dhcp_decimal_location));
        $('#spinEdit29').spinedit('setMinimum', 0);
        $('#spinEdit29').spinedit('setMaximum', 255);
        $('#spinEdit29').spinedit('setStep', 1);

        $('#spinEdit30').spinedit('setValue', WIN_IP_wifi_info[5].slice((dhcp_decimal_location + 1), dhcp_next));
        $('#spinEdit30').spinedit('setMinimum', 0);
        $('#spinEdit30').spinedit('setMaximum', 255);
        $('#spinEdit30').spinedit('setStep', 1);

        $('#spinEdit31').spinedit('setValue', WIN_IP_wifi_info[5].slice((dhcp_next + 1), dhcp_last));
        $('#spinEdit31').spinedit('setMinimum', 0);
        $('#spinEdit31').spinedit('setMaximum', 255);
        $('#spinEdit31').spinedit('setStep', 1);

        $('#spinEdit32').spinedit('setValue', WIN_IP_wifi_info[5].slice(dhcp_last + 1));
        $('#spinEdit32').spinedit('setMinimum', 0);
        $('#spinEdit32').spinedit('setMaximum', 255);
        $('#spinEdit32').spinedit('setStep', 1);
    }

    $('#spinEdit37').spinedit('setValue', WIN_IP_wifi_info[4].slice(0, dns_decimal_location));
    $('#spinEdit37').spinedit('setMinimum', 0);
    $('#spinEdit37').spinedit('setMaximum', 255);
    $('#spinEdit37').spinedit('setStep', 1);

    var numCheck = $('#spinEdit37').val();

    if (numCheck != 'NaN') {
        $('#spinEdit38').spinedit('setValue', WIN_IP_wifi_info[4].slice((dns_decimal_location + 1), dns_next));
        $('#spinEdit39').spinedit('setValue', WIN_IP_wifi_info[4].slice((dns_next + 1), dns_last));
        $('#spinEdit40').spinedit('setValue', WIN_IP_wifi_info[4].slice(dns_last + 1));
    }
    else {
        $("#spinEdit37").val(127);
        $("#spinEdit38").val(0);
        $("#spinEdit39").val(0);
        $("#spinEdit40").val(1);
    }

    $('#spinEdit38').spinedit('setMinimum', 0);
    $('#spinEdit38').spinedit('setMaximum', 255);
    $('#spinEdit38').spinedit('setStep', 1);

    $('#spinEdit39').spinedit('setMinimum', 0);
    $('#spinEdit39').spinedit('setMaximum', 255);
    $('#spinEdit39').spinedit('setStep', 1);

    $('#spinEdit40').spinedit('setMinimum', 0);
    $('#spinEdit40').spinedit('setMaximum', 255);
    $('#spinEdit40').spinedit('setStep', 1);

    if (WIN_IP_wifi_info[0] == 1) {
        $("#dhcp_server1").show();
        $("#dns1").hide();
        $('#static_b').removeClass("btn btn-warning").addClass("btn btn-default");
        $('#dynamic_b').removeClass("btn btn-default").addClass("btn btn-warning");
        $('#static_dynamic_b').val(1);
        $('#specific_server_b').removeClass("btn btn-warning").addClass("btn btn-default");
        $('#dhcp_auto_b').removeClass("btn btn-default").addClass("btn btn-warning");
        $('#dhcp_server_b').val(1);
    }
    else {
        $("#dhcp_server1").hide();
        $("#dns1").show();
        $('#static_b').removeClass("btn btn-default").addClass("btn btn-warning");
        $('#dynamic_b').removeClass("btn btn-warning").addClass("btn btn-default");
        $('#static_dynamic_b').val(0);
        $('#specific_server_b').removeClass("btn btn-default").addClass("btn btn-warning");
        $('#dhcp_auto_b').removeClass("btn btn-warning").addClass("btn btn-default");
        $('#dhcp_server_b').val(0);
    }

    $('#mac_b').text("MAC: " + WIN_IP_wifi_info[6]);

    if ($('#static_dynamic_b').val() == 1) {
        $("INPUT[type=text]").attr("disabled", "disabled");
        $("INPUT[type=text]").css("background-color", "#999999")
    }
};

function checkEthernetChange() {
    var staticDynamicBSide = $('#static_dynamic_b').val();
    var dhcpDNSBSide = $('#dhcp_server_b').val();

    staticDynamicBSide = parseInt(staticDynamicBSide);

    dhcpDNSBSide = parseInt(dhcpDNSBSide);

    $('#spinEdit17').spinedit('setValue', 0);
    $('#spinEdit18').spinedit('setValue', 0);
    $('#spinEdit19').spinedit('setValue', 0);
    $('#spinEdit20').spinedit('setValue', 0);
    $('#spinEdit21').spinedit('setValue', 0);
    $('#spinEdit22').spinedit('setValue', 0);
    $('#spinEdit23').spinedit('setValue', 0);
    $('#spinEdit24').spinedit('setValue', 0);
    $('#spinEdit25').spinedit('setValue', 0);
    $('#spinEdit26').spinedit('setValue', 0);
    $('#spinEdit27').spinedit('setValue', 0);
    $('#spinEdit28').spinedit('setValue', 0);
    $('#spinEdit29').spinedit('setValue', 0);
    $('#spinEdit30').spinedit('setValue', 0);
    $('#spinEdit31').spinedit('setValue', 0);
    $('#spinEdit32').spinedit('setValue', 0);

    $('#spinEdit37').spinedit('setValue', 0);
    $('#spinEdit38').spinedit('setValue', 0);
    $('#spinEdit39').spinedit('setValue', 0);
    $('#spinEdit40').spinedit('setValue', 0);

    if (staticDynamicBSide) {
        $("#dhcp_server1").show();
        $("#dns1").hide();
        $("INPUT[type=text]").attr("disabled", "disabled");
        $("INPUT[type=text]").css("background-color", "#999999")
    }
    else {
        $("#dhcp_server1").hide();
        $("#dns1").show();
        $("INPUT[type=text]").attr("disabled", false);
        $("INPUT[type=text]").css("background-color", "#FFFFFF")
    }

    if (dhcpDNSBSide) {
        $("#dns1").hide();
    }
    else {
        $("#dns1").show();
    }
};
function checkInputs() {
    var numCheck = $('#spinEdit37').val();
    var ipAddress;
    var subNetMask;
    var gateway;
    var dns;
    var staticDynamicBSide = $('#static_dynamic_b').val();
    var e, i;

    staticDynamicBSide = parseInt(staticDynamicBSide);
    $("#iptype1").val($("#static_dynamic_b").val());

    $("#ipaddressfirst1").val($('#spinEdit17').val());
    $("#ipaddresssecond1").val($('#spinEdit18').val());
    $("#ipaddressthird1").val($('#spinEdit19').val());
    $("#ipaddressfourth1").val($('#spinEdit20').val());

    $("#subnetmaskfirst1").val($('#spinEdit21').val());
    $("#subnetmasksecond1").val($('#spinEdit22').val());
    $("#subnetmaskthird1").val($('#spinEdit23').val());
    $("#subnetmaskfourth1").val($('#spinEdit24').val());

    $("#gatewayfirst1").val($('#spinEdit25').val());
    $("#gatewaysecond1").val($('#spinEdit26').val());
    $("#gatewaythird1").val($('#spinEdit27').val());
    $("#gatewayfourth1").val($('#spinEdit28').val());

    if (numCheck != 'NaN') {
        $("#dnsfirst1").val($('#spinEdit37').val());
        $("#dnssecond1").val($('#spinEdit38').val());
        $("#dnsthird1").val($('#spinEdit39').val());
        $("#dnsfourth1").val($('#spinEdit40').val());
    }
    else {
        $("#dnsfirst1").val(127);
        $("#dnssecond1").val(0);
        $("#dnsthird1").val(0);
        $("#dnsfourth1").val(1);
    }
    $('#authenticationSelection').val($('#authenticationTypes').val());
    $('#encryptionSelection').val($('#encryptionTypes').val());
    $('#user_ssid').val($('#ssid').val());
    $('#user_pw').val($('#wifi_password').val());

    for (i = 0; e = document.forms[0].elements[i]; i++) {
        if (e.type == 'hidden') {
            if (e.value === 'NaN') {
                $('#invalidAddressModal').modal();
                return;
            }
        }
    }

    ipAddress = $("#ipaddressfirst1").val() + $("#ipaddresssecond1").val() + $("#ipaddressthird1").val() + $("#ipaddressfourth1").val();
    subNetMask = $("#subnetmaskfirst1").val() + $("#subnetmasksecond1").val() + $("#subnetmaskthird1").val() + $("#subnetmaskfourth1").val();
    gateway = $("#gatewayfirst1").val() + $("#gatewaysecond1").val() + $("#gatewaythird1").val() + $("#gatewayfourth1").val();
    dns = $("#dnsfirst1").val() + $("#dnssecond1").val() + $("#dnsthird1").val() + $("#dnsfourth1").val();

    if (((ipAddress === "0000") || (subNetMask === "0000") || (gateway === "0000") || (dns === "0000")) && !(staticDynamicBSide)) {
        $('#invalidAddressModal').modal();
    }
    else {
        $('#ipAddressChangeModal').modal();
    }
};

function wifiSubmit() {
    $('#ipsubmit').val(1);
    $('#securitysubmit').val(0);
    $('#ip_change').blur();
    form_submit();
};

function form_submit() {
    function getDoc(frame) {
        var doc = null;

        // IE8 cascading access check
        try {
            if (frame.contentWindow) {
                doc = frame.contentWindow.document;
            }
        } catch (err) {
        }

        if (doc) { // successful getting content
            return doc;
        }

        try { // simply checking may throw in ie8 under ssl or mismatched protocol
            doc = frame.contentDocument ? frame.contentDocument : frame.document;
        } catch (err) {
            // last attempt
            doc = frame.document;
        }
        return doc;
    }
    $("#cont_wifi_security").submit(function(e) {

        var formObj = $(this);
        var formURL = formObj.attr("action");

        if (window.FormData !== undefined)  // for HTML5 browsers
        {

            var formData = new FormData(this);
            $.ajax({
                url: formURL,
                type: 'POST',
                data: formData,
                mimeType: "multipart/form-data",
                contentType: false,
                cache: false,
                processData: false,
                success: function(data, textStatus, jqXHR) {
                    refreshInfo();
                },
                error: function(jqXHR, textStatus, errorThrown) {

                }
            });
            e.preventDefault();
            $("#cont_wifi_security").unbind();
        }
        else  //for olden browsers
        {
            //generate a random id
            var iframeId = 'unique' + (new Date().getTime());

            //create an empty iframe
            var iframe = $('<iframe src="javascript:false;" name="' + iframeId + '" />');

            //hide it
            iframe.hide();

            //set form target to iframe
            formObj.attr('target', iframeId);

            //Add iframe to body
            iframe.appendTo('body');
            iframe.load(function(e) {
                var doc = getDoc(iframe[0]);
                var docRoot = doc.body ? doc.body : doc.documentElement;
                var data = docRoot.innerHTML;
                //data is returned from server.
                refreshInfo();
            });
        }
    });
    $("#cont_wifi_security").submit();
};

function refreshInfo() {
    //Generate javascript to get correct sequence information based on program selected
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    var newScript1 = document.createElement('script');
    var newScript2 = document.createElement('script');

    newScript.type = 'text/javascript';
    newScript.async = false;
    // Need to understand how to append multiple variables to address bar....
    newScript.src = "/webaxcess/WiFiEncryption.js";
    headID.appendChild(newScript);

    newScript2.type = 'text/javascript';
    newScript2.async = false;
    // Need to understand how to append multiple variables to address bar....
    newScript2.src = "/webaxcess/WiFiAuthentication.js";
    headID.appendChild(newScript2);

    newScript1.type = 'text/javascript';
    newScript1.async = false;
    newScript1.onload = pageLoad;
    // Need to understand how to append multiple variables to address bar....
    newScript1.src = "/webaxcess/WIN_IP_wifi_info.js";
    headID.appendChild(newScript1);
}; 