/*
 *  File:     basic_functions.js
 *  Contents: Basic and useful Javascript functions.
 *  Created:  07.30.2006
 *  Modified: 08.01.2006
 */

/*
 *  preloadImages
 *
 *  @author  Kevin Barsotti <kevin.barsotti@tribaldawn.com>
 *
 *  This script is designed to preload all images whose URLS are
 *  passed to it as input parameters in quote-encapsulated string
 *  format.
 * 
 *  @param   images       A comma-seperated list of image URL strings.
 */
function preloadImages(){
  var myImages = new Array();
  for(i=0;i<preloadImages.arguments.length;i++){
    myImages[i]=new Image();
    myImages[i].src=preloadImages.arguments[i];
  }/* end for (i=0;i<preloadImages.arguments.length;i++) */
  return true;
}// end preloadImages()

/*
 *  changeClassByID
 *
 *  @author  Kevin Barsotti <kevin.barsotti@tribaldawn.com>
 *
 *  This script is designed to change the class of a certain HTML
 *  element based upon a supplied ID.
 *
 *  Relies upon getByID as a helper function.
 *
 *  @param   id           The ID number of the element to change.
 *  @param   newClass     The name to assign the element's class.
 */
function changeClassByID(id,newClass){
  toChange = getByID(id);
  toChange.className = newClass;
}// end changeClassByID()


/*
 *  fadeElementIn
 *
 *  @author  Nick O'Neill
 * 
 *  This script fades an element in from total transparency to
 *  total opacity.
 *
 *  Relies upon setOpacity and getByID as helper functions.
 *
 *  @param    id          ID of element to fade.
 *  @param    time        Time for fadein to occur in seconds.
 *  @param    begin       Countdown until fadein occurs in seconds.
 */
function fadeElementIn(id,time,begin){
  var elem = getByID(id);

  var current = new Date().getTime();
  if(typeof(begin) == "number"){}
  else{
    begin = new Date().getTime()
  }// end else

  var timeDiff = (current - begin) / 1000; // /
  if(timeDiff < time){
    var boxOpacity = (timeDiff/time)*(timeDiff/time);
    setOpacity(elem.id,boxOpacity);
    setTimeout("fadeElementIn(\'"+id+"\',"+time+","+begin+")",16);
  }// end if(timeDiff < time)
  else{
    setOpacity(elem.id,.99);
    return;
  }// end else
}// end function fadeElementIn(id,time,begin)

/*
 *  fadeElementOut
 *
 *  @author  Nick O'Neill
 * 
 *  This script fades an element out from total opacity to total
 *  transparency.
 *
 *  Relies upon setOpacity and getByID as helper functions.
 *
 *  @param    id          ID of element to fade.
 *  @param    time        Time for fadeout to occur in seconds.
 *  @param    begin       Countdown until fadeout occurs in seconds.
 */
function fadeElementOut(id,time,begin){
   var elem = getByID(id);

   var current = new Date().getTime();
   if(typeof(begin) == "number"){}
   else{
     begin = new Date().getTime()
   }// end else

   var timeDiff = (current - begin) / 1000; // /

   if(timeDiff < time){
      var boxOpacity = .99-((timeDiff/time)*(timeDiff/time));
      setOpacity(elem.id,boxOpacity);
      setTimeout("fadeElementOut(\'"+id+"\',"+time+","+begin+")",16);
   }// end if(timeDiff < time)
   else{
     setOpacity(elem.id,0);
     return;
   }// end else
}// end function fadeElementOut(id,time,begin)

/*
 *  setOpacity
 *
 *  @author  Nick O'Neill
 * 
 *  This script sets an element's opacity from 0 (totally transparent)
 *  to 100 (totally opaque).
 *
 *  @param    id          ID of element.
 *  @param    opacity     Opacity level (0 to 100)
 */
function setOpacity(id,opacity){
   var toSet = getByID(id);

   if(toSet.style.MozOpacity != null){
     toSet.style.MozOpacity = opacity;
   }// end if(toSet.style.MozOpacity != null)
   if(toSet.style.opacity != null){
     toSet.style.opacity = opacity;
   }// end if(toSet.style.opacity != null)
   if(toSet.style.filter != null){
     toSet.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+(opacity*100)+")";
   }// end if(toSet.style.filter != null)
}// end function setOpacity(id,opacity)

/*
 *  getByID
 *
 *  @author   Nick O'Neill
 *
 *  This script retrieves an element by ID in a way that's a bit more
 *  inclusive than the getElementByID innate Javascript function.
 *
 *  @param    n           ID of element to be retrieved.
 *  @return   element     Element retrieved based upon ID parameter.
 */
function getByID(n){
  var d = window.document;
  if (d.getElementById){
    return d.getElementById(n);
  }// end if(d.getElementByID)
  else if (d.all){
    return d.all[n];
  }// end if(d.all)
}// end function getByID(n)

/*
 *  openCloseByID
 *
 *  @author   Kevin Barsotti <kevin.barsotti@tribaldawn.com>
 *
 *  This script toggles the visibility of a given object, by ID.
 *
 *  @param    id          ID of element.
 */
function openCloseByID(id){
  var obj = "";

  if(document.getElementById) obj = getElementById(id).style;
  else if(document.all) obj = document.all[id];
  else if(document.layers) obj = document.layers[id];
  else return 1;

  if(obj.display == "") obj.display = "none";
  else if(obj.display != "none") obj.display = "none";
  else obj.display = "block";
}// end function openCloseByID(id)

/*
 *  checkAll
 *
 *  @author   David J. Allison <david.allison@tribaldawn.com>
 *
 *  This script selects and deselects all checkboxes in a form.
 *
 *  @param    formname          ID of form.
 */
function checkAll(formname) {

  var i=0;

  for(i=0; i < formname.length; i++) {
    if(formname[i].type == "checkbox") {
      if(formname[i].checked == true) {
        formname[i].checked = false;
      }// end if(formname[i].checked == 1)
      else {
        formname[i].checked = true;
      }// end else
    }// end if(formname[i].type == "checkbox")
  }// end for(i=0, i < formname.length; i++)
}// end function checkall(formname)


