SERVICE = 212775;

function show(node) {
    node.style.display = '';
    return node;
}

function hide(node) {
    node.style.display = 'none';
    return node;
}

function update(node, text) {
    node.appendChild(document.createTextNode(text));
    return node;
}

function clear(node) {
    while (node.hasChildNodes()) {
        node.removeChild(node.firstChild);
    }
    return node;
}

function $(id) { return document.getElementById(id) }

function show$(id) { return show($(id)) }

function hide$(id) { return hide($(id)) }

function update$(id, text) { return update($(id), text) }

function clear$(id) { return clear($(id)) }

function updateInstructions(data) {
    show$('instructions');
    update(clear$('message_text'), (data.rewrite==""?[data.prefix, SERVICE]:[data.rewrite]).join(' ') + PARTNER);
    update(clear$('shortcode'), [data.number]);
    //update(clear$('message_cost'), [(data.price/30).toFixed(2), data.currency,
    //    (parseInt(data.vat)? '(ñ ÍÄÑ)': '(áåç ÍÄÑ)')].join(' '));
    //if (data.special) show(update(clear$('notes'), data.special));
    //else hide(clear$('notes'));
}

function selectRussia() {
    var select_country = $('select_country');
    
    for (var i=0; i < select_country.options.length; i++)
    {
        var country = select_country.options[i];
        
        if (country.text == "Ðîññèÿ")
        {
            country.selected = true;
            selectCountry(country.value);
        }
    }
}

function selectBeeline() {
    var select_provider = $('select_provider'); 
    
    for (var i=0; i < select_provider.options.length; i++)
    {
        var provider = select_provider.options[i];
        
        if ((provider.text == "ÌÒÑ") || (provider.text == "Azercell"))
        {
            provider.selected = true;
            selectProvider(provider.value);
        }
    }
}

function selectProvider(i) {
    if (i == '-') {
        hide$('instructions');
        return;
    }
    updateInstructions(DATA.providers[i]);
}

function selectCountry(i) {
    if (i == '-') {
        hide$('select_provider');
        hide$('instructions');
        return;
    }
    if (JSONResponse[i].providers && JSONResponse[i].providers.length) {
        hide$('instructions');
        show$('select_provider');
        DATA = JSONResponse[i];
        var select_provider = clear$('select_provider');
        for (var j = 0; j < DATA.providers.length; ++j) {
            var opt = document.createElement('option');
            update(opt, DATA.providers[j].name).value = j;
            select_provider.appendChild(opt);
        }
        select_provider.onchange = function() {
            selectProvider(this.value);
        }
        
        selectBeeline();
    }
    else {
        hide$('select_provider');
        updateInstructions(JSONResponse[i]);
    }
}

function JSONHandleResponse() {
    if (!window.JSONResponse) {
        show$('fail');
        return;
    }
    show$('ui');
    var select_country = $('select_country');
    for (var i = 0; i < JSONResponse.length; ++i) {
        var opt = document.createElement('option');
        update(opt, JSONResponse[i].country_name).value = i;
        select_country.appendChild(opt);
    }
    select_country.onchange = function() {
        selectCountry(this.value);
    }
    
    selectRussia();
    
    document.forms[0].sms_key.focus(); // ïîëå ââîäà ïàðîëÿ èç ñìñ
}

window.onload = JSONHandleResponse;
