﻿// JScript File

function StartButton_ShowComboEntry()
{

    HidePanel("#UpgradePre");
    ShowPanel("#UpgradeEntry");
    SetFocus('txtsbCombination');
    
    jQuery("#txtsbCombination").keypress(function (e) {
    
        if (e.which == 13) {
            UpgradeCheck();
            return false;
        }
        
        var v = document.getElementById('txtsbCombination').value.replace(' ',''); 
        
        // IE browsers use the document.selection object below, whereas FF, Safari and Chrome use the selectionStart/selectionEnd object 
        var selectedTxt = ( typeof(document.selection)!='undefined' && document.selection.type.toLowerCase()=="text" || typeof(this.selectionStart)!="undefined" && typeof(this.selectionEnd)!="undefined" && this.selectionEnd>this.selectionStart ) ? true : false; 
        
        if (v.replace(' ','').length >= 7 && !selectedTxt && !(e.which == 0 || e.which == 8 || e.which == 127))
            return false;
            
        if (v.length > 8 && !selectedTxt && !(e.which == 0 || e.which == 8 || e.which == 127)) 
            return false;
            
        if (v.length == 0 && e.which == 32)
            return false;
                        
        if (e.which == 0 || e.which == 8 || e.which == 127 || e.which == 32 || (65 <= e.which && e.which <= 65 + 25) || (97 <= e.which && e.which <= 97 + 25) || (48 <= e.which && e.which <= 48 + 9)) 
            return true;
        else
            return false;
    });

}

function UpgradeCheck()
{
    var Combination = document.getElementById('txtsbCombination').value;
    Combination = Combination.toUpperCase();

    if (StartupCombinationIsValid(Combination)) {
        if (ComboFormat(Combination.replace(' ','').replace(' ','').replace(' ','')) == "QLLNNZ")
            Combination = Combination.replace(' ','').replace(' ','').replace(' ','');
            
        location.href = CAPUrl() + "?upgrade=" + Combination;
        }
        
    return false;
}


function StartupCombinationIsValid(Combination)
{
   var Result = checkCombo(Combination.replace(' ',''));
   
    if (Result == "")
        return true;
    else {
        alert(Result);    
        return false;
        }
}


function ShowPanel(n)
{ jQuery(n).css("display",""); }

function HidePanel(n)
{ jQuery(n).css("display","none"); }

function SetFocus(n)
{
var e = document.getElementById(n);

if (e != null)
    e.focus();
}

function ComboFormat(_Combination)
{
    var _CombinationFormat = "";
   
    if (_Combination != null) {
        _Combination = _Combination.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','');
        if (_Combination.length == 5) {
            if ((IsLetter(_Combination, 1) && IsLetter(_Combination, 2) && IsLetter(_Combination, 3) && IsNumeric(_Combination, 4) && IsNumeric(_Combination, 5)) || (IsNumeric(_Combination, 1) && IsNumeric(_Combination, 2) && IsLetter(_Combination, 3) && IsLetter(_Combination, 4) && IsLetter(_Combination, 5))) {
                if (IsQ(_Combination, 1))
                    _CombinationFormat = "QLLNN";
                else
                    _CombinationFormat = "3x2";
                }
            else
                _CombinationFormat = "5";
            }
        else if (_Combination.length == 6)
            if (IsQ(_Combination, 1) && IsLetter(_Combination, 2) && IsLetter(_Combination, 3) && IsNumeric(_Combination, 4) && IsNumeric(_Combination, 5) && IsZ(_Combination, 6))
                _CombinationFormat = "QLLNNZ";
            else if ((IsLetter(_Combination, 1) && IsLetter(_Combination, 2) && IsLetter(_Combination, 3) && IsNumeric(_Combination, 4) && IsNumeric(_Combination, 5) && IsNumeric(_Combination, 6)) || (IsNumeric(_Combination, 1) && IsNumeric(_Combination, 2) && IsNumeric(_Combination, 3) && IsLetter(_Combination, 4) && IsLetter(_Combination, 5) && IsLetter(_Combination, 6))) 
                _CombinationFormat = "3x3";
            else
                _CombinationFormat = "6";
        else if (_Combination.length == 1)
            _CombinationFormat = "1";
        else if (_Combination.length == 2) 
            _CombinationFormat = "2";
        else if (_Combination.length == 3)
            _CombinationFormat = "3";
        else if (_Combination.length == 4)
            _CombinationFormat = "4"; 
        else if (_Combination.length == 7)
            _CombinationFormat = "7"; 
        }
                
    return _CombinationFormat;
}

function IsLetter(t, o)
{
var alpha = "abcdefghijklmnopqrstuvwxyz";

return alpha.indexOf(t.charAt(o - 1).toLowerCase()) >= 0;
}

function IsNumeric(t, o)
{
var alpha = "0123456789";

return alpha.indexOf(t.charAt(o - 1).toLowerCase()) >= 0;
}

function IsQ(t, o)
{
var alpha = "q";

return alpha.indexOf(t.charAt(o - 1).toLowerCase()) >= 0;
}

function IsZ(t, o)
{
var alpha = "zy";

return alpha.indexOf(t.charAt(o - 1).toLowerCase()) >= 0;
}

