var rowId = "";
var rowSourceID = "";
var winLoc = window.location.href.toLowerCase();
var First_Load = true;
var lastWindowLoad = null;
var sConfirmMessage = "";
var symbol = "";
var bPosting = false;
var bCancelSubmit = false;
var helpLocation = "http://mercosi.com/Help/Default.aspx";

if (winLoc.indexOf("?") > -1) { winLoc = winLoc.substring(0, winLoc.indexOf("?")); }

if (winLoc.indexOf("/reports/") > -1) {
  document.write('<script type="text/javascript" src="..\\Include\\Scripts2.js"></script>');
  document.write('<script type="text/javascript" src="..\\Include\\Report.js"></script>');
  document.write('<script type="text/javascript" src="..\\Include\\Report_Loader.js"></script>');
  document.write('<script type="text/javascript" src="..\\Include\\MenuFunctions.js"></script>');
}
else {
  document.write('<script type="text/javascript" src="Include\\Scripts2.js"></script>');
  document.write('<script type="text/javascript" src="Include\\Report.js"></script>');
  document.write('<script type="text/javascript" src="Include\\Report_Loader.js"></script>');
  document.write('<script type="text/javascript" src="Include\\MenuFunctions.js"></script>');
}

function getPageName(tmpLoc) {
  while (tmpLoc.indexOf("/") > -1) {
    tmpLoc = tmpLoc.substring(tmpLoc.indexOf("/") + 1);
  }
  return tmpLoc;
}

function redirect(rDirect) {
  if (rDirect == null) {
    rDirect = "frmLogout.aspx?Logout=True";
  }
  if (rDirect.indexOf("?") > -1) {
    rDirect = rDirect + "&";
  }
  else {
    rDirect = rDirect + "?";
  }
  try {
    if (winLoc.indexOf("/reports/") > -1) { rDirect = "../" + rDirect; }
    top.location.href = rDirect + "From_Page=" + getPageName(top.location.href);
  }
  catch (err) {
    if (winLoc.indexOf("/reports/") > -1) { rDirect = "../" + rDirect; }
    top.location.href = rDirect + "From_Page=" + getPageName(top.location.href);
  }
}

var oheight;
var owidth;
var Enlarge_Grid_Rows = false;
var showAlert_Visable = false;
var minDate = "1900/01/02";
var maxDate = "9999/12/30";

function Hide_Loading() {
  try {
    var Loading = getTBObj("Loading");
    if (showAlert_Visable == false) {
      showDropDowns();
    }
    if (Loading != null && Loading.src == null) {
      Loading.style.display = "none";
    }
    else {
      if (Loading != null) { Loading.parentNode.removeChild(Loading); }
      Create_Loading("none");
    }
  }
  catch (err) { }
}

function Create_Loading(dsp) {
  if (winLoc.indexOf("default.aspx") == -1) {
    var Loading = document.createElement("table");
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td = document.createElement("td");

    Loading.style.height = "100%";
    Loading.style.width = "100%";
    Loading.cellSpacing = "0";
    Loading.cellPadding = "0";
    Loading.onclick = "return false;";

    Loading.appendChild(tbody);
    tbody.appendChild(tr);
    tr.appendChild(td);
    td.align = "center";
    var src = "";
    if (winLoc.indexOf("/reports/") > -1) {
      src = "../";
    }
    td.innerHTML = "<table cellspacing='0' cellpadding='5' style='border: 3px solid #6E89DD; background-color: #EEF0FC; width:300px;height:55px'>"
              + "<tbody><tr><td width='32'><img src='" + src + "img/loading_Icon.gif'/>"
              + "</td><td align='left'>Please wait.<br/>Loading...</td>"
              + "</tr></tbody></table>";

    Loading.id = "Loading";
    Loading.style.zIndex = "10005";
    Loading.style.left = "0";
    Loading.style.position = "absolute";
    Loading.style.top = "0";
    document.body.appendChild(Loading);
    Loading.style.display = dsp;
  }
}

function Show_Loading() {
  try {
    try {
      if ((window.frameElement != null) && (window.frameElement.id.toLowerCase() != "main")) {
        return;
      }
    }
    catch (e) { }
    var Loading = getTBObj("Loading");
    hideDropDowns();
    if (Loading != null) {
      Loading.style.display = "";
    }
    else {
      Create_Loading("");
    }
  }
  catch (err) { }
}

function hideDropDowns() {
  var drop = getTags("SELECT");
  for (var p = 0; p < drop.length; p++) {
    if (drop[p].id.indexOf("WebCalendar") < 0) {
      drop[p].style.visibility = "hidden";
    }
  }
}

function showDropDowns() {
  var drop = getTags("SELECT");
  for (var p = 0; p < drop.length; p++) {
    if (drop[p].id.indexOf("WebCalendar") < 0) {
      drop[p].style.visibility = "visible";
    }
  }
}

var HighlightColour;
var SelectColour;
var MultiSelectColour;
var BorderColour;
var BackgroundColour;
var AlternateBackgroundColour;
var HeaderColour;
var ButtonColour;
var ButtonHighlightColour;


function getParentValue(Name, BlankVal) {
  if (BlankVal == null) { BlankVal = ""; }
  var tmpVal;
  tmpVal = getTBObj(Name);
  try {
    if (tmpVal == null && opener != null) {
      try {
        tmpVal = opener.getTBObj(Name);
      } catch (err) { tmpVal = null; }
    }
    if (tmpVal == null && parent != null) {
      try {
        tmpVal = parent.getTBObj(Name);
      } catch (err) { tmpVal = null; }
    }
  }
  catch (err) {
  }
  if (tmpVal != null) { tmpVal = tmpVal.value; }
  else { tmpVal = BlankVal; }
  temp = getTBObj(Name);
  if (temp == null) {
    temp = document.createElement("INPUT");
    temp.id = Name;
    temp.type = "text";
    temp.className = "hide";
    document.body.appendChild(temp);
  }
  temp.value = tmpVal;

  return tmpVal;
}

function Show_Demo() {
  var imgDemoVersion = getTBObj("imgDemoVersion");
  if (imgDemoVersion == null && winLoc.indexOf("default.aspx") == -1
        && winLoc.indexOf("frmbanner_logo.aspx") == -1
        && winLoc.indexOf("frmsystem_update.aspx") == -1
        && winLoc.indexOf("selector.aspx") == -1
        && winLoc.indexOf("frmfirst_screen.aspx") == -1) {
    var img = document.createElement("IMG");
    img.id = "imgDemoVersion";
    img.src = "img/demo.gif";
    img.style.zIndex = "10005";
    img.style.left = "0";
    img.style.position = "absolute";
    img.style.top = "0";
    img.style.width = '800';
    img.style.height = '600';
    document.body.appendChild(img);
    imgDemoVersion = getTBObj("imgDemoVersion");
    hideDropDowns();
    var str = "try {getTBObj('imgDemoVersion').style.display = 'none';} catch(err){} showDropDowns();";
    setTimeout(str, 500);
  }
}

function LoadParentValues(SecondLoad) {
  if (winLoc.indexOf("frmhtml_preview.aspx") == -1 && winLoc.indexOf("frmset_value.aspx") == -1) {
    var Report_URL;
    var Is_Demo;
    Report_URL = getParentValue("txtReport_URL");
    Is_Demo = getParentValue("txtIs_Demo");
    if (Is_Demo == "true") {
      setTimeout("Show_Demo();", 5000);
    }
    HighlightColour = getParentValue("HighlightColour", "red");
    SelectColour = getParentValue("SelectColour", "PowderBlue");
    MultiSelectColour = getParentValue("MultiSelectColour", "lightgreen");
    BorderColour = getParentValue("BorderColour", "Silver");
    BackgroundColour = getParentValue("BackgroundColour", "White");
    AlternateBackgroundColour = getParentValue("AlternateBackgroundColour", "WhiteSmoke");
    HeaderColour = getParentValue("HeaderColour", "#e5ecf9");
    ButtonColour = getParentValue("ButtonColour", "WhiteSmoke");
    ButtonHighlightColour = getParentValue("ButtonHighlightColour", "lightgreen");
    Has_Address_Bar = getParentValue("Has_Address_Bar");

    if (Report_URL == "" && winLoc.indexOf("default.aspx") == -1) {
      if (SecondLoad != true && window.location.href.indexOf("NoSecondLoad") == -1) {
          setTimeout("LoadParentValues(true);", 500);
        return false;
      }
      setTBVal("txtReport_URL", "http://" + window.location.host + ":8080/MercoReports");
    }
    Change_StyleSheets();
  }
}

function keyDown(e) {
  var code = KeyCode(e);
  if (code == 71 && e.ctrlKey) {
    if (Enlarge_Grid_Rows == true) {
      Enlarge_Grid_Rows = false;
      changeStyle("format.css", ".gridRowHeight", "white-space: nowrap; height:20px;");
    }
    else {
      Enlarge_Grid_Rows = true;
      changeStyle("format.css", ".gridRowHeight", "height:100px;");
    }
    return false;
  }
  else if (code == 112) {
    loadHelp();
    return false;
  } else if (code == 13) {
    var obj = getSrcObj(e);
    if (obj.tagName.toLowerCase() != "textarea" && obj.tagName.toLowerCase() != "td" && obj.type != "submit" && obj.type != "button" && obj.type != "password" && obj.tagName.toLowerCase() != "div") {
      var bReturn = tabOnEnter(e);
      return cancelEvents(e, false);
    }
  }
}

function tabOnEnter(e) {
  var keyCode = KeyCode(e);
  if (keyCode != 13) {
    return true;
  }
  else {
    var obj = getNextField(getSrcObj(e));
    if(obj && obj != null){
      obj.focus();
    }
    return false;
  }
}

function getNextField(field) {
  var form = field.form;
  if (form != null) {
    for (var e = 0; e < form.elements.length; e++)
      if (field == form.elements[e])
      break;
    var obj = form.elements[++e % form.elements.length];
    if (!checkObjTypeForFocus(obj)) { return getNextField(obj); }
    return obj;
  }
  return form;
}

function loadHelp() {
  LoadWindow(helpLocation + '?Page=' + getPageName(winLoc), 'Help');
  return false;
}

document.onhelp = new Function("return false;");

function formatMessage(Message) {
  if ((Message != null) && (Message != "[object]")) {
    Message = ReplaceAll(Message, "<br />", "<br />");
    Message = ReplaceAll(Message, "<br /><br />", "<br />");
    Message = ReplaceAll(Message, "@@qt", "'");
  }
  return Message;
}

function ReplaceAll(sMessage, sReplace, sWith) {
  sMessage = sMessage + "";
  var p = 0;
  while (sMessage.indexOf(sReplace) > -1 && p < 100) {
    sMessage = sMessage.replace(sReplace, sWith);
    p++;
  }
  return sMessage;
}

function TrimVal(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showAlert(e, Message, withAlert) {
  Message = formatMessage(Message);
  if (withAlert == "true") {
    Message = ReplaceAll(Message, "<br />", "\r\n");
    alert(Message);
  }
  else {
    showAlert_Visable = true;
    var div = document.createElement("DIV");
    var btn = document.createElement("INPUT");
    var text = document.createElement("H3");

    text.innerHTML = Message;

    btn.type = "button";
    btn.className = "button";
    btn.value = "OK!";

    btn.onclick = function() {
      div.style.visibility = "hidden";
      showAlert_Visable = false;
      showDropDowns();
    }

    btn.onkeydown = function() {
      div.style.visibility = "hidden";
      showAlert_Visable = false;
      showDropDowns();
    }

    div.appendChild(text);
    div.appendChild(btn);

    document.onclick = function() {
      div.style.visibility = "hidden";
      showAlert_Visable = false;
      showDropDowns();
    }

    div.onclick = function() {
      e.cancelBubble = true;
      showAlert_Visable = false;
      return false;
    }

    div.style.position = "absolute";
    div.className = "message";
    hideDropDowns();
    document.body.appendChild(div);
    var divw = div.clientWidth;
    var divh = div.clientHeight;

    if (divw < 200) {
      divw = 200;
      div.style.width = 200;
    }

    div.style.top = (document.body.clientHeight / 2) - (divh / 2);
    div.style.left = (document.body.clientWidth / 2) - (divw / 2);

    try {
      btn.focus();
    }
    catch (err) {
    }
    div.style.visibility = "visible";
  }
}

function attachObjectEvents() {
  var obj = getTags("DIV");
  for (var ci = 0; ci < obj.length; ci++) {
    var div = obj[ci];
    var hasBeenChanged = (div.getAttribute("hasBeenChanged") == null) ? "false" : (div.getAttribute("hasBeenChanged").toString());

    if (hasBeenChanged != "true") {
      div.setAttribute("hasBeenChanged", true);
      if (div.className == "editstyle" &&
          winLoc.indexOf("_cube.aspx") == -1 &&
			    winLoc.indexOf("_view.aspx") == -1 &&
			    winLoc.indexOf("_analysis.aspx") == -1 &&
          div.style.width == "") {
        if (browser == "Internet Explorer") {
          div.style.width = "0";
          div.style.width = div.style.width.replace("px", "") + 10;
        }
        div.style.height = div.clientHeight + 2;
      }
      else if (div.className == "dropdown") {
        var divTB = getFirstElement(div);
        var divIMG = getNextElement(divTB);
        if (divIMG.id == null) {
          divIMG = getNextElement(divIMG);
        }
        if (divIMG != null && divIMG.getAttribute("multiselect") != null && divIMG.getAttribute("multiselect").toLowerCase() == "true") {
          div.style.borderColor = "darkseagreen";
          if (div.title != "") { div.title += "<br />" };
          div.title = div.title + "This selector is multi-selectable";
        }
        else {
          div.style.borderColor = "darkgray";
        }
        if (div.oncontextmenu != null) {
          div.style.borderStyle = "double";
          div.style.borderWidth = "3px";
          if (div.title != "") { div.title += "<br />" };
          div.title = div.title + "Right mouse click for extra options";
          try {
            divTB.style.width = divTB.style.width.replace("px", "") - 3;
            if (browser != "Internet Explorer") {
              div.style.width = div.style.width.replace("px", "") - 6 + "px";
            }
          } catch (err) { }
        }
        else {
          div.style.borderStyle = "solid";
          div.style.borderWidth = "1px";
          if (browser != "Internet Explorer") {
            div.style.width = div.style.width.replace("px", "") - 2 + "px";
          }
        }
      }
    }
  }
  var objs = getTags("INPUT");
  for (var p = 0; p < objs.length; p++) {
    var btn = objs[p];
    if (btn.getAttribute("hasBeenChanged") != true) {
      btn.setAttribute("hasBeenChanged", true);
      if ((btn.className == "" || btn.className == "button") && (btn.type == "submit" || btn.type == "button")) {
        btn.className = "button";
        addCustomEvent(btn, "mouseover", btnMouseOver);
        addCustomEvent(btn, "mouseout", btnMouseOut);
        if (btn.type == "submit" && btn.onclick == null) {
          addCustomEvent(btn, "click", clickButton);
        }
      }
      try {
        if (objs[p].className == "droptextbox") {
          addCustomEvent(objs[p], "keydown", SelectorKeyDown);
          addCustomEvent(objs[p], "change", tbSelectorChange);
          addCustomEvent(objs[p], "focus", SetSelectorFocus);
          addCustomEvent(objs[p], "mouseup", SetSelectorMouseUp);
        }
        else if (objs[p].getAttribute("Search_Table") != null && objs[p].getAttribute("Search_Table") != "") {
          addCustomEvent(objs[p], "keydown", DoQuickSearch);
          addCustomEvent(objs[p], "blur", hideFrameInASec);
        }
      }
      catch (e) { }
    }
  }

  var objs = getTags("TEXTAREA");
  for (var p = 0; p < objs.length; p++) {
    if (objs[p].getAttribute("hasBeenChanged") != true) {
      objs[p].setAttribute("hasBeenChanged", true);
      if (objs[p].getAttribute("custommaxlength") != null) {
        addCustomEvent(objs[p], "keypress", setLengthKeypress);
        addCustomEvent(objs[p], "beforepaste", setLengthBeforePaste);
        addCustomEvent(objs[p], "paste", setLengthPaste);
      }
      if (objs[p].getAttribute("Search_Table") != null && objs[p].getAttribute("Search_Table") != "") {
        addCustomEvent(objs[p], "keydown", DoQuickSearch);
        addCustomEvent(objs[p], "blur", hideFrameInASec);
      }
    }
  }
  var images = getTags("IMG");
  for (var p = 0; p < images.length; p++) {
    var img = images[p];
    if (img.getAttribute("hasBeenChanged") != true) {
      img.setAttribute("hasBeenChanged", true);
      addCustomEvent(img, "error", ChangeErrorImage);
      if (img.getAttribute("src") != null) {
        if (img.getAttribute("src").toLowerCase().indexOf("undelete.gif") < 0 && img.getAttribute("src").toLowerCase().indexOf("delete.gif") > -1) {
          var loc = new String(window.location);

          if (loc.indexOf("frmVacancies") < 0
                    && loc.indexOf("frmWritten_Off") < 0 && loc.indexOf("frmOverview.aspx") < 0) {
            addCustomEvent(img, "click", ConfirmDelete);
            if (loc.indexOf("frmEmployee_Expense_Edit.aspx") < 0) {
              var runUnloadEvent = getTBObj("runUnloadEvent");
              if (runUnloadEvent != null) {
                runUnloadEvent.value = "false";
              }
            }
          }
        }
        else if (img.getAttribute("src").toLowerCase().indexOf("printer.jpg") > -1) {
          addCustomEvent(img, "click", Load_Report_Menu);
          addCustomEvent(img, "contextmenu", Load_Report_Menu1);
          img.getAttribute("style").cursor = "hand";
          img.title = img.title + '.<br /> Hold shift and click or right mouse click to bring up advanced options and to be able to email report';
        }
        else if (img.getAttribute("src").toLowerCase().indexOf("img/cb.jpg") > -1 || img.getAttribute("src").toLowerCase().indexOf("img/cbchecked.jpg") > -1) {
          addCustomEvent(img, "mouseover", cbMouseOver);
          addCustomEvent(img, "mouseout", cbMouseOut);
        }
        else if (img.getAttribute("financialselector") == "true") {
          initFininacilPeriodSelector(img);
        }
      }
    }
  }
}

function initAllValues(e) {
  if (First_Load == false) {
    return true;
  }
  First_Load = false;

  try {
    var tmpLoc = getPageName(winLoc);
    if (tmpLoc.toLowerCase().indexOf("default.aspx") == -1) {
      if (tmpLoc.toLowerCase().substring(0, 3) == "frm") {
        tmpLoc = tmpLoc.substring(3);
      }
      tmpLoc = ReplaceAll(tmpLoc.substring(0, tmpLoc.indexOf(".")), "_", " ");
      if (tmpLoc != "") {
        document.title = "Merco - " + tmpLoc;
      }
    }
  }
  catch (err) { }
  try {
    LoadParentValues();
  } catch (err) { }
  Hide_Loading();
  addCustomEvent(window.document, "mouseover", initTitle);
  addCustomEvent(window.document, "mouseout", hideTitle);
  addCustomEvent(window.document.body, "submit", onSubmit);
  attachObjectEvents();
  setTabIndexes();
}

function ChangeErrorImage(e) {
  var imgOBJ = getSrcObj(e);
  if (imgOBJ.getAttribute("Changed2")) {
  }
  else if (imgOBJ.getAttribute("Changed1")) {
    imgOBJ.setAttribute("Changed2", true);
    getSrcObj(e).src = "../Other Files/NA.gif";
    getSrcObj(e).OriginalSize = "100";
  }
  else {
    imgOBJ.setAttribute("Changed1", true);
    getSrcObj(e).src = "Other Files/NA.gif";
    getSrcObj(e).OriginalSize = "100";
  }
}


function MakeImageBigger(e) {
  if (getSrcObj(e).OriginalSize < 100) {
    getSrcObj(e).style.height = getSrcObj(e).OriginalSize;
  }
  else {
    getSrcObj(e).style.height = "100px";
  }
}

function MakeImageSmaller(e) {
  getSrcObj(e).style.height = getSrcObj(e).SmallSize;
}

function Update_Value(e, Table_Name, Column_Name, Value, ID_Name, ID, ValueIsDate, updateSuccessfull, updateFailed, Column_Value) {
  if (updateSuccessfull == null || updateSuccessfull == "") {
    updateSuccessfull = "";
  }
  else
    updateSuccessfull = "parent." + updateSuccessfull;
  if (Column_Value == null || Column_Value == "") {
    Column_Value = "";
  }
  if (Value == "geTbValue") {
    Value = getSrcObj(e).value;
  }
  if (updateFailed == null || updateFailed == "") {
    updateFailed = "";
  }
  else
    updateFailed = "parent." + updateFailed;
  Value = "" + Value;
  var frm = getTBObj("frmUpdate_Value");
  if (frm != null) {
    frm.src = "frmSet_Value.aspx?Table_Name=" + Table_Name
          + "&Column_Name=" + Column_Name + "&Value=" + Check_Query_Value(Value)
          + "&ID_Name=" + ID_Name + "&ID=" + ID + "&ValueIsDate=" + ValueIsDate
          + "&updateSuccessfull=" + updateSuccessfull + "&updateFailed=" + updateFailed
          + "&Column_Value=" + Column_Value;
  }
  else {
    alert("frmUpdate_Value Frame is Missing");
  }
  Hide_Loading();
}

function Check_Query_Value(Value) {
  Value = ReplaceAll(Value, "&", "@AND");
  Value = ReplaceAll(Value, "?", "@QUE");
  Value = ReplaceAll(Value, "#", "@HAS");
  Value = ReplaceAll(Value, "\r\n", "@NL");
  Value = ReplaceAll(Value, "<", "@LT");
  Value = ReplaceAll(Value, ">", "@GT");
  return Value;
}

function Strip_HTML(Value) {
  Value = ReplaceAll(Value, ">", " ");
  Value = ReplaceAll(Value, "<", " ");
  return Value;
}

function imgChangeCB(img) {
  var val = "";
  if (img.src.indexOf("img/cbchecked-h.jpg") > -1 || img.src.indexOf("img/cbchecked.jpg") > -1) {
    img.src = "img/cb-h.jpg";
    val = "0";
  }
  else {
    img.src = "img/cbchecked-h.jpg";
    val = "1";
  }
  return val;
}

function CheckedBoxClicked(e, img) {
  if (img == null || img.id == null || img.id == "") {
    img = getSrcObj(e);
  }
  var val = imgChangeCB(img);

  if (img.getAttribute("colval") != null) {
    Update_Value(e, img.getAttribute("tblName"), img.getAttribute("colname"), val, img.getAttribute("ID_Name"),
         img.id, '', '', '', img.getAttribute("colval"));
  }
  else {
    Update_Value(e, img.getAttribute("tblName"), img.getAttribute("colname"), val, img.getAttribute("ID_Name"),
         img.id, '', '', '', '');
  }
}

function Change_StyleSheets() {
  var c = -1;
  for (var r = 0; r < document.styleSheets.length; r++) {
    if (document.styleSheets[r].href.toLowerCase().indexOf("format.css") > -1) {
      c = r;
      r = document.styleSheets.length;
    }
  }

  try {
    if (c > -1) {
      addStyle(c, ".gridheader"
             + "{"
             + "   font-weight: bold;"
             + "   color:Black;"
             + "   height:20px;"
             + "   background-color:" + HeaderColour + ";"
             + "}", -1);

      addStyle(c, ".gridselected"
             + "{"
             + "   font-weight: bold;"
             + "   wrap:False;"
             + "   color:Black;"
             + "   background-color:" + SelectColour + ";"
             + "}", -1);

      addStyle(c, ".griditem"
             + "{ "
             + "   wrap:False;"
             + "   height:23px;"
             + "   color:Black;"
             + "   background-color:" + BackgroundColour + "; "
             + "}", -1);

      addStyle(c, ".gridalternatingitem"
             + "{"
             + "   wrap:False;"
             + "   height:23px;"
             + "   color:Black;"
             + "   background-color:" + AlternateBackgroundColour + ";"
             + "}", -1);

      addStyle(c, ".gridpager"
             + "{"
             + "	color:Black;"
             + "	background-color:" + BackgroundColour + ";"
             + "}", -1);

      addStyle(c, ".gridfootercalcs"
             + "{"
             + "	color:Black;"
             + "	background-color:" + BackgroundColour + ";"
             + "   font-weight:bold;"
             + "   Text-align: Right;"
             + "}", -1);

      addStyle(c, ".gridfooter"
             + "{"
             + "	color:Black;"
             + "	background-color:" + BackgroundColour + ";"
             + "}", -1);

      addStyle(c, ".button"
             + "{"
             + "	FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=" + ButtonColour + ",endColorStr=gainsboro)"
             + "		progid:DXImageTransform.Microsoft.Alpha(Opacity=80);"
             + "	border-width:1px;"
             + "	border-color:" + ButtonColour + ";"
             + "  background-color:" + ButtonColour + "; "
             + "	font-weight:bold;"
             + "	color: black;"
             + "	font-family: 'Verdana';"
             + "    font-size: 7pt;"
             + "    height:22px;"
             + "}", -1);

      addStyle(c, ".buttonh"
             + "{"
             + "	FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=" + ButtonHighlightColour + ",endColorStr=gainsboro)"
             + "		progid:DXImageTransform.Microsoft.Alpha(Opacity=80);"
             + "	border-width:1px;"
             + "	border-color:" + ButtonHighlightColour + ";"
             + "  background-color:" + ButtonHighlightColour + "; "
             + "	font-weight:bold;"
             + "	color: black;"
             + "	font-family: 'Verdana';"
             + "    font-size: 7pt;"
             + "    height:22px;"
             + "}", -1);
    }
  }
  catch (err) { }
}

function addStyle(c, sValue, iPos) {
  if (document.styleSheets[c].cssRules != null) {
    if (iPos == -1) { iPos = document.styleSheets[c].cssRules.length; }
    document.styleSheets[c].insertRule(sValue, iPos);
  }
  else {
    if (iPos == -1) { document.styleSheets[c].cssText += sValue; }
    else { document.styleSheets[c].cssText = sValue + document.styleSheets[c].cssText; }
  }
}

function changeStyle(sStyleSheet, sName, sValue) {
  var c = -1;
  for (var r = 0; r < document.styleSheets.length; r++) {
    if (document.styleSheets[r].href.toLowerCase().indexOf("format.css") > -1) {
      c = r;
      r = document.styleSheets.length;
    }
  }
  if (c > -1) {
    if (document.styleSheets[c].cssRules != null) {
      for (var r = 0; r < document.styleSheets[c].cssRules.length; r++) {
        if (document.styleSheets[c].cssRules[r].selectorText.toLowerCase() == sName.toLowerCase()) {
          document.styleSheets[c].cssRules[r].style.cssText = sValue;
        }
      }
    }
    else {
      for (var r = 0; r < document.styleSheets[c].rules.length; r++) {
        if (document.styleSheets[c].rules[r].selectorText.toLowerCase() == sName.toLowerCase()) {
          document.styleSheets[c].rules[r].style.cssText = sValue;
        }
      }
    }
  }
}

var detect = navigator.userAgent.toLowerCase();
var OS, browser, thestring;
var version = 0;

if (checkIt('konqueror')) {
  browser = "Konqueror";
  OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari";
else if (checkIt('omniweb')) browser = "OmniWeb";
else if (checkIt('opera')) browser = "Opera";
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab";
else if (checkIt('msie')) browser = "Internet Explorer";
else if (!checkIt('compatible')) {
  browser = "Netscape Navigator";
  version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS) {
  if (checkIt('linux')) OS = "Linux";
  else if (checkIt('x11')) OS = "Unix";
  else if (checkIt('mac')) OS = "Mac";
  else if (checkIt('win')) OS = "Windows";
  else OS = "an unknown operating system";
}

function checkIt(string) {
  place = detect.indexOf(string) + 1;
  thestring = string;
  return place;
}

if ((winLoc.indexOf("frmreports.aspx") == -1) &&
        (winLoc.indexOf("frmemployee_efficiency.aspx") == -1) &&
        (winLoc.indexOf("frmwip.aspx") == -1)) {
  addCustomEvent(window, "beforeunload", Show_Loading);
}

function setLengthKeypress(e) {
  var txtArea = getSrcObj(e);
  maxLength = txtArea.custommaxlength;
  value = txtArea.value;
  if (maxLength && value.length > maxLength - 1) {
    e.returnValue = false;
    maxLength = parseInt(maxLength);
  }
}
function setLengthBeforePaste(e) {
  var txtArea = getSrcObj(e);
  maxLength = txtArea.custommaxlength;
  if (maxLength) {
    e.returnValue = false;
  }
}
function setLengthPaste(e) {
  var txtArea = getSrcObj(e);
  maxLength = txtArea.custommaxlength;
  value = txtArea.value;
  if (maxLength) {
    e.returnValue = false;
    maxLength = parseInt(maxLength);
    var oTR = txtArea.document.selection.createRange();
    var iInsertLength = maxLength - value.length + oTR.text.length;
    var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
    oTR.text = sData;
  }
}

function ReplaceChars(Val, Curr) {
  if (Curr != null) {
    Val = Val.replace(Curr, "");
  }
  Val = ReplaceAll(Val, ",", "");
  return Val;
}

function Remove_Numbers(tmpVal) {
  tmpVal = Remove_Character(tmpVal, "0");
  tmpVal = Remove_Character(tmpVal, "1");
  tmpVal = Remove_Character(tmpVal, "2");
  tmpVal = Remove_Character(tmpVal, "3");
  tmpVal = Remove_Character(tmpVal, "4");
  tmpVal = Remove_Character(tmpVal, "5");
  tmpVal = Remove_Character(tmpVal, "6");
  tmpVal = Remove_Character(tmpVal, "7");
  tmpVal = Remove_Character(tmpVal, "8");
  tmpVal = Remove_Character(tmpVal, "9");
  tmpVal = Remove_Character(tmpVal, ".");
  tmpVal = Remove_Character(tmpVal, "-");
  return tmpVal;
}

function Remove_Character(tmpVal, tmpChar) {
  tmpVal = ReplaceAll(tmpVal, tmpChar, "");
  return tmpVal;
}

function addCustomEvent(obj, sEventName, CustomeEvent) {
  if (obj.addEventListener) {
    obj.removeEventListener(sEventName, CustomeEvent, false);
    obj.addEventListener(sEventName, CustomeEvent, false);
  }
  else {
    obj.detachEvent("on" + sEventName, CustomeEvent)
    obj.attachEvent("on" + sEventName, CustomeEvent);
  }
  return true;
}

function TriggerEvent(obj, sEventName) {
  if (document.createEventObject) {
    obj.fireEvent("on" + sEventName);
  }
  else {
    var evt = document.createEvent("HTMLEvents");
    if (evt.initEvent) {
      evt.initEvent(sEventName, true, true);
    }
    if (obj.dispatchEvent) {
      obj.dispatchEvent(evt);
    }
  }
}

function getTags(tName, obj) {
  var items;
  if (obj == null) {
    if (document == null) {
      return new Array();
    }
    obj = document;
  }
  if (obj.all == null || obj.all.tags == null) {
    items = obj.getElementsByTagName(tName);
  }
  else {
    if (tName == "") {
      items = obj.all;
    }
    else {
      items = obj.all.tags(tName);
    }
  }
  return items;
}

function KeyCode(e) {
  if (e == null) { e = event; }
  if (e.which != null && e.which != 0) {
    var code = e.which;
  }
  else {
    var code = e.keyCode;
  }
  return code;
}

function ShiftKey(e) {
  if (e == null) { e = event; }
  if (e.shiftKey != null) {
    var bShift = e.shiftKey;
  }
  else {
    var bShift = (KeyCode(e) == 16) ? true : false;
  }
  return bShift;
}

function getSrcObj(e) {
  if (e == null) { e = window.event; }
  if (e != null) {
    if (e.srcElement) {
      var obj = event.srcElement;
    }
    else {
      var obj = e.target;
    }
  }
  else {
    var obj = e;
  }
  return obj;
}

function setCurrencySymbol(val, branchCurr) {
  if (getTBObj("Currency_Symbol") != null) {
    symbol = getTBVal("Currency_Symbol");
  }
  if (val != null && val != "") {
    val = ReplaceAll(val, ",", "");
    var tmpSymbol = Remove_Numbers(val);
    if (tmpSymbol != "") {
      if ((getTBObj("Currency_Symbol") != null) && (branchCurr == undefined)) {
        setTBVal("Currency_Symbol", tmpSymbol);
      }
      symbol = tmpSymbol;
    }
  }
}

function getFloatVal(val, branchCurr) {
  val = val + "";
  setCurrencySymbol(val, branchCurr);

  if (symbol != "") {
    val = val.replace(symbol, "");
  }
  val = ReplaceAll(val, ",", "");
  if (val == "") { val = "0"; }
  return parseFloat(val);
}

function getGL_Account(Branch_ID, Payment_Method_ID, GL_Search, GL_ID) {
  var frm = getTBObj("dropDownFrame");
  if (frm != null) {
    frm.src = "frmGL_Java.aspx?Get_GL=true"
         + "&Branch_ID=" + Branch_ID
         + "&Payment_Method_ID=" + Payment_Method_ID
         + "&GL_Search=" + GL_Search
         + "&GL_ID=" + GL_ID;
  }
  else {
    alert("dropDownFrame Frame is Missing");
  }
}

function getGL_Account_2(Branch_ID, Cash_Drawer_ID, Payment_Method_ID, GL_Search, GL_ID) {
  var frm = getTBObj("dropDownFrame");
  if (frm != null) {
    frm.src = "frmGL_Java.aspx?Get_GL_With_CashDrawerID=true"
         + "&Branch_ID=" + Branch_ID
         + "&Cash_Drawer_ID=" + Cash_Drawer_ID
         + "&Payment_Method_ID=" + Payment_Method_ID
         + "&GL_Search=" + GL_Search
         + "&GL_ID=" + GL_ID;
  }
  else {
    alert("dropDownFrame Frame is Missing");
  }
}

function getPayment_Method(Branch_ID, Payment_Method_Search, Payment_Method_ID, GL_Search, GL_ID) {
  var frm = getTBObj("dropDownFrame");
  if (frm != null) {
    frm.src = "frmGL_Java.aspx?Get_PM=true"
         + "&Branch_ID=" + Branch_ID
         + "&Payment_Method_Search=" + Payment_Method_Search
         + "&Payment_Method_ID=" + Payment_Method_ID
         + "&GL_Search=" + GL_Search
         + "&GL_ID=" + GL_ID;
  }
  else {
    alert("dropDownFrame Frame is Missing");
  }
}

function clearTb(e, sTextToClear) {
  if (sTextToClear != null) {
    if (getSrcObj(e).value.indexOf(sTextToClear) > -1)
      getSrcObj(e).value = getSrcObj(e).value.replace(sTextToClear, "");
    else
      return false;
  }
  else {
    getSrcObj(e).value = "";
  }
}

function getTBVal(ObjName) {
  var obj = "";
  if (ObjName != "") {
    try {
      obj = getTBObj(ObjName).value;
    } catch (err) { }
  }
  return obj;
}

function getTBObj(ObjName) {
  var obj = null;
  if (ObjName != "") {
    obj = document.getElementById(ObjName);
  }
  return obj;
}

function setTBVal(ObjName, Val) {
  var obj = "";
  try {
    getTBObj(ObjName).value = Val;
  } catch (err) { }
}

function setLBLVal(ObjName, Val) {
  var obj = "";
  try {
    getTBObj(ObjName).innerHTML = Val;
  } catch (err) { }
}

function getLBLVal(ObjName) {
  var obj = "";
  if (ObjName != "") {
    try {
      obj = getTBObj(ObjName).innerHTML;
    } catch (err) { }
  }
  return obj;
}

function setPosting() {
  bPosting = true;
  setTimeout("bPosting = false;", 100);
}

function numericKeyPress(e) {
  var code = KeyCode(e);
  var oValue = getSrcObj(e).value;
  if ((code == 48) && (oValue == "0")) {
    e.cancelBubble = true;
    return false;
  }
  else if (isNumeric(e) || (code == 45) || (code == 46)) {
    return true;
  }
  else {
    e.cancelBubble = true;
    return false;
  }
}

function onSubmit(e) {
  if (bCancelSubmit == true) {
    bCancelSubmit = false;
    return cancelEvents(e, false);
  }
}

function setTabIndexes() {
  var objs = document.getElementsByTagName("*"); ;
  var iTabIndex = 1;
  for (var r = 0; r < objs.length; r++) {
    var control = objs[r];
    if (control.tagName.toLowerCase() == "input" || control.tagName.toLowerCase() == "textarea" || control.tagName.toLowerCase() == "select") {
      if (checkObjTypeForFocus(control)) {
        (checkIt('msie')) ? control.tabIndex = iTabIndex : control.setAttribute("tabindex", iTabIndex);
      } else {
        (checkIt('msie')) ? control.tabIndex = (0 - iTabIndex) : control.setAttribute("tabindex", (0 - iTabIndex));
      }
      iTabIndex++;
    }
  }
}

function checkObjTypeForFocus(control) {
  if (!isVisible(control) || control.getAttribute("readonly") == "readonly"
                          || control.getAttribute("disabled") == "disabled"
                          || control.getAttribute("disabled") == true
                          || control.getAttribute("type") == "image"
                          || control.getAttribute("type") == "hidden") {
    return false;
  }
  return true;
}

function isVisible(obj) {
  if (obj == document) return true;
  if (!obj) return false;
  if (!obj.parentNode) return false;
  if (obj.style) {
    if (obj.style.display == 'none') return false;
    if (obj.style.visibility == 'hidden') return false;
  }
  
  // Standard way
  if (window.getComputedStyle) {
    var style = window.getComputedStyle(obj, "");
    if (style) {
      if (style.display == 'none') return false;
      if (style.visibility == 'hidden') return false;
    }
  }
  
  // IE way
  var style = obj.currentStyle;
  if (style) {
    if (style['display'] == 'none') return false;
    if (style['visibility'] == 'hidden') return false;
  }

  return isVisible(obj.parentNode)
}

function setFocusAfterPostback(objName) {
  var obj = getTBObj(objName);

  if (!obj)
    return;

  if (!checkObjTypeForFocus(obj))
    if (objName.indexOf('ID') != -1)
      obj = getTBObj(objName.replace('ID', 'Search'));

  if (obj && checkObjTypeForFocus(obj)) {
    try {
      obj.focus();
      obj.select();
    } catch (err) { }
  }
}

function cancelEvents(e, toReturn) {
  e.returnValue = toReturn;
  
  if (e.stopPropagation) e.stopPropagation();
  if (e.preventDefault) e.preventDefault();
  
  e.cancelBubble = true;
  e.cancel = true;
  return toReturn;
}

function percent_focus(e) {
  var tb = getSrcObj(e);
  tb.value = rtrim(tb.value.replace('%', ''));
  tb.select();
}

function percent_blur(e) {
  var tb = getSrcObj(e);

  tb.value = round_decimals(rtrim(tb.value.replace('%', '')), 4) + ' %';
}

function percent_change(e) {
  var tb = getSrcObj(e);
  
  if (Number(tb.value) < 0 && tb.getAttribute("allownegative") == "false")
    tb.value = 0;

  if (tb.getAttribute("maxvalue") && Number(tb.value) > tb.getAttribute("maxvalue"))
    tb.value = tb.getAttribute("maxvalue");
}

function percent_keydown(e) {
  var code = KeyCode(e);
  var oValue = getSrcObj(e).value;
  if (isNumeric(e) ||
        (code <= 105 && code >= 96) ||
        code == 45 || code == 8 ||
        code == 37 || code == 39 ||
        code == 46 || code == 9) {
    return true;
  }
  else if (code == 190 || code == 110) {
    if (oValue.indexOf('.') > -1)
      getSrcObj(e).value = oValue.replace('.', '');
    return true;
  }
  else if (code == 189 || code == 109) {
    if (oValue.indexOf('-') > -1)
      return false;
    return true;
  }
  else if (code != 13) return cancelEvents(e, false);
}

addCustomEvent(window, "load", initAllValues);
addCustomEvent(window.document, "keydown", keyDown);
