function getSelectedCountryOption(object_name)
{
    return $(object_name + '_country_code').options[$(object_name + '_country_code').selectedIndex];
}
function getSelectedCountryCode(object_name)
{
    return getSelectedCountryOption(object_name).value;
}
function getSelectedCountryName(object_name)
{
    return getSelectedCountryOption(object_name).innerHTML;
}

/* Province functions */
function getSelectedProvinceOption(object_name)
{
    return $(object_name + '_province_code').options[$(object_name + '_province_code').selectedIndex];
}
function getSelectedProvinceCode(object_name)
{
    return getSelectedProvinceOption(object_name).value;
}
function getSelectedProvinceName(object_name)
{
    return getSelectedProvinceOption(object_name).innerHTML;
}

/* State functions */
function getSelectedStateOption(object_name)
{
    return $(object_name + '_state_code').options[$(object_name + '_state_code').selectedIndex];
}
function getSelectedStateCode(object_name)
{
    return getSelectedStateOption(object_name).value;
}
function getSelectedStateName(object_name)
{
    return getSelectedStateOption(object_name).innerHTML;
}

/* General Location */
function disableLocationDropdowns(object_name)
{
    $(object_name + '_country_code').disable();
    $(object_name + '_province_code').disable();
    $(object_name + '_state_code').disable();
}
function enableLocationDropdowns(object_name)
{
    $(object_name + '_country_code').enable();
    $(object_name + '_province_code').enable();
    $(object_name + '_state_code').enable();
}


function disableSubmit(object_name)
{
    $(object_name + '_submit_btn').disable();
}
function enableSubmit(object_name)
{
    $(object_name + '_submit_btn').enable();
}

/* Decides whether or not to show states/provinces */
function hideStatic(object_name)
{
    $(object_name + '_live_static').hide();
}
function showStatic(object_name)
{
    $(object_name + '_live_static').hide();
}
function setStaticName(object_name, value)
{
    $(object_name + '_live_static_name').update(value);
}
function getLiveNameValue(object_name)
{
    var live_name = $(object_name + '_live_name');
    return live_name.value;
}
function setLiveStaticName(object_name)
{
    $(object_name + '_live_static').show();
    $(object_name + '_live_static_name').update($(object_name + '_live_name').value);
    $(object_name + '_submit_btn').enable();
}
function disableLiveAuto(object_name)
{
    $(object_name + '_leave_blank').hide();
    $(object_name + '_live_name').disable();
    $(object_name + '_live_static').hide();
    $(object_name + '_suggestion_box').hide();
}
function enableLiveAuto(object_name)
{
    var shortname = getStaticName(object_name);
    $(object_name + '_region_name').update(shortname);
    $(object_name + '_live_static_name').update(shortname);
    $(object_name + '_leave_blank').show();
    $(object_name + '_live_auto').show();
    $(object_name + '_live_name').value = '';
    $(object_name + '_live_name').enable();
    $(object_name + '_live_static').hide();
    $(object_name + '_suggestion_box').hide();
    $(object_name + '_suggest_button').show();
    $(object_name + '_submit_btn').disable();
    enableLocationDropdowns(object_name);
}
function updateLeaveBlank(object_name, region_name)
{
    $(object_name + '_leave_blank').show();
    $(object_name + '_region_name').update(region_name);
}


function showRegions(object_name)
{
    var country_code = getSelectedCountryCode(object_name);
    var provinces = $(object_name + '_provinces');
    var states = $(object_name + '_states');
    /* Update the "leave blank" message */
    
    switch (country_code)
    {
    case 'CA':
        /* Canada */
        provinces.show();
        states.hide();
        disableLiveAuto(object_name);
        break;
    case 'US':
        /* USA */
        states.show();
        provinces.hide();
        disableLiveAuto(object_name);
        break;
    default:
        /* Hide both */
        provinces.hide();
        states.hide();
        enableLiveAuto(object_name);
        break;
    }
}

function getStaticName(object_name)
{
    var country_code = getSelectedCountryCode(object_name);
    var country_name = getSelectedCountryName(object_name);

    switch (country_code)
    {
    case 'CA':
        /* Canada */
        var province_name = getSelectedProvinceName(object_name);
        var shortname = province_name + ', ' + country_name;
        break;
    case 'US':
        /* USA */
        var state_name = getSelectedStateName(object_name);
        var shortname = state_name + ', ' + country_name;
        break;
    default:
        var shortname = country_name;
        break;
    }
    return shortname;
}

function getStaticParentName(object_name)
{
    var country_code = getSelectedCountryCode(object_name);

    switch (country_code)
    {
    case 'CA':
        /* Canada */
        var parent_name = getSelectedProvinceName(object_name);
        break;
    case 'US':
        /* USA */
        var parent_name = getSelectedStateName(object_name);
        break;
    default:
        var parent_name = getSelectedCountryName(object_name);
        break;
    }
    return parent_name;
}

function resetRegions(object_name) {
    $(object_name + '_province_code').selectedIndex = 0;
    $(object_name + '_state_code').selectedIndex = 0;
    $(object_name + '_hidden_province_code').value = '';
    $(object_name + '_hidden_state_code').value = '';
}
function hideRegions(object_name) {
    $(object_name + '_provinces').hide();
    $(object_name + '_states').hide();
}
/* Sets the country, region, and location to empty */
function resetCountry(object_name) {
    $(object_name + '_country_code').selectedIndex = 0;
    $(object_name + '_hidden_country_code').value = '';
    resetRegions(object_name);
}


function hideSuggestionBox(object_name)
{
    resetRegions(object_name);
    enableLiveAuto(object_name);
    $(object_name + '_country_code').focus();
    $(object_name + '_suggestion_box').hide();
}
function showSuggestionBox(object_name) {
    disableLocationDropdowns(object_name);
    $(object_name + '_live_static_name').show();
    $(object_name + '_suggestion_box').show();
    $(object_name + '_live_name').value = '';
    $(object_name + '_live_auto').hide();
    $(object_name + '_submit_btn').enable();
    $(object_name + '_country_code').focus();
    $(object_name + '_location_box').visualEffect('highlight', {duration: 2});
}

function focusRegion(object_name)
{
    var country_code = getSelectedCountryCode(object_name);
    /* Update the "leave blank" message */
    
    switch (country_code)
    {
    case 'CA':
        /* Canada */
        $(object_name + '_province_code').focus();
        break;
    case 'US':
        /* USA */
        $(object_name + '_state_code').focus();
        break;
    }
}
function chooseCountry(object_name)
{
    var country_code = getSelectedCountryCode(object_name);
    $(object_name + '_hidden_country_code').value = country_code;
    /* Update the "leave blank" message */
    
    resetRegions(object_name);
    switch (country_code)
    {
    case 'CA':
        /* Canada */
    case 'US':
        /* USA */
        showRegions(object_name);
        focusRegion(object_name);
        break;
    default:
        /* Hide both */
        hideRegions(object_name);
        enableLiveAuto(object_name);
        $(object_name + '_leave_blank').show();
        $(object_name + '_suggest_button').show();
        break;
    }
}

function changeRegion(object_name)
{
    var country_code = getSelectedCountryCode(object_name);
    /* Update the "leave blank" message */
    
    switch (country_code)
    {
    case 'CA':
        /* Canada */
        var province_code = getSelectedProvinceCode(object_name);
        $(object_name + '_hidden_province_code').value = province_code;
        break;
    case 'US':
        /* USA */
        var state_code = getSelectedStateCode(object_name);
        $(object_name + '_hidden_state_code').value = state_code
        break;
    }
    enableLiveAuto(object_name);
    $(object_name + '_live_name').value = '';
    $(object_name + '_submit_btn').disable();
    $(object_name + '_leave_blank').show();
    $(object_name + '_suggest_button').show();
    $(object_name + '_live_name').focus();
    
}

function focusName(object_name)
{
    $(object_name + '_'+ (['picture','place','suggestion'].indexOf(object_name) == '-1' ? 'title' : 'name')).focus();
}
function onChooseLocation(object_name, value)
{
    var shortname = value + ', ' + getStaticParentName(object_name);
    $(object_name + '_live_static_name').update(shortname);
    $(object_name + '_live_auto').hide();
    $(object_name + '_live_static').show();
    $(object_name + '_live_static_name').show();
    focusName(object_name);
    $(object_name + '_submit_btn').enable();
    disableLocationDropdowns(object_name);
}
function chooseRegion(object_name)
{
    var shortname = getStaticName(object_name);
    $(object_name + '_live_static_name').update(shortname);
    $(object_name + '_live_auto').hide();
    $(object_name + '_live_static').show();
    $(object_name + '_live_static_name').show();
    $(object_name + '_live_name').value = '';
    focusName(object_name);
    $(object_name + '_submit_btn').enable();
    disableLocationDropdowns(object_name);
}
function showLocationBox(object_name)
{
    enableLocationDropdowns(object_name);
    $(object_name + '_live_auto').show();
    $(object_name + '_suggestion_box').hide();
    $(object_name + '_suggestion_name').value='';
    $(object_name + '_location_box').visualEffect('highlight', {duration: 2});
    $(object_name + '_submit_btn').disable();
    $(object_name + '_suggestion_name').focus();
}