
/* -------------------------------------------------------------------------- */
/* -------------------------------- Form ------------------------------------ */
/* -------------------------------------------------------------------------- */

Form = function( target ) {
	Form.superClass.apply( this, arguments );
	
	this.Id = 'Form';
	this.element = target;
	this.inputs = new Array();
	this.status = false;
}
Form.inheritsFrom( HTMLGlif );

/* -------------------------------------------------------------------------- */

Form.prototype.check = function() {
	var oS = this.status;
	
	this.status = true;
	for( var c in this.inputs ) {
		if( !this.inputs[c].check() ) {
			this.status = false;
		}
	}

	if( !( oS === this.status ) ) this.notify( 'formStatusChange' );
	return this.status;
}

Form.prototype.getInputsLength = function() {
	return this.inputs.length;
}

Form.prototype.setError = function( errorString, input ) {
	this.notify( 'formError', { obj: input, sErr: errorString });
}

Form.prototype.onInputChange = function( params, input ) {
	//if( input && input.status ) this.notify( 'formClearError', input );
	this.check();
}

Form.prototype.onInputDelete = function( params ) {}

Form.prototype.getParams = function( params ) {
	return false;
}

Form.prototype.getInput = function( idx ) {
	return this.inputs[ idx ] || false;
}

Form.prototype.setInput = function( template ) {
	var newInput = new TextInput();
	newInput.idx = this.inputs.length;
	this.inputs[ this.inputs.length ] = newInput;
	newInput.append( this.element );
	newInput.setTemplate( template );
	newInput.showTemplate( true );
	return newInput;
}

Form.prototype.removeInput = function( iIdx ) {
	iIdx = iIdx || false;
	if( typeof( iIdx ) == 'object' ) {
		for( var c in this.inputs ) {
			if( iIdx == this.inputs[ c ] ) {
				iIdx = c;
				break;
			}
		}
	}
	
	if( typeof( iIdx ) != 'number' && iIdx >= this.inputs.length ) return false;
	this.inputs[ iIdx ].remove();
	this.inputs.splice( iIdx, 1 );
}
