
function popWindow(url, width, height, scrollable)	{
	var w = window.open(url, 'pop', "height=" + height + ", width=" + width + ", scrollbars=" + scrollable); 
	w.focus(); 
	return w;
}


/*******************************************************************/


function disableAnchors(container) {
	if (!container)
		container = document;
	
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++) {
		if (isAncestor(container, anchors[i])) {   /* isAncestor() is defined in DOM.js */
			anchors[i].href = 'javascript:void(0);';
			anchors[i].target = null;
			anchors[i].onclick = null;
		}
	}
}


/*******************************************************************/


/* 
GTO Glossary popup function.
Show single glossary term in popup        
Use in conjunction with utility/glossaryPopup/__layout/default.aspx
*/

var GlossaryWindow = null;

function showGlossary(term) {
	if( GlossaryWindow == null || GlossaryWindow.closed == true ) {
		GlossaryWindow = openGlossaryWindow(term);
		/* if we're launching a new window, the window will call showTerm from its body.onload event */
	}
	else {
		GlossaryWindow.showTerm(term);
	}
	return false;
}

/* called by onload of popup */
function showTerm(term) {
	loaded = true;
	
	if( term == null )
		term = getTerm();

	if( lastTerm != null )
		hideElementById(lastTerm);		/* hide previous selected term */
	
	if (document.getElementById(term)){
		displayElementById(term,'block');		/* Show current selected term */
		lastTerm = term;
	}
	focus();
}

function openGlossaryWindow(term){
	return window.open('/tools/glossary_popup.jsp#'+term, 'newwinGloss', 'height=350, width=600, scrollbars=0');
}

function getTerm(){
	return location.hash.substring(1);
}




/*******************************************************************/


function displayEventsByState(oSelect) {
	var stateSelected = oSelect.value;
	var stateId = 'stateEvents_'+stateSelected;

	displayElementById(stateId,'block');

	for (var i=0; i<oSelect.length; i++) {
		if (i != oSelect.selectedIndex) {
			hideElementById('stateEvents_'+oSelect.options[i].value);
		}
	}
}


/*******************************************************************/

function printEmail(user,domain,ext) {
	document.write('<a '+'href="mail'+'to:'+user+'@'+domain+'.'+ext+'">'+user+'@'+domain+'.'+ext+'</a>');
}

function privacyWindow() {
	popWindow('/tools/privacy_popup.jsp', '630', '500', 'yes');
}







function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}




function launchVideoWin(url) {
	popWindow(url, '355', '290', 'no');
}





function URLEncode(plaintext) {
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
};

function URLDecode(encoded) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};
