//---------- HELPER FUNCTIONS

function disableEnterKey(ev)
{
	if(window.event)
		keyCode = window.event.keyCode;
	else
		keyCode = ev.which;

	if(keyCode == 13)
		return false;
	else
		return true;
}

function getElementAt(nodeList, n)
{
	var cnt = 0;
	for(var c=0; c<nodeList.length; c++)
		if(nodeList[c].nodeType ==1)
		{
			if(cnt == n)
				return nodeList[c];
			cnt++;
		}
}

function setSelectByValue(elmtSelect, val)
{
	for(var c=0; c<elmtSelect.options.length; c++)
	{
		if(val == elmtSelect.options[c].value)
		{
			elmtSelect.options.selectedIndex = c;
			return;
		}
	}
}

function enableSubmit(ctrlId, loadId)
{
	$(loadId).style.display = 'none';
	$(ctrlId).style.display = 'block';
}

function disableSubmit(ctrlId, loadId)
{
	$(ctrlId).style.display = 'none';
	$(loadId).style.display = 'block';
}

function arraySearch(needle, haystack)
{
	for(var c=0; c<haystack.length; c++)
	{
		if(needle == haystack[c])
			return c;
	}
	return -1;
}
function show(id)
{
	$(id).style.display='block';
}
function hide(id)
{
	$(id).style.display = 'none';
}
function toggleDisplay(id)
{
	var elmt = $(id);
	if(elmt.style.display == 'none')
	show(id);
	else
	hide(id);
}

function toggleDisplayRow(id)
{
	var elmt = $(id);
	if(elmt.style.display == 'none')
	elmt.style.display = '';
	else
	elmt.style.display = 'none';
}
function goto(url)
{
	location.href=url;
}

function getNextElement(currentElement)
{
	var x = currentElement.nextSibling;
	if(!x) return null;

	while(x.nodeType != 1)
	{
		x = x.nextSibling;
		if(!x) return null;
	}
	return x;
}


function getPreviousElement(currentElement)
{
	var x = currentElement.previousSibling;
	if(!x) return null;

	while(x.nodeType != 1)
	{
		x = x.previousSibling;
		if(!x) return null;
	}
	return x;
}

/***************************************************

	SCROLLING FUNCTIONS

***************************************************/
function scrollTop()
{
        var pos = document.body.scrollTop;

        if(pos == 0)
        {
                if(window.pageYOffset)
                        pos = window.pageYOffset;
                else
                        pos = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
        return pos;
}

function scrollBottom()
{
	var pos = scrollTop();
	var delta = window.innerHeight;

	if(window.innerHeight)
	{
		pos += window.innerHeight;
	}
	else if(document.body.parentElement.clientHeight)
	{
		pos += document.body.parentElement.clientHeight;
	}
	return pos;
}

function scrollCenter(isHorizontal)
{
	if(isHorizontal)
	{
		if(window.innerWidth)
			return window.innerWidth/2;
		else if(document.body.parentElement.clientWidth)
			return document.body.parentElement.clientWidth/2;
		return 0;
	}

	return (scrollBottom() + scrollTop())/2;
}

// containerId is the id of the element you want the player to show up in
function loadPlayer(containerId, link,autostart, loop)
{
	var autostart = (autostart) ? ' autostart="true" ' : '';
	var loop = (loop) ? ' loop="true" ' : '';
	var tpl = '<object><embed src="'+link+'" '+autostart+loop+' ></object>';
	$(containerId).innerHTML = tpl;
}
