var active = "none";

function setCustomer(id,e)
{
	if(id!=0) location.href = '?setCustomer='+id;
	else move('newCustomer',e);
}

function isNewCustomer()
{
	for(i=0;i<activeCustomers.length;i++) if(document.newCustomer.newCustomer.value==activeCustomers[i])
	{
		alert(document.newCustomer.newCustomer.value+" already exits!");
		return false;
	}
	return true;
}

function isNewDepartment()
{
	for(i=0;i<activeDepartments.length;i++) if(document.newDepartment.newDepartment.value==activeDepartments[i])
	{
		alert(document.newDepartment.newDepartment.value+" already exits!");
		return false;
	}
	return true;
}

function moveDepartment(i,e,privileged)
{
	document.newAction.departmentID.value = i;
	tmp = "";
	if(activeActions[i]!=null) for(j=0;j<activeActions[i].length;j++)
	{
		tmp+= "<option value=\""+activeActions[i][j][1]+"\">&uuml;ber "+activeActions[i][j][0]+"</option>\n";
	}
	tmp+= "<option value=\"0\" selected=\"selected\">neu</option>\n";
	document.newAction.insertBeyond.innerHTML = tmp;
	if(i!=0 && privileged) document.getElementById('delDepartment').innerHTML = "<input type=\"button\" value=\"l&ouml;schen\" onclick=\"if(confirm('Wirklich löschen?')) location.href='?delDepartment="+i+"'\" style=\"width:70px\" />";
	else document.getElementById('delDepartment').innerHTML = "&nbsp;";
	move('newAction',e);
}

function moveAction(i,e)
{
	document.newSubaction.actionID.value = i;
	document.newSubaction.rename.value = activeActionValues[i][0];
	document.newSubaction.quantity.value = activeActionValues[i][1];
	document.newSubaction.single.value = activeActionValues[i][2];
	document.newSubaction.foreign.value = activeActionValues[i][3];
	document.newSubaction.own.value = activeActionValues[i][4];
	if(activeActionValues[i][5]==1) document.newSubaction.ack.checked = true;
	else document.newSubaction.ack.checked = false;
	document.newSubaction.comment.value = activeActionValues[i][6];
	tmp = "";
	if(activeSubactions[i]!=null) for(j=0;j<activeSubactions[i].length;j++)
	{
		tmp+= "<option value=\""+activeSubactions[i][j][1]+"\">&uuml;ber "+activeSubactions[i][j][0]+"</option>\n";
	}
	tmp+= "<option value=\"0\" selected=\"selected\">neu</option>\n";
	document.newSubaction.insertBeyond.innerHTML = tmp;
	if(i!=0) document.getElementById('delAction').innerHTML = "<input type=\"button\" value=\"l&ouml;schen\" onclick=\"if(confirm('Wirklich löschen?')) location.href='?delAction="+i+"'\" style=\"width:70px\" />";
	else document.getElementById('delAction').innerHTML = "&nbsp;";
	if(activeActionValues[i][7]!=null) for(j=1;j<activeActionValues[i][7].length;j++)
	{
		tmp = eval("document.newSubaction.remind"+activeActionValues[i][7][j]);
		tmp.checked = true;
	}
	move('newSubaction',e);
}

function moveSubaction(i,e)
{
	document.changeSubaction.subactionID.value = i;
	document.changeSubaction.rename.value = activeSubactionValues[i][0];
	document.changeSubaction.quantity.value = activeSubactionValues[i][1];
	document.changeSubaction.single.value = activeSubactionValues[i][2];
	document.changeSubaction.foreign.value = activeSubactionValues[i][3];
	document.changeSubaction.own.value = activeSubactionValues[i][4];
	if(activeSubactionValues[i][5]==1) document.changeSubaction.ack.checked = true;
	else document.changeSubaction.ack.checked = false;
	document.changeSubaction.comment.value = activeSubactionValues[i][6];
	if(i!=0) document.getElementById('delSubaction').innerHTML = "<input type=\"button\" value=\"l&ouml;schen\" onclick=\"if(confirm('Wirklich löschen?')) location.href='?delSubaction="+i+"'\" style=\"width:70px\" />";
	else document.getElementById('delSubaction').innerHTML = "&nbsp;";
	move('changeSubaction',e);
}

function moveEvent(i,j,k,l,e)
{
	document.newEvent.eventID.value = i;
	document.newEvent.parentID.value = j;
	document.newEvent.subTrigger.value = k;
	if(l==0) document.newEvent.begin.selectedIndex = (activeEventValues[i][3]-1);
	else document.newEvent.begin.selectedIndex = l-1;
	document.newEvent.duration.selectedIndex = (activeEventValues[i][4]-1);
	if(i>0) document.newEvent.kind.selectedIndex = (activeEventValues[i][5]*-1)+2;
	document.newEvent.txt.value = activeEventValues[i][6];
	if(l!=0) document.getElementById('eventHeader').innerHTML = "Neues Ereignis";
	else document.getElementById('eventHeader').innerHTML = "Ereignis &auml;ndern";
	if(i!=0) document.getElementById('delEvent').innerHTML = "<input type=\"button\" value=\"l&ouml;schen\" onclick=\"if(confirm('Wirklich löschen?')) location.href='?delEvent="+i+"&subTrigger="+k+"'\" style=\"width:70px\" />";
	else document.getElementById('delEvent').innerHTML = "&nbsp;";
	move('newEvent',e);
}

function isNewEvent()
{
	if((document.newEvent.begin.selectedIndex+document.newEvent.duration.selectedIndex)>11)
	{
		alert("Leider können keine Events über die Jahresgrenze angelegt werden!");
		return false;
	}
	return true;
}

function eventProp(e)
{
    if (!e) var e = window.event;
	var posx = 100;
    var posy = 100;
    if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
  	}
	return Array(posx,posy);
}

function move(what,e)
{
	moveX = 20;
	moveY = 10;
	if(what=="bonusLayer")
	{
		moveX = 0;
		moveY = 0;
	}
	hide();
	active = what;
	where = eventProp(e);	
	document.getElementById(what).style.top = (where[1]-moveY)+"px";
	document.getElementById(what).style.left = (where[0]-moveX)+"px";
}

function move1(what)
{
	moveY = 300;
	hide();
	active = what;
	
	//document.getElementById(what).style.top = moveY+"px";
	//document.getElementById(what).style.left = moveX+"px";
}

function hide()
{
	if(active!="none")
	{
		document.getElementById(active).style.top = "-1000px";
		document.getElementById(active).style.left = "-1000px";
		active = "none";
	}
}

function dontMove() {}