﻿/// <reference path="../A1/jquery-1.7.min.js" />
/// <reference path="../A1/jquery-ui-1.8.16.custom.min.js" />
/// <reference path="Global.js" />
var TXT_SWITCH;
var DDL_SWITCH;
var TXTA_SWITCH;
var DEBUG = true;
//
var COOKIE_PAGE_INDEX = 'COOKIE_PAGE_INDEX';
//
var TOTAL_AMOUNT = 0;
var TOTAL_PAGE = 0;
var PAGE_SIZE = 10;
var IS_ASC = true;
var ORDER_BY = 'CreatedTime DESC';
// *******************************************************************************************************************************
$(function () {
  ////////// Global Ajax Start
  $("body").ajaxStart(ShowWaitingBox);
  $("body").ajaxComplete(HideWaitingBox);
  //////////// Global Ajax Error
  $("body").ajaxError(function (e, xhr, settings, exception) {
    if (DEBUG) {
      var jObj = $(xhr.responseText);
      if (jObj.length == 4) {
        var data = jObj[3].data;
        alert(data);
      }
      else if (jObj.length == 10) {
        var data = jObj[9].data;
        alert(data);
      }
      else {
        alert(xhr.responseText);
      }
    }
    else {
      ShowMessage("操作失败。");
    }
  });
  ////////// Enter key
  $(".txtGoto").keyup(function (event) {
    if (event.keyCode == '13') {
      GoIndex($(this).val());
    } else {
      CopyTo(this, '.txtGoto');
    }
  });
  /////////
  $(".searchbox :text,.searchbox :password").keydown(function (event) {
    if (event.keyCode == '13') {
      BindData();
    }
  });
});
// Json Handle
// *******************************************************************************************************************************
function JsonHandle(result) {
  if (result.Mode == 1) {
    ShowMessage(result.Message, result.Url);
  }
  else if (result.Mode == 2) {
    window.location.href = result.Url;
  }
  else if (result.Mode == 3) {
    ShowMessages(result.Messages);
  }
}
// Show Message
// *******************************************************************************************************************************
function ShowMessage(msg, url) {
  if (IsNullOrWhiteSpace(msg)) {
    return;
  }
  //
  var dia = $('<div class="msgbox">' + msg + '</div>');
  dia.dialog({
    modal: true,
    buttons: {
      "确定": function () {
        if (!IsNullOrWhiteSpace(url)) {
          window.location.href = url;
        }
        $(this).dialog("close");
      }
    }
  });
}
function ShowMessages(msgs) {
  var html = '<ul>';
  $.each(msgs, function (i, msg) {
    html += '<li>' + msg + '</li>';
  });
  html += '</ul>';
  //
  ShowMessage(html);
}
function ShowConfirmMessage(msg, okfn) {
  var dia = $('<div class="msgbox">' + msg + '</div>');
  dia.dialog({
    modal: true,
    buttons: {
      "确定": function () {
        okfn();
        $(this).dialog("close");
      },
      "取消": function () { $(this).dialog("close"); }
    }
  });
}
// Waiting Box
// ***********************************************************************************************************************************
function ShowWaitingBox() {
  if ($('.divWaitingBox').length > 0) {
    return;
  }
  //
  DisableInput();
  //
  var pageHeight = GetHtmlVisualSize().pageHeight;
  var divBox = $("<div class=\"divWaitingBox\"></div>");
  if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
    divBox.css("height", pageHeight);
  }
  $(document.body).append(divBox);
  divBox.height(pageHeight);
  divBox.animate({ opacity: 0.3 }, 0).show(0);
}
function HideWaitingBox() {
  EnableInput();
  //
  $(".divWaitingBox").remove();
}
// Input Switch
// ***********************************************************************************************************************************
function DisableInput() {
  $("a").blur();
  //
  TXT_SWITCH = $("input:not(:disabled)");
  TXTA_SWITCH = $("textarea:not(:disabled)");
  DDL_SWITCH = $("select:not(:disabled)");
  //
  TXT_SWITCH.prop("disabled", true);
  TXTA_SWITCH.prop("disabled", true);
  DDL_SWITCH.prop("disabled", true);
}
function EnableInput() {
  if (!IsNullOrWhiteSpace(TXT_SWITCH)) { TXT_SWITCH.prop("disabled", false); TXT_SWITCH = null; }
  if (!IsNullOrWhiteSpace(TXTA_SWITCH)) { TXTA_SWITCH.prop("disabled", false); TXTA_SWITCH = null; }
  if (!IsNullOrWhiteSpace(DDL_SWITCH)) { DDL_SWITCH.prop("disabled", false); DDL_SWITCH = null; }
}
// Pagination
// ***********************************************************************************************************************************
function GetIndex() {
  var index = GetCookie(COOKIE_PAGE_INDEX);
  if (IsInt(index)) {
    return parseInt(index);
  }
  else {
    return 1;
  }
}
function SetIndex(index) {
  // Path '/' is very important to synchronize value with C# code,
  // it must be the same with C#'s, this problem bother me a whole afternoon, shit!
  // (Comment by Lukiya 3/22/2011 18:18)
  SetCookie(COOKIE_PAGE_INDEX, index, null, '/');
}
function UpdatePageStatus() {
  $('.txtGoto').val(GetIndex());
  $('.TotalRecord').text(TOTAL_AMOUNT);
  TOTAL_PAGE = Math.ceil(TOTAL_AMOUNT / PAGE_SIZE);
  TOTAL_PAGE = TOTAL_PAGE > 0 ? TOTAL_PAGE : 1;
  $('.TotalPage').text(TOTAL_PAGE);
}
function GoFirst(callback) {
  SetIndex(1);
  BindData();
}
function GoPrevious() {
  var index = GetIndex();
  if (index > 1) {
    index--;
  }
  SetIndex(index);
  //
  BindData();
}
function GoNext() {
  var index = GetIndex();
  if (TOTAL_PAGE > 1 && index < TOTAL_PAGE) {
    index++;
  }
  SetIndex(index);
  BindData();
}
function GoLast() {
  SetIndex(TOTAL_PAGE);
  BindData();
}
function GoIndex(i) {
  if (IsInt(i)) {
    if (i > 0 && i <= TOTAL_PAGE) {
      SetIndex(i);
      BindData();
    }
  }
}
function OrderBy(fieldName) {
  if (IS_ASC) {
    ORDER_BY = fieldName + " DESC"
    IS_ASC = false;
  }
  else {
    ORDER_BY = fieldName + " ASC"
    IS_ASC = true;
  }
  BindData();
}
// Show All
// ***********************************************************************************************************************************
function ShowAll(boxExpress) {
  SetIndex(1);
  //
  if (IsNullOrWhiteSpace(boxExpress)) {
    ResetValue(".searchbox");
  }
  else {
    ResetValue(boxExpress);
  }
  //
  BindData();
}
// Reset Value
// ***********************************************************************************************************************************
function ResetValue(boxExpress) {
  $(boxExpress + " input[type='text']").val('');
  $(boxExpress + " select").val('');
  $(boxExpress + " textarea").val('');
  $(boxExpress + " input[type='checkbox']").prop("checked", false);
  $(boxExpress + " input[type='radio']").prop("checked", false);
}
// CopyTo
// ***********************************************************************************************************************************
function CopyTo(obj, copyToExpression) {
  var value = $(obj).val();
  var array = copyToExpression.split('|');
  for (var i = 0; i < array.length; i++) {
    $(array[i]).val(value);
  }
}
// GetBoolText
// ***********************************************************************************************************************************
function GetBoolText(flag) {
  if (flag) {
    return '<label class="icon01"/>';
  }
  else {
    return '';
  }
}
// SwitchPic
// ***********************************************************************************************************************************
function SwitchPic(obj, pic_1, pic_2) {
  $(obj).attr("class", "big");
  $(obj).attr("src", pic_2);
  $(obj).attr("onclick", "SwitchPic(this, '" + pic_2 + "','" + pic_1 + "');");
  //
  if (pic_1.indexOf('_s') > -1) {
    $(obj).parent().addClass("picbox");
  }
  else {
    $(obj).parent().removeClass("picbox");
  }
  //
  $(obj).parent().find(".picinfo").toggle();
}
// Fill
// ***********************************************************************************************************************************
function Fill(obj) {
  return IsNullOrWhiteSpace(obj) ? '' : obj;
}
