// window size funcs
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var result = n_win ? n_win : (n_docel ? n_docel : (n_body ? n_body : -1));
	return result;
/*	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result; */
}

// form input auto clear funcs
function removeField(field, remove) {
	if (field.value == remove)
	{
		field.value = '';
	}
}

function defaultField(field, defaultval) {
	if (field.value.length == 0 || field.value == null)
	{
		field.value = defaultval;
	}
}

// open print timesheet page
function showPrint(offset, eid) {
	window.open("print.php?o=" + offset + '&eid=' + eid)
}

// display hour edit popup
function showHourEdit(offset, day, job, showReason) {
	var w = 360;
	var h = 150;
	
	var cellid = "cell_" + day + "_" + job;
	var cell = document.getElementById(cellid);

	var hrs = cell.innerHTML;
	hrs = Number(hrs);
	if (isNaN(hrs)) {
		hrs = 0;
	}
	
	var popup = document.getElementById('popup');
	var popup_title = document.getElementById('popup_title');
	var popup_content = document.getElementById('popup_content');
	var popup_close = document.getElementById('popup_close');
	
	popup_title.innerHTML = "Editing hours for job " + job + " on day " + day;
	popup_close.innerHTML = '<a href="javascript:deselectCell(' + day + ', ' + job + '); javascript:closePopup(document.getElementById(\'popup\'));"><small>Close</small></a>';
	
	createUpdateForm(popup_content, offset, day, job, hrs, showReason);
	
	x = f_clientWidth()/2 - w/2;
	y = f_clientHeight()/2 - h/2;
	
	popup.style.top = y;
	popup.style.left = x;
	popup.style.minWidth = w;
	popup.style.minHeight = h;
	popup.style.visibility = "visible";
	
	document.getElementById('hrs').focus();
	
	for (i=1; document.getElementById('cell_' + i + '_1'); i++) {
		for (j=1; document.getElementById('cell_1_' + j); j++) {
			deselectCell(i, j);
		}
	}
	
	selectCell(day, job);
}

function selectCell(day, job) {
	var cell = document.getElementById('cell_' + day + '_' + job);
	
	cell.style.borderWidth = 3;
	cell.style.borderColor = "purple";
}

function deselectCell(day, job) {
	var cell = document.getElementById('cell_' + day + '_' + job);
	
	cell.style.borderWidth = 1;
	cell.style.borderColor = "black";
}

function closePopup(elem) {
	elem.style.visibility = 'hidden';
}

function createUpdateForm(elem, offset, day, job, hrs, showReason) {
	
	// clear the popup box of any old elements
	if (elem.hasChildNodes()) {
		while (elem.childNodes.length > 0) {
			elem.removeChild(elem.firstChild);
		}
	}
	
	elem.appendChild(document.createElement('br'));

	var tmpForm = document.createElement('form');
	tmpForm.setAttribute('method', 'POST');
	tmpForm.name = 'updateForm';
	tmpForm.setAttribute('name', 'updateForm');
	var tmpStr = 'javascript:updateHrs(' + offset + ', ' + day + ', ' + job + ', document.getElementById(\'hrs\').value, ' + showReason + ');';
	tmpForm.setAttribute('action', tmpStr);

	tmpElem = document.createElement('input');
	tmpElem.setAttribute('type', 'hidden');
	tmpElem.setAttribute('name', 'job');
	tmpElem.value = job;
	tmpForm.appendChild(tmpElem);

	tmpElem = document.createElement('input');
	tmpElem.setAttribute('type', 'hidden');
	tmpElem.setAttribute('name', 'day');
	tmpElem.value = day;
	tmpForm.appendChild(tmpElem);

	tmpElem = document.createElement('input');
	tmpElem.setAttribute('type', 'text');
	tmpElem.setAttribute('class', 'text_box');
	tmpElem.setAttribute('name', 'hrs');
	tmpElem.setAttribute('id', 'hrs');
	tmpElem.setAttribute('size', '3');
	tmpElem.value = hrs;
	tmpForm.appendChild(tmpElem);

	tmpElem = document.createElement('input');
	tmpElem.setAttribute('type', 'submit');
	tmpElem.setAttribute('class', 'button');
	tmpElem.setAttribute('name', 'update');
	tmpElem.value = 'Update...';
	tmpForm.appendChild(tmpElem);

	if (showReason) {
		tmpForm.appendChild(document.createElement('br'));
		tmpForm.appendChild(document.createElement('br'));
	
		tmpElem = document.createElement('input');
		tmpElem.setAttribute('type', 'text');
		tmpElem.setAttribute('name', 'reason');
		tmpElem.setAttribute('id', 'reason');
		tmpElem.setAttribute('size', '35');
		tmpElem.setAttribute('onfocus', 'javascript:removeField(this, \'Reason for change\');');
		tmpElem.setAttribute('onblur', 'javascript:defaultField(this, \'Reason for change\');');
		tmpElem.value = 'Reason for change';
		tmpForm.appendChild(tmpElem);
	}

	elem.appendChild(tmpForm);
	tmpElem = document.createElement('small');
	tmpElem.appendChild(document.createTextNode('Enter your hours and click Update.'));
	tmpElem.appendChild(document.createElement('br'));
	tmpElem.appendChild(document.createTextNode('If you are changing previous hours,'));
	tmpElem.appendChild(document.createElement('br'));
	tmpElem.appendChild(document.createTextNode('you must provide a reason for the change.'));
	tmpElem.appendChild(document.createElement('br'));
	elem.appendChild(tmpElem);
}

function updateTotalHrs(day, job, newHours) {
	newHours = Number(newHours);
	if (isNaN(newHours) || newHours<0) {
		newHours = 0;
	}
	
	var oldHours = Number(document.getElementById('cell_' + day + '_' + job).innerHTML);
	if (isNaN(oldHours)) {
		oldHours = 0;
	}
	
	var oldDay = Number(document.getElementById('dayHours_' + day).innerHTML);
	if (isNaN(oldDay)) {
		oldDay = 0;
	}
	
	var oldJob = Number(document.getElementById('jobHours_' + job).innerHTML);
	if (isNaN(oldJob)) {
		oldJob = 0;
	}
	
	var oldPd = Number(document.getElementById('periodHours').innerHTML);
	if (isNaN(oldPd)) {
		oldPd = 0;
	}
	
	var dayHours = oldDay - oldHours + newHours;
	var jobHours = oldJob - oldHours + newHours;
	var periodHours = oldPd - oldHours + newHours;
	
	document.getElementById('dayHours_' + day).innerHTML = dayHours;
	document.getElementById('jobHours_' + job).innerHTML = jobHours;
	document.getElementById('periodHours').innerHTML = periodHours;
}

function updateHrs(offset, day, job, hrs, showReason)
{
	//document.getElementById('popup_content').innerHTML = '<br />Updating...';
	
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		var xmlhttp = new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function() {
		var cell = document.getElementById('cell_' + day + '_' + job);
	
		// completed request, good response
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var response = xmlhttp.responseText;
			
			// did the update go through?
			if (response.substr(0,1) != '-') {
				if (response == '') {
					response = '&nbsp;';
				}
				
				document.getElementById('popup_content').innerHTML = '<br />Hours updated!';
				updateTotalHrs(day, job, hrs);
				cell.innerHTML = response;
				setTimeout('closePopup(document.getElementById(\'popup\'))', 1000);
			} else {
				// update failed on server side
				var popup_content = document.getElementById('popup_content');
				popup_content.innerHTML = '<br />ERROR: Failed to update hours!<br />' + response.substr(1);
			}
			
			deselectCell(day, job);
		} else {
			// did not receive a HTTP/200 OK message
			if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				var popup_content = document.getElementById('popup_content');
				popup_content.innerHTML = '<br />ERROR: Unable to update hours!<br />Contact an admin.';
				
				deselectCell(day, job);
			}
		}
	}

	var reason = null;
	
	if (hrs > 24) {
		alert('You cannot charge more than 24 hours to a job each day.');
		return;
	}
	
	if (showReason) {
		removeField(document.getElementById('reason'), 'Reason for change');
		reason = document.getElementById('reason').value;
	}
	
	if (reason == '') {
		alert('You must provide a reason when you change your timesheet.');
		closePopup(document.getElementById('popup'));
		deselectCell(day, job);		
		return;
	}
	
	xmlhttp.open("POST","update.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	var request = 'update=1&hrs=' + hrs + '&o=' + offset + '&day=' + day + '&job=' + job; 
	if (reason != null) {
		request = request + '&reason=' + reason;
	}
	xmlhttp.send(request);
}

// show employee info popup
function showEmployeeInfo(e, id) {
	var w = 550;
	var h = 250;

	var popup = document.getElementById('popup');
	var popup_title = document.getElementById('popup_title');
	var popup_content = document.getElementById('popup_content');
	
	//popup.innerHTML = '<div onmouseover="javascript:showEmployeeInfo(event, ' + id + ');">' + popup.innerHTML + '</div>';
	popup_title.innerHTML = 'Information card for employee #' + id;
	popup_content.innerHTML = "<hr /><br /><br />Loading employee info...";
	loadEmployeeInfo(popup_content, id);
		
	var x, y;
	
	if (!e) { e = window.event; }
	if (e.pageX && e.pageY) {
		x = e.pageX + 10;
		y = e.pageY + 10;
	} else if (e.clientX && e.clientY) {
		x = e.clientX + 10;
		y = e.clientY + 10;
	} else {
		x = f_clientWidth()/2 - w/2;
		y = f_clientHeight()/2 - h/2;
	}
	
	popup.style.top = y;
	popup.style.left = x;
	popup.style.minWidth = w;
	popup.style.minHeight = h;
	popup.style.visibility = "visible";
}

function positionEmployeeInfo(e) {
	var popup = document.getElementById('popup');
	var x, y;
	
	if (!e) { e = window.event; }
	if (e.pageX && e.pageY) {
		x = e.pageX + 10;
		y = e.pageY + 10;
	} else if (e.clientX && e.clientY) {
		x = e.clientX + 10;
		y = e.clientY + 10;
	} else {
		x = f_clientWidth()/2 - w/2;
		y = f_clientHeight()/2 - h/2;
	}
	
	popup.style.top = y;
	popup.style.left = x;
}

function loadEmployeeInfo(elem, id) {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		var xmlhttp = new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function() {
		// completed request, good response
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var response = xmlhttp.responseText;
			
			elem.innerHTML = response;
		} else {
			// did not receive a HTTP/200 OK message
			if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				var popup_content = document.getElementById('popup_content');
				popup_content.innerHTML = 'ERROR: Unable to retrieve user info!<br />Contact an admin.';
			}
		}
	}

	xmlhttp.open("GET","info.php?id="+id,true);
	xmlhttp.send();
}
