// Cross-browser object and style scripts

// Capabilities flags
var isALL = 0;
var isDHTML = 0;
var isID = 0;
var isLayers = 0;

//  Detect capabilities
if ( document.getElementById ) {
  // Preferred
  isID = 1;
  isDHTML = 1;
} else {
  if ( document.all ) {
    isAll = 1;
    isDHTML=1;
  } else {
    //  Only Netscape 4 used layers
    browserVersion = parseInt( navigator.appVersion );
    if ( ( navigator.appName.indexOf('Netscape') != -1)
        &&  ( browserVersion == 4 ) ) {
      isLayers = 1;
      isDHTML = 1;
    }
  }
}

//  findElementById -- Cross-browser object location
function findElementById( objectID ) {
  if (isID) {
    return( document.getElementById( objectID ) );
  } else {
    if (isAll) {
      return( document.all[objectID]);
    } else {
      if (isLayers) {
        return( document.layers[objectID] );
      }
    }
  }
  return null;
}

//  findFrameElementById -- Cross-browser object location
function findFrameElementById( frameName, objectID ) {
  if (isID) {
    return( top[frameName].document.getElementById( objectID ) );
  } else {
    if (isAll) {
      return( top[frameName].document.all[objectID]);
    } else {
      if (isLayers) {
        return( top[frameName].document.layers[objectID] );
      }
    }
  }
  return null;
}

//  elementStyle -- Get style reference for given object
function elementStyle( obj ) {
  if ( isID || isAll ) {
    return obj.style;
  } else if (isLayers) {
    return obj;
  }
  return null;
}

//  findElementStyle -- Cross-browser style object
function findElementStyle( objectID ) {
  var obj = findElementById( objectID );
  return elementStyle( obj );
}

// BlockShow -- display one object
function BlockShow( objectID ) {
  var obj = findElementById( objectID );
  obj.style.display = 'block';
  var label = objectID + 'Label';
  var obj = findElementById( label );
  if( obj ) {
    obj.style.display = 'block';
  }
}

// BlockShowList -- display multiple objects.
//  idListStr is a string containing a comma-separated list of object IDs.
function BlockShowList( idListStr ) {
  var idList = idListStr.split( ',' );
  var numItems = idList.length;
  for (var idx=0; idx<numItems; idx++ ) {
    BlockShow( idList[idx] );
  }
}

// BlockHide -- hide one object
function BlockHide( objectID ) {
  var obj = findElementById( objectID );
  obj.style.display = 'none';
  var label = objectID + 'Label';
  var obj = findElementById( label );
  if( obj ) {
    obj.style.display = 'none';
  }
}

// BlockHideList -- hide multiple objects.
//  idListStr is a string containing a comma-separated list of object IDs.
function BlockHideList( idListStr ) {
  var idList = idListStr.split( ',' );
  var numItems = idList.length;
  for (var idx=0; idx<numItems; idx++ ) {
    BlockHide( idList[idx] );
  }
}

// ToggleDisplay -- toggle display of one object
function ToggleDisplay( objectID ) {
  var obj = findElementById( objectID );
  //alert( objectID + '\n' + obj.style.display );
  if ( obj.style.display == 'none' ) {
    obj.style.display = 'block';
  } else {
    obj.style.display = 'none';
  }
}

// ToggleDisplayList -- toggle display of multiple objects.
//  idListStr is a string containing a comma-separated list of object IDs.
function ToggleDisplayList( idListStr ) {
  var idList = idListStr.split( ',' );
  var numItems = idList.length;
  for (var idx=0; idx<numItems; idx++ ) {
    ToggleDisplay( idList[idx] );
  }
}

// ToggleVisibility -- toggle visibility of one object
function ToggleVisibility( objectID ) {
  var obj = findElementById( objectID );
  var state = obj.visibility;
  if ( state == 'hidden' || state == 'hide' ) {
    obj.visibility = 'visible';
  } else {
    obj.visibility = 'hidden';
  }
}

// ToggleVisibilityList -- toggle visibility of multiple objects.
//  idListStr is a string containing a comma-separated list of object IDs.
function ToggleVisibilityList( idListStr ) {
  var idList = idListStr.split( ',' );
  var numItems = idList.length;
  for (var idx=0; idx<numItems; idx++ ) {
    ToggleVisibility( idList[idx] );
  }
}

// Generic BlockShow/BlockHide for V3 projects
// "actor" is what you clicked - select box or checkbox
// "actee" is what is changed. May be a string or an array
// "attrib" is either "value" or "name" - attribute of actee to check
// "val" is value of "attrib" that is checked against
function doBlockShow( actor, actee, attrib, val ) {
  var flShow;
  var formName = actor.form.name;
  var tmp = actor.type.split( "-" );
  var elementType = tmp[0];
  if( elementType == "select" ) {
    if( actor[actor.selectedIndex].value == val ) {
      flShow = 1;
    } else {
      flShow = 0;
    }
  } else if( elementType == "checkbox" ) {
    if( actor.checked == "1" && actor[attrib] == val ) {
      flShow = 1;
    } else {
      flShow = 0;
    }
  }
  if( typeof( actee ) != "object" ) {
    var tmp = actee;
    var actee = Array( tmp );
  }
  for (var i = 0; i < actee.length; i++) {
    var txt = i+": "+actee[i];
    var acteeLabel = actee[i]+"Label";
    if( flShow == 1 ) { // Means we'll show actee(s)
      if( findElementById( acteeLabel ) ) { 
        BlockShow( acteeLabel );
      }
      BlockShow( actee[i] );
      txt += "\n"+document[formName][actee[i]].value;
      
      document[formName][actee[0]].focus();
    } else if( flShow == 0 ) { // Means we'll hide actee(s)
      txt += "\nactee["+i+"] is "+actee[i]+" and is a(n) "+typeof( document[formName][actee[i]] );
      if( document[formName][actee[i]].checked ) {
        document[formName][actee[i]].checked = 0;
      } else if( document[formName][actee[i]].selectedIndex ) {
        document[formName][actee[i]].selectedIndex = 0;
      } else {
        document[formName][actee[i]].value="";
      }
      
      if( findElementById( acteeLabel ) ) { BlockHide( acteeLabel ); }
      BlockHide( actee[i] );
      
    }
    //alert( txt );
  }
  
}

// unCheck -- uncheck one checkbox
function unCheck( objectID ) {
  objectID.checked = 0;
}

// unCheck -- uncheck one checkbox
function unCheck( objectID ) {
  objectID.checked = 0;
}

// Goes through the inputString and replaces every occurrence of fromString with toString
// from http://www.breakingpar.com/bkp/home.nsf/0/45A5696524D222C387256AFB0013D850
// - bzm 2006-01-18
function replaceSubstring(inputString, fromString, toString) {
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) {
   // If the string being replaced is not a part of the replacement string
   // (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else {
   // String being replaced is part of replacement string (like "+" being replaced with "++")
   // - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function
