//<!-- TOPIC Javascript Libraries -->
/* 
 * Functions to preload/swap images
 */

// detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);

// swap in the image
function imgIn(image, state)
{
    if(capable && image != null) {
        // swap to the rollover mode
        // if state is null, assume default state is off

       	var src = image.src;
        if(state != null && (state == "on" || state == "cl")) {
        	image.src = src.replace("_cl.gif", "_ro.gif");
        } else {
        	image.src = src.replace("_wh.gif", "_ro.gif");
		}
    }
}

// swap out the image
function imgOut(image, state) 
{
    if(capable && image != null) {

        // revert to the idle state
        // if state is null, assume default state is off

       	var src = image.src;
        if(state != null && (state == "on" || state == "cl")) {
        	image.src = src.replace("_ro.gif", "_cl.gif");
        } else {
        	image.src = src.replace("_ro.gif", "_wh.gif");
		}
    }
}

/*
 * 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 to execute on loading
function InitForm(szField, szMsg, szType, szIndex) {
	SetFocus(szField);
	
	if (szMsg && szMsg.length > 0) {
		with(document.FAFSA) {
			if (szIndex && szIndex != "null" && szIndex != "" && szIndex != "0") {
				if (confirm(szMsg)) {
					confirmErr.value = "1";
					confirmErr.name = "szCONFIRMXX" + szIndex;
					NextPage();
				}
			} else {
				alert(szMsg);
			}
		}
	}
}

// funtion to set focus on the form field
function SetFocus(szField)
{
	if (szField) {
		var fieldType = szField.type;
    		szField.focus();
		if (fieldType.indexOf("select") < 0) 
		{
			szField.select();
		}
	} else {
		with (document.FAFSA) {
			for (var i=0; i<length; i++) {
				if (elements[i].type == "text"		|| 
					elements[i].type == "textarea" 	||
					elements[i].type == "password" 	|| 
					elements[i].type == "file") {
					elements[i].focus();
					elements[i].select();
					break;
				} else if (elements[i].type.indexOf("select") >= 0) {
					elements[i].focus();
					break;
				}
			}
		} 
	}
}

// function to set the menuitem active, usually on the load of the page
function setMenuitemOn(szMenuId) {
  if (capable) {
     var ID = document.getElementById(szMenuId);
     if (ID != null) {
     	ID.style.background = "url(../images/left_main_on.gif)";
     }
  }
}

// function to set the submenu active, usually on the load of the page
// bTwo = ture when the submenu has two lines
function setSubmenuOn(szMenuId, bTwo) {
  if (capable) {
     var ID = document.getElementById(szMenuId);
     if (ID != null) {
     	if (bTwo) {
     	  	ID.style.background = "url(../images/subnav2_on.gif) no-repeat";
     	} else {
     	    ID.style.background = "url(../images/subnav_on.gif) no-repeat";
     	}
     }
  }
}