﻿var autocompleteServiceVal = "";
var autocompleteLocationVal = "";
var autocompleteInAction = false;
var autocompleteNeedToSubmit = false;

$(document).ready(function () {

    //Watermarks
    $('.watermark').watermark('watermark');

    //Create Rollover style
    $("img.rollover").hover(
     function () {
         this.src = this.src.replace("_off", "_on");
     },
     function () {
         this.src = this.src.replace("_on", "_off");
     }
    );

    //Autosuggest Service (also adds watermark)
    //requires hidden field #ServiceUid
    $("input.AutocompleteService").watermark('watermark').keypress(function (event) {
        // if any key pressed then we set autocompleteInAction flag
        if (event.which == '13') {  //except return, which acts as normal.
            $('.formWithAutocomplete').submit();
        }
        autocompleteInAction = true;
    }).autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/WebService/AutocompleteServices",
                dataType: "json",
                data: { query: request.term },
                success: function (data) {
                    response($.map(data, function (value, key) {
                        return { label: value.Service, value: value.Service, key: value.Id }
                    }));
                }
            });
        },
        minLength: 2,
        autoFocus: true,
        change: (function (e, ui) {
            if (jQuery.trim($(e.target).val()) != autocompleteServiceVal && $("#ServiceUid").val() != "") {
                $("#ServiceUid").val("");
                autocompleteInAction = false; // unset autocompleteInAction flag
                if (autocompleteNeedToSubmit) {// if autocompleteNeedToSubmit flag is set then try submit again
                    $('.formWithAutocomplete').submit();
                }
            }
        }),
        select: (function (e, ui) {
            $("#ServiceUid").val(ui.item.key);
            autocompleteServiceVal = (ui.item.value);
            autocompleteInAction = false; // unset autocompleteInAction flag
        })
    });

    //Autosuggest Location (also adds watermark)
    //requires hidden field #LocationUid
    $("input.AutocompleteLocation").watermark('watermark').keypress(function (event) {
        // if any key pressed then we set autocompleteInAction flag
        if (event.which == '13') {  //except return, which acts as normal.
            $('.formWithAutocomplete').submit();
        }
        autocompleteInAction = true;
    }).autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/WebService/AutocompleteLocations",
                dataType: "json",
                data: { query: request.term },
                success: function (data) {
                    response($.map(data, function (value, key) {
                        return { label: value.DisplayText, value: value.DisplayText, key: value.Id }
                    }));
                }
            });
        },
        minLength: 2,
        autoFocus: true,
        change: (function (e, ui) {
            if (jQuery.trim($(e.target).val()) != autocompleteLocationVal && $("#LocationUid").val() != "") {
                $("#LocationUid").val("");
                autocompleteInAction = false; // unset autocompleteInAction flag
                if (autocompleteNeedToSubmit) {// if autocompleteNeedToSubmit flag is set then try submit again
                    $('.formWithAutocomplete').submit();
                }
            }
        }),
        select: (function (e, ui) {
            $("#LocationUid").val(ui.item.key);
            autocompleteLocationVal = (ui.item.value);
            autocompleteInAction = false; // unset autocompleteInAction flag
        })
    });

    //Set up form to cope with autocomplete foibles over validation happening after form submit due to async processing.
    $('.formWithAutocomplete').submit(function () {
        if (autocompleteInAction) {  // if autocompleteInAction then cancel form submition
            autocompleteNeedToSubmit = true;
            return false;
        }
    });
});
