﻿$(document).ready(function () {


    // when the page loads hide the div that surrounds the region text boxes and the error message
    $("#regionDataContainer").hide();
    $("#PostCodeErrorCheck").hide();

    $("#regionTextBoxContainer").hide();

    $("#UserNameCheckText").hide();

    // when the user starts entering text on the postcode input box
    $("#PostCode").keyup(function () {
        // when the user enters a new postcode, hide the container
        $("#regionTextBoxContainer").hide();
    });

    // function to show the escort options
    // 1 = escort, 2 = Client
    $("#ClientRadioButton").click(function () {
        // if the user selects Client, hide the Div that surrounds the escort only options        
        $("#escortOnlyOprions").hide();

        // also hide the escort calling options as this has nothing to do with the a client
        $("#EscortCallingFieldSet").hide();
    });
    $("#EscortRadioButton").click(function () {
        // if the user selects Escort, show the Div that surrounds the escort only options
        $("#escortOnlyOprions").show();
        // also show the escort calling options
        $("#EscortCallingFieldSet").show();
    });


    ///
    /// check user name function
    ///
    $("#check-button").click(function () {

        // when the check username button is clicked, we need to hide the message
        // and remove any css classes and text
        $("#UserNameCheckText").hide();
        $("#UserNameCheckText").removeClass('GreenOKText');
        $("#UserNameCheckText").removeClass('RederrorText');
        $("#UserNameCheckText").text("");

        // set the username value and get the JSON data
        var username = $("#UserName").val();

        if (username.length == 0 || username.length < 5) {
            // the user name is too short, return and show an error message
            $("#UserNameCheckText").show();
            $("#UserNameCheckText").removeAttr('class');
            $("#UserName").addClass('redInputBoxText');
            $("#UserNameCheckText").addClass('RederrorText');
            $("#UserNameCheckText").append("Please make sure your username has at least 5 letters");
            return;
        }

        // trim any white space
        username = jQuery.trim(username);

        $.ajax({
            type: "GET",
            url: "/Member/CheckUserName/" + username,
            success: function (data) {
                $.each(data, function (i, item) {

                    // if the username is available
                    if (item == "True") {
                        // add the GreenOKText css class and the ok text
                        $("#UserNameCheckText").show();
                        $("#UserNameCheckText").removeAttr('class');
                        $("#UserName").removeClass('redInputBoxText');
                        $("#UserNameCheckText").addClass('UserNameOKText');
                        $("#UserNameCheckText").append("This Username Available");
                        return true;
                    }
                    else {
                        // 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");
                        return true;
                    }
                });
            },
            error: null,
            dataType: "json",
            cache: false
        });
    });


    ///
    /// set regional data by postcode function
    ///
    $("#find-button").click(function () {


        // set the postcode value and get the JSON data
        var postcode = $("#PostCode").val();

        // trim any white space
        postcode = jQuery.trim(postcode);

        $.ajax({
            type: "GET",
            url: "/Member/PostcodeRegionData/" + postcode,
            success: function (data) {
                $("#PostCodeErrorCheck").hide();

                // if the number returened is 0 hide the feedback table            
                if (data == null) {
                    $("#PostCodeErrorCheck").show();
                    $("#regionTextBoxContainer").hide();
                    return false;
                }

                $.each(data, function (i, item) {

                    // iteration 0 = RegionDropDown
                    // iteration 1 = CountyDropDown
                    // iteration 2 = TownDropDown

                    // now were getting the data, populate the region dropdowns
                    // were checking the classes so if the red box class has been applied the it will be removed when find in clicked
                    switch (i) {
                        case 0:
                            $("#PostCodeRegionTextBox").val(item.Name);
                            $("#PostCode").removeClass("redInputBoxText");
                            $("#PostCodeRegionTextBox").removeClass("redInputBoxText");
                            // show the region text boxes
                            $("#regionTextBoxContainer").show();
                            break;
                        case 1:
                            $("#PostCodeCountyTextBox").val(item.Name);
                            $("#PostCodeCountyTextBox").removeClass("redInputBoxText");
                            break;
                        case 2:
                            $("#PostCodeTownTextBox").val(item.Name);
                            $("#PostCodeTownTextBox").removeClass("redInputBoxText");
                            break;
                    }
                });
            },
            error: function () {
                $("#PostCodeErrorCheck").show();
                $("#regionTextBoxContainer").hide();
            },
            dataType: "json",
            async: false,
            cache: false
        });

    });

});


function HideTheRelevantRegisterOptions() {
    // function to show the escort options
    // 1 = escort, 2 = Client - checked="checked"
    if ($("#ClientRadioButton").attr("checked") == true) 
    {
        // if the user selects Client, hide the Div that surrounds the escort only options        
        $("#escortOnlyOprions").hide();
        // also hide the escort calling options as this has nothing to do with the a client
        $("#EscortCallingFieldSet").hide();
    }

    if ($("#EscortRadioButton").attr("checked") == true) 
    {
        // if the user selects Escort, show the Div that surrounds the escort only options
        $("#escortOnlyOprions").show();
        // also show the escort calling options
        $("#EscortCallingFieldSet").show();
    }
}


function disableEnterKey(e) {
    var key;

    if (window.event)
        key = window.event.keyCode;   // IE
    else                              // firefox
        key = e.which;                

    if (key == 13)
        return false;
    else
        return true;
}


// Initializes a new instance of the StringBuilder class

// and appends the given value if supplied

function StringBuilder(value) {
    this.strings = new Array("");
    this.append(value);
}

// Appends the given value to the end of this instance.

StringBuilder.prototype.append = function (value) {
    if (value) {
        this.strings.push(value);
    }
}

// Clears the string buffer

StringBuilder.prototype.clear = function () {
    this.strings.length = 1;
}

// Converts this instance to a String.

StringBuilder.prototype.toString = function () {
    return this.strings.join("");
}
