﻿$(document).ready(function () {

    var userNameOk = false;
    var emailAddressOk = false;

    // hide the dialog box 
    $("#CreateUserFormSubmitted").hide();


    // Member Username
    // Returns a boolean to say weather the username is ok
    function CheckUserNameDoesNotExist(username) {        
        userNameOk = false;
        $.ajax({
            type: "GET",
            url: "/Member/CheckUserName/" + username,
            success: function (data) {
                $.each(data, function (i, item) {
                    // if the username is available
                    if (item == "True") {
                        userNameOk = true;
                        //alert("TRUE " + " username " + username + " " + "userNameOk " + userNameOk + " item " + item);
                    }
                    else {
                        userNameOk = false;
                        //alert("FALSE " + " username " + username + " " + "userNameOk " + userNameOk + " item " + item);
                    }
                });
            },
            error: null,
            dataType: "json",
            async: false,
            cache: false
        });
    }

    // Edit Member Username
    // Returns a boolean to say weather the new username is ok or not
    // ie, is it already used by an agency or escort
    function CheckUsernameCanChangeToThis(NewUsername) {
        userNameOk = false;
        $.ajax({
            type: "GET",
            url: "/Member/CheckUsernameCanChangeToThis/" + NewUsername,
            success: function (data) {
                $.each(data, function (i, item) {
                    // if the username is available
                    if (item == "True") {
                        userNameOk = true;
                        //alert("TRUE " + " username " + username + " " + "userNameOk " + userNameOk + " item " + item);
                    }
                    else {
                        userNameOk = false;
                        //alert("FALSE " + " username " + username + " " + "userNameOk " + userNameOk + " item " + item);
                    }
                });
            },
            error: null,
            dataType: "json",
            async: false,
            cache: false
        });
    }


    // Member Email Address
    // Returns a boolean to say weather the email address is ok
    function CheckEmailAddressIsUnique(emailAddress) {
        emailAddressOk = false;
        $.ajax({
            type: "GET",
            url: "/Member/CheckEmailAddressIsUnique/" + emailAddress,
            success: function (data) {
                $.each(data, function (i, item) {
                    // if the email address us unique
                    if (item == "True") {
                        emailAddressOk = true;
                    }
                    else {
                        emailAddressOk = false;
                    }
                });
            },
            error: null,
            dataType: "json",
            async: false,
            cache: false
        });
    }



    // when the page loads, hide the FormSubmitionErrorText, and kill any text
    $("#FormSubmitionErrorText").hide();
    $("#ErrorTitle").hide();
    $("#FormSubmitionErrorText").text("");

    // hide both the tick and the cross for email validation
    $("#tickImage").hide();
    $("#crossImage").hide();


    // ********************************************
    // *** Edit member script ***************
    // ********************************************
    // when the the create button is clicked...    
    $("#Edit-user-button").click(function () {
        $("#FormSubmitionErrorText").text("");

        // 1: first check all the fields are ok
        var my_form = $('#edit-form');
        var NullFields = [];
        var CannotBeNull = [];
        var IsValid = true;
        var ErrorString = "";

        $('select, input', my_form).each(function () {
            // check if the input type is not checkbox, we dont care for checkboxes
            if ($(this).attr('type') != "checkbox") {
                var name = $(this).attr('name');
                var ID = $(this).attr('id');
                var val = $(this).val();

                if ($("#" + name).hasClass("redInputBoxText")) {
                    //console.log(($("#" + name).val() + " it has this class"));
                }

                switch (ID) {
                    case "UserName":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You must enter a User Name. " + "<br/>";
                            IsValid = false;
                        }
                        else {                            
                            CheckUsernameCanChangeToThis($("#" + name).val());
                            if (userNameOk == false) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* This username is already taken, please select another " + "<br/>";

                                // add the RederrorText css class and the error text
                                $("#UserNameCheckText").show();
                                $("#UserNameCheckText").removeAttr('class');
                                $("#UserNameCheckText").addClass('UserNameExistsText');
                                $("#UserName").addClass('redInputBoxText');
                                $("#UserNameCheckText").text(ErrorString);

                                IsValid = false;
                            }
                            else {
                                $("#UserNameCheckText").hide();
                            }
                        }
                        break;
                    case "Email":
                        // initially remove the RedInputBox class
                        $("#" + name).removeClass("redInputBoxText");

                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your email address. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            ErrorString += "* Enter a valid email address. " + "<br/>";
                            IsValid = false;
                        }

                        break;
                    case "Name":
                        if (IsTextString($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You cannot have numbers in your name. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter a name. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Password":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to select a password. " + "<br/>";
                            IsValid = false;
                        }
                        else {
                            $("#" + name).removeClass("redInputBoxText");
                        }
                        break;
                    case "Contact_Number_1":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your Contact Number 1. " + "<br/>";
                            IsValid = false;
                        }
                        if (IsNumeric($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Contact Number 1 Cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        // now check to see if the number is to long
                        if ($("#" + name).val().length > 20) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Contact Number 1 Cannot Contain more than 20 digits. " + "<br/>";
                            IsValid = false;
                        }
                        // now check to see if the number is to short
                        if ($("#" + name).val().length < 9) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Contact Number 1 Cannot be less than 9 digits. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    case "Age":
                        if (IsNumeric($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Age can only consist of numbers. " + "<br/>";
                            IsValid = false;
                        }
                        if (IsAgeOver18($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You must be over 18 to become an Escort. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your Age. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    case "PostCode":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your poscode. " + "<br/>";
                            IsValid = false;
                        }
                        // check to see if the "Invalid Postcode, Please Try Again"
                        if ($("#PostCodeErrorCheck").is(":visible")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter a valid poscode. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // Incall Price Boxes                              
                    case "Half_Hour_Incall":
                        if ($("#" + name).length > 0) {
                            if ($("#" + name).hasClass("redInputBoxText")) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* Your Half hour incall price cannot contain letters. " + "<br/>";
                                IsValid = false;
                            }
                        }
                        break;
                    case "1_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 1 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "2_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 2 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "3_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 3 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "4_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 4 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Overnight_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Overnight incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // Outcall Price Boxes                               
                    case "1_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 1 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "2_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 2 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "3_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 3 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "4_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 4 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Overnight_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Overnight Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                }
            }
        });

        if (IsValid == false) {
            $("#FormSubmitionErrorText").show();
            $("#FormSubmitionErrorText").append(ErrorString);
            $("#FormSubmitionErrorText").addClass("RederrorText");
            return false;
        }
        else {
            // the form is ok, submit the form - but show a dialog to distract the user from interrupting it
            //$("#CreateUserFormSubmitted").hide();
            //ShowFormSubmittedDialog();
            $("#FormSubmittedOKText").show();
            $("#FormSubmittedOKText").append("Your changes were updated successfully");
            $("#FormSubmittedOKText").addClass("greenText");

            // TODO get validation from the email address
            $("#edit-form").submit();
        }
    });


    // ********************************************
    // *** Create new member script ***************
    // ********************************************
    // when the the create button is clicked...    
    $("#create-user-button").click(function () {
        $("#FormSubmitionErrorText").text("");

        // 1: first check all the fields are ok
        var my_form = $('#create-form');
        var NullFields = [];
        var CannotBeNull = [];
        var IsValid = true;
        var ErrorString = "";

        $('select, input', my_form).each(function () {
            // check if the input type is not checkbox, we dont care for checkboxes
            if ($(this).attr('type') != "checkbox") {
                var name = $(this).attr('name');
                var ID = $(this).attr('id');
                var val = $(this).val();

                if ($("#" + name).hasClass("redInputBoxText")) {
                    //console.log(($("#" + name).val() + " it has this class"));
                }

                switch (name) {
                    case "UserName":
                        if ($("#" + name).val().length == 0) {                        
                            $("#" + name).addClass('redInputBoxText');    
                            ErrorString += "* You must enter a User Name. " + "<br/>";                            
                            IsValid = false;
                        }
                        else if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* This username is already taken. " + "<br/>";
                            IsValid = false;
                        }
                        else {
                            CheckUserNameDoesNotExist($("#" + name).val());
                            if (userNameOk == false) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* This username is already taken, please select another " + "<br/>";

                                // add the RederrorText css class and the error text
                                $("#UserNameCheckText").show();
                                $("#UserNameCheckText").removeAttr('class');
                                $("#UserNameCheckText").addClass('UserNameExistsText');
                                $("#UserName").addClass('redInputBoxText');
                                $("#UserNameCheckText").text("This username is already taken, please try a different name");

                                IsValid = false;
                            }
                            else {
                                $("#UserNameCheckText").hide();
                            }
                        }
                        break;
                    case "Email":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your email address. " + "<br/>";
                            IsValid = false;
                        }
                        // check that the email input boxes match
                        if ($("#" + name).val() != $("#ConfirmEmail").val()) {
                            $("#" + name).removeClass("greenInputBoxText");
                            $("#ConfirmEmail").removeClass("greenInputBoxText");
                            $("#" + name).addClass("redInputBoxText");
                            $("#ConfirmEmail").addClass("redInputBoxText");
                            ErrorString += "** The email address's must match ** " + "<br/>";
                            IsValid = false;
                        }
                        // if the two email fields do match
                        // then remove the red class and add the green one
                        else if($("#" + name).val() == $("#ConfirmEmail").val())
                        {
                            // both have to have some data
                            if(($("#" + name).val() != "" && $("#ConfirmEmail").val() != ""))
                            {
                                $("#ConfirmEmail").removeClass("redInputBoxText");
                                $("#ConfirmEmail").addClass("greenInputBoxText");
                                $("#Email").removeClass("redInputBoxText");
                                $("#Email").addClass("greenInputBoxText");
                            }
                        }
                        else if ($("#" + name).hasClass("redInputBoxText")) {
                            ErrorString += "* Enter a valid email address. " + "<br/>";
                            IsValid = false;
                        }
                        else {
                            CheckEmailAddressIsUnique($("#" + name).val());
                            if (emailAddressOk == false) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* This email address is already taken, have you registered before ? " + "<br/>";
                                IsValid = false;
                                // email address already exists - set the box to red
                                $("#Email").removeClass("greenInputBoxText");
                                $("#Email").addClass("redInputBoxText");
                                $("#crossImage").show();
                                $("#tickImage").hide();
                            }
                            else {
                                // the email is ok
                                $("#Email").addClass("greenInputBoxText");
                                $("#tickImage").show();
                                $("#crossImage").hide();
                            }
                        }
                        break;
                    case "ConfirmEmail":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to confirm your Email address. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            ErrorString += "* Enter a valid Email address. " + "<br/>";
                            IsValid = false;
                        }

                        /*
                        // check that the email input boxes match
                        if ($("#" + name).val() != $("#Email").val()) {
                            $("#" + name).addClass("redInputBoxText");
                            $("#Email").addClass("redInputBoxText");
                            ErrorString += "* The Email address must match " + "<br/>";
                            IsValid = false;
                        }
                        */
                        break;
                    case "Name":
                        if (IsTextString($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You cannot have numbers in your name. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter a name. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Password":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to select a password. " + "<br/>";
                            IsValid = false;
                        }
                        else {
                            $("#" + name).removeClass("redInputBoxText");
                        }
                        break;
                    case "Contact_Number_1":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your Contact Number 1. " + "<br/>";
                            IsValid = false;
                        }
                        if (IsNumeric($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Contact Number 1 Cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        // now check to see if the number is to long
                        if ($("#" + name).val().length > 20) {
                            ErrorString += "* Contact Number 1 Cannot Contain more than 20 digits. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // dont need to worry about Contact_Number_2                                                  
                    case "Contact_Number_2":
                        /*
                            if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your Contact Number 2. " + "<br/>";
                            IsValid = false;
                        }
                        */
                        if (IsNumeric($("#" + name).val()) == false) {
                            ErrorString += "* Contact Number 2 Cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        // now check to see if the number is to long
                        if ($("#" + name).val().length > 20) {
                            ErrorString += "* Contact Number 2 Cannot Contain more than 20 digits. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    case "Age":
                        if (IsNumeric($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Age can only consist of numbers. " + "<br/>";
                            IsValid = false;
                        }
                        if (IsAgeOver18($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You must be over 18 to become an Escort. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your Age. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCode":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your poscode. " + "<br/>";
                            IsValid = false;
                        }
                        // check to see if the "Invalid Postcode, Please Try Again"
                        if ($("#PostCodeErrorCheck").is(":visible")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter a valid poscode. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCodeRegionTextBox":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Enter your postcode and click 'Find' this will add your 'Region' for you. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCodeCountyTextBox":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Enter your postcode and click 'Find' this will add your 'County' for you. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCodeTownTextBox":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Enter your postcode and click 'Find' this will add your 'Town' for you. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // Incall Price Boxes                             
                    case "Half_Hour_Incall":
                        if ($("#" + name).length > 0) {
                            if ($("#" + name).hasClass("redInputBoxText")) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* Your Half hour incall price cannot contain letters. " + "<br/>";
                                IsValid = false;
                            }
                        }
                        break;
                    case "1_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 1 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "2_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 2 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "3_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 3 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "4_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 4 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Overnight_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Overnight incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // Outcall Price Boxes                              
                    case "1_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 1 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "2_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 2 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "3_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 3 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "4_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 4 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Overnight_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Overnight Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                }
            }
        });

        if (IsValid == false) {
            //console.log("Valid is false");
            $("#FormSubmitionErrorText").show();
            $("#FormSubmitionErrorText").append(ErrorString);
            $("#FormSubmitionErrorText").addClass("RederrorText");
            return false;
        }
        else {

            // the form is ok, submit the form - but show a dialog to distract the user from interrupting it
            $("#CreateUserFormSubmitted").hide();
            ShowFormSubmittedDialog();

            // TODO get validation from the email address
            $("#create-form").submit();
        }
    });


    // ***********************************************
    // *** Create new Agency Escort script ***********
    // ***********************************************
    // when the the create button is clicked...    
    $("#create-Agency-Escort-button").click(function () {
        $("#FormSubmitionErrorText").text("");

        // 1: first check all the fields are ok
        var my_form = $('#create-Agency-Escort-form');
        var NullFields = [];
        var CannotBeNull = [];
        var IsValid = true;
        var ErrorString = "";

        $('select, input', my_form).each(function () {
            // check if the input type is not checkbox, we dont care for checkboxes
            if ($(this).attr('type') != "checkbox") {
                var name = $(this).attr('name');
                var ID = $(this).attr('id');
                var val = $(this).val();

                if ($("#" + name).hasClass("redInputBoxText")) {
                    //console.log(($("#" + name).val() + " it has this class"));
                }

                switch (name) {
                    case "UserName":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You must enter a User Name. " + "<br/>";
                            IsValid = false;
                        }
                        else if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* This username is already taken. " + "<br/>";
                            IsValid = false;
                        }
                        else {
                            CheckUserNameDoesNotExist($("#" + name).val());
                            if (userNameOk == false) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* This username is already taken, please select another " + "<br/>";

                                // add the RederrorText css class and the error text
                                $("#UserNameCheckText").show();
                                $("#UserNameCheckText").removeAttr('class');
                                $("#UserNameCheckText").addClass('UserNameExistsText');
                                $("#UserName").addClass('redInputBoxText');
                                $("#UserNameCheckText").text("This username is already taken, please try a different name");

                                IsValid = false;
                            }
                            else {
                                $("#UserNameCheckText").hide();
                            }
                        }
                        break;
                    case "Name":
                        if (IsTextString($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You cannot have numbers in your name. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter a name. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Password":
                        if ($("#" + name).val().length == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to select a password. " + "<br/>";
                            IsValid = false;
                        }
                        else {
                            $("#" + name).removeClass("redInputBoxText");
                        }
                        break;
                    case "Age":
                        if (IsNumeric($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Age can only consist of numbers. " + "<br/>";
                            IsValid = false;
                        }
                        if (IsAgeOver18($("#" + name).val()) == false) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You must be over 18 to become an Escort. " + "<br/>";
                            IsValid = false;
                        }
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your Age. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCode":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter your poscode. " + "<br/>";
                            IsValid = false;
                        }
                        // check to see if the "Invalid Postcode, Please Try Again"
                        if ($("#PostCodeErrorCheck").is(":visible")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* You need to enter a valid poscode. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCodeRegionTextBox":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Enter your postcode and click 'Find' this will add your 'Region' for you. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCodeCountyTextBox":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Enter your postcode and click 'Find' this will add your 'County' for you. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "PostCodeTownTextBox":
                        if ($("#" + name).val() == 0) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Enter your postcode and click 'Find' this will add your 'Town' for you. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // Incall Price Boxes                              
                    case "Half_Hour_Incall":
                        if ($("#" + name).length > 0) {
                            if ($("#" + name).hasClass("redInputBoxText")) {
                                $("#" + name).addClass("redInputBoxText");
                                ErrorString += "* Your Half hour incall price cannot contain letters. " + "<br/>";
                                IsValid = false;
                            }
                        }
                        break;
                    case "1_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 1 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "2_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 2 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "3_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 3 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "4_Hour_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 4 hour incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Overnight_Incall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Overnight incall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;

                    // Outcall Price Boxes                               
                    case "1_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 1 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "2_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 2 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "3_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 3 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "4_Hour_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your 4 hour Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                    case "Overnight_Outcall":
                        if ($("#" + name).hasClass("redInputBoxText")) {
                            $("#" + name).addClass("redInputBoxText");
                            ErrorString += "* Your Overnight Outcall price cannot contain letters. " + "<br/>";
                            IsValid = false;
                        }
                        break;
                }
            }
        });

        if (IsValid == false) {
            //console.log("Valid is false");
            $("#FormSubmitionErrorText").show();
            $("#FormSubmitionErrorText").append(ErrorString);
            $("#FormSubmitionErrorText").addClass("RederrorText");
            return false;
        }
        else {

            // the form is ok, submit the form - but show a dialog to distract the user from interrupting it
            $("#CreateUserFormSubmitted").hide();
            ShowFormSubmittedDialog();

            // TODO get validation from the email address
            $("#create-Agency-Escort-form").submit();
        }
    });


    // show the dialog form when the form is being submitted
    function ShowFormSubmittedDialog() {
        $("#CreateUserFormSubmitted").dialog({
            modal: true
        });
    }

    // needs combining into one function
    $("#UserName").keyup(function () {
        if (DoesStringContainInvalidChars($("#UserName").val()) == true) {
            $("#UserNameCheckText").text("Invalid characters in your username");
            $("#UserNameCheckText").addClass('RederrorText');
            $("#UserNameCheckText").show()
            $("#UserName").addClass("redInputBoxText");
        }
        else {
            $("#UserNameCheckText").hide();
            $("#UserName").removeClass("redInputBoxText");
            $("#UserNameCheckText").removeClass('RederrorText');
        }
    });
    $("#UserName").blur(function () {
        if (DoesStringContainInvalidChars($("#UserName").val()) == true) {
            $("#UserNameCheckText").text("Invalid characters in your username");
            $("#UserNameCheckText").addClass('RederrorText');
            $("#UserNameCheckText").show()
            $("#UserName").addClass("redInputBoxText");
        }
        else {
            $("#UserNameCheckText").hide();
            $("#UserName").removeClass("redInputBoxText");
            $("#UserNameCheckText").removeClass('RederrorText');
        }
    });

    // ***********************************
    // *** New Member Valiation ***
    // ***********************************
    // Contact_Number_1
    $("#Contact_Number_1").keyup(function () { TestForInvalidCharacters("#Contact_Number_1", "#ContactNumber1Error", "You cannot have letters in phone numbers", "letters") });
    $("#Contact_Number_1").blur(function () { TestForInvalidCharacters("#Contact_Number_1", "#ContactNumber1Error", "You cannot have letters in phone numbers", "letters") });

    // check for the contact number being too long
    //$("#Contact_Number_1").keyup(function () { CheckThatTheContactNumberIsNotTooLong("#Contact_Number_1", "#ContactNumber1Error", "Phone Numbers cannot have more than 20 digits") });
    //$("#Contact_Number_1").blur(function () { CheckThatTheContactNumberIsNotTooLong("#Contact_Number_1", "#ContactNumber1Error", "Phone Numbers cannot have more than 20 digits") }); 

    // Contact_Number_2
    $("#Contact_Number_2").keyup(function () { TestForInvalidCharacters("#Contact_Number_2", "#ContactNumber2Error", "You cannot have letters in phone numbers", "letters") });
    $("#Contact_Number_2").blur(function () { TestForInvalidCharacters("#Contact_Number_2", "#ContactNumber2Error", "You cannot have letters in phone numbers", "letters") });

    // check for the contact number being too long
    //$("#Contact_Number_2").keyup(function () { CheckThatTheContactNumberIsNotTooLong("#Contact_Number_2", "#ContactNumber2Error", "Phone Numbers cannot have more than 20 digits") });
    //$("#Contact_Number_2").blur(function () { CheckThatTheContactNumberIsNotTooLong("#Contact_Number_2", "#ContactNumber2Error", "Phone Numbers cannot have more than 20 digits") }); 

    // Age
    $("#Age").keyup(function () { TestForInvalidCharacters("#Age", "#AgeError", "You cannot have letters in your age", "letters") });
    $("#Age").blur(function () { TestForInvalidCharacters("#Age", "#AgeError", "You cannot have letters in your age", "letters") });

    // Age validation - needs to be >= 18
    $("#Age").keyup(function () { AgeCheck("#Age", "#AgeError", "You must be 18 or over to Become an Escort") });
    $("#Age").blur(function () { AgeCheck("#Age", "#AgeError", "You must be 18 or over to Become an Escort") });

    // Name
    $("#Name").keyup(function () { TestForInvalidCharacters("#Name", "#NameErrorText", "You cannot have numbers in your name", "numbers") });
    $("#Name").blur(function () { TestForInvalidCharacters("#Name", "#NameErrorText", "You cannot have numbers in your name", "numbers") });

    // test for Email - not doing anymore due to confrimation email box - 12/03/2011
    //$("#Email").keydown(EmailRegEX);
    //$("#Email").blur(EmailRegEX);

    //////////////////////////
    ///Incall price validation
    //////////////////////////
    // test the price fields for letters
    $("#Half_Hour_Incall").keyup(function () { TestForInvalidCharacters("#Half_Hour_Incall", "#Half_Hour_IncallError", "You cannot have letters in your Half Hour Incall price", "letters") });
    $("#Half_Hour_Incall").blur(function () { TestForInvalidCharacters("#Half_Hour_Incall", "#Half_Hour_IncallError", "You cannot have letters in your Half Hour Incall price", "letters") });

    $("#1_Hour_Incall").keyup(function () { TestForInvalidCharacters("#1_Hour_Incall", "#1_Hour_IncallError", "You cannot have letters in your 1 Hour Incall price", "letters") });
    $("#1_Hour_Incall").blur(function () { TestForInvalidCharacters("#1_Hour_Incall", "#1_Hour_IncallError", "You cannot have letters in your 1 Hour Incall price", "letters") });

    $("#2_Hour_Incall").keyup(function () { TestForInvalidCharacters("#2_Hour_Incall", "#2_Hour_IncallError", "You cannot have letters in your 2 Hour Incall price", "letters") });
    $("#2_Hour_Incall").blur(function () { TestForInvalidCharacters("#2_Hour_Incall", "#2_Hour_IncallError", "You cannot have letters in your 2 Hour Incall price", "letters") });

    $("#3_Hour_Incall").keyup(function () { TestForInvalidCharacters("#3_Hour_Incall", "#3_Hour_IncallError", "You cannot have letters in your 3 Hour Incall price", "letters") });
    $("#3_Hour_Incall").blur(function () { TestForInvalidCharacters("#3_Hour_Incall", "#3_Hour_IncallError", "You cannot have letters in your 3 Hour Incall price", "letters") });

    $("#4_Hour_Incall").keyup(function () { TestForInvalidCharacters("#4_Hour_Incall", "#4_Hour_IncallError", "You cannot have letters in your 4 Hour Incall price", "letters") });
    $("#4_Hour_Incall").blur(function () { TestForInvalidCharacters("#4_Hour_Incall", "#4_Hour_IncallError", "You cannot have letters in your 4 Hour Incall price", "letters") });

    $("#Overnight_Incall").keyup(function () { TestForInvalidCharacters("#Overnight_Incall", "#Overnight_IncallError", "You cannot have letters in your overnight Incall price", "letters") });
    $("#Overnight_Incall").blur(function () { TestForInvalidCharacters("#Overnight_Incall", "#Overnight_IncallError", "You cannot have letters in your overnight Incall price", "letters") });

    ////////////////////////////
    ///Outcall price validation
    ////////////////////////////
    // test the price fields for letters
    $("#1_Hour_Outcall").keyup(function () { TestForInvalidCharacters("#1_Hour_Outcall", "#1_Hour_OutcallError", "You cannot have letters in your 1 Hour Outcall price", "letters") });
    $("#1_Hour_Outcall").blur(function () { TestForInvalidCharacters("#1_Hour_Outcall", "#1_Hour_OutcallError", "You cannot have letters in your 1 Hour Outcall price", "letters") });

    $("#2_Hour_Outcall").keyup(function () { TestForInvalidCharacters("#2_Hour_Outcall", "#2_Hour_OutcallError", "You cannot have letters in your 2 Hour Outcall price", "letters") });
    $("#2_Hour_Outcall").blur(function () { TestForInvalidCharacters("#2_Hour_Outcall", "#2_Hour_OutcallError", "You cannot have letters in your 2 Hour Outcall price", "letters") });

    $("#3_Hour_Outcall").keyup(function () { TestForInvalidCharacters("#3_Hour_Outcall", "#3_Hour_OutcallError", "You cannot have letters in your 3 Hour Outcall price", "letters") });
    $("#3_Hour_Outcall").blur(function () { TestForInvalidCharacters("#3_Hour_Outcall", "#3_Hour_OutcallError", "You cannot have letters in your 3 Hour Outcall price", "letters") });

    $("#4_Hour_Outcall").keyup(function () { TestForInvalidCharacters("#4_Hour_Outcall", "#4_Hour_OutcallError", "You cannot have letters in your 4 Hour Outcall price", "letters") });
    $("#4_Hour_Outcall").blur(function () { TestForInvalidCharacters("#4_Hour_Outcall", "#4_Hour_OutcallError", "You cannot have letters in your 4 Hour Outcall price", "letters") });

    $("#Overnight_Outcall").keyup(function () { TestForInvalidCharacters("#Overnight_Outcall", "#Overnight_OutcallError", "You cannot have letters in your overnight Incall price", "letters") });
    $("#Overnight_Outcall").blur(function () { TestForInvalidCharacters("#Overnight_Outcall", "#Overnight_OutcallError", "You cannot have letters in your overnight Incall price", "letters") });

    function EmailRegEX() {
        // email reg ex
        var ck_email = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);

        // remove any existing CSS class - but add the FormInputBox
        $("#Email").removeAttr('class');
        $("#Email").addClass('FormInputBox');

        if (!ck_email.test($("#Email").val())) {
            $("#Email").addClass("redInputBoxText");
            $("#crossImage").show();
            $("#tickImage").hide();
        }
        else {
            $("#Email").addClass("greenInputBoxText");
            $("#tickImage").show();
            $("#crossImage").hide();
        }
    }
});

 function IsNumeric(sText) {
     var ValidChars = "0123456789. ";
     var IsNumber = true;
     var Char;

     for (i = 0; i < sText.length && IsNumber == true; i++) {
         Char = sText.charAt(i);
         if (ValidChars.indexOf(Char) == -1) {
             IsNumber = false;
         }
     }
     return IsNumber;
 }

 function DoesStringContainInvalidChars(sText) {
     var InvalidChars = ".,?<{}:@~[]*+;'#¬`|!£$%^&*()_+=-//\\";
     var IsOK = false;
     var Char;

     for (i = 0; i < sText.length; i++) {
         Char = sText.charAt(i);         
         if (InvalidChars.indexOf(Char) != -1) {
             IsOK = true;
             return IsOK;
         }
     }
     return IsOK;
 }

 function IsTextString(sText) {
     sText = sText.toLowerCase();
     var ValidChars = "abcdefghijklmnopqrstuvwxyz- ";
     var IsText = true;
     var Char;

     for (i = 0; i < sText.length && IsText == true; i++) {
         Char = sText.charAt(i);
         if (ValidChars.indexOf(Char) == -1) {
             IsText = false;
         }
     }
     return IsText;
 }

 function CheckThatTheContactNumberIsNotTooLong(ControlID, errorLabelID, errorText) 
 {
     if ($(ControlID).val().length > 20) {
         $(errorLabelID).text(errorText);
         $(ControlID).addClass("redInputBoxText");
     }
     else {
         $(errorLabelID).text("");
         $(ControlID).removeClass("redInputBoxText");
     }
 }

 // test to check the age is >= 18
 function AgeCheck(ControlID, errorLabelID, errorText)
 {
     if ( ($(ControlID).val() < 18) && ($(ControlID).val().length > 0) )
     {
         $(errorLabelID).text(errorText);
         $(ControlID).addClass("redInputBoxText");
     }
 }

 function IsAgeOver18(value) {
     if (value < 18) {
         return false;
     }
     else {
         return true;
     }
 }

 // function to test for invalid characters, such as letters and numbers
 function TestForInvalidCharacters(ControlID, errorLabelID, ErrorText, letterType) {

     // get the text of the text box
     ControlTextValue = $(ControlID).val();
     var character = null;

     // we can test for invalid characters being letters or numbers
     if (letterType == "letters") {
         var i = 0;
         while (i <= (ControlTextValue.length - 1)) {

             character = ControlTextValue.charAt(i);

             if (isNaN(character) == true) {
                 $(errorLabelID).text(ErrorText);
                 $(ControlID).addClass("redInputBoxText");
                 return false;
                 break;
             }
             else if (isNaN(character) == false) {
                 $(errorLabelID).text("");
                 $(ControlID).removeClass("redInputBoxText");
             }
             i++;
         }
     }
     // if were testing for numbers
     if (letterType == "numbers") {
         var j = 0;
         while (j <= (ControlTextValue.length - 1)) {

             character = ControlTextValue.charAt(j);

             if (IsNumeric(character) == true && character != ' ') {
                 $(errorLabelID).text(ErrorText);
                 $(ControlID).addClass("redInputBoxText");
                 return false;
                 break;
             }
             else if (IsNumeric(character) == false) {
                 $(errorLabelID).text("");
                 $(ControlID).removeClass("redInputBoxText");
             }
             j++;
         }
     }
 }
