function FlyOut() { }

FlyOut.CloseModal = function() {
    $.modal.close();
    System.Gadget.Settings.write("FlyOpen", "0");
    System.Gadget.Flyout.show = false;
}

FlyOut.ShowWelcome = function(msg) {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#welcometitle").text(Strings.GetTitle());
    $("#welcomemessage").text(Strings.GetWelcome());
    $("#welcomebutton").attr({ value: Strings.Getok() });
    $("#wiphone").text(Strings.GetIphone());
    $("#wmac").text(Strings.GetMac());
    $("#wgoogle").text(Strings.GetGoogle());
    $("#whelp").text(Strings.GetHelp());
    $('#WelcomeContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}
FlyOut.ShowLogin = function(msg) {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#LoginHeader").text(Strings.GetLogin());
    $("#Login_UserNameText").text(Strings.GetloginUser());
    $("#Login_PasswordText").text(Strings.GetloginPass());
    $("#LoginButton").attr({ value: Strings.GetLogin() });
    $("#A1").text(Strings.GetForgot());
    $("#A2").text(Strings.GetRegister());
    $("#A3").text(Strings.Getcontinueoffline());
    $("#liphone").text(Strings.GetIphone());
    $("#lmac").text(Strings.GetMac());
    $("#lgoogle").text(Strings.GetGoogle());
    $("#lhelp").text(Strings.GetHelp());
    $('#LoginContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}

FlyOut.ValidateLogin = function(type, args) {
    if (args[0].Code == "-1" || args[0].Code == "0") {
        $("#Login_Msg").text(args[0].Text);
    }
    else {
        $("#Login_Msg").text(Strings.Getloggedin());


        System.Gadget.document.parentWindow.UI.InitByGUID(args[0].Text, 4);
        FlyOut.CloseModal();
    }
}

FlyOut.UpdateWithServerNotes = function(type, args) {
    FlyOut.CloseModal();
    FlyOut.CloseModal();
        System.Gadget.document.parentWindow.closeFlyout();
    System.Gadget.document.parentWindow.UI.InitByOption(2);
    
}

FlyOut.UpdateWithClientNotes = function(type, args) {
    System.Gadget.document.parentWindow.UI.InitByOption(3);
    FlyOut.CloseModal();
}

FlyOut.ContinueOffline = function(type, args) {
    System.Gadget.document.parentWindow.UI.InitClose();
    FlyOut.CloseModal();
}

FlyOut.SyncTime = function(type, args) {
    System.Gadget.document.parentWindow.UI.InitBySync(1);
    FlyOut.CloseModal();
}


FlyOut.ShowEmail = function() {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#EmailContentTitle").text(Strings.Getemailall());
    $("#EmailTo").text(Strings.GetsubmitEmail());
    $("#EmailSelf").text(Strings.Getself());
    $("#EmailAnother").text(Strings.Getotheremail());
    $("#EmailContentOtherEmail").text(Strings.Getemail());
    $("#EmailButton").attr({ value: Strings.GetsubmitEmail() });
    $("#EmailCancel").attr({ value: Strings.Getcancel() });
    $("#eiphone").text(Strings.GetIphone());
    $("#emac").text(Strings.GetMac());
    $("#egoogle").text(Strings.GetGoogle());
    $("#ehelp").text(Strings.GetHelp());

    $('#EmailContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}

FlyOut.EM_ShowRows = function(val) {
    document.getElementById("OtherEmail").style.display = val;
    if (val == 'none') {
        document.getElementById("OtherEmailText").value = "";
    }
}

FlyOut.SubmitEmail = function() {
    $("#EmailMsg").text("");
    var email = document.getElementById("OtherEmailText").value;
    Login.EmailNotes(email, Global.GetGUID(), document.getElementById("EMOther").checked);
}

FlyOut.ValidateEmailSent = function(type, args) {
    $("#EmailMsg").text(args[0].Text);
}

FlyOut.ShowRegister = function(msg) {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#Registerheader").text(Strings.GetCreate());
    $("#RegisterUserName").text(Strings.GetloginUser());
    $("#RegisterPassword").text(Strings.GetloginPass());
    $("#ConfirmPassword").text(Strings.GetconfirmPassword());
    $("#RegisterEmail").text(Strings.Getemail());
    $("#RegisterSecurityQuestion").text(Strings.GetforgotQuestion());
    $("#RegisterSecurityAnswer").text(Strings.GetforgotAnswer());
    $("#RegisterButton").attr({ value: Strings.GetRegister() });
    $("#riphone").text(Strings.GetIphone());
    $("#rmac").text(Strings.GetMac());
    $("#rgoogle").text(Strings.GetGoogle());
    $("#rhelp").text(Strings.GetHelp());
    $("#A4").text(Strings.GetLogin());
    $("#A5").text(Strings.GetForgot());
    $("#A6").text(Strings.Getcontinueoffline());
   
    $('#RegisterContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}

FlyOut.ValidateRegister = function(type, args) {
    if (args[0].Code == "-1" || args[0].Code == "0") {
        $("#CU_Msg").text(args[0].Text);
    }
    else {
        $("#CU_Msg").text(Strings.Getloggedin());
        System.Gadget.document.parentWindow.UI.InitByGUID(args[0].Text, 3);
        FlyOut.CloseModal();
    }
}

FlyOut.ShowForgot = function() {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#ForgotHeader").text(Strings.GetForgot());
    $("#ForgotEmail").text(Strings.GetforgotEmail());
    $("#ForgotQuestion").text(Strings.GetforgotQuestion());
    $("#FPSubmitEmail").attr({ value: Strings.GetsubmitEmail() });
    $("#A7").text(Strings.GetLogin());
    $("#A8").text(Strings.GetRegister());
    $("#A9").text(Strings.Getcontinueoffline());
    $("#fiphone").text(Strings.GetIphone());
    $("#fmac").text(Strings.GetMac());
    $("#fgoogle").text(Strings.GetGoogle());
    $("#fhelp").text(Strings.GetHelp());
    $('#FPContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}

FlyOut.ValidateForgot = function(type, args) {
    if (args[0].Code == "-1" || args[0].Code == "0") {
        $("#FP_Msg").text(args[0].Text);
        $("#QR1").css("display", "none");
        $("#QR2").css("display", "none");
        $("#QR3").css("display", "none");
        $("#FPQuestion").text("");
        $("#sQuestion").text("");
        $("#sAnswer").text("");
    }
    else {
        $("#QR1").css("display", "block");
        $("#QR2").css("display", "block");
        $("#QR3").css("display", "block");
        $("#FPQuestion").text(args[0].Text);
        $("#sQuestion").text(Strings.GetforgotQuestion());
        $("#sAnswer").text(Strings.GetforgotAnswer());
        $("#FP_Msg").text("");
        $("#FPSendAnswer").attr({ value: Strings.GetsubmitAnswer() });
    }
}

FlyOut.SendAnswer = function() {
    Login.GetPassword($("#FPEmail").attr("value"), $("#FPAnswer").attr("value"));
}
FlyOut.ValidateMail = function(type, args) {
    $("#FP_Msg").text(args[0].Text);
}


FlyOut.ShowSync = function() {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#SyncHeader").text(Strings.GetsnoAlert());
    $("#SyncText").text(Strings.GetClientGreater());
    $("#SyncWithServer").text(Strings.Getupdatewithserver());
    $("#SyncWithClient").text(Strings.Getupdatewithclient());
    $("#SyncNothing").text(Strings.Getupdatewithnone());
    $("#siphone").text(Strings.GetIphone());
    $("#smac").text(Strings.GetMac());
    $("#sgoogle").text(Strings.GetGoogle());
    $("#shelp").text(Strings.GetHelp());
    
    $('#SyncContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}
FlyOut.ShowOff = function() {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#OffHeader").text(Strings.Getlogout());
    $("#OffDoYou").text(Strings.Getdoyou());
    $("#OffContinueOff").text(Strings.Getupdatewithnone());
    $("#OffCancel").text(Strings.Getcancel());
    $("#OffSync").text(Strings.GetsyncNow());
    $("#oiphone").text(Strings.GetIphone());
    $("#omac").text(Strings.GetMac());
    $("#ogoogle").text(Strings.GetGoogle());
    $("#ohelp").text(Strings.GetHelp());
    $('#OffContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}

FlyOut.ShowTry = function() {
    $.modal.close();
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#TryHeader").text(Strings.GetsnoAlert());
    $("#TryDoYou").text(Strings.GetsyncServerErrorText());
    $("#TryTry").text(Strings.GetsyncServerError1());
    $("#TryLogout").text(Strings.GetsyncServerError2());
    $("#tiphone").text(Strings.GetIphone());
    $("#tmac").text(Strings.GetMac());
    $("#tgoogle").text(Strings.GetGoogle());
    $("#thelp").text(Strings.GetHelp());
    $('#TryContent').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
}

FlyOut.SubmitLogin = function() {
    Login.SubmitLogin($("#Login_UserName").attr("value"), $("#Login_Password").attr("value"));
}

FlyOut.CreateUser = function() {
    Login.CreateUser($("#CUUserName").attr("value"), $("#CUPassword").attr("value"), $("#CUConfirmPassword").attr("value"), $("#CUEmail").attr("value"), $("#CUQuestion").attr("value"), $("#CUAnswer").attr("value"));
}

FlyOut.SendEmail = function() {
    Login.GetQuestion($("#FPEmail").attr("value"));
    $("#FPAnswer").attr({ value: "" });
}
FlyOut.InitWin = function() {
    Global.SettingsInit();
    $("#LoginContent > #Login_LoginRow").css("display", "none");
    $("#RegisterContent > #Login_NewRow").css("display", "none");
    $("#FPContent > #Login1_PasswordRecoveryLink").css("display", "none");
    /*
    w-Welcome
    l-Login
    r-Register
    f-Forgot Pass
    s-Show Sync
    t-Try Again
    o-Logout
    */
    switch (System.Gadget.Settings.read("flyval") + '') {
        case "w":
            FlyOut.ShowWelcome();
            break;
        case "l":
            FlyOut.ShowLogin();
            break;
        case "r":
            FlyOut.ShowRegister();
            break;
        case "f":
            FlyOut.ShowForgot();
            break;
        case "s":
            FlyOut.ShowSync();
            break;
        case "t":
            FlyOut.ShowTry();
            break;
        case "o":
            FlyOut.ShowOff();
            break;
        case "m":
            FlyOut.ShowEmail();
            break;
      
    }

}

FlyOut.CheckEnter = function(e) {
    var characterCode;
    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }
    if (characterCode == 13) {
        FlyOut.SubmitLogin();
        return false;
    }
    else {
        return true;
    }
}
onLogIn.subscribe(FlyOut.ValidateLogin);
onRegister.subscribe(FlyOut.ValidateRegister);
onForgotPass.subscribe(FlyOut.ValidateForgot);
onForgotMail.subscribe(FlyOut.ValidateMail);
onNotesMail.subscribe(FlyOut.ValidateEmailSent);