
// Initialize variables:

var qq = '\"';						// Escaped double quote character
var d=document;

// ============================================================================================================

var thisURL = unescape(location.href);				// Get current docName from thisURL

var docNameStartPos = thisURL.lastIndexOf("/") + 1;
var docName = thisURL.substring(docNameStartPos, thisURL.length);

if (self.location == top.location && docName.search(/index.html/)==-1 ) {			// Prevent this frame from loading outside the frameset
	top.location.href = 'index.html';
}

if ( docName.search(/bodyAnchor/) > 0 ) {

		// Scroll to bodyAnchor, if specified:

	var parsedURL = docName.split("bodyAnchor=");	// GET Parameter
	var bodyAnchor = parsedURL[1];
				
	var url = 'welcome.html' + '#' + bodyAnchor;
	var cmd = "getObjectByID('bodyFrame').contentWindow.location.href = url; ";
	setTimeout(cmd, 1800);
}


// ============================================================================================================


function preloadGIF() {					// Load button highlights into the browser's memory cache

	if (d.images){ 
		if(!d.allImages) d.allImages=new Array();
		var i,j=d.allImages.length, imgList=preloadGIF.arguments;

		for(i=0; i<imgList.length; i++)
			if (imgList[i].indexOf("#")!=0){ 
					d.allImages[j]=new Image; 
					d.allImages[j++].src='images/' + imgList[i] + '.gif';
			}
		}
}


function preloadGIFsub() {				// Load button highlights from a subdirectory

	if (d.images){ 
		if(!d.allImages) d.allImages=new Array();
		var i,j=d.allImages.length, imgList=preloadGIFsub.arguments;

		for(i=0; i<imgList.length; i++)
			if (imgList[i].indexOf("#")!=0){ 
					d.allImages[j]=new Image; 
					d.allImages[j++].src='../images/' + imgList[i] + '.gif';
			}
		}
}


function validateEmailField() {				// Checks to see if the field named 'email' has @ and . chars
							// Returns +1 if true
	emailFieldObj = getObjectByID('email');
	var email = emailFieldObj.value;

	var emailRegEx = /^[^@]+@[^@]+.[a-z]{2,}$/i;

	if(email.search(emailRegEx) == -1){
		validEmail = 0;
	} else {
		validEmail = 1;
	}
	
	return validEmail;
}


function getObjectByID(name) {				// Get an object's ID tag for easy referencing

	if (document.getElementById) {				// Firefox and Safari
		return document.getElementById(name);
	} else if (document.all) {				// IE
		return document.all[name]; 
	} else if (document.layers) {
		return document.layers[name];
	} else {
		return document.getElementById(name);		// Default for unknown browsers
	}
}

function setValue() {				// Sets the value property of a specified field or control

	var lastObj = (setValue.arguments.length);
	for(n=0; n<lastObj; n++) {
		thisID = setValue.arguments[n]
		thisObj = getObjectByID(thisID);
		thisVal = setValue.arguments[++n];

		thisObj.value = thisVal;
	}
}

function getValue(id) {			// Returns the value property of the specified object

	var obj = getObjectByID(id);
	return obj.value;
}


function getFrameObj(whichFrame){				// Returns an iFrame object reference when called from
								//   the parent document. Use to call iFrame functions
								//   from the parent.
	var frameObj = getObjectByID(whichFrame);
	return frameObj.contentWindow;
}

function embedFlash(thisFile, w, h) {	// Shows the activeX control without an annoying 'activate' prompt

	var embedCode = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' " 
		+ "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + w + "' height='" + h + "'> " 
		+ "<param name='movie' value='" + thisFile + "' /> " 
		+ "<param name='quality' value='high' /> " 
		+ "<param name='wmode' value='transparent' /> " 
		+ "<embed src='" + thisFile + "' width='" + w + "' height='" + h + "' quality='high' " 
		+ "pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' wmode='transparent'> " 
		+ "</embed></object>";

	document.write(embedCode);
}


function buttonState(buttonID, state) {			// Change button state by replacing .gif files:
							// Valid states are '', '_h', and '_s'
	var buttonName = buttonID + 'Btn';
	var newImgFile = 'images/mmBtn_' + buttonID + state + '.gif';

	buttonObj = getObjectByID(buttonName);

	buttonObj.src = newImgFile;
}


function toggleMenu(menuID) {				// If table is visible, hide it and vice versa

	var menuName = menuID + "Menu";
	menuObj = getObjectByID(menuName);

	if (menuObj.style.display == "none") {
		menuObj.style.display = "";
	} else {
		menuObj.style.display = "none";
	}
}

function toggleObject(ID) {				// If table is visible, hide it and vice versa

	thisObj = getObjectByID(ID);

	if (thisObj.style.display == "none") {
		thisObj.style.display = "";
	} else {
		thisObj.style.display = "none";
	}
}

function showObject() {					// Show a dynamic form field or menu
							// Iterates through argument list if an array is input
	var lastObj = showObject.arguments.length;
	for(n=0; n<lastObj; n++) {
		thisObj = getObjectByID(showObject.arguments[n]);
		thisObj.style.display = "";
	}
}



function hideObject() {					// Show a dynamic form field or menu
							// Iterates through argument list if an array is input
	var lastObj = hideObject.arguments.length;
	for(n=0; n<lastObj; n++) {
		thisObj = getObjectByID(hideObject.arguments[n]);
		thisObj.style.display = "none";
	}
}


function expandMenu(menuID) {				// Show menu and highlight associated button

	buttonState(menuID, '_h');
	showObject(menuID + 'Menu');
}


function collapseMenu(menuID) {				// Hide menu and de-highlight associated button

	buttonState(menuID, '');
	hideObject(menuID + 'Menu');
}


function getScreenSpecs(defaultW, defaultH) {		// Accepts default width and height as input and returns 
							// either actual screen width and height or the defaults
	screenH = defaultH
	screenW = defaultW;

	if (parseInt(navigator.appVersion)>3) {
		 screenH = screen.height;
		 screenW = screen.width;
	}
	else if (navigator.appName == "Netscape" 
	    && parseInt(navigator.appVersion)==3
	    && navigator.javaEnabled()
	) 
	{
	 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
	 var jScreenSize = jToolkit.getScreenSize();
	 screenH = jScreenSize.height;
	 screenW = jScreenSize.width;
	}

	return [screenW, screenH];
}

function getAvailHeight() {

    var screenH = screen.height;
    if(document.all){
        availH = document.body.clientHeight;	// For IE
    }else{
        availH = innerHeight - 15;		// Offset for Firefox
    }
    return availH;
}


function getAvailWidth() {

    var screenW = screen.width;
    if(document.all){
        availW = document.body.clientWidth;
    }else{
        availW = innerWidth;
    }
    return availW;
}

function setHeading(headText, subheadText, topIcon) {			// Changes the page heading to new text

	if (headText) {
		headingObj = getObjectByID('headingSpan');
		headingObj.innerHTML = headText;
	}

	if (subheadText) {
		subheadingObj = getObjectByID('subheadingSpan');
		subheadingObj.innerHTML = subheadText;
	}

	if (topIcon) {
		headerIconObj = getObjectByID('headerIcon');
		headerIconObj.src = "images/" + topIcon;
	}
}

function toggleOnSelect(id, showValue, v) {		// Shows an object if the selected value = a specified value;
							//	otherwise hides it
	var thisObj = getObjectByID(id);

	if (v == showValue) {					// user selected the show value: show it

		thisObj.style.display = '';
	} else {						// user selected something else; hide it
		thisObj.style.display = 'none';
	}
}


function openWin(whichURL,winW,winH) {			// Open document in a new window with no toolbars or resizing

	var screenWH = getScreenSpecs(800, 600);
	screenW = screenWH[0];
	screenH = screenWH[1];

	popWin=window.open(whichURL,"pWin","toolbar=no,scrollbars=yes,resizable=no,width="+winW+",height="+winH+ ",left=" + Math.round((screenW/2)-(winW/2))+ ",top=" + Math.round((screenH/3)-(winH/2)) );
}


function browserWin(whichURL,winW,winH) {		// Open document in a new window with full browser controls

	var screenWH = getScreenSpecs(800, 600);
	screenW = screenWH[0];
	screenH = screenWH[1];

	browserWin=window.open(whichURL,"pWin","menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,width="+winW+",height="+winH+ ",left=" + Math.round((screenW/2)-(winW/2))+ ",top=" + Math.round((screenH/3)-(winH/2)) );
}


function focusOn(targetID) {		// Gives focus to the named field or form object

	var targetObj = getObjectByID(targetID);
	targetObj.focus();
}


function setFormDate() {				// Places the current date on the EDLST212 Registration Form

        var now = new Date();
	var m = now.getMonth();
	m+=1;
	var d = now.getDay();
	var y = now.getFullYear();

	thisDate = m + '/' + d + '/' + y;

		// Set hidden Date field on the form:

	var dateObj = getObjectByID('date');
	dateObj.value = thisDate;

		// Display Semester Year on the form (i.e., "Fall 2007")

	var yearObj = getObjectByID('year');
	yearObj.innerHTML = y+' ';
}


function setActionAndSubmit (formName, state) {			// Specify the current action and post form data
// alert(formName + ', state = '+state);
	formObj = getObjectByID(formName);
// alert(formObj);
	formObj.action.value = state;
	formObj.submit();
}


function setText (ID, text) {					// Sets the innerHTML of a <span> field to desired text.

	var textSpanObj = getObjectByID(ID);
	textSpanObj.innerHTML = text;
}

function getText (ID) {						// Returns the innerHTML of a specified <span> field

	var textSpanObj = getObjectByID(ID);
	var text = textSpanObj.innerHTML;

	return(text);
}

function submitURLdata(url, id) {				// Sends the value of listed objects to the URL via GET method

	var thisObj = getObjectByID(id);
	var thisURL = url + "?" + id + "=" + thisObj.value;

	window.location = thisURL;
}

function submitURLformData(url, form, id1, id2) {		// Sends the value of <form> objects to the URL via GET method

	if (form) {
		objRef = form + '.' + id1;
	} else {
		objRef = id1;
	}

	var thisObj = eval(objRef);
	var thisURL = url + "?" + id1 + "=" + thisObj.value;

	if (id2) {
		objRef = form + '.' + id2;
		thisObj = eval(objRef);
		thisURL += '&' + id2 + '=' + thisObj.value;
	}
// alert(thisURL);	
	window.location = thisURL;
}

function forwardURLdata(gotoURL) {				// Reads the GET data string from the current bodyFrame URL, appends it to gotoURL,
								//	and branches to that page

	var parameterList = parent.bodyFrame.location.href.split("?");		// GET Parameters

	gotoURL += "?" + parameterList[1];
	window.location = gotoURL;
}


function parseURLdata(param) {

	var paramResult = '';

	var parameterList = parent.bodyFrame.location.href.split("?");	// GET Parameters
	var result = 0;

	if (parameterList.length == 1) {			// No parameters; set defaults for home page:

		result = 0;

	} else {						// Split list into name.value pairs

		var parameterPairs = parameterList[1].split("&");
		var nameAndValue = new Array();
		var paramValue = new Array();
		var paramKey = new Array();

		for(p=0; p < parameterPairs.length; p++) {	// Cycle through name/value pairs & store values

			nameAndValue = parameterPairs[p].split("=");
			paramKey[p] = nameAndValue[0];
			paramValue[p] = nameAndValue[1];

			if (paramKey[p] == param) {		// Is this the target key?
				paramResult = paramValue[p];
			}
		}
	}

	// Remove anchors from selected:

	var last = (paramValue.length) - 1;

	if (paramValue[last].search('#') > 0) {

		var segment = selected.split("#");
		paramValue[last] = segment[0];
	}

	if (param) {
		return paramResult;
	} else {						// No target param specified; return a list of values
		return paramValue;
	}
}

function disableIfEqual(targetControl, srcControl, thisValue) {		// Disables targetControl if srcControl has thisValue

	srcObj = getObjectByID(srcControl);
	targetObj = getObjectByID(targetControl);

	if (srcObj.value == thisValue) {

		targetObj.disabled = true;
	} else {
		targetObj.disabled = false;
	}
}

function disableIfNotEqual(targetControl, srcControl, thisValue) {	// Disables targetControl if srcControl has thisValue

	srcObj = getObjectByID(srcControl);
	targetObj = getObjectByID(targetControl);

	if (srcObj.value !== thisValue) {

		targetObj.disabled = true;
	} else {
		targetObj.disabled = false;
	}
}


function showLayer (thisLayer, position) {		// Makes a layer visible. If position is null, the layer
							// stays where it is. Otherwise it appears centered at mouseY.
	var layerObj = getObjectByID(thisLayer);
	layerObj = layerObj.style;

	if (position = 'atClick') {

		var thisOffset = 88;

		var ieMouseY = event.clientY + document.body.scrollTop;

		var screenWH = getScreenSpecs(800, 600);

		var layerH = parseInt(layerObj.height);
		var layerW = parseInt(layerObj.width); 

		var setY = ieMouseY - parseInt(layerH / 2) + thisOffset;
		var setX = parseInt((screenWH[0] - layerW)/2)-26;

		layerObj.top = setY+'px';
 		layerObj.left = setX+'px';
	}

	layerObj.visibility = 'visible';
}


function hideLayer(thisLayer) {					// Makes a named layer invisible.

	var layerObj = getObjectByID(thisLayer);
	layerObj.style.visibility = 'hidden';
}

var ie4 = (document.all)?true:false;				// Browser detection variables for hideDiv/showDiv...
var ns4 = (document.layers)?true:false;
var ns6 = (document.getElementById && !document.all)?true:false;

function hideDiv(whichLayer){						// Hide a layer

	if (ie4){document.all[whichLayer].style.visibility = "hidden";}
	if (ns4){document.layers[whichLayer].visibility = "hide";}
	if (ns6){document.getElementById([whichLayer]).style.display = "none";}
}

function showDiv(whichLayer){						// Show a layer

	if (ie4){document.all[whichLayer].style.visibility = "visible";}
	if (ns4){document.layers[whichLayer].visibility = "show";}
	if (ns6){document.getElementById([whichLayer]).style.display = "block";}
}

function changeDivText(whichLayer,txt){				// Change text within a layer

	if (ie4){document.all[whichLayer].innerHTML = txt;}
	if (ns4){
		document[whichLayer].document.write(txt);
		document[whichLayer].document.close();
	}
	if (ns6){
		over = document.getElementById([whichLayer]);
		range = document.createRange();
		range.setStartBefore(over);
		domfrag = range.createContextualFragment(txt);
		while (over.hasChildNodes()){over.removeChild(over.lastChild);}
		over.appendChild(domfrag);
	}
}

function showTip(textSrc){						// Moves a hidden layer named 'tip' to the
									//    mouse position, acquires text from a
		textSrc+= 'Tip';					//    specified hidden field (textSrc), and 
		textSrcObj = getObjectByID(textSrc);			//    shows the layer; id est: a tooltip.

		tipText = textSrcObj.value;
		changeDivText('tip', tipText);

		layerObj = getObjectByID('tip');

		var layerH = parseInt(layerObj.style.height);		// Get the dimensions of the layer for 
		var layerW = parseInt(layerObj.style.width); 		//     a good layer offset

		if (!document.all) {					// Capture mouse events
		  window.captureEvents(Event.MOUSEMOVE);
		  Ypos = evnt.pageY+100;
		  Xpos = evnt.pageX+100;
		} else {
		  Xpos=event.x;
		  Ypos=event.y;
		// e.pageX;
		// e.pageY;
		}
		Ypos = parseInt(Ypos - layerH - 18);
		layerObj.style.top = Ypos+'px';				// Move the tip near the cursor
 		layerObj.style.left = Xpos+'px';

		showDiv('tip');								// Make it visible
}

function hideTip(){
	hideDiv('tip');
}


// ############################################################

//   CSS PAGE FORMAT CUSTOMIZATION (resizing bodyFrame, etc.)

// ############################################################


function setColumns(n) {	// Switches between 1 or 2 column page format

	var wOffset = 330;		// If left coloumn is visible, reduce it

	if (n == 1) {
		hideObject('leftColumn');
		getObjectByID('columnPeak').style.backgroundColor = '#FBE1A3';
		wOffset = 0;
	} else {
		showObject('leftColumn');
		getObjectByID('columnPeak').style.backgroundColor = '#DAC28A';
		wOffset = 330;
	}

	setValue('wOffset', wOffset);
	initBody();
}

function initBody() {		// Resize the bodyFrame when drawing or refreshing the page

	var wOffset = parseInt( getValue('wOffset') );

	if (document.all) {
		getObjectByID('bodyFrame').style.height = (parseInt(document.body.clientHeight) - 135) + 'px';		// IE
		getObjectByID('bodyFrame').style.width = (parseInt(document.body.clientWidth) - (38*2 + wOffset) ) + 'px';
	} else {
		getObjectByID('enclosingDiv').style.height = (parseInt(document.body.clientHeight) - 135) + 'px';	// FF
		getObjectByID('bodyFrame').style.height = (parseInt(document.body.clientHeight) - 136) + 'px';
		getObjectByID('bodyFrame').style.width = (parseInt(document.body.clientWidth) - (38*2 + wOffset) ) + 'px';
	}
}


// ####################

//   AJAX SUBROUTINES

// ####################


function fetcHTML(url, targetDiv) {		// Requests content from url and displays it in targetDiv: a simple way to load HTML into a scrolling DIV

	getObjectByID('bodyFrame').scrollTop = 0;

	getObjectByID('fetcHTMLForm').action = url;
	sendReq('fetcHTMLForm', targetDiv);


}


function getPage(path, url) {

	getObjectByID('bodyFrame').scrollTop = 0;

	setValue('getPage', url, 'getPath', path);
	sendReq('getPageForm', 'bodyFrame');
}