/*
* copyright (c) 2006 WebService | www.webservice.gr
*/

/**
* General javascript utility methods.
*/
function wsDom()
{
	// public
	this.explicitMode = true;
	
	// private
	this._popupwindowInstance = null;
	this._getByIdSupported = document.getElementById ? true : false;
	
	this._isIE = (navigator.userAgent.indexOf('MSIE') != -1);
	this._isNN = (navigator.appName == 'Netscape');
	this._isOp = (navigator.appName == 'Opera');
	
	/**
	* Returns an element by its id.
	*
	* @param string		The element's id.
	* @return mixed
	*/
	this.getById = function (id) {
		if (this._getByIdSupported && this.isElement(id))
			return document.getElementById(id);
		return null;
	}
	
	/**
	* Returns true if an element with the id supplied exists, else false.
	*
	* @param string		The lement's id.
	* @return bool
	*/
	this.isElement = function (id) {
		if (this._getByIdSupported)
			if (document.getElementById(id))
				return true;
		return false;
	}
	
	/**
	* Changes an element's css style to the one supplied to the funtion.
	*
	* @param string 	Element's id
	* @param string 	CSS Classname
	* @return void
	*/
	this.setStyle = function (id, stylename) {
		if (this._getByIdSupported)
			this.getById(id).className = stylename;
	}

	/**
	* Hides an element.
	*
	* @param string 	Element's id
	* @return void
	*/
	this.hideElement = function (id) {
		if (this._getByIdSupported) {
			this.getById(id).style.display    = 'none';
			this.getById(id).style.visibility = 'hidden';
		}
	}

	/**
	* Shows an element.
	*
	* @param string 	Element's id
	* @return void
	*/
	this.showElement = function (id) {
		if (this._getByIdSupported) {
			this.getById(id).style.display    = 'block';
			this.getById(id).style.visibility = 'visible';
		}
	}
	
	/**
	* Toggles a div's visibility.
	*
	* @param string 	The div's id.
	* @return void
	*/
	this.toggleVisibility = function (id) {
		if (this._getByIdSupported) {
			if (this.getById(id).style.display    == 'none' ||
				this.getById(id).style.visibility == 'hidden')
			{
				this.showElement(id);
			} else {
				this.hideElement(id);
			}
		}
	}
	
	/**
	* Popups a win with predefined dimensions and loads 'url' into the body.
	*
	* @param string 	The URL to load into the window
	* @param int 		The width of the popup
	* @param int 		The height of the popup
	* @param object		The properties of the window
	* @param string 	The name of the window
	* @return boolean
	*/
	this.popupwin = function (url, width, height, parameters, name) {
		
		if (url == null)
			return false;
		
		width  		= (width != null 		? width  		: 250);
		height 		= (height != null 		? height 		: 350);
		name   		= (name != null 		? name   		: 'bpopup');
		parameters 	= (parameters != null) 	? parameters 	: new Object();
		
		width  += 1;
		height += 6;
		
		var _params = new Object();
		
		_params.width 		= width;
		_params.height 		= height;
		_params.left 		= (parameters.left != null) 		? parameters.left 			: 200;
		_params.top 		= (parameters.top != null) 			? parameters.top 			: 100;
		_params.screenX 	= (parameters.screenX != null) 		? parameters.screenX 		: 0;
		_params.screenY 	= (parameters.screenY != null) 		? parameters.screenY 		: 0;
		_params.menubar 	= (parameters.menubar != null) 		? parameters.menubar 		: 'no';
		_params.location 	= (parameters.location != null) 	? parameters.location 		: 'no';
		_params.toolbar 	= (parameters.toolbar != null) 		? parameters.toolbar 		: 'no';
		_params.scrollbar 	= (parameters.scrollbar != null) 	? parameters.scrollbar 		: 'no';
		_params.status 		= (parameters.status != null) 		? parameters.status 		: 'no';
		_params.resizable 	= (parameters.resizable != null) 	? parameters.resizable 		: 'yes';
		_params.directories = (parameters.directories != null) 	? parameters.directories 	: 'no';
		_params.copyhistory = (parameters.copyhistory != null) 	? parameters.copyhistory 	: 'yes';
		
		var _parameters = '';
		for (var i in _params)
			_parameters += ', ' + i + '=' + _params[i];
		
		this.closePopupwin();
		this._popupwindowInstance = window.open(
			url,
			name,
			_parameters.substr(2, _parameters.length)
		);
		
		if (window.focus)
			this._popupwindowInstance.focus();
		
		return false;
	}
	
	/**
	* Closes the window that popped up by the popupwin() method.
	*
	* @return void
	*/
	this.closePopupwin = function () {
		if (this._popupwindowInstance) {
			if (!this._popupwindowInstance.closed) {
				this._popupwindowInstance.close();
				this._popupwindowInstance = null;
			}
		}
	}
	
	/**
	* Displays an alert box with 'Ok' and 'Cancel' buttons for the msg argument.
	*
	* Returns the true if the user clicks on 'Ok', and false if he clicks on 'Cancel'.
	*
	* @param string		The message to display to the user
	* @return bool
	*/
	this.verify = function (msg) {
		if (this.explicitMode)
			return confirm(msg);
		else
			return true;
	}
	
	/**
	* Displays an alert box with a one line textfield, an 'Ok' and a 'Cancel'
	* button for the msg argument.
	*
	* Returns the string the user has input in the textfield if he clicks on
	* 'Ok', and null if he clicks on 'Cancel'.
	*
	* @param string		The message to display to the user
	* @param string		A default string for the textfield
	* @return string
	*/
	this.prompt = function (msg, defaultValue) {
		if (this.explicitMode)
			return prompt(msg, defaultValue);
		else
			return defaultValue;
	}
	
	//------------- utilities -----------------------------------------------------------
	
	/**
	* Returns the index of a value in an Array.
	*
	* @return int
	*/
	if (!Array.indexOf) {
		Array.prototype.indexOf = function (value, offsetIndex) {
			offsetIndex = (offsetIndex == null ? 0 : offsetIndex);
			for (var i = offsetIndex; i < this.length; i++)
				if (value === this[i])
					return i;
			return -1;
		}
	}

}

//var bedom = new BendDom();

