var dobj;
var objclone;

function calculate_height(el)
{
return;
	var total_height = 0;
	for(i=0;i<el.childNodes.length;i++)
	{
		thisnode = el.childNodes[i];
		if(thisnode.className && thisnode.className.indexOf("draggable") > -1)
		{
			total_height += parseInt(el.childNodes[i].style.height+0);
		}
	}
	el.style.height = total_height + 'px';
}

function move_element(which_el,direction)
{
	if(!document.getElementById(which_el))
	{
		return false;
	}
	which_el = document.getElementById(which_el);
	opacity(which_el.id,80,100,0);
	objclone = which_el.cloneNode(true);
	
	this_parent = which_el.parentNode;

	if(direction == "up" && which_el.previousSibling)
	{
		this_parent.insertBefore(objclone,which_el.previousSibling);
		this_parent.removeChild(which_el);
	}
	else if(direction == "down" && which_el.nextSibling)
	{
		insertAfter(objclone,which_el.nextSibling);
		this_parent.removeChild(which_el);
	}

	reposition_nodes(this_parent);

	objclone = null;
	return true;
}

function insertAfter(newElm,elm)
{
	var clone = elm.cloneNode(true);
	elm.parentNode.insertBefore(clone,elm);
	elm.parentNode.replaceChild(newElm,elm);
}

function reposition_nodes(el)
{
	thistop = 0;
	for(i=0;i<el.childNodes.length;i=i+1)
	{
		thisnode = el.childNodes[i];
		if(thisnode.className && thisnode.className.indexOf("draggable") > -1)
		{
			thisnode.style.zIndex = 1;
			thisnode.style.position = 'absolute';
			thisnode.style.top = thistop+'px';
			thistop += parseInt(thisnode.style.height+0);	
		}
	}
	calculate_height(el);
}

function attempt_external_login()
{
	if(typeof window.external.login_all == 'function')
	{
		window.external.login_all();
	}
}

function keypressed(e,element)
{
	if(e.keyCode == 13)
	{	
		if(element)
		{
			element.submit();
			return false;
		}
	}
}

function changeOpac(opacity, id)
{
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function opacity(id, opacStart, opacEnd, millisec)
{
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function hide_header()
{
	document.getElementById('header').style.display = "none";
	document.getElementById('show_header').style.display = "block";
}

function show_header()
{
	document.getElementById('header').style.display = "block";
	document.getElementById('show_header').style.display = "none";
}

myvars = [];
function send_msg(msg_id, msg_contents)
{
	if(myvars[msg_id] == undefined)
	{
		myvars[msg_id]=document.createElement('div');
		myvars[msg_id].className = 'msg';
		myvars[msg_id].setAttribute('id', msg_id);
		document.getElementById('msgs').appendChild(myvars[msg_id]);
		myvars[msg_id].innerHTML=msg_contents;
	}
}

//Enter "frombottom" or "fromtop"
var verticalpos="fromtop"

function JSFX_FloatTopDiv()
{
	var startX = 0,
	startY = 0;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
	function ml(id)
	{
		var el=document.getElementById(id);
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		el.x = startX;
		if (verticalpos=="fromtop")
		el.y = startY;
		else{
		el.y = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
		el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft=function()
	{
			if (verticalpos=="fromtop"){
			var pY = ns ? pageYOffset : document.body.scrollTop;
			ftlObj.y += Math.round((pY + startY - ftlObj.y)/8);
			}
			else{
			var pY = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
			ftlObj.y += (pY - startY - ftlObj.y)/8;
			}
			//window.alert(ftlObj.y);
			ftlObj.sP(ftlObj.x, ftlObj.y);

		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml("divStayTopLeft");
	stayTopLeft();
}

function destroy_tab(destroy_target)
{
	if(document.getElementById(destroy_target) !== undefined)
	{
		document.getElementById('max_media_container').removeChild(document.getElementById(destroy_target));
	}

	if(document.getElementById('tab_' + destroy_target) !== undefined)
	{
		document.getElementById('max_media_tabs').removeChild(document.getElementById('tab_' + destroy_target));
	}

	myRequest.queuePost('destroy_tab', 'destroy_tab=' + destroy_target, '');
	var container = document.getElementById('max_media_container');
	var display_next = container.childNodes.length;
	display_next--;
	if(display_next > -1)
	{
		container.childNodes[display_next].style.display = "block";
		var tab_url;

		if(container.childNodes[display_next].getAttribute('url') != undefined && container.childNodes[display_next].getAttribute('url') != '')
		{
			tab_url = container.childNodes[display_next].getAttribute('url');
			//window.alert('tab' + tab_url + tab_name + destroy_target);
		}
		else
		{
			//window.alert('oops');
			tab_url = container.childNodes[display_next].id;
		}

		var tab_controls;
		var tab_refresh_click;
		tab_refresh_click = 'myRequest.queuePost(\'' + tab_url  + '\', \'' + container.childNodes[display_next].getAttribute('post_data') + '\', \'' + container.childNodes[display_next].id + '\')';
		tab_controls = "<img src=\"/ext/images/close_button.gif\" style=\"cursor:pointer;\" onClick=\"destroy_tab('"+container.childNodes[display_next].id+"');\" />";
		tab_controls += " &nbsp; ";
		tab_controls += "<img src=\"/ext/images/refresh_button.gif\" style=\"cursor:pointer;\" onClick=\""+tab_refresh_click+"\" />";
		document.getElementById('tab_' + container.childNodes[display_next].id).style.backgroundColor = '#FFFFFF';
		document.getElementById('tabcontrols').innerHTML = tab_controls;
	}
	else
	{
		document.getElementById('tabcontrols').innerHTML = "";
	}
}

function show_hide(div, auto)
{
	var element = document.getElementById(div);
	if (element.style.display == "none")
	{
		element.style.display = 'block';
	}
	else
	{
		element.style.display = 'none';
	}

	if(auto != undefined && auto > 0)
	{
		setTimeout("show_hide('"+ div +"')", auto);
	}

return false;
}

function show(div)
{
	var element = document.getElementById(div);
	element.style.display = 'block';
	return false;
}

function hide(div)
{
	var element = document.getElementById(div);
	element.style.display = 'none';
	return false;
}

function change_date($d,$m,$y)
{
	if (document.pr_search)
	{
	document.pr_search.day.selectedIndex = $d-1;
	document.pr_search.month.selectedIndex = $m-1;
	document.pr_search.year.selectedIndex = $y-1;
	}
}

function check_boxes(controller,theElements) {
//Programmed by Shawn Olson
//Copyright (c) 2006
//Permission to use this function provided that it always includes this credit text
//  http://www.shawnolson.net
//Find more JavaScripts at http://www.shawnolson.net/topics/Javascript/


//theElements is an array of objects designated as a comma separated list of their IDs
//If an element in theElements is not a checkbox, then it is assumed
//that the function is recursive for that object and will check/uncheck
//all checkboxes contained in that element


var formElements = theElements.split(',');
var theController = document.getElementById(controller);

	for(var z=0; z<formElements.length;z++)
	{

	theItem = document.getElementById(formElements[z]);

		if(theItem)
		{
			if(theItem.type)
			{
				if(theItem.type == 'checkbox' && theItem.id != theController.id)
				{
					theItem.checked = theController.checked;
				}

			}
			else
			{

				var nextArray = '';

				for(var x=0;x <theItem.childNodes.length;x++)
				{
					if(theItem.childNodes[x])
					{
						if (theItem.childNodes[x].id)
						{
							nextArray += theItem.childNodes[x].id+',';
						}
					}
				}

			checkUncheckSome(controller,nextArray);

			}

		}
	}
}

function noNumbers(e,element)
{
	//usage: <input type="text" onkeypress="return noNumbers(event)" />
	var keynum;
	var keychar;
	var numcheck;
	keynum = e.keyCode;
	if(keynum == 13)
	{
		onKeyPress(e,element);
	}
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return !numcheck.test(keychar);
}

function capWords(inputString)
{
	var outputString = ""; // The output text field
	var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = inputString.toLowerCase();
	stringLen = tmpStr.length;

	if (stringLen > 0)
	{
		for (i = 0; i < stringLen; i++)
		{
			if (i == 0)
			{
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,stringLen);
				tmpStr = tmpChar + postString;
			}
			else
			{
				tmpChar = tmpStr.substring(i,i+1);
				if (tmpChar == " " && i < (stringLen-1))
				{
				tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
				preString = tmpStr.substring(0,i+1);
				postString = tmpStr.substring(i+2,stringLen);
				tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}
outputString = tmpStr;
return outputString;
}

function select_element(select_el,target_el)
{
	// find and display target
	if(document.getElementById(target_el))
	{
		target_parent_el = document.getElementById(target_el).parentNode;
		for(i=0;i<target_parent_el.childNodes.length;i++)
		{
			this_el = target_parent_el.childNodes[i];

			if(this_el.id == target_el)
			{
				this_el.style.display = "block";
			}
			else
			{
				this_el.style.display = "none";
			}
		}
	}
	
	// highlight this trigger, normalize others
	parent_el = document.getElementById(select_el).parentNode;
	for(x=0;x<parent_el.childNodes.length;x++)
	{
		this_el = parent_el.childNodes[x];
		if(this_el.id == select_el)
		{
			opacity(this_el.id,100,60,40);
		}
		else
		{
			opacity(this_el.id,60,100,40);
		}
	}	
}	

