/* jquery functions */
$(document).ready(function()
{
	$('.dialog').dialog({modal:true, autoOpen:false, width:760, height:528, minWidth:760, minHeight:528});
});


function openDialog(data)
{
	$('.dialog').html(data);
	$('.dialog').dialog('open');	
}

function closeDialog()
{
	$('.dialog').dialog('close');
}

function loadDialogData(action, week, year, companyID, projectID, location)
{	
	$.post('/urenregistratie/includes/content.php', {action:action, week:week, year:year, companyID:companyID, projectID:projectID, location:location}, openDialog);
}


function loadDialogDataForCheck(action, week, year, employeeID, projectID, location)
{	
	$.post('/urenregistratie/includes/content.php', {action:action, week:week, year:year, employeeID:employeeID, projectID:projectID, location:location}, openDialog);
}



/* Format functies */
function formatHour(textInput, hourLimit)
{
	if (hourLimit == undefined)
		hourLimit = 24;

	value = textInput.value;

	if (value.indexOf(',') != -1)
		values = value.split(',');
	else if (value.indexOf('.') != -1)
		values = value.split('.');
	else if (value.indexOf(':') != -1)
		values = value.split(':');
	else
		values = new Array(value, 0);


	hour = values[0];
	min  = (values[1]=='') ? 0 : values[1];
	msg	 = '';

	if (hour > hourLimit) {
		msg += '* Het aantal uren moet minder zijn dan 24.\n';
		hour = 0;
	}
		
	if (min == 15)
		min = 25;
	else if (min == 30)
		min = 50;
	else if (min == 45)
		min = 75;

	if (min!=0 && min!=00 && min!=25 && min!=5 && min!=50 && min!=75) {
		msg += '* Uren moeten worden afgerond op kwartieren. (00, 25, 50 of 75).\n';
		min  = 0;
	}
	
	if (msg != '')
		alert(msg);
		
	min = min.toString();
	if (min.length == 1)
		min = min +'0';

	formatedValue = hour +'.'+ min;
	textInput.value = (formatedValue=='0.00' || formatedValue=='.00') ? '' : formatedValue;
}

function formatKilometer(textInput)
{
	/*
	value = textInput.value;

	if (value.indexOf(',') != -1)
		value = value.substr(0, ',');
	else if (value.indexOf('.') != -1) 
		value = value.substr(0, '.');

	textInput.value = (value=='0') ? '' : value;
	*/
}

function formatZipcode(textInput)
{
	value = textInput.value;
	value = value.replace(' ', '');
	
	numbers = value.substr(0,4);
	letters = value.substr(4,2).toUpperCase();

	textInput.value = numbers + letters;
}

function updateTotal(id)
{
	total = 0;
	if (id == 'totalNormal') {
		$('input[name*=normal]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalOver1') {
		$('input[name*=over1]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalOver2') {
		$('input[name*=over2]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalOver3') {
		$('input[name*=over3]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalSickness') {
		$('input[name*=sickness]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalLeave1') {
		$('input[name*=leave1]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalLeave2') {
		$('input[name*=leave2]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalDrivenKilometers') {
		$('input[name*=driven]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}
	else if (id == 'totalTravel') {
		$('input[name*=travel]').each(function()
		{
			value = new Number($(this).val());
			total += value;
		});
	}

	textInput = document.getElementById(id);
	textInput.value = total;
	
	if (id != 'totalDrivenKilometers')
		formatHour(textInput, 24*7);
}



/* Debug functions */
function trace(msg)
{
	console.log(msg);
}
