﻿//Prototypes
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); }
String.prototype.ltrim = function() { return this.replace(/^\s+/, ""); }
String.prototype.rtrim = function() { return this.replace(/\s+$/, ""); }
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str) }
String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }

//fieldName - The name associated to the validation indicator icons
//ctrlName - The name of the control to validate
//displayMode - The CSS display property to apply when the validation indicator is made visible (i.e."block", "inline", etc.)
//msgText - The text to display when a field is deemed invalid
function CheckDateField(fieldName, ctrlName, displayMode, msgText) {
  if ($("[id$=" + ctrlName + "]").length > 0) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var isValid = (($("[id$=" + ctrlName + "]")[0].value.match(RegExPattern)) && ($("[id$=" + ctrlName + "]")[0].value.trim() != ''));
    $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
    $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
    $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
    $("[id$=" + ctrlName + "]").attr("class", (isValid ? "validFld" : "invalidFld"));
  }
}
function CheckCostField(fieldName, ctrlName, displayMode, msgText) {
  if ($("[id$=" + ctrlName + "]").length > 0) {
    var RegExPattern = /^\d{0,4}(\.\d{1,2})?$/;
    var isValid = (($("[id$=" + ctrlName + "]")[0].value.match(RegExPattern)) && ($("[id$=" + ctrlName + "]")[0].value.trim() != ''));
    $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
    $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
    $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
    $("[id$=" + ctrlName + "]").attr("class", (($("[id$=" + ctrlName + "]")[0].value.match(RegExPattern)) && ($("[id$=" + ctrlName + "]")[0].value.trim() != '')) ? "validFld" : "invalidFld");
  }
}
function CheckRequiredField(fieldName, ctrlName, displayMode, msgText) {
  if ($("[id$=" + ctrlName + "]").length > 0) {
    var isValid = ($("[id$=" + ctrlName + "]")[0].value.trim() != "");
    $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
    $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
    $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
    $("[id$=" + ctrlName + "]").attr("class", (isValid ? "validFld" : "invalidFld"));
  }
}
function CheckRequiredSelection(fieldName, ctrlName, countRequired, displayMode, msgText) {
  if ($("span[id$=" + ctrlName + "] input[id*=" + ctrlName + "]").length > 0) {
    var isValid = ($("span[id$=" + ctrlName + "] input[id*=" + ctrlName + "]:checked").length >= countRequired);
    $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
    $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
    $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
  }
}
function CheckEmailField(fieldName, ctrlName, displayMode, msgText) {
  if ($("[id$=" + ctrlName + "]").length > 0) {
    if ($("[id$=" + ctrlName + "]")[0].value.trim() != '') {
      var RegExPattern = /^([a-zA-Z0-9]+([\.+_-][a-zA-Z0-9]+)*)@(([a-zA-Z0-9]+((\.|[-]{1,2})[a-zA-Z0-9]+)*)\.[a-zA-Z]{2,6})$/;
      var isValid = (($("[id$=" + ctrlName + "]")[0].value.match(RegExPattern)));
      $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
      $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
      $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
      $("[id$=" + ctrlName + "]").attr("class", (($("[id$=" + ctrlName + "]")[0].value.match(RegExPattern)) && ($("[id$=" + ctrlName + "]")[0].value.trim() != '')) ? "validFld" : "invalidFld");
    }
    else {
      $("[id*=imgGC_" + fieldName + "]").css("display", displayMode);
      $("[id*=imgRE_" + fieldName + "]").css("display", "none");
      $("[id*=msg_" + fieldName + "]").html("");
      $("[id$=" + ctrlName + "]").attr("class", "validFld");
    }
  }
}
function CheckEmailUniqueness(emailAddress, ignoreUserID, fieldName, ctrlName, displayMode, msgText) {
  $.ajax({
    type: "POST",
    async: false, //Required so that subsequent page modifications are performed after the results of this check are final.
    cache: false,
    url: "EventManagement/AJAXValidation.aspx/CheckEmailAddressIsUnique",
    data: "{Email:'" + emailAddress + "',IgnoreUserID:" + ignoreUserID + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
      $("[id*=imgGC_" + fieldName + "]").css("display", (response.d ? displayMode : "none"));
      $("[id*=imgRE_" + fieldName + "]").css("display", (response.d ? "none" : displayMode));
      $("[id*=msg_" + fieldName + "]").html(response.d ? "" : msgText);
      $("[id$=" + ctrlName + "]").attr("class", (response.d ? "validFld" : "invalidFld"));
    }
  });
}
function CheckUserSelected(fieldName, ctrlName, displayMode, msgText, disallowedUserID, disallowedUserMsgText) {
  disallowedUserID = disallowedUserID || 0;
  disallowedUserMsgText = disallowedUserMsgText || "";
  var selectedName = $("span[id*=" + ctrlName + "_lblSelectedUser]").text().trim();
  var selectedUserID = $("[id*=" + ctrlName + "_txtUserID]").val();
  var isValid = ((selectedName.length > 0) && (selectedName != "None selected"));
  if (isValid  && disallowedUserID != 0) {
    isValid = (selectedUserID != "0") && (selectedUserID != disallowedUserID.toString());
    msgText = disallowedUserMsgText;
  }
  $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
  $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
  $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
}
function CheckClubSelection(fieldName, ctrlName, displayMode, msgText) {
  if ($("[id$=" + ctrlName + "]").length > 0) {
    var ddl = $("[id$=" + ctrlName + "]")[0];
    //var isValid = (ddl.options[ddl.selectedIndex].value != "0") ? true : ($("[id$=txtOtherClubName]")[0].value.trim() != "");
    var isValid = (parseInt(ddl.options[ddl.selectedIndex].value) > 0) ? true : ($("[id$=txtOtherClubName]")[0].value.trim() != "");
    $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
    $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
    $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
    $("[id$=" + ctrlName + "]").attr("class", (isValid ? "validFld" : "invalidFld"));
  }
}
function CheckDropdownSelection(fieldName, ctrlName, displayMode, msgText) {
  if ($("[id$=" + ctrlName + "]").length > 0) {
    var ddl = $("[id$=" + ctrlName + "]")[0];
    var isValid = (parseInt(ddl.options[ddl.selectedIndex].value) != "0") ? true : false;
    $("[id*=imgGC_" + fieldName + "]").css("display", (isValid ? displayMode : "none"));
    $("[id*=imgRE_" + fieldName + "]").css("display", (isValid ? "none" : displayMode));
    $("[id*=msg_" + fieldName + "]").html(isValid ? "" : msgText);
    $("[id*=" + ctrlName + "]").attr("class", (isValid ? "validFld" : "invalidFld"));
  }
}
function ClearValidation(fieldName, ctrlName, displayMode) {
  $("[id*=imgGC_" + fieldName + "]").css("display", displayMode);
  $("[id*=imgRE_" + fieldName + "]").css("display", "none");
  $("[id*=msg_" + fieldName + "]").html("");
  $("[id$=" + ctrlName + "]").attr("class", "validFld");
}
function SetInvalid(fieldName, ctrlName, displayMode, msgText) {
  $("[id*=imgGC_" + fieldName + "]").css("display", "none");
  $("[id*=imgRE_" + fieldName + "]").css("display", displayMode);
  $("[id*=msg_" + fieldName + "]").html(msgText);
  $("[id$=" + ctrlName + "]").attr("class", "invalidFld");
}

function SuppressEnterKey() {
  $("form").bind("keypress", function(e) {
    if (e.keyCode == 13) return false;
  });
}

//Set up some basic page functionality.
$(document).ready(function() {
  //Set the Status Message to automatically fade out after the user clicks anywhere on the page.
  $('*').bind('click', function(event) { $('.msgBox').fadeOut(); });
});

//*** - UserSearch.ascx scripts - BEGIN - ****************************************************************
function CheckSearchCriteria() {
  //Check required fields.
  CheckRequiredField('SearchText', 'txtSearchText', 'block', 'Search criteria is required');
  AdjustButton('btnSearch');
}
function AdjustButton(buttonName) {
  //Disable the Update button if any of the entries are not valid.
  if ($("img[id*=imgRE_]:visible").length > 0) {
    $("input[id*=" + buttonName + "]").attr("disabled", "disabled").css("color", "gray");
  }
  else {
    $("input[id*=" + buttonName + "]").removeAttr("disabled").css("color", "#000");
  }
}
//*** - UserSearch.ascx scripts - END - ******************************************************************

function DisplayPayment(paymentID) {
  $.ajax({
    type: "POST",
    cache: false,
    url: "EventManagement/PaymentSummary.aspx/PopulatePaymentSummary",
    data: "{PaymentID:" + paymentID + ",DisplayPrintButton:true}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
      $.blockUI({
        message: response.d,
        css: {
          padding: '10px',
          height: '450px',
          width: '400px',
          overflow: 'auto',
          top: '75px'
        }
      });
      $('.blockOverlay').attr('title', 'Click to unblock').click($.unblockUI);
    },
    error: function(xml, textStatus, errorThrown) {
      //alert("Details for this payment are unavailable.  If this situation persists, please contact Support.");
      alert(errorThrown);
  }
  });
}

