/*
 *	Author:				Rob Barthle
 *	Create Date:		08/28/2007
 *	Filename:			main.js
 *	Description:		A copy of the main.js and delman.js files from ed.gov, minorly altered so this application can use it.
 *	
 *	$Log$
 *	Revision 1.3  2007/09/04 17:31:45  rbarthle
 *	client requested changes
 *	
 *	Revision 1.2  2007/08/29 19:21:29  rbarthle
 *	fix help file link and consolidate javascript
 *	
 *	Revision 1.1  2007/08/28 19:24:47  rbarthle
 *	E_COHORT_8 - new look and feel to site
 *
 */

// * Functions to preload/swap images

// detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);

var imageDir = 'http://www.ed.gov/offices/OSFAP/cssetc/images/';

// MenuImage constructor
function MenuImage(img_id, img_out, img_in, img_act, swapped) 
{
    this.img_id = img_id;           // the image id
    this.img_out = new Image();     // the image to use when mouseout
    this.img_out.src = imageDir + img_out;
    this.img_in = new Image();      // the image to use when mouseover
    this.img_in.src = imageDir + img_in;
    this.img_act = new Image();     // the image when page active
    if(img_act) {
        this.img_act.src = imageDir + img_act;
    } else {
        this.img_act.src = '';
    }    
    this.swapped = swapped;         // register if the image is swapped
    this.over = 0;                  // register if the image is 'over'
}

// MenuImage(image_id, normal_image, active_image, mouseover_image, image_swapped)
var MImages = new Array(
   new MenuImage("btnGo1", "gocyan_off.gif", "gocyan_on.gif", "gocyan_on.gif", 0),
   new MenuImage("btnGo2", "gocyan_off.gif", "gocyan_on.gif", "gocyan_on.gif", 0),
   new MenuImage("btnGo3", "gocyan_off.gif", "gocyan_on.gif", "gocyan_on.gif", 0),
   new MenuImage("btnGo4", "gocyan_off.gif", "gocyan_on.gif", "gocyan_on.gif", 0),
	
   new MenuImage("download1", "download_off.gif", "download_on.gif", "download_on.gif", 0),
   new MenuImage("download2", "download_off.gif", "download_on.gif", "download_on.gif", 0),
   new MenuImage("download3", "download_off.gif", "download_on.gif", "download_on.gif", 0),
	
   new MenuImage("return1", "return_off.gif", "return_on.gif", "return_on.gif", 0),
   new MenuImage("return2", "return_off.gif", "return_on.gif", "return_on.gif", 0),
	
   new MenuImage("results1", "results_off.gif", "results_on.gif", "results_on.gif", 0),
   new MenuImage("results2", "results_off.gif", "results_on.gif", "results_on.gif", 0)
);

// take a full or partial path, and return its last part
function basename(path) 
{
    var parts = path.split('/');
    return parts[parts.length-1];
}

// get an image from an image_id
function get_image(id)
{
    for(var a=0; a<=MImages.length; a++)
    {
        if(MImages[a].img_id == id)
            return a;
    }
}

// swap in the image
function imgIn(image_id)
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        var theObj = MImages[imgId];
        var theImg = document.getElementById(theObj.img_id);
        var filename = basename(theImg.src);

        // don't swap if image is 'down' or currently active 
        if( filename.indexOf('_at') == -1 &&
           (filename.indexOf('_over') == -1 || filename.indexOf('_on') == -1) ) 
        {
            theImg.src = MImages[imgId].img_in.src;
            MImages[imgId].swapped = 1;
        }
    }
}

// swap out the image
function imgOut(image_id) 
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        theImg = document.getElementById(MImages[imgId].img_id);
        // only revert if "swapped" is true
        if(MImages[imgId].swapped) 
        {
            theImg.src = MImages[imgId].img_out.src;
            MImages[imgId].swapped = 0;
        }
    }
}


/*
 * Functions to swap the background color of a div 
 */

var savedBG = new Array();

function changeBG(id, color)
{
    if(capable && id && color)
    {
        var objStyle = document.getElementById(id).style;
        savedBG[id] = objStyle.backgroundColor;
        objStyle.backgroundColor = "#"+color;
    }
}

function changeBackBG(id)
{
    if(capable && id && savedBG[id])
    {
        var objStyle = document.getElementById(id).style;
        objStyle.backgroundColor = savedBG[id];
        savedBG[id] = ""; 
    }
}


/*
 * Functions to open links in a new window
 */
var newwin='';

// open a popup window
function popWindow(theUrl,width,height,full) {
    // use defaults if width and height were not supplied
    var ismoz = navigator.userAgent.indexOf("Gecko");
    var isie = navigator.userAgent.indexOf("MSIE");
    var default_width = 600;
    var default_height = 450;
    var offset_width = (isie != -1) ? 4 : 0;
    var offset_height = (isie != -1) ? 45 : 0;
    var popType = (full) ? ",scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes" : ",scrollbars=no,resizable=no,status=no";
    
    var popWidth = (width) ? width + offset_width : default_width + offset_width;
    var popHeight = (height) ? height + offset_height : default_height + offset_height;
    var popLeft = self.screen.availWidth/2 - popWidth/2;
    var popTop = self.screen.availHeight/2 - popHeight/2;
    
    if(theUrl) {
        if(newwin)
            newwin.close();
        newwin = window.open(theUrl,'newwin','left='+popLeft+',top='+popTop+',width='+popWidth+',height='+popHeight+popType);
    }

    return;
}

function isState(theField) {
	for(var i=0;i<theField.length;i++) {
		if(theField.options[i].selected) {
			if (theField.options[i].value.length != 2) {
				alert("Invalid selection; Please try again.");
				return false;
			}
			return true;
		}
	}
}	

function ShowTrainingHelp(){
 		var helpwin1 = window.open("http://www.ed.gov/offices/OSFAP/defaultmanagement/help.html","help1","toolbar=0,statusbar=0,height=220,width=320,menubar=0,left=100,top=100")
}
