

	/*
	 * what to do when input is focused
	 */
	function inputFocus(el, zeroState)
	{
		//alert($F(el));
		if($F(el) == zeroState)
			el.value = '';
	}

	/*
	 * what to do when input is blured
	 */
	function inputBlur(el, zeroState)
	{
		if($F(el) == '')
			el.value = zeroState;
	}

	/*
	 * Search back in the node tree for the next instance of the specified element
	 */
	function findNodeBehindByTagName(el, tag)
	{
		tag = tag.toUpperCase();
		if(el.parentNode.tagName == tag)
			return el.parentNode;
		else
			return findNodeBehindByTagName(el.parentNode, tag);
	}

	/*
	 *  multi use
	 */
	function newEl(type)     { return document.createElement(type); }
	function newTxt(content) { return document.createTextNode(content); }
	function getByTag(tag) { return document.getElementsByTagName(tag); }


	function newInput(type, name, id, value)
	{
		var i   = newEl('input');
		i.type  = type;
		if(name)  i.name  = name;
		if(id)    i.id    = id;
		if(value) i.value = value;
		return i;
	}

	/*
	 *  multi use
	 */
	function newPage(id, containerToAppend)
	{

		if(typeof(containerToAppend) == 'string')
			containerToAppend = $(containerToAppend);
	
		var title       = newEl('input');
		title.type      = 'text';
		title.name      = 'pages[' + id + '][title]';
		title.className = 'text';
		title.value     = 'Title';

		if(id == 0)
		{
			title.id       = 'newpageTitle';
		}
		var div = newEl('span');
		div.id  = 'page' + id;
	
		div.appendChild(title);
		containerToAppend.appendChild(div);
		window.setTimeout(function() { title.focus(); }, 1);
	}


	function showLoading(elName, destroyContainerBefore)
	{
		if(typeof(destroyContainerBefore) != 'undefined' && destroyContainerBefore == true)
			$(elName).update('');

		// if(typeof(greyBkg) == 'undefined' || !greyBkg) 
			$(elName).appendChild(Builder.build('<img src="' + sysUrl + 'template/images/spinner.gif" alt="loading..." id="loading' + elName + '" />'));
		// else
		// 	$(elName).appendChild(Builder.build('<img src="' + sysUrl + 'template/images/spinner-grey.gif" alt="loading..." id="loading' + elName + '" />'));
			
		$(elName).appendChild(document.createTextNode(' '));
	}function hideLoading(elName){ $(elName).removeChild($('loading' + elName)); }


	function CreateInPlaceEditor(editableContainer, multiLine, table, field, id, inputFieldsOnReturn)
	{
		Ajax.InPlaceEditor.defaultHighlightColor = '#f1f1f1';
		if(!multiLine)
		{
			if(field == 'title')
				var r = new Ajax.InPlaceEditor(editableContainer, ajaxUrl + '?ajaxAction=saveInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id, {okText:'Save', onComplete: function() { $('jumpchartNav').innerHTML = ''; showLoading('jumpchartNav'); new Ajax.Updater('jumpchartNav', ajaxUrl + '?ajaxAction=buildNavbar&projectId=' + $F('pageProjectId'));}});
			else
				var r = new Ajax.InPlaceEditor(editableContainer, ajaxUrl + '?ajaxAction=saveInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id, {okText:'Save'});
		}
		else
		{
			// alert(inputFieldsOnReturn);
			if(!inputFieldsOnReturn)
				var inputFieldsOnReturn = 0;
			else
				var inputFieldsOnReturn = 1;
		
			var height   = Element.getHeight(editableContainer);
			var colsRows = Math.round(height/14)+3;
			if(colsRows < 10)
				colsRows = 10;
		
			var r = new Ajax.InPlaceEditor(editableContainer, ajaxUrl + '?ajaxAction=saveInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id + '&multiLine' + '&inputFieldsOnReturn=' + inputFieldsOnReturn, {okText:'Save', rows:colsRows, cols:70, loadTextURL: ajaxUrl + '?ajaxAction=loadTextForInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id});
		}

		return r;
	}


	function Down(el)  { new Effect.SlideDown(el,{duration:0.4}); }
	function Up(el) { new Effect.SlideUp(el,{duration:0.4});}
	function Shake(el) { new Effect.Shake(el); }
	function Scroll(el) { new Effect.ScrollTo(el, {offset:-10});}
	function Fade(el) { new Effect.Fade(el, {duration:0.4});}


	function openPopUp(el, width, height, appendix) 
	{
		if(!appendix) appendix = 'resizable=yes,toolbar=no,location=no,status=no';
		if(!width) width = '600';
		if(!height) height = '500';

		window.open(el.href,'','scrollbars=yes,menubar=no,height=' + height + ',width=' + width + ',' + appendix);
	}
	
	function px(str)
	{
		var pxStr = str.split('px');
		if(pxStr)
			return pxStr[0]*1;
		else
			return str;
	}


	/*
	 * A fix for focusFirstElement on IE
	 */
	function focusFirst(formName) {
		window.setTimeout( function() { 
			if($(formName) && $(formName).findFirstElement())
				$(formName).focusFirstElement(); 
		}, 10 );
	}

	function pageChangeConfirmationOn() 
	{
		if(!Prototype.Browser.IE)
			window.onbeforeunload = function() { return "You're trying to leave a page that could have unsaved content.";};
	}
	
	function pageChangeConfirmationOff() 
	{
		if(!Prototype.Browser.IE)
			window.onbeforeunload = function() { };
	}
	
	
	
