/**
 * Convenience method for arrays...
 */
Array.prototype.swap = function(a, b) {
	var tmp = this[a];
	this[a] = this[b];
	this[b] = tmp;
}

/**
 * A global default method that maps directly to document.getElementById
 */
function $(strElementId) {
	return document.getElementById(strElementId);
}

/** 
 * Util Class 
 * This general util class provides some help DOM function. Some of these function are very useful
 * when using with YUI getChildrenBy DOM method.
 * 		clearAllChildren - This function will remove all child element from the parent object
 *		isOptionSelected - This is a function that will return true if the passed in option
 *						   DOM element is selected.
 * 		markUnselected   - This function will take in an option DOM element and mark it
 *						   unselected. Always return false.
 */
var GSUtil = new Object();
GSUtil.clearAllChildren = function(elmTarget) {
	while(elmTarget.firstChild) {
		// Ensure we detach all event listener before removing the node to prevent memory leak.
		// This only detach all event listener added via the YAHOO.util.Event.addListener method.
		YAHOO.util.Event.purgeElement(elmTarget.firstChild, true);
		elmTarget.removeChild(elmTarget.firstChild);
	}											
}

/**
 * This set text content method will only work with textarea, input, span and div tag.
 * In the textarea and input case, this will use the .value attribute and put the text into the
 * element. In the span and div class, it will attempt to append the text as a text node into the
 * element. As a result of the insert, it will first remove all child from the target element 
 * before the appending.
 */
GSUtil.setTextContent = function(elmTarget, text) {
	if(elmTarget.tagName == 'TEXTAREA' || elmTarget.tagName == 'INPUT') {
		elmTarget.value = text;
	}
	else if(elmTarget.tagName == 'SPAN' || elmTarget.tagName == 'DIV') {
		GSUtil.clearAllChildren(elmTarget);
		elmTarget.appendChild(document.createTextNode(text));
	}
}

// Test to see if an element is selected
GSUtil.isOptionSelected = function(elmOption) {
	if(elmOption.selected) {
		return true;
	}
	return false;
}

// Mark an option element as unselected
GSUtil.markUnselected = function(elmOption) {
	if(elmOption.selected) {
		elmOption.selected = false;
	}
	
	return false;
}

// Hide an element.
GSUtil.hideElement = function(elmTarget) {
	if(typeof(elmTarget) != 'undefined' && elmTarget != null) {
		elmTarget.style.display = 'none';
	}	
}

// Show an element.
GSUtil.showElement = function(elmTarget) {
	if(typeof(elmTarget) != 'undefined' && elmTarget != null) {
		elmTarget.style.display = 'block';
	}
}

// Trim text
GSUtil.trim = function(textToTrim) {
	return textToTrim.replace(/^\s+|\s+$/g, '');
}

/**
 * Look up in the global Messages object for a particular message
 */
GSUtil.getMessage = function(msgKey, attributes) {
    if(typeof(Messages) != 'undefined' && typeof(Messages[msgKey]) != 'undefined') {
        var outputMessage = Messages[msgKey];
        if(typeof(attributes) != 'undefined') {
            for(var i = 0; i < attributes.length; i++) {
                var index = i + 1;
                outputMessage = outputMessage.replace('%' + index, attributes[i]);
            }
        }
        return outputMessage;
    }
    else {
        return 'Message not found in resources!';
    }
}

// Refresh the page.
GSUtil.refreshPage = function() {
    window.location = window.location;
}

/**
 * Takes key-value pairs and concatenates them together in a single
 * string that is of the form:
 *
 * [key]=[value]&...
 *
 * For example, GSUtils.argify('myName', myName) where myName = 'Dave' would
 * return myName=Dave
 */
GSUtil.argify = function() {
	var argStr = '';
	try {
		if((arguments.length % 2) != 0) {
			throw new GSException('IllegalArgumentException', 
			  	'GSUtil.argify must be called with an even number of arguments.');
		}
								
		for(var i = 0; i < arguments.length; i+=2) {
			argStr += arguments[i] + '=' + arguments[i+1];
			if(i < arguments.length - 2)
				argStr += '&';					
		}		
	} catch(e) {
		GSException.handleException(e);
	}
	
	return argStr;
}
