/**
 * Class for form field properties, extends property
 */
PACIFICFOX.FormProperty = function( arg ) { 
	/**
	 * Inheritance, not to be confused with true inheritance in a OOP language, note: doesn't allow overriding of base methods
	 */
	this.base = new Property( arg );
	for ( myFunction in this.base ) {
		this[ myFunction ] = this.base[ myFunction ];
	}

	/**
	 * Private properties
	 */
	var form; // required, a reference to the form object
	
	/**
	 * Public properties
	 */
	this.onerror = ( typeof arg.onerror == "undefined" ? null : arg.onerror );
	this.onvalidate = ( typeof arg.onvalidate == "undefined" ? null : arg.onvalidate );

	if ( typeof arg.form != "undefined" ) {
		if ( typeof arg.form != "object" || arg.form == null ) {
			alert( "value of form is not of type object, or null, please provide an object reference to the form" );
		}
		form = arg.form;
	} else {
		alert( "form is a required argument, please provide reference to the form object" );
	}

	/**
	 * Priviliged methods, may be invoked publicly and may access private variables
	 */
	this.isChecked = function() {
		if ( this.getObjectType() != "radio" && this.getObjectType() != "checkbox" ) {
			alert( "You cannot call isChecked() on a object other than a radio button or check box, this object is of type " + this.getObjectType() );
		}
		var result = false;
		var myObject = this.getObject();
		for ( var i = 0; i < myObject.length; i++ ) {
			if ( myObject[ i ].checked ) {
				result = true;
				break;
			}
		}
		return result;
	}
	this.getForm = function() {
		return form;
	}
}

/**
 * Public methods
 * getObjectType returns the type of the actual object
 * possible values are: text, checkbox, radio, select, textarea
 */
PACIFICFOX.Property.prototype.getObjectType = function() { 
	var myObject = this.getObject();
	// **** it is assumed that multiple objects are of the same type
	return myObject[ 0 ].type;
};

PACIFICFOX.Property.prototype.getObject = function() { 
	var result = new Array();
	var form = this.getForm();
	for ( var i = 0; i < form.elements.length; i ++ ) {
		if ( form.elements[ i ].name == this.getName() ) {
			result.push( form.elements[ i ] );
		}
	}
	if ( result.length == 0 ) {
		alert( "Could not find the property name: " + this.getName() + " in the form" );
	}
	return result;
};
PACIFICFOX.Property.prototype.setValueFromObject = function() { 
	this.setValue( { value:this.getObjectValue() } );
}
PACIFICFOX.Property.prototype.getObjectValue = function() { 
	// It would have been nice if we could have overwritten the getValue method in the base class
	var result = new String();
	switch ( this.getObjectType() ) {
		case "text": {
			var myObject = this.getObject();
			result = myObject[ 0 ].value;
			break;
		}
		case "file": {
			var myObject = this.getObject();
			result = myObject[ 0 ].value;
			break;
		}
		case "password": {
			var myObject = this.getObject();
			result = myObject[ 0 ].value;
			break;
		}
		case "textarea": {
			var myObject = this.getObject();
			result = myObject[ 0 ].value;
			break;
		}
		case "select-one": {
			var myObject = this.getObject();
			result = myObject[ 0 ].options[ myObject[ 0 ].selectedIndex ].value;
			break;
		}
		// select multiple can be multiple values
		case "select-multiple": {
			var myObject = this.getObject();
			result = new Array();
			for ( var i = 0; i < myObject[ 0 ].options.length; i++ ) {
				if ( myObject[ 0 ].options[ i ].selected ) {
					result.push( myObject[ 0 ].options[ i ].value );
				}
			}
			result.toString();
			break;
		}
		case "radio": {
			var myObject = this.getObject();
			for ( var i = 0; i < myObject.length; i++ ) {
				if ( myObject[ i ].checked ) {
					result = myObject[ i ].value;
					break;
				}
			}
			break;
		}
		// checkbox can be multiple values
		case "checkbox": {
			var myObject = this.getObject();
			result = new Array();
			for ( var i = 0; i < myObject.length; i++ ) {
				if ( myObject[ i ].checked ) {
					result.push( myObject[ i ].value );
				}
			}
			result.toString();
			break;
		}
		default: {
			alert( "An unknown object of type " + this.getObjectType() + " encountered" );
			return false;
		}
	}
	return result;
};