$(document).ready(function(){
	$("#vdcode").val("");
	var checkvcodevar;
	$("#vdcode").focus(
		function(){
			$("#vdcodebox").show();
			if ($("#vdcodebox").html()==""){
				$("#vdcodebox").html("<img src='../../include/vdimgck.php?"+Math.random()+"' width='50' height='20' title='点击可换一个验证码' />");
			}
		}
	);
	$("#vdcodebox").click(
		function (){
			changevdcode();
		}
	);
	$("#uname").blur(
		function(){
			if ($(this).val()!=""){
				$("#unametip").html("");
			}
		}
	);
	$("#mobiel").blur(
		function(){
			if ($(this).val()!=""){
				$("#mobieltip").html("");
			}
		}
	);
	$("#car").blur(
		function(){
			if ($(this).val()!=""){
				$("#cartip").html("");
			}
		}
	);
	$("#msg").blur(
		function(){
			if ($(this).val()!=""){
				$("#msgtip").html("");
			}
		}
	);
	$("button[type='reset']").click(
		function(){
			$("#uname").focus();
			$("dl input").val("");
			$("dl textarea").val("");
		}
	);
	$("button[type='submit']").click(
		function(){
			checkvcode();
			if (checkvcodevar=="no"){
				$("#vdcodetip").html("验证码错误");
				$("#vdcode").focus();
				return;
			}else if(checkvcodevar=="yes"){
				$("#vdcodetip").html("");
			}
			if ($.trim($("#vdcode").val())==""){
				$("#vdcodetip").html("请填写验证码");
				$("#vdcode").focus();
				return;
			}
			if ($.trim($("#uname").val())==""){
				$("#unametip").html("请填写您的姓名");
				$("#uname").focus();
				return;
			}
			if ($.trim($("#mobiel").val())==""){
				$("#mobieltip").html("请填写您的移动电话");
				$("#mobiel").focus();
				return;
			}
			if ($.trim($("#car").val())==""){
				$("#cartip").html("请选择预约车型");
				$("#car").focus();
				return;
			}
			$.ajax({
				type: "post",//方法为POST
				url: "order_drive_action.php",//处理页面
				data: "vdcode="+$("#vdcode").val()+"&uname="+$("#uname").val()+"&sex="+$("#sex").val()+"&mobiel="+$("#mobiel").val()+"&car="+$("#car").val()+"&year="+$("#year").val()+"&month="+$("#month").val()+"&date="+$("#date").val()+"&msg="+$("#msg").val()+"&action=addorder",//要POST的数据
				timeout: 5000,
				success: function(data){
					if (data=="ok")
					{
						alert("您的试驾预约已经登记，我们的销售代表会在24小时内与您取得联系，谢谢！");
						changevdcode();
						$("dl input").val("");
						$("dl textarea").val("");
					}
				}
			});
		}
	);
	$(".loadingbtn").click(
		function(){
			$("#uname").focus();
			$("dl input").val("");
			$("dl textarea").val("");
			changevdcode();
		}
	);

	//检查验证码
	function checkvcode(){
		$.ajax({
            type: "post",
            url: "order_drive_action.php",
            data: "action=checkvcode&vdcode="+$("#vdcode").val(),
			async: false,
			complete :function(){$(".circleloading").hide();},
            success: function(data){
				if (data=="no"){
					checkvcodevar = "no";
				}else if(data=="yes"){
					checkvcodevar = "yes";
				}
            }
        });
	}
	//切换验证码
	function changevdcode(){
		checkvcodevar="";
		$("#vdcodebox").empty();
		$("#vdcodebox").html("<img src='../../include/vdimgck.php?"+Math.random()+"' width='50' height='20' title='点击可换一个验证码' />");
		$("#vdcode").focus();
	}
})
