// --- tools.js -----
var sessionGraceTime = 10; // number of grace minutes till session times out
var sessionTimer;
var showSessionStatus = true;
var formIsClean = true;

function filterInnerText(inStr) {
var outStr = inStr+"";
var re = /a/
outStr = outStr.replace(/&/g,"&amp;");
outStr = outStr.replace(/</g,"&lt;");
outStr = outStr.replace(/>/g,"&gt;");
return outStr;

}

function startSessionTimer(timeout) {
  sessionTimer = setTimeout("displayGraceWarning()",(timeout-sessionGraceTime)*60000)
  if (showSessionStatus) {
    window.defaultStatus ="Session warning timer: "+(timeout-sessionGraceTime)
    if(window.parent != null) {
      parent.window.defaultStatus ="Session warning timer: "+(timeout-sessionGraceTime)
    }
  }
}

function displayGraceWarning() {
  alert("This work session is about to expire. Click the OK button within "+sessionGraceTime+" minutes to "+
      "extend the session")
  var newWin = window.open("http://data.ngumc.org/conf/login/refreshsession.asp","refreshSession","dependent,fullscreen")
  newWin.focus()
}

function parseDollars(str){
if (str=="") { return 0;}
var aChar = "";
var aStr = "";
var theSign = 1;
  for(var i=0; i<str.length; i++){
    aChar = str.charAt(i);
    if (aChar == ".") { break; }
    if ((aChar == "-")||(aChar == "(")) {theSign = -1}
    if ((aChar >= "0") && (aChar <="9")) {
      if ((aChar != "0") || (aStr != "")){
        aStr = aStr+aChar;
      }
    }
  }
  return (aStr==""? 0 : theSign * parseInt(aStr));
}

function parseNumber(str){
  return parseDollars(str);
}

function formatDollars(amt) {
  var tStr = ""+Math.abs(amt);
  aStr = "";
  for (var i=tStr.length-1; i>=0; i--) {
    aStr = tStr.charAt(i)+aStr;
    if (((tStr.length-i) % 3 == 0)&&(i!=0)){ aStr = ","+aStr; }
  }
  return (amt<0 ? "($"+aStr+")" : "$"+aStr);
}

function formatDate(theDate) {
  var mo = 0
  var day = 0
  var year = 0
  var tStr;
  var dStr = ""+theDate;
  if (dStr=="") { return "";}
  try {
    tStr = dStr.split(/[\-\/]/g);
  } catch (e) { return "";}
  var aStr = "";
  var numVal = tStr.length;
  if (numVal==0) {return "";}
  year= parseNumber(tStr[numVal-1]);
  if (year < 100) {
    if (year<20) {year+=2000} else {year+=1900}
  }
  if(numVal>1) { 
    mo=parseNumber(tStr[0])
    if (numVal>2){
      day=parseNumber(tStr[1])
    }
  }
  if(mo>0) {
    aStr+=mo+"/";
    if (mo>12) {alert("Invalid month")}
  }
  if(day>0) {
    aStr+=day+"/";
    if (day>31) {alert("Invalid day")}
  }
  aStr+=year;
  if ((year<2002)||(year>2005)) {alert("Check year")}
  return aStr;
}

function cleanupDollars(obj) {
  obj.value = formatDollars(parseDollars(obj.value));
}

function cleanupNumber(obj) {
  obj.value = parseNumber(obj.value);
}

function cleanupDate(obj) {
  obj.value = formatDate(obj.value);
}

function isFormClean() {
  return formIsClean;
}

function dirtyForm() {
  formIsClean = false;
}

var hideTxt = "none"
var showTxt = "inline"

function disableSupportBtn(val) {
  document.getElementById("supportBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("supportBtng").style.display = (!val ? hideTxt : showTxt);
}

function disableMainBtn(val) {
  document.getElementById("mainBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtng").style.display = (!val ? hideTxt : showTxt);
}

function disableLeaderBtn(val) {
  document.getElementById("leaderBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("mainBtng").style.display = hideTxt;
}

function disableSpeakerBtn(val) {
  document.getElementById("speakerBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("mainBtng").style.display = hideTxt;
}

function disableStudentBtn(val) {
  document.getElementById("studentBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("mainBtng").style.display = hideTxt;
}

function disableClergyBtn(val) {
  document.getElementById("clergyBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("mainBtng").style.display = hideTxt;
}

function disableEditBtn(val) {
  document.getElementById("editBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("editBtng").style.display = (!val ? hideTxt : showTxt);
}

function disableSaveBtn(val) {
  document.getElementById("saveBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("saveBtng").style.display = (!val ? hideTxt : showTxt);
}

function disablePreviewBtn(val) {
  document.getElementById("previewBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("saveBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("saveBtng").style.display = hideTxt;
}

function disableSaveonlyBtn(val) {
  document.getElementById("saveonlyBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("saveBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("saveBtng").style.display = hideTxt;
}

function disablePrintBtn(val) {
  document.getElementById("printBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("printBtng").style.display = (!val ? hideTxt : showTxt);
}

function disableLogoutBtn(val) {
  document.getElementById("logoutBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("logoutBtng").style.display = (!val ? hideTxt : showTxt);
}


function disableAllBtns(val) {
  disableMainBtn(val);
  disableEditBtn(val);
  disableSaveBtn(val);
  disablePrintBtn(val);
  disableLogoutBtn(val);
}

function startLogout() {
  if (confirm("Do you really want to log out?")) {
    top.location = "http://www.nicumc.org/church/logout.php";
  }
}

function startformLogout() {
  var doit = true;
  if (document.getElementById("formchanged").value == 1) {
    doit = confirm("You have modified this form. If you log out now, your changes WILL NOT be saved.\n\nDo you want to throw away changes and log out anyway?")
  }
  if (doit) {
    if (confirm("Do you really want to log out?")) {
      top.location = "http://www.nicumc.org/church/logout.php";
    }
  }
}

var printfunc = null;
var printDone = true;
function startPrint() {
  disablePrintBtn(true);
  if (printfunc != null) {
    printfunc();
  } else {
    var noPrint = document.getElementById("topHeader");
    noPrint.style.display="none";
    try {
      printDone = false;
      window.print();
    } catch(e){ }
    window.setTimeout("contPrint('0')",100);
  }
}

function contPrint(ct) {
  if (ct=="0") {
    window.setTimeout("contPrint('1')",2000);
  } else {
    var noPrint = document.getElementById("topHeader");
    noPrint.style.display="block";
    disablePrintBtn(false);
    printDone = true;
  }
}

function startMain(){
  disableMainBtn(true);
  if (page2 != null) {
    if (!page2.closed){
      alert("Please close opened supplemental window before leaving.");
      return;
    }
  }
  var doit = true;
  if (!isFormClean()) {
    doit = confirm("You have modified the form. Do you want to throw away the changes?")
  }
  if (doit) {
      top.location = "http://data.ngumc.org/conf/ccindex.asp";
  }
  disableMainBtn(false);
}

var editPage = null;
function startEdit(){
  disableEditBtn(true);
  var doit = true;
  if (!isFormClean()) {
    doit = confirm("You have modified the form. Do you want to throw away the changes?")
  }
  if (doit) {
    if (editPage != null) {
      window.location.href = editPage;
    }
  }
  disableEditBtn(false);
}

function startSupport() {
  disableSupportBtn(true);
  window.open("http://data.ngumc.org/trea/support.htm","support","scrollbars,resizable,width=700,height=500").focus();
  disableSupportBtn(false);
}

var saveForm = null;
function startSave() {
  disableSaveBtn(true);
  if (saveForm != null) {
    var doit = true;
    if (saveForm.onsubmit != null) {
      doit = saveForm.onsubmit();
    }
    if (doit) {
      saveForm.submit();
    }
  }
  disableSaveBtn(false);
}

var saveForm = null;
function startSaveonly() {
  disableSaveonlyBtn(true);
  if (saveForm != null) {
    var doit = true;
    if (saveForm.onsubmit != null) {
      doit = saveForm.onsubmit();
    }
    if (doit) {
      saveForm.submit();
    }
  }
  disableSaveonlyBtn(false);
}

var listPage = null;
function startList() {
//  disableListBtn(true);
  var doit = true;
  if (!isFormClean()) {
    doit = confirm("You have modified the form. Do you want to throw away the changes?")
  }
  if (doit) {
    if (listPage != null) {
      window.location.href = listPage;
    }
  }
//  disableListBtn(false);
}

var page2 = null;
function editSupp(fieldName,prompt) {
  if (page2 != null) {
    if (!page2.closed){
      alert("Please close opened supplemental window before opening a new one.");
      return;
    }
  }
  var value = document.getElementById(fieldName).value;
  var new_page_content = "<HTML><BODY>"+prompt+"<BR><TEXTAREA WRAP ROWS=20 COLS=80 id='entry'></TEXTAREA>"+       
       "<BR><BUTTON onClick='opener.acceptSupp(document.getElementById("+
       '"entry"'+"))'>Accept Changes</BUTTON>&nbsp;&nbsp;<BUTTON onClick='window.close()'>Ignore Changes</BUTTON>"+
       "<BR>Notes:<br>&nbsp; 1 - Edited supplementals are attached to the parent form and saved when it is saved."+
       "<BR>&nbsp; 2 - To edit this supplemental again simply click the button for it on the parent form."+
       "<br>&nbsp; 3 - The supplementals are attached to the end of the parent form in the print/view mode.  </BODY></HTML>"

  page2 = open("about:blank","Supplement","scrollbars,dependent");
  if (page2 != null) {
    page2.document.write(new_page_content);
    page2.document.close();
    var ele = page2.document.getElementById("entry");
    ele.setAttribute("fieldname",fieldName);
    ele.value = value;
    ele.focus();
  }
}

function acceptSupp(entryElem) {
  var ele = document.getElementById(entryElem.getAttribute("fieldname"))
  ele.value = entryElem.value;
  page2.close();
  page2 = null;
  dirtyForm();
}


function checkSubmit() {
  if (page2 != null) {
    if (!page2.closed){
      alert("Please close opened supplemental window before saving form.");
      return false;
    }
  }
  return true;
}

function doWindowClose() {
  if (page2 != null) {
    if (!page2.closed){
      page2.close();
      page2 = null;
    }
  }
}

function supportwindow() {
  msgWindow=open("/church/faq.php","_blank",'left= 25, top=25, resizable=yes, scrollbars=yes, width=700, height=600');
  if (msgWindow.opener == null) msgWindow.opener = self;
}
