//Copyright (c) 2000 direction inc. All rights reserved.
//Any reuse of this or any code in any of direction's solutions is strictly prohibited without written consent.
//Please refer to "www.directionsolutions/legal.html" for further important copyright & licensing information.

function pageCode() {
    findLength();
    if (allowMenu==1) writeMenus();
    if (is_ie5) initPulls();
    // Fix for IE to allow the :hover psuedoelement on input
    // type="(submit|button|reset)" and button.
    if (document.all && document.getElementById) {
        // Given a node, sets or removes the "over" class on all type of input
        // buttons. This is a fix for IE which doesn't support the :hover
        // pseudoelement on anything other than anchors.
        var f = function (node) {
            if (node.nodeName == 'BUTTON' ||
                node.nodeName == 'INPUT'  && ( node.type == 'submit' ||
                                                 node.type == 'button' ||
                                                 node.type == 'reset'  )) {
                node.onmouseover = function() { this.className +=" over"; } // Append class.
                node.onmouseout  = function() { this.className  = this.className.replace(" over", ""); }
            }
        }

        // Traverse the DOM tree and apply our mouse(over|out)s.
        var root = document.getElementById("content");
        if (root)
            mapTree(f, root);
    }
}

// Maps a function onto the nodes of a DOM tree (depth-first).
function mapTree (f, tree) {
    // Run on current element.
    f(tree);
    // Run on all child elements (type == 1)
    for (var i=0; i < tree.childNodes.length; i++) {
        var node = tree.childNodes[i];
        if (node.nodeType == 1)
            mapTree(f, node);
    }
}

function getDiv(divID, divStyle){
    var divObj;

	if (is_nav4only) {
		divObj = document.layers[divID];
	}
	else if (is_ie4up) {
		if (divStyle) divObj = document.all[divID].style;
		else divObj = document.all[divID];
	}
	else {
		if (divStyle) divObj = document.getElementById(divID).style;
		else divObj = document.getElementById(divID);
	}
	return divObj;
}

function getImage( name, layer ) {
	if ( ! layer )
		layer = 'content';
	var image = null;
	if ( is_nav4only ) {
		if ( document.layers[layer] ) {
			image = document.layers[layer].document.images[name];
		} else {
			image = document.images[name];
		} 
	} else {
		image = document.images[name];
	} /* end if */
	return image;
} /* function getImage( name ) */

/* ------------ btn/menu scripts ------------ */

function btnOn( btnName ) {
	var image = getImage( btnName );
	var filename;
	if ( image ) {
		filename = image.src.substring( image.src.lastIndexOf('/') + 1 );
		image.src = '/site_specific/images/buttons/on/' + filename;
	} /* end if */
	self.status = filename.substring( 0, filename.lastIndexOf('.') );
	return true;
} 

function btnOff( btnName ) {
	var image = getImage( btnName );
	if ( image ) {
		var filename = image.src.substring( image.src.lastIndexOf('/') + 1 );
		image.src = '/site_specific/images/buttons/off/' + filename;
	} /* end if */
	self.status = '';
} 


/* ------------ form scripts ------------ */
function on(iName){
//var formOn;	// I'm using a sneaky trick here to save on time ... if we declare formOn here without initializing it, then we set a pointer to the namespace of the
			// already defined variable good enough to check if it is defined or not. So when it is defined we can call it with the value it has already been given.
			// When it isn't defined we can still check it for undefined without getting a runtime error. It is "cheating" but it works ... though it should be
			// checked still in all supported browsers - Duke
// To have this work in IE the formOn/formOff need to be declared before loading the menus. So we have done this in detect.js.

// added 'formOn &&' to if statment for this logic to work in Windows IE.
	if (formOn && formOn != "undefined") {
		if ( (is_ie4up || is_nav5up) && iName && iName.style )  {
			iName.style.background = formOn;
		}
	}
}

function off(iName){
	if (formOff && formOff != "undefined") {
		if ((is_ie4up || is_nav5up) && iName && iName.style )  {
			iName.style.background = formOff;
		}
	}
}

function clearForm(what) {
	for (var i=0, j=what.elements.length; i<j; i++) {
		myName = what.elements[i].type;

		if (myName != undefined) {	// Check for Undefined because Fieldset Tags
			if (myName.indexOf('checkbox') > -1 || myName.indexOf('radio') > -1) {
				what.elements[i].checked = "";
			}
			if (myName.indexOf('hidden') > -1 || myName.indexOf('password') > -1 || myName.indexOf('text') > -1) {
				 what.elements[i].value = "";
			}
			if (myName.indexOf('select') > -1) {
				for (var k=0, l=what.elements[i].options.length; k<l; k++) {
					what.elements[i].options[k].selected = 0;
					what.elements[i].options[0].selected = 1;
				}
			}
		}
	}
}

var fmChange = 0;
function fmCheck( form ) {
	if ( fmChange == 1 ) {
		if ( confirm("Are you sure you want to leave this record without saving your changes?") ) {
			fmChange == 0;
            form.submit();
			return true;
		}
	} else if (fmChange == 2) {
		fmChange = 0;
		if ( confirm("Are you sure you want to delete this record?") ) {
            form.submit();
			return true;
		}
	} else if (fmChange == 3) {
		fmChange = 0;
		if ( confirm("Are you sure you want to save your changes?") ) {
            form.submit();
			return true;
		}
	} else {
		form.submit();
		return true;
	}
	return false;
}

function addCheck(formName) {
	if (fmChange == 1) {
		if (confirm("Are you sure you want to create a new record, without saving your changes")) {
			clearForm(formName);
		}
	}
	else {
		clearForm(formName);
	}
}

/* ----------- browser bug code ----------- */

// fix pulldowns in ie 5.0
function initPulls() {
	for ( var j=0; j < document.forms.length; j++) {
		var form = document.forms[j];
		for ( var i = 0; i < form.elements.length; i++) {
			var type = form.elements[i].type;
			if (type.indexOf('select') > -1) {
				for (var k=0; k<form.elements[i].options.length; k++) {
					if ( ! form.elements[i].options[k].selected ) on(form.elements[i].options[k]);
				}
			}
		}
	}
}

// create scrolling
function findLength() {
 	var conObj = getDiv("content");
	if ( conObj ) {
		var scr=0;
		var pgLength;
		if (is_nav4only) {
			scr=1;
			pgLength = conObj.clip.height + conObj.top;
		} else if ((is_ie4up) && (plat == "mac")) {
			scr=1;
			pgLength = conObj.offsetHeight + conObj.offsetTop;
		}
		if(scr) document.write('<img src="/site_specific/images/main/header/trans.gif" width=2 height='+pgLength+' />');
	} 
}

// n4 resize window
if( is_nav4only ) {
reloadPage(true);
}
function reloadPage(init) {
  	if (init==true) with (navigator) {
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
  	  		document.pgW=innerWidth; document.pgH=innerHeight; onresize=reloadPage;
		}
	}
  	else if (innerWidth!=document.pgW || innerHeight!=document.pgH) {
		location.reload();
	}
}


/*
 EVENT HANDLING : Very simplistic, ignores many know cases.
*/

// Cross-browser (sorta) (W3C and MS event models) event listening.
function addEvent(target, action, callback, bubble) {
    if (target.addEventListener)
        target.addEventListener(action, callback, bubble);
    else if (target.attachEvent)
        target.attachEvent('on' + action, function () {
            callback.apply(target, [window.event])
        });
}

// Cross-browser (W3C and MS event models) event removal.
function removeEvent (target, action, callback, bubble) {
    // W3C Model (Mozilla, Safari, Opera)
    if (target.removeEventListener )
        target.removeEventListener (action, callback, bubble);
    // Microsoft model (Win IE5+)
    else if (target.detachEvent) {
        target.detachEvent('on' + action, function () {
            callback.apply(target, [window.event])
        });
    }
}
