var Cog = {};//名前空間

Cog.Form = Class.create();
Cog.Form.prototype = {
	counter: 0,
	//コンストラクタ
	initialize: function(formID){
		this.form = $(formID);
		this.inputs = {};
		
		var elms = this.form.getElements();
		for (i=0; i<elms.length; i++){
			var element = elms[i];
			if (!this.inputs[element.name]){
				switch (element.type){
					case 'text': this.inputs[element.name] = new Cog.TextBox(this, element); break;
					case 'radio': this.inputs[element.name] = new Cog.RadioButton(this, element.name); break;
					case 'textarea': this.inputs[element.name] = new Cog.TextArea(this, element); break;
				}
			}
		}
		for (key in this.inputs)
			this[key] = this.inputs[key];
	},
	//更新
	update: function(){
		this.updateValue();
		this.updateControl();
	},
	updateValue: function(){
		for (key in this.inputs)
			this.inputs[key].updateValue();
	},
	updateControl: function(){
		for (key in this.inputs)
			this.inputs[key].updateControl();
	},
	validated: function(){
		for (key in this.inputs)
			if (!this.inputs[key].validated())
				return false;
		return true;
	}
}
Cog.Input = Class.create();
Cog.Input.History = [];
Cog.Input.prototype = {
	index: -1,
	parent: null,
	name: null,
	value: null,
	updateTimerID: -1,
	initialize: function(){},
	update: function(){
		this.updateValue();
		this.parent.updateControl();
		this.updateTimerID = -1;
	},
	updateValue: function(){},
	updateControl: function(){},
	validated: function(){ return true; },
	enabled: function(){ return true; },
	relation: [],
	setUpdateTimer: function(){
		if (this.updateTimerID == -1)
			this.updateTimerID = setTimeout("Cog.Input.History["+(this.index)+"].update()", 200);
	}
}
Cog.TextBox = Class.create();
Cog.TextBox.prototype = Object.extend(new Cog.Input(), {
	//コンストラクタ
	initialize: function(parent, element){
		this.index = Cog.Input.History.length;
		this.parent = parent;
		this.element = element;
		this.name = element.name;//名前
		this.value = null;//値
		
		Cog.Input.History.push(this);
		eval("var onchange = function(){ Cog.Input.History["+(this.index)+"].setUpdateTimer(); }");
		element.onchange = onchange;
		element.onblur = onchange;
	},
	//更新
	updateValue: function(){
		var old = this.value;
		var now = this.element.value;
		this.value = now;
		return (old != now);
	},
	updateControl: function(){
		var validated = this.validated();
		var enabled = this.enabled();
		this.element.disabled = !enabled;
		this.element.style.backgroundColor = !enabled?"#EEEEEE":(validated?"#FFFFFF":"#FFDDDD");
	},
	//検証
	is: function(str){ return (this.value==str ) },
	isEmpty: function(){ return (this.value=="") },
	isNotEmpty: function(){ return (this.value!="") }
});
Cog.RadioButton = Class.create();
Cog.RadioButton.prototype = Object.extend(new Cog.Input(), {
	//コンストラクタ
	initialize: function(parent, inputName){
		this.index = Cog.Input.History.length;
		this.parent = parent;
		this.name = inputName;//名前
		this.value = null;//値
		this.elements = parent.form.getInputs('radio', inputName);
		
		Cog.Input.History.push(this);
		eval("var onchange = function(){ Cog.Input.History["+(this.index)+"].setUpdateTimer(); }");
		for (i=0; i<this.elements.length; i++){
			this.elements[i].onclick = onchange;
		}
	},
	//更新
	updateValue: function(){
		var old = this.value;
		var now;
		for (i=0; i<this.elements.length; i++)
			if (this.elements[i].checked)
				now = this.elements[i].value;
		this.value = now;
		return (old != now);
	},
	updateControl: function(){
		var validated = this.validated();
		var enabled = this.enabled();
		for (i=0; i<this.elements.length; i++){
			this.elements[i].disabled = !enabled;
			this.elements[i].style.backgroundColor = (validated?"":"#FFDDDD");
		}
	},
	//検証
	is: function(str){ return (this.value==str ) }
});
Cog.TextArea = Class.create();
Cog.TextArea.prototype = Object.extend(new Cog.Input(), {
	//コンストラクタ
	initialize: function(parent, element){
		this.index = Cog.Input.History.length;
		this.parent = parent;
		this.element = element;
		this.name = element.name;//名前
		this.value = null;//値
		
		Cog.Input.History.push(this);
		eval("var onchange = function(){ Cog.Input.History["+(this.index)+"].setUpdateTimer(); }");
		element.onchange = onchange;
		element.onblur = onchange;
	},
	//更新
	updateValue: function(){
		var old = this.value;
		var now = this.element.value;
		this.value = now;
		return (old != now);
	},
	updateControl: function(){
		var validated = this.validated();
		var enabled = this.enabled();
		this.element.disabled = !enabled;
		this.element.style.backgroundColor = !enabled?"#EEEEEE":(validated?"#FFFFFF":"#FFDDDD");
	},
	//検証
	is: function(str){ return (this.value==str ) },
	isEmpty: function(){ return (this.value=="") },
	isNotEmpty: function(){ return (this.value!="") }
});
