﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />

(function()
{
    BSC.UI.Ws.signup = function()
    {
        var HelpText =
            {
                "signup_name": { header: BSC.T.Get("JS.BSC.UI.Ws.SignUp.HelpName.Title"), desc: BSC.T.Get("JS.BSC.UI.Ws.SignUp.HelpName.Desc") + "<br><br>" },
                "signup_email": { header: BSC.T.Get("JS.BSC.UI.Ws.SignUp.HelpEmail.Title"), desc: BSC.T.Get("JS.BSC.UI.Ws.SignUp.HelpEmail.Desc") + "<br><br>" },
                "signup_password": { header: BSC.T.Get("JS.BSC.UI.Ws.SignUp.HelpPass.Title"), desc: BSC.T.Get("JS.BSC.UI.Ws.SignUp.HelpPass.Desc") + "<br><br>" }
            };
        var SignupData = { file: null };
        var suo;
        return {
            Init: function(uiid)
            {
                suo = new SWFUpload({
                    upload_url: BSC.BASE_UPLOAD_PATH,
                    flash_url: BSC.BASE_MEDIA_PATH + "Content/Flash/SWFUpload/swfupload_f9.swf", //
                    file_size_limit: "204800",
                    // Event Handler Settings
                    //file_queued_handler : BSC.UI.Ws.signup.FileQueued,
                    file_dialog_complete_handler: BSC.UI.Ws.signup.FileDialogComplete,
                    //upload_progress_handler : BSC.UI.Ws.signup.UploadProgress,
                    upload_success_handler: BSC.UI.Ws.signup.UploadSuccess,
                    upload_complete_handler: BSC.UI.Ws.signup.UploadComplete,
                    upload_error_handler: BSC.UI.Ws.signup.UploadError
                });

                //                    $("#btn_login").click(Login);
                $("#btn_signup").click(BSC.UI.Ws.signup.Send);
            },
            FileQueued: function(file)
            {

            },
            FileDialogComplete: function(n)
            {
                try
                {
                    if (n > 0)
                    {
                        $("#default_image").css("display", "none");
                        $("#LOADER").css("display", "block");

                        this.startUpload();
                    }
                } catch (ex)
                {
                    //this.debug(ex);
                }
            },
            UploadProgress: function(file, bytesLoaded)
            {
                try
                {
                    var percent = Math.min(Math.ceil((bytesLoaded / file.size) * 100), 100);
                    $("#imageloader").css({ width: percent + "%" });
                } catch (ex)
                {
                    this.debug(ex);
                }
            },
            UploadSuccess: function(file, fileGuid)
            {
                try
                {
                    SignupData.file = fileGuid;
                    var img = $(document.createElement("img"));
                    img.attr("src", BSC.PicturePath(fileGuid, "small"));
                    img.css({ display: 'none' });
                    $("#pictureholder").html("");
                    $("#pictureholder").attr("align", "center");
                    $("#pictureholder").append(img);
                    img.fadeIn("fast", function() { });
                } catch (ex)
                {
                    this.debug(ex);
                }
            },
            UploadComplete: function(file)
            {
                //                    try {
                //                        //alert("upload done");
                //	                } catch (ex) {
                //		                this.debug(ex);
                //	                }
            },
            UploadError: function(fileobject, errorcode, message)
            {
                alert(message);
            },
            SelectFile: function(file)
            {
                BSC.UI.HideHelpBubble();
                if (!SignupData.file)
                    suo.selectFile();
                else
                    alert("File already uploaded");

                return false;
            },
            Send: function(e)
            {
                var n = ($("#signup_name").getValue()).replace($("#signup_name").attr("title"), "");
                if (n.length == 0)
                {
                    BSC.UI.ShowHelpBubble("signup_name", "alert", BSC.T.Get("JS.BSC.UI.Ws.SignUp.Name.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.Name.Desc") + "<br><br>");
                    return false;
                }
                var e = ($("#signup_email").getValue()).replace($("#signup_email").attr("title"), "")
                if (!BSC.R.Email.test(e))
                {
                    BSC.UI.ShowHelpBubble("signup_email", "alert", BSC.T.Get("JS.BSC.UI.Ws.SignUp.Email.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.Email.Desc") + "<br><br>", { top: -10, left: 202 });
                    return false;
                }
                var pwd = $("#signup_password").getValue();
                if (pwd.length < 4 && !BSC.R.Password.test(pwd))
                {
                    BSC.UI.ShowHelpBubble("signup_password", "alert", BSC.T.Get("JS.BSC.UI.Ws.SignUp.Pass.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.Pass.Desc") + "<br><br>", null);
                    return false;
                }
                var d = parseInt($("#signup_day").getValue());
                var m = parseInt($("#signup_month").getValue());
                var y = parseInt($("#signup_year").getValue());
                if (d == 0 || m == 0 || y == 0)
                {
                    BSC.UI.ShowHelpBubble("signup_day", "alert", BSC.T.Get("JS.BSC.UI.Ws.SignUp.DOB.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.DOB.Desc") + "<br/><br/>", null);
                    return false;
                }

                var g = $("input[@name='signup_gender']").getValue(); //$("#signup_female").val();                    
                //g = (g) ? g : $("#signup_male").val();                     
                if (g.length == 0)
                {
                    BSC.UI.ShowHelpBubble("signup_female", "alert", BSC.T.Get("JS.BSC.UI.Ws.SignUp.Gender.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.Gender.Desc") + "<br/><br/>", { top: -16, left: 20 }, null);
                    return false;
                }
                var c = $("#signup_country").getValue();

                if (!SignupData.file)
                {
                    BSC.UI.ShowHelpBubble("btn_selectpicture", "alert", BSC.T.Get("JS.BSC.UI.Ws.SignUp.Picture.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.Picture.Desc") + "<br/><br/>", { top: -5, left: 200 });
                    return false;
                }

                var city = ($("#signup_city").getValue()).replace($("#signup_city").attr("title"), "")
                var state = ($("#signup_state").getValue()).replace($("#signup_state").attr("title"), "")

                var p = new BSC.D.Param("BSC.WS.JS.Public.Signup");
                p.Add("name", n);
                p.Add("email", e);
                p.Add("password", pwd);
                p.Add("gender", g);
                p.Add("bdday", d);
                p.Add("bdmonth", m);
                p.Add("bdyear", y);
                p.Add("country", c);
                p.Add("city", city);
                p.Add("state", state);
                p.Add("picture", SignupData.file);

                //vis loader
                $("#AlertOverlay").css({ display: 'block' });
                var loader = BSC.D.Ts.Get("Alert.SigningUp");
                $("#Alert .box").html(loader);
                $("#Alert").css({ display: 'block', top: '40%' });
                BSC.D.Send(p, "Public", "Signup", BSC.UI.Ws.signup.SCB); //,BSC.UI.Ws.signup.SCBE
            },
            SCB: function(res)
            {
                if (!res.d.validresponse)
                {
                    $("#AlertOverlay").css({ display: 'none' });
                    $("#Alert").css({ display: 'none' });
                    BSC.UI.Alert("Ok", null, "Error Creating profile, p", "<span>" + res.d.message + "</span>", function() { window.Reload(); });
                    return false;
                }
                if (!res.d.success)
                {
                    $("#AlertOverlay").css({ display: 'none' });
                    $("#Alert").css({ display: 'none' });
                    if (res.d.message == "ee")
                    {
                        // Profile with Email allready exists
                        $("#login_email").val(res.d.data.email);
                        BSC.UI.HideHelpBubble();

                        BSC.UI.ShowHelpBubble("login_email", "info", BSC.T.Get("JS.BSC.UI.Ws.SignUp.EmailError.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.EmailError.Desc") + "<br/><a onclick=\"OnForgotPasswordClick()\">" + BSC.T.Get("JS.BSC.UI.Ws.SignUp.EmailError.Button") + "</a><br/><br/>", null);
                    }
                    //alert(res.d.message);
                    return false;
                }
                pageTracker._trackPageview("/welcome");
                top.location = res.d.returnurl;
            }
                ,
            SCBE: function(res)
            {
                $("#AlertOverlay").css({ display: 'none', top: '40%' });
                $("#Alert").css({ display: 'none' });
                ///Alert: function(ok_key, cancel_key, header, description, callback, data, icon)
                BSC.UI.Alert("Try again", null, BSC.T.Get("JS.BSC.UI.Ws.SignUp.Error.Title"), BSC.T.Get("JS.BSC.UI.Ws.SignUp.Error.Desc"), function() { location.reload(); }, null, "info");
            }
        };
    } ();
})();

