//Скрипты
function nevibrano() {
	$("select").append($("<option></option>").attr("value","Не выбрано").attr("value","Не выбрано").attr('selected', 'selected').text("Не выбрано"));
	$("select").bind('change', function() {
		$(this).find("option[value='Не выбрано']").remove();
	});
}

function showgreen(i) {
	$("#onlineform").show();
	$(".steps").hide();
	$("#step"+i).show();
	$("#v1-8-0").val($("select:eq(0) option:selected").text());
	$("#v1-9-0").val($("select:eq(1) option:selected").text());
	$("#v1-10-0").val($("select:eq(2) option:selected").text());
	$.scrollTo("#onlineform", 1000);
}

function showkasko(i) {
//	$("#onlineform").show();
//	alert (i);
	if (i>0) {
		var c=i-1;
		if (submitform('kasko', false, "#step"+c+" ")) {} else {
			$(".steps").hide();
			$("#step"+i).show();
			if (i==2) {
				$("#driver0, #driver1, #driver2, #driver3, #driver4").hide();
					$("#drivers").show();
					$(".plus").bind('click', function() {
						$(".close").unbind('click');
						if (parseInt($("#driver").html())<4) {
							//$(".close").hide();
							$("#driver").html((parseInt($("#driver").html())+1));
							$("#driver"+parseInt($("#driver").html())).show();
//							$("#driver"+parseInt($("#driver").html())+" .close").show();
							if (parseInt($("#driver").html())==4) {
								$(".plus").hide();
							}
						} else {
							$(".plus").hide();
						}
						$(".close").bind('click', function() {
							$("#driver"+parseInt($("#driver").html())).hide();
							$("#driver").html((parseInt($("#driver").html())-1));
//							$("#driver"+parseInt($("#driver").html())+" .close").show();
							if (parseInt($("#driver").html())<4) {
								$(".plus").show();
							}
						});
					});

				$("#v2-1-0").bind('change', function() {
					if ($('#v2-1-0').is(':checked')) {
						$("#drivers").hide();
					} else {
						$("#drivers").show();
					}
				});
			} else if (i==1)
			{
				$("#subtext-2-1, #subselect-2-0, #subtext-3-5").hide();
				$("input[name=v1-3]:radio").bind('change', function() {
					if ($('#v1-3-1').is(':checked'))
					{
						$("#subselect-2-0").show();
						$("#subselect-2-0 select").bind('change', function() {
							if ($('#subselect-2-0 select').val()==9) {
								$("#subtext-2-1").show();
							} else {
								$("#subtext-2-1").hide();
							}
						});
					} else {
						$("#subselect-2-0 select").unbind('change');
						$("#subselect-2-0").hide();
						$("#subtext-2-1").hide();
					}
				});

				$("input[name=v1-sub3-4]:radio").bind('change', function() {
					if ($('#v1-sub3-4-1').is(':checked'))
					{
						$("#subtext-3-5").show();
					} else {
						$("#subtext-3-5").hide();
					}
				});
			} else {
				$("input[name=v1-sub3-4]:radio").unbind('change');
				$("input[name=v1-3]:radio").unbind('change');
			}
		}
	} else {
		$(".steps").hide();
		$("#step"+i).show();
	}

//	recalcKasko();
//	$.scrollTo("#onlineform", 1000);
}

function showosago(i) {
	$("#onlineform").show();
	if (i==2) {
		if ($('#v0-1-0').is(':checked')) {
			if (submitform('osago', false, "#1_fiz ")) {} else {
				$(".steps").hide();
				$("#step2").show();
			}
		} else {
			if (submitform('osago', false, "#1_yur ")) {} else {
				$(".steps").hide();
				$("#step2").show();
			}
		}

				$("#driver0, #driver1, #driver2, #driver3, #driver4").hide();
					$("#drivers").show();
					$(".plus").bind('click', function() {
						$(".close").unbind('click');
						if (parseInt($("#driver").html())<4) {
							//$(".close").hide();
							$("#driver").html((parseInt($("#driver").html())+1));
							$("#driver"+parseInt($("#driver").html())).show();
//							alert (parseInt($("#driver").html()));
//							$("#driver"+parseInt($("#driver").html())+" .close").show();
							if (parseInt($("#driver").html())==4) {
								$(".plus").hide();
							}
						} else {
							$(".plus").hide();
						}

						$(".close").bind('click', function() {
							$("#driver"+parseInt($("#driver").html())).hide();
							$("#driver").html((parseInt($("#driver").html())-1));
//							$("#driver"+parseInt($("#driver").html())+" .close").show();
//							alert (parseInt($("#driver").html()));
							if (parseInt($("#driver").html())<4) {
								$(".plus").show();
							}
						});
					});

				$("#v3-1-0").bind('change', function() {
					if ($('#v3-1-0').is(':checked')) {
						$("#drivers").hide();
					} else {
						$("#drivers").show();
					}
				});

				$("#v3-2-1").bind('change', function() {
					if ($(this).is(':selected')||$(this).is(':checked')) {
						$("#tr3-3, #tr3-4, #tr3-5, #tr3-6, #hr3-3, #hr3-4, #hr3-5, #hr3-6").show();
					}
				});

				$("#v3-2-0").bind('change', function() {
					if ($(this).is(':selected')||$(this).is(':checked')) {
						$("#tr3-3, #tr3-4, #tr3-5, #tr3-6, #hr3-3, #hr3-4, #hr3-5, #hr3-6").hide();
					}
				});

	} else if (i>1) {
		var c=i-1;
		if (submitform('osago', false, "#step"+c+" ")) {} else {
			$(".steps").hide();
			$("#step"+i).show();
		}
	} else {
		$(".steps").hide();
		$("#step"+i).show();
	}

	recalcOsago();
	$.scrollTo("#onlineform", 1000);
}

function recalcOsago() {
	var price=1980;
	var type=parseInt($("select:eq(3)").val());
	if ($('#v0-1-0').is(':checked')) {
//		osagoyur=$("#1_yur").html();
//		$("#1_yur").html('');
		$("#1_yur").hide();
		$("#1_fiz").show();
//		$("#1_fiz").html(osagofiz);
		price=price1[type];
	} else {
//		osagofiz=$("#1_fiz").html();
//		$("#1_fiz").html('');
		$("#1_fiz").hide();
		$("#1_yur").show();
//		$("#1_yur").html(osagoyur);
		price=price2[type];
	}

	if (type==12||type==11) {
		price=price*k2[city[$("select:eq(2)").val()]];
//		alert (k2[city[$("select:eq(2)").val()]]);
	} else {
		price=price*k1[city[$("select:eq(2)").val()]];
//		alert (k1[city[$("select:eq(2)").val()]]);
	}

//	alert($("select:eq(3)").val());

	price=price*kvs[$("select:eq(7)").val()]*kbm[$("select:eq(6)").val()]*ks[parseInt($("select:eq(4)").val())]*km[parseInt($("select:eq(5)").val())];

//	alert ("ks="+ks[parseInt($("select:eq(3)").val())]+" km="+km[parseInt($("select:eq(4)").val())]);

	price=roundNumber(price,2);

	if (price>0) {
		$("#price").html(price+'&nbsp;P.');
	}
}

function recalcKasko() {

}

function recalcGreen() {
	var price=1420;
	if ($("select:eq(0)").val()==0) {
		price=price1[13*parseInt($("select:eq(1)").val()) + parseInt($("select:eq(2)").val())];
	} else {
		price=price2[13*parseInt($("select:eq(1)").val()) + parseInt($("select:eq(2)").val())];
	}

	if (price>0) {
		$("#price").html(price+'&nbsp;P.');
	}
}


function submitform(type, send, id) {
	var i=0;
	var str="";
	var error=false;

	$(".errinfo").remove();

	if (send) {
		$("#onlineform").addClass("frame");
		$(".steps").show();
		if ($('#v0-1-0').is(':checked')) {
			$("#1_yur").html('');
		} else {
			$("#1_fiz").html('');
		}

		$("#driver"+(parseInt($("#driver").html())+1)).remove();
		$("#driver"+(parseInt($("#driver").html())+2)).remove();
		$("#driver"+(parseInt($("#driver").html())+4)).remove();
		$("#driver"+(parseInt($("#driver").html())+4)).remove();
		$("#driver"+(parseInt($("#driver").html())+5)).remove();
	}

	$.each($.find(id+"input:checkbox"), function() {
		if ($(this).is(':checked')) {
			str=str+'&ch'+i+'=on';
	} else {
			str=str+'&ch'+i+'=no';
		}
		i++;
	});

	i=0
	$.each($.find(id+"input:radio"), function() {
		if ($(this).is(':selected')||$(this).is(':checked')) {
			str=str+'&ra'+i+'='+escape($(this).attr("text"));
		} else {
			str=str+'&ra'+i+'=';
		}
		i++;
	});

	i=0;
	$.each($.find(id+"input:text"), function() {
		str=str+'&tx'+i+'='+escape($(this).val());
		if ($(this).is('.noerr')) {
//			alert($(this).attr("id"));
		} else if ($(this).is('.check17')&&!($(this).val().length==17)) {
			$(this).addClass('error');
			$(this).after('<span class="errinfo" style="color: red; font-size: 80%; margin-top: -5px;">Должно быть 17 символов</span>')
			error=true;
//			alert(" 0txt=>"+i+"=>"+$(this).val());
		} else if ($(this).is('.check14')&&!($(this).val().length==14)) {
			$(this).addClass('error');
			$(this).after('<span class="errinfo" style="color: red; font-size: 80%; margin-top: -5px;">Должно быть 14 символов</span>')
			error=true;
//			alert(" 1txt=>"+i+"=>"+$(this).val());
		} else if ($(this).is('.check11')&&!($(this).val().length==11)) {
			$(this).addClass('error');
			$(this).after('<span class="errinfo" style="color: red; font-size: 80%; margin-top: -5px;">Должно быть 11 символов</span>')
			error=true;
//			alert(" 2txt=>"+i+"=>"+$(this).val());
		} else if ($(this).val().length<3||$(this).val().length<3) {
			$(this).addClass('error');
			error=true;
//			alert(" 3txt=>"+i+"=>"+$(this).val());
		} else {
			$(this).removeClass('error');
		}
		i++;
	});

	i=0;
	$.each($.find(id+"option:selected"), function() {
		str=str+'&sl'+i+'='+escape($(this).val());
//		alert($(this).val());
		if ($(this).is('.noerr')||$(this).parent().is('.noerr')) {
		} else if ($(this).text()==='день'||$(this).text()==='месяц'||$(this).text()==='год'||$(this).text()==='лет'||$(this).text()==='Не выбрано'||$(this).val()==='Не выбрано') {
			$(this).parent().addClass('error');
			error=true;
//			alert(" opt=>"+i+"=>"+$(this).val());
		} else {
			$(this).parent().removeClass('error');
		}
		i++;
	});

	i=0;
	$.each($.find(id+"input:hidden"), function() {
		str=str+'&hi'+i+'='+escape($(this).val());
		i++;
	});

	if (!error&&send) {
//		alert("/ajax/send.php?send="+type+"&price="+escape($("#price").html())+str);
		$(".ajax").show();
		$.ajax({
			url: "/ajax/send.php?send="+type+"&price="+escape($("#price").html())+str,
			context: "#onlineform",
			success: function(html) {
				$(".ajax").hide();
				$("#onlineform").removeClass("frame");
				$("#onlineform").html(html);
				$.scrollTo("#onlineform", 1000);
			}
		});
	} else if(send) {
		$("#onlineform").removeClass("frame");
		if (type=='green') {
			showgreen(1);
		}
		if (type=='kasko') {
			showkasko(2);
		}
		if (type=='osago') {
			showosago(3);
		}
		$.scrollTo("#onlineform", 1000);
	}

return error;
}

//Меню
function jsddm_open(){jsddm_canceltimer(); jsddm_close(); ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}
function jsddm_close(){if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}
function jsddm_timer(){closetimer = window.setTimeout(jsddm_close, timeout);}
function jsddm_canceltimer(){if(closetimer){window.clearTimeout(closetimer);closetimer = null;}}

//Быстрый поиск
  function findValue(li) {
//  	if( li == null ) return alert("No match!");
  	if( !!li.extra ) var sValue = li.extra[0];
 	else var sValue = li.selectValue;
  }

  function selectItem(li) {
    	findValue(li);
  }

  function formatItem(row) {
    	return row[0] ; //+ " (id: " + row[1] + ")"
  }

  function lookupAjax(){
  	var oSuggest = $(".getmarks")[0].autocompleter;
    oSuggest.findValue();
  	return false;
  }

  function lookupLocal(){
    	var oSuggest = $(".getmarks")[0].autocompleter;

    	oSuggest.findValue();

    	return false;
  }
  
function getmarks () {
    $(".getmarks").autocomplete(
      "/ajax/getmarks.php",
      {
  			delay:10,
  			minChars:1,
  			matchSubset:1,
  			matchContains:1,
  			cacheLength:10,
  			onItemSelect:selectItem,
  			onFindValue:findValue,
  			formatItem:formatItem,
  			autoFill:true
  		}
    );
}

  
function getmodels () {
    $(".getmodels").autocomplete(
      "/ajax/getmodels.php",
      {
  			delay:10,
  			minChars:1,
  			matchSubset:1,
  			matchContains:1,
  			cacheLength:10,
  			onItemSelect:selectItem,
  			onFindValue:findValue,
  			formatItem:formatItem,
  			autoFill:true
  		}
    );
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
