/***********************************************
* Display utilities © Tim Reed  (unless otherwise noted)
* This notice must stay intact for use
* Visit http://www.i4synthesis.com for contact info
***********************************************/

/**
 * change background color of element
 * will insert class if prop=="class" && val!=""
 * will replace backgroundColor if prop=="color" && val!=""
 * by default it changes background color to #FFEE00
 * returns true if succeeds
 * returns false if fails
**/
function addStyle(Obj,prop,val) {
  if (!Obj || Obj.className == 'clickSelected') return false;
  switch(prop) {
    case "color":
      if (!val) val = "";
      if (null == Obj.style) return false;
      Obj.style.backgroundColor = val;
      break;
    case "class":
      if (!val) return false;
      if (null == Obj.className) return false;
      Obj.className += ' ' + val;
      break;
    default:
      if (null == Obj.style) return false;
      Obj.style.backgroundColor="#FFEE00";
      break;
  }
  return true;
}

/**
 * turn off background color set by addStyle()
 * will attempt to remove class if prop=="class" && val!=""
 * will replace backgroundColor if prop=="color" && val!=""
 * will remove backgroundColor if prop=="color" && val==""
 * by default it removes backgroundColor
 * returns true if succeeds
 * returns false if fails
**/
function delStyle(Obj,prop,val) {
  if (!Obj) return false;
  //alert('running delStyle(' + Obj.id + ')');
  switch(prop) {
    case "color":
      if (!val) val = "";
      if (null == Obj.style) return false;
      Obj.style.backgroundColor = val;
      break;
    case "class":
      if (!val) return;
      if (null == Obj.className) return false;
      if (Obj.className.indexOf(val) != -1) {
        Obj.className = replaceStr(Obj.className,val,"",true);
        //var classPattern = new RegExp("\\b"+val+"\\b"); //find word match (avoids partial matches)
        //Obj.className = Obj.className.replace(classPattern, ""); //clear element class of selectClass
        //Obj.className = normalizeString(Obj.className); //clean up class string
      } else {
        return false;
      }
      break;
    default:
      if (null == Obj.style) return false;
      Obj.style.backgroundColor="";
      break;
  }
  return true;
}

function hideIt(elementId) {
  var elt=document.getElementById(elementId);
  if (null==elt) { return; }
  elt.style.display="none";
}

function showIt(elementId) {
  var elt=document.getElementById(elementId);
  if (null==elt) { return false; }
  elt.style.display="block";
  return true;
}

// modified toggleBox to provide the ability to just switch the image
function toggleImg(imgElt,forcedImgName) {
  if ((null == imgElt) || (null == imgElt.src)) return;
  var name = imgElt.src;
  var start = name.lastIndexOf("/")+1;
  var path = name.substring(0,start);
  var displayMode = "none";
  name = name.substring(start,name.length);
  var ext = name.substring(name.lastIndexOf('.')+1,name.length);
  var newName = (name.indexOf("minus.") < 0) ? "minus" : "plus";
  // should probably do this with regexp
  var name_common = (name.lastIndexOf("plus.")!=0) ? name.substring(0,name.lastIndexOf("plus.")) : "";
  if ("" == name_common) {
    name_common = (name.lastIndexOf("minus.")!=0) ? name.substring(0,name.lastIndexOf("minus.")) : "";
  }

  if (null == forcedImgName) {
    imgElt.src = path + name_common + newName + "." + ext;
  } else {
    imgElt.src = path + name_common + forcedImgName + "." + ext;
  }
}

/**
 * taken fron explorer
 * this toggles an image between plus.gif & minus.gif values (any image will work as long as it ends in these values)
 * then it toggles visibility of the passed rootId and any sequence of children elements starting with (rootId-0)
**/
function toggleBox(imgElt,rootId,forceOpen,inc) {
  try { if (LOG.showDebug()) LOG.debug("toggleBox("+imgElt+","+rootId+","+forceOpen+")"); } catch (e) {}
  if ((null == imgElt) || (null == imgElt.src)) return;
  var name = imgElt.src;
  var start = name.lastIndexOf("/")+1;
  var pathLen = name.length;
  var path = name.substring(0,start);
  var displayMode = "none";
  var rootIcon = (null!=rootId) ? 'root_' + rootId : ''; // ID for folder icon for the +/-
  var rootIconElt = document.getElementById(rootIcon);
  name = name.substring(start,pathLen);
  var ext = name.substring(name.lastIndexOf('.')+1,name.length);
  var imgRoot = ""
  if (name.indexOf("minus") < 0) imgRoot = name.substring(0,name.lastIndexOf("plus."));
  if (name.indexOf("plus") < 0)  imgRoot = name.substring(0,name.lastIndexOf("minus."));

  // TODO: improve this test. It's messy but it works
  if (null != forceOpen) {
    if (forceOpen) {
      imgElt.src = path + imgRoot + "minus." + ext;
      if (rootIconElt!=null) rootIconElt.src = path + "openfoldericon.gif";
      displayMode = "block";
    } else {
      imgElt.src = path + imgRoot + "plus." + ext;
      if (rootIconElt!=null) rootIconElt.src = path + "foldericon.gif";
      displayMode = "none";
    }
  } else {
    if (name.indexOf("minus") < 0) {
      imgElt.src = path + imgRoot + "minus." + ext;
      if (rootIconElt!=null) rootIconElt.src = path + "openfoldericon.gif";
      displayMode = "block";
    } else {
      imgElt.src = path + imgRoot + "plus." + ext;
      if (rootIconElt!=null) rootIconElt.src = path + "foldericon.gif";
      displayMode = "none";
    }
  }

  // look for child elements to toggle
  inc = (null==inc) ? 0 : inc;
  if (null!=rootId) {
    while (document.getElementById(rootId + "-" + inc) != null) {
      elt = document.getElementById(rootId + "-" + inc);
      // test to see if elt has custom property specifying visibility permission
      // toggle visibility based on that test
      if (null == elt.getAttribute('doShow')) {
        elt.style.display = displayMode;
      } else {
        elt.style.display = (elt.getAttribute('doShow')) ? displayMode : "none";
      }
      inc++;
    }
    if (inc==0 && document.getElementById(rootId + "-" + inc) == null) { // rootId has no children, toggle its visibility
      elt = document.getElementById(rootId);
      try { if (LOG.showDebug()) LOG.debug("toggling element: elt.id="+elt.id+", elt.style.display="+elt.style.display+", displayMode="+displayMode); } catch (e) {}
      if (elt!=null) {
        // test to see if elt has custom property specifying visibility permission
        // toggle visibility based on that test
        if (null == elt.getAttribute('doShow')) {
          elt.style.display = displayMode;
        } else {
          elt.style.display = (elt.getAttribute('doShow')) ? displayMode : "none";
        }
      }
      try { if (LOG.showDebug()) LOG.debug("elt.style.display="+elt.style.display); } catch (e) {}
    }
  }
}

/**
 * toggles visibility of a group of items (any) that have ids that
 * follow a sequence of integers
 * the starting index number defaults to 0
 * the stopping index number is optional
**/
function toggleElt(idRoot,startIdx,stopIdx,currElt) {
  //alert('toggleElt('+idRoot+","+startIdx+","+stopIdx+")");
  if (null == idRoot) return;
  if (null != startIdx) {
    if (null == stopIdx) {
      //increment up from unbounded starting index
      inc = (null != startIdx) ? startIdx : 0;
      while (document.getElementById(idRoot + inc) != null) {
        //alert(idRoot + inc);
        tmpElt = document.getElementById(idRoot + inc);
        tmpElt.style.display = ("none" == tmpElt.style.display) ? "block" : "none";
        inc++;
      }
    } else {
      // loop through bounded set of indices
      for (var i=startIdx; i<=stopIdx; i++) {
        tmpElt = document.getElementById(idRoot + i);
        if (null == tmpElt) { continue; }
        tmpElt.style.display = ("none" == tmpElt.style.display) ? "block" : "none";
      }
    }
  } else {
    // test for single element (no index at all)
    tmpElt = document.getElementById(idRoot);
    if (null != tmpElt) {
      tmpElt.style.display = ("none" == tmpElt.style.display) ? "block" : "none";
    }
  }
  if (null!=currElt) {
    if (currElt.innerHTML) {
      try {
        // TODO: maybe this should be set by parameters passed to the function
        // look for [+] and [-] text togglers
        if (currElt.innerHTML.indexOf("[+]")+1) {
          currElt.innerHTML=currElt.innerHTML.replace(/\[\+\]/,"[-]");
        } else if (currElt.innerHTML.indexOf("[-]")+1) {
          currElt.innerHTML=currElt.innerHTML.replace(/\[\-\]/,"[+]");
        // look for (+) and (-) text togglers
        } else if (currElt.innerHTML.indexOf("(+)")+1) {
          currElt.innerHTML=currElt.innerHTML.replace(/\(\+\)/,"(-)");
        } else if (currElt.innerHTML.indexOf("(-)")+1) {
          currElt.innerHTML=currElt.innerHTML.replace(/\(\-\)/,"(+)");
        // look for + and - text togglers
        } else if (currElt.innerHTML.indexOf("+")+1) {
          currElt.innerHTML=currElt.innerHTML.replace(/\+/,"-");
        } else if (currElt.innerHTML.indexOf("-")+1) {
          currElt.innerHTML=currElt.innerHTML.replace(/\-/,"+");
        }
      } catch (e) {}
    }
  }
}

/**
 * initialize properties of object that holds data about last element clicked
**/
function initLastClicked() {
  oLastClicked = {elt:null,className:''}; // set object properties to default
}

// set up object to hold data about last row clicked
var oLastClicked = new Object(); // holds last clicked item (for hilighting);
initLastClicked(); // initialize object properties

/**
 * selectIt(elt,className,isDetail) adds/removes class element to element passed
 * intended to be used for click events
 * last clicked element is stored in global object "oLastClicked"
 * modified to include call to ableIt and ableDetailButtons, functions that act on form buttons
 * NOTE: this code permits only one clicked element per document
 *  elt = item clicked
 *  className = class to set/remove for elt
 *  isDetail  = boolean value to indicate if additional buttons need to be toggled for detail data
**/

function selectIt(elt,className) {
  isOK = selectElt(elt,className,isDetail); // refactored this code to isolate from button actions
  if (isOK) {
    //do stuff
  }
  return true;
}

/*
 * selectElt() is the bulk of the code from selectIt()
 * was refactored  to isolate select functionality
 * from button functionality
**/
function selectElt(elt,className,isDetail) {
  if (null != elt) {
    if (null == className) {
      className = 'selected'; // default class is 'selected'
    }
    if (elt == oLastClicked.elt) {
      delStyle(elt,'class',oLastClicked.className);
      initLastClicked()
    } else {
      delStyle(oLastClicked.elt,'class',oLastClicked.className);
      addStyle(elt,'class',className);
      oLastClicked.elt = elt; // save curr element to global var
    }
    oLastClicked.className=className;
    return true;
  }
  return false;
}


/**
 * displays a message with an animated bar of .'s
 * msgLoc is the id of the object into which the message will be inserted
 * msgTxt is the text that will be displayed
**/
var displayMsg_dots = 0; //variable used in displayMsg()
var displayMsg_msgLocDef = "msgHolder"; //default location for displayMsg() output;
var do_displayMsg = true;
function displayMsg(msgTxt,msgLoc) {
  if (!msgTxt) return;
  if (!msgLoc) msgLoc = displayMsg_msgLocDef;
  var m = "<b>" + msgTxt + "</b>"
  for (var i = 0; i < displayMsg_dots; i++) m += ".";
  displayMsg_dots++;
  document.getElementById(msgLoc).innerHTML = m;
  var interval = 50; //pause (in ms) between adding .'s 50
  var rep = 10; //number of .'s to generate 10
  var wait = 100; //pause at end of generation before starting over
  if (displayMsg_dots > rep) {
    displayMsg_dots = 0;
    interval = (interval*rep) + wait;
  }
  if (do_displayMsg) {
    var waitFor = setTimeout("displayMsg('"+msgTxt+"','"+msgLoc+"')",interval);
  } else {
    document.getElementById(msgLoc).innerHTML="";
    do_displayMsg = true;
  }
}


