

/*
 * 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('');

	$(elName).appendChild(document.createTextNode(' '));
	$(elName).appendChild(Builder.build('<img src="' + templateUrl + '/images/spinner.gif" alt="loading..." id="loading' + elName + '" />'));
}function hideLoading(elName){ $(elName).removeChild($('loading' + elName)); }


function openPopUp(el, width, height, appendix) 
{
	if(!appendix) appendix = 'resizable=yes,toolbar=yes,location=no,status=yes';
	if(!width) width = '600';
	if(!height) height = '500';

	window.open(el.href,'','scrollbars=yes,menubar=no,height=' + height + ',width=' + width + ',' + appendix);
}


/*
 * A fix for focusFirstElement on IE
 */
function focusFirst(formName) {
	window.setTimeout( function() { $(formName).focusFirstElement(); }, 10 );
}




