var generateSchedule = new Entity (generateSchedule, false,["title.draw(['generate staffing plan']);", 
	function (){
		return $('#staffingPlanName').val() + ' - Staffing Plan ' ;	
	},
   function(){ 
      return $('#direction').val()==2 ? '' : 'style="visibility:hidden"';
   }]);	

generateSchedule.go = function (){
	var d=Array(), data, tmp;
	try {
		d[0] = $('#instance').val();
		d[1] = $('#gsName').val();
		for (var i = 0; i < document.post_form.timeLimit.length ; i++) {
			if ( document.post_form.timeLimit[i].checked ){
				d[2] = document.post_form.timeLimit[i].value;
				found = true;
				break;		
			}		
		}
		if ( ! found ) {
			throw 'Please select a time limit';
		}
		d[3] = $('#gsDesc').val();
		if ( $('#direction').val() == 2 ) {
			d[4]='2';
			d[5] = $('#gsDuration').val();
			if ( isNaN(d[5]) ) {
			    throw 'You must enter a valid duration when maximizing the value';
			}
			d[5] = Math.floor(1.0 * d[5]);
			if ( d[5] <= 0 ) {
			    throw 'You must enter a valid duration when maximizing the value';
			    
			}
		} else {
			d[4] = '1';
			d[5] = 0;
		}

		d[6] = 1;
      
		tmp = $('#gsMult').val();
		if (tmp == 2) {
			d[6] = $('#gsNumSch').val();			
			if ( ! d[6] || isNaN(d[6])) {
				throw 'You must enter a valid number of staffing plans (1-10)';
			}	
			d[6] = Math.floor(1.0*d[6]);
			if (d[6] <= 0 || d[6] >= 10) {
				throw 'You must enter a valid number of staffing plans (1-10)';
			}
		}
		$('#progress').html('<br />Generating ... <br/><img src="/gfx/bars/loading.gif"><br/><br/>');
		data = $.toJSON(d);
		$.ajax({
			data: {q:'generate', d: data},
			success: function(response, textStatus) {
				if ( ! response ) 
					return;
				try {                
					if (response.error == '1' ||  response.error == 1) {
                  $('#instance').val(response.instance);
						$('#progress').html('<br/> Generation successful'); 
					} else {
						$('#progress').html(response.error);
						alert(response.error);
					}
				} catch (e) {
					myCatch(e);
				}			 				
			}
		});
	} catch(e) {
		myCatch(e);
	}
}


