
Cespa.Util = {
		
	/**
	 * Opens a popup window
	 *
	 * @param string href
	 * @param int width
	 * @param int height
	 * @param string name
	 * @param int top
	 * @param int left
	 * @param string scrollbars
	 * @param string toolbar
	 * @param string location
	 * @param string status
	 * @param string menubar
	 * @param string resizable
	 * @param string dependent
	 */
	linkPopup: function(href, width, height, name, top, left, scrollbars, toolbar, location, status, menubar, resizable, dependent) {
		
		// href
		if(!href) {
			href = '/';
		}
		// width
		if(!width) {
			width = 400;
		}
		// height
		if(!height) {
			height = 300;
		}
		// window name
		if(!name) {
			name = '_window';
		}
		// top
		if(!top) {
			top = (screen.width - width)/2;
		}
		// left
		if(!left) {
			left = (screen.height - height)/2;
		}
		// scrollbars
		if(!scrollbars) {
			scrollbars = 'yes';
		}
		// toolbar
		if(!toolbar) {
			toolbar = 'no';
		}
		// location
		if(!location) {
			location = 'no';
		}
		// status
		if(!status) {
			status = 'no';
		}
		// menubar
		if(!menubar) {
			menubar = 'no';
		}
		// resizable
		if(!resizable) {
			resizable = 'no';
		}
		// dependent
		if(!dependent) {
			dependent = 'yes';
		}

		settings = 'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars='+scrollbars+',toolbar='+toolbar+',location='+location+',status='+status+',menubar='+menubar+',resizable='+resizable+',dependent='+dependent;		
		var win = window.open (''+href+'', ''+name+'', settings);		
		
		if (parseInt(navigator.appVersion) >= 4) {
			win.window.focus();
		}
	},	
	
	/**
	 * Returns a cookie
	 *
	 * @param string name
	 * @return string
	 */
	getCookie: function(name) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ((!start) && (name != document.cookie.substring( 0, name.length))) {
			return null;
		}
		if (start == -1) return null;
		var end = document.cookie.indexOf(";", len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));		
	},
	
	/**
	 * Sets a cookie
	 *
	 * @param string name
	 * @param string value
	 * @param string expires
	 * @param string path
	 * @param string domain
	 * @param boolean secure
	 */
	setCookie: function(name, value, expires, path, domain, secure) {
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
	
		/*
		if the expires variable is set, make the correct
		expires time, the current script below will set
		it for x number of days, to make it for hours,
		delete * 24, for minutes, delete * 60 * 24
		*/
		if (expires) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
	
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );		
	},
	
	/**
	 * Removes a cookie
	 *
	 * @param string name
	 * @param string path
	 * @param string domain
	 */
	delCookie: function(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}		
	},	
	
	/**
 	 * Reloads a window
 	 *
 	 * @param Object win
	 */
	reload: function(win) {
		if(!win) {
			win = window;
		}
		win.location.reload();
	},

	/**
	 * Sets a refresh
	 *
	 * @param int time
	 */
	refresh: function(time) {
		setTimeout("Cespa.Util.reload()", time);
	},

	/**
	 * Preloads images
	 */
	preloadImages: function() {
		
		if(document.images){ 
			if(!document.preload) {
				document.preload = new Array();
			}
		}
		var i;
		var j = document.preload.length;		

		for(i=0; i<arguments.length; i++) {
			if (arguments[i].indexOf("#") != 0) {
				document.preload[j] = new Image();
				document.preload[j++].src = arguments[i];
			}
		}
	},
	
	/**
	 * Modifies url parameters and reloads
	 *
	 * @param string name
	 * @param string value
	 * @param string url
	 * @param boolean ret
	 * @return string
	 */
	modifyUrlParameter: function(name, value, url, ret) {		
		
		// url
		if(!url) {
			url = location.href;
		}
		url = url.replace(new RegExp(/(#.*)/), '');

		if(url.indexOf('?' + name + '=') != -1 || url.indexOf('&' + name + '=') != -1) {
			var replace = new RegExp(name + '=[^&]*');						
			url = url.replace(replace, name + '=' + escape(value));
		} else {
			if(url.indexOf('?') != -1) {
				url = url + '&' + name + '=' + escape(value);
			} else {
				url = url + '?' + name + '=' + escape(value);
			}
		}
		
		if(!ret) {
			location.href = url;
		}
		
		return url;
	},
		
	/**
	 * Append html to the innerHTML
	 * 
	 * @param string div
	 * @param string html
	 */
	appendHtml: function(div, html) {
		var e = $(div);
		e.innerHTML += html;	
	},
	
	/**
	 * Updates parent and close window	 
	 */
	updateParent: function() {
		if (window.opener && !window.opener.closed) {
			Cespa.Util.reload(window.opener);			
		}
		self.close();
	},
	
	/**
	 * FckEditor openFile
	 * 
	 * @param string url
	 */
	openFile: function(url) {
		window.top.opener.SetUrl(url);
		window.top.close();
		window.top.opener.focus();
	},
	
	/**
	 * Changes the display state
	 * 
	 * @param string element
	 */
	changeDisplay: function(element) {
		if($D.getStyle(element, 'display') == 'none') {
			$D.setStyle(element, 'display', '');	
		} else {
			$D.setStyle(element, 'display', 'none');	
		}		
	}	
};
