//Class represents a group of dropdown lists / radio buttons that changes the value of the group
//based on the member that has changed.

//NOTE: JTS 10/09/2007
//For the sake of time the more elegant, OO-like inheritance model in JavaScript was not implemented.
var debugDropDownNonMandUnitFlag = false;

/*
*/  
/*  	
/// <author>John Shewchuk</author>
/// <date>10/09/2007</date>
/// <summary>
/// This function adds a set of data and creates a matchAcrossUnitSibling object that represents this 
/// group of coverage item controls for client side scripting.
/// </summary>
/// <remarks>Generally called by a wrapper function in the common </remarks>
*/
function dropDownTargetNonMand_AddTargetDrownDownSibling(rdoYesNoIDItem, ddlSiblingIDItem, vehicleIndexItem, enableValidatorFunctionPointer, disableValidatorFunctionPointer)
{
    if(debugDropDownNonMandUnitFlag)
    {
        alert("In dropDownTargetNonMand_AddTargetDrownDownSibling");
    }
    
    if (this.targetDropDownSiblings == null)
    {
        //This is the first addition into the collection, initialize the
        //collection and add the value
        this.targetDropDownSiblings = new Array(1);
        this.targetDropDownSiblings[0] = new matchAcrossUnitSibling(rdoYesNoIDItem, ddlSiblingIDItem, vehicleIndexItem, enableValidatorFunctionPointer, disableValidatorFunctionPointer);
    }
    else  
    {
        var unitTarget = null;
        var unit=null;
        
        //Search for the target name            
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            unit = this.targetDropDownSiblings[i];
            
            if (unit.rdoYesNoID == rdoYesNoIDItem)
            {
                unitTarget = unit
            }
        }
        
        //Did not find the name, so we add the child target name.
        if (unitTarget ==null)
        {
            this.targetDropDownSiblings.push(
            new matchAcrossUnitSibling(rdoYesNoIDItem, ddlSiblingIDItem, vehicleIndexItem, enableValidatorFunctionPointer, disableValidatorFunctionPointer));
        }
        
    }
    
    //alert(this.targetDropDownSiblings.length);
    
            
}

/*  	
/// <author>John Shewchuk</author>
/// <date>10/09/2007</date>
/// <summary>
/// This function sets the data for a group of matchAcrossUnitSibling objects to the
/// actively updated value.
/// </summary>
/// <remarks>Generally called by a wrapper function in the common </remarks>
*/
function dropDownTargetNonMand_SetDropDownsToSelectedIndex(sender, selectedIndex, ignoreRadioUnitEnabled)
{
    var processUnitOnRadioUnit = false;
    
    if(debugCommonFlag)
    {
        alert("dropDownTargetNonMand_SetDropDownsToSelectedIndex fired");
    }
    
    if (this.targetDropDownSiblings != null)
    {   
        //Check to see if the sender is enabled because the sender
        //should only be changing values if it is enabled because a 
        //reset could change the box to the default index, causing other
        //values to be lost.
        
        //Iterate over the collection of objects to check and see if        
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            //Set the name of the drop down control to be updated
            unit = this.targetDropDownSiblings[i];
            
            var onChangePointer=null;
            var ddlSibling=null;
            
            if(debugDropDownNonMandUnitFlag)
	        {
                alert("Sender: " + sender.id);            
                alert("unit ID: " + unit.ddlSiblingID);
            }
            
            /*
                NOTE: JTS 10/26/2007
                ASP.NET does not render controls that are invisible.  Bypasses the check
                because the assumption is they are enabled
            */            
            if(ignoreRadioUnitEnabled | unit.isRadioUnitEnabled())
            {processUnitOnRadioUnit = true;}
            
            //Final check to make sure we do not update the value we 
            //are listening to a different value, causing an in advertant
            //circular reference
            if((unit.ddlSiblingID != sender.id) & (processUnitOnRadioUnit))
            {	
                //Retreive the sibling value to be updated to that of the sender
                ddlSibling = document.getElementById(unit.ddlSiblingID);
                
                if(ddlSibling != null)
                {
                    //Get the original
                    onChangePointer = ddlSibling.onchange;
                            
                    //Remove the "OnSelectedIndexChange" function pointer
                    ddlSibling.onchange = null;
                
                    if(debugDropDownNonMandUnitFlag)
	                {
                        alert("SetDropDown - Name: " + unit.ddlSiblingID);
                    }
                    
                    //TODO: JTS 10/10/2007
                    //Need to place a catch block to ensure that the pointers
                    //are reset
                    
                    try
                    {
                        //Calls the common method to change a drop down.
                        setDropDownList(sender, unit.ddlSiblingID, selectedIndex);
                    }
                    catch(exception)
                    {
                        alert("Exception occured setting the Drop Down: " + exception);         
                    }
                    
                    //Reset the "OnSelectedIndexChange" function pointer
                    ddlSibling.onchange = onChangePointer;
                    onChangePointer = null;
                }
            }
        }
    }
        
}

/*  	
/// <author>John Shewchuk</author>
/// <date>10/09/2007</date>
/// <summary>
/// This method sets the recently activated Coverage Item to that of it's siblings, in particular
/// the first vehicle number available that is already active.
/// </summary>
/// <remarks>Generally called by a wrapper function in the common </remarks>
*/
function dropDownTargetNonMand_UpdateValueToMinVehicleNumber(sender, selectedIndex)
{
    var unitToUpdate = null;
    var unitMinVehicle = null;
    var ddlMinVehicle = null;
    
    //alert("dropDownTargetNonMand_SetDropDownsToSelectedIndex");
    if (this.targetDropDownSiblings != null)
    {   

        //Get the objects to be updated
        unitToUpdate = this.GetUnitByRadioID(sender.id);
        /*
        NOTE: JTS 10/15/07
        Changed this to get the first vehicle because the requirements changed
        where the non mandatory coverages are always set to the first unit
        */
        //unitMinVehicle = this.GetMinVehicleNumber(true, sender.id);      
        unitMinVehicle = this.GetMinVehicleNumber(false, null);      
        
        if( (unitToUpdate !=null) & (unitMinVehicle !=null))
        {
            ddlMinVehicle = document.getElementById(unitMinVehicle.ddlSiblingID);
            
            if(debugDropDownNonMandUnitFlag)
            {
                alert("UpdateValueToMinVehicleNumber - MIN Name: " + unitMinVehicle.ddlSiblingID);
            }
                        
            if(ddlMinVehicle !=null)
            {
                setDropDownList(sender, unitToUpdate.ddlSiblingID, ddlMinVehicle.selectedIndex);                
            }
            
        }      
       
    }
        
}

/*  	
/// <author>John Shewchuk</author>
/// <date>10/09/2007</date>
/// <summary>
/// This method gets the vehicle with that has the lowest vehicle number.
/// If the isMinUnitEnabledFlag is set, the Coverage must be enabled
/// If the radioIDToExclude is set, the passed in coverage cannot be included
/// </summary>
/// <remarks>Generally called by a wrapper function in the common </remarks>
*/
function dropDownTargetNonMand_GetMinVehicleNumber(isMinUnitEnabledFlag, radioIDToExclude)
{        
    var matchAcrossObjMin=null;
    
    //alert("dropDownTargetNonMand_SetDropDownsToSelectedIndex");
    if (this.targetDropDownSiblings != null)
    {   
        //Check to see if the sender is enabled because the sender
        //should only be changing values if it is enabled because a 
        //reset could change the box to the default index, causing other
        //values to be lost.
        
        //Iterate over the collection of objects to check and see if 
    
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            //Set the name of the drop down control to be updated
            matchAcrossObjI = this.targetDropDownSiblings[i];
            
            //If there is only one value in the collection we 
            //want to set the value to something
            if(matchAcrossObjMin == null)
            {
                if (!isMinUnitEnabledFlag | matchAcrossObjI.isRadioUnitEnabled())
                {
                    if (radioIDToExclude == null | radioIDToExclude != matchAcrossObjI.rdoYesNoID)
                    {
                        //A) It does not matter if the I object is enabled or 
                        //disabled if isMinUnitEnabledFlag is false
                        //B) Radio unit is enabled
                        matchAcrossObjMin = matchAcrossObjI;
                    }
                }
                
            }
            
            //Compare against the next item in the collection
            for (j = i + 1; j < this.targetDropDownSiblings.length; j++)
            {            
                //Final check to make sure we do not update the value we 
                //are listening to a different value, causing an in advertant
                //circular reference
                matchAcrossObjJ = this.targetDropDownSiblings[j];
                
                //If there is only one value in the collection we 
                //want to set the value to something
                if(matchAcrossObjMin == null)
                {
                    if (!isMinUnitEnabledFlag | matchAcrossObjJ.isRadioUnitEnabled())
                    {
                        if (radioIDToExclude == null | radioIDToExclude != matchAcrossObjJ.rdoYesNoID)
                        {
                            //A) It does not matter if the I object is enabled or 
                            //disabled if isMinUnitEnabledFlag is false
                            //B) Radio unit is enabled
                            matchAcrossObjMin = matchAcrossObjJ;
                        }
                    }
                    
                }
                else if(matchAcrossObjMin.vehicleIndex > matchAcrossObjJ.vehicleIndex)
                {
                    //The min value is greater than the next object  
                    //Check to see if the next object is enabled
                    if (!isMinUnitEnabledFlag | matchAcrossObjJ.isRadioUnitEnabled())
                    {
                        if (radioIDToExclude == null | radioIDToExclude != matchAcrossObjJ.rdoYesNoID)
                        {
                            //A) It does not matter if the I object is enabled or 
                            //disabled if isMinUnitEnabledFlag is false
                            //B) Radio unit is enabled
                            matchAcrossObjMin = matchAcrossObjJ;
                        }
                    }                                      
                
                }
                
            }//j
        }//i
    }//targetDropDownSiblings
    
        if(debugDropDownNonMandUnitFlag)
        {
            if (matchAcrossObjMin==null)
            {
                alert("matchAcrossObjMin is null");
            }
        }
    
    return matchAcrossObjMin;
        
}

function dropDownTargetNonMand_GetUnitByDropDownID(unitDropDownID)
{        
    var matchAcrossObjTarget=null;
    
    //alert("dropDownTargetNonMand_SetDropDownsToSelectedIndex");
    if (this.targetDropDownSiblings != null)
    {           
        //Iterate over the collection of objects to grab the desired
        //object     
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            //Set the name of the drop down control to be updated
            matchAcrossUnit = this.targetDropDownSiblings[i];
            
            if(matchAcrossUnit.ddlSiblingID == unitDropDownID)
            {
                matchAcrossObjTarget = matchAcrossUnit;
                break;
            }
            
            
        }//i
    }//targetDropDownSiblings
    
    return matchAcrossObjTarget;
        
}

function dropDownTargetNonMand_GetUnitByRadioID(unitRadioYesNoID)
{        
    var matchAcrossObjTarget=null;
    
    //alert("dropDownTargetNonMand_SetDropDownsToSelectedIndex");
    if (this.targetDropDownSiblings != null)
    {           
        //Iterate over the collection of objects to grab the desired
        //object     
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            //Set the name of the drop down control to be updated
            matchAcrossUnit = this.targetDropDownSiblings[i];
            
            if(matchAcrossUnit.rdoYesNoID == unitRadioYesNoID)
            {
                matchAcrossObjTarget = matchAcrossUnit;
                break;
            }
            
            
        }//i
    }//targetDropDownSiblings
    
    return matchAcrossObjTarget;
        
}


/*  	
/// <author>John Shewchuk</author>
/// <date>10/26/2007</date>
/// <summary>
/// This function sets the validators to enabled for a group of matchAcrossUnitSibling
/// </summary>
/// <remarks>Generally called by a wrapper function in the common </remarks>
*/
function dropDownTargetNonMand_EnableValidators(sender)
{
    var processUnitOnRadioUnit = false;
    
    if(debugCommonFlag)
    {
        alert("dropDownTargetNonMand_EnableValidators fired");
    }
    
    if (this.targetDropDownSiblings != null)
    {   
        //Check to see if the sender is enabled because the sender
        //should only be changing values if it is enabled because a 
        //reset could change the box to the default index, causing other
        //values to be lost.
        
        //Iterate over the collection of objects to check and see if        
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            //Set the name of the drop down control to be updated
            unit = this.targetDropDownSiblings[i];
            
            var onChangePointer=null;
            var ddlSibling=null;
            
            if(debugDropDownNonMandUnitFlag)
	        {
                alert("Sender: " + sender.id);            
                alert("unit ID: " + unit.ddlSiblingID);
            }
                       
            
            //Final check to make sure we do not update the value we 
            //are listening to a different value, causing an in advertant
            //circular reference
            if((unit.ddlSiblingID != sender.id))
            {	
                if(debugDropDownNonMandUnitFlag)
                {
                    alert(unit.enableValidator);
                }
                
                try
                {                  
                    unit.enableValidator();
                }
                catch(exception)
                {
                    alert("unit.enableValidator() failed!");
                    alert("Exception: " + exception.message);
                }
            }
        }
    }
        
}
/*  	
/// <author>John Shewchuk</author>
/// <date>10/26/2007</date>
/// <summary>
/// This function sets the validators to disable for a group of matchAcrossUnitSibling
/// </summary>
/// <remarks>Generally called by a wrapper function in the common </remarks>
*/
function dropDownTargetNonMand_DisableValidators(sender)
{
    var processUnitOnRadioUnit = false;
    
    if(debugCommonFlag)
    {
        alert("dropDownTargetNonMand_DisableValidators fired");
    }
    
    if (this.targetDropDownSiblings != null)
    {   
        //Check to see if the sender is enabled because the sender
        //should only be changing values if it is enabled because a 
        //reset could change the box to the default index, causing other
        //values to be lost.
        
        //Iterate over the collection of objects to check and see if        
        for (i = 0; i < this.targetDropDownSiblings.length; i++)
        {
            //Set the name of the drop down control to be updated
            unit = this.targetDropDownSiblings[i];
            
            var onChangePointer=null;
            var ddlSibling=null;
            
            if(debugDropDownNonMandUnitFlag)
	        {
                alert("Sender: " + sender.id);            
                alert("unit ID: " + unit.ddlSiblingID);
            }
                       
            
            //Final check to make sure we do not update the value we 
            //are listening to a different value, causing an in advertant
            //circular reference
            if((unit.ddlSiblingID != sender.id))
            {	
                if(debugDropDownNonMandUnitFlag)
                {
                    alert(unit.disableValidator);
                }
                
                try
                {                    
                    unit.disableValidator();
                }
                catch(exception)
                {
                    alert("unit.disableValidator() failed!");
                    alert("Exception: " + exception.message);
                }
            }
        }
    }
        
}

//Constructor
function dropDownTargetNonMand(targetKeyItem)
{
    //alert("Constructor - targetKeyItem: " + targetKeyItem);
    //alert("Constructor - this.targetKey: " + this.targetKey
    
    this.targetKey = targetKeyItem;
    this.targetDropDownSiblings = null;  //collection of children ids to update
    
    //Assign methods and functions to pointers.
    this.AddTargetDrownDownSibling = dropDownTargetNonMand_AddTargetDrownDownSibling;
    this.SetDropDownsToSelectedIndex = dropDownTargetNonMand_SetDropDownsToSelectedIndex;
    this.UpdateValueToMinVehicleNumber = dropDownTargetNonMand_UpdateValueToMinVehicleNumber;
    this.GetMinVehicleNumber=dropDownTargetNonMand_GetMinVehicleNumber;
    this.GetUnitByDropDownID = dropDownTargetNonMand_GetUnitByDropDownID;
    this.GetUnitByRadioID = dropDownTargetNonMand_GetUnitByRadioID;
    this.enableValidators = dropDownTargetNonMand_EnableValidators;
    this.disableValidators = dropDownTargetNonMand_DisableValidators;
}




	
