/*
* copyright (c) 2006 WebService | www.webservice.gr
*/

/**
* General javascript utility methods.
*/
function wsDom()
{
	// private
	
	this._isIE = (navigator.userAgent.indexOf('MSIE') != -1);
	this._isNN = (navigator.appName == 'Netscape');
	this._isOp = (navigator.appName == 'Opera');
	
	this._warningsEnabled = true;
	this._popupwindowInstance = null;
	
	/**
	* Enables warnings for actions
	*
	* @return void
	*/
	this.enableWarnings = function () {
		this._warningsEnabled = true;
	}
	
	/**
	* Disables warnings for actions
	*
	* @return void
	*/
	this.disableWarnings = function () {
		this._warningsEnabled = false;
	}
	
	/**
	* Returns true if an element with the id supplied exists, else false.
	*
	* @param string		The element's id.
	* @return bool
	*/
	this.isElement = function (idOrObj) {
		var rs = false;
		
		if (idOrObj == null)
			return false;
		
		if (typeof idOrObj == 'string') {
			if (document.getElementById)
				if (document.getElementById(idOrObj))
					rs = true;
			else if (document.all)
				if (document.all[idOrObj])
					rs = true;
			else if (document.layers)
				if (__getByIdNN4(document, idOrObj))
					rs = true;
		} else if ((idOrObj && typeof idOrObj == 'object') || 
				   (typeof idOrObj == 'function')) {
			rs = true;
		}
		
		return rs;
	}
	
	/**
	* Returns an element by its id.
	*
	* @param string		The element's id.
	* @return mixed
	*/
	this.getById = function (id) {
		if (!this.isElement(id))
			return null;
		
		var obj = null;
		
		if (document.getElementById) {
			obj = document.getElementById(id);
			//obj.style = document.getElementById(id).style;
		} else if (document.all) {
			obj = document.all[id];
			obj.style = document.all[id].style;
		} else if (document.layers) {
			obj = __getByIdNN4(document, id);
			obj.style = document.layers[id];
		}
		
		return obj;
	}
	
	/**
	* @access private
	*/
	this.__getByIdNN4 = function (obj, name) {
		var x = obj.layers;
		var foundLayer;
		for (var i = 0; i < x.length; i++) {
			if (x[i].id == name)
				foundLayer = x[i];
			else if (x[i].layers.length)
				var tmp = getObjNN4(x[i], name);
			if (tmp)
				foundLayer = tmp;
		}
		return foundLayer;
	}
	
	/**
	* Returns an element either by its id or self.
	*
	* @param string|Object 	The element or its id
	* @return mixed
	*/
	this.getObject = function (idOrObj) {
		if (idOrObj == null)
			return null;
		
		var obj = null;
		
		if (typeof idOrObj == 'string')
			obj = this.getById(idOrObj);
		else if ((idOrObj && idOrObj.id && typeof idOrObj == 'object') ||
				 (typeof idOrObj == 'function'))
			obj = this.getById(idOrObj.id);
		
		return obj;
	}

	/**
	* Hides an element.
	*
	* @param string|Object 	The element or its id
	* @return void
	*/
	this.hideElement = function (idOrObj) {
		var obj = this.getObject(idOrObj);
		
		if (obj != null) {
			obj.style.display    = 'none';
			obj.style.visibility = 'hidden';
		}
	}

	/**
	* Shows an element.
	*
	* @param string|Object 	The element or its id
	* @return void
	*/
	this.showElement = function (idOrObj) {
		var obj = this.getObject(idOrObj);
		
		if (obj != null) {
			obj.style.display    = 'block';
			obj.style.visibility = 'visible';
		}
	}
	
	/**
	* Toggles an element's visibility.
	*
	* @param string|Object 	The element or its id
	* @return void
	*/
	this.toggleVisibility = function (idOrObj) {
		var obj = this.getObject(idOrObj);
		
		if (obj != null) {
			if (obj.style.display    == 'none' ||
				obj.style.visibility == 'hidden')
				this.showElement(obj);
			else
				this.hideElement(obj);
		}
	}
	
	/**
	* Changes an element's css style to the one supplied to the function.
	*
	* @param string|Object 	The element or its id
	* @param string 		CSS Classname
	* @return void
	*/
	this.setStyle = function (idOrObj, stylename) {
		var obj = this.getObject(idOrObj);
		
		if (obj != null)
			obj.className = stylename;
	}
	
	/**
	* Sets focus to the supplied element
	*
	* @param string|Object 	The element or its id
	* @return void
	*/
	this.setFocus = function (idOrObj) {
		var obj = this.getObject(idOrObj);
		
		if (obj != null)
			obj.focus();
	}
	
	/**
	* Checks if an input field's value is empty and throws an error.
	*
	* @param string
	* @param mixed
	* @param string
	* @return true
	*/
	this.isEmpty = function (inputId, defaultValue, errorId) {
		var obj = this.getObject(inputId);
		if (obj.value == "" || obj.value == defaultValue) {
			if (errorId != null) {
				this.showElement(errorId);
				this.setFocus(obj);
			}
			return true;
		}
		return false;
	}
	
	//------------- popups -----------------------------------------------------------
	
	/**
	* 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;
		
		var  params = new Object();
		var _params = new Object();
		
		width  = (width		 != null ? width	  : 250);
		height = (height	 != null ? height	  : 350);
		name   = (name		 != null ? name		  : 'bpopup');
		params = (parameters != null ? parameters : params);
		
		width  += 1;
		height += 6;
		
		_params.width 		= width;
		_params.height 		= height;
		_params.left 		= (params.left != null)		   ? params.left 		: 200;
		_params.top 		= (params.top != null)		   ? params.top 		: 100;
		_params.screenX 	= (params.screenX != null)	   ? params.screenX 	: 0;
		_params.screenY 	= (params.screenY != null)	   ? params.screenY 	: 0;
		_params.menubar 	= (params.menubar != null) 	   ? params.menubar 	: 'no';
		_params.location 	= (params.location != null)    ? params.location 	: 'no';
		_params.toolbar 	= (params.toolbar != null) 	   ? params.toolbar 	: 'no';
		_params.scrollbar 	= (params.scrollbar != null)   ? params.scrollbar 	: 'no';
		_params.status 		= (params.status != null) 	   ? params.status 		: 'no';
		_params.resizable 	= (params.resizable != null)   ? params.resizable 	: 'yes';
		_params.directories = (params.directories != null) ? params.directories : 'no';
		_params.copyhistory = (params.copyhistory != null) ? params.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;
			}
		}
	}
	
	//------------- alerts -----------------------------------------------------------
	
	/**
	* 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._warningsEnabled)
			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._warningsEnabled)
			return prompt(msg, defaultValue);
		else
			return defaultValue;
	}
	
	//------------- cookies -----------------------------------------------------------
	
	/**
	* Creates a cookie.
	*
	* @param string
	* @param string
	* @param int
	* @return void
	*/
	this.createCookie = function (name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*3600*1000));
			var expires = "; expires=" + date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	}
	
	/**
	* Reads a cookie.
	*
	* @param string
	* @return string
	*/
	this.readCookie = function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ')
				c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length, c.length);
		}
		return null;
	}
	
	/**
	* Erases a cookie.
	*
	* @param string
	* @return void
	*/
	this.eraseCookie = function (name) {
		createCookie(name, "", -1);
	}
	
	//------------- utilities -----------------------------------------------------------
	
	/**
	* Creates the indexOf function to the Array class.
	*/
	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;
		}
	}
}

