var MAG = Object.extend({}, MAG || {});
MAG.Roaming = {
	Init: function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});

		if ($('postpaid')) {
			$('postpaid').observe('click', this.updateCountries.bind(this));
			$('rad_postpaid').observe('click', this.updateCountries.bind(this));
		}
		if ($('prepaid')) {
			$('prepaid').observe('click', this.updateCountries.bind(this));
			$('rad_prepaid').observe('click', this.updateCountries.bind(this));
		}

		if($('has_gprs')) $('has_gprs').observe('click', this.updateCountries.bind(this));

		$('going_abroad_submit').observe('click', function()
		{
			if($F('country_id') == '') {
				alert(this.options.no_country_label);
				return;
			}

			setTimeout("$('going_abroad').submit()", 200);

		}.bind(this));

	},

	updateCountries: function() {
		type = 0;
		if ($('prepaid').checked) {
			if($('has_gprs_block')) {
				$('has_gprs').checked = false;
				$('has_gprs_block').hide();
			}
			type = 'prepaid';
		}
		if ($('postpaid').checked) {
			if($('has_gprs_block')) {
				$('has_gprs_block').show();
			}
			type = 'postpaid';
		}
		MAG.Custom.clear();
		new Ajax.Request(this.options.url,{
			method: 'get',
			parameters: 'method=get_countries&type=' + type + ($('has_gprs') ? '&has_gprs=' + $F('has_gprs') : ''),
			onComplete: this.reloadCountries.bind(this)
		});
	},

	reloadCountries: function(req)
	{
		var json = req.responseText.evalJSON();
		var countries = $('country_id');
		countries.options.length = 0;

		if (json.length) {
			for(i = 0; i < json.length; i++) {
				countries.options[i] = new Option(json[i].title, json[i].id);
			}
		}
		else {
			countries.options[0] = new Option(this.options.no_records_label, 0);
		}
		MAG.select_country_id.reSet();
	}
};

