function NoState(objCountry)
{
    return (objCountry.value != COUNTRYID_USA) && (objCountry.value != COUNTRYID_CANADA);
}

function SetStateEnable(objCountry, objState)
{
    var fNoState = NoState(objCountry);

    objState.disabled = objCountry.disabled || fNoState;                    
    if (fNoState)
        objState.selectedIndex = 0;
}

function OnCountryChange(objCountry, objState)
{
    if (objState != null)
    {
        var fNoState = NoState(objCountry);

        objState.disabled = fNoState;                    
        objState.selectedIndex = 0;

        if (!fNoState)
        {
            while (objState.options.length > 1)
                objState.options[1] = null;//.remove(1);
                
            var rStates = (objCountry.value == COUNTRYID_USA) ? rStatesUSA : rStatesCanada;
            var i;
            
            for (i = 0; i < rStates.length; i++)
            {
                var rState = rStates[i].split(':');
                var opt    = new Option();
                
                opt.value = rState[0];
                opt.text  = rState[1];
                opt.StateAbrv = rState[2];
                objState.options[objState.options.length] = opt;
            }
        }
    }
}

function ValidateStateAndCountryEx(sPrefix, objCountry, objState, fRequired)
{            
    var fStateRequired = fRequired && ((objCountry.value == COUNTRYID_USA) || (objCountry.value == COUNTRYID_CANADA));
    
    if (sPrefix == "")
        sPrefix = " ";
    else
        sPrefix = " " + sPrefix + " ";
        
    if (CheckListValue(objState, !fStateRequired))
        AddError("Select a" + sPrefix + "state");
        
    if (CheckListValue(objCountry, !fRequired))
        AddError("Select a" + sPrefix + "country");
}

function ValidateStateAndCountry(objForm, fRequired)
{
    return ValidateStateAndCountryEx("", objForm.CountryID, objForm.StateID, fRequired);
}

function SelectListItem(objList, sText)
{
    var i;
    
    for (i = 0; i < objList.options.length; i++)
    {
        if ((objList.options[i].text == sText) || (objList.options[i].StateAbrv == sText))
        {
            objList.selectedIndex = i;
            break;
        }
    }
}
