/**
* HGForm
*
* Organizes a collection of HGFormElements into a single form object
*
**/
function HGForm() { 

	//generate the DOM form element
	this.form = document.createElement( 'form' );

	//associate the form element with the HGForm object
	this.form.associatedHGForm = this;

	//create an associative array of HGFormElements which have been inserted into the HGForm
	this.form_element_array = new Object();

}

new HGForm();

/**
* HGForm.addHGFormElement
*
* Add an HGFormElement to the DOM object and the form array
*
* @param f: The form element to add
**/
HGForm.prototype.addHGFormElement = function( f ) { 

	//create a div for the new form element
	var nfdiv = document.createElement( 'div' );

	//attach a tool tip if one is defined for the form element
	if ( f.tool_tip_content && f.tool_tip_content != null ) { 
		var tt_img = document.createElement( 'img' );
		tt_img.src = 'img/page/form/tooltip_sm.gif';
		tt_img.title = f.tool_tip_content;
		nfdiv.appendChild( tt_img );
	}

	//if a verbose description is defined for the form element, attach it as the element text, else use the name
	if ( f.verbose_description && f.verbose_description != null ) { 
		nfdiv.appendChild( textToDOM( "span", " " + f.verbose_description + ": " ) );
	}
	else { 
		nfdiv.appendChild( textToDOM( "span", f.name + ": " ) );
	}

	//make sure that the form_element_array does not already contain an element with the same name
	if ( this.form_element_array[ f.name ] && this.form_element_array[ f.name ] != null ) 
		throw ( "HGForm->addHGFormElement: form_element_array already contains a member with name " + f.name );

	//add the form_element to the form_element_array
	this.form_element_array[ f.name ] = f;

	//add the DOM representation of the form_element to the DOM form
	nfdiv.appendChild( f.form_element );
	this.form.appendChild( nfdiv );

	//associate the form with the form element
	f.associatedHGForm = this;

	return true;

}

/**
* HGForm.removeHGFormElement
*
* Remove a particular form element from the form_element_array and the DOM form.  
* NOTE: If the form_element is not present in the HGForm object, no exception will be thrown but the function will return false.
*
* @param f: The HGFormElement to be removed
**/
HGForm.prototype.removeHGFormElement = function( f ) { 

	if ( this.form_element_array[ f.name ] && this.form_element_array[ f.name ] != null ) { 

		this.form.removeChild( f.form_element );
		this.form_element_array[ f.name ] = null;

		//remove the association between the form and the form element
		f.associatedHGForm = null;

		return true;

	}
	
	return false;

}

/**
* HGForm.replaceHGFormElementIfPresent
*
* Swaps one HGFormElement with another HGFormElement if the HGFormElement is present in the HGForm.  If the HGFormElement is not present, 
* the HGFormElement is simply added to the HGForm.
*
* @param newf: The HGFormElement being swapped in
* @param oldf: The HGFormElement which is being swapped out
**/
HGForm.prototype.replaceHGFormElementIfPresent = function( newf, oldf ) { 

	//first we check to make sure the new form_element is not already in the HGForm
	if ( this.form_element_array[ newf.name ] && this.form_element_array[ newf.name ] != null )  
		throw ( "HGForm->swapHGFormElementIfPresent: form_element_array already contains a member with name " + newf.name );

	if ( this.form_element_array[ oldf.name ] && this.form_element_array[ oldf.name ] != null ) { 

		this.form.replaceChild( newf.form_element, oldf.form_element );
		this.form_element_array[ oldf.name ] = null;
		this.form_element_array[ newf.name ] = newf;

		//add an association between the form and the form element to the new form element and remove from the old
		newf.associatedHGForm = this;
		oldf.associatedHGForm = null;

		return true;

	}
	else { 
		
		return this.addHGFormElement( newf );

	}

	return false;

}

/**
* HGForm.getHGFormElement
*
* Returns the form element indexed by the form element name passed in
*
* @param name The name of the form element to retrieve
**/
HGForm.prototype.getHGFormElement = function( name ) { 

	if ( this.form_element_array[name] ) { 
		return this.form_element_array[name];
	}
	return false;

} 


