/**
 * Popup a new window with minimal chrome for displaying
 * terms and help dialogs.
 * 
 * Example usages:
 * <a href="javascript:void(popup('/terms.cfm'))">Terms</a>
 * <a href="javascript:void()" onClick="popup('/terms.cfm')">Terms</a>
 * <a href="/terms.cfm" onClick="return popup('/terms.cfm')">Terms</a> 
 * 
 * @method popup
 * @param url {String} The url to load into the popup window
 * @param width {Number} The width of the window in pixels. Defaults to 550
 * @param height {Number} The height of the window in pixels. Defaults to 550
 * @return {Boolean} Always returns false to stop any execution of HTMLElement
 */
function popitup( url, width, height ) {
	var newwindow = window.open(
		url,
		'defaultwin',
		[
			'height=',
			height != null ? height : 550,
			',width=',
			width != null ? width : 550,
			',resizable=yes',
			',scrollbars=yes',
		].join('')
	);
		
	if(!newwindow) {
		alert("Could not open window! You may have a popup blocker running." + "/n" + "  Please disable it to view this content.")
	}
	else {
		if (window.focus) {
			newwindow.focus()
		}
	}
	return false;
}

/**
 * Show a browser alert box with the specified confirm message
 * as the text label.
 * 
 * @method confirmDialog
 * @param text {String} The message to show in the dialog.
 * @return true if the user confirms OK.
 */
function confirmDialog( message ) {
	return confirm( message );
}

/**
 * Return the dom node for the passed string (id), or dom node
 * @param {Mixed} el
 * @return HTMLElement
 */
function getDom(el) {
	if(!el || !document){
		return null;
	}
	return typeof el == 'string' ? document.getElementById(el) : el;
}

/**
 * Returns an array of HTMLElements which have a matching
 * class name
 * 
 * @param clsName {String} The class name to find element by
 * @return {Array} An array of HTMLElements
 */
function getElementsByClassName(clsName){
	var retVal = new Array();
	var elements = document.getElementsByTagName("*");
	for(var i = 0;i < elements.length;i++){
    	if(elements[i].className.indexOf(" ") >= 0){
        	var classes = elements[i].className.split(" ");
        	for(var j = 0;j < classes.length;j++){
            	if(classes[j] == clsName)
                	retVal.push(elements[i]);
        	}
    	}
    	else if(elements[i].className == clsName)
       		retVal.push(elements[i]);
	}
 	return retVal;
}

function removeClass(el, clsName) {
	var newVal = new Array();
	var el = getDom(el);
	if(el == null)
		return;
	
	if(el.className.indexOf(" ") >= 0){
    	var classes = el.className.split(" ");
    	for(var j = 0;j < classes.length;j++){
        	if(classes[j] != clsName)
        		newVal.push(classes[j]);
    	}
    	el.className = newVal.join('');
	}
}

function addClass(el, clsName) {
	el.className = el.className + " " + clsName ;
}

/**
 * Sets a HTML element to be hidden/displayed.
 * 
 * @param el {HTMLElement} The element to show/hide
 * @param flag {Boolean} True to show the element, false to hide.
 * @return false
 */
function setVisible(el, flag) {
	if(el == null)
		return;
		
	el = getDom(el);
	
	if(el != null)
		if(el.style)
			el.style.display = flag == true ? '' : 'none' ;
}