var lang = '';
$(document).ready(
	function () {
		if (lang == 'ru') {
			$('#applyButton').click(function (e) { location.href = 'http://159.148.70.230/regforma/';});
		} else {
			$('#applyButton').click(function (e) { location.href = 'http://159.148.70.230/regforma/';});
		}
		$('.chooser').click( function (e) {
			$('.chooser .selector').removeClass('selected')
			$(this).find('.selector').addClass('selected');
			$('#type').val($(this).attr('rel'));
			switch ($('#type').val()) {
				case 'car':
					makeOptions('sum', 50, 5000, 50);
					makeTermOptions(60);
					$('#maxSum').html(5000);
					$('#maxTerm').html(60);
					break;
				default:
					makeOptions('sum', 50, 3000, 50);
					makeTermOptions(36);
					$('#maxSum').html(3000);
					$('#maxTerm').html(36);
			}
			var sumClone = $('#sum').clone(true);
			var termClone = $('#term').clone(true);
			$('.jqTransformSelectWrapper').remove();

			sumClone.removeClass('jqTransformHidden').appendTo('#sumDrop');
			termClone.removeClass('jqTransformHidden').appendTo('#termDrop');


		    $("form.transform").removeClass('jqtransformdone').jqTransform();
		});
		makeOptions('sum', 50, 3000, 50);
		makeTermOptions(36);
		doMath();
		$("a.#scheduleLink").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600,
			'speedOut'		:	200,
			'overlayShow'	:	true,
			'type'			: 'iframe',
			'width'			: 730,
			'hideOnOverlayClick': true
		});
	    $("form.transform").jqTransform();
	}
);
function makeOptions (id, from, to, step) {
	var sel = $('#' + id).get(0);
	optNum = 0;
	sel.options.length = 0;
	for (var i = from; i <= to; i += step) {
		opt = new Option(i, i);
		sel.options[optNum] = opt;
		optNum++;
	}
}
function doMath() {
	var params = {
		type: $('#type').val(),
		term: $('#term').val(),
		sum: $('#sum').val()
		};
	if ($('#sum').val() < 3050 && $('#term').val() > 36) {
		alert('Kredītu ar termiņu vairāk par 3 gadiem piedāvājam tikai automašīnu pirkumiem ar summu virs Ls 3000');
		$('#scheduleLink').attr('href', '#');
		return;
	}
	if ($('#sum').val() <= 500 && $('#type').val() == 'car') {
		$('#type').val('money');
	}
	$('#scheduleLink').attr('href', 'graph.php?sum=' + $('#sum').val() + '&term=' + $('#term').val() + '&type=' + $('#type').val());
	$.get('/do_math.php', params, function (data) { $('#monthly').html(data);});
}
function makeTermOptions(termMax) {
	var sel = $('#term').get(0);
	optNum = 0;
	sel.options.length = 0;
	opt = new Option(3, 3);
	sel.options[optNum] = opt;
	optNum++;
	opt = new Option(6, 6);
	sel.options[optNum] = opt;
	optNum++;
	opt = new Option(12, 12);
	sel.options[optNum] = opt;
	optNum++;
	opt = new Option(18, 18);
	sel.options[optNum] = opt;
	optNum++;
	opt = new Option(24, 24);
	sel.options[optNum] = opt;
	optNum++;
	opt = new Option(36, 36);
	sel.options[optNum] = opt;
	optNum++;
	if (termMax == 60) {
		opt = new Option(48, 48);
		sel.options[optNum] = opt;
		optNum++;
		opt = new Option(60, 60);
		sel.options[optNum] = opt;
		optNum++;
	}
}