first_countries = Array('RU','UA','KZ'); // Страна (двух буквенный код страны) которая будет первой в списке

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

function easy_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 dollar(id) { return document.getElementById(id) }

function showdollar(id) { return easy_show(dollar(id)) }
//function showdollar(id) { return dollar(id).show }

function hidedollar(id) { return easy_hide(dollar(id)) }
//function hidedollar(id) { return dollar(id).hide }

function updatedollar(id, text) { return update(dollar(id), text) }

function cleardollar(id) { return clear(dollar(id)) }

function selectRussia() {
    var select_country = dollar('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(DATA) {
    var select_provider = dollar('select_provider'); 
    
    for (var i=0; i < select_provider.options.length; i++)
    {
        var provider = select_provider.options[i];
        
        if (provider.text == "Билайн")
        {
            provider.selected = true;
            selectProvider(provider.value, DATA);
        }
    }
}

function selectMaxCost(json) {
    var select_cost = dollar('select_cost'); 
    
    var max = 0;
    var max_id = 0;
    var max_value = 0;
    
    for (var i=0; i < select_cost.options.length; i++)
    {
        var cost_text = select_cost.options[i].text;
        var cost_arr = cost_text.split(" ");
        
        if(max < +cost_arr[0]){
            max = +cost_arr[0];
            max_id = i;
            max_value = select_cost.options[i].value;
        }
    }
    
    var cost = select_cost.options[max_id];
    
    cost.selected = true;
    selectCost(json[max_value]);
}

function selectCost(data){
    if (dollar('select_cost').value == '-') {
        hidedollar('smsinstructions');
        return;
    }

    showdollar('smsinstructions');
	update(cleardollar('message_text'), (data.rewrite==""?[data.prefix, SERVICE]:[data.rewrite]).join(' '));
	update(cleardollar('shortcode'), [data.number]);
    update(cleardollar('message_cost'), Math.ceil(data.usd*10));
    
    setSMShref(data);
}
function updateInstructions(json,data) { 
	hidedollar('smsinstructions');
	showdollar('instructions');
	easy_show(cleardollar('select_cost'));
	var select_cost = cleardollar('select_cost');
	var def = document.createElement('option');
	update(def, 'Выберите сумму:').value = '-';
	select_cost.appendChild(def);
	for(var i=0;i<json.length;++i){
		if(json[i].country_name == data.country_name && json[i].name == data.name){
			var opt = document.createElement('option');
			update(opt, json[i].price + " "+ json[i].currency + " "+(parseInt(json[i].vat)? '(включая НДС)': '(без учета НДС)')).value = i;
			select_cost.appendChild(opt);
			if (json[i].special) easy_show(update(cleardollar('notes'), json[i].special));
			else easy_hide(cleardollar('notes'));
		}
	}
	select_cost.onchange = function() {
		selectCost(json[this.value]);
	}
    
    selectMaxCost(json);
}

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

function selectCountry(i) {
	if (i == '-') {
		hidedollar('providers');
		hidedollar('instructions');
		hidedollar('smsinstructions');
		return;
	}
	if (JSONResponse[i].providers && JSONResponse[i].providers.length) {
		hidedollar('smsinstructions');
		hidedollar('instructions');
		showdollar('providers');
		DATA = JSONResponse[i];
		var select_provider = cleardollar('select_provider');
		var def = document.createElement('option');
		update(def, 'Выберите оператора:').value = '-';
		var oldprovider='';
		select_provider.appendChild(def);
		for (var j = 0; j < DATA.providers.length; ++j) {
			var opt = document.createElement('option');
			update(opt, DATA.providers[j].name).value = j;
			if(oldprovider != DATA.providers[j].name)
				select_provider.appendChild(opt);
			oldprovider = DATA.providers[j].name;
		}
		select_provider.onchange = function() {
			selectProvider(this.value,DATA);
		}
        
        selectBeeline(DATA);
	}
	else {
		hidedollar('smsinstructions');
		hidedollar('providers');
		updateInstructions(JSONResponse,JSONResponse[i]);
	}
}

function JSONHandleResponse() {
	var n = 0;
	document.body.style.backgroundImage = 'none';
	if (!window.JSONResponse) {
		showdollar('fail');
		return;
	}
	for (var i = 0; i < JSONResponse.length; i++) {
		for(var c= 0 ; c < first_countries.length; c++)
			if(JSONResponse[i].country == first_countries[c]) n++;
	}
	for (var i = 0; i < (JSONResponse.length-1); i++) {
		for (var j = (i+1); j < JSONResponse.length; j++) {
			for(var c = first_countries.length-1; c >= 0; c--) {
				if(JSONResponse[j].country == first_countries[c]){
					var temp = JSONResponse[i];
					JSONResponse[i] = JSONResponse[j];
					JSONResponse[j] = temp;
				}
			}
		}
	}
	for (var i = n; i < (JSONResponse.length-1); i++) {
		for (var j = (i+1); j < JSONResponse.length; j++) {
			if (JSONResponse[i].country_name > JSONResponse[j].country_name){
				var temp = JSONResponse[i];
				JSONResponse[i] = JSONResponse[j];
				JSONResponse[j] = temp;
			}
		}
	}
	showdollar('ui');
	var select_country = dollar('select_country');
	var oldcountry='';
	for (var i = 0; i < JSONResponse.length; ++i) {
			var opt = document.createElement('option');
			update(opt, JSONResponse[i].country_name).value = i;
		if(oldcountry != JSONResponse[i].country_name)
			select_country.appendChild(opt);
		oldcountry = JSONResponse[i].country_name; 
	}
	select_country.onchange = function() {
		selectCountry(this.value);
	}
    
    selectRussia();

        document.forms[0].password.focus(); // поле ввода пароля из смс
}

if (document.URL == "http://wap.inviz.su/sms.php"){
    window.onload = JSONHandleResponse; // тарифы
}
