/**** COMMON DHTML FUNCTIONS (Cross-Browser) */
 
function DHTML()
{
}

DHTML.addEvent = function (obj, eventName, callback)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(eventName, callback, false);
	}
	else if (obj.attachEvent)
	{
		obj.attachEvent('on' + eventName, callback);
	}
	else
	{
		obj['on' + eventName] = callback;
	}
}

DHTML.removeEvent = function(obj, eventName, callback, useCapture)
{
	if (obj.removeEventListener)
	{
		obj.removeEventListener(eventName, callback, useCapture);
	}
	else if (obj.detachEvent)
	{
		obj.detachEvent('on' + eventName, callback);
	}
	else
	{
		obj['on' + eventName] = null;
	}
}

/**
 * Gets the full width/height because it's different for most browsers.
 * getViewportHeight and getViewportWidth by http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 */
DHTML.getViewportHeight = function() {
	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; 
}
DHTML.getViewportWidth = function() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}

/** Other
 */
 
DHTML.removeClass = function(obj, className)
{
	if (!(obj && obj.className)) {
		return;
	}
	var classes = obj.className.split(' ');
	var newClasses = new Array();
	for (var i = classes.length; i > 0;) {
		if (classes[--i] != className) {
			newClasses[newClasses.length] = classes[i];
		}
	}
	obj.className = newClasses.join(' ');
};

DHTML.addClass = function(obj, className)
{
	DHTML.removeClass(obj, className); //make sure it isn't a duplicate
	obj.className += ' ' + className;
};

DHTML.escape = function(inputString)
{
	try
	{
		return encodeURIComponent(inputString);
	}
	catch(err)
	{
	}
	
	return escape(inputString);
};


DHTML.selectToCSV = function(selectElement)
{
	var selectedOptions = '';
	
	for (i = 0; i < selectElement.options.length; i++)
	{
		if (selectElement.options[i].selected)
		{
			if (selectedOptions.length > 0)
				selectedOptions = selectedOptions + ',' + selectElement.options[i].value;
			else
				selectedOptions = selectElement.options[i].value;
		}	
	}
	
	return selectedOptions;
};

DHTML.WindowFeatures = function()
{
	this.menubar = 'yes';
	this.toolbar = 'yes';
	this.location = 'yes';
	this.status = 'yes';
	this.resizable = 'yes';
	this.scrollbars = 'yes';
	this.modal = 'no';
};

DHTML.WindowFeatures.prototype = 
{
	apply: function(features)
	{
		for (var feature in features)
		{
			this[feature] = features[feature];
		}
	},
	
	toString: function()
	{
		var retval = '';
		
		for (var feature in this)
		{
			retval = retval + feature + '=' + this[feature] + ',';
		}
		
		return retval;
	}
};

DHTML.openWindow = function(url, name, features)
{
	if (features)
	{
		//merge features with the default
		var windowFeatures = new DHTML.WindowFeatures();	
		windowFeatures.apply(features); //apply the requested features to the defaults
		
		var featuresString = windowFeatures.toString();

		return window.open(url, name, featuresString);
	}
	else
	{
		return window.open(url, name);
	}
};

