var g_selectedPerson = null;
var g_personTargets = new Array();
var g_weeksSource = new Array();
var g_weeksTargets = new Array();

function turnus_updateView(name)
{
	var from = document.getElementById('from_week');
	var to = document.getElementById('to_week');
	var data = document.getElementById('cl_1');
	var year = document.getElementById('year');
	
	if (from == null || to == null)
	{
		alert("WARNING: From week and/or to week is null!");
		return;
	}
	
	var url = name + '.php?from_week=' + from.options[from.selectedIndex].text + "&to_week=" + to.options[to.selectedIndex].text + "&filter=" + 
			data.value + "&year=" + year.options[year.selectedIndex].text;
	ajax_fetch(url, name + '_div');

	//alert(url);
}

function turnus_colorWeek(week, color)
{
	//Color a single week
	for (d = 1; d < 8; d++)
	{
		var head = document.getElementById('week_' + week + '_' + d);
		if (head != null)
		{
			head.style.backgroundColor = color;
		}
		else
		{
			
		}
	}
}

function turnus_togglePerson(person)
{
	for (i = 0; i < g_personTargets.length; i++)
	{
		if (g_personTargets[i] == person)
		{
			g_personTargets.splice(i, 1);
			return;
		}
	}

	g_personTargets.push(person);
}

var g_turnusChanged = new Array();

function turnus_updateChanged(person, day, index)
{
	for (t = 0; t < g_turnusChanged.length; t++)
	{
		var data = g_turnusChanged[t].split(":");
		if (data[0] == person && data[1] == day)
		{
			g_turnusChanged.splice(t, 1);
			break;
		}
	}
	
	//Add it
	g_turnusChanged.push(person + ":" + day + ":" + index);
	
}

function turnus_flushChanged()
{
	g_turnusChanged = new Array();
	g_selectedPerson = null;
	g_personTargets = new Array();
	g_weeksSource = new Array();
	g_weeksTargets = new Array();
}

function turnus_flattenChanged()
{
	var destination = document.getElementById('turnus_changed');
	if (destination != null) 
	{
		destination.value = "";
		
		for (t = 0; t < g_turnusChanged.length; t++)
		{
			destination.value += g_turnusChanged[t] + ";";
		}
	}
}

function turnus_copySingleDayToSelPersons(day, index)
{
	//Set the index for all selected persons
	for (d = 0; d < g_personTargets.length; d++)
	{
		turnus_copySingleDayToSinglePerson(day, index, g_personTargets[d]);
	}
}

function turnus_copySingleDayToSinglePerson(day, index, person)
{
	var target = document.getElementById(person + "_" + day);
	if (target != null && target.selectedIndex != index)
	{
		//Mark the target element as changed
		target.style.border = "1px solid #FF0000";
		target.selectedIndex = index;
		turnus_updateChanged(person, day, target.options[target.selectedIndex].value);
	}
}


function turnus_copyPersonToPersons(firstWeekNum, maxDays)
{
	//Is the selected person valid?
	if (g_selectedPerson == null)
	{
		alert('Du må velge en person å kopiere fra først');
		return;
	}
	
	//Are there anyone in the target list?
	if (g_personTargets.length == 0)
	{
		alert("Du må velge minst én person å kopiere til");
		return;		
	}
	
	//So far so good, we may now preform the actual copying.
	
	//For each of the days...
	var dayFrom = 0;
	var dayTo = maxDays;
	
	//Do we need to use the month selectors?
	if (g_weeksTargets.length > 0)
	{
		//Yup. This means that we should only copy the data in the selected months.
		//To do this, we need to go trough each day of each selected week
		var day = 0;
		for (i = 0; i < g_weeksTargets.length; i++)
		{
			//Go trough all the days of the selected week
			for (j = 1; j < 8; j++)
			{
				//Now find the day index in range [0..lastWeek * 7] instead of [firstDay..lastWeek * 7]
				day = ((g_weeksTargets[i] - firstWeekNum) * 7 ) + j;

				var source = document.getElementById(g_selectedPerson + "_" + day);
				if (source != null)
				{
					//Preform the copying
					turnus_copySingleDayToSelPersons(day, source.selectedIndex);
				}
			}
		}
		turnus_flattenChanged();
		return;
	}
	
	//No week specified, so copy everything
	for (i = dayFrom; i <= dayTo; i++)
	{
		//...read the source data
		var source = document.getElementById(g_selectedPerson + "_" + i);
		if (source != null)
		{
			turnus_copySingleDayToSelPersons(i, source.selectedIndex);
		}
	}
	turnus_flattenChanged();
}

function turnus_copyPeriodToPeriodForPerson(person, firstWeekNum)
{
	var currentTargetWeek = 0;
	var day = 0;
	var targetDay = 0;
	
	//Go trough the targets
	//alert("copying to " + g_weeksTargets.length + " weeks");
	for (w = 0; w < g_weeksTargets.length; w++)
	{
		//alert("copying to week " + w);
		//Go trough all the days of the selected week
		for (j = 1; j < 8; j++)
		{
			//Now find the day index in range [0..lastWeek * 7] instead of [firstDay..lastWeek * 7]
			day = ((g_weeksSource[currentTargetWeek] - firstWeekNum) * 7 ) + j;
			
			//Now we have the day to copy, insert it into the corresponding day in the first week target.
			//This will be mapped to the current iterator day, which is relative to the current target week.
			targetDay = ( (g_weeksTargets[w] - firstWeekNum) * 7) + j;
	
			var source = document.getElementById(person + "_" + day);
			if (source != null)
			{
				//Preform the actual copying
				turnus_copySingleDayToSinglePerson(targetDay, source.selectedIndex, person);
			}
		}
		
		//Increment the target week. If there are no further targets, return.
		currentTargetWeek++;
		if (currentTargetWeek == g_weeksSource.length)
		{
			//Wrap the source
			currentTargetWeek = 0;
		}
	}
}

function turnus_copyPeriodToPeriod(allPersons, firstWeekNum)
{
	//Copies a week period to another week period, either for all persons, or for selected persons

	//Targets (checkboxes)?
	if (g_weeksTargets.length == 0)
	{
		alert("Velg en eller flere uker og kopiere til først");
		return;
	}
	
	//Sources (month selectors)?
	if (g_weeksSource.length == 0)
	{
		alert("Velg en eller flere uker og kopiere fra først");
		return;
	}
	
	//Sort weeks
	g_weeksSource.sort(function(a,b){return a - b});
	g_weeksTargets.sort(function(a,b){return a - b});
	
	//Are there any persons selected?
	if (g_personTargets.length > 0)
	{
		//Yup. Copy period from/to the selected people
		for (i = 0; i < g_personTargets.length; i++)
		{
			turnus_copyPeriodToPeriodForPerson(g_personTargets[i], firstWeekNum);
		}
		turnus_flattenChanged();
		return;
	}
	
	//Use all persons
	var data = allPersons.split(";");
	for (i = 0; i < data.length; i++)
	{
		turnus_copyPeriodToPeriodForPerson(data[i], firstWeekNum);
	}
	
	turnus_flattenChanged();
}

function turnus_entryChanged(sender, person, day)
{
	sender.style.border = "1px solid #FF0000";
	turnus_updateChanged(person, day, sender.options[ sender.selectedIndex ].value);
	turnus_flattenChanged();
}

function turnus_selectWeek(number)
{
	for (i = 0; i < g_weeksSource.length; i++)
	{
		if (g_weeksSource[i] == number)
		{
			//Remove it
			turnus_colorWeek(number, '#EEEEFF'); //Color default color
			g_weeksSource.splice(i, 1);
			return;
		}
	}
	//Add it
	g_weeksSource.push(number);
	//Color selected color
	turnus_colorWeek(number, '#AEFFAE'); 
	
	
	//Remove from target array
	for (i = 0; i < g_weeksTargets.length; i++)
	{
		if (g_weeksTargets[i] == number)
		{
			//Remove it
			g_weeksTargets.splice(i, 1);
			break;
		}
	}
	
	//Uncheck
	var box = document.getElementById('week_ch_' + number);
	if (box != null)
	{
		box.checked = false;
	}
	//*/
}

function turnus_checkWeek(sender, week)
{
	if (sender.checked)
	{
		//Remove the week from the sources
		for (i = 0; i < g_weeksSource.length; i++)
		{
			if (g_weeksSource[i] == week)
			{
				//Remove it
				turnus_colorWeek(week, '#EEEEFF'); //Color default color
				g_weeksSource.splice(i, 1);
				break;
			}
		}
		
		for (i = 0; i < g_weeksTargets.length; i++)
		{
			if (g_weeksTargets[i] == week)
			{
				//already added
				return;
			}
		}
		//Add it
		g_weeksTargets.push(week);
	}
	else
	{
		//Remove it
		for (i = 0; i < g_weeksTargets.length; i++)
		{
			if (g_weeksTargets[i] == week)
			{
				//Remove it
				g_weeksTargets.splice(i, 1);
				return;
			}
		}
	}
}

function turnus_selectPerson(person)
{

	if (g_selectedPerson != "" || g_selectedPerson == person) 
	{
		var per_bg = document.getElementById('pers_' + g_selectedPerson);
		if (per_bg != null)
		{
			per_bg.style.backgroundColor = '#EEEEFF';
		}
	}
	
	if (g_selectedPerson == person)
	{
		g_selectedPerson = null;
		return;
	}
	
	g_selectedPerson = person;
	
	var per_box = document.getElementById('pers_ch_' + person);
	if (per_box)
	{
		per_box.checked = false;
		for (i = 0; i < g_personTargets.length; i++)
		{
			if (g_personTargets[i] == person)
			{
				g_personTargets.splice(i, 1);
			}
		}
	}
	
	var per_bg = document.getElementById('pers_' + person);
	if (per_bg != null)
	{
		per_bg.style.backgroundColor = '#AEFFAE';
	}
	
}

function turnus_checkPerson(sender, id)
{
	if (sender.checked)
	{
		if (g_selectedPerson != null)
		{
			if (g_selectedPerson == id)
			{
				g_selectedPerson = "";
				var per_bg = document.getElementById('pers_' + id);
				if (per_bg != null)
				{
					per_bg.style.backgroundColor = '#EEEEFF';
				}
			}
		}
		
	}
	
	for (pe = 0; pe < g_personTargets.length; pe++)
	{
		if (g_personTargets[pe] == id)
		{
			g_personTargets.splice(pe, 1);
			break;
		}
	}
	
	if (sender.checked)
	{
		g_personTargets.push(id);
	}
}
