<!-- 
// Module:	icform.js
// Author:	William E. Lanier Jr
// Company:	InfoCode Corporation
// Purpose:	Provides standard methods for Forms
//-->

// Reserved Button Indexes
var Index_btnExit	= 0;
var Index_btnClose	= 1;
var Index_btnHelp	= 2;
var Index_btnSearch	= 3;
var Index_btnOk		= 4;
var Index_btnCancel	= 5;
var Index_btnBack	= 6;
var Index_btnNext	= 7;
var Index_btnEmail	= 8;
var Index_btnNetmeeting	= 9;
var Index_btnPrint	= 10;
var Index_btnSave	= 11;
var Index_btnLookup	= 12;
var Index_btnSearch	= 13;
var Index_btnClear	= 14;
var Index_btnGISViewer	= 15;

var Index_btnLastIndex	= 15;

var arrButtons      = new Array();
var arrBtnTooltips  = new Array(" Press to close this window. ",
                                " Press to close this window. ",
                                " Press to display help for this window. ",
                                " Press to perform a search. ",
                                " Press to save changes and close this window. ",
                                " Press to close this window without saving changes. ",
                                " Press to go back to the previous window. ",
                                " Press to goto the next window. ",
                                " Send customer support an email... ",
                                " Call customer support via Netmeeting... ",
                                " Print this page... ",
                                " Press to save your results. ",
                                " Press to perform your lookup. ",
                                " Press to perform your search. ",
                                " Press to clear all entries. ",
                                " Press to open the GIS Viewer. ");

//-----------------------------------------------------
// Button Object and Support Methods
//-----------------------------------------------------
function FrmButton(Index, oImgCtrl, sImg, sImgOver, sImgDown, sImgDisabled, oClassCtrl, sClass, sClassOver, sClassDown, sClassDisabled, isDisabled, isCheckBox, isChecked) {
  this.index        = Index;
  this.isDisabled   = (typeof(isDisabled)  == "boolean"? isDisabled : false);
  this.isCheckBox   = (typeof(isCheckBox)  == "boolean"? isCheckBox : false);
  this.isChecked    = (typeof(isChecked)   == "boolean"? isChecked  : false);
  this.imgCtrl      = (typeof(oImgCtrl)    == "object" ? oImgCtrl   : null);
  this.img          = (typeof(sImg)        == "string" ? new Image(): null);
  this.imgOver      = (typeof(sImgOver)    == "string" ? new Image(): null);
  this.imgDown      = (typeof(sImgDown)    == "string" ? new Image(): null);
  this.imgDisabled  = (typeof(sImgDisabled)== "string" ? new Image(): null);
  this.classCtrl    = (typeof(oClassCtrl)  == "object" ? oClassCtrl : null);
  this.cssclass     = (typeof(sClass)      == "string" ? sClass     : null);
  this.cssclassOver = (typeof(sClassOver)  == "string" ? sClassOver : null);
  this.cssclassDown = (typeof(sClassDown)  == "string" ? sClassDown : null);
  this.cssclassDisabled = (typeof(sClassDisabled)  == "string" ? sClassDisabled : null);

  if (this.imgCtrl) this.imgCtrl.tabstop = true;
  if (this.img)     this.img.src     = sImg;
  if (this.imgOver) this.imgOver.src = sImgOver;
  if (this.imgDown) this.imgDown.src = sImgDown;
  if (this.imgDisabled) this.imgDisabled.src = sImgDisabled;
}

function Frm_getImgButton(Index) {
  for (var intNdx=0; intNdx < arrButtons.length; intNdx++) {
    if (arrButtons[intNdx].index == Index) {
      return arrButtons[intNdx];
    }
  }
}

function Frm_ImgButtonCheck(Index, Value) {
  event.cancelBubble = true;
  var oBtn = Frm_getImgButton(Index);
  if (oBtn.isCheckBox) {
      with (oBtn) {
        isChecked = Value;
        if (imgCtrl) {
          imgCtrl.src = (isChecked) ? (imgDown) ? imgDown.src : (img) ? img.src : null : (img) ? img.src : null
        }
      }
  }
}

function Frm_AddButton(Index, oImgCtrl, sImg, sImgOver, sImgDown, sImgDisabled, oClassCtrl, sClass, sClassOver, sClassDown, sClassDisabled, isDisabled, sToolTip, isCheckBox, isChecked) {
  arrButtons = arrButtons.concat(new FrmButton(Index, oImgCtrl, sImg, sImgOver, sImgDown, sImgDisabled, oClassCtrl, sClass, sClassOver, sClassDown, sClassDisabled, isDisabled, isCheckBox, isChecked));
  with (arrButtons[arrButtons.length-1]) {
    if (imgCtrl) {
      if (isDisabled && imgDisabled) {
        imgCtrl.src = imgDisabled.src
      }else{
        if (isCheckBox && isChecked && imgDown) 
          imgCtrl.src = imgDown.src;
        else
          if (img) imgCtrl.src = img.src;
      }
      Frm_AddStds(Index, oImgCtrl, sToolTip);
    }

    if (classCtrl) {
      if (isDisabled && cssclassDisabled && cssclassDisabled.length) 
        classCtrl.className = cssclassDisabled;
      else
        if (cssclass && cssclass.length) classCtrl.className = cssclass;
      Frm_AddStds(Index, oClassCtrl, sToolTip);
    }

  }

}

function Frm_AddStds(Index, oCtrl, sTooltip) {
  with (oCtrl) {
    ondragstart = new Function("return false;");
    onclick     = new Function("FrmBtn_Click(" + Index + ");");
    onmouseover = new Function("FrmBtn_Over(" + Index + ");");
    onmouseout  = new Function("FrmBtn_Out(" + Index + ");");
    onmouseup   = new Function("FrmBtn_Up(" + Index + ");");
    onmousedown = new Function("FrmBtn_Down(" + Index + ");");
onkeypress     = new Function("FrmBtn_KeyPress(" + Index + ");");
    if (typeof(sTooltip) == "string")
      title = sTooltip;
    else
      if (Index <= Index_btnLastIndex) title = arrBtnTooltips[Index];
  }
}

//-----------------------------------------------------
// Generic Button Event Handlers
//-----------------------------------------------------
function FrmBtn_Enabled(Index, Enabled) {
  var oBtn = Frm_getImgButton(Index);
  if (oBtn) {
    if (typeof(Enabled) == "boolean") {
      oBtn.isDisabled = !Enabled;
      if (oBtn && oBtn.imgCtrl) {
        with (oBtn) {
          if (isDisabled && imgDisabled) {
            imgCtrl.src = imgDisabled.src;
          }else{
            if (img) imgCtrl.src = img.src;
          }
        }
      }

      if (oBtn && oBtn.classCtrl) {
        with (oBtn) {
          if (isDisabled && cssclassDisabled) 
            classCtrl.className = cssclassDisabled;
          else
            if (cssclass) classCtrl.className = cssclass;
        }
      }
    }else{
      return !oBtn.isDisabled;
    }
  }
}


function FrmBtn_Over(Index) {
  event.cancelBubble = true;
  var oBtn = Frm_getImgButton(Index);
  if (oBtn && !oBtn.isDisabled) {
    with (oBtn) {
      if (!isCheckBox && imgCtrl && imgOver) imgCtrl.src = imgOver.src;
      if (classCtrl && cssclassOver) classCtrl.className = cssclassOver;
      if (typeof(OnMouseOver) == "function") OnMouseOver(Index);
    }
  }
}

function FrmBtn_Out(Index) {
  event.cancelBubble = true;
  var oBtn = Frm_getImgButton(Index);
  if (oBtn && !oBtn.isDisabled) {
    with (oBtn) {
      if (!isCheckBox && imgCtrl && img) imgCtrl.src = img.src;
      if (classCtrl && cssclass) classCtrl.className = cssclass;
      if (typeof(OnMouseOut) == "function") OnMouseOut(Index);
    }
  }
}

function FrmBtn_Up(Index) {
  event.cancelBubble = true;
  var oBtn = Frm_getImgButton(Index);
  if (oBtn && !oBtn.isDisabled) {
    with (oBtn) {
      if (!isCheckBox && imgCtrl) {(imgOver) ? imgCtrl.src = imgOver.src : (img) ? imgCtrl.src = img.src : null;}
      if (classCtrl && cssclassOver) classCtrl.className = cssclassOver;
      if (typeof(OnMouseUp) == "function") {OnMouseUp(Index);}
    }
  }
}

function FrmBtn_Down(Index) {
  event.cancelBubble = true;
  var oBtn = Frm_getImgButton(Index);
  if (oBtn && !oBtn.isDisabled) {
    with (oBtn) {
      if (!isCheckBox && imgCtrl && imgDown) imgCtrl.src = imgDown.src;
      if (classCtrl && cssclassDown) classCtrl.className = cssclassDown;
      if (typeof(OnMouseDown) == "function") {OnMouseDown(Index);}
    }
  }
}

function FrmBtn_Click(Index) {
  event.cancelBubble = true;
  var Cancel = false;
  var oBtn = Frm_getImgButton(Index);
  if (oBtn && !oBtn.isDisabled) {
    if (oBtn.isCheckBox && oBtn.imgCtrl) {
      with (oBtn) {
        isChecked = !isChecked;
        imgCtrl.src = (isChecked) ? (imgDown) ? imgDown.src : (img) ? img.src : null : (img) ? img.src : null
      }
    }

    switch (Index) {
      case Index_btnHelp:
        // call doc's OnHelp Handler if it exists
        if (typeof(OnHelp) == "function") {OnHelp();}
        break;

      case Index_btnOk:
        if (typeof(OnOk) == "function") {Cancel = OnOk();}
        if (!Cancel) self.close(); 
        break;

      case Index_btnExit:
      case Index_btnClose:
      case Index_btnCancel:
        // call doc's OnClose Handler if it exists
        if (typeof(OnClose) == "function") {Cancel = OnClose();}
        if (!Cancel) self.close(); 
        break;

      default:
        if (typeof(OnClick) == "function") {OnClick(Index, oBtn.isChecked);}
    }
  }
}

function FrmBtn_KeyPress(Index) {
  // provide for key press activation
  if (event.keyCode == 13) {
    FrmBtn_Click(Index);
  }
}

//-----------------------------------------------------
// Document Event Handlers
//-----------------------------------------------------
function Frm_OnLoad(hideContextMenu, preventSelection) {

  // prevent the context menu if requested
  if ((typeof(hideContextMenu) == "boolean") && hideContextMenu) {document.oncontextmenu = new Function("return false;"); }

  // prevent document selection if requested
  if ((typeof(preventSelection) == "boolean") && preventSelection) {document.body.onselectstart = new Function("return false;"); }

  // call local OnLoad event
  if (typeof(OnLoad) == "function") {OnLoad();}

  // assign OnKeyUp Handler
  if (typeof(OnKeyUp) == "function") {document.onkeyup = OnKeyUp;}

}

//-----------------------------------------------------
// Form Persistance methods
//-----------------------------------------------------
//-----------------------------------------------------
// Save Positional Info
//-----------------------------------------------------
function Frm_PutPosition(objStore, sXMLBranch, Top, Left, Width, Height) {
  try {
    objStore.setAttribute("FTop", Frm_RemovePX(Top));
    objStore.setAttribute("FLeft", Frm_RemovePX(Left));
    objStore.setAttribute("FWidth", Frm_RemovePX(Width));
    objStore.setAttribute("FHeight", Frm_RemovePX(Height));
    objStore.save(sXMLBranch);
  } catch (e) {};
}

//-----------------------------------------------------
// Remove any trailing px
//-----------------------------------------------------
function Frm_RemovePX(Value) {
  if (typeof(Value) == "string") {
    if (Value.substr(Value.length - 3, 2) == "px")
      return parseInt(Value.substr(0, Value.length - 2));
    else
      return parseInt(Value);
  }else{
    return Value;
  }
}

//-----------------------------------------------------
// Get Positional Info
//-----------------------------------------------------
function Frm_GetPosition(objStore, sXMLBranch, oFormPos) {
  var sTmp;
  try {
    objStore.load(sXMLBranch);
  } catch (e) {return null;};
  try {
    sTmp = objStore.getAttribute("FTop");
    if (typeof(sTmp) == "string") oFormPos.Top = parseInt(sTmp);
  } catch (e) {return null;};
  try {
    sTmp = objStore.getAttribute("FLeft");
    if (typeof(sTmp) == "string") oFormPos.Left = parseInt(sTmp);
  } catch (e) {return null;};
  try {
    sTmp = objStore.getAttribute("FWidth");
    if (typeof(sTmp) == "string") oFormPos.Width = parseInt(sTmp);
  } catch (e) {return null;};
  try {
    sTmp = objStore.getAttribute("FHeight");
    if (typeof(sTmp) == "string") oFormPos.Height = parseInt(sTmp);
  } catch (e) {return null;};

}


//-----------------------------------------------------
// Save A Single Value
//-----------------------------------------------------
function Frm_PutValue(objStore, sXMLBranch, Name, Value) {
  try {
    objStore.setAttribute(Name, Value);
    objStore.save(sXMLBranch);
  } catch (e) {};
}

//-----------------------------------------------------
// Get A Single Value
//-----------------------------------------------------
function Frm_GetValue(objStore, sXMLBranch, Name) {
  try {
    objStore.load(sXMLBranch);
    return objStore.getAttribute(Name);
  } catch (e) {};
}

//-----------------------------------------------------
// Remove A Branch
//-----------------------------------------------------
function Frm_ClearBranchAttributes(objStore, sXMLBranch) {
  try {
    objStore.load(sXMLBranch);
    var oRoot = objStore.XMLDocument.documentElement;
    var oAttribute = null;
    with(oRoot) {
      for (var intNdx = 0; intNdx < attributes.length; intNdx++) {
        oAttribute = attributes.item(intNdx);
        oAttribute.nodeValue = ""
      }
    }
    objStore.save(sXMLBranch);
  } catch (e) {};
}

function Frm_ExpireBranch(objStore, sXMLBranch) {
  try {
    var oTimeNow = new Date(); // Start Time
    oTimeNow.setMilliseconds(oTimeNow.getMilliseconds() + 1);
    var sExpirationDate = oTimeNow.toUTCString();
    objStore.expires = sExpirationDate;
    objStore.setAttribute("Expire", sExpirationDate);
    objStore.save(sXMLBranch);
  } catch (e) {};
}
