function fillType(first, second, field, cat) {
	var first_val = $(first).value;
	if (first_val == '') {
		$(second).set('html', '');
		return;
	}
	$(first).disabled = 1;
	$(second).disabled = 1;

	if ($defined($('cat'))) {
		cat = $('cat').value;
	} else {
		cat = 0;
	}

	var ajax = new Request({method: 'get', url: "admin/ajax.php", onSuccess: function(res){
		$(first).disabled = 0;
		$(second).disabled = 0;

		// IE Fix for select
		if (Browser.Engine.trident) {
			if ($defined($(second+'_store'))) {
				var html = '<select name="'+field+'" id="'+second+'">'+res;
				html += '</select>';
				$(second+'_store').set('html', html);
			}
		}

		// Other browsers
		else
			$(second).set('html', res);
	}});
	ajax.send('field='+field+'&value='+first_val+'&cat='+cat);
}

function fillType2() {
	var first_val = $('cat').value;
	if (first_val == '') {
		$('type').set('html', '');
		$('zavod').set('html', '');
		$('subtype').set('html', '');
		$('brand').set('html', '');
		return;
	}
	$('cat').disabled = 1;
	$('type').disabled = 1;
	$('zavod').disabled = 1;

	var ajax = new Request({method: 'get', url: "admin/ajax.php", onSuccess: function(res){
		$('cat').disabled = 0;
		$('type').disabled = 0;

		// IE Fix for select
		if (Browser.Engine.trident) {
			if ($defined($('type_store'))) {
				var html = '<select name="att_type" id="type" onchange="fillType(\'type\',\'subtype\',\'subtype\')">'+res;
				html += '</select>';
				$('type_store').set('html', html);
			}
		}

		// Other browsers
		else
			$('type').set('html', res);
	}});
	ajax.send('field=type&value='+first_val);

	var ajax = new Request({method: 'get', url: "admin/ajax.php", onSuccess: function(res){
		$('cat').disabled = 0;
		$('zavod').disabled = 0;

		// IE Fix for select
		if (Browser.Engine.trident) {
			if ($defined($('zavod_store'))) {
				var html = '<select name="att_zavod" id="zavod" onchange="fillType(\'zavod\',\'brand\',\'brand\')">'+res;
				html += '</select>';
				$('zavod_store').set('html', html);
			}
		}

		// Other browsers
		else
			$('zavod').set('html', res);
	}});
	ajax.send('field=zavod&value='+first_val);
}

function fillType2_main() {
	var first_val = $('x_cat').value;
	if (first_val == '') {
		$('x_type').set('html', '');
		$('x_zavod').set('html', '');
		$('x_subtype').set('html', '');
		$('x_brand').set('html', '');
		return;
	}
	$('x_cat').disabled = 1;
	$('x_type').disabled = 1;
	$('x_zavod').disabled = 1;

	var ajax = new Request({method: 'get', url: "admin/ajax.php", onSuccess: function(res){
		$('x_cat').disabled = 0;
		$('x_type').disabled = 0;

		// IE Fix for select
		if (Browser.Engine.trident) {
			if ($defined($('x_type_store'))) {
				var html = '<select name="att_type" id="x_type" onchange="fillType(\'x_type\',\'x_subtype\',\'subtype\')">'+res;
				html += '</select>';
				$('x_type_store').set('html', html);
			}
		}

		// Other browsers
		else
			$('x_type').set('html', res);
	}});
	ajax.send('field=type&value='+first_val);

	var ajax = new Request({method: 'get', url: "admin/ajax.php", onSuccess: function(res){
		$('x_cat').disabled = 0;
		$('x_zavod').disabled = 0;

		// IE Fix for select
		if (Browser.Engine.trident) {
			if ($defined($('x_zavod_store'))) {
				var html = '<select name="att_zavod" id="x_zavod" onchange="fillType(\'x_zavod\',\'x_brand\',\'brand\')">'+res;
				html += '</select>';
				$('x_zavod_store').set('html', html);
			}
		}

		// Other browsers
		else
			$('x_zavod').set('html', res);
	}});
	ajax.send('field=zavod&value='+first_val);
}

function doSearch(page) {
	$('searchForm').set('send', {onSuccess: function(res){
		if (res.length == 0)
			res = '<div class="nothing">Ничего не найдено</div>';
		$('body_content').set('html',res);
	}});
	$('searchForm').page.value = page;
	$('searchForm').send();
}

function doSearchAll(page) {
	$('filterForm').set('send', {onSuccess: function(res){
		if (res.length == 0)
			res = '<div class="nothing">Ничего не найдено</div>';
		res = '<div class="main_title"><img src="templates/first//images/barrier_bg_vertical.jpg" class="barrier_bg_vertical" alt="barrier_bg_vertical" /><img src="templates/first//images/lifting_crane.png" class="lifting_crane" alt="lifting_crane" /><h1>Результаты поиска</h1></div>' + res;
		$('main_content').set('html',res);
	}});
	$('filterForm').page.value = page;
	$('filterForm').send();
}

function loadBrand(brand, cat, page) {
	var ajax = new Request({method: 'post', url: "admin/ajax.php", onSuccess: function(res){
		if (res.length == 0)
			res = '<div class="nothing">Ничего не найдено</div>';
		$('body_content').set('html',res);
	}});
	ajax.send('task=brand&brand='+brand+'&cat='+cat+'&page='+page);
}

function loadBankPage(type, page) {
	var ajax = new Request({method: 'post', url: "admin/ajax.php", onSuccess: function(res){
		if (res.length == 0)
			res = '<div class="nothing">Ничего не найдено</div>';
		if (type == 2) $('body_content2').set('html',res);
		else $('body_content').set('html',res);
	}});
	ajax.send('task=bank&type='+type+'&page='+page);
}


window.addEvent('domready', function(){
if ($defined($('credit_calc'))) {

}
});

var Calc = new Class();

Calc.implement({
	credit: function() {
		var form = $('credit_calc');
		var data = {};

		data.price = $type(form.price.value.toInt()) ? form.price.value.toInt() : 0;
		data.first_pay = $type(form.first_pay.value.toInt()) ? form.first_pay.value.toInt() : 0;
		data.percent = $type(form.percent.value.toInt()) ? form.percent.value.toInt() : 0;
		data.comission = $type(form.comission.value.toInt()) ? form.comission.value.toFloat() : 0;
		data.sum = data.price - data.first_pay;
		data.com = (data.sum*data.comission/100).toFloat();

		data.month = [];
		data.prc = [];

		form.price.value = data.price;
		form.first_pay.value = data.first_pay;
		form.percent.value = data.percent;
		form.comission.value = data.comission;

		if (data.percent <= 0 || data.comission <= 0 || data.price <= 0 || data.first_pay <= 0) {
			alert('Заполните все поля');
			return;
		}
		if (data.sum <= 0) {
			alert('Стоимость автомобиля меньше начального платежа');
			return;
		}

		// Fill results

		$('sum').set('html', data.sum + ' ' + form.cur.value);
		$('com').set('html', data.com + ' ' + form.cur.value);

		var years = [1, 2, 3, 5, 7];
		$$('DIV.month').each(function(el, key){
			if (1-Math.pow(1+data.percent/12/100, -years[key]*12) == 0)
				month = 0;
			else
				month = (data.sum*data.percent/12*365/360/100)/(1-Math.pow(1+data.percent/12*365/360/100, -years[key]*12)) + data.com;
			el.set('html', month.toFloat().round(2));
			data.month.push(month);
		});

		$$('DIV.prc').each(function(el, key){
			prc = data.month[key]*years[key]*12 - data.sum;
			el.set('html', prc.toFloat().round(2));
			data.prc.push(prc);
		});

		$$('DIV.up').each(function(el, key){
			up = data.prc[key]/data.price*100;
			el.set('html', up.toFloat().round(2) + '%');
		});

		$$('.hide').each(function(el){
			el.style.display = 'block';
		});
	},

	changeCurr: function() {
		var curr = $('credit_calc').cur.value;
		$$('SPAN.curr').each(function(el){
			el.set('html', curr);
		});
	},

	leasing: function() {
		$('credit_calc').set('send', {onSuccess: function(res){
			if (res.length == 0)
				res = '<div class="nothing">Error</div>';
			$('result_table').set('html',res);
			$$('.hide').each(function(el){
				el.style.display = 'block';
			});
		}});
		$('credit_calc').send();
	}
});