function coin(){
	JSON_URL = 'media/js/coin.local.js';
	first_countries = Array('RU','IL','UA'); // Страна (двух буквенный код страны) которая будет первой в списке
	
	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 selectCost(data){
		show$('smsinstructions');
		update(clear$('message_text'), (data.rewrite==""?[data.prefix, SERVICE]:[data.rewrite]).join(' '));
		update(clear$('shortcode'), [data.number]);
		update(clear$('message_cost'), [data.price, data.currency, (parseInt(data.vat)? '(включая НДС)': '(без учета НДС)')].join(' '));
	}
	function updateInstructions(json,data) { 
		hide$('smsinstructions');
		show$('instructions');
		show(clear$('select_cost'));
		var select_cost = clear$('select_cost');
		var def = document.createElement('option');
		update(def, 'Выберите сумму:').value = '-';
		select_cost.appendChild(def);
		var minimal = -1;
		for (var i = 0; i < json.length; ++i) {
			if (json[i].country_name == data.country_name && json[i].name == data.name) {
				minimal = (minimal == -1 || parseInt(json[i].price) < parseInt(json[minimal].price)) ? i : minimal;
			}
		}
		if (minimal > 0){
			var opt = document.createElement('option');
			update(opt, json[minimal].price + " "+ json[minimal].currency + " "+(parseInt(json[minimal].vat)? '(включая НДС)': '(без учета НДС)')).value = minimal;
			select_cost.appendChild(opt);
			if (json[minimal].special) show(update(clear$('notes'), json[minimal].special));
			else hide(clear$('notes'));		
		}	
	//	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) show(update(clear$('notes'), json[i].special));
	//			else hide(clear$('notes'));
	//		}
	//	}
		select_cost.onchange = function() {
			selectCost(json[this.value]);
		}
	}
	
	function selectProvider(i,DATA) {
		if (i == '-') {
			hide$('instructions');
			hide$('smsinstructions');
			return;
		}
		updateInstructions(DATA.providers,DATA.providers[i]);
	}
	
	function selectCountry(i) {
		if (i == '-') {
			hide$('providers');
			hide$('instructions');
			hide$('smsinstructions');
			return;
		}
		if (JSONResponse[i].providers && JSONResponse[i].providers.length) {
			hide$('smsinstructions');
			hide$('instructions');
			show$('providers');
			DATA = JSONResponse[i];
			var select_provider = clear$('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);
			}
		}
		else {
			hide$('smsinstructions');
			hide$('providers');
			updateInstructions(JSONResponse,JSONResponse[i]);
		}
	}
	
	function JSONHandleResponse() {
		var n = 0;
		document.body.style.backgroundImage = 'none';
		if (!window.JSONResponse) {
			show$('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;
				}
			}
		}
		show$('coin-ui');
		var select_country = $$('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);
		}
	}
	
	function JSONSendRequest() {
		var ui = $$('coin-ui');
	
		if (ui == null) return;
		
		var head_node = document.getElementsByTagName('head').item(0);
		var js_node = document.createElement('script');
		js_node.src = site_url + JSON_URL;
		js_node.type = 'text/javascript';
		js_node.charset = 'utf-8';
		if (navigator.product == 'Gecko') {
			js_node.onload = JSONHandleResponse;
		}
		else {
			js_node.onreadystatechange = function(evt) {
				evt? 1: evt = window.event;
				var rs = (evt.target || evt.srcElement).readyState;
				if (rs == 'loaded' || rs == 'complete') {
					JSONHandleResponse();
				}
			}
		}
		head_node.appendChild(js_node);
	}
	
	JSONSendRequest()
}

