var $controls = {"ZW3D_MAJOR": "radio", "ZW3D_MINOR": "checkbox"};
var $onclicks = {"ZW3D_MAJOR": "onCheckMajorLicense", "ZW3D_MINOR": "onCheckMinorLicense"};
var $features = {"ZW3D_STD": true, "ZW3D_PRO":true, "ZW3D_PRE": true, "ZW3D_PRE+ZW3D_AXIS": true};
var $l_months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];
var $u_months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var $o = ['floating', 'floating', 'nodelock', 'nodelock'];
var $s = ['nodelock', 'nodelock', 'floating', 'floating'];

var IDOK = 1;
var IDCANCEL = 2;

var FLXNET_APP = 0;
var ROCKEY_APP = 1;
var FLXNET_SVR = 2;
var ROCKEY_SVR = 3;

var FLXNET_NODELOCK = 0;
var ROCKEY_NODELOCK = 1;
var FLXNET_FLOATING = 2;
var ROCKEY_FLOATING = 3;

var UNKNOWN = 0;
var	TRIAL = 1;
var	SHORTCODE = 2;
var	EMERGENCY = 3;
var	SERVED_ACTIVATION = 4;
var	SERVED_OVERDRAFT_ACTIVATION = 5;
var	PUBLISHER_ACTIVATION = 6;
var	PUBLISHER_OVERDRAFT_ACTIVATION = 7;

var $errorBox;
var $alertBox;
var $enterBox;
var $returnBox;
var $repairBox;
var $optionBox;

var $gLicenses = [];
$gLicenses[FLXNET_NODELOCK] = [];
$gLicenses[ROCKEY_NODELOCK] = [];
$gLicenses[FLXNET_FLOATING] = [];
$gLicenses[ROCKEY_FLOATING] = [];

var $gIndex = {"LicType": -1, "Index": -1}

var $gCurrentMajor = null;
var $gCurrentMinors = [];
var $gOriginalMajor = null;
var $gOriginalMinors = [];
var $gAppliedMajor = null;
var $gAppliedMinors = [];

var $gRemotes = [];

Array.prototype.del = function(n)
{
    if (n < 0)
        return this;
    return this.slice(0, n).concat(this.slice(n + 1, this.length));
};
Array.prototype.toString = function()
{
    var s = "";
    for (var i = 0; i < this.length; i++)
    {
        s += this[i];
        if (i != this.length - 1)
            s += "+";
    }
    return s;
};
Object.prototype.toString = function()
{
    var s = "";
    for (k in this)
    {
        s += this[k] + ",";
    }
    return s;
};
function ZwLicense()
{
    this.LicTech = FLXNET_APP;
    this.LicType = FLXNET_NODELOCK;
    this.FulType = UNKNOWN;
    this.FulfillmentId = "";
    this.EntitlementId = "";
    this.Version = "";
    this.VendorString = "";
    this.NoticeString = "";
    this.EnableDate = "";
    this.ExpireDate = "permanent";
    this.ServerName = "localhost";
    this.ServerPort = "";
    this.Count = 1;
    this.Status = 0;
    this.Trusts = 7;
    this.Features = [];
}
function strToDate(d)
{
    try {
        x = $.datepicker.parseDate('d-M-yy', d , {monthNamesShort:$l_months});
        return x;
    } catch (e) {
        try {
            x = $.datepicker.parseDate('d-M-yy', d , {monthNamesShort:$u_months});
            return x;
        } catch (e) {
            return new Date();
        }
    }
}
function equalLicense(l1, l2)
{
    try {
        if (l1 == null && l2 == null) {
            return true;
        }
        if (l1.LicTech != l2.LicTech || l1.LicType != l2.LicType) {
            return false;
        }
        if (l1.LicType == FLXNET_NODELOCK || l1.LicType == ROCKEY_NODELOCK) {
            return (l1.FulfillmentId == l2.FulfillmentId);
        } else {
            return l1.ServerName == l2.ServerName &&
                   l1.ServerPort == l2.ServerPort &&
                   l1.Features.toString() == l2.Features.toString();
        }
    } catch (e) {
        return false;
    }
}
function equalLicenseArray(a1, a2)
{
    if (a1.length != a2.length) {
        return false;
    }
    for (var i = 0; i < a1.length; i++) {
        var find = false;
        for (var j = 0; j < a2.length; j++) {
            if (licenseEqual(a1[i], a2[j])) {
                find = true;
                break;
            }
        }
        if (!find) {
            return false;
        }
    }
    return true;
}
function couldSelect(l)
{
    if (l.LicType == FLXNET_FLOATING || l.LicType == ROCKEY_FLOATING)
        return true;
    if (l.Trusts != 7 && l.Trusts != 5)
        return false;
    return l.Status == 0;
}
function couldReturnNodelock(l)
{
    return (l.FulType == PUBLISHER_ACTIVATION);
}
function couldReturnBorrowed(l)
{
    if (l.FulType != SERVED_ACTIVATION) {
        return false;
    }
    if (l.Trusts != 7) {
        return false;
    }
    var today = new Date();
    var expire = strToDate(l.ExpireDate);
    return expire > today;
}
function couldRemove(l)
{
    if (l.LicType == FLXNET_FLOATING || l.LicType == ROCKEY_FLOATING)
        return true;
    if (l.Trusts != 7 && l.Trusts != 5)
        return true;
    if (l.ExpireDate == "permanent")
        return (l.Status == 1);
    var today = new Date();
    var expire = strToDate(l.ExpireDate);
    return (expire < today) ? true : (l.Status == 1);
}
function couldRepair(l)
{
    return (l.FulType == PUBLISHER_ACTIVATION) && (l.Trusts == 5);
}
function beforeEnableDate(l)
{
    if (l.EnableDate == "")
        return false;
    var today = new Date();
    var myday = strToDate(l.EnableDate);
    return today < myday;
}
function passedExpireDate(l)
{
    if (l.ExpireDate == "permanent" || l.ExpireDate == "")
        return false;
    var today = new Date();
    var myday = strToDate(l.ExpireDate);
    return today > myday;
}
function statusToString(l)
{
    if (l.Trusts != 7 && l.Trusts != 5) 
        return $statusStrings[3];
    if (l.ExpireDate == "permanent")
        return $statusStrings[l.Status];
    var today = new Date();
    var expire = strToDate(l.ExpireDate);
    if (expire < today) {
        return $statusStrings[2];
    } else {
        return $statusStrings[l.Status];
    }
}
function enableDateToString(l)
{
    try {
        var x = $.datepicker.parseDate('d-M-yy', l.EnableDate , {monthNamesShort:$l_months});
        return $.datepicker.formatDate('yy-mm-dd', x);
    } catch (e) {
        try {
            var x = $.datepicker.parseDate('d-M-yy', l.EnableDate , {monthNamesShort:$u_months});
            return $.datepicker.formatDate('yy-mm-dd', x);
        } catch (e) {
            return l.EnableDate ;
        }
    }
}
function expireDateToString(l)
{
    if (l.ExpireDate == "permanent")
        return $uiStrings.UI_PERMANENT;
    try {
        var x = $.datepicker.parseDate('d-M-yy', l.ExpireDate , {monthNamesShort:$l_months});
        return $.datepicker.formatDate('yy-mm-dd', x);
    } catch (e) {
        try {
            var x = $.datepicker.parseDate('d-M-yy', l.ExpireDate , {monthNamesShort:$u_months});
            return $.datepicker.formatDate('yy-mm-dd', x);
        } catch (e) {
            return l.ExpireDate ;
        }
    }
}
function checkMajorLicense(type, index, check, quiet)
{
    try {
        var l = $gLicenses[type][index];
        if (l.VendorString != "ZW3D_MAJOR") {
            return false;
        }
        var c = $('#'+$s[type]+'_license_list input[ltype='+type+'][value='+index+']');
        if (c.length > 0) {
            if (!couldSelect(l)) {
                if (!quiet) {
                    showError($errStrings.ERR_CHECK_1);
                }
                c.attr('checked', false);
                return false;
            }
            if (beforeEnableDate(l)) {
                if (!quiet) {
                    showError($errStrings.ERR_CHECK_2);
                }
                c.attr('checked', false);
                return false;
            }
            if (passedExpireDate(l)) {
                if (!quiet) {
                    showError($errStrings.ERR_CHECK_3);
                }
                c.attr('checked', false);
                return false;
            }
            $('#nodelock_license_list input:radio').attr('checked', false);
            $('#floating_license_list input:radio').attr('checked', false);
            c.attr('checked', check);
            $('#'+$o[type]+'_license_list input:checkbox').attr('checked', false).attr('disabled', true);
            var s_minors = $('#'+$s[type]+'_license_list input:checkbox');
            if (s_minors.filter(':enabled').length == 0)
                s_minors.attr('disabled', false);
            s_minors.each(function() {
                var t = $gLicenses[$(this).attr('ltype')][$(this).val()];
                if ((t.LicType == FLXNET_FLOATING || t.LicType == ROCKEY_FLOATING) && 
                    (t.ServerName != l.ServerName || t.ServerPort != l.ServerPort)) {
                    $(this).attr('checked', false).attr('disabled', true);
                } else if (l.Features.toString().indexOf("ZW3D_PRE") == -1 && t.Features.toString().indexOf("ZW3D_AXIS") != -1) {
                    $(this).attr('checked', false).attr('disabled', true);
                } else {
                    $(this).attr('disabled', false);
                }
            });
            return true;
        }
    } catch (e) {
    }
    return false;
}
function checkMinorLicense(type, index, check, quiet)
{
    try {
        var l = $gLicenses[type][index];
        if (l.VendorString != "ZW3D_MINOR") {
            return false;
        }
        var c = $('#'+$s[type]+'_license_list input[ltype='+type+'][value='+index+']');
        if (c.length > 0) {
            if (!couldSelect(l)) {
                if (!quiet) {
                    showError($errStrings.ERR_CHECK_1);
                }
                c.attr('checked', false);
                return false;
            }
            if (beforeEnableDate(l)) {
                if (!quiet) {
                    showError($errStrings.ERR_CHECK_2);
                }
                c.attr('checked', false);
                return false;
            }
            if (passedExpireDate(l)) {
                if (!quiet) {
                    showError($errStrings.ERR_CHECK_3);
                }
                c.attr('checked', false);
                return false;
            }
            if (!c.attr('disabled')) {
                c.attr('checked', check);
                var s_minors = $('#'+$s[type]+'_license_list input:checkbox');
                s_minors.not(c).each(function() {
                    var t = $gLicenses[$(this).attr('ltype')][$(this).val()];
                    if ((t.LicType == FLXNET_FLOATING || t.LicType == ROCKEY_FLOATING) && 
                        (t.ServerName != $gCurrentMajor.ServerName || t.ServerPort != $gCurrentMajor.ServerPort)) {
                        $(this).attr('disabled', true);
                    } else if (t.Features.toString() == l.Features.toString()) {
                        if (check) {
                            $(this).attr('disabled', true);
                        } else {
                            $(this).attr('disabled', false);
                        }
                    }
                });
                return true;
            }
        }
    } catch (e) {
    }
    return false;
}
function checkLastLicense(type)
{
    var size = $gLicenses[type].length;
    if (size > 0) {
        var l = $gLicenses[type][size-1];
        var f = (l.VendorString == "ZW3D_MAJOR") ? checkMajorLicense : checkMinorLicense;
        if (f(type, size-1, true, false)) {
            pickActiveLicense();
            saveActiveLicense();
            $('#btn_license_table_apply').button('disable');
        }
    }
}
function onCheckMajorLicense(obj)
{
    if (checkMajorLicense($(obj).attr('ltype'), $(obj).val(), true, false)) {
        $('#btn_license_table_apply').button('enable');
        pickActiveLicense();
    }
}
function onCheckMinorLicense(obj)
{
    if (checkMinorLicense($(obj).attr('ltype'), $(obj).val(), $(obj).attr('checked'), false)) {
        $('#btn_license_table_apply').button('enable');
        pickActiveLicense();
    }
}
function showError(msg, callback)
{
    $('#errorMsg').html(msg);
    var callback = callback || function () {};
    var buttons = {};
    buttons[$uiStrings.UI_OK] = function() {$(this).dialog('close'); callback();};
    $errorBox.dialog('option', 'buttons', buttons);
    $errorBox.dialog('open');
}
function showAlert(msg, callback)
{
    $('#alertMsg').html(msg);
    var callback = callback || function () {};
    var buttons = {};
    buttons[$uiStrings.UI_OK] = function() {$(this).dialog('close'); callback();};
    $alertBox.dialog('option', 'buttons', buttons);
    $alertBox.dialog('open');
}
function showEnter(msg, callback)
{
    $('#enterMsg').html(msg);
    var callback = callback || function () {};
    var buttons = {};
    buttons[$uiStrings.UI_CANCEL] = function() {$(this).dialog('close');}
    buttons[$uiStrings.UI_OK] = function() {$(this).dialog('close'); callback();};
    $enterBox.dialog('option', 'buttons', buttons);
    $enterBox.dialog('open');
}
function returnNodelockLicense(type, index)
{
    $gIndex.LicType = type;
    $gIndex.Index = index;
    $('#returnBox').dialog('open');
}
function repairNodelockLicense(type, index)
{
    $gIndex.LicType = type;
    $gIndex.Index = index;
    $('#repairBox').dialog('open');
}
function removeNodelockLicense(type, index)
{
    showEnter($errStrings.ERR_REMOVE_1, function(){
        var license = $gLicenses[type][index];
        var ret = window.external.Remove(license.toString());
        if (ret != 0) {
            showError(window.external.Resource(ret));
        }
        loadNodelockLicenses();
        showNodelockLicenses();
        showActiveLicense();
    });
}
function obtainFloatingLicense(type, index)
{
    var l = $gLicenses[type][index];
    var ret = window.external.Connect(l.toString());
    if (ret == 0) {
        showAlert($errStrings.ERR_FLOATING_9);
    } else {
        showError(window.external.Resource(ret));
    }
}
function removeFloatingLicense(type, index)
{
    showEnter($errStrings.ERR_REMOVE_1, function(){
        var license = $gLicenses[type][index];
        window.external.RemoveFloatingLicense(license.toString());
        $gLicenses[type] = $gLicenses[type].del(index);
        showFloatingLicenses();
        showActiveLicense();
        saveActiveLicense();
    });
}
function returnBorrowedLicense(type, index)
{
    $gIndex.LicType = type;
    $gIndex.Index = index;
    var license = $gLicenses[type][index];
    $('#server_fulfill_return').html(license.FulfillmentId);
    $('#server_feature_return').html(license.Features.toString());
    $('#server_name_return').val(license.ServerName);
    $('#server_port_return').val(license.ServerPort);
    $('#license_table').hide();
    $('#borrowed_flxnet_return').fadeIn();
}
function borrowFloatingLicense(type, index)
{
    $gIndex.LicType = type;
    $gIndex.Index = index;
    var license = $gLicenses[type][index];
    var server_name = $('#server_name_borrow').html(license.ServerName);
    var server_port = $('#server_port_borrow').html(license.ServerPort);
    var server_feat = $('#server_feat_borrow').html(license.Features.toString());
    $('#license_table').hide();
    $('#floating_flxnet_borrow').fadeIn();
}
function pickActiveLicense()
{
    $gCurrentMajor = null;
    $gCurrentMinors = [];
    var n = $('#nodelock_license_list input[type=radio]:checked');
    if (n.length > 0) {
        $gCurrentMajor = $gLicenses[n.attr('ltype')][n.val()];
        $('#nodelock_license_list input[type=checkbox]:checked').each(function() {
            $gCurrentMinors.push($gLicenses[$(this).attr('ltype')][$(this).val()]);
        });
    } else {
        var f = $('#floating_license_list input[type=radio]:checked');
        if (f.length > 0) {
            $gCurrentMajor = $gLicenses[f.attr('ltype')][f.val()];
            $('#floating_license_list input[type=checkbox]:checked').each(function() {
                $gCurrentMinors.push($gLicenses[$(this).attr('ltype')][$(this).val()]);
            });
        }
    }
}
function markActiveLicense()
{
    $('.active').removeClass('active');
    var n = $('#nodelock_license_list input[type=radio]:checked');
    if (n.length > 0) {
        n.parent().parent().addClass('active');
        $('#nodelock_license_list input[type=checkbox]:checked').each(function() {
            $(this).parent().parent().addClass('active');
        });
    } else {
        var f = $('#floating_license_list input[type=radio]:checked');
        if (f.length > 0) {
            f.parent().parent().addClass('active');
            $('#floating_license_list input[type=checkbox]:checked').each(function() {
                $(this).parent().parent().addClass('active');
            });
        }
    }
}
function loadActiveLicense()
{
    try {
        $gCurrentMajor = jQuery.parseJSON(window.external.GetActiveMajorLicense());
        $gCurrentMinors = jQuery.parseJSON(window.external.GetActiveMinorLicenses());
        $gOriginalMajor = $gCurrentMajor;
        $gOriginalMinors = $gCurrentMinors;
    } catch (e) {
        $gCurrentMajor = null;
        $gCurrentMinors = [];
    }
}
function showActiveLicense()
{
    if ($gCurrentMajor != null) {
        var majorIndex = null;
        for (var i = 0; i < $gLicenses[$gCurrentMajor.LicType].length; i++) {
            if (equalLicense($gCurrentMajor, $gLicenses[$gCurrentMajor.LicType][i])) {
                majorIndex = {"Type": $gCurrentMajor.LicType, "Index": i};
                break;
            }
        }
        if (majorIndex != null) {
            if (checkMajorLicense(majorIndex.Type, majorIndex.Index, true, true)) {
                var minorIndices = [];
                for (var i = 0; i < $gCurrentMinors.length; i++) {
                    for (var j = 0; j < $gLicenses[$gCurrentMinors[i].LicType].length; j++) {
                        if (equalLicense($gCurrentMinors[i], $gLicenses[$gCurrentMinors[i].LicType][j])) {
                            minorIndices.push({"Type": $gCurrentMinors[i].LicType, "Index": j});
                            break;
                        }
                    }
                }
                for (var i = 0; i < minorIndices.length; i++) {
                    checkMinorLicense(minorIndices[i].Type, minorIndices[i].Index, true, true);
                }
            } else {
                var major = $('#'+$s[$gCurrentMajor.LicType]+'_license_list input:radio').first();
                if (major.length > 0) {
                    checkMajorLicense(major.attr('ltype'), major.val(), true, true);
                } else {
                    $('#nodelock_license_list input:checkbox').attr('checked', false).attr('disabled', true);
                    $('#floating_license_list input:checkbox').attr('checked', false).attr('disabled', true);
                }
            }
        } else {
            var major = $('#'+$s[$gCurrentMajor.LicType]+'_license_list input:radio').first();
            if (major.length > 0) {
                checkMajorLicense(major.attr('ltype'), major.val(), true, true);
            } else {
                $('#nodelock_license_list input:checkbox').attr('checked', false).attr('disabled', true);
                $('#floating_license_list input:checkbox').attr('checked', false).attr('disabled', true);
            }
        }
    } else {
        try {
            var n_majors = $('#nodelock_license_list input:radio');
            var f_majors = $('#floating_license_list input:radio');
            if (n_majors.length > 0) {
                checkMajorLicense(n_majors.first().attr('ltype'), n_majors.first().val(), true, true);
            } else if (f_majors.length > 0) {
                checkMajorLicense(f_majors.first().attr('ltype'), f_majors.first().val(), true, true);
            } else {
                $('#nodelock_license_list input:checkbox').attr('checked', false).attr('disabled', true);
                $('#floating_license_list input:checkbox').attr('checked', false).attr('disabled', true);
            }
        } catch (e) {
        }
    }
    pickActiveLicense();
    saveActiveLicense();
}
function saveActiveLicense()
{
    try {
        $gAppliedMajor = $gCurrentMajor;
        $gAppliedMinors = $gCurrentMinors;
        window.external.DelActiveMajorLicense();
        window.external.ClearActiveMinorLicenses();
        if ($gCurrentMajor != null) {
            window.external.SetActiveMajorLicense($gCurrentMajor.toString());
            for (var i = 0; i < $gCurrentMinors.length; i++) {
                window.external.AppendActiveMinorLicense($gCurrentMinors[i].toString());
            }
            markActiveLicense();
        }
        if (!window.external.Apply()) {
            showError($errStrings.ERR_CHECK_4);
        }
    } catch (e) {
    }
}
function loadNodelockLicenses()
{
    try {
        $gLicenses[FLXNET_NODELOCK] = jQuery.parseJSON(window.external.GetFlxnetNodelockLicenses());
        $gLicenses[ROCKEY_NODELOCK] = jQuery.parseJSON(window.external.GetRockeyNodelockLicenses());
    } catch (e) {
        $gLicenses[FLXNET_NODELOCK] = [];
        $gLicenses[ROCKEY_NODELOCK] = [];
    }
}
function showNodelockLicenses()
{
    var total_length = $gLicenses[FLXNET_NODELOCK].length + $gLicenses[ROCKEY_NODELOCK].length;
    var style = $app_styles[total_length > 4 ? 1 : 0];
    var nodelock = '';
    nodelock += '<div class="ui-table-head">';
    nodelock += '<table celspacing="0" class="ui-table">';
    nodelock += '<thead><tr>';
    nodelock += '<th style="'+style[0]+'"></th>';
    nodelock += '<th style="'+style[1]+'">'+$uiStrings.UI_OPERATIONS+'</th>';
    nodelock += '<th style="'+style[2]+'">'+$uiStrings.UI_FEATURES+'</th>';
    nodelock += '<th style="'+style[3]+'">'+$uiStrings.UI_EXPIREDATE+'</th>';
    nodelock += '<th style="'+style[4]+'">'+$uiStrings.UI_STATUS+'</th>';
    nodelock += '<th style="'+style[5]+'">'+$uiStrings.UI_LICTECH+'</th>';
    nodelock += '</tr></thead></table></div>';
    nodelock += '<div class="ui-table-body">';
    nodelock += '<table celspacing="0" class="ui-table">';
    nodelock += '<tbody id="nodelock_license_list">';
    try {
        for (var i = 0; i < $gLicenses[FLXNET_NODELOCK].length; i++) {
            var license = $gLicenses[FLXNET_NODELOCK][i];
            nodelock += '<tr class="normal">';
            nodelock += '<td style="'+style[0]+'"><input type="'+$controls[license.VendorString]+'" onclick="'+$onclicks[license.VendorString]+'(this)" ltype="'+license.LicType+'" value="'+i+'"/></td>';
            nodelock += '<td style="'+style[1]+'">';
            if (couldReturnNodelock(license)) {
                nodelock += '<a href="#" onclick="returnNodelockLicense('+license.LicType+','+i+')">'+$uiStrings.UI_RETURN+'</a>';
            }
            if (couldReturnBorrowed(license)) {
                nodelock += '<a href="#" onclick="returnBorrowedLicense('+license.LicType+','+i+')">'+$uiStrings.UI_BORROW_RETURN+'</a>';
            }
            if (couldRepair(license)) {
                nodelock += '<a href="#" onclick="repairNodelockLicense('+license.LicType+','+i+')">'+$uiStrings.UI_REPAIR+'</a>';
            }
            if (couldRemove(license)) {
                nodelock += '<a href="#" onclick="removeNodelockLicense('+license.LicType+','+i+')">'+$uiStrings.UI_REMOVE+'</a>';
            }
            nodelock += '</td>';
            nodelock += '<td style="'+style[2]+'"><a href="#" onclick="showNodelockLicenseSlide('+license.LicType+','+i+')">'+license.Features+'/'+license.Version+'</a></td>';
            nodelock += '<td style="'+style[3]+'">'+expireDateToString(license)+'</td>';
            nodelock += '<td style="'+style[4]+'">'+statusToString(license)+'</td>';
            nodelock += '<td style="'+style[5]+'">'+$techStrings[license.LicTech]+'</td>';
            nodelock += '</tr>';
        }
        for (var i = 0; i < $gLicenses[ROCKEY_NODELOCK].length; i++) {
            var license = $gLicenses[ROCKEY_NODELOCK][i];
            nodelock += '<tr class="normal">';
            nodelock += '<td style="'+style[0]+'"><input type="'+$controls[license.VendorString]+'" onclick="'+$onclicks[license.VendorString]+'(this)" ltype="'+license.LicType+'" value="'+i+'"/></td>';
            nodelock += '<td style="'+style[1]+'"></td>';;
            nodelock += '<td style="'+style[2]+'"><a href="#" onclick="showNodelockLicenseSlide('+license.LicType+','+i+')">'+license.Features+'/'+license.Version+'</a></td>';
            nodelock += '<td style="'+style[3]+'">'+expireDateToString(license)+'</td>';
            nodelock += '<td style="'+style[4]+'">'+statusToString(license)+'</td>';
            nodelock += '<td style="'+style[5]+'">'+$techStrings[license.LicTech]+'</td>';
            nodelock += '</tr>';
        }
    } catch (e) {
    }
    nodelock += '</tbody></table></div>';
    $('#nodelock_license_table').html(nodelock);
}
function showNodelockLicenseSlide(type, index)
{
    var license = $gLicenses[type][index];
    var xhtml = '<div class="license">';
    xhtml += '<table><tbody>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_LICTECH+$colon+'</td><td class="license-value">'+$techStrings[license.LicTech]+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_LICTYPE+$colon+'</td><td class="license-value">'+$typeStrings[license.LicType]+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_FULFILLMENTID+$colon+'</td><td class="license-value">'+license.FulfillmentId+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_ENTITLEMENTID+$colon+'</td><td class="license-value">'+license.EntitlementId.substr(0, license.EntitlementId.length-4)+'****</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_FEATURES+$colon+'</td><td class="license-value">'+license.Features+'/'+license.Version+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_NOTICESTRING+$colon+'</td><td class="license-value">'+license.NoticeString+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_ENABLEDATE+$colon+'</td><td class="license-value">'+enableDateToString(license)+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_EXPIREDATE+$colon+'</td><td class="license-value">'+expireDateToString(license)+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_COUNT+$colon+'</td><td class="license-value">'+license.Count+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_STATUS+$colon+'</td><td class="license-value">'+statusToString(license)+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_TRUSTS+$colon+'</td><td class="license-value">'+$trustsStrings[license.Trusts]+'</td></tr>';
    xhtml += '</tbody></table>';
    xhtml += '</div>';
    $('#license_board').html(xhtml);
    $('#license_table').hide();
    $('#license_slide').fadeIn();
}
function loadFloatingLicenses()
{
    try {
        $gLicenses[FLXNET_FLOATING] = jQuery.parseJSON(window.external.GetFlxnetFloatingLicenses());
        $gLicenses[ROCKEY_FLOATING] = jQuery.parseJSON(window.external.GetRockeyFloatingLicenses());
    } catch (e) {
        $gLicenses[FLXNET_FLOATING] = [];
        $gLicenses[ROCKEY_FLOATING] = [];
    }
}
function showFloatingLicenses()
{
    var total_length = $gLicenses[FLXNET_FLOATING].length + $gLicenses[ROCKEY_FLOATING].length;
    var style = $app_styles[total_length > 4 ? 3 : 2];
    var floating = '';
    floating += '<div class="ui-table-head">';
    floating += '<table celspacing="0" class="ui-table">';
    floating += '<thead><tr>';
    floating += '<th style="'+style[0]+'"></th>';
    floating += '<th style="'+style[1]+'">'+$uiStrings.UI_OPERATIONS+'</th>';
    floating += '<th style="'+style[2]+'">'+$uiStrings.UI_FEATURES+'</th>';
    floating += '<th style="'+style[3]+'">'+$uiStrings.UI_SERVERNAME+'</th>';
    floating += '<th style="'+style[4]+'">'+$uiStrings.UI_SERVERPORT+'</th>';
    floating += '<th style="'+style[5]+'">'+$uiStrings.UI_LICTECH+'</th>';
    floating += '</tr></thead></table></div>';
    floating += '<div class="ui-table-body">';
    floating += '<table celspacing="0" class="ui-table">';
    floating += '<tbody id="floating_license_list">';
    try {
        for (var i = 0; i < $gLicenses[FLXNET_FLOATING].length; i++) {
            var license = $gLicenses[FLXNET_FLOATING][i];
            floating += '<tr class="normal">';
            floating += '<td style="'+style[0]+'"><input type="'+$controls[license.VendorString]+'" onclick="'+$onclicks[license.VendorString]+'(this)" ltype="'+license.LicType+'" value="'+i+'"/></td>';
            floating += '<td style="'+style[1]+'">';
            floating += '<a href="#" onclick="borrowFloatingLicense('+license.LicType+','+i+')">'+$uiStrings.UI_BORROW_BORROW+'</a>';
            floating += '<a href="#" onclick="removeFloatingLicense('+license.LicType+','+i+')">'+$uiStrings.UI_REMOVE+'</a>';
            floating += '</td>';
            floating += '<td style="'+style[2]+'"><a href="#" onclick="showFloatingLicenseSlide('+license.LicType+','+i+')">'+license.Features+'/'+license.Version+'</a></td>';
            floating += '<td style="'+style[3]+'" title="'+license.ServerName+'">'+license.ServerName+'</td>';
            floating += '<td style="'+style[4]+'">'+license.ServerPort+'</td>';
            floating += '<td style="'+style[5]+'">'+$techStrings[license.LicTech]+'</td>';
            floating += '</tr>';
        }
        for (var i = 0; i < $gLicenses[ROCKEY_FLOATING].length; i++) {
            var license = $gLicenses[ROCKEY_FLOATING][i];
            floating += '<tr class="normal">';
            floating += '<td style="'+style[0]+'"><input type="'+$controls[license.VendorString]+'" onclick="'+$onclicks[license.VendorString]+'(this)" ltype="'+license.LicType+'" value="'+i+'"/></td>';
            floating += '<td style="'+style[1]+'">';
            floating += '<a href="#" onclick="removeFloatingLicense('+license.LicType+','+i+')">'+$uiStrings.UI_REMOVE+'</a>';
            floating += '</td>';
            floating += '<td style="'+style[2]+'"><a href="#" onclick="showFloatingLicenseSlide('+license.LicType+','+i+')">'+license.Features+'/'+license.Version+'</a></td>';
            floating += '<td style="'+style[3]+'">'+license.ServerName+'</td>';
            floating += '<td style="'+style[4]+'">'+license.ServerPort+'</td>';
            floating += '<td style="'+style[5]+'">'+$techStrings[license.LicTech]+'</td>';
            floating += '</tr>';
        }
    } catch (e) {
    }
    floating += '</tbody></table></div>';
    $('#floating_license_table').html(floating);
}

function showFloatingLicenseSlide(type, index)
{
    var license = $gLicenses[type][index];
    var xhtml = '<div class="license">';
    xhtml += '<table><tbody>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_LICTECH+$colon+'</td><td class="license-value">'+$techStrings[license.LicTech]+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_LICTYPE+$colon+'</td><td class="license-value">'+$typeStrings[license.LicType]+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_SERVERNAME+$colon+'</td><td class="license-value">'+license.ServerName+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_SERVERPORT+$colon+'</td><td class="license-value">'+license.ServerPort+'</td></tr>';
    xhtml += '<tr><td class="license-key">'+$uiStrings.UI_FEATURES+$colon+'</td><td class="license-value">'+license.Features+'</td></tr>';
    xhtml += '</tbody></table>';
    xhtml += '</div>';
    $('#license_board').html(xhtml);
    $('#license_table').hide();
    $('#license_slide').fadeIn();
}
function showRemoteLicenses(server_name, server_port)
{
    try {
        $('#remote_license_table').html('');
        $gRemotes = jQuery.parseJSON(window.external.GetRemoteLicenses(server_name, server_port));
        if ($gRemotes.length > 0) {
            var remote = '';
            remote += '<div class="ui-table-head">';
            remote += '<table celspacing="0" class="ui-table">';
            remote += '<thead><tr>';
            remote += '<th style="width: 20px;"></th>';
            remote += '<th style="width: 280px;">'+$uiStrings.UI_FEATURES+'</th>';
            remote += '<th style="border-right: none; width:75px;">'+$uiStrings.UI_USING+'</th>';
            remote += '</tr></thead></table></div>';
            remote += '<div class="ui-table-body">';
            remote += '<table celspacing="0" class="ui-table">';
            remote += '<tbody id="remote_license_list">';
            for (var i = 0; i < $gRemotes.length; i++) {
                var license = $gRemotes[i];
                if (license.LicType == FLXNET_FLOATING) {
                    remote += '<tr>';
                    remote += '<td style="width: 20px;"><input type="radio" name="remote" value="'+i+'" /></td>';
                    remote += '<td style="width: 280px;">'+license.Features+'/'+license.Version+'</td>';
                    remote += '<td style="border-right: none; width: 75px">'+license.Status+'/'+license.Count+'</td>';
                    remote += '</tr>';
                }
            }
            remote += '</tbody></table></div>';
            $('#remote_license_table').html(remote);
        }
    } catch (e) {
    }
}
function hideRemoteLicenses()
{
    $('#remote_license_table').html('');
    $gRemotes = [];
}
$(function() {
    loadNodelockLicenses();
    loadFloatingLicenses()
    loadActiveLicense();
    showNodelockLicenses();
    showFloatingLicenses();
    showActiveLicense();
    var box1_option = { modal: true, width: 300, autoOpen: false, draggable: false, resizable: false, height: 180 };
    $alertBox = $('#alertBox').dialog(box1_option);
    $errorBox = $('#errorBox').dialog(box1_option);
    $enterBox = $('#enterBox').dialog(box1_option);
    var box3_option = { modal: true, width: 300, autoOpen: false, draggable: false, resizable: false, height: 95 };
    $returnBox = $('#returnBox').dialog(box3_option);
    $repairBox = $('#repairBox').dialog(box3_option);
    $optionBox = $('#optionBox').dialog(box3_option);
    $('#server_exp_borrow').datepicker({minDate: 1, maxDate: 181, showButtonPanel: true, dateFormat: 'yy-mm-dd'});
    $('#ui-datepicker-div').wrap('<div style="position: absolute; left: 0; top: 0"></div>');
    $cdkey1 = $('#activationId_online').cdkey();
    $cdkey2 = $('#activationId_offline').cdkey();
    /*
    *
    *
    *
    */
    $('#btn_online_activate_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#nodelock_flxnet_wizard').hide();
        $('#license_table').fadeIn();
        $cdkey1.cdkey('clear');
        return false;
    });
    $('#btn_online_activate_prev').button({icons: {primary: 'ui-icon-circle-triangle-w'}}).click(function() {
        $('#nodelock_flxnet_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_online_activate_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        $('#nodelock_flxnet_wizard').hide();
        $('#license_table').fadeIn();
        $cdkey1.cdkey('clear');
        return false;
    });
    $('#btn_online_activate').button({icons: {primary: 'ui-icon-circle-triangle-e'}}).click(function() {
        var entitlementId = $cdkey1.cdkey('value');
        if (entitlementId == "") {
            showError($errStrings.ERR_ACTIVATE_1);
            return false;
        }
        if (entitlementId.length != 29) {
            showError($errStrings.ERR_ACTIVATE_7);
            return false;
        }
        var l = new ZwLicense();
        l.LicTech = FLXNET_APP;
        l.LicType = FLXNET_NODELOCK;
        l.EntitlementId = entitlementId;
        var ret = window.external.Activate(l.toString());
        if (ret == 0) {
            showAlert($errStrings.ERR_ACTIVATE_3, function(){
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                checkLastLicense(FLXNET_NODELOCK);
                $('#nodelock_flxnet_wizard').hide();
                $('#license_table').fadeIn();
                $cdkey1.cdkey('clear');
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_activate_prev').button({icons: {primary: 'ui-icon-circle-triangle-w'}}).click(function() {
        $('#nodelock_offline_activate').hide();
        $('#nodelock_flxnet_wizard').fadeIn();
        return false;
    });
    $('#btn_offline_activate_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $cdkey2.cdkey('clear');
        $('#fileName_offline_activate').val('');
        $('#nodelock_offline_activate').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_offline_activate_generate').button({icons: {primary: 'ui-icon-circle-arrow-e'}}).click(function() {
        var entitlementId = $cdkey2.cdkey('value');
        if (entitlementId == "") {
            showError($errStrings.ERR_ACTIVATE_1);
            return false;
        }
        if (entitlementId.length != 29) {
            showError($errStrings.ERR_ACTIVATE_7);
            return false;
        }
        var l = new ZwLicense();
        l.LicTech = FLXNET_APP;
        l.LicType = FLXNET_NODELOCK;
        l.EntitlementId = entitlementId;
        var ret = window.external.GenerateActivateRequestFile(l.toString());
        if (ret == 0) {
            loadNodelockLicenses();
            showNodelockLicenses();
            showActiveLicense();
            showAlert($errStrings.ERR_ACTIVATE_4);
        } else if (ret == 40005) {
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_activate_process').button({icons: {primary: 'ui-icon-circle-arrow-e'}}).click(function() {
        var filePath = $('#fileName_offline_activate').val();
        if (filePath == "") {
            showError($errStrings.ERR_ACTIVATE_5);
            return false;
        }
        var ret = window.external.ProcessActivateResponseFile(filePath);
        if (ret == 0) {
            showAlert($errStrings.ERR_ACTIVATE_6, function(){
                $cdkey2.cdkey('clear');
                $('#fileName_offline_activate').val('');
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                checkLastLicense(FLXNET_NODELOCK);
                $('#nodelock_offline_activate').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_activate_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        $cdkey2.cdkey('clear');
        $('#fileName_offline_activate').val('');
        $('#nodelock_offline_activate').hide();
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_online_return_wizard').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        $returnBox.dialog('close');
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        var ret = window.external.Return(license.toString());
        if (ret == 0) {
            showAlert($errStrings.ERR_RETURN_1, function(){
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                $('#nodelock_flxnet_license_board').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_return_wizard').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        $returnBox.dialog('close');
        $('#license_table').hide();
        $('#nodelock_flxnet_license_board').hide();
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        $('#fulfillmentId_offline_return').val(license.FulfillmentId);
        $('#nodelock_offline_return').fadeIn();
        return false;
    });
    $('#btn_offline_return_generate').button({icons: {primary: 'ui-icon-circle-arrow-e'}}).click(function() {
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        var ret = window.external.GenerateReturnRequestFile(license.toString());
        if (ret == 0) {
            loadNodelockLicenses();
            showNodelockLicenses();
            showActiveLicense();
            showAlert($errStrings.ERR_RETURN_2);
        } else if (ret == 40005) {
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_return_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#fileName_offline_return').val('');
        $('#nodelock_offline_return').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_offline_return_process').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        var filePath = $('#fileName_offline_return').val();
        if (filePath == "") {
            showError($errStrings.ERR_RETURN_3);
            return false;
        }
        var ret = window.external.ProcessReturnResponseFile(filePath);
        if (ret == 0) {
            showAlert($errStrings.ERR_RETURN_4, function(){
                $('#fileName_offline_return').val('');
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                $('#nodelock_offline_return').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_return_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        $('#fileName_offline_return').val('');
        $('#nodelock_offline_return').hide();
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_online_repair_wizard').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        $repairBox.dialog('close');
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        var ret = window.external.Repair(license.toString());
        if (ret == 0) {
            showAlert($errStrings.ERR_REPAIR_1, function(){
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                $('#nodelock_flxnet_license_board').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_repair_wizard').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        $repairBox.dialog('close');
        $('#license_table').hide();
        $('#nodelock_flxnet_license_board').hide();
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        $('#fulfillmentId_offline_repair').val(license.FulfillmentId);
        $('#nodelock_offline_repair').fadeIn();
        return false;
    });
    $('#btn_offline_repair_generate').button({icons: {primary: 'ui-icon-circle-arrow-e'}}).click(function() {
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        var ret = window.external.GenerateRepairRequestFile(license.toString());
        if (ret == 0) {
            loadNodelockLicenses();
            showNodelockLicenses();
            showActiveLicense();
            showAlert($errStrings.ERR_REPAIR_2);
        } else if (ret == 40005) {
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_repair_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#fileName_offline_repair').val('');
        $('#nodelock_offline_repair').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_offline_repair_process').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        var filePath = $('#fileName_offline_repair').val();
        if (filePath == "") {
            showError($errStrings.ERR_REPAIR_3);
            return false;
        }
        var ret = window.external.ProcessRepairResponseFile(filePath);
        if (ret == 0) {
            showAlert($errStrings.ERR_REPAIR_4, function(){
                $('#fileName_offline_repair').val('');
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                $('#nodelock_offline_repair').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_offline_repair_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        $('#fileName_offline_repair').val('');
        $('#nodelock_offline_repair').hide();
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_borrowed_return_return').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        var server_name = $('#server_name_return').val();
        var server_port = $('#server_port_return').val();
        if (server_name == "") {
            showError($errStrings.ERR_FLOATING_1);
            return false;
        }
        if (server_name.length > 15) {
            showError($errStrings.ERR_FLOATING_16);
            return false;
        }
        var pattern = /^[0-9a-zA-Z\.\-]*$/;
        if (!pattern.test(server_name)) {
            showError($errStrings.ERR_FLOATING_17);
            return false;
        }
        pattern =/^[1-9]+[0-9]*$/;
        if (server_port != "" && !pattern.test(server_port) || parseInt(server_port) > 65535) {
            showError($errStrings.ERR_FLOATING_15);
            return false;
        }
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        var l = new ZwLicense();
        l.LicTech = FLXNET_APP;
        l.LicType = FLXNET_NODELOCK;
        l.FulType = license.FulType;
        l.EntitlementId = license.EntitlementId;
        l.FulfillmentId = license.FulfillmentId;
        l.ServerName = server_name;
        l.ServerPort = server_port;
        l.EnableDate = license.EnableDate
        l.ExpireDate = license.ExpireDate;
        l.Features = license.Features;
        var ret = window.external.ReturnBorrowedLicense(l.toString());
        if (ret == 0) {
            loadNodelockLicenses();
            showNodelockLicenses();
            showActiveLicense();
            showAlert($errStrings.ERR_FLOATING_5, function() {
                $('#server_name_return').val('');
                $('#server_port_return').val('');
                $('#borrowed_flxnet_return').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_borrowed_return_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#server_name_return').val('');
        $('#server_port_return').val('');
        $('#borrowed_flxnet_return').hide();
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_floating_borrow_borrow').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        var license = $gLicenses[$gIndex.LicType][$gIndex.Index];
        var expire_date = new Date();
        var entitlement_id = $('#server_acid_borrow').val();
        if (entitlement_id == "") {
            showError($errStrings.ERR_ACTIVATE_1);
            return false;
        }
        if (entitlement_id.length != 29) {
            showError($errStrings.ERR_ACTIVATE_7);
            return false;
        }
        if ($('#date_type_exp').attr('checked')) {
            var server_exp = $('#server_exp_borrow').val();
            if (server_exp == "") {
                showError($errStrings.ERR_FLOATING_6);
                return false;
            }
            try {
                expire_date = $.datepicker.parseDate('yy-mm-dd', server_exp);
            } catch (e) {
                showError($errStrings.ERR_FLOATING_10);
                return false;
            }
            var expire_min = new Date();
            if (expire_date < expire_min) {
                showError($errStrings.ERR_FLOATING_14);
                return false;
            }
            var expire_max = new Date();
            expire_max.setMonth(expire_max.getMonth() + 6);
            if (expire_date > expire_max) {
                showError($errStrings.ERR_FLOATING_13);
                return false;
            }
        } else {
            var server_dur = $('#server_dur_borrow').val();
            var pattern =/^[1-9]+[0-9]*$/;
            if (!pattern.test(server_dur)) {
                showError($errStrings.ERR_FLOATING_7);
                return false;
            }
            var date_unit = $('#date_unit').val();
            if (date_unit == 0) {
                if (server_dur > 0 && server_dur < 181) {
                    expire_date.setDate(expire_date.getDate() + parseInt(server_dur));
                } else {
                    showError($errStrings.ERR_FLOATING_11);
                    return false;
                }
            } else {
                if (server_dur > 0 && server_dur < 7) {
                    expire_date.setMonth(expire_date.getMonth() + parseInt(server_dur));
                } else {
                    showError($errStrings.ERR_FLOATING_12);
                    return false;
                }
            }
        }
        var l = new ZwLicense();
        l.LicTech = FLXNET_APP;
        l.LicType = FLXNET_NODELOCK;
        l.FulType = UNKNOWN;
        l.EntitlementId = entitlement_id;
        l.ServerName = license.ServerName;
        l.ServerPort = license.ServerPort;
        l.Features = license.Features;
        l.ExpireDate = $.datepicker.formatDate('d-M-yy', expire_date, {monthNamesShort:$l_months});
        var ret = window.external.BorrowFloatingLicense(l.toString());
        if (ret == 0) {
            loadNodelockLicenses();
            showNodelockLicenses();
            showActiveLicense();
            checkLastLicense(FLXNET_NODELOCK);
            showAlert($errStrings.ERR_FLOATING_4, function() {
                $('#floating_flxnet_borrow').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_floating_borrow_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#floating_flxnet_borrow').hide();
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_nodelock_rockey_activate').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        var keyId = $('#keyId_rockey').val();
        var entitlementId = $('#activationId_rockey').val();
        if (keyId == "") {
            showError($errStrings.ERR_ACTIVATE_8);
            return false;
        }
        if (entitlementId == "") {
            showError($errStrings.ERR_ACTIVATE_1);
            return false;
        }
        if (entitlementId.length != 8) {
            showError($errStrings.ERR_ACTIVATE_9);
            return false;
        }
        var l = new ZwLicense();
        l.LicTech = ROCKEY_APP;
        l.LicType = ROCKEY_NODELOCK;
        l.EntitlementId = entitlementId;
        l.FulfillmentId = keyId;
        var ret = window.external.Activate(l.toString());
        if (ret == 0) {
            showAlert($errStrings.ERR_ACTIVATE_3, function(){
                loadNodelockLicenses();
                showNodelockLicenses();
                showActiveLicense();
                checkLastLicense(ROCKEY_NODELOCK);
                $('#nodelock_rockey_wizard').hide();
                $('#license_table').fadeIn();
            });
        } else {
            showError(window.external.Resource(ret));
        }
        return false;
    });
    $('#btn_nodelock_rockey_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#nodelock_rockey_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_nodelock_rockey_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        $('#nodelock_rockey_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_nodelock_rockey_prev').button({icons: {primary: 'ui-icon-circle-triangle-w'}}).click(function() {
        $('#nodelock_rockey_wizard').hide();
        $('#nodelock_flxnet_wizard').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_type_floating_flxnet').button({icons: {primary: 'ui-icon-wrench'}}).click(function() {
        $optionBox.dialog('close');
        $('#license_table').hide();
        var options = '';
        for (var k in $features) {
            options += '<option>'+k+'</option>';
        }
        $('#flxnet_server_feat').html(options);
        $('#floating_flxnet_wizard').fadeIn('slow', function(){
            $('#flxnet_server_name').focus();
        });
        return false;
    });
    $('#btn_floating_flxnet_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        hideRemoteLicenses();
        $('#flxnet_server_name').val('');
        $('#flxnet_server_port').val('');
        $('#floating_flxnet_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_floating_flxnet_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        var server_name = $('#flxnet_server_name').val();
        var server_port = $('#flxnet_server_port').val();
        var server_feat = $('#flxnet_server_feat').val();
        if (server_name == "") {
            showError($errStrings.ERR_FLOATING_1);
            return false;
        }
        if (server_name.length > 15) {
            showError($errStrings.ERR_FLOATING_16);
            return false;
        }
        var pattern = /^[0-9a-zA-Z\.\-]*$/;
        if (!pattern.test(server_name)) {
            showError($errStrings.ERR_FLOATING_17);
            return false;
        }
        pattern =/^[1-9]+[0-9]*$/;
        if (server_port != "" && !pattern.test(server_port) || parseInt(server_port) > 65535) {
            showError($errStrings.ERR_FLOATING_2);
            return false;
        }
        var l = new ZwLicense();
        l.LicTech = FLXNET_APP;
        l.LicType = FLXNET_FLOATING;
        l.FulType = UNKNOWN;
        l.Version = "1.0";
        l.ServerName = server_name;
        l.ServerPort = server_port;
        var r = $('#remote_license_list input[type=radio]:checked');
        if (r.length > 0) {
            var license = $gRemotes[r.val()];
            l.VendorString = $features[license.Features[0]] ? "ZW3D_MAJOR" : "ZW3D_MINOR";
            l.Features = license.Features;
        } else {
            l.VendorString = $features[server_feat] ? "ZW3D_MAJOR" : "ZW3D_MINOR";
            l.Features.push(server_feat);
        }
        if (window.external.AppendFloatingLicense(l.toString())) {
            $('#flxnet_server_name').val('');
            $('#flxnet_server_port').val('');
            $gLicenses[FLXNET_FLOATING].push(l);
            showFloatingLicenses();
            showActiveLicense();
            checkLastLicense(FLXNET_FLOATING);
            hideRemoteLicenses();
            $('#floating_flxnet_wizard').hide();
            $('#license_table').fadeIn();
        } else {
            showError($errStrings.ERR_FLOATING_3);
        }
        return false;
    });
    $('#btn_floating_flxnet_prev').button({icons: {primary: 'ui-icon-circle-triangle-w'}}).click(function() {
        hideRemoteLicenses();
        $('#floating_flxnet_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_floating_flxnet_connect').button({icons: {primary: 'ui-icon-transferthick-e-w'}}).click(function() {
        var server_name = $('#flxnet_server_name').val();
        var server_port = $('#flxnet_server_port').val();
        var server_feat = $('#flxnet_server_feat').val();
        if (server_name == "") {
            showError($errStrings.ERR_FLOATING_1);
            return false;
        }
        if (server_name.length > 15) {
            showError($errStrings.ERR_FLOATING_16);
            return false;
        }
        var pattern = /^[0-9a-zA-Z\.\-]*$/;
        if (!pattern.test(server_name)) {
            showError($errStrings.ERR_FLOATING_17);
            return false;
        }
        pattern =/^[1-9]+[0-9]*$/;
        if (server_port == "" || pattern.test(server_port) && parseInt(server_port) <= 65535) {
            var l = new ZwLicense();
            l.LicTech = FLXNET_APP;
            l.LicType = FLXNET_FLOATING;
            l.FulType = UNKNOWN;
            l.Version = "1.0";
            l.ServerName = server_name;
            l.ServerPort = server_port;
            l.Features = server_feat.split('+');
            hideRemoteLicenses();
            var ret = window.external.Connect(l.toString());
            if (ret == 0) {
                showAlert($errStrings.ERR_FLOATING_8);
            } else if (ret == 51015 || ret == 51073 || ret == 51096) {
                showError(window.external.Resource(ret));
            } else {
                showError(window.external.Resource(ret), function() {
                    showRemoteLicenses(server_name, server_port);
                });
            }
        } else {
            showError($errStrings.ERR_FLOATING_2);
        }
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_type_floating_rockey').button({icons: {primary: 'ui-icon-wrench'}}).click(function() {
        $optionBox.dialog('close');
        $('#license_table').hide();
        var options = '';
        for (var k in $features) {
            options += '<option>'+k+'</option>';
        }
        $('#rockey_server_feat').html(options);
        $('#floating_rockey_wizard').fadeIn('slow', function(){
            $('#rockey_server_name').focus();
        });
        return false;
    });
    $('#btn_floating_rockey_cancel').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        $('#rockey_server_name').val('');
        $('#rockey_server_port').val('');
        $('#floating_rockey_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    $('#btn_floating_rockey_finish').button({icons: {primary: 'ui-icon-circle-check'}}).click(function(){
        var server_name = $('#rockey_server_name').val();
        var server_port = $('#rockey_server_port').val();
        var server_feat = $('#rockey_server_feat').val();
        if (server_name == "") {
            showError($errStrings.ERR_FLOATING_1);
            return false;
        }
        if (server_name.length > 15) {
            showError($errStrings.ERR_FLOATING_16);
            return false;
        }
        var pattern = /^[0-9a-zA-Z\.\-]*$/;
        if (!pattern.test(server_name)) {
            showError($errStrings.ERR_FLOATING_17);
            return false;
        }
        pattern =/^[1-9]+[0-9]*$/;
        if (server_port != "" && !pattern.test(server_port) || parseInt(server_port) > 65535) {
            showError($errStrings.ERR_FLOATING_2);
            return false;
        }
        var l = new ZwLicense();
        l.LicTech = ROCKEY_APP;
        l.LicType = ROCKEY_FLOATING;
        l.FulType = UNKNOWN;
        l.Version = "1.0";
        l.VendorString = $features[server_feat] ? "ZW3D_MAJOR" : "ZW3D_MINOR";
        l.ServerName = server_name;
        l.ServerPort = server_port;
        l.Features.push(server_feat);
        if (window.external.AppendFloatingLicense(l.toString())) {
            $('#rockey_server_name').val('');
            $('#rockey_server_port').val('');
            $gLicenses[ROCKEY_FLOATING].push(l);
            showFloatingLicenses();
            showActiveLicense();
            checkLastLicense(ROCKEY_FLOATING);
            $('#floating_rockey_wizard').hide();
            $('#license_table').fadeIn();
        } else {
            showError($errStrings.ERR_FLOATING_3);
        }
        return false;
    });
    $('#btn_floating_rockey_prev').button({icons: {primary: 'ui-icon-circle-triangle-w'}}).click(function() {
        $('#floating_rockey_wizard').hide();
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#server_exp_borrow').focus(function() {
        $('#date_type_exp').attr('checked', true);
        $('#server_dur_borrow').val('');
        return false;
    });
    $('#server_dur_borrow').focus(function() {
        $('#date_type_dur').attr('checked', true);
        $('#server_exp_borrow').val('');
        return false;
    });
    /*
    *
    *
    *
    */
    $('.platform').click(function(){
        window.external.OpenUrl($(this).attr('href'));
        return false;
    });
    $('#btn_offline_activate_link').click(function() {
        $('#nodelock_flxnet_wizard').hide();
        $('#nodelock_offline_activate').fadeIn('slow', function(){
            $('#activationId_offline').focus();
        });
        return false;
    });
    $('#btn_nodelock_rockey_activate_link').click(function() {
        $('#nodelock_flxnet_wizard').hide();
        var RockeyLockerIdArry = jQuery.parseJSON(window.external.GetAllRockeyLockerId());
        var RockeyLockerIdArryHtml = '';
        for (var i = 0; i < RockeyLockerIdArry.length; i++)  {
            RockeyLockerIdArryHtml += '<option>';
            var tempOption = RockeyLockerIdArry[i];
            if (tempOption.length < 8) {
                for (var j = 0; j < 8 - tempOption.length; j++) {
                    tempOption = '0' + tempOption;
                }
            }
            else if (tempOption.length > 8) {
                tempOption = tempOption.substr(0, 8);
            }
            RockeyLockerIdArryHtml += tempOption;
            RockeyLockerIdArryHtml += '</option>';
        }
        $('#keyId_rockey').html(RockeyLockerIdArryHtml);
        $('#nodelock_rockey_wizard').fadeIn('slow', function(){
            $('#activationId_rockey').focus();
        });
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_license_table_close').button({icons: {primary: 'ui-icon-circle-close'}}).click(function() {
        if (!equalLicense($gAppliedMajor, $gOriginalMajor) || !equalLicenseArray($gAppliedMinors, $gOriginalMinors)) {
            if (window.external.IsQuiet()) {
                window.external.Finish(IDOK);
            } else {
                showAlert($errStrings.ERR_ALERT_1, function() {
                    window.external.Finish(IDOK);
                });
            }
        } else {
            window.external.Finish(IDCANCEL);
        }
        return false;
    });
    $('#btn_license_table_apply').button({icons: {primary: 'ui-icon-circle-check'}, disabled: true}).click(function() {
        saveActiveLicense();
        $(this).button('disable');
        return false;
    });
    $('#btn_license_table_ok').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        if (!$('#btn_license_table_apply').button('option', 'disabled')) {
            saveActiveLicense();
        }
        if (!equalLicense($gAppliedMajor, $gOriginalMajor) || !equalLicenseArray($gAppliedMinors, $gOriginalMinors)) {
            if (window.external.IsQuiet()) {
                window.external.Finish(IDOK);
            } else {
                showAlert($errStrings.ERR_ALERT_1, function() {
                    window.external.Finish(IDOK);
                });
            }
        } else {
            window.external.Finish(IDCANCEL);
        }
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_license_slide_ok').button({icons: {primary: 'ui-icon-circle-check'}}).click(function() {
        showNodelockLicenses();
        showActiveLicense();
        $('#license_slide').hide()
        $('#license_table').fadeIn();
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_nodelock_activate').button({icons: {primary: 'ui-icon-circle-arrow-s'}}).click(function() {
        $('#license_table').hide();
        $('#nodelock_flxnet_wizard').fadeIn('slow', function(){
            $('#activationId_online').focus();
        });
        return false;
    });
    /*
    *
    *
    *
    */
    $('#btn_floating_add').button({icons: {primary: 'ui-icon-circle-plus'}}).click(function() {
        $optionBox.dialog('open');
        return false;
    });
    /*
    *
    *
    *
    */
    $('body').show();
});
