var DataInput = Class.create({
	initialize: function(elem){
		this.container = elem;
		this.count = 1;
		this.add();
	},
	add: function(){
		var li = new Element('li',{id:'list_item_data_'+this.count});
		var bigInput = new Element('input', {id:'big_input_'+this.count, size: '36'});
		var smallInput = new Element('input', {id:'small_input_'+this.count, size: '6'});
		var removeBtn = new Element('a', {'class': 'cross', id:'close_btn_'+this.count, 'title': 'Click here to delete'}).update("&nbsp;");
		li.insert(bigInput);
		li.insert(smallInput);
		li.insert(removeBtn);
		this.container.insert({bottom:li});
		bigInput.activate();
		/*
			TODO TAB to add row when only one row is left
		*/
		$("small_input_"+this.count).observe("keydown",function(e) {
			// if (e) e.stop();
			// alert(1)
			var el = Event.element(e);
			if ((e.keyCode === Event.KEY_RETURN)){
				if(!el.ancestors().first().next()){
					e.stop();
					this.add();
				}else{
					el.ancestors().first().next().childElements().first().activate();
				}
			};
			if ((e.keyCode === Event.KEY_TAB)){
				if(el.ancestors()[1].childElements().length == 1){
					e.stop();
					this.add();
				};
				if(!el.ancestors().first().next()){
					e.stop();
					this.add();
				};
			};
		}.bind(this));
		
		$("big_input_"+this.count).observe('keydown',function(e){
			var el = Event.element(e);
				if ((e.keyCode === Event.KEY_RETURN)){
					if(!el.ancestors().first().next()){
						this.add();
					}else{
						el.ancestors().first().next().childElements().first().activate();
					}
					e.stop();
				}
		}.bind(this));
		
		$("close_btn_"+this.count).observe('click',function(e) {
			if (e) e.stop();
			var count = this.id.split("_")[2];
			$("list_item_data_"+count).remove();
		});
		
		$("big_input_"+this.count).focus();
		this.count++;
	},
	getData: function(){
		var data = new Array;
		var i = 0;
		var count;
		var flag = false;
		this.container.childElements().each(function(el) {
			var number;
			if (el.visible()) {
				count = el.id.split("_")[3];
				$("small_input_"+count).setStyle({background:"#fff"})
				$("big_input_"+count).setStyle({background:"#fff"})
				if($("small_input_"+count).value.strip() == ""){
					$("small_input_"+count).setStyle({background:"#FFCCCF"});
					flag = true;
				};
				
				number = $("big_input_"+count).value;
				number = new Number(number);
				if(($("big_input_"+count).value.strip() == "") | (! isNaN(number))){
					$("big_input_"+count).setStyle({background:"#FFCCCF"});
					flag = true;
				};
				
				number = $("small_input_"+count).value;
				number = new Number(number);
				if (isNaN(number) || $("small_input_"+count).value.include(".") ) {
					$("small_input_"+count).setStyle({background:"#FFCCCF"});
					flag = true;
				};
				data[i] = [$("big_input_"+count).value, parseInt($("small_input_"+count).value, 10)];
				i++;
			}
		});
		if (flag) {
			return false
		}else{			
			return data;
		};
	}
});

var EditableField = Class.create({
	initialize: function(fld, cls, p) {
		var f = $(fld);
		f.observe('focus',function(e) {
			if (e) e.stop(); 
			f.addClassName(cls);
			if (f.value === p)
				f.value = "";
		});
		f.observe('blur',function(e) {
			if (e) e.stop();
			f.removeClassName(cls);
			if (f.value.blank())
				f.value = p;
		});
	}
});
