function resetDTSForm( selectregion, selectcountry, selectwhen, button ) {
    selectregion.set('value', 0);
    selectcountry.set('value', 0);
    selectcountry.set('disabled', 'disabled');
    selectwhen.set('value', 0);
    button.set('disabled', 'disabled');
}
function fetchCountry( currentRegionValue, selectregion, selectcountry, selectwhen, button ) {
    if( currentRegionValue.get('value') != 0 ) {
        jsonurl = currentRegionValue.get('value');
        URItoFIle = new URI(document.id( document.body ).getElements( 'script[src*=nxcdtssearch.js]' ).get('src'));
        jsonurltosystem = URItoFIle.get('data');
        jsonurltosystem = jsonurltosystem['url'];
        jsonurl = jsonurltosystem + '/' + jsonurl;
        jsonRequest = new Request.JSON({url: jsonurl, onSuccess: function(xml, json){
            regions = JSON.decode(json);
            selectcountry.set('html', '<option selected="selected" value="0">Country</option>');
            selectcountry.set('disabled', 'disabled');
            regions.each(function(item){
                selectcountry.set('html', selectcountry.get('html') + '<option value="' + item.NodeID + '">' + item.Name + '</option>');
            });
            selectcountry.removeProperty('disabled');
        }}).get({});
        selectwhen.set('value', '0');
        button.removeProperty('disabled');

    }
    else {
        selectcountry.set('disabled', 'disabled');
        selectcountry.set('html', '<option selected="selected" value="0">Country</option>');
        selectwhen.removeProperty('disabled');
        selectwhen.set('value', '0');
        button.set('disabled', 'disabled');
    }
}

window.addEvent('domready', function(){
    selectregion = document.id('dts-region-select');
    selectcountry = document.id('dts-country-select');
    selectwhen = document.id('dts-when-select');
    button = document.id('search-button-dts');
    resetDTSForm( selectregion, selectcountry, selectwhen, button );

    selectregion.addEvent('change', function(){
        fetchCountry( this, selectregion, selectcountry, selectwhen, button );
    });

    selectwhen.addEvent('change', function(){
        if( ((selectregion.get('value') == 0) && (selectcountry.get('value') == 0) && (this.get('value') != 0)) ||
          (selectregion.get('value') != 0) ) {
            button.removeProperty('disabled');
        }
        else {
            button.set('disabled', 'disabled');
        }
    });
});