//
//  progressbar.js - javascript to show/hide a progress bar defined by div object
//
//
//  This script works with a div object called 'waitDiv'. Users can provide their own
//  messages and picture for progress bar and display it on the middle of window,
//  the location of a given object or where they click.
//

var DHTML = (document.getElementById || document.all || document.layers);

ie = (navigator.appName.indexOf("Microsoft")!=-1);
ns = (navigator.appName.indexOf("Netscape") != -1);

ns4= (ns && navigator.appVersion.indexOf("5") == -1);
ns6= (ns && navigator.appVersion.indexOf("5") != -1);
ns6X = 0;
ns6Y = 0;

// Get the object by name.
function ap_getObj(name)
{
    if (document.getElementById) { return document.getElementById(name).style; }
    else if (document.all)
        { return document.all[name].style;}
        else if (document.layers) { return document.layers[name]; }
}

// Hide the div layers by name.
function hideDivObject(divId)
{

    if (ie){
        document.all[divId].style.visibility = 'hidden';
    }
    if (ns4){
        document.layers[divId].visibility = false;
    }
    if (ns6){
        document.getElementById(divId).style.visibility = 'hidden';
    }
}

// Show progress bar with message "Process your request, please wait..."
function showProgressBar()
{
  showProgressBar(null, null)
}

// Show progress bar with given messages.
// The progress bar picture is from ../includes/await.gif
function showProgressBar(msg1, msg2)
{
  showProgressBar(msg1, msg2, "await.gif")
}

// Show progress bar with given messages and the picture
function showProgressBar(msg1, msg2, progressBarPicture)
{
 try
 {
  if (document.images)
    document['progressBar'].src = progressBarPicture;
    if (!DHTML)
          return;
    if (msg1==null)
    	msg1 = "Processing Your Request.";
    if (msg2==null)
    	msg2 = "Please wait...";

    document.getElementById('waitMsg1').innerHTML = msg1;
    document.getElementById('waitMsg2').innerHTML = msg2;
    var x = ap_getObj("waitDiv");
    if(! document.getElementById)
          if(document.layers)
            x.left=280/2;
    x.visibility = 'visible';

    hideElement( 'SELECT', x );
  }catch (e){}

    return true;
}

// Show progress bar on the location of the given object with give
// two messages and picture.
function showProgressBarAt(msg1, msg2, progressBarPicture, obj)
{
 try
 {
  document.getElementById('waitMsg1').innerHTML = msg1;
  document.getElementById('waitMsg2').innerHTML = msg2;
  if (document.images)
    document['progressBar'].src = progressBarPicture;
  showDivObject("waitDiv", obj, 0, 0);
 }catch (e){}
}

// Hide the progress bar.
function hideProgressBar()
{
 try
 {
   hideDivObject('waitDiv');
 }catch (e){}

}


// Show div layer by name at the location of the given object
// or the most current event if obj is null.
function showDivObject(divId, obj)
{
  showDivObject(divId, obj, 0, 0);
}

// Show div layer by name at the location of the given object
// with the given offsetx and offsety, or the most current event if obj is null, .
function showDivObject(divId, obj, offsetx, offsety)
{

    var posX = window.width/2;
    var posY = window.height/2;
    //offsetx = 0;

    if (obj!=null)
    {
        posX = obj.x;
        posY = obj.y;
        //offsetx = 210
    }
    if (ie)
    {
        with (document.getElementById(divId))
        {
            var posx = posX;
            var posy = posY;
            if (obj==null || typeof obj.x == 'undefined')
    	    {
              posx = window.event.clientX + document.body.scrollLeft;
              posy = window.event.clientY + document.body.scrollTop;
            }
            style.left = posx -offsetx;
            style.top  = posy;
            style.visibility = 'visible';
        }
    }
    if (ns4)
    {
        with(document.layers[divId])
        {
            top = posY + offsety;
            left = posX -offsetx;
            visibility = true;
        }
    }
    if (ns6)
    {
        with(document.getElementById(divId).style)
        {
            top = ns6Y + offsety;
            left = ns6X - offsetx;
            visibility = 'visible';
        }
    }

    showElement( 'SELECT');

}

// hide element (<select> and <applet> objects for IE only)
function hideElement( elmID, overDiv ){
    if( ie ){
        for( i = 0; i < document.all.tags( elmID ).length; i++ ){
            obj = document.all.tags( elmID )[i];
            if( !obj || !obj.offsetParent ){
                    continue;
            }
              // Find the element's offsetTop and offsetLeft relative to the BODY tag.
              objLeft   = obj.offsetLeft;
              objTop    = obj.offsetTop;
              objParent = obj.offsetParent;
              while( objParent.tagName.toUpperCase() != "BODY" )
              {
                objLeft  += objParent.offsetLeft;
                objTop   += objParent.offsetTop;
                objParent = objParent.offsetParent;
              }
              objHeight = obj.offsetHeight;
              objWidth = obj.offsetWidth;
              if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
              else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
    /* CHANGE by Charlie Roche for nested TDs*/
              else if( overDiv.offsetTop >= ( objTop + objHeight + obj.height ));
    /* END CHANGE */
              else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
              else
              {
                obj.style.visibility = "hidden";
              }
        }
    }
}

//show element (<select> and <applet> objects for IE only)
function showElement( elmID ){
    if( ie ){
        for( i = 0; i < document.all.tags( elmID ).length; i++ ){
            obj = document.all.tags( elmID )[i];
            if( !obj || !obj.offsetParent ){
                    continue;
            }
            obj.style.visibility = "";
        }
    }
}
