var need_hide = true;
var now_sm_opened = "";


function hiding_pop()
{
	if(need_hide)
	{
		$('.popsm').fadeOut(1);		
	}
}


function pics(img,ref)
{
	document.getElementById(img).src = ref;
}

function openwin(img,w,h,title)
{
 var hWnd;
 if(hWnd!=null) hWnd.close();
 hWnd=window.open(img,"","toolbar=no,location=no,directories=no,resizable=yes,scrollbars=yes,left=100,top=100,width="+w+",height="+h);
 hWnd.document.open();
 hWnd.document.write("<html><head><title>"+title+"</title></head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>");
 hWnd.document.write("<img src=\""+img+"\" width="+w+" height="+h+">");
 hWnd.document.write("</body></html>");
 hWnd.document.close();
}

function IEPngFix( pngimg, gifimg, width, height, unique_name )
{
	var code = '<img src="'+ gifimg +'" width="' + width + '" height="' + height + '" border="0" id="'+ unique_name +'" style="background:url('+pngimg+') no-repeat 0px 0px;" alt="'+unique_name+'"/>';
	if( navigator.userAgent.indexOf("Opera") > 1 ) document.write(code);
	else
		if( navigator.userAgent.indexOf("MSIE 6") > 1 ) document.write('<img src="'+gifimg+'" width="' + width + '" height="' + height + '" border="0" alt="'+unique_name+'" id="'+ unique_name +'" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src=' + pngimg + ' , sizingMethod=crop );"/>');
		else
			if(navigator.userAgent.indexOf("MSIE 5") != -1) document.write(code);
			else document.write(code);
}

function switch_head_bg(bg)
{
	document.getElementById("head_pic").style.backgroundImage = "url("+bg+")";
}



function gotolink(link, param_id){
	if(param_id)
	{
		$param_select = $('#'+param_id);
		param = '?'+param_id+'='+$param_select.val();
	} else param = '';

	document.location=link+param;
}


function show_div(id)
{
	var div;
	div = document.getElementById(id);
	if ( div.style.display == "none" ) div.style.display = "block";
		else div.style.display = "none";
}


function init_calc_selects()
{
	$('.only_nums').keypress(function(evt){
		 var charCode = (evt.which) ? evt.which : event.keyCode;
		 //alert("--"+charCode+"--");
		 if (charCode!=116 && charCode!=13 && charCode!=9 && charCode!=8 && charCode!=46 && charCode!=36 && charCode!=35 && charCode!=37 && charCode!=39 && charCode!=0 && (charCode<48 || charCode>57) )
			 return false;
		 return true;
	});


	$('#complectation_select').change(function(){
		//fp = $('#first_pay').val();
		$.ajax({
			url: "/inc/refresh_calculator_block.php?ajax_process=1&complectation_id="+$(this).val()+"&first_pay=-1",
			cache: false,
			success: update_calculator_block
		});
	});
	$('#currency_select').change(function(){
		//fp = $('#first_pay').val();
		$.ajax({
			url: "/inc/refresh_calculator_block.php?ajax_process=1&currency_id="+$(this).val()+"&first_pay=-1",
			cache: false,
			success: update_calculator_block
		});
	});
	$('#period_select').change(function(){
		$.ajax({
			url: "/inc/refresh_calculator_block.php?ajax_process=1&period="+$(this).val()+"",
			cache: false,
			success: update_calculator_block
		});
	});
}


function update_calculator_block(data) {
	$("#calucaltor_block").html(data);
	init_calc_selects();
//	refresh_price($('#price'), null);
	refresh_first_percent($('#first_pay'), null);

	$('#price').keyup(function(evt){
		refresh_price($(this), evt);
	});

}

function update_calculator_block2(data) {
	$("#calucaltor_block2").html(data);
	init_calc_selects();
//	$('#first_pay').focus();
}


$(document).ready(function()
{
	init_calc_selects();

	$('#first_pay').keyup(function(evt){
		refresh_first_percent($(this), evt);
	});

	$('#price').keyup(function(evt){
		refresh_price($(this), evt);
	});



	//	
	$('.menu2 td a, .start_menu2 td a').hover(
		function()
		{
			need_hide = false;
			sm = $(this).attr('sm_name');
			if(sm)
			{
				if(sm != now_sm_opened)
				{
					if(now_sm_opened!="")$('.'+now_sm_opened).fadeOut(1);
					now_sm_opened = sm;
				}
				if($('.'+sm))
				{
					$('.'+sm).fadeIn(1);
				}
			}
		},
		function()
		{
			need_hide = true;
			setTimeout( 'hiding_pop();', 200);
		}
	);

	//	
	$('.popsm').hover(
		function()
		{
			need_hide = false;
		},
		function()
		{
			need_hide = true;
			setTimeout( 'hiding_pop();', 200);
		}
	);



});


function refresh_price(obj, evt)
{
	flag = true;
	if(evt)
	{
		var charCode = (evt.which) ? evt.which : event.keyCode;
		if ( ((charCode >= 48) && (charCode <= 57)) || ((charCode >= 96) && (charCode <= 105)) || (charCode == 46) || (charCode == 8) || (charCode == 45) )
			flag = true;
		else flag = false;
	}		
	if(flag)
	{
		$.ajax({
			url: "/inc/refresh_calculator_block2.php?ajax_process=1&price="+obj.val(),
			cache: false,
			success: //function(){
				update_calculator_block2
		});
	}
}



function refresh_first_percent(obj, evt)
{
	flag = true;
	if(evt)
	{
		var charCode = (evt.which) ? evt.which : event.keyCode;
		if ( ((charCode >= 48) && (charCode <= 57)) || ((charCode >= 96) && (charCode <= 105)) || (charCode == 46) || (charCode == 8) || (charCode == 45) )
			flag = true;
		else flag = false;
	}		
	if(flag)
	{
		$.ajax({
			url: "/inc/refresh_calculator_block2.php?ajax_process=1&first_pay="+obj.val(),
			cache: false,
			success: //function(){
				update_calculator_block2
		});
	}
}
