﻿// JavaScript for the Address Entry dialog

function openAddressEntryDialog()
{
  $('#addressEntryDialog').dialog('open');
}

function getSampleBallot()
{
  var state = $.cookie('State');
  var congress = $.cookie('Congress');
  var stateSenate = $.cookie('StateSenate');
  var stateHouse = $.cookie('StateHouse');
  var county = $.cookie('County');

  if (state && congress && stateSenate && stateHouse && county)
  {
    if ($.globals.getUpcomingElectionsCalled)
    {
      if ($.globals.upcomingElections)
        handleUpcomingElections();
    }
    else
    {
      $.ajax({
        type: "POST",
        url: "/WebService.asmx/GetUpcomingElections",
        data: "{" +
          "'stateCode': '" + state +
          "','congress': '" + congress +
          "','stateSenate': '" + stateSenate +
          "','stateHouse': '" + stateHouse +
          "','county': '" + county +
          "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: getUpcomingElectionsSucceeded,
        error: getUpcomingElectionsFailed
      });
      $.globals.getUpcomingElectionsCalled = true;
    }
  }
  else
    openAddressEntryDialog();
}

function redirectToVoters()
{
  document.location.pathname = "/Voters.aspx";
}

function getSampleBallot2()
{
  var state = $.cookie('State');
  var congress = $.cookie('Congress');
  var stateSenate = $.cookie('StateSenate');
  var stateHouse = $.cookie('StateHouse');
  var county = $.cookie('County');

  if (state && congress && stateSenate && stateHouse && county)
  {
    if (!$.globals.getUpcomingElectionsCalled)
    {
      $.ajax({
        type: "POST",
        url: "/WebService.asmx/GetUpcomingElections",
        data: "{" +
          "'stateCode': '" + state +
          "','congress': '" + congress +
          "','stateSenate': '" + stateSenate +
          "','stateHouse': '" + stateHouse +
          "','county': '" + county +
          "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: getUpcomingElectionsSucceeded2,
        error: getUpcomingElectionsFailed
      });
      $.globals.getUpcomingElectionsCalled = true;
    }
  }
  else
    redirectToVoters();
}

function getUpcomingElectionsSucceeded(result)
{
  var upcomingElections = result.d;
  $.globals.upcomingElections = upcomingElections;
  var menu = $('#socialMediaButtons .sampleBallot .electionMenu');
  if (upcomingElections.length > 1)
  {
    for (var n = 0; n < upcomingElections.length; n++)
    {
      var upcomingElection = upcomingElections[n];
      var a = $('<a />',
    {
      href: upcomingElection.Href,
      title: upcomingElection.Description
    }).html(upcomingElection.Description);
      menu.append(a);
    }
  }
  else
  {
    menu.addClass("padded");
    menu.html("We do not currently have any upcoming <br /> elections for this state.")
  }
  handleUpcomingElections();
}

function getUpcomingElectionsSucceeded2(result)
{
  var upcomingElections = result.d;
  if (upcomingElections.length == 1)
  {
    document.location.href = upcomingElections[0].Href;
  }
  else
  {
    redirectToVoters();
  }
}

function handleUpcomingElections()
{
  var upcomingElections = $.globals.upcomingElections;
  if (upcomingElections)
  {
    if (upcomingElections.length != 1)
    {
      var menu = $('#socialMediaButtons .sampleBallot .electionMenu');
      if (menu.is(":hidden"))
        menu.show('fast');
      else
        menu.hide('slow');
    }
    else
    {
      document.location.href = upcomingElections[0].Href;
    }
  }
}

function getUpcomingElectionsFailed(result)
{
  alert(result.status + ' ' + result.statusText);
}

function initAddressEntryDialog()
{
  $('#addressEntryDialog').dialog({
    autoOpen: false,
    width: 708,
    height: 367,
    resizable: false,
    // custom open and close to fix various problems
    open: onOpenAddressEntryDialog,
    close: onCloseJqDialog,
    modal: true
  });
}

function onOpenAddressEntryDialog()
{
  onOpenJqDialog.apply(this);
  $('#addressEntryDialog .ajaxMessage').hide();
}

function initAddressEntryAddressToFind()
{
  // enter in this input box triggers button click
  $('#addressEntryDialog .addressToFind').keyup(
    function (evt)
    {
      $('#addressEntryDialog .ajaxMessage').hide();
      var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
      if (keyCode == 13)
        addressEntryFindAddress();
    });
}

function initAddressEntryFindAddressButton()
{
  $("#addressEntryDialog .address .find").click(addressEntryFindAddress);
}

function initAddressEntryFindStateButton()
{
  $("#addressEntryDialog .state .find").click(addressEntryFindState);
}

function setAddressEntryAjaxActive(newState)
{
  var currentState = $.globals.ajaxState ? true : false;
  if (newState != currentState)
  {
    $.globals.ajaxState = newState;
    var visibility = newState ? "visible" : "hidden";
    $('#addressEntryDialog img.ajaxLoader').css("visibility", visibility);
  }
  return currentState;
}

function addressEntryFindAddress()
{
  var select = $('#addressEntryDialog .addressToFind');
  var input = select.val();
  // A workaround for wierd problem
  if (!input && select.length > 1)
    input = $(select[1]).val();
  if (input && !setAddressEntryAjaxActive(true))
  {
    $('#addressEntryDialog .ajaxMessage').hide();
    $.ajax({
      type: "POST",
      url: "/WebService.asmx/FindAddress",
      data: "{'input': '" + input + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: addressEntryFindAddressSucceeded,
      error: addressEntryFindAddressFailed
    });
  }
}

function addressEntryFindState()
{
  var select = $('#addressEntryDialog .selectState');
  var input = select.val();
  // A workaround for wierd problem
  if (!input && select.length > 1)
    input = $(select[1]).val();
  if (input && !setAddressEntryAjaxActive(true))
  {
    //$('#addressEntryDialog .ajaxMessage').hide();
    $.ajax({
      type: "POST",
      url: "/WebService.asmx/FindState",
      data: "{'input': '" + input + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: addressEntryFindStateSucceeded,
      error: addressEntryFindStateFailed
    });
  }
}

function showAddressEntryAjaxMessage(html, cssClass)
{
  var o = $('#addressEntryDialog .ajaxMessage');
  o.removeClass('ajaxSuccess ajaxFailure');
  o.addClass(cssClass);
  o.html(html);
  o.show();
}

function showStateAjaxMessage(html, cssClass)
{
  var o = $('#addressEntryDialog .ajaxMessage2');
  o.removeClass('ajaxSuccess ajaxFailure');
  o.addClass(cssClass);
  o.html(html);
  o.show();
}

function addressEntryFindAddressSucceeded(result)
{
  setAddressEntryAjaxActive(false);
  var addressFinderResult = result.d;
  if (addressFinderResult.SuccessMessage)
  {
    //if (isDefaultPage())
    {
      showAddressEntryAjaxMessage(addressFinderResult.SuccessMessage, "ajaxSuccess");
      if (addressFinderResult.RedirectUrl)
        document.location.href = addressFinderResult.RedirectUrl;
    }
//    else
//    {
//      showStateAjaxMessage("Redirecting to " + addressFinderResult.StateName, "ajaxSuccess");
//      document.location.href = "http://" + 
//        addressFinderResult.RedirectHost + location.pathname;
//    }
  }
  else
  {
    // only show up to 3 messages *****
    //var messages = addressFinderResult.ErrorMessages.slice(0, 3);
    var messages = addressFinderResult.ErrorMessages;
    showAddressEntryAjaxMessage(messages.join('<br />'), "ajaxFailure");
  }
}

function addressEntryFindAddressFailed(result)
{
  setAddressEntryAjaxActive(false);
  alert(result.status + ' ' + result.statusText);
}

function addressEntryFindStateSucceeded(result)
{
  setAddressEntryAjaxActive(false);
  var addressFinderResult = result.d;
  if (addressFinderResult.SuccessMessage)
  {
    //if (isDefaultPage())
    {
      showStateAjaxMessage(addressFinderResult.SuccessMessage, "ajaxSuccess");
      document.location.href = addressFinderResult.RedirectUrl;
    }
//    else
//    {
//      showStateAjaxMessage("Redirecting to " + addressFinderResult.StateName, "ajaxSuccess");
//      document.location.href = "http://" + 
//      addressFinderResult.RedirectHost + location.pathname;
//    }
  }
}

function addressEntryFindStateFailed(result)
{
  setAddressEntryAjaxActive(false);
  alert(result.status + ' ' + result.statusText);
}

function isDefaultPage()
{
  var path = location.pathname.toLowerCase();
  return path == "/" || path == "/default.aspx";
}

if (typeof ($.globals) == "undefined")
  $.globals = {}; // create global namespace

// Initialize when ready
$(function ()
{
  initAddressEntryDialog();
  initAddressEntryAddressToFind();
  initAddressEntryFindAddressButton();
  initAddressEntryFindStateButton();
});

