﻿// JavaScript Document

var content;

function loadData(data) {
	//content = data;
	data = data.replace(/\r/gm, '');
	//data = data.replace(/€/gm, '&euro;');
	var lines = data.split('\n');

	var output = '<table id=selects>';
	output += '<tr>' +
	'<th>'+_('Features')+'</th>' +
	'<th>'+_('Your Selections')+'</th>' +
	//'<th>Setup</th>' +
	'<th>'+_('Monthly')+'</th>' +
	'</tr>';
	//skip the first line with captions
	var i = 1;
	while (i < lines.length){
		
		var elems = lines[i].split('\t');
		if (trim(elems[0]))
		{
		//table row start
			output += '<tr>';
		//category
			output += '<td>';
			category = elems[0];
			output += _(category);
			output += '</td>';
		//select
			output += '<td><select>';
			while(i < lines.length)
			{
				elems2 = lines[i].split('\t');
				var option = elems2[1];
				var setup = elems2[3];
				if (!trim(setup)) setup = '&euro; 0';
				var month = elems2[2];
				if (!trim(month)) month = '&euro; 0';
				var isDefault = (elems2[4] == 'Default');
				var noCost = (elems2[4] == 'None');
				var additionalInfo = elems2[5];
				
				output += '<option ' +
				'setup="' + setup + '" ' +
				'month="' + month + '" ' +
				(isDefault ? 'selected' : '') +
				'>';
				output += _(option)
				if (!noCost) output += ' -- ' + setup + ' '+_('setup')+', ' + month + '/'+_('mo');
				if (additionalInfo) output += ' -- ' + _(additionalInfo);
				output += '</option>';
				i++;
				if (i >= lines.length || trim(lines[i].split('\t')[0])) break; //a new category is found
			}
			output += '</select></td>';
			//output += '<td></td>';	
			output += '<td></td>';	
		//end of row
			output += '</tr>';
		}
	}
	output += '</table>';
	output += periods();
	
	var caption = $('#calculator').attr('caption');
	if (!caption) caption = 'Configure Your Dedicated Server';

	$('#calculator').prepend(output).prepend('<h1>'+_(caption) + '</h1>');
	
   $('#calculator select').change(function()
	   {
	    // var $oSel = $(this).find('option:selected');
        // var setup = trimEuro($oSel.attr('setup'));
        // var month = trimEuro($oSel.attr('month'));
		// $(this).parent().next().text(setup).next().text(month);
        showTotals();
		//alert(setup);
     }).css('width', '690px');
	
	$('#calculator select:first').css('color', 'red');
	
	showTotals();
	
	//styles
	$('#totals2 tr')
	.find('td:first').css('text-align', 'left').end()
	.find('td:not(:first)').css('text-align', 'right').end()
	.click(function() {
		$('#totals2 tr').removeClass('selected');
		$(this).addClass('selected').find('input:radio').attr('checked','true');
	});
	
	$('#calculator tr')
	.find('td:last').css('text-align', 'right').end()
	.find('td:first').css('text-align', 'left').end();
	
	$('#selects').find('td:first-child, th:first-child, td:last-child, th:last-child').css('border-bottom', '1px solid lightblue');
	
	
}

function trimEuro(a)
{
	var b = parseInt(a.replace(/(\\&euro;)|\s|€/g, ''));
	if (isNaN(b))
		return 0;
	else
		return b;
}

$(document).ready(
	function()
	{
	var filename = $('#calculator').attr('data');
	if (!filename) filename = 'servers.txt';
	
		$.get(filename, loadData);
     
	 $('#input_submit').add('#input_submit2').click(function()
	 {
			//alert('aaa');
			GetData($(this).val());
			//alert(res);
			
			return true;
			//return false;
	 });
	 
	 $('#input_clear').click(ButtonClearClick);
	 
	});
	
	//var summa = 0;
	
	function GetData(buttonName)
	{
	var d = new Date();
			var curr_date = d.getDate();
			var curr_month = d.getMonth() + 1;
			var curr_year = d.getFullYear();
			var curr_hour = d.getHours();
			var curr_min = d.getMinutes();

			var res = "";
			//form name
			res += "Local date and time: " + curr_year + "-" + curr_month + "-" + curr_date + " "+curr_hour+':'+curr_min;
			res += "\r\n";
			res += "Request from: " + trim($('#input_subject').attr('value'));
			res += "\r\n";
			res += "Button pressed: " + buttonName;
			res += "\r\n";
			res += "\r\n";
			//services
			$("#selects tr").each(function(index) {
				res += trim($(this).find('td:first').text());
				res += ": ";
				res += trim( $(this).find('select option:selected').text());
				res += "\r\n";
			});
			
			//totals2
			$radioTd = $('#totals2 input:radio:checked').parent();
			res += "\r\n";
			res += "Period: " + trim($radioTd.text());
			res += "\r\n";
			res += "Total monthly cost: " + trim($radioTd.next().text());
			res += "\r\n";
			res += "Setup: " + trim($radioTd.next().next().text());
			
			res += "\r\n";
			res += "\r\n";
			//contacts
			$("#contacts tr").each(function(index) {
				res += trim($(this).find('td:first').text());
				res += ": ";
				
				//var v = trim($(this).find('td:last input').attr('value'));
				
				//res += (v == '' ? '--' : v);
				switch(index)
					{
					case 0:
					  res += $('#name').attr('value');
					  break;
					case 1:
					  res += $('#company').attr('value');
					  break;
					case 2:
					  res += $('#email').attr('value');
					  break;
					case 3:
					  res += $('#phone').attr('value');
					  break;
					case 4:
					  res += trim($('#questions').attr('value'));
					  break;
					case 5:
					  res += $('#promo').attr('value');
					  break;
					}
				res += "\r\n";
			});
			
			res += "\r\n";
			
			$('#input_info').attr('value', res);
	}
	
	function ButtonClearClick()
	{
		GetData($(this).val());
		$.post('process.php', 
			{
	            lang: $('#input_lang').val(),
	            subject: $('#input_subject').val(),
	            info: $('#input_info').val()
	        });
		$('select option:first-child').attr('selected', 'selected');
		showTotals();
		return false;
	}
	
	function showTotals()
	{
	   var totalSetup = 0;
	   var totalMonth = 0;
	   $('#selects select option:selected').each(function () {
                //summa += parseInt($(this).attr('value'));
				var $oSel = $(this);
		        var setup = trimEuro($oSel.attr('setup'));
				totalSetup += setup;
				var aMonth = $oSel.attr('month');
		        var month = trimEuro(aMonth);
				if (aMonth.match(/%/)) 
				{
					//percentage of total
					month = (totalMonth * month / 100);
				}
				totalMonth += month;
				$oSel.parent().parent()
				//.next().text('€ ' + setup)
				.next().text("€ " + month);
              });
	   //$('#td12').text('&euro; '+(summa*12).toString());
	   //$('#td24').text('&euro; '+(summa*24).toString());
	   //$('#td48').text('&euro; '+(summa*48).toString());
	   //$('#td1').text('€ '+(summa*1).toString());
	   $('#totals2 input:radio').each(function() {
			var mult = parseInt($(this).val());
			var month = totalMonth;
			var discount = parseInt($(this).attr('discount'));
			var monthDis = month * (100 - discount)/100;
			var setupDis = totalSetup * (100 - discount)/100;
			var total = totalSetup + month;
			var totalDis = monthDis + setupDis;
			$(this).parent()
			//.next().text('€ ' + total.toFixed(2) + (discount > 0 ? ' - '+discount+'% = € '+totalDis.toFixed(2) : ''))
			.next()
			.text('€ ' + month.toFixed(2) + (discount > 0 && month>0 ? ' - '+discount+'% = € '+monthDis.toFixed(2) : ''))
			.next()
			.text('€ ' + totalSetup.toFixed(2) + (discount > 0 && totalSetup>0 ? ' - '+discount+'% = € '+setupDis.toFixed(2) : ''));
	   });
  }
  
  function trim(stringToTrim) {
	return stringToTrim.replace(/^(\s|\r|\n)+|(\s|\r|\n)+$|(\r|\n)+/g,"");
}

function _(s) {
if (typeof(i18n)!='undefined' && i18n[s]) {
return i18n[s];
}
return s;
}
