function checkTDForm()
{
	var cheking = true;
	var fio = document.getElementById("fio").value;
	var phone = document.getElementById("phone").value;
	var email = document.getElementById("email").value;
	
	if ( fio == "")
	{
		cheking = false;
	}
	if ( phone == "" )
	{
		cheking = false;
	}
	if ( email == "")
	{
		cheking = false;
	}
	if ( cheking == false ) alert("Заполните необходимые поля");

	return cheking;
}

function roundEx(x, dgt)
{
	var p, n;
	p=Math.floor(Math.pow(10,dgt));
	(x>0) ? n = Math.floor(x) : n = Math.ceil(x);
	return(n + Math.round((x-n)*p)/p);
};

function ChangeModel (idmark, sel) {
	$('#car_'+sel).css("display","none");
	$('#car_'+idmark).css("display","block");
	return idmark;
}

function MoneyFormat(x)
{
	var p,i,j;
	var s="", s2="";
	p=roundEx(x,2);	s=p.toString();
	p=s.lastIndexOf(".");
	if (p>0) {s2="," + s.substring(p+1, s.length); s = s.substring(0,p);} else {s2=",00";};
	if(s2.length<3)s2+="0";	j=1; i=s.length;
	for((i%3)?i=Math.floor(i/3):i=Math.floor(i/3)-1; i>0;--i)
	{
		p=s.length-(3*j)-(j-1);j++;
		s=s.substring(0,p) + " "+ s.substring(p,s.length);
	}
	return(s+s2);
};

function KASKO()
{
	var tarif1 = new Array(3,3);
	var tarif2 = new Array(3,3);
	var tarif3 = new Array(3,3);
	var tarif4 = new Array(3,3);
	var tarif5 = new Array(3,3);
	var age = $('#age').val();
	var stage = $('#stage').val();
	// Tarif 1
	tarif1 = [[21.50,18.35,16.25,13.10],[16.25,13.10,12.05,11],[13.10,11,9.95,9.43],[13.10,11,8.90,8.90]];
	tarif2 = [[11.60,10.04,9,7.44],[9,7.44,6.92,6.40],[7.44,6.40,5.88,5.62],[7.44,6.40,5.36,5.36]];
	tarif3 = [[12.60,10.89,9.75,8.04],[9.75,8.04,7.47,6.90],[8.04,6.90,6.33,6.05],[8.04,6.90,5.76,5.76]];
	tarif4 = [[16.10,13.76,12.20,9.86],[12.20,9.86,9.08,8.30],[9.86,8.30,7.52,7.13],[9.86,8.30,6.74,6.74]];
	tarif5 = [[11.30,9.68,8.60,6.98],[8.60,6.98,6.44,5.90],[6.98,5.90,5.36,5.09],[6.98,5.90,4.82,4.82]];

	amount = 0;
	car_id = $('#model').val();
	if ( $('#model').value == 26) {
		amount = $('#completing_'+car_id).val()/100 * tarif1[stage][age];
	} else if ( $('#model').val() == 35 || $('#model').val() == 36 || $('#model').val() == 34 || $('#model').val() == 49 || $('#model').val() == 46 || $('#model').val() == 42) { // Джипы
		amount = $('#completing_'+car_id).value/100 * tarif2[stage][age];
	} else if ( $('#model').val() == 20 || $('#model').val() == 19 ) {
		amount = $('#completing_'+car_id).val()/100 * tarif3[stage][age];
	} else if ( $('#model').val() == 37 ) {
		amount = $('#completing_'+car_id).val()/100 * tarif5[stage][age];
	} else {
		amount = $('#completing_'+car_id).val()/100 * tarif4[stage][age];
	}
	
	$('#result').html(MoneyFormat(amount)+' руб.');
}

function OSAGO()
{
	var amount = 0;
	
	if ( $('#owner').val() == 2 )
	{
		$('#drivernum').attr('disabled','true');
		$('#driverage').attr('disabled','true');
		$('#driverexp').attr('disabled','true');
	} else {
		$('#drivernum').removeAttr('disabled');
		$('#driverage').removeAttr('disabled');
		$('#driverexp').removeAttr('disabled');
	}
	
	amount = 2375;
	if ( $('#owner').val() == 1 ) amount = 1980;
	amount = amount * $('#region').val();
	
	if ( $('#owner').val() == 1 ) {
		if ( $('#drivernum').val() == 1 ) {
			amount = amount * 1;
		} else {
			amount = amount * 1.5;
			$('#driverage').attr('disabled','true');
			$('#driverexp').attr('disabled','true');
		}
		if ( $('#drivernum').val() == 1 ) {
			if ( $('#driverage').val() == 1 ) {
				if ( $('#driverexp').val() == 1 ) amount = amount * 1.3; else amount = amount * 1.2;
			} else if ( $('#driverage').val() == 2 ) {
				if ( $('#driverexp').val() == 1 ) amount = amount * 1.15; else amount = amount * 1;
			}
		}
	} else if ( $('#owner').val() == 2 )  {
		amount = amount * 1.5;
	}
	amount = amount * $('#power').val();
	amount = amount * $('#used').val();
	amount = amount * $('#ub').val();
	$('#result').html(MoneyFormat(amount)+' руб.');
}

$.fn.smartBgImage = function(title, color, url) {
	var container = $(this);
	var img = container.find("img.cview-img");

	img.css("display","none");
	img.attr('src', url);
    img.bind('load',function() {
    	$('#cbox').css("background-color", color);
		$('#ctitle').html(title);
    	img.fadeIn("slow");
    });

	return true;
};

$.fn.popupMenu = function() {
	var container = $(this);
	container.find('> A').click(function() {
		var pblock = $(this).parent().find('> P');

		if ( pblock.css("display") == "block" ) {
			pblock.fadeOut("fast");
			$(this).find('> IMG').attr("src", "/themes/default/img/btn_plus.gif");
		} else {
			pblock.fadeIn("fast");
			$(this).find('> IMG').attr("src", "/themes/default/img/btn_minus.gif");
		}
		return false;
	});
	
	container.find('> P').click(function() {
		$(this).fadeOut("fast");
		$(this).parent().find('> A IMG').attr("src", "/themes/default/img/btn_plus.gif");
	});
};

/* FADE MAIN MENU */
$.fn.fadeMenu = function(options) {
	this.each(function() {
		function getSubmenu(element) {
			if (element.nodeName.toLowerCase() == 'li') {
				var submenu = $('> ul', element);
				return submenu.length ? submenu[0] : null;
			} else {
				return element;
			}
    	}
    	
    	function show() {
    		var subnav = getSubmenu(this);
    		if (!subnav) return;
    		$('> A', this).addClass("on");
    		$(subnav).slideDown('fast');
    	}
    	
    	function hide() {
    		var subnav = getSubmenu(this);
    		$('> A.on', this).removeClass("on");
    		$(subnav).css("display","none"); //slideUp(); 
    	}

		$('li', this).hover(show, hide);
	});
};

/* INIT WIDGETS */
$(document).ready(function() {
	$('#menu').fadeMenu();
});
