/*
   may 24 2011
   bug 1 - ashish 
   feature value must be numeric 
   bug 13 - ashish 
   put focus on the first field 
   create a new feature
   the same to edit feature -> field feature name (an) 
   
*/
// data coming from server 
//   [[requirement_id, custom_id, name, value]] 
// 
var featureRow = new Entity(featureRow, true, [[zipAlt1],[zipCount1],0,1,2,3]);	 
var features = new Entity(features, false,["title.draw(['features']);", "(getSize())[1]/2;", [featureRow]]);

features.refresh = function() {
	$.ajax({
		data: {q:"features", d: $("#instance").val()}, 
		success:function(data,textStatus) {
			if ( ! data ) 
				return;
			try {				
				zipCount1.reset();
				featureRow.set(data);				
            updateFormNumCB = data.length;
				$('#rightside').html(features.draw());
				var scr = 	["None", "CaseInsensitiveString", "CaseInsensitiveString", "None"]; 
				var st1 = new SortableTable($("#featuresList").get(0), scr);
			} catch(e){
				myCatch(e);
			} 
		}
	});		
}

featureRow.goClick = function() {
	var action = $("#action").val(); 
	
	var ids="";
	var selected=-1;
	try { 
		for (var i=0; i < featureRow.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("Are you sure?")) {
				return;
			}
			$.ajax({	
				data: {q:'featuredelete', d: ids},
				success: function(data,textStatus) {
					if ( ! data) return;
					try {
						rightsideDraw();
					} catch (e) {
						myCatch(e);
					}
				}
			});
		} else { // edit
			$('#featureOne').css('visibility','visible');
			$('#featureOneReqId').val(this.data[selected][0]);
			$('#featureOneCId').val(this.data[selected][1]);
			$('#featureOneCId').attr('disabled','disabled');
			$('#featureOneName').val(this.data[selected][2]).focus(); // bug13 - may 24 11
			$('#featureOneValue').val(this.data[selected][3]);
		}
	} catch(e) {
		myCatch(e);
	}
}

featureRow.create = function() {
	$('#featureOne').css('visibility','visible');
	$('#featureOneReqId').val('');
	$('#featureOneCId').val('').focus(); // bug 13 - may 24 11 
	$('#featureOneCId').removeAttr('disabled');
	$('#featureOneName').val('');
	$('#featureOneValue').val('');
}

featureRow.save = function(){
	var d=[];
	var elm;
	var precReqIds;
	
	d[0] = $('#instance').val();
	d[1] = $('#featureOneReqId').val();	
	
	try {
		
		elm = $('#featureOneCId').get(0); 
		if ( ! elm || elm.value == '') 
			throw "Feature Custom ID must not be empty";
		d[2] = elm.value;
		elm = $('#featureOneName').get(0);
		if ( ! elm || elm.value == '') 
			throw "Feature Name must not be empty";
		d[3] = elm.value;
		elm = $('#featureOneValue').get(0);
		if ( ! elm || elm.value == '' || isNaN(elm.value)) { 
			throw "Value field must be numeric"; // bug 1 - may 24 11
		} else { 
			d[4] = elm.value;
		}

		precReqIds = featureRow.getPrecReqIds(d[1],d[2]);
		if ( precReqIds  == -1) 
			return;
		
		var data = $.toJSON(d);
		$.ajax({
			data: {q:'featuresave', d: data},
			success: function(data,textStatus) {
				if ( ! data) return;
				try { 
					rightsideDraw();
				} catch(e){
					myCatch(e);
				} 				
			}
		});
	} catch(e) {
		myCatch(e);
	}
}	

featureRow.getPrecReqIds = function(ReqId,ReqCid) {
	var reqMap=Array();
	for(var i=0; i < this.numRows; i++){
		// map cid -> id
		reqMap[this.data[i][1]] = this.data[i][0];		
    }
    
    if ( ReqId == '' && reqMap[ReqCid]) { // new feature 
    	alert('Feature Custom ID ' + ReqCid + ' must be unique');
    	return -1;
    }    
}	
 

