// Syntax: FieldHider( "parentfieldname", childfieldarray )
//         parentfieldname: name of field that values are taken from
//         childfieldarray: array of value/fieldarray pairs ( i.e. "1", new Array( "field1name", "field2name" ) )
//                          if the value matches the parent value then the fields in the array are shown
// Example: var objFieldHider1 = new FieldHider( "intCounterSigns_40", new Array( "1", new Array("intCounterSigns_39"), "2", new Array("strCounterSigns_12","strCounterSigns_13","strCounterSigns_14","strCounterSigns_15","strCounterSigns_38") ) )
//          this example will show 1 field when the parent value equals 1, show 5 fields when the parent value equals 2, and hide all fields when the parent has any other value

FieldHider = Class.create();
FieldHider.prototype = {
	initialize: function ( strParentName, aryParams )
	{	
		if( document.getElementsByName( strParentName ).length == 0 ) return;
		
		this.aryParams = aryParams;
		this.objParent = document.getElementsByName( strParentName )[0];
		this.strParentType = this.objParent.type.toLowerCase();
		if( this.strParentType == "radio" || this.strParentType == "checkbox" )	{
			this.objParent = document.getElementsByName( strParentName );
		}
		
		// Check for blank option values
		this.blnNoValues = false;
		if( this.strParentType == "select-one" ) {
			if( this.objParent.length > 1 ) {
				this.blnNoValues = true;
				for( var i = 0; i < this.objParent.length; i++ ) {
					if( this.objParent.options[ i ].value != "" ) {
						this.blnNoValues = false;
					}
				}
			}
		}
		
		this.aryFields = new Array();
		for( var i=0; i < this.aryParams.length; i+=2 ) {
			for( var j=0; j < this.aryParams[i+1].length; j++ ) {
				var objField = null;
				if( document.getElementsByName( this.aryParams[i+1][j] ).length > 0 )
				{
					objField = document.getElementsByName( this.aryParams[i+1][j] )[0];
				}
				else {
					objField = $( this.aryParams[i+1][j] );
				}
				if( objField ) {
					this.aryFields.push( objField );
				}
			}
		}
		
		if( this.strParentType == "radio" || this.strParentType == "checkbox" )	{
			for( var i=0; i < this.objParent.length; i++ )
			{
				Event.observe(this.objParent[i], 'click', this.CheckFields.bindAsEventListener(this), false);
			}
		}
		else {
			Event.observe(this.objParent, 'change', this.CheckFields.bindAsEventListener(this), false);
		}
		
		this.CheckFields( );
	},

	SetDisplay : function( obj, strValue )
	{
		var intTagCount = 2;
		var strTag = "table";
				
		if( window.location.toString().toLowerCase().indexOf( "form.asp" ) >= 0 || window.location.toString().toLowerCase().indexOf( "formwcs01" ) >= 0 ) {
			strTag = "tr";
			intTagCount = 1;
		}
		else if( obj.tagName.toLowerCase() != "input" && obj.tagName.toLowerCase() != "textarea" ) {
			intTagCount = 1;
		}
		else if( obj.type.toLowerCase() == "radio" || obj.type.toLowerCase() == "checkbox" ) {
			intTagCount = 3;
		}
		
		do {
			if( obj.tagName ) {
				if( obj.tagName.toLowerCase() == strTag ) {
					intTagCount -= 1;
					if( intTagCount == 0 )
					{
						obj.style.display = strValue;
						break;
					}
				}
			}
		}
		while (obj = obj.parentNode);
	},
	
	CheckFields : function( e )
	{
		var strParentValue = "";
		var strComma = "";
		if( this.strParentType == "radio" || this.strParentType == "checkbox" ) {
			for( var i=0; i < this.objParent.length; i++ )
			{
				if( this.objParent[i].checked )
				{
					strParentValue += strComma + this.objParent[i].value;
					strComma = ", ";
				}
			}
		}
		else if( this.strParentType == "select-one" && this.blnNoValues ) {
			strParentValue = this.objParent.options[ this.objParent.selectedIndex ].innerHTML;	
		}
		else {
			strParentValue = this.objParent.value;
		}
		
		for( var i=0; i < this.aryFields.length; i++ )	{
			this.SetDisplay( this.aryFields[i], "none" );
		}
		
		for( var i=0; i < this.aryParams.length; i+=2 ) {
			if( this.aryParams[i] == strParentValue ) {
				for( var j=0; j < this.aryParams[i+1].length; j++ ) {
					var objField = null;
					if( document.getElementsByName( this.aryParams[i+1][j] ).length > 0 )
					{
						objField = document.getElementsByName( this.aryParams[i+1][j] )[0];
					}
					else {
						objField = $( this.aryParams[i+1][j] );
					}
					if( objField ) {
						this.SetDisplay( objField, "" );
					}
				}
			}
		}
	}
}