var url_root = '';


var Engine = {
	load: function(root_path, scriptal, scripts) {
		url_root = root_path;
		if( typeof Prototype == 'undefined' ) {
			throw('Error: Prototype required.');
		} else {
			if( scriptal.match(/[^\s]/) )
			{
				document.write("<script src=\""+root_path+"engine/jscripts/scriptaculous/scriptaculous.js?load="+scriptal+"\" type=\"text/javascript\"></script>");
			}
			scripts.split(',').each( function(sinc) {
				if( sinc.match(/[^\s]/) )
				{
					document.write("<script src=\""+sinc+"\" type=\"text/javascript\"></script>");
				}
			} );
		}
	},
	
	jaxcommand : function( jcommand, args, params )
	{
		var url = url_root + 'request/' + jcommand;
		for( var i=0; i<args.length; i++ )
		{
			url += '/' + args[i];
		}
		
		new Ajax.Request(url,
		  {
		    method:'post',
			parameters:params,
		    onSuccess: function(transport){
		      var response = transport.responseText;
			  eval("__jxevt_"+jcommand+"(\""+escape(response)+"\");");
		    },
		    onFailure: function(){ eval("__jxevt_"+jcommand+"(0);"); }
		  });
	},
	
	disable_select: function (target) {
		var target = $(target);
		if (typeof target.onselectstart != "undefined")
		{
			target.onselectstart = function() { return false }
		}
		else if ( typeof target.style.MozUserSelect != "undefined" )
		{
			target.style.MozUserSelect = "none";
		}
		else
		{
			target.onmousedown=function() { return false }
		}
		target.style.cursor = "default";
	},
	
	init_grey_box: function( box ) {
		Element.extend(box);
		if( box.hasClassName('grey_box_start') )
		{
			box.removeClassName('grey_box_start');
			box.value = '';
		}
	},
	
	delete_item_confirm: function( item, url ) {
		if( confirm("Are you sure you want to delete " + item + "?") )
		{
			document.location.href=url;
		}
	},
	
	highlight_delete_confirm: function( objid, url ) {
		$(objid).addClassName('cms_delete_highlight');
		var yesorno = confirm("Are you sure you want to delete this?");
		$(objid).removeClassName('cms_delete_highlight');
		if( yesorno )
		{
			if( url == '' || typeof url == 'undefined' )
			{
				document.location.reload();
			}
			else
			{
				document.location.href=url;
			}
		}
	},
	
	apply_wysiwyg: function( sEditorID )
	{
		var oFCKeditor = new FCKeditor( sEditorID );
		oFCKeditor.BasePath	= url_root + 'engine/jscripts/fckeditor/';
		oFCKeditor.ToolbarSet = 'Default';
		oFCKeditor.ReplaceTextarea();
	},
	
	txtStart : function(obj)
	{		
		Engine.txtHighlight(obj, 0, 0);
	},
	
	txtSelect : function(obj, txt)
	{
		var start = obj.value.toLowerCase().search(txt.toLowerCase());
		var len = txt.length;
		Engine.txtHighlight(obj, start, len);
	},
	
	txtHighlight : function(obj, start, len)
	{		
		if( obj.createTextRange )
		{
			obj.focus();
			var objr = obj.createTextRange();
			objr.moveStart("character", start);
			objr.moveEnd("character", (start+len) - obj.value.length);
			objr.select();
			return true;
		}
		else if( obj.setSelectionRange )
		{
			obj.setSelectionRange(start,start+len);
			obj.focus();
			return true;
		}
		else
		{
			return false;
		}
	},
	
	daysInMonth : function() {
		return 32 - new Date(iYear, iMonth, 32).getDate();
	},
	
	numbersOnly : function(e, dec, dash) {
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if( ! (code < 48 || code > 57) || (code >= 37 && code <= 40) 
		|| code == 8 || code == 9 || code == 13 
		|| (dec == true && code == 46) || (dash == true && code == 45) ) {
			return true;
		} else {
			return false;
		}
	}
}