
function showCalc () {
	var psize=getPageSize();
	$(".shadow").show().css({height:psize});
	document.getElementById('calxForm').reset();
	setFieldsByDeposit('prognoz', 1);
	setFieldsByDeposit('flexmoney', 2);
	setFieldsByDeposit('freedom', 3);
	setFieldsByDeposit('classic', 4);
	

	$(".calc").show();

	$(".shadow").click(function(){
		$(this).hide();
		$("#c_result").html(" ");
		$(".resultBg, .sh").hide();
		$(".calc").hide();
		if($.browser.msie && $.browser.version=="6.0") {
			location.reload(true)
			}
		document.getElementById('calxForm').reset();
		chekPopup ();
	});



}



function chekPopup () {
	if ($("input.chekPopup:checked").length>0)
			{
				setFieldsByDeposit('classic', 4);
				$('#deposit_type4 > option[value="classic"]').attr('selected','selected');
				$(".sh").show();
				$('#flvvideo').hide();
			} else {
				$(".sh").hide();

	}
}


function showRes(){
	$(".resultBg, .obvodka").show();

}

function  getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	if (pageWidth<windowWidth) {
	pageWidth=windowWidth
	}
	if (pageHeight<windowHeight) {
		pageHeight=windowHeight
	}
	return [pageHeight+10];
}

function sumInput(id_sum_input){

		if ($("input.chekPopup:checked").length>0){
			var sum_input = 0;
			if($('#textfield'+id_sum_input).val()){
				sum_input = $('#textfield'+id_sum_input).val();
			}
			if(sum_input){
				for(var i=1; i <= 4; i++){
					if(i != id_sum_input){
						if(!$('#textfield'+i).val()){
							$('#textfield'+i).val(sum_input);
						}
					}
				}
			}
		}

}

	function setFieldsByDeposit(itype, iid){

			switch (itype)
			{
				case 'prognoz':
					$('#summ_normal'+iid).show();
					$('#summ_multi'+iid).hide();
					$('#bucks'+iid).show();
					$('#hrn'+iid).show();
					$('#euro'+iid).hide();
					$('#Vklad'+iid).empty();
					$('#Vklad'+iid).append('<option value="1">щомісячно та в кінці періоду</option>');
					$('#Days'+iid).hide();
					getPeriodsByType(itype, iid);
					$('#Periods'+iid).show();
					$('#div_plus'+iid).hide();

					document.getElementById('curr_id'+iid+'_'+0).checked = 1;
				break;

				case 'freedom':
					$('#summ_normal'+iid).show();
					$('#summ_multi'+iid).hide();
					$('#hrn'+iid).show();
					$('#bucks'+iid).show();
					$('#euro'+iid).show();
					$('#Vklad'+iid).empty();
					$('#Vklad'+iid).append('<option value="1">в кінці строку</option><option value="2">щомісячно</option>');
					$('#Days'+iid).hide();
					getPeriodsByType(itype, iid);
					$('#Periods'+iid).show();
					$('#div_plus'+iid).hide();

					//document.getElementById('curr_id'+iid+'_'+0).checked = 1;
				break;

				case 'classic':
					$('#summ_normal'+iid).show();
					$('#summ_multi'+iid).hide();
					$('#hrn'+iid).show();
					$('#bucks'+iid).show();
					$('#euro'+iid).show();
					$('#Vklad'+iid).empty();
					$('#Vklad'+iid).append('<option value="1">в кінці строку</option><option value="2">щомісячно</option><option value="3">з приєднанням % до вкладу</option>');
					$('#Days'+iid).hide();
					getPeriodsByType(itype, iid);
					$('#Periods'+iid).show();
					$('#div_plus'+iid).show();
				break;

				case 'flexmoney':
					$('#hrn'+iid).hide();
					$('#bucks'+iid).hide();
					$('#euro'+iid).hide();
					$('#summ_normal'+iid).hide();
					$('#summ_multi'+iid).show();
					$('#Vklad'+iid).empty();
					$('#Vklad'+iid).append('<option value="1">в кінці періоду</option>');
					$('#Days'+iid).hide();
					getPeriodsByType(itype, iid);
					$('#Periods'+iid).show();
					$('#div_plus'+iid).hide();
					
					document.getElementById('curr_id'+iid+'_'+0).checked = 1;
				break;

			}

		return false;
		}

	function getPeriodsByType(itype, iid){
		var curr_id;
		for(i = 0; i<=2; i++)
			if (document.getElementById('curr_id'+iid+'_'+i).checked)
				curr_id = document.getElementById('curr_id'+iid+'_'+i).value;
		var vklad_id = document.getElementById('Vklad'+iid).value;
		$.post("./calculator.php", {action: 'periodsbytype', curr_id: curr_id, deposit_type: itype, vklad_id: vklad_id}, function(data){
  			$('#Periods'+iid).empty();
  			$('#Periods'+iid).append(data);
			});

		}

	function getCalxResult(){
		document.calxForm.mode.value = '';
		$.post("./calculator.php", $("#calxForm").serialize(), function(data){
			$('#c_result').empty();
  			$('#c_result').append(data);
				var psize=getPageSize();
				//alert(psize);
				$(".shadow").show().css({height:psize});

			});
		}

	function chechCalxForm(){
		var i = document.calxForm.do_compare.checked ? 1 : 4;
		var entered_summ;
		var entered_days;

		var symbols = new Array();
		symbols[1] = 'гривнi';
		symbols[2] = 'доларах США';
		symbols[3] = 'євро';

		var maxd = 360;

		var mins = new Array();

		while (i <= 4)
		{
			entered_summ = document.getElementById('textfield'+i).value;
			entered_days = document.getElementById('ddys'+i).value;

			var mins = new Array();
			switch (document.getElementById('deposit_type'+i).value)
			{
				case 'prognoz':
				mins[1]=500;
				mins[2]=100;
				break;

				case 'freedom':
				mins[1]=500;
				mins[2]=100;
				mins[3]=100;
				break;

				case 'classic':
				mins[1]=200;
				mins[2]=50;
				mins[3]=50;
				break;

				case 'flexmoney':
				mins[1]=1000;
				mins[2]=100;
				mins[3]=100;
				break;
			}

			for(j = 0; j<=2; j++)
			if (document.getElementById('curr_id'+i+'_'+j).checked)
				curr_id = document.getElementById('curr_id'+i+'_'+j).value;

			if((!entered_summ.match(/^\d+$/) || entered_summ < mins[curr_id]) && document.getElementById('deposit_type'+i).value != 'flexmoney')
			{alert('Сума вкладу в '+symbols[curr_id]+' повинна бути цiлим числом вiд '+mins[curr_id]+'!');
				document.getElementById('textfield'+i).focus();
				return false;
				}
				 else if (document.getElementById('deposit_type'+i).value == 'flexmoney'){
					for(nn = 1; nn <= 3; nn++) if (!document.getElementById('mtextfield'+i+''+nn).value.match(/^\d+$/) || document.getElementById('mtextfield'+i+''+nn).value < mins[nn]){
					alert('Сума вкладу в '+symbols[nn]+' повинна бути цiлим числом вiд '+mins[nn]+'!');
					document.getElementById('mtextfield'+i+''+nn).focus();
					return false;
						}
					 }
				 else if ((!entered_days.match(/^\d+$/) || entered_days > maxd || entered_days < 1) && document.getElementById('deposit_type'+i).value == 'calendar')
			{alert('Кiлькiсть днiв повинна бути цiлим числом до 360!');
				document.getElementById('ddys'+i).focus();
				return false;
				}


			i++;
		}

		return true;

		}

// Банківський радник
function changeAdvice(id){
	if(id == 1){
		$('#advice2').hide();
		$('#advice1').show();
		setFieldsByDeposit('classic', 4);
		$('#deposit_type4 > option[value="classic"]').attr('selected','selected');
	}else{
		$('#advice1').hide();
		$('#advice2').show();
		setFieldsByDeposit('prognoz', 4);
		$('#deposit_type4 > option[value="prognoz"]').attr('selected','selected');
	}
}