﻿function pageLoad(sender, args) {
  // buttons mouseover effect
  initializeButtons();

  if ((Sys != null) && (Sys.WebForms != null) && (Sys.WebForms.PageRequestManager != null)) {
    if (typeof (beginRequestHandler) == "function") Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler);
    var myAjaxInstance = Sys.WebForms.PageRequestManager.getInstance();
    myAjaxInstance.add_endRequest(endRequestHandler);
  }

  Sys.Application.add_init(function() {
    if (typeof (alertText) != "undefined")
      if (alertText.length > 0)
      alert(alertText);
    alertText = "";
    if (typeof (CallThisFunction) != "undefined")
      CallThisFunction();
  });
}

function beginRequestHandler(sender, args) {
  
  showLoadingDiv("divPerdea");
}

function endRequestHandler(sender, args) {
  // buttons mouseover effect
  initializeButtons();
  hideLoadingDiv();
}

function hideLoadingDiv() {
  var divToShow = document.getElementById("divLoading");
  var imgToShow = document.getElementById("imgLoading");
  if (divToShow != null) divToShow.style.display = "none";
  if (imgToShow != null) imgToShow.style.display = "none";
}

function showLoadingDiv(div) {
  var divToShowOld = document.getElementById("divLoading");
  var imgToShow = document.getElementById("imgLoading");
  var d = $("#" + div);

  if (divToShowOld) {
    divToShowOld.style.height = d.height().toString() + "px";
    divToShowOld.style.width = d.width().toString() + "px";
    divToShowOld.style.left = d.offset().left.toString() + "px";
    divToShowOld.style.top = d.offset().top.toString() + "px";

    imgToShow.style.top = (d.offset().top + d.height() / 2 - 5).toString() + "px";
    imgToShow.style.left = (d.offset().left + d.width() / 2 - 35).toString() + "px";

    divToShowOld.style.display = "block";
    imgToShow.style.display = "block";
  }
}

function alert(text, showOKButton) {
  if (!$find('popupAlert')) {
    alertText = text;
  }
  else {
    $get('divAlertText').innerHTML = text.replace(/\r\n/g, "<br/>");

    var okButton = document.getElementById($find('popupAlert').get_OkControlID());
    if ((typeof (showOKButton)) != "undefined" && (!showOKButton)) {
      okButton.parentNode.parentNode.style.display = "none";
    }
    else {
      okButton.parentNode.parentNode.style.display = "block";
    }
    
    $find('popupAlert').show();
  }
}

function showAllSelects() {
  var s = document.getElementsByTagName("select");

  for (var i = 0; i < s.length; i++) {
    s[i].style.display = "block";
    s[i].style.visibility = "visible";
  }
}
function search(e, controlToClick) {
  if (!e) var e = window.event;
  var keyCode = (e.which) ? e.which : e.keyCode;

  if (keyCode == 13) {
    __doPostBack(searchButtonName, '');
  }
}

function makeInterfaceReadOnly() {
  var content = $("#content")[0];
  var selects = $("SELECT", content);
  for (var i = 0; i < selects.length; i++) {
    selects[i].style.display = "none";
    var newdiv = document.createElement('div');
    newdiv.innerHTML = selects[i][selects[i].selectedIndex].text;
    selects[i].parentNode.appendChild(newdiv);
  }

  var inputs = $(".textArea", content);
  for (var i = 0; i < inputs.length; i++) {
    if ((inputs[i].isMultiLine) && (inputs[i].style.display != "none")) {
      inputs[i].style.display = "none";
      var newdiv = document.createElement('div');
      newdiv.innerHTML = inputs[i].value;
      inputs[i].parentNode.appendChild(newdiv);
    }
  }
}

function changeButtonBackground(el) {
  var parent = el.parentElement;
  $("td.left_button", parent).css("background-image", "url('../Resources/Images/buttonleft_over.gif')");
  $("td.center_button", parent).css("background-image", "url('../Resources/Images/buttoncenter_over.gif')");
  $("td.right_button", parent).css("background-image", "url('../Resources/Images/buttonright_over.gif')");
}
function restoreButtonBackground(el) {
  var parent = el.parentElement;
  $("td.left_button", parent).css("background-image", "url('../Resources/Images/buttonleft.gif')");
  $("td.center_button", parent).css("background-image", "url('../Resources/Images/buttoncenter.gif')");
  $("td.right_button", parent).css("background-image", "url('../Resources/Images/buttonright.gif')");
}
function initializeButtons() {
  $("td.left_button", $("table.table_button")).hover(
     function() {
       changeButtonBackground(this);
     },
     function() {
       restoreButtonBackground(this);
     }
  ).click(function() {
    $(this).unbind("click");
    var el = $("a", $(this.parentElement))[0];
    if (el.nameProp.indexOf("javascript") == 0) eval(el.pathname);
  });
  $("td.center_button", $("table.table_button")).hover(
     function() {
       changeButtonBackground(this);
     },
     function() {
       restoreButtonBackground(this);
     }
  );
  $("td.right_button", $("table.table_button")).hover(
     function() {
       changeButtonBackground(this);
     },
     function() {
       restoreButtonBackground(this);
     }
  ).click(function() {
    var el = $("a", $(this.parentElement))[0];
    if (el.nameProp.indexOf("javascript") == 0) eval(el.pathname);
  });
}


function displayToolTipIframe(enable)
{
   if (enable==true)
   {
    $(".tooltipIframe").show();
   }
   else
   {
   $(".tooltipIframe").hide();
   }
}

