
RCaccess = RCaccess ? RCaccess : '||'; // required
StartTariff = !isNaN(StartTariff) ? StartTariff : '5';
ClientNr =  !isNaN(ClientNr) ? ClientNr : ''; 

var RatesCalculator =
{
	methods: '',
	
	init: function()
	{
		//RatesCalculator.fillMethods(); // they said to remove "automatic filling", now they want it again!

		if ((jaaulde.utils.cookies.get('rcalc_to_country')))
		{
			$('#to_country').get(0).selectedIndex = jaaulde.utils.cookies.get('rcalc_to_country');
		}
		if ((jaaulde.utils.cookies.get('rcalc_from_country')))
		{
			$('#from_country').get(0).selectedIndex = jaaulde.utils.cookies.get('rcalc_from_country');
		}
		if ((jaaulde.utils.cookies.get('rcalc_methods')))
		{
			if($('#rate_methods').length > 0)
			{
				$('#rate_methods').get(0).selectedIndex = jaaulde.utils.cookies.get('rcalc_methods');
			}
		}

		RatesCalculator.changeCountry();
		RatesCalculator.updateDisplay(true);
	},

	fillMethods: function()
	{
		if($('#rate_methods').length > 0)
		{
			methods = new Array();
			methods['PC to Phone'] = '||';
			methods['SMS'] = '||';
			methods['Phone to Phone'] = '||';
			methods['Accessnumber'] = '||';
			$('#rate_methods').empty();
			i = 0;
			for (method in methods)
			{
				val = methods[method].split('|');
				if (val[0] != 'NaN' && val[0] != '0')
				{
					$('#rate_methods').append('<option value="' + methods[method] + '"' +(!i ? 'selected="selected"' : '')+ '>' + method + '</option>');
				}
				i++;
			}
		}
	},

	updateValues: function(methods)
	{
		if($('#rate_methods').length > 0)
		{
			var i = 0;
			var selected = $("#rate_methods").get(0).selectedIndex;

			$('#rate_methods').empty();
			for (method in methods)
			{
				val = methods[method].split('|');
				$('#rate_methods').append('<option value="' + methods[method] + '"' +((i == selected) ? 'selected="selected"' : '')+ '>' + method + '</option>');
				i++;
			}

			if ((jaaulde.utils.cookies.get('rcalc_methods')))
			{
				$('#rate_methods').get(0).selectedIndex = jaaulde.utils.cookies.get('rcalc_methods');
			}
		}
		else
		{
			selected = true;
		}
		
		RatesCalculator.updateDisplay(selected);
	},

	updateDisplay: function(update)
	{
		if($('#rate_methods').length > 0)
		{
			val = $('#rate_methods').val() ? $('#rate_methods').val() : '||';

			if (!update)
			{
				jaaulde.utils.cookies.set('rcalc_methods', $('#rate_methods').get(0).selectedIndex);
			}
		}
		else
		{
			if (RCaccess.indexOf("|"+from_rciso+"|"  ) >-1 )
		   {
			   val = this.methods['Accessnumber'];
		   }
			else
			{
				val = this.methods['Phone to Phone'];
			}
		}

		val = val.split('|');
		if(ClientNr == '205' && val[0] != 'Free') //callingcredit has euro sign
		{
			price = parseFloat(val[0]);
			priceFormat2 = price/100;
			priceFormat2 = priceFormat2.toString();
			if(priceFormat2.length == 3)
				priceFormat2 = priceFormat2 + '00';
			else if(priceFormat2.length == 4)
				priceFormat2 = priceFormat2 + '0';
			value = '<div class="value"><small>&euro;</small>' + priceFormat2 + '<small>/min</small></div><div class="rate">' + val[1]+val[2] + '</div>';
		}
		else
		{
			value = '<div class="value">' + val[0] + '</div><div class="rate">' + val[1]+val[2] + '</div>';
		}
		$('#rates_container').html(value);
	},

	parseDisplay: function(price, append, force_display, setup_text, method)
	{
		var result = "||";
    var connection_fee = '';
    
		if (parseFloat(price) == 0 && method != 'phone')
		{
			result = "Free||"+force_display;
		}
    else if (parseFloat(price) == 0 && method == 'phone')
		{
			result = "Free||"+setup_text+force_display;
		}
		else if (!isNaN(parseFloat(price)))
		{
			price = parseFloat(price);

      if((ClientNr == '204' && method == 'pc') || method == 'phone')
      {
        connection_fee = setup_text;
      }
      
			result = (price.toFixed(1) > 0 ? price.toFixed(1) : price.toFixed(2)) + "|" + append + "|" +connection_fee+force_display;
		}    
    else
    {
			result = "||" + force_display;
		}
    
		return result;
	},

	changeCountry: function()
	{
		var to_country = $('#to_country'),
		from_country = $('#from_country'),
		access_number = '',
		pc_to_phone = '',
		sms = '',
		phone_to_phone = '';

		from_rciso = from_country.get(0).options[from_country.get(0).selectedIndex].getAttribute('rciso');
		from_sms = from_country.get(0).options[from_country.get(0).selectedIndex].getAttribute('sms');
		to_sms = to_country.get(0).options[to_country.get(0).selectedIndex].getAttribute('sms');
		//document.cookie = "fromcountry = " + from_rciso;


        if (to_country.val() && (RCaccess.indexOf("|"+from_rciso+"|"  ) >-1 ))
		{
			access_number = to_country.val();
		}

		if (to_country.val())
		{
			pc_to_phone = to_country.val();
		}

		if (to_sms && from_sms)
		{
			sms = to_sms;
		}

		if (from_country.val() && to_country.val()) {
			phone_to_phone = parseFloat(from_country.val()) + parseFloat(to_country.val());
		}

		rcstarttarif = StartTariff;
    connection_text = '<br><small>+'+rcstarttarif+'ct setup fee</small>';

    this.methods = new Array();
    this.methods['PC to Phone'] = RatesCalculator.parseDisplay(pc_to_phone, 'cent/min', '<br><small><a href=download.html>more info</a></small>', connection_text, 'pc');
    this.methods['SMS'] = RatesCalculator.parseDisplay(sms, 'cent/sms', '<br><small><a href=sms.html>more info</a></small>', '', 'sms');
    this.methods['Phone to Phone'] = RatesCalculator.parseDisplay(phone_to_phone, 'cent/min', '<br><small><a href=phone-to-phone.html>more info</a></small>', connection_text, 'phone');
    this.methods['Accessnumber'] = RatesCalculator.parseDisplay(access_number, 'cent/min', '<br><small><a href=geo.html>more info</a></small>', '', 'access');
    
    	// set options
    	var cookieExpireTime = new Date();
    	cookieExpireTime.setTime(cookieExpireTime.getTime() + (1000 * 60 * 60 * 24 * 30)); // 30 days expire time
    
		// save selections to cookies
        document.cookie = 'rcalc_to_country=' + to_country.get(0).selectedIndex + ';expires=' + cookieExpireTime;
        document.cookie = 'rcalc_from_country=' + from_country.get(0).selectedIndex + ';expires=' + cookieExpireTime;
        
        /*
		jaaulde.utils.cookies.set('rcalc_to_country', to_country.get(0).selectedIndex, {expiresAt: AlertShownCookieExpires});
		jaaulde.utils.cookies.set('rcalc_from_country', from_country.get(0).selectedIndex, {expiresAt: AlertShownCookieExpires});
		*/

		RatesCalculator.updateValues(this.methods);
	}
};

function popUp(URL,breed,hoog) {
	myleft=(screen.width)?(screen.width-breed)/2:100;
	mytop=(screen.height)?(screen.height-hoog)/2:100;
	properties = "width="+breed+",height="+hoog+",toolbar=0,location=0,statusbar=0,menubar=0,resizable=0,scrollbars=no, top="+mytop+",left="+myleft;

	window.open(URL,name,properties)
}

$(document).ready(function() {
	RatesCalculator.init();
});
