/*****************************************************************************
 *
 * Contains functions commonly used by the other scripts
 * 
 *****************************************************************************/


/*
 * Find the X position of any object
 */
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

/*
 * Find the Y position fo any object
 */
function findPosY(obj)
{
        var curtop = 0;
        if (obj.offsetParent)
        {
                while (obj.offsetParent)
                {
                        curtop += obj.offsetTop
                        obj = obj.offsetParent;
                }
        }
        else if (obj.y)
                curtop += obj.y;
        return curtop;
}

/*
 * Place and show a Popup window at the bottom left corner of a menu_bar object
 */
function placePopup(item, menu)
{
	var pm = document.getElementById(menu);
	var mi = document.getElementById(item);
	mi.style.left=(findPosX(pm)+2)+'px';
	mi.style.top=(findPosY(pm)+pm.offsetHeight+2)+'px';
	mi.style.visibility='visible';
	
	popup_hidden=false;
	//alert(findPosX(pm));
}


function setElementValue(elem, val)
{
	var e = document.getElementById(elem);
	e.value = val;
}

/*  ** Not needed anymore since the frames isn't used anymore
function show_login_link(){
	link_hide("td_logout");
	link_show("td_login");
}

function show_logout_link(){
	link_hide("td_login");
	link_show("td_logout");
}

function link_hide(linkId)
{
	var ll = document.getElementById(linkId);
	ll.style.display="none";
}

function link_show(linkId)
{
	var ll = document.getElementById(linkId);
	IE_setDisplayStyle(ll);
}
*/


function buildEditPopup(basename, spara, avbryt, hiddenE)
{
	var p = document.createElement('DIV');
	p.setAttribute('id', basename+'_popup');
	IE_setClass(p, 'popup');
	
	pForm = document.createElement('FORM');
	pForm.setAttribute('action', 'mainpage.php');
	pForm.setAttribute('method', 'POST');
	pForm.setAttribute('name', basename+'_form');
	pForm.setAttribute('id', basename+'_form');
	
	pTable = document.createElement('TABLE');	
	pTable.setAttribute('id', basename+'_table');
	pTable.setAttribute('border', 0);
	
	// Skapa spara och abryt länkar
	pTr = pTable.insertRow(-1);
	pTomTr = pTr.insertCell(-1);
	pTd = pTr.insertCell(-1);
	
	pTd.setAttribute('colspan', 2);
	pTd.setAttribute('align', 'right');
	pTd.setAttribute('valign', 'bottom');
	
	pSpara = document.createElement('A');
	pSpara.style.cursor='pointer';
	IE_addEventListener(pSpara, 'click', spara);
	pSparaButton = document.createElement('INPUT');
	pSparaButton.setAttribute('type', 'button');
	pSparaButton.setAttribute('value', 'Spara');
	pSparaButton.style.height='20px';
	pSpara.appendChild(pSparaButton);
		
	pAvbryt = document.createElement('A');
	pAvbryt.style.cursor='pointer';
	IE_addEventListener(pAvbryt, 'click', avbryt);
	pAvbrytButton = document.createElement('INPUT');
	pAvbrytButton.setAttribute('type', 'button');
	pAvbrytButton.setAttribute('value', 'Avbryt');
	pAvbrytButton.style.height='20px';
	pAvbryt.appendChild(pAvbrytButton);


	pTd.appendChild(pAvbryt);
	pTd.appendChild(pSpara);

	insertHidden(pTd, basename, 'origin');
	insertHidden(pTd, basename+'_todo', 'todo');

	var l = hiddenE.length;
	for(i=0; i<l; i++){
		h = hiddenE[i];
		insertHidden(pTd, h[0], h[1]);
	}
	
	pForm.appendChild(pTable);
	p.appendChild(pForm);
	
	document.body.appendChild(p);
}

function insertHidden(parentE, eId, eName)
{
	e = document.createElement('input');
	e.setAttribute('id', eId);
	e.setAttribute('type', 'hidden');
	e.setAttribute('name', eName);
	parentE.appendChild(e);
}
