/* 
 * bug 13 may 24 11
 * focus first field 
 */
// data coming from server 
// [[sid scid sname],...]
// [[rid rcid rname],...]
// [[tid tcid tname],...]
// [[pid sid rid tid from to pct],...]   

var sPARow = new Entity(sPARow, true, [[zipAlt1],[zipCount1],0,1,2,3,4,5,6,7]);// aid
var optionItemSPAStaff = new Entity(optionItem, true, [0, function(b) { return ''+b[1] + ' ' + b[2];}] );
var optionItemSPAReq = new Entity(optionItem, true, [0, function(b) { return ''+b[1] + ' ' + b[2];}] );
var optionItemSPATask = new Entity(optionItem, true, [0, function(b) { return ''+b[1] + ' ' + b[2];}] ) ;
optionItemSPAStaff.set = function(data) {
	Entity.prototype.set.call(this, data);
	this.hash = {};
	for ( var i = 0; i < data.length; i++) {
		this.hash[data[i][0]] = [data[i][1], data[i][2],i];
	}
};

optionItemSPAReq.set = function(data) {
	Entity.prototype.set.call(this, data);
	this.hash = {};
	for ( var i = 0; i < data.length; i++) {
		this.hash[data[i][0]] = [data[i][1], data[i][2],i];
	}
};

optionItemSPATask.set = function(data) {
	Entity.prototype.set.call(this, data);
	this.hash = {};
	for ( var i = 0; i < data.length; i++) {
		this.hash[data[i][0]] = [data[i][1], data[i][2],i];
	}
};

sPARow.prepare = function (d) {
	var staff = optionItemSPAStaff.hash[d[1]];
	if ( staff == undefined) {
		throw 'Database is corrupted! Staff id ' + d[1] + ' not found';
	}
	var req = optionItemSPAReq.hash[d[2]];
	if ( req == undefined) {
		throw 'Database is corrupted! Feature id ' + d[2] + ' not found';
	}	
	var task = optionItemSPATask.hash[d[3]];
	if ( task == undefined) {
		throw 'Database is corrupted! Task id ' + d[3] + ' not found';
	}
	
	return [d[0],staff[0], staff[1], d[4],d[5],req[1],task[1], d[6]]; 
}
 
var sPA = new Entity(sPA, false,["title.draw(['time windows when staff is pre-assigned']);", "(getSize())[1]/2;",[sPARow],
	[optionItemSPAStaff], [optionItemSPAReq], [optionItemSPATask]]);

sPA.refresh = function() {
	$.ajax({	
		data: {q:"sPA", d: $("#instance").val()}, 
		success:function(data,textStatus) {
			if ( ! data ) 
				return;
			try { 
				optionItemSPAStaff.set(data[0]);		
				optionItemSPAReq.set(data[1]);
				optionItemSPATask.set(data[2]);
				zipCount1.reset();
				sPARow.set(data[3]);
				updateFormNumCB = data[3].length;
				var rs = sPA.draw();
				// $('#debug').html(debugStr(rs));
				$('#rightside').html(rs);
				var scr = 	["None", "CaseInsensitiveString", "CaseInsensitiveString", "Number", "Number","CaseInsensitiveString", "CaseInsensitiveString", "Number"]; 
				var st1 = new SortableTable($("#sPAList").get(0), scr);
			} catch (e){
				myCatch(e);
			} 
		}
	});		
}

sPARow.go = function() {
	var action = $("#action").val(); 
	var ids="";
	var selected=-1;
	try { 
		for (var i=0; i < this.numRows; i++){
			if ($("#checkBox_"+i).attr('checked')){
				if ( ids.length > 0 ) ids +=",";
				ids += $("#checkBox_"+i).val();
				if (selected == -1) selected  = i;
			}
		}
    
		if ( ids.length == 0 ) {
			throw "You must select something!";
		}
    
		if  (action == 'delete') {
			if ( ! confirm("Do you really want to delete these items?")) {
				return;
			}
			$.ajax({
				data: {q:'sPAdelete', d: ids},
				success: function(data, textStatus) {
					if ( ! data ) return;
					try { 
						rightsideDraw();
					} catch (e) { 
						myCatch(e);
					}					
				}
			});
		} else { // edit - prepare data 
			$('#sPAOne').css('visibility','visible');
			$('#sPAOneId').val(this.data[selected][0]);
			var st = optionItemSPAStaff.hash[this.data[selected][1]]; 
			$('#sPAOneSelectStaffCid').attr('selectedIndex',st[2]);
			$('#sPAOneSelectStaffCid').attr('disabled','disabled');
			$('#sPAOneFrom').val(this.data[selected][4]).focus(); // 13 may 24 11
			$('#sPAOneTo').val(this.data[selected][5]);
			var req = optionItemSPAReq.hash[this.data[selected][2]];    	 
			$('#sPAOneSelectReqCid').attr('selectedIndex',req[2]);
			var tk = optionItemSPATask.hash[this.data[selected][3]]; 
			$('#sPAOneSelectTaskCid').attr('selectedIndex',tk[2]);
			$('#sPAOnePct').val(this.data[selected][6]);
		}
	} catch (e) {
		myCatch(e);
	}
	return true;
}

sPARow.save = function(){
	var d=[], data; 
	try { 
		d[0] = $('#sPAOneId').val(); // pid
		d[1] = $('#sPAOneSelectStaffCid').val(); // sid
		d[2] = $('#sPAOneSelectReqCid').val(); // rid
		d[3] = $('#sPAOneSelectTaskCid').val(); // tid
		 
		d[4] = myParseInt($('#sPAOneFrom').val()); // from
		d[5] = myParseInt($('#sPAOneTo').val()); // to
		d[6] = myParseInt($('#sPAOnePct').val()); // pct
		
		if ( d[4] > d[5] ) {
			throw "Invalid data! to_time < from_time ";
		}
		if ( 0> d[6] || d[6] > 100 ) {
			throw "Percent must be between 0 and 100";
		}
		data = $.toJSON(d);		
		$.ajax({
			data: {q:'sPAsave', d: data},
			success: function(data, textStatus) {
				if ( ! data ) return;
				try { 
					rightsideDraw();
				} catch (e) { 
					myCatch(e);
				}
			}
		});    
	} catch (e) {
		myCatch(e);		
	}	
}	

/*
 * Create new pre-assignment 
 */
sPARow.create = function(){
	$('#sPAOne').css('visibility','visible'); 	// form -> visible 
	$('#sPAOneId').val('');					// initializebug 
	$('#sPAOneFrom').val('');
   	$('#sPAOneTo').val('');
   	$('#sPAOnePct').val('');   	
	$('#sPAOneSelectStaffCid').removeAttr('disabled').focus(); // no change - staff 13 may 24 11    	
}
 
 

