// data coming from server 
// [[sid scid sname],...]
// [[aid sid from to],...]   
var optionItemSAV = new Entity(optionItem,true, [0, function(b) { return ''+b[1] + ' ' + b[2];}]);
optionItemSAV.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];
	}
};

var sAvRow = new Entity(sAvRow, true, [[zipAlt1],[zipCount1],0,1,2,3,4]);// aid

sAvRow.prepare = function (d) {
	var staff = optionItemSAV.hash[d[1]];
	return [d[0],staff[0], staff[1], d[2],d[3]]; 
}

var sAv = new Entity(sAv, false,["title.draw(['time windows when staff is unavailable']);", "(getSize())[1]/2;",[sAvRow],	
	[optionItemSAV]]);

sAv.refresh = function() {
	$.ajax({
		data: {q:"sAv", d: $("#instance").val()}, 
		success:function(data,textStatus) {
			if ( ! data ) 
				return;
			try {
				optionItemSAV.set(data[0]);
				zipCount1.reset();
				sAvRow.set(data[1]);
				updateFormNumCB = data[1].length;
				$('#rightside').html(sAv.draw());
				var scr = 	["None", "CaseInsensitiveString", "CaseInsensitiveString", "Number", "Number"]; 
				var st1 = new SortableTable(document.getElementById("sAvList"), scr);
			} catch (e) { 
				alert('Exception '+e.name+' '+e.message);
			} 
		}
	});		
}

sAvRow.go = function() {
	var action = document.getElementById("action").options[document.getElementById("action").selectedIndex].value;; 
	
	var ids="";
	var selected=-1;
	for (var i=0; i < this.numRows; i++){
		if (document.getElementById("checkBox_"+i).checked){
			if ( ids.length > 0 ) ids +=",";
			ids += document.getElementById("checkBox_"+i).value;
			if (selected == -1) selected  = i;
		}
    }
    
    if ( ids.length == 0 ) {
    	alert("You must select something!");
    	return false;
    }
    
    if  (action == 'delete') {
		if ( ! confirm("Do you really want to delete these items?")) {
			return false;
		}
    	$.ajax({
    		data: {q:'sAvdelete', d: ids},
			success: function(data, textStatus) {
    			if ( ! data ) return;
				try { 
					rightsideDraw();
				} catch(e) {
					alert('Exception ' + e.name + ' ' + e.message);
				}
			}
    	});
    } else { // edit
    	$('#sAvOne').css('visibility','visible');
    	$('#sAvOneId').val(this.data[selected][0]);
    	var st = optionItemSAV.hash[this.data[selected][1]]; 
    	$('#sAvOneSelectStaffCid').attr('selectedIndex',st[2]);
    	$('#sAvOneSelectStaffCid').attr('disabled','disabled');
    	$('#sAvOneFrom').val(this.data[selected][2]).focus(); // bug 13 may 24 11
    	$('#sAvOneTo').val(this.data[selected][3]);
    }
	return true;
}

sAvRow.save = function(){
	var d=[];
	
	// aid
	var elm = $('#sAvOneId').get(0);
	if (!( elm && elm.value)) { 
		d[0] = ''; // no aid => new  
	} else {
		d[0]= elm.value; // aid - edit 
	} 
	
	// sid 
	elm = $('#sAvOneSelectStaffCid').get(0);
	var si = $('#sAvOneSelectStaffCid').attr('selectedIndex');
	if ( si == -1) {
		alert('no staff is selected');
		return;
	}
	d[1] = elm.options[si].value;
	if ( ! d[1] ) {
		alert('custom id cannot be empty');
		return;
	}
	// from 
	d[2] = parseInt($('#sAvOneFrom').val());
	// to 
	d[3] = parseInt($('#sAvOneTo').val());
    if ( isNaN(d[2]) || isNaN(d[3])) {
    	alert( 'from_time, to_time must be integer');
    	return;
    }
    if ( d[3] < d[2]) {
    	alert('Invalid data, to_time less than from_time');
    	return;
    }
	var data = $.toJSON(d);
	$.ajax({
		data: {q:'sAvsave', d: data},
		success: function(data, textStatus) {
			if ( ! data ) return;
			try { 
				rightsideDraw();
			} catch ( e){
				alert('Exception '+e.name + ' ' + e.message);
			}
		}
	});    
}	

sAvRow.create = function(){
	$('#sAvOne').css('visibility','visible');
   	$('#sAvOneId').val('');
   	$('#sAvOneSelectStaffCid').removeAttr('disabled').focus(); // bug 13 may 24 11
   	$('#sAvOneFrom').val('');
   	$('#sAvOneTo').val('');
}
 
 

