﻿var brand_id, model_id, compl_id = 0;
function outmsg(msg,ctrlwidth)
{
   msg = "  "+msg
   newmsg = msg
   while (newmsg.length*2 < ctrlwidth) {newmsg += msg}
   
   $("#scrolltext").html('<FORM NAME="Outmsg"><CENTER><INPUT NAME="outmsg" VALUE= "'+newmsg+'" style="width:'+ctrlwidth+'px; border: 0px; font: 30px Times New Roman; font-weight: bold; color: #980000"></CENTER></FORM>');
   if (typeof window.document.Outmsg  != "undefined")
	rollmsg()
}
function rollmsg()
{
   NowMsg=document.Outmsg.outmsg.value
   NowMsg=NowMsg.substring(1,NowMsg.length)+NowMsg.substring(0,1)
   document.Outmsg.outmsg.value = NowMsg
   bannerid=setTimeout("rollmsg()",300)
}
$(document).ready(function(){
	//$("div[id^='aa']").hide();
	
	outmsg("ВСЕ МОДЕЛИ В НАЛИЧИИ! СТАВКИ СНИЖЕНЫ!",560);
	
	if (typeof brand != "undefined" && brand != ""){
	    $("#"+brand).show();
		//div = $(".t2_5 a[href*='"+brand+"']").parent().parent();
		//id = div.attr('id');
		//idn = id.substring(1);
		$(".visual-list a[href*="+brand+"]").parent().addClass('active');
		//console.log($(".visual-list a[href*="+brand+"]").html())
	}
	
	hash = document.location.hash;
	if (hash.match(/#anchc(.*)/i)) {
		compl = hash.replace("#anchc", "");
		$("#compl"+compl).slideToggle();
	}
	
	$(".visual-list a").each(function (){
      $(this).hover(
		  function () {
			if (!$(this).parent().hasClass('active')){
				
				brandsrc = $(this).children("img[alt=brand]").attr("src");
				newbrandsrc = brandsrc.substr(5);
				newbrandsrc = newbrandsrc.substr(0, newbrandsrc.length - 4);
				//$(this).children("img[alt=brand]").attr("src", "/img/"+newbrandsrc+"_big.jpg");
				
				src = $(this).children(".title").attr("src");
				newsrc = src.substr(11);
				newsrc = newsrc.substr(0, newsrc.length - 4);
				$(this).children(".title").attr("src", "/img/title/"+newsrc+"_red.gif");
			}
			}, 
		  function () {
			if (!$(this).parent().hasClass('active')) {
				$(this).children("img[alt=brand]").attr("src", brandsrc);
				$(this).children(".title").attr("src", src);
			}
		  });
	});
	/*
	activebrandsrc = $("li.active img[alt=brand]").attr("src");
	activesrc = $("li.active .title").attr("src");
	if (activesrc != null){
		$("li.active .title").attr("src", "/img/title/"+activenewsrc+"_red.gif");
	}
	*/
	/*
	$(".tab_content").hide();
	$("ul.tabs li:first a").addClass("active"); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li a").click(function() {

		$("ul.tabs li a").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	*/
	$(".tab_content2").hide();
	$("ul.tabs2 li:first a").addClass("active"); //Activate first tab
	$(".tab_content2:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs2 li a").click(function() {

		$("ul.tabs2 li a").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content2").hide(); //Hide all tab content

		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

	$(".tabs a").hover(
      function () {
        $(this).addClass("active2");
      }, 
      function () {
        $(this).removeClass("active2");
      }
    );
	
	$("#feedbackform").submit(function() { 
                
		if($("#feedbackform #name").val() == "" || $("#feedbackform #name").val() == "Имя")   
			$("#div-feedbackform").html("Сообщите ваше имя!");
		
		else if($("#feedbackform #phone1").val() == "" || $("#feedbackform #phone1").val() == "Телефон")
			$("#div-feedbackform").html("Сообщите ваш телефон!");
			
		else if($("#feedbackform #extra").val() == "" || $("#feedbackform #extra").val() == "Сообщение")
			$("#div-feedbackform").html("Напишите ваше сообщение!");
			
		else 
		{
			$.post("/feedback?action=feedback",
			{ brand: $("#feedbackform input[name=brand]").val(),
			  model: $("#feedbackform input[name=model]").val(),
			  name: $("#feedbackform #name").val(), 
			  phone: $("#feedbackform #phone1").val(),
			  mail: $("#feedbackform #mail").val(),
			  extra: $("#feedbackform #extra").val()
			},
				function(data){
					$("#div-feedbackform").html(data);
			});
		}
		
		return false;
	});
	
	$("#vykupform").submit(function() { 
                
		if($("#vykupform input[name=maker_id]").val() == "")   
			$("#div-vykupform").html("Укажите марку автомобиля!");
		else if($("#vykupform input[name=model_id]").val() == "")   
			$("#div-vykupform").html("Укажите модель автомобиля!");
		else if($("#vykupform input[name=mileage]").val() == "")   
			$("#div-vykupform").html("Укажите пробег автомобиля!");
		else if($("#vykupform input[name=customer_name]").val() == "")   
			$("#div-vykupform").html("Укажите Ваше имя!");
		else if($("#vykupform input[name=customer_phone]").val() == "")
			$("#div-vykupform").html("Укажите Ваш телефон!");
		else if($("#vykupform input[name=customer_email]").val() == "")
			$("#div-vykupform").html("Укажите Ваш email!");
		else 
		{
			$.post("/feedback?action=vykup",
			{	brand: $("#vykupform input[name=maker_id]").val(),
				model: $("#vykupform input[name=model_id]").val(),
				body: $("#vykupform select[name=body_id]").val(),
				engine_type: $("#vykupform select[name=engine_type_id]").val(),
				engine_vol: $("#vykupform select[name=engine_vol_id]").val(),
				transmission: $("#vykupform select[name=transmission_id]").val(),
				year: $("#vykupform select[name=year]").val(),
				mileage: $("#vykupform input[name=mileage]").val(),
				city: $("#vykupform select[name=city_id]").val(),
				name: $("#vykupform input[name=customer_name]").val(), 
				phone: $("#vykupform input[name=customer_phone]").val(),
				mail: $("#vykupform input[name=customer_email]").val(),
				comments: $("#vykupform textarea[name=customer_comments]").val()
			},
				function(data){
					$("#div-vykupform").html(data);
			});
		}
		
		return false;
	});

	$("#buycarform").submit(function() { 
                
		if($("#buycarform input[name=maker_id]").val() == "")   
			$("#div-buycarform").html("Укажите марку автомобиля!");
		else if($("#buycarform input[name=model_id]").val() == "")   
			$("#div-buycarform").html("Укажите модель автомобиля!");
		else if($("#buycarform input[name=mileage]").val() == "")   
			$("#div-buycarform").html("Укажите пробег автомобиля!");
		else if($("#buycarform input[name=price]").val() == "")   
			$("#div-buycarform").html("Укажите цену автомобиля!");
		else if($("#buycarform input[name=customer_name]").val() == "")   
			$("#div-buycarform").html("Укажите Ваше имя!");
		else if($("#buycarform input[name=customer_phone]").val() == "")
			$("#div-buycarform").html("Укажите Ваш телефон!");
		else if($("#buycarform input[name=customer_email]").val() == "")
			$("#div-buycarform").html("Укажите Ваш email!");
		else 
		{
			$.post("/feedback?action=buycar",
			{	brand: $("#buycarform input[name=maker_id]").val(),
				model: $("#buycarform input[name=model_id]").val(),
				transmission: $("#buycarform select[name=transmission_id]").val(),
				year: $("#buycarform select[name=year]").val(),
				mileage: $("#buycarform input[name=mileage]").val(),
				price: $("#buycarform input[name=price]").val(),
				city: $("#buycarform select[name=city_id]").val(),
				name: $("#buycarform input[name=customer_name]").val(), 
				phone: $("#buycarform input[name=customer_phone]").val(),
				mail: $("#buycarform input[name=customer_email]").val(),
				comments: $("#buycarform textarea[name=customer_comments]").val()
			},
				function(data){
					$("#div-buycarform").html(data);
			});
		}
		
		return false;
	});
	
	
	
	$(".credit-button a img").hover(function(){$(this).attr("src", "/uploads/images/krakt.jpg")}, function(){$(this).attr("src", "/uploads/images/krneakt.jpg")});
    $(".credit-button a").click(function() {$("ul.tabs li a[href='#tabcredit']").click()});
	
	$("table tr.car").hover(function(){
        $(this).toggleClass("tr_hover");
		$("table tr.car td a").toggleClass("hover");
    });
	
	$(".editlink").click(function (){
		$(this).attr("href");
		returnlink = location.href;
		returnlink = returnlink.replace(/(http:\/\/(.*)\/)(catalog)(.*)/, "/$3$4");
		returnlink = escape(returnlink);
		location.href = $(this).attr("href") + "&returnlink=" + returnlink;
		return false;
	});
	
	$('<select name="brand" class="forms-08"><option value="0">выберите марку</option></select>').replaceAll("#creditform input[name=brand]");
	$('<select name="model" class="forms-08"><option value="0">выберите модель</option></select>').replaceAll("#creditform input[name=model]");
	$('<select name="cost" class="forms-08"><option value="0">выберите комплектацию</option></select>').replaceAll("#creditform input[name=cost]");
	
	$('#creditform select[name=sost]').change(function(){
		if ($('#creditform select[name=sost] option:selected').val() == 'автомобиль с пробегом') {
			inittrade();
		} else {
			initnew();
		}
	});
	
	$('#creditform select[name=sost]').change();
	
	if (!$('#creditform select[name=sost]').length)
		initnew();
	
	
	$('<select name="brand"><option value="0">выберите марку</option></select>').replaceAll("#evalform input[name=brand]");
	$('<select name="model"><option value="0">выберите модель</option></select>').replaceAll("#evalform input[name=model]");
	
	$("#evalform").submit(function() {
		if($("#evalform input[name=name]").val() == "") {
			alert("Укажите Ваше имя");
			return false;
		} else if ($("#evalform input[name=phone]").val() == "") {
			alert("Укажите контактный телефон");
			return false;
		} else {
			$("#evalform select[name=brand] option:selected").val($("#evalform select[name=brand] option:selected").html());
			$("#evalform select[name=model] option:selected").val($("#evalform select[name=model] option:selected").html());
			return true;
		}
	});
	
	if ($('body').hasClass('eval')){
		$.get('/feedback?action=eval_brand', function(data) {
			$('#evalform select[name=brand]').append(data);
		});
	}
	
	$("#evalform select[name=brand]").change(function(){
		$("#evalform select[name=model] option").remove();
		$.get('/feedback?action=eval_model&brand='+$("option:selected", this).val(), function(data) {
			$('#evalform select[name=model]').append(data).show();
		});
	});
	
	$('#credit-wrap input:radio').screwDefaultButtons({
		checked: "url(/img/credit-wrap-radio-checked.png)",
		unchecked: "url(/img/credit-wrap-radio-unchecked.png)",
		width: 17,
		height: 17
	});
});

function inittrade() {
	$("#credit-wrap select").selectBox('destroy');
	
	$('<select name="brand" class="forms-08"><option value="0">выберите марку</option></select>').replaceAll("#creditform select[name=brand]");
	$('<select name="model" class="forms-08"><option value="0">выберите модель</option></select>').replaceAll("#creditform select[name=model]");
	$('<input class="forms-03" type="text" value="" name="cost">').replaceAll("#creditform select[name=cost]");
	
	$('#creditform select[name=brand]').change(function(){
		//$('<select name="model" class="forms-08"><option value="0">выберите модель</option></select>').replaceAll("#creditform select[name=model]");
		$.get('/feedback?action=eval_model&brand='+$("option:selected", this).val(), function(data) {
			//$('#creditform select[name=model]').append(data);
			$('#creditform select[name=model]').selectBox('options', '<option value="0">выберите модель</option>'+data);
			$('#creditform select[name=model]').change();
		});
	});
	
	$.get('/feedback?action=eval_brand', function(data) {
		//$('#creditform select[name=brand]').append(data);
		$('#creditform select[name=brand]').selectBox('options', '<option value="0">выберите марку</option>'+data);
		
	});
	$("#creditform").unbind("submit");
	$("#creditform").submit(function() {
		if ($("#creditform select[name=brand] option:selected").val() == 0) {
			alert("выберите марку");
			return false;
		} else if ($("#creditform select[name=model] option:selected").val() == 0) {
			alert("выберите модель");
			return false;
		} else {
			$("#creditform select[name=brand] option:selected").val($("#creditform select[name=brand] option:selected").html());
			$("#creditform select[name=model] option:selected").val($("#creditform select[name=model] option:selected").html());
			return true;
		}
	});
	
	$("#credit-wrap select").selectBox();
}

function initnew() {
	$("#credit-wrap select").selectBox('destroy');
	
	$('<select name="brand" class="forms-08"><option value="0">выберите марку</option></select>').replaceAll("#creditform select[name=brand]");
	$('<select name="model" class="forms-08"><option value="0">выберите модель</option></select>').replaceAll("#creditform select[name=model]");
	$('<select name="cost" class="forms-08"><option value="0">выберите комплектацию</option></select>').replaceAll("#creditform input[name=cost]");
	
	$('#creditform select[name=brand]').change(function(){
		$("#creditform select[name=model] option:gt(0)").remove();
		$("#creditform select[name=cost] option:gt(0)").remove();
		$('#creditform select[name=cost]').selectBox('options', $('#creditform select[name=cost]').html());
		$.get('/feedback?action=getmodel&brand='+$("option:selected", this).val(), function(data) {
			$('#creditform select[name=model]').append(data);
			$('#creditform select[name=model]').selectBox('options', $('#creditform select[name=model]').html());
			if (model_id != 0) {
				$("#creditform select[name=model]").selectBox('value', model_id);
				$("#creditform select[name=model]").change();
			}	
		});
	});
	
	$("#creditform select[name=model]").change(function(){
		$("#creditform select[name=cost] option:gt(0)").remove();
		$.get('/feedback?action=get_compl&model='+$("option:selected", this).val(), function(data) {
			$('#creditform select[name=cost]').append(data);
			$('#creditform select[name=cost]').selectBox('options', $('#creditform select[name=cost]').html());
			if (compl_id != 0) {
				$("#creditform select[name=cost]").selectBox('value', compl_id);
			}	
		});
	});
	
	$.get('/feedback?action=getbrand', function(data) {
		//$('#creditform select[name=brand]').append(data);
		$('#creditform select[name=brand]').selectBox('options', '<option value="0">выберите марку</option>'+data);
		if (brand_id != 0) {
			$("#creditform select[name=brand]").selectBox('value', brand_id);
			$('#creditform select[name=brand]').change();
		}
	});
	$("#creditform").unbind("submit");
	$("#creditform").submit(function() {
		if ($("#creditform select[name=brand] option:selected").val() == 0) {
			alert("выберите марку");
			return false;
		} else if ($("#creditform select[name=model] option:selected").val() == 0) {
			alert("выберите модель");
			return false;
		} else if ($("#creditform select[name=cost] option:selected").val() == 0) {
			alert("выберите комплектацию");
			return false;
		} else {
			$("#creditform select[name=brand] option:selected").val($("#creditform select[name=brand] option:selected").html());
			$("#creditform select[name=model] option:selected").val($("#creditform select[name=model] option:selected").html());
			$("#creditform select[name=cost] option:selected").val($("#creditform select[name=cost] option:selected").html());
			return true;
		}
	});
	
	$("#credit-wrap select").selectBox();
}

function evalform() {
	$('#popup_div').togglePopup({ajaxurl: '/feedback?action=evaluation', width: 650, height: 500, iframe: true});
}

function backcall() {
	PopUp('/feedback?action=backcall', 'backcall', 400, 320, 1);
}

function vykup() {
	PopUp('/feedback?action=vykup2', 'vykup2', 400, 510, 1);
}

function otzyv() {
	PopUp('/feedback?action=otzyv', 'otzyv', 350, 280, 1);
}

function director() {
	PopUp('/feedback?action=director', 'director', 400, 310, 1);
}

function show(x)
{
  document.getElementById('d'+x).className = 'a'+x+'_';
  var i=1; j=1;
  while (i<18)
  {
   if (i != x)
   {
	document.getElementById('d'+i).className = 'a'+i;
   }
   i=i+1;
  }

  document.getElementById('aa'+x).style.display = "block";
  while (j<18)
  {
   if (j != x)
   {
  document.getElementById('aa'+j).style.display = "none";
   }
   j=j+1;
  }

}

function PopUp(url, name, width,height,center,posleft,postop)
{
	showx = "";
	showy = "";
	
	if (posleft != 0) { X = posleft; }
	if (postop  != 0) { Y = postop  }
	
	if ((parseInt (navigator.appVersion) >= 4 ) && (center))
	{
		X = (screen.width  - width ) / 2;
		Y = (screen.height - height) / 2;
	}
	
	if (posleft == 0)
	{
		X = (screen.width  - width ) / 2;
	}	
	if ( X > 0 )
	{
		showx = ',left='+X;
	}

	if(postop  == 0)
	{
		Y = (screen.height - height) / 2;
	}	
	if ( Y > 0 )
	{
		showy = ',top='+Y;
	}
	
	Win = window.open( url, name, 'width='+width+',height='+height+ showx + showy + ',resizable=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no');
	return Win;
}

function em(box, server, zone, img)
{
 var at = String.fromCharCode(64);
 if (img == 1)
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">';
 else
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">'+box+at+server+'.'+zone+'<'+'/a>';
}

function calculate()
{
    with (document.forms['fm1'])
    {
        if(cost.value < 120000)
        {
            alert("Кредит предоставляется на автомобили дороже 120 000 руб."); 
            return false; 
        }
        
        var fpr = firstpersent.value;
        var sum = cost.value*(1-fpr/100);
        var pc = 8;
        
        firstpay.value = Math.round(cost.value*fpr/100);
        monthpay.value = Math.round((sum*pc/1200)/(1-Math.pow(1/(1+pc/1200), period.value)));
    }
}

