﻿/* Kleenheat Google Map / Store Locator Javascript */

// start Google Maps JS
var map;
var gdir;
var geocoder = null;
var addressMarker;

// Debounce function used for ajax search suggestions
Function.prototype.debounce = function (threshold, execAsap) {
    var func = this, // reference to original function
        timeout; // handle to setTimeout async task (detection period)
    // return the new debounced function which executes the original function only once
    // until the detection period expires
    return function debounced () {
        var obj = this, // reference to original context object
            args = arguments; // arguments at execution time
        // this is the detection function. it will be executed if/when the threshold expires
        function delayed () {
            // if we're executing at the end of the detection period
            if (!execAsap)
                func.apply(obj, args); // execute now
            // clear timeout handle
            timeout = null; 
        };
        // stop any current detection period
        if (timeout)
            clearTimeout(timeout);
        // otherwise, if we're not already waiting and we're executing at the beginning of the detection period
        else if (execAsap)
            func.apply(obj, args); // execute now
        // reset the detection period
        timeout = setTimeout(delayed, threshold || 100); 
    };
}

$(document).ready(function() {

    $("input.slPostcode_Small").attr("autocomplete", "off");
    
    var postcodeText = "Eg: 6000 or Perth";

    $(".slPostcode_Small").focus(function() {
    if ($(".slPostcode_Small").val() == postcodeText) { $(".slPostcode_Small").val(""); }
        if ($("input[id$=SelectedState_Small]").val() != "" && $("input[id$=SelectedPostcode_Small]").val() == "") {
            $("input[id$=SelectedState_Small]").val("");
        }
    });

    $(".slPostcode_Small").blur(function() {
    if ($(".slPostcode_Small").val() == "") { $(".slPostcode_Small").val(postcodeText); };
    });
   

    // ajax suggested search
    $("input.slPostcode_Small").keyup(function() {

    if (!/^\d+$/i.test($("input.slPostcode_Small").val())) {
            $("input[id$=SelectedPostcode_Small]").val("");
            $("input[id$=SelectedState_Small]").val("");
            $("input[id$=SelectedTown_Small]").val("");
        }
        if ($("input.slPostcode_Small").val() == "") {
            HideSuggestedText_Small();
        }

        var criteria = $("input.slPostcode_Small").val();
        if (criteria != "") {
            $("div#suggested_Small").fadeIn();
            $.post("/UserControls/Kleenheat/SuggestedLocationsSmall.aspx", { criteria: criteria }, function(data) {
            $("div#suggestedContent_Small").html(data);
                if ($("div#suggestedContent_Small").find("ul").html() == null) {
                    $("div#suggested_Small").hide();
                    $("div#suggestedContent_Small").html("");
                }

            });
        }
    } .debounce(500));

});

// Form Validation
function ValidateStoreSearchFormOnly()
{
    var aErrors = new Array(0);

    // check all fields
    if (
        $("input[id$=SelectedState_Small]").val() == "" &&
        ($("input.slPostcode_Small").val() == "" || $("input.slPostcode_Small").val() == "Eg: 6000 or Perth") &&
        $("input[id$=SelectedPostcode_Small]").val() == ""
        )
    {
        aErrors.push("Please enter some search criteria by either selecting a state or postcode / town");
    }
        
    
    // if state, check filter options
    if ($("input[id$=SelectedState_Small]").val() != "")
    {
        if ($("input[id$=SelectedState_Small]").val() == "WA" || $("input[id$=SelectedState_Small]").val() == "NT")
        {
            if ($("input[id$=SelectedBusinessUnit_Small]").val() == "KleenheatAppliances")
            {
                aErrors.push("Kleenheat Gas Appliance services are not available in WA or NT");
            }     
        }
        if ($("input[id$=SelectedState_Small]").val() != "WA" && $("input[id$=SelectedState_Small]").val() != "NT")
        {
            if ($("input[id$=SelectedBusinessUnit_Small]").val() == "gashouse") // gashouse
            {
                aErrors.push("Gas House services are available in WA and NT only");
            }
            if ($("input[id$=SelectedBusinessUnit_Small]").val() == "autogas")  // autogas
            {
                aErrors.push("AutoGas services are available in WA and NT only");
            }
        }
    }
    
    // if postcode, check filter options
    if ($("input[id$=slPostcode_Small]").val() != "")
    {
        var postcode = $("input[id$=slPostcode_Small]").val();
        if (postcode > 5999 && postcode < 7000) {
            if ($("input[id$=businessUnit_Small]").val() == "kleenheatappliances")
            {
                aErrors.push("Kleenheat Gas Appliance services are not available in WA or NT");
            }
        }
        if (postcode < 1000) {
            if ($("input[id$=businessUnit_Small]").val() == "kleenheatappliances")
            {
                aErrors.push("Kleenheat Gas Appliance services are not available in WA or NT");
            }
        }
        if (postcode > 1000 && (postcode < 5999 || postcode > 7000)) {
            if ($("input[id$=businessUnit_Small]").val() == "gashouse")
            {
                aErrors.push("Gas House services are available in WA and NT only");
            }
            if ($("input[id$=businessUnit_Small]").val() == "autogas")
            {
                aErrors.push("AutoGas services are available in WA and NT only");
            }
        }
    }
    if (aErrors.length) {
	    displayErrorsAlert(aErrors);
	    return false;
	}
	
	return true;
}

function SetSuggestionValues_Small(postcode, suburb, state)
{
    $("input[id$=slPostcode_Small]").val(suburb + ", " + state + " " + postcode);
    $("input[id$=SelectedPostcode_Small]").val(postcode);
    $("input[id$=SelectedState_Small]").val(state);
    $("input[id$=SelectedTown_Small]").val(suburb);
    HideSuggestedText_Small();
}

function HideSuggestedText_Small()
{
    $("div#suggested_Small").fadeOut(function() { $("div#suggestedContent_Small").html(""); });
}


