/*
 * may 24 11 
 * bug 8 - if name has character , (in copy, rename) the part after it is truncated 
 * give a message to user that , is not allowed in name, it will be replace by '-' 
 */
var existStaffingPlansRow = new Entity(existStaffingPlansRow, true, 
	[[zipAlt1],
	[zipCount1],0,0,
    1,2,3]);

var existStaffingPlans = new Entity(existStaffingPlans, false, [ 'title.draw(["existing staffing projects"])', '(getSize())[1]/2', [existStaffingPlansRow]]);

existStaffingPlans.refresh = function () {
	$.ajax({
		data: {q:'staffingList', u:$('#user').val()},
		success: function(data, textStatus){
			if ( ! data)
				return;
			try { 
				updateFormNumCB = data.length;
	    		zipCount1.reset();
    			existStaffingPlansRow.set(data);
				$('#rightside').html(existStaffingPlans.draw());
		 		var st1 = new SortableTable($("#staffingList").get(0),
					["None", "Number","CaseInsensitiveString", "CaseInsensitiveString", "CaseInsensitiveString"]);
			} catch ( e) {
				myCatch(e);
			}
		}
	});
}

/**
 * bug 8 - may 24 11
 * add var newName 
 */
existStaffingPlansRow.go = function (){
	var action = $("#action").val(); 	 
	var staffPlanIds="";
	var newName = "";
	try {
		for(var i=0; i < this.numRows; i++){
			if ($("#checkBox_"+i).attr('checked')) {
				if ( staffPlanIds.length > 0 ) staffPlanIds +=",";
				staffPlanIds += $("#checkBox_"+i).val();
			}
		}
    
		if ( staffPlanIds.length == 0 ) {
			throw "You must select a plan!";
		}
    
		switch (action) {
    		case 'delete':			
    			if ( ! confirm("You selected the staffing project(s) with the id(s):\n\n"+staffPlanIds+"\n\nDo you really want to delete them?")) {
    				return;
    			}
    			break;
    		case 'rename':
    			// bug 8 - may 24 11
    			newName = $("#newName").val($('#newName').val().trim()).val();
    			if ( newName.search(/[,'"]/) != -1 ) {
    				if ( ! confirm("These characters: comma, single quote, double quote are not not allowed in name, they will be replaced by dash blank\nAre you OK with that?"))
    					return;
    				newName = newName.replace(/[,'"]/g," ").trim();
    			}
    			if ( newName.length == 0 ) {
    				throw "You must give a name";    				
    			}
    			
    			staffPlanIds += "," + newName;
    			break;
    		case 'copy':
    			newName = $("#newName").val($('#newName').val().trim()).val();
    			if ( newName.search(/[,'"]/) != -1 ) {
    				if ( ! confirm("These characters: comma, single quote, double quote are not not allowed in name, they will be replaced by dash blank\nAre you OK with that?"))
    					return;
    				newName = newName.replace(/[,'"]/g," ").trim();
    			}
    			if ( newName.length == 0 ) {
    				throw "You must give a name";    				
    			}
    			staffPlanIds += "," + newName;
    			staffPlanIds += "," + $("#user").val();
    			break;
    		case 'xml':
    			newName = $("#newName").val($('#newName').val().trim()).val();
    			if ( newName.search(/[,'"]/) != -1 ) {
    				if ( ! confirm("These characters: comma, single quote, double quote are not not allowed in name, they will be replaced by dash blank\nAre you OK with that?"))
    					return;
    				newName = newName.replace(/[,'"]/g," ").trim();
    			}
    			if ( newName.length == 0 ) {
    				throw "You must give a file name";    				
    			}
    			$('#filename').val(newName);
            var tmp = $('#staffing').val();
    			$('#staffingid').val(tmp);
            tmp = $('#instance').val()
    			$('#instanceid').val(tmp);
    			document.post_form.submit();
    			break;
    		//case 'synchronize':
    		//	var cur = $('#staffing').val();
    		//	if ( cur=="") {
    		//		throw "You must select a current staffing project which is Shared first";
    		//	}
    		//	if ( $('#staffingShared').val() == "0" ) {
    		//		throw "The current staffing project must be Shared";
    		//	}
    		//	staffPlanIds = cur + "," + staffPlanIds;
    		//	break;
    			
		}
	
		$.ajax({
			data: {q:'staffingPlanAction', type: action, planIds: staffPlanIds},
			success: function(responses, textStatus) {
				if ( ! responses ) 
					return;
				try {
					if ( typeof (responses.name) == 'undefined') responses.name ='';
					if ( typeof (responses.id) == 'undefined') responses.id ='';
					if ( typeof (responses.instance) == 'undefined') responses.instance ='';
					$('#staffingPlanName').val(responses.name);
					$('#staffing').val(responses.id);
					$('#instance').val(responses.instance);
               $('#direction').val(responses.direction);
					sideNavSetStaffingPlanName(responses.name);
					if (action == 'open' ) {
						// if action is open  => overview
						sideNavClick('overview');	 
					} else {
						existStaffingPlans.refresh();
					}
				} catch (e) {
					myCatch(e); 
				}
			}
		});
	} catch(e) {
		myCatch(e);
	}
}
	

