﻿function showHide(clientIDToShow, clientIDToHide)
{	
    if(clientIDToHide) $(clientIDToHide).hide();	
    if(clientIDToShow) $(clientIDToShow).show();   
}    

function EnableValidator(validatorId, enabled)
{
    validator = document.getElementById(validatorId);
    if(validator){
        validator.enabled = enabled;
        //ValidatorUpdateDisplay(validator);
    }
}

function DefaultOtherWeightInputs(expressionToDefault)
{        
    //click() other weight input radio buttons that are not already clicked (to prevent loops)
    $(expressionToDefault).not(':checked').click();
}

//Enables and disables validators and options depending on the intial 'checked' value of the control (driven off the IsMetric property).
//Also sets up the onChecked events.
function InitWeightInput(rbImperialID, rbMetricID, imperialOptionsID, metricOptionsID, rfvStonesID, rfvPoundsID, rfvKgID, revStonesID, revPoundsID, revKgID, weightTypeID, unitSelectedEnabled, defaultOtherWeightInputs)
{
    //Initialise
    if($('#'+rbImperialID).attr('checked') == true)
    {        
        EnableValidator(rfvStonesID,true);
        EnableValidator(revStonesID,true);
        EnableValidator(rfvPoundsID,true);
        EnableValidator(revPoundsID,true);
        EnableValidator(rfvKgID,false);
        EnableValidator(revKgID,false);
        showHide('#'+imperialOptionsID,'#'+metricOptionsID);
    }
    else if($('#'+rbMetricID).attr('checked') == true)
    {        
        EnableValidator(rfvStonesID,false);  
        EnableValidator(revStonesID,false);          
        EnableValidator(rfvPoundsID,false);
        EnableValidator(revPoundsID,false); 
        EnableValidator(rfvKgID,true);    
        EnableValidator(revKgID,true); 
        showHide('#'+metricOptionsID,'#'+imperialOptionsID);       
    }
    else 
    {   //Hide both options if no checkbox selected
        EnableValidator(rfvStonesID,false);  
        EnableValidator(revStonesID,false);          
        EnableValidator(rfvPoundsID,false);
        EnableValidator(revPoundsID,false); 
        EnableValidator(rfvKgID,false);    
        EnableValidator(revKgID,false);    
        showHide(null,'#'+imperialOptionsID);
        showHide(null,'#'+metricOptionsID);              
    }        
    
    //Hide Imperial/Metric selection
    if(unitSelectedEnabled == 'False'){
        showHide(null, '#'+weightTypeID);
    }
    
    //Checkbox click functions
    $('#'+rbImperialID).click(function(){                  
        EnableValidator(rfvStonesID,true);
        EnableValidator(revStonesID,true);
        EnableValidator(rfvPoundsID,true);
        EnableValidator(revPoundsID,true);
        EnableValidator(rfvKgID,false);
        EnableValidator(revKgID,false);
        showHide('#'+imperialOptionsID,'#'+metricOptionsID); 
        
        if(defaultOtherWeightInputs == 'True'){
            DefaultOtherWeightInputs("[id$='_rbImperial']");
        }
    });
	
    $('#'+rbMetricID).click(function(){        
        EnableValidator(rfvStonesID,false);       
        EnableValidator(revStonesID,false);      
        EnableValidator(rfvPoundsID,false);
        EnableValidator(revPoundsID,false); 
        EnableValidator(rfvKgID,true);
        EnableValidator(revKgID,true);
        showHide('#'+metricOptionsID,'#'+imperialOptionsID);  
        
        if(defaultOtherWeightInputs == 'True'){
            DefaultOtherWeightInputs("[id$='_rbMetric']");
        }            
    });    
}


function InitHeightInput(rbImperialID, rbMetricID, imperialOptionsID, metricOptionsID, rfvFeetID, rfvInchesID, rfvCmID, revFeetID, revInchesID, revCmID, heightTypeID, unitSelectedEnabled)
{
    if($('#'+rbImperialID).attr('checked') == true)
    {        
        EnableValidator(rfvFeetID,true);
        EnableValidator(revFeetID,true);
        EnableValidator(rfvInchesID,true);
        EnableValidator(revInchesID,true);
        EnableValidator(rfvCmID,false); 
        EnableValidator(revCmID,false); 
        showHide('#'+imperialOptionsID,'#'+metricOptionsID);          
        
    }
    else if($('#'+rbMetricID).attr('checked') == true)
    {          
        EnableValidator(rfvFeetID,false);
        EnableValidator(revFeetID,false);         
        EnableValidator(rfvInchesID,false); 
        EnableValidator(revInchesID,false);
        EnableValidator(rfvCmID,true);
        EnableValidator(revCmID,true);      
        showHide('#'+metricOptionsID,'#'+imperialOptionsID);     
    }
    else 
    {   //Hide both options if no checkbox selected
        EnableValidator(rfvFeetID,false);
        EnableValidator(revFeetID,false);         
        EnableValidator(rfvInchesID,false); 
        EnableValidator(revInchesID,false);
        EnableValidator(rfvCmID,false);
        EnableValidator(revCmID,false);     
        showHide(null,'#'+imperialOptionsID);
        showHide(null,'#'+metricOptionsID);              
    } 
    
    //Hide Imperial/Metric selection
    if(unitSelectedEnabled == 'False'){
        showHide(null, '#'+heightTypeID);
    }           
    
    //Checkbox click functions
    $('#'+rbImperialID).click(function(){                  
        EnableValidator(rfvFeetID,true);
        EnableValidator(revFeetID,true);
        EnableValidator(rfvInchesID,true);
        EnableValidator(revInchesID,true);
        EnableValidator(rfvCmID,false);
        EnableValidator(revCmID,false);
        showHide('#'+imperialOptionsID,'#'+metricOptionsID); 
    });
	
    $('#'+rbMetricID).click(function(){        
        EnableValidator(rfvFeetID,false); 
        EnableValidator(revFeetID,false);           
        EnableValidator(rfvInchesID,false); 
        EnableValidator(revInchesID,false);
        EnableValidator(rfvCmID,true);
        EnableValidator(revCmID,true);
        showHide('#'+metricOptionsID,'#'+imperialOptionsID);  
    });   
}
