﻿$(document).ready(function () {
    $.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr) {
            if (xhr.responseText.indexOf("User is not logged in.") > -1) {
                window.location.replace("AccessDenied.aspx");
            }
            else {
                //display the error
                alert(xhr.statusText + " " + xhr.status);

                //log the error
                var errorString = xhr.statusText + " " + xhr.status + "\n" + xhr.responseText
                errorString = errorString.replace(/'/g, "&#39;").replace("/\\/g", "&#92;")
                $.ajax({
                    url: "Services/Utility.aspx/WriteLog",
                    data: "{'message':'" + errorString + "'}",
                    success: function (msg) {
                        alert("An error has occurred and has been logged.");
                    },
                    error: function (xhr2) {
                    }
                });
            }
        }
    });
    $("table.dataTable").tablesorter({ widgets: ['zebra'] });


    //button image hovers
    $("[id*='cmdSave'] img, [id*='btnSavePrefs'] img, [id*='btnOK'] img, [id*='btnSave'] img, [id*='btnRegister'] img, [id*='btnReserveTeeTime'] img, [id*='btnFinalizeReservation'], [id*='btnSearchTeeTime'] img").hover(
    function () {
        //need if statement becuase something is causing the second hover() function not to run sometimes
        if ($(this).attr("src").indexOf("-over.gif") < 0)
            $(this).attr("src", $(this).attr("src").replace(".gif", "-over.gif"));
    },
    function () {
        $(this).attr("src", $(this).attr("src").replace("-over", ""));
    });

    //needed for firefox, a tags don't have click events, which the DefaultButton implementation assumes.
    $("[id*='pnlLoginContent']").keypress(function (event) {
        if (13 == event.keyCode) {
            event.preventDefault();
            eval($(this).find("[id*='btnLogin']").attr("href")); //runs the code in the href attribute of the LinkButton
        }
    });

    //left-hand course line-up
    $("[id*='lineUpLocationLabel_']").click(function () {
        var locId = $(this).attr("data-locID");
        var locCourses = $(this).parent().find("[id*='lineUpLocaitonCourses_']");
        var plusMinusImage = "images/icons/toggle_plus.png";
        $(locCourses).toggle();
        if ($(locCourses).is(":visible")) { plusMinusImage = "images/icons/toggle_minus.png"; }
        $("#lineUpLocationExpandContractImg_" + locId).attr("src", plusMinusImage);
    });

});

//gets cookie value from a delimited list of values: a=1&b=2
function getCookieValue(contents, valueName) {
  if (contents==undefined || contents==null) {contents = ''; }
  var returnVal = "";
  var contentsArray = contents.split("&")
  for (i = 0; i < contentsArray.length; i++) {
    if (contentsArray[i].startsWith(valueName)) {
      returnVal = contentsArray[i].split("=")[1];
    }
  }
  return returnVal;
}

