﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />
var idCities = "divCities";
var idLocations = "divLocations";
var idErrors = "lblError";
var idhnLocationId1 = "hnLocationId1";
var idhnLocationId = "hnLocationId";
var idhnStateId = "hnStateId";
var idhnCityId = "hnCityId";
var idhnShowLocation = "hnShowLocation"
var idfldsetStates = "fldsetStates";
var idbtnMapIt = "btnMapIt";
var idMapItZip = "MapItZip";
var idtxtZipCode = "txtZipCode";
var idddlDistance = "ddlDistance";
var idLocationByZip = "LocationByZip";
var idhnCityId1 = "hnCityId1"
var idhnStateId1 = "hnStateId1";
var idbtnRestartSearch = "btnRestartSearch";
var classCities = "cities";
var classLocations = "locations";
var errorMessage = "An error occured!";
var selectCityTitle = "Select city";
var selectLocationTitle = "Select location";

function RestartSearch() {
    ShowElementWithId(idfldsetStates);
    Disable(idbtnRestartSearch);
    HideElementsWithClass(classCities);
    HideElementsWithClass(classLocations);
    HideElementWithId(idLocationByZip);
    HideElementWithId(idMapItZip);
    CancelEdit();
}

function SearchByZip() {
    HideElementWithId(idMapItZip);
    CancelEdit();
    var txt = $("#" + idtxtZipCode);
    if (txt.val().length > 0) {
        var range = $("#" + idddlDistance).val();
        var params = "{zipcode:'" + txt.val() + "', range:'" + range + "'}";
        ExecuteWebService(
        params,
        "ajax.asmx/SearchByZip",
        function(result) {
            if (result.d.Result) {
                ShowElementWithId(idMapItZip);
                //if (result.d.LocationId > 0) {
                //    ShowLocation(result.d.LocationId, result.d.CityId, result.d.StateId, 1);
                //}
                //else {
                    ShowElementWithId(idLocationByZip, result.d.Html);
                //}
                HideElementWithId(idfldsetStates);
                HideElementsWithClass(classCities);
                HideElementsWithClass(classLocations);
                UnDisable(idbtnRestartSearch);
            }
            else {
                ShowElementWithId(idLocationByZip, result.d.Error);
            }
        },
        onError
      );
    }
    else {
        ShowErrorMessage("<b>ZIP Code</b> is required.");
    }
}
function LoadPhones(locationId, id) {
    HideElementWithId(idErrors);
    //ShowElementWithId(idbtnMapIt);
    if (id) {
        SetAnchorUnSelected("alocation");
        SetAnchorSelected(id);
    }
    ToggleClassOffForFieldSetWithClass(classLocations);
    SetValue(idhnLocationId1, locationId);
}
function ShowAdditionalStates() {

}
function ShowAdditionalCities() {

}
function ShowAdditionalLocations() {
  
}

function CancelEdit() {
    SetDefaultValues();
    HideElementsWithClass(classCities);
    HideElementWithId(idErrors);
    HideElementsWithClass(classLocations);
    HideElementWithId(idCities);
    HideElementWithId(idLocations);
    SetAnchorUnSelected("astate");
    ToggleClassOnForFieldSetWithId(idfldsetStates);
    ShowElementWithId(idfldsetStates);
}


function ShowBusy(id) {
    $("#" + id).html('&nbsp;&nbsp;Loading.&nbsp;Please wait...<img src="images/busy.gif" alt="Please wait loading" />');
}
function SetElementVisibilityVisible(className) {
    $('.' + className).css("visibility", 'visible');
}
function ShowElementsWithClass(className) {
    $('.' + className).css("display", 'block');
}
function HideElementsWithClass(className) {
    $('.' + className).css("display", 'none');
}
function ShowElementWithId(id, text) {
    var element = $('#' + id);
    if (text) {
        element.html(text);
    }
    element.css("display", 'block');
}
function HideElementWithId(id, text) {
    var element = $('#' + id);
    element.css("display", 'none');
    if (text) {
        element.html(text);
    }
}

function SetAnchorSelected(id) {
    var element = $("#" + id)
    element.css("font-weight", "bolder");
    element.css("color", "black");
    element.css("text-decoration", "none");
}
function SetAnchorUnSelected(className) {
    var elements = $("." + className)
    elements.css("font-weight", "normal");
    elements.css("color", "#d00032");
    elements.css("text-decoration", "underline");
}
function SetLegend(className, text) {
    $('.' + className + " legend").html("<b>" + text + "</b>");
}
function ToggleClassOnForFieldSetWithId(id) {
    $("#" + id).addClass("fieldset_active");
}
function ToggleClassOnForFieldSetWithClass(className) {
    $("." + className).addClass("fieldset_active");
}
function ToggleClassOffForFieldSetWithId(id) {
    $("#" + id).removeClass("fieldset_active");
}
function ToggleClassOffForFieldSetWithClass(className) {
    $("." + className).removeClass("fieldset_active");
}
function LoadCities(stateId, showonlyone, id) {
    ShowBusy(idCities);
    ShowElementsWithClass(classCities);
    HideElementWithId(idErrors);
    HideElementWithId(idLocationByZip);
    if (showonlyone != 2) {
        HideElementsWithClass(classLocations);
    }
    if (id) {
        SetAnchorUnSelected("astate");
        SetAnchorSelected(id);
    }
    var params = "{stateId:'" + stateId + "'}";
    ToggleClassOffForFieldSetWithId(idfldsetStates);
    ToggleClassOnForFieldSetWithClass(classCities);
    var params = "{stateId:'" + stateId;
    if (showonlyone == 1) {
        params += "', cityId:'" + GetValue(idhnCityId);
    }
    else {
        params += "', cityId:'0";
    }
    params += "'}";
    ExecuteWebService(
        params,
        "ajax.asmx/GetCities",
        function(result) {
            if (result.d.Result) {
                ShowElementWithId(idCities, result.d.Html);
                SetLegend(classCities, selectCityTitle + " in " + result.d.State);
                var cityid = GetValue(idhnCityId);
                //SetValue(idhnCityId, 0);
                if (cityid > 0 && showonlyone != 2) {
                    HideElementWithId(idLocations);
                    HideElementsWithClass(classLocations);
                    SetValue(idhnCityId, 0);
                    LoadLocations(cityid, showonlyone, "city_" + cityid);
                }
                if (showonlyone == 2) {
                    cityid = GetValue(idhnCityId1);
                    SetAnchorUnSelected("acity");
                    SetAnchorSelected("city_" + cityid);
                    ToggleClassOffForFieldSetWithClass(classCities);
                }
            }
            else {
                ShowElementWithId(idCities, result.d.Error);
                SetLegend(classCities, selectCityTitle);
            }
        },
        onError
      );
}
function LoadLocations(cityId, showonlyone, id) {
    ShowBusy(idLocations);
    ShowElementsWithClass(classLocations);
    HideElementWithId(idErrors);
    if (id) {
        SetAnchorUnSelected("acity");
        SetAnchorSelected(id);
    }
//    if (showonlyone != 2) {
//        HideElementWithId(idbtnMapIt);
//    }
    ToggleClassOnForFieldSetWithClass(classLocations);
    ToggleClassOffForFieldSetWithClass(classCities);
    var params = "{cityId:'" + cityId;
    if (showonlyone == 1) {
        params += "', locationId:'" + GetValue(idhnLocationId);
    }
    else {
        params += "', locationId:'0";
    }
    params += "'}";
    ExecuteWebService(
        params,
        "ajax.asmx/GetLocations",
        function(result) {
            if (result.d.Result) {
                ShowElementWithId(idLocations, result.d.Html);
                SetLegend(classLocations, selectLocationTitle + " in " + result.d.StateAndCity);
                SetValue(idhnCityId1, cityId);
                //debugger 
            }
            else {
                ShowElementWithId(idLocations, result.d.Error);
                SetLegend(classLocations, selectLocationTitle);
            }
        },
        onError
    );
}
function MapIt(url, type) {
    var locationId = GetValue(idhnLocationId1);
    if (locationId > 0) {
        MapItWindowOpen(url + "?locid=" + locationId);
    }
    else if (type == 'zipsearch') {
        var zip = GetValue(idtxtZipCode);
        var distance = GetValue(idddlDistance);
        if (zip && distance > 0) {
            MapItWindowOpen(url + "?zip=" + zip + "&distance=" + distance);
        }
    }
    else {
        MapItWindowOpen(url + "?cityid=" +  GetValue(idhnCityId1));     
    }
}
function MapItWindowOpen(url) {
    window.open(url, "wndMapIt", "location=0,status=0,scrollbars=0, width=820, height=620");
}
function UnDisable(id) {
    $("#" + id).removeAttr("disabled");
}
function Disable(id) {
    $("#" + id).attr("disabled", "disabled");
}
function onError(XMLHttpRequest, textStatus, errorThrown) {
    ShowErrorMessage(errorMessage)
}

function ExecuteWebService(params, url, callbackSuccess, callbackError) {
    $.ajax({
        type: "POST",
        url: url,
        cache: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: params,
        success: callbackSuccess,
        error: callbackError
    });
}
function ShowErrorMessage(message) {
    var message = '<ul><li>' + message + '</li></ul><br>';
    ShowElementWithId(idErrors, message);
}
function SetValue(id, value) {
    $("#" + id).val(value);
}
function GetValue(id) {
    return $("#" + id).val();
}

function SignUp() {
    var id = GetValue(idhnLocationId1)
    if (id > 0) {
        window.document.location = 'signup.aspx?locid=' + id; 
    }
    else {
        window.document.location = 'signup.aspx'; 
    }
}

function ShowLocationByZip(locationId, cityId, stateId, showonlyone, id) {
    SetDefaultValues();
    if (id) {
        SetAnchorUnSelected("aziplocation");
        SetAnchorSelected(id);
    }
    //ShowElementWithId(idMapItZip);
    LoadPhones(locationId);
}
function ShowLocation(locationId, cityId, stateId, showonlyone, id) {
    //5, 7, 10, 1, this.id
    SetDefaultValues();
    SetValue(idhnStateId1, stateId);
    SetValue(idhnCityId1, cityId);
    SetValue(idhnLocationId1, locationId);
    SetValue(idhnStateId, stateId);
    SetValue(idhnCityId, cityId);
    SetValue(idhnLocationId, locationId);
    if (id) {
        SetAnchorUnSelected("aziplocation");
        SetAnchorSelected(id);
    }
    SetValue(idhnShowLocation, showonlyone);
    LoadCities(stateId, showonlyone, "state_" + stateId);
}
function SetDefaultValues() {
    SetValue(idhnStateId, 0);
    SetValue(idhnCityId, 0);
    SetValue(idhnLocationId, 0);
    SetValue(idhnStateId1, 0);
    SetValue(idhnCityId1, 0);
    SetValue(idhnLocationId1, 0);
}


