// Legal texts popup window object
var legalWindowObj = null;

function ChangePrices(currency)
{
	var allElements = document.getElementsByTagName('span');
	
	for (i = 0; i < allElements.length; i++)
	{
		if ($(allElements[i]).hasClass('usd') || $(allElements[i]).hasClass('eur'))
		{
			if ($(allElements[i]).hasClass(currency))
				allElements[i].style.display = 'inline';
			else
				allElements[i].style.display = 'none';
		}
	}
	
	$('ch_usd').className='';
	$('ch_eur').className='';
	$('ch_'+currency).className='on';
	
	url = '/mail-server/buy-axigen.php?change_currency='+currency;
	new Ajax(url, {	method: 'get', update: null }).request();
}

function commtouch(){
    $$('input.commtouch').addEvents({
		'click': function(){
				var str = this.id;
				var idlink = str.replace(/_commtouch/, "");
				
				if (this.getProperty("checked")) {
					$(idlink).href += "&cmt=1";
				} else {
					$(idlink).href = $(idlink).href.replace(/&cmt=1/, "");					
				}
		}	
	});
}

function commtouchAlone(){
	if ($('cmtnombx') != null)	{
	    $('cmtnombx').addEvent('change', function() {
			$('eurcmtalone').setHTML("&euro;" + comtouchprices[this.value].eur);
			$('usdcmtalone').setHTML("$" + comtouchprices[this.value].usd);
		});
	}
}

function avg(){
    $$('input.avg').addEvents({
		'click': function(){
				var str = this.id;
				var idlink = str.replace(/_avg/, "");
				
				if (this.getProperty("checked")) {
					$(idlink).href += "&avg=1";
				} else {
					$(idlink).href = $(idlink).href.replace(/&avg=1/, "");					
				}
		}	
	});
}

function avgAlone(){
	if ($('avgnombx') != null)	{
	    $('avgnombx').addEvent('change', function() {
			$('euravgalone').setHTML("&euro;" + avgprices[this.value].eur);
			$('usdavgalone').setHTML("$" + avgprices[this.value].usd);
		});
	}
}

function pushEmailAlone() {
	if ($('pushemailnombx') != null) {
		$('pushemailnombx').addEvent('change', function() {
				$('pushemail_contact').href = $('pushemail_contact').href.replace(/\?pushemail=\d*$/,'?pushemail=' + this.value);
		});
	}
}

function notifySyncAlone() {
	if ($('notifysyncnombx') != null) {
		$('notifysyncnombx').addEvent('change', function() {
			$('notifysync_contact').href = $('notifysync_contact').href.replace(/\?notifysync=\d*$/, '?notifysync=' + this.value);
		});
	}
}
function astraSyncAlone() {
	if ($('astrasyncnombx') != null) {
		$('astrasyncnombx').addEvent('change', function() {
			$('astrasync_contact').href = $('astrasync_contact').href.replace(/\?astrasync=\d*$/, '?astrasync=' + this.value);
		});
	}
}

// ISP Single Server
function iss() {
	// Assure the correct radios are selected by default, FF seems to remember the last setting
	$$('input.iss-perpetual').each(function (radio) { radio.checked = true; });
	
	$$('input.iss-perpetual,input.iss-yearly').addEvent('click', function (event) {
		if ($(this).hasClass('radio-checked')) {
			return;
		}
		else {
			// Get the classname before adding the radio-checked class
			// so we don't do processing afterwards on the classname string
			var classname = $(this).getProperty('class');
			// Remove old radio-checked class
			$(this).getParent().getElement('.radio-checked').removeClass('radio-checked');
			// Add radio-checked class to selected input
			$(this).addClass('radio-checked');
		}
		// Modify the link
		var col3 = $(this).getParent().getNext().getNext();
		var prod = col3.getElement('.' + classname + '-prod').getProperty('value');
		var buy = col3.getElement('a');
		buy.setProperty('href', buy.getProperty('href').replace(/&prods=(\d+)$/, '&prods=' + prod));
		
		// Toggle CSS class to highlight new selection and dim the previous one
		// First, for title
		$(this).getParent().getElement('.active-option-title').removeClass('active-option-title');
		$(this).getNext('label').addClass('active-option-title');
		// Then, for the price
		$(this).getParent().getNext().getElements('.active-option-price').removeClass('active-option-price');
		$(this).getParent().getNext().getElements('.' + classname).addClass('active-option-price');
	});
}

window.addEvent('domready', function() { 
	commtouch();
	commtouchAlone();
	avg();
	avgAlone();
	pushEmailAlone();
	notifySyncAlone();
	astraSyncAlone();
	iss();
	
	/*
		Restrict SSL domains and emails inputs to numbers
		We want to allow control characters though (like LEFT, RIGHT, HOME, END...)
		The only shortcoming is that DEL is not allowed because MooTools 1.1 assigns it 
		to '.' which we also don't want. We don't allow SHIFT since HOME and END
		are assigned to '$' and '#' but we don't want the latter two. We allow SHIFT+TAB though.
		
		We use keypress because we can stop the event default action here, 
		and only redo the price calculation on keyup after the event has taken place, 
		since we know that only the allowed keys went through. Doing the calculation
		in the keyup event allows us instant calculation feedback.
	*/
	$$(['sslemails']).addEvent('keypress', function (event) {
		var event = new Event(event);
		var control_char = 
			event.code == 35 || event.code == 36 || event.code == 37 || 
			event.code == 39 || event.code == 8 || event.code == 9;
		
		if (event.shift && !event.code == 9) {
			event.preventDefault();
			event.stopPropagation();
		}
		else if	(event.code == 38 && this.value < 99)	{
			this.value++;
		}
		else if	(event.code == 40 && this.value > 10)	{
			this.value--;
		}
		else if (
			(event.code < 48 || event.code > 57) && !control_char) {
			event.preventDefault();
			event.stopPropagation();
		}

	}).addEvent('keyup', function (event) {
		var n = isNaN(this.value) || !this.value.length || parseInt(this.value) < 10 ? 10 : this.value; 
		if (this.id == 'sslemails') {
			$('eursslemails').setHTML('&euro;' + sslprices.email.eur * n);
			$('usdsslemails').setHTML('$' + sslprices.email.usd * n);
		}
		
		// Update the buy-now link with the new quantity
		var buy = $(this).getParent().getParent().getNext().getNext().getElement('a');
		buy.setProperty(
				'href', 
				buy.getProperty('href').replace(/&qty=(\d+)$/, '&qty=' + n)
			);
	}).addEvent('blur', function(){
		if (this.value < 10) this.value = 10;
	});
});