﻿$(document).ready(function() {
    $(".BigButton a").click(function() {

        var subdomainReg = /^([qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-_]+)$/;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        $(".Error").hide();
        var companyName = $("#companyName").val();
        if (companyName == "") {
            $(".Error").html("You must enter your company name");
            $(".Error").show();
            $("#companyName").focus();
            return true;
        }
        var subdomain = $("#subdomain").val();
        if (subdomain == "") {
            $(".Error").html("You must enter your chosen web address");
            $(".Error").show();
            $("#subdomain").focus();
            return true;
        }
        if (!subdomainReg.test(subdomain)) {
            $(".Error").html("The web address can only contain english letters, digits, hyphens and underscores");
            $(".Error").show();
            $("#subdomain").focus();
            return true;
        }
        var firstName = $("#firstName").val();
        if (firstName == "") {
            $(".Error").html("You must enter your first name");
            $(".Error").show();
            $("#firstName").focus();
            return true;
        }
        var lastName = $("#lastName").val();
        if (lastName == "") {
            $(".Error").html("You must enter your last name");
            $(".Error").show();
            $("#lastName").focus();
            return true;
        }
        var email = $("#email").val();
        if (email == "") {
            $(".Error").html("You must enter your e-mail address");
            $(".Error").show();
            $("#email").focus();
            return true;
        }
        if (!emailReg.test(email)) {
            $(".Error").html("The e-mail address you entered is invalid");
            $(".Error").show();
            $("#email").focus();
            return true;
        }
        var password1 = $("#password1").val();
        var password2 = $("#password2").val();
        if (password1 == "") {
            $(".Error").html("You must choose a password");
            $(".Error").show();
            $("#password1").focus();
            return true;
        }
        if (password2 == "") {
            $(".Error").html("Please confirm your password");
            $(".Error").show();
            $("#password2").focus();
            return true;
        }
        if (password1 != password2) {
            $(".Error").html("The password and confirm password fields do not match, please re-type your password");
            $(".Error").show();
            $("#password1").val("");
            $("#password2").val("");
            $("#password1").focus();
            return true;
        }

        $.blockUI({ message: '<img src="/Content/Images/busy.gif" /> Creating Account' });

        $.ajax({
            type: "POST",
            url: "DoSignup.castle",
            data: ({
                companyName: companyName,
                subdomain: subdomain,
                firstName: firstName,
                lastName: lastName,
                jobTitle: $("#jobTitle").val(),
                email: email,
                password: password1
            }),
            success: function(result) {
                if (result.substr(0, 2) == "OK") {
                    $("#LoginEmail").val($("#email").val());
                    $("#LoginPassword").val($("#password1").val());
                    $("#Login").attr("action", result.substr(3));
                    $("#Login").submit();
                }
                else {
                    alert(result);
                    $.unblockUI();
                }
            },
            error: function() {
                $.unblockUI();
                alert("Can not communicate with server, please try again later");
            }
        });

        return false;
    });

    $("#subdomain").blur(function() {
        var subdomainReg = /^([qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-_]+)$/;
        var subdomain = $("#subdomain").val();
        if (subdomain == "") {
            return true;
        }
        if (!subdomainReg.test(subdomain)) {
            return true;
        }
        $("#subdomainLabel").hide();
        $("#subdomainOk").hide();
        $("#subdomainTaken").hide();
        $("#subdomainCheck").show();
        $('#subdomain').attr("disabled", true);

        $.ajax({
            type: "GET",
            url: "IsSubdomainAvailable.castle",
            data: ({ subdomain: subdomain }),
            success: function(result) {
                $("#subdomainCheck").hide();
                $('#subdomain').removeAttr("disabled");
                if (result == "OK") {
                    $("#subdomainOk").show();
                }
                else {
                    $("#subdomainTaken").show();
                }
            },
            error: function() {
                $("#subdomainCheck").hide();
                $('#subdomain').removeAttr("disabled");
                $("#subdomainLabel").show();
            }
        });
    });
});