/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

//=========================================================
// Disable submit button
/*
Page_Load()
btnSave.Attributes.Add("onclick", "DisableButton()")
*/
    function DisableButton() {    
        document.forms[0].submit();
        window.setTimeout("disableButton('" + 
            window.event.srcElement.id + "')", 0);
    }

    function disableButton(buttonID) {
        //document.getElementById(buttonID).disabled=true; 
        disableAllButtons(true);    
    }

//=====================================================
function disableAllButtons(blnAction) {
			for (i=0; i<document.forms[0].elements.length; i++) {
				InputNodeName = document.forms[0].elements[i].name;
				if(InputNodeName !=null){	//safeguard
					if(InputNodeName.toUpperCase() != 'BTNCANCEL'){	//Exempt for cancel buttons (using an OR "||" didn't work)
						if ((document.forms[0].elements[i].type == 'submit') || (document.forms[0].elements[i].type == 'button')) {
							if(blnAction){document.forms[0].elements[i].disabled = true;}
							else{document.forms[0].elements[i].disabled = false;}
						}
					}
				}
			}
}
//=====================================================
function cursorWait() {
	var fe;
		for(var f=0;f<=(document.forms.length-1);f++) {
			for(var e=0;e<=(document.forms[f].elements.length-1);e++){
				fe=document.forms[f].elements[e];
				fe.style.cursor='wait';
				
				if(fe.type) {
					if(fe.type.indexOf("select-one")==0 || fe.type.indexOf("select")==0) fe.style.cursor='wait';
				}
			}
		}
}
//=====================================================
function resetCursor(objForm)
	{
		for (i=0; i<objForm.elements.length; i++) {
			objForm.elements[i].style.cursor = 'auto';
		}
	}
	
//=====================================================
function trim(strText)
	{
		//this will get rid of leading spaces
		while (strText.substring(0,1) == ' ')			
			strText = strText.substring(1, strText.length)
		//this will get rid of trailing spaces
		while (strText.substring(strText.length-1,strText.length) == ' ')			
			strText = strText.substring(0, strText.length-1)
		return strText
	}	//end trim()

//====================================================

//With nested layers for netscape, this function 
//hides the layer if it's visible and visa versa
//Toggles same div on/off

//'nest' arg is optional
function showHide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0; 
	if(obj.visibility=='visible' || obj.visibility=='show') {
			obj.visibility='hidden';
			obj.display='none';
	}
	else {
		obj.visibility='visible';
		obj.display='block';
	}
}

//Shows the div
function show(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0; 
	obj.visibility='visible';
	obj.display='block';
}
//Hides the div
function hide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0; 
	obj.visibility='hidden';
	obj.display='none';
}

//swaps between two divs
function swap(showObj,hideObj)
{
	show(showObj);
	hide(hideObj);
}

/*
======= POP-UP WINDOW Function ===========================
Author: Fred Scales

An example of the code used to fire this function:
<A HREF="somepage.asp" 

onclick="openPopupWindow(this.href,'theWindow','640','480','scrollbars=no,menubar=yes,resizable=no,status=no,toolbar=no');return false;">Click Here open Popup Window</A>
-- OR --
openPopupWindow('somepage.asp','theWindow','640','480','scrollbars=no,menubar=yes,resizable=no,status=no,toolbar=no');

Optional parameter values:
	w = 'full'
	h = 'full'
Will create a width or a height that occupies the full screen of that user's available space.
=========================================================
*/
	var win = null
	var winl;
	var wint;
	function openPopupWindow(mypage, myname, w, h, strother) {

	// if w = 'full' and h = 'full'
	if(h == "full") {
		//h = screen.height - 170;		//To adjust for windows taskbar
		h = window.screen.availHeight - 110;	//To adjust for windows taskbar
		wint = 0;
	}
	else{
		wint = 20;
	}
	
	if(w == "full") {
		//w = screen.width - 10		//Just a little space
		w = window.screen.availWidth - 10
		strother += ' fullscreen=yes';
		winl = 0;
	}
	else{
		winl = (screen.width - w) / 2;	
	}
	
	// default if no w or h specified
	if(isNaN(h) || (h == "")) {
		h = 420;
	}
	if(isNaN(w) || (w == "")) {
		w = 620;
	}
	
	
	winprops = 'width='+w+',height='+h+',top='+wint+',left='+winl+','+strother+''
	win = window.open(mypage, myname, winprops)
	
	
	//Check if window already exists
		if (!win || win.closed)	{
			//store new window object in global variable
			win = window.open(mypage, myname, winprops);
		}
		else {
			//Window already exists, so bring it forward
			win.focus()
		}
		
}