﻿/// <reference name="MicrosoftAjax.js"/>

/* Schedule edit page */
function scheduleTable(tableId, patientsPerNurse) {
	this.table = $get(tableId);
	this.perNurse = patientsPerNurse;
	this.cellBounds = null;

	// init each cell
	this.init();
}

scheduleTable.prototype.init = function() {
	if(!this.table) return;
	
	var rows = this.table.tBodies[0].rows;
	
	for(var i=0; i < rows.length; i++) {
		var cells = rows[i].cells;
		for(var j=0; j < cells.length; j++) {
			var cell = cells[j];
			if(Sys.UI.DomElement.containsCssClass(cell, '')) {
				// we found edit cell
				this.initCell(cell, j*100+i);
			}
		}
	}
}

scheduleTable.prototype.initCell = function(cell, tabIndex) {
	// find textbox
	var tbx = null;
	var tbxs = cell.getElementsByTagName('input');
	for(var i=0; i < tbxs.length; i++) {
		var elem = tbxs[i];
		if(elem.tagName == "INPUT") {
			tbx = elem;
			tbx.cell = cell;
			tbx.tabIndex = tabIndex;
			cell.visits = Number.parseInvariant(cell.getAttribute('vi'));
			cell.isWorkingDay = Boolean.parse(cell.getAttribute('wd'));
			
			// set textbox property
			tbx.maxLength = 2;
			
			// create display element
			cell.lblDisplay = document.createElement('span');
			//insertAfter(cell.lblDisplay, tbx);
			cell.insertBefore(cell.lblDisplay, tbx);
			
			tbx.delegates = {
				change : Function.createDelegate(this, this.onBlur),
				keydown : Function.createDelegate(this, this.onKeyDown)
			}
			$addHandlers(tbx, tbx.delegates);
			
			// set background image position
			this.recalculate(tbx);
		}
	}
}

scheduleTable.prototype.recalculate = function(tbx) {
	var cell = tbx.cell;
	if(!this.cellBounds) {
		this.cellBounds = Sys.UI.DomElement.getBounds(cell);
	}
	var nurces = Number.parseInvariant(tbx.value);
	var maxVisit = nurces * this.perNurse;
	var maxPx = this.cellBounds.width;
	
	if(!isNaN(maxVisit) && nurces > 0) {
		// working time slot, nurces > 0
		// calculate percent of current visits
		var percentVisits = cell.visits / maxVisit;
		var pxOffset = null;
		
		// calculate x-axis bg offset
		if(percentVisits <= 1) {
			// set to green
			pxOffset = (maxPx * 1) - 250;
		}
		else {
			// we have reached limit of visits, display warning
			//if(percentVisits >= 2) {
				// don't calculate to many visits per time slot, set max warning
				pxOffset = maxPx - 750;
//			}
//			else { pxOffset = (maxPx * (percentVisits - 1)) - 750; }
		}
		cell.style.backgroundPosition = pxOffset.toString() + 'px 0';
		cell.lblDisplay.innerHTML = cell.visits + ' / ' + maxVisit + '&nbsp;&nbsp;&nbsp;';
	}
	else {
		// non working time slot, nurces = 0 or nothing
		if(tbx.value.trim() != '') tbx.value = '';
		
		// if no visit is scheduled set non working time slot
		if(cell.visits === 0) {
			cell.style.backgroundPosition = -1000 + 'px 0';
		} else {
			cell.style.backgroundPosition = maxPx - 750 + 'px 0';
		}
		cell.lblDisplay.innerHTML = cell.visits + '&nbsp;&nbsp;&nbsp;';
	}
	
	if(!cell.isWorkingDay) {
		Sys.UI.DomElement.addCssClass(cell, 'nonWorking');
	}
}

// elements event handlers
scheduleTable.prototype.onBlur = function(e) {
	var tbx = e.target;
	this.recalculate(tbx);
}

scheduleTable.prototype.onKeyDown = function (e) {
	var keyCode = e.keyCode;
	// allow only number
	if (!(keyCode < 57 || Sys.UI.Key.del)) stopEvent(e);
	if (Sys.Debug.isDebug) {
		window.status = keyCode;
	}
}

// helper functions
function insertAfter(node, referenceNode) {
	referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
}
