﻿// AjaxFunctions.js

function ClearSelectElement(SelectElement) {
    while ( SelectElement.options.length > 0 ) {
        SelectElement.options[0] = null;
    }
}

// UpdateRegions_callback(RegionArray) - executes when the GetRegionArray Ajax method calls back.
// Updates the controls on the page with the new data.
function UpdateRegions_callback(RegionArray) {
    
    var Region = document.getElementById(RegionArray.value[0]);
    var OtherRegion = document.getElementById(RegionArray.value[1]);
    
    ClearSelectElement( Region );
    
    if(RegionArray.value.length > 2)
    {
        Region[0] = new Option(RegionArray.value[2],RegionArray.value[2],true);
        var newOption;
        var index = 0;
        for (index=3; index < RegionArray.value.length; index+=2) {
            newOption = new Option(RegionArray.value[index], RegionArray.value[index+1], false);
            Region.options.add(newOption);
        }
        OtherRegion.style.display = 'none';
        Region.style.display = '';
    }
    else
    {
        OtherRegion.style.display = '';
        Region.style.display = 'none';
    }
}

// UpdateRegions(regionListElementName, otherRegionElementName, countryCode) - Calls
// the GetRegionArray Ajax method
function UpdateRegions(regionElementName, otherRegionElementName, countryCode) {
    AjaxMethods.GetRegionArray(regionElementName, otherRegionElementName, countryCode, UpdateRegions_callback);
}

