﻿$(document).ready(function () {

    // initially hide the login box and the sign out box
    $("#SignOut").hide();
    $("#SignIn").hide();

    // hover over an advert container - STANDARD   
    $(".advert-container-div").hover(
      function () {
          $(this).addClass("advert-container-div-hover");
      },
      function () {
          $(this).removeClass("advert-container-div-hover");
      }
    );

    /*
    // hover over an advert container - ENHANCED
    $(".Enhanced-advert-container-div").hover(
      function () {
          $(this).addClass("Enhanced-advert-container-div-hover");
      },
      function () {
          $(this).removeClass("Enhanced-advert-container-div-hover");
      }
    );
    */

    // when the user clicks the clearAllServices link, clear all the services
    $(".clearAllServices").click(function () {
        var my_form = $('.searchBar-ServicesDiv');
        $('select, input', my_form).each(function () {
            // check if the input type is a checkbox
            if ($(this).attr('type') == "checkbox") {
                if ($(this).is(':checked')) {
                    $(this).attr('checked', false);
                }
            }
        });
    });

    // when the user clicks the clearAllRatings link, clear all the Member Ratings
    $(".clearAllRatings").click(function () {
        var my_form = $('.searchBar-RatingsDiv');
        $('select, input', my_form).each(function () {
            // check if the input type is a checkbox
            if ($(this).attr('type') == "checkbox") {
                if ($(this).is(':checked')) {
                    $(this).attr('checked', false);
                }
            }
        });
    });

    // when the pink seach button is clicked clear the escort name field
    $("#MainSeachButton").hover(function () {
        $("#EscortName").val("");
    });

    // initially hide the error messages
    $("#PriceErrorMessageDiv").hide();
    $("#AgeErrorMessageDiv").hide();
    $("#MilesWithinErrorMessageDiv").hide();

    // ** serach bar age and price validation **
    //
    // - Price From
    $("#PriceFrom").keyup(function () {
        if (IsNumeric($("#PriceFrom").val()) == false) {
            $("#PriceFrom").addClass('redInputBoxText');
            $("#PriceErrorMessageDiv").show();
            $("#PriceErrorMessage").addClass("RederrorText");
            $("#PriceErrorMessage").text("Please enter only numbers");
        }
        else {
            // check to see if from is higher than to
            if (IsFromHigherThanTo($("#PriceFrom").val(), $("#PriceTo").val())) {
                $("#PriceErrorMessageDiv").show();
                $("#PriceErrorMessage").addClass("RederrorText");
                $("#PriceErrorMessage").text("Price From is higher than price to");

                // set both text boxes to red
                $("#PriceFrom").addClass('redInputBoxText');
                $("#PriceTo").addClass('redInputBoxText');
            }
            else {
                $("#PriceTo").removeClass('redInputBoxText');
                $("#PriceFrom").removeClass('redInputBoxText');
                $("#PriceErrorMessageDiv").hide();
            }

            // the from box is red, do the same check on the to box
            if (IsNumeric($("#PriceTo").val()) == false) {
                $("#PriceTo").addClass('redInputBoxText');
                $("#PriceErrorMessageDiv").show();
                $("#PriceErrorMessage").addClass("RederrorText");
                $("#PriceErrorMessage").text("Please enter only numbers");
            }

        }
    });
    //
    // - Price To
    $("#PriceTo").keyup(function () {
        if (IsNumeric($("#PriceTo").val()) == false) {
            $("#PriceTo").addClass('redInputBoxText');
            $("#PriceErrorMessageDiv").show();
            $("#PriceErrorMessage").addClass("RederrorText");
            $("#PriceErrorMessage").text("Please enter only numbers");
        }
        else {
            // check to see if from is higher than to
            if (IsFromHigherThanTo($("#PriceFrom").val(), $("#PriceTo").val())) {
                $("#PriceErrorMessageDiv").show();
                $("#PriceErrorMessage").addClass("RederrorText");
                $("#PriceErrorMessage").text("Price From is higher than price to");

                // set both text boxes to red
                $("#PriceFrom").addClass('redInputBoxText');
                $("#PriceTo").addClass('redInputBoxText');
            }
            else {
                $("#PriceFrom").removeClass('redInputBoxText');
                $("#PriceTo").removeClass('redInputBoxText');
                $("#PriceErrorMessageDiv").hide();
            }

            // the from box is red, do the same check on the to box
            if (IsNumeric($("#PriceFrom").val()) == false) {
                $("#PriceFrom").addClass('redInputBoxText');
                $("#PriceErrorMessageDiv").show();
                $("#PriceErrorMessage").addClass("RederrorText");
                $("#PriceErrorMessage").text("Please enter only numbers");
            }
        }
    });


    // ** serach age validation **
    //
    // - Age From
    $("#ageFrom").keyup(function () {
        if (IsNumeric($("#ageFrom").val()) == false) {
            $("#ageFrom").addClass('redInputBoxText');
            $("#AgeErrorMessageDiv").show();
            $("#ageErrorMessage").addClass("RederrorText");
            $("#ageErrorMessage").text("Please enter only numbers");
        }
        else {
            // initailly check that the age is 18 or higher
            if (IsAtLeast18($("#ageFrom").val())) {
                // check to see if from is higher than to
                if (IsFromHigherThanTo($("#ageFrom").val(), $("#ageTo").val())) {
                    $("#AgeErrorMessageDiv").show();
                    $("#ageErrorMessage").addClass("RederrorText");
                    $("#ageErrorMessage").text("age From is higher than age to");

                    // set both text boxes to red
                    $("#ageFrom").addClass('redInputBoxText');
                    $("#ageTo").addClass('redInputBoxText');
                }
                else {
                    $("#ageTo").removeClass('redInputBoxText');
                    $("#ageFrom").removeClass('redInputBoxText');
                    $("#AgeErrorMessageDiv").hide();
                }

                // the from box is red, do the same check on the to box
                if (IsNumeric($("#ageFrom").val()) == false) {
                    $("#ageTo").addClass('redInputBoxText');
                    $("#AgeErrorMessageDiv").show();
                    $("#ageErrorMessage").addClass("RederrorText");
                    $("#ageErrorMessage").text("Please enter only numbers");
                }
            }
            else {
                $("#ageTo").addClass('redInputBoxText');
                $("#AgeErrorMessageDiv").show();
                $("#ageErrorMessage").addClass("RederrorText");
                $("#ageErrorMessage").text("age needs to be at least 18");
                $("#ageFrom").addClass('redInputBoxText');
            }
        }
    });
    //
    // - Age To
    $("#ageTo").keyup(function () {
        if (IsNumeric($("#ageTo").val()) == false) {
            $("#ageTo").addClass('redInputBoxText');
            $("#AgeErrorMessageDiv").show();
            $("#ageErrorMessage").addClass("RederrorText");
            $("#ageErrorMessage").text("Please enter only numbers");
        }
        else {
            // check to see if from is higher than to
            if (IsFromHigherThanTo($("#ageFrom").val(), $("#ageTo").val())) {
                $("#AgeErrorMessageDiv").show();
                $("#ageErrorMessage").addClass("RederrorText");
                $("#ageErrorMessage").text("age From is higher than age to");

                // set both text boxes to red
                $("#ageFrom").addClass('redInputBoxText');
                $("#ageTo").addClass('redInputBoxText');
            }
            else {
                $("#ageFrom").removeClass('redInputBoxText');
                $("#ageTo").removeClass('redInputBoxText');
                $("#AgeErrorMessageDiv").hide();
            }

            // the from box is red, do the same check on the to box
            if (IsNumeric($("#ageFrom").val()) == false) {
                $("#ageFrom").addClass('redInputBoxText');
                $("#AgeErrorMessageDiv").show();
                $("#ageErrorMessage").addClass("RederrorText");
                $("#ageErrorMessage").text("Please enter only numbers");
            }
        }
    });

    //
    // - Miles within
    $("#MilesRadius").keyup(function () {
        if (IsNumeric($("#MilesRadius").val()) == false) {
            $("#MilesRadius").addClass('redInputBoxText');
            $("#MilesWithinErrorMessageDiv").show();
            $("#MilesWithinErrorMessage").addClass("RederrorText");
            $("#MilesWithinErrorMessage").text("Please enter only numbers");
        }
        else {
            $("#MilesRadius").removeClass('redInputBoxText');
            $("#MilesWithinErrorMessageDiv").hide();
        }
    });
});

// this function will tick the relevant services box and submit the form
function ServicesTagCloudLink(DataSet, ServiceName) {

    // split the dataset into the data type and value
    var DataSetArray = DataSet.split('-');

    //alert(DataSetArray[0]);

    switch (DataSetArray[0]) {
        case "Services":
            // find the textBox element
            var CheckBox = $("#SearchBarDivContainer").find('#CheckBox' + DataSetArray[1]);
            // tick the checkbox then submit the form
            CheckBox.attr('checked', true);

            // now submit the form 
            $('#SeachBarForm').submit();

            break;
        case "PriceIncall":
            var PriceTo = $("#SearchBarDivContainer").find('#PriceTo');
            var PriceFrom = $("#SearchBarDivContainer").find('#PriceFrom');

            // set the price value to both the to and from price box's that way it will
            // only return escorts at that exact price
            PriceTo.val(DataSetArray[1]);
            PriceFrom.val(DataSetArray[1]);

            // now submit the form 
            $('#SeachBarForm').submit();

            break;
        case "Ages":
            // get the two age boxs. to and from
            var AgeTo = $("#SearchBarDivContainer").find('#ageTo');
            var AgeFrom = $("#SearchBarDivContainer").find('#ageFrom');

            // set the age value to both the to and from age box's that way it will
            // only return escorts at that exact age
            AgeTo.val(DataSetArray[1]);
            AgeFrom.val(DataSetArray[1]);

            // now submit the form 
            $('#SeachBarForm').submit();

            break;
    }
}

// kill the search sessions function
function KillSearchSessions() {
    // send the booking request
    $.ajax({
        type: "Get",
        url: "/EscortSearch/KillSearchSessions"
    });
}

// Sign in to the site
function SignIn() 
{
    $("#SignIn").dialog({
        bgiframe: true,
        modal: true,
        resizable: false,
        buttons: {

            'Sign In': function () {
                
                // the username and password
                var Username = $("#Username").val();
                var Password = $("#Password").val();

                var dataString = 'Username=' + Username + '&Password=' + Password;

                // send the booking request
                $.ajax({
                    type: "Post",
                    url: "/Index/MemberLogin",
                    data: dataString,
                    success: function (data) {

                        $.each(data, function (key, value) {
                            if (data.LoginOK == "False") {
                                // the login has failed, indicated the reason why
                                $("#SignInError").addClass("redText");
                                $("#SignInError").addClass("StandardText");
                                $("#SignInError").text(data.errorMessage);
                            }
                            if (data.LoginOK == "True") {
                                // the login was successfull
                                // now close the window and reload the page
                                $(this).dialog('close');
                                window.location.reload();
                            }
                        });
                    },
                    error: function () {
                        $("#BookingOutComeText").removeClass("MessageSentText");
                        $("#BookingOutComeText").addClass("redText");
                        $("#BookingOutComeText").text("ERROR: An Error has occured, please check your details and trt again.");
                    },
                    dataType: "json",
                    cache: false
                });

                // when the user clicks sign in, the form is submitted                
                //$("#SignInForm").submit();
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
}

// Sign out of the site
function SignOut() {
    $("#SignOut").dialog({
        bgiframe: true,
        modal: true,
        resizable: false,
        buttons: {

            'Sign Out': function () {

                // send the booking request
                $.ajax({
                    type: "Get",
                    url: "/Index/MemberSignOut",
                    success: function (data) {

                        $.each(data, function (key, value) {
                            if (data.SessionAbandon == "False") {
                                // the login has failed, indicated the reason why
                                $("#SignOutError").addClass("redText");
                                $("#SignOutError").addClass("StandardText");
                                $("#SignOutError").text(data.errorMessage);
                            }
                            if (data.SessionAbandon == "True") {
                                // the sign out was successfull
                                // now close the window and reload the page
                                $(this).dialog('close');
                                window.location.reload();
                            }
                        });
                    },
                    error: function () {
                        $("#SignOutError").removeClass("MessageSentText");
                        $("#SignOutError").addClass("redText");
                        $("#SignOutError").text("ERROR: An Error has occured, please check your details and trt again.");
                    },
                    dataType: "json",
                    cache: false
                });

                // when the user clicks sign in, the form is submitted                
                //$("#SignInForm").submit();
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
}  

/*
$("#BookingConfirmed").dialog({
    bgiframe: true,
    height: 180,
    modal: true,
    buttons: {
        Ok: function () {
            // initially clear all the bookings and rebuild
            $(".MemberBookingTR-ToBeAcepted").remove();

            // refresh the members new bookings and the historical, the booking just confirmed should disapear :)
            GetNewEscortMemberBookings(memberID);
            GetNewClientMemberBookings(memberID);
            GetHistoricMemberBookings(0);
            $(this).dialog('close');
        }
    }
});
*/

function IsAtLeast18(age)
{
    if(parseInt(age) < 18)
    {
        return false;
    }
    else
    {
        return true;
    }    
}

function IsFromHigherThanTo(from, to) {
    var fromInt = parseInt(from);
    var toInt = parseInt(to);

    if (fromInt > toInt) {
        return true;
    }
    else {
        return false;
    }
}

function IsNumeric(sText) {
    var ValidChars = "0123456789 ";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
            return IsNumber;
        }
        else continue;
    }
    return IsNumber;
}
