// --- tools.js -----
var sessionGraceTime = 15; // 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)*120000)
  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. Any unsaved data will be lost if the session expires.")
}

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>2009)) {alert("Check year")}
  return aStr;
}

function formatDatePast(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<1900)||(year>2009)) {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 cleanupDatePast(obj) {
  obj.value = formatDatePast(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 disableDataBtn(val) {
  document.getElementById("dataBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("dataBtng").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = hideTxt;
}

function disableMainBtn(val) {
  document.getElementById("mainBtn").style.display = (val ? hideTxt : showTxt);
}


function disableLeaderBtn(val) {
  document.getElementById("leaderBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableSpeakerBtn(val) {
  document.getElementById("speakerBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableStudentBtn(val) {
  document.getElementById("studentBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableClergyBtn(val) {
  document.getElementById("clergyBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableEvangBtn(val) {
  document.getElementById("evangBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disablePayBtn(val) {
  document.getElementById("payBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableEoyBtn(val) {
  document.getElementById("eoyBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableFundBtn(val) {
  document.getElementById("fundBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableInsurBtn(val) {
  document.getElementById("insurBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("mainBtn").style.display = (!val ? hideTxt : showTxt);
}

function disableEditBtn(val) {
  document.getElementById("editBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("editBtng").style.display = (!val ? hideTxt : showTxt);
}

function disableMainsBtn(val) {
  document.getElementById("mainsBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("editBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("editBtng").style.display = hideTxt;
}

function disableProfileBtn(val) {
  document.getElementById("profileBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("editBtn").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("editBtng").style.display = hideTxt;
}

function disableSaveBtn(val) {
  document.getElementById("saveBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("saveBtng").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("saveonlyBtn").style.display = hideTxt;
}

function disableSaveonlyBtn(val) {
  document.getElementById("saveonlyBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("saveonlyBtng").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("saveBtn").style.display = hideTxt;
}

function disablePreviewBtn(val) {
  document.getElementById("previewBtn").style.display = (val ? hideTxt : showTxt);
  document.getElementById("previewBtng").style.display = (!val ? hideTxt : showTxt);
  document.getElementById("saveBtn").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);
  disableHomeBtn(val);
  disableDataBtn(val);
  disableEditBtn(val);
  disableMainsBtn(val)
  disableEoyBtn(val);
  disableSaveBtn(val);
  disablePrintBtn(val);
  disableLogoutBtn(val);
}

function startLogout() {
  disableLogoutBtn(true);
  var doit = true;
  if (!isFormClean()) {
    doit = confirm("You have modified the form. Do you want to throw away any changes?")
  }
  if (doit) {
    if (confirm("Do you really want to log out?")) {
      self.location = "logout.asp";
    }
  }
  disableLogoutBtn(false);
}

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 printer()
{
    document.getElementById("printreport").style.display="none";
	window.print()
    document.getElementById("printreport").style.display="block";
}

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) {
      self.location = "indexcconf.asp";
  }
  disableMainBtn(false);
}

function startHome() {
  disableHomeBtn(true);
      self.location = "indexspeak.asp";
  disableHomeBtn(false);	
}

function startData() {
  disableDataBtn(true);
      self.location = "indexredirect.asp";
  disableDataBtn(false);	
}

function startMains() {
  disableMainsBtn(true);
      self.location = "indexcconf.asp";
  disableMainsBtn(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);
}

var previewPage = null;
function startPrev(){
  disablePreviewBtn(true);
  if (saveForm != null) {
    var doit = true;
    if (saveForm.onsubmit != null) {
      doit = saveForm.onsubmit();
    }
    if (doit) {
      saveForm.submit();
    }
  }
  disablePreviewBtn(false);
}

function startSupport() {
  disableSupportBtn(true);
  window.open("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>"+
       "Notes:<br>&nbsp; 1 - Click Accept Changes to  save changes."+
       "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Warning! Extremely long supplements may not save."+
       "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Supplements are attached to the parent report and it must also be saved."+
       "<br>&nbsp; 2 - To edit this supplement again simply click the button for it on the parent report."+
       "<br>&nbsp; 3 - The supplements are attached to the end of the parent report in the print/view mode."+
       "<br><BUTTON onClick='opener.acceptSupp(document.getElementById("+
       '"entry"'+"))'>Accept Changes</BUTTON>&nbsp;&nbsp;<BUTTON onClick='window.close()'>Ignore Changes</BUTTON>"+
       "<br>"+prompt+"<BR>"+
       "<TEXTAREA WRAP ROWS=25 COLS=75 id='entry'></TEXTAREA>"+       
       "<BR></BODY></HTML>"

  page2 = open("about:blank","Supplement","top=5,left=5,scrollbars,resizable,width=700,height=550,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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
