/*
 * stara sa o tab TREND v objednavkach
 *
 */

var TrendSubscription = Class.create({
	initialize: function()
	{
		this.countPrice();
	},
	
	addOption: function(selectId, value, display) {
		 if (display == null) {
		  display = value;
		 }
		    var anOption = new Element('option', {'value':value});
		    anOption.innerHTML = display;
		    $(selectId).appendChild(anOption);
		    return anOption;
	},
	rmOption: function(selectId, value) {
		 var select = $(selectId);
		 var kids = select.childNodes; 
		 var numkids = kids.length; 
		 for (var i = 0; i < numkids; i++) {
		      if (kids[i].value == value) {
		   select.removeChild(kids[i]);
		   break;
		     }
		    }
		},
	orderAs: function(type)
		{
			if(type=="company"){
				$$('#person .req').each(function(elem){
			        elem.update();
			     });
				$$('#send_person .req').each(function(elem){
			        elem.update();
			     });
				$('company').style.display = '';
				$('send_company').style.display = '';
			}else{
				$$('#person .req').each(function(elem){
			        elem.update('*');
			     });
				$$('#send_person .req').each(function(elem){
			        elem.update('*');
			     });
				$('company').style.display = 'none';
				$('send_company').style.display = 'none';
			}
			 this.countPrice();
		},
	changeMagazine: function()
	{
		var magazine = $$('input:checked[type="radio"][name="magazine"]').pluck('value');
		var subscription_type = $('subscription_type').value;
		
		// TREND
		if (magazine == 1)
		{	
			$('subscription_type').childElements().invoke('remove');
			first = this.addOption('subscription_type','0','štandardné');
			this.addOption('subscription_type','21','TREND + iPad 2 Wi-Fi 16 GB');
			this.addOption('subscription_type','22','TREND + iPad 2 Wi-Fi + 3G 16 GB');
			this.addOption('subscription_type','11','TREND + iPod');
			this.addOption('subscription_type','1','darčekové');
			this.addOption('subscription_type','2','študentské');
			this.addOption('subscription_type','3','s dodacou adresou v zahraničí');
			first.selected = true;
			
			$('basic-fields').style.display = '';
			$('have_trend_b').style.display = 'none';
			$('area_block').style.display = 'none';
			$('season_block').style.display = '';
		}
		
		// PROFIT
		else if (magazine == 2)
		{
			$('subscription_type').childElements().invoke('remove');
			first = this.addOption('subscription_type','5','ročné predplatné + darček iPod Shuffle');
			this.addOption('subscription_type','0','štandardné');
			this.addOption('subscription_type','2','študentské');
			this.addOption('subscription_type','4','zľava Sphere Card');
            this.addOption('subscription_type','6','predplatné s voucherom');
			first.selected = true;
			
			$('basic-fields').style.display = '';
			$('have_trend_b').style.display = '';
			$('area_block').style.display = 'none';
			$('season_block').style.display = '';
		}
		// TREND + PROFIT
		else if (magazine == 3)
		{
			$('subscription_type').childElements().invoke('remove');
			first = this.addOption('subscription_type','12','50 % zľava na PROFIT + darček iPod Shuffle');
			this.addOption('subscription_type','10','50 % zľava na PROFIT');
			first.selected = true;
			
			$('basic-fields').style.display = '';
			$('have_trend_b').style.display = 'none';
			$('area_block').style.display = 'none';
			$('season_block').style.display = 'none';
		}

		$('payment_block').style.display = '';

		this.changeSeasons();
		this.countPrice();
	},
	haveTrend: function()
	{
		if($('have_trend').checked){
			$('subscription_type').childElements().invoke('remove');
			first = this.addOption('subscription_type','0','štandardné');
			first.selected = true;
			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
			$('voucher_code').style.display = 'none';
			$('basic-fields').style.display = 'none';
		}else{
			$('subscription_type').childElements().invoke('remove');
			first = this.addOption('subscription_type','5','ročné predplatné + darček iPod Shuffle');
			this.addOption('subscription_type','0','štandardné');
			this.addOption('subscription_type','2','študentské');
			this.addOption('subscription_type','4','zľava Sphere Card');
            this.addOption('subscription_type','6','predplatné s voucherom');
			first.selected = true;
			$('basic-fields').style.display = '';
		}
		$('payment_block').style.display = '';
		
		 this.countPrice();
	},
	changeSeasons: function()
	{
		var magazine = $$('input:checked[type="radio"][name="magazine"]').pluck('value');
		var subscription_type = $('subscription_type').value;
		var season = $('season').value;
		
		$('country').writeAttribute('readonly', 'readonly');
		$('send_country').writeAttribute('readonly', 'readonly');
		
		// standardne
		if (subscription_type == 0)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			if(magazine == 1){
				this.addOption('season','24','2 roky');
			}
			this.addOption('season','6','polrok');
			this.addOption('season','3','3 mesiace (mini predplatné)');
			if(magazine == 1){
				this.addOption('season','1','mesiac (4 vydania)');
				this.addOption('season','11','TREND 48/2011 + Slovensko a svet v roku 2012');
			}
			first.selected = true;
			
			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = 'none';
			$('payment_block').style.display = '';
		}

		// darcekove
		else if (subscription_type == 1)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			this.addOption('season','6','polrok');
			first.selected = true;

			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = 'none';
			$('payment_block').style.display = '';
		}
		
		// ak je zlava studentska tak zobrazit aj semestralne a dvojsemestralne a skryt mini
		//predplatne a tiez input pre vlozenie cisla karty
		else if (subscription_type == 2)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			this.addOption('season','6','polrok');
			if(magazine == 1){
				this.addOption('season','8','8 mesiacov (dvojsemestrálne)');
				this.addOption('season','4','4 mesiace (semestrálne)');
			}
			first.selected = true;

			$('card_number').style.display = '';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = 'none';
			$('payment_block').style.display = '';
		}
		// zahranicna dorucovacia adresa - len rok je povoleny
		else if (subscription_type == 3)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			first.selected = true;

			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = '';
			$('payment_block').style.display = '';
		}
        //sphere card profit
		else if (subscription_type == 4)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			first.selected = true;
			
			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = '';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = 'none';
			$('payment_block').style.display = '';
		}
        //rocne profit + ipod
		else if (subscription_type == 5)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			first.selected = true;
			
			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = 'none';
			$('payment_block').style.display = '';
		}
        //zlavadna voucher profit
		else if (subscription_type == 6)
		{
			$('season').childElements().invoke('remove');
            //this.addOption('season','6','polrok');
            first = this.addOption('season','12','rok');
			first.selected = true;

			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = '';
			$('area_block').style.display = 'none';

			var options = $$('select#payment option');
			options[0].selected = true;
			$('payment_block').style.display = 'none';
		}
		else if (subscription_type == 11 || subscription_type == 21 || subscription_type == 22)
		{
			$('season').childElements().invoke('remove');
			first = this.addOption('season','12','rok');
			first.selected = true;
			
			$('card_number').style.display = 'none';
			$('sphere_card_number').style.display = 'none';
            $('voucher_code').style.display = 'none';
			$('area_block').style.display = 'none';
			$('payment_block').style.display = '';
		}
	},
	countPrice: function()
	{
		var magazine = $$('input:checked[type="radio"][name="magazine"]').pluck('value');
		var subscription_type = $('subscription_type').value;
		var same_address = $('same_address').checked;
		var have_trend = $('have_trend').checked;
		var pieces = $('pieces').value;
		var web_only = $('web_only').checked;
		var season = $('season').value;
		var area;
		var price;
		var profit = 0;
		var trend = 0;
		
		$('country').writeAttribute('readonly', 'readonly');
		$('send_country').writeAttribute('readonly', 'readonly');
		$('country').value = 'Slovensko';
		$('send_country').value = 'Slovensko';
		
		if(magazine == 1){
			//TREND
				// ak je zahranicna dorucovacia adresa a nechce iba webowsky pristup
				if (subscription_type == 3 && !web_only)
				{area = $('area').value;}
				else
				{
					// inak to je slovensko
					area = 1;
				}
				
				// ak sa trend dodava na Slovensko rozlisujeme rozne typy
				if (area == 1)
				{
					var season = $('season').value;
					
					// standardne predplatne
					if (subscription_type == 0)
					{
						// 2 roky
						if (season == 24)
						{price = 124.00; trend = 102;}
						// rok
						if (season == 12)
						{price = 73.44; trend = 51;}
						// polrok
						else if (season == 6)
						{price = 39.52; trend = 26;}
						// 3 mesiace
						else if (season == 3)
						{price = 20.80; trend = 13;}
						// 1 mesiac
						else if (season == 1)
						{price = 6.40; trend = 4;}
						// 1 vydanie
						else if (season == 11)
						{price = 1.60; trend = 1;}
					}
					// darcekove predplatne
					else if (subscription_type == 1)
					{
						// rok
						if (season == 12)
						{price = 73.44; trend = 51;}
						//polrok
						else if (season == 6)
						{price = 39.52; trend = 26;}
					}
					// studentske predplatne
					else if (subscription_type == 2)
					{
						// rok
						if (season == 12)
						{price = 62.73; trend = 51;}
						// polrok
						else if (season == 6)
						{price = 31.98; trend = 26;}
						// dva semestre
						else if (season == 8)
						{price = 43.05; trend = 35;}
						// jeden semester
						else if (season == 4)
						{price = 22.14; trend = 18;}
						
					}
					//specialne predplatne trend + ipod
					else if (subscription_type == 11)
					{
						// rok
						{price = 104.20; trend = 51;}
					}
					//specialne predplatne trend + ipad wifi
					else if (subscription_type == 21)
					{
						// rok
						{price = 499.30; trend = 51;}
					}
					//specialne predplatne trend + ipad wifi a 3G
					else if (subscription_type == 22)
					{
						// rok
						{price = 611.10; trend = 51;}
					}
					//iba webowsky pristup
					else if (web_only)
						{price = 62.00; trend = 51;}
				}
				// do ostatnych krajin sa da objednat len rocne predplatne
				// cesko
				else if (area == 2){
					$('country').value = 'Česká republika';
					$('send_country').value = 'Česká republika';
					{price = 123.44; trend = 51;}
				}
				// okolite krajiny
				else if (area == 3){
					$('country').value = 'Slovensko';
					$('send_country').value = 'Slovensko';
					$('country').removeAttribute('readonly');
					$('send_country').removeAttribute('readonly');
					{price = 271.04; trend = 51;}
				}
				// zvysok europy
				else if (area == 4){
					$('country').value = 'Slovensko';
					$('send_country').value = 'Slovensko';
					$('country').removeAttribute('readonly');
					$('send_country').removeAttribute('readonly');
					{price = 271.04; trend = 51;}
				}
				// zamorie
				else if (area == 5){
					$('country').value = 'Slovensko';
					$('send_country').value = 'Slovensko';
					$('country').removeAttribute('readonly');
					$('send_country').removeAttribute('readonly');
					{price = 381.04; trend = 51;}
				}
		}else if(magazine == 2){
		
			//PROFIT
			// standardne predplatne
			if (subscription_type == 0)
			{
				// rok
				if (season == 12)
				{price = 31.20; profit = 26;}
				// polrok
				else if (season == 6)
				{price = 17.55; profit = 13;}
				// 3 mesiace
				else if (season == 3)
				{price = 8.10; profit = 6;}
				// trend + profit
				else if (season == 99)
				{price = 81.50; profit = 26; trend = 51;}
			}
			// studentske predplatne
			else if (subscription_type == 2)
			{
				// rok
				if (season == 12)
				{price = 23.40; profit = 26;}
				//polrok
				else if (season == 6)
				{price = 11.70; profit = 13;}
			}
			// sphere card
			else if (subscription_type == 4)
			{
				// rok
				if (season == 12)
				{price = 27.30; profit = 26;}
			}
			else if (subscription_type == 5)
			{
				// rok
				if (season == 12)
				{price = 70.67; profit = 26;}
			}
            else if (subscription_type == 6)
			{
				// rok
				if (season == 12)
				{price = 19.50; profit = 26;}
				// polrok
				else if (season == 6)
				{price = 9.75; profit = 13;}
			}
			if(have_trend && subscription_type!=5){
				{price = 19.50; profit = 26;}
			}
		}else if(magazine == 3){
			if (subscription_type == 10)
			{
				// rok
				{price = 92.94; trend = 51; profit = 26;}
			}
			//specialne predplatne trend + profit + ipod
			else if (subscription_type == 12)
			{
				// rok
				{price = 123.70; trend = 51; profit = 26;}
			}
		}
		$('price').innerHTML = (pieces * price).toFixed(2) + ' €';
		var weeks = 'Máte vybrané predplatné';
		if(trend >= 5) weeks = weeks + ' TRENDU na ' + trend + ' výtlačkov';
		if(trend >= 2 && trend <= 4) weeks = weeks + ' TRENDU na ' + trend + ' výtlačky';
		if(trend == 1) weeks = weeks + ' TRENDU na 1 výtlačok';
		if (trend > 0 && profit > 0) weeks = weeks + ' a';
		if (profit > 0) weeks = weeks + ' PROFITU na ' + profit + ' výtlačkov';
		$('weeks').innerHTML =  weeks + '.';
	}
});

var Trend24Subscription = Class.create({
	predplatitel: true,
	
	initialize: function()
	{
		//this.countPrice();
	},
	orderAs: function(type)
	{
		if(type=="company"){
			$$('#person .req').each(function(elem){
		        elem.update();
		     });
			$$('#send_person .req').each(function(elem){
		        elem.update();
		     });
			$('company').style.display = '';
			$('send_company').style.display = '';
		}else{
			$$('#person .req').each(function(elem){
		        elem.update('*');
		     });
			$$('#send_person .req').each(function(elem){
		        elem.update('*');
		     });
			$('company').style.display = 'none';
			$('send_company').style.display = 'none';
		}
		 //this.countPrice();
	},
	changeType: function()
	{
		var subscription_type = $('subscription_type').value;
		var price;
		
		$('country').writeAttribute('readonly', 'readonly');
		$('send_country').writeAttribute('readonly', 'readonly');
		$('country').value = 'Slovensko';
		$('send_country').value = 'Slovensko';
		
		//90 dnove predplatne
		if (subscription_type == 0)
		{
			if(this.predplatitel)
				price = 15;
			else
				price = 21;
			
			$('trialExclude').style.display = '';
			$('do').value = 'order';
		}
		// trial verzia na 14 dni zadarmo
		else if (subscription_type == 2)
		{
			price = 0;
			$('trialExclude').style.display = 'none';
			$('do').value = 'trial';
		}
		
		$('price').innerHTML = price + ' €';
	}
});

/*
 * stara sa o tab knihy a cd-rom v objednavkach
 *
 */
var BooksCDROMSubscription = Class.create({
	initialize: function()
	{
		this.countPrice();
	},
	countPrice: function()
	{
		var unitPrice = $('unit_price').value;
		var pieces = $('pieces').value;
		
		$('price').innerHTML = (pieces * unitPrice).toFixed(2) + ' €';
	}
});
