
$(function() {

	var adult = $("#O_Adult");
	var children = $("#O_Children");
	var single_price = $("#single_price");
	var btn_submit = $("#btn-submit");
	
	var login = $("#login");
	
	var memberId = $('#memberId').attr('value');
	
	
	login.click(function(){
		
	
						 
		var v_account = $("#member_account").attr('value');
		var v_password = $("#password").attr('value');
		
		if(v_account != '' && v_password != '')
		{
			$.post(root + "?m=login.check",{ account:v_account, password:v_password, isSetCookieDate:1 , code:'no' }, function(data) 			{
																															   
					
					if(data[0] == "success")
					{		
						alert('登录成功');
						$("#memberId").attr('value' , 1);
						var info = data[1].split(',');
						
						memberId = info[0];
						$('#perName').attr('value' , info[1]);
						$('#mobile').attr('value' , info[3]);
						$('#email').attr('value' , info[4]);
						
						$('.login_info').hide();
//						
					}else{
						alert(data[1]);
						return;
					}
					return;
		
			return;
		}, "json");
		}else{
			alert('账号或密码不能为空');	
			return;
		}
	});

	btn_submit.click(function(){
					   
		var adult_count = $('#O_Adult').attr('value');
		var children_count = $('#O_Children').attr('value');		
		var perName = $('#perName').attr('value');
		var mobile = $('#mobile').attr('value');
		var cert = $('#cert').attr('value');
		var email = $('#email').attr('value');
		var visa_id = $('#visa_id').attr('value');
		
		var memberId = $("#memberId").val();
		
		var price = $('#total_price').html();
		
	
		if(adult_count + children_count  == 0)
		{
			alert('请选择预订的人数');	
			$('#O_Adult').focus();
			return;
		}
		
		if(perName == '')
		{
			
			alert('请填写客人姓名');
			$('#perName').focus();
			return;
		}
		
		if(mobile == '')
		{
			alert('请填写联系手机');	
			$('#mobile').focus();
			return;
		}
		
		if(mobile.length < 11)
		{
			alert('请输入正确的手机号码');	
			$('#mobile').focus();
			return;
		}
		if(email.length > 0 && !/.+@.+\.[a-zA-Z]{2,4}$/.test(email)) {
			alert("E-mail格式错误！");
			$('#email').focus();
			return;
		}
		
		/**********************************************/
		if(memberId == 0) {
				
				// 判断登录或注册
				$.post(root + "?m=login.checkAccount", { account:mobile }, function(data) {
					
					var data = eval("(" + data + ")");
					
																					
					if(data[0] == "error") {
						
						var msg = data[1];
						if(data[2] == 'account is exists') msg += "请先登录！";
						alert(msg);
						$btnSubmit.attr("disabled", false);
					} else {
						
						$.post(root + "?m=login.register",{ account:mobile, password:mobile, name:perName, mobile:mobile, email:email }, function(data) {
							
							var ary = eval("(" + data + ")");
							
							if(ary[0] == "success") {

								$.post(root + "?m=order.createVisaOrder", { adult:adult_count , children:children_count , certType:cert, name:perName, phone:mobile, email:email, visaId:visa_id, memberId:ary[3] , price:price }, function(data) {
					
									var ary = eval("(" + data + ")");
									if(ary[0] == "success") {
										alert("你的订单已下定成功,请等待处理。");
								//		$btnSubmit.attr("disabled", true);
										window.location.href = "visainfo-" + ary[2] + ".html";
									} else {
										alert(data[1]);
										$btnSubmit.attr("disabled", false);
									}
								
								});
								
								
							} else {
								alert(mobile + "在您上一次下订单时已注册了,请先登录。");
								$('#member_account').focus();
							
							}
							
						});
							
						
					}
				});
			} else {
				
				$.post(root + "?m=order.createVisaOrder", { adult:adult_count , children:children_count , certType:cert, name:perName, phone:mobile, email:email, visaId:visa_id, memberId:memberId, price:price  }, function(data) {
					
					var ary = eval("(" + data + ")");
					if(ary[0] == "success") {
						alert("你的订单已下定成功,请等待处理。");
						window.location.href = "visainfo-" + ary[2] + ".html";
					} else {
						alert(ary[1]);
					//	$btnSubmit.attr("disabled", false);
					}
					
				});
			}
		/**********************************************/

	
		
   });

	//大人订购的选项 用于计算价格
	adult.change(function() {
		
		var adult_count = $(this).attr('value');
		var children_count = $('#O_Children').attr('value');



		var price = (parseInt(adult_count) + parseInt(children_count)) * single_price.html();
		
		$('#total_price').html(price);
		$('#totalPrice').html(price);
	

		$('.adult_count').each(function(){
			$(this).html(adult_count);										
		});
		
		
		
		
	}); 
	
		//小孩订购的选项 用于计算价格
	children.change(function() {
		
		var children_count = $(this).attr('value');
		var adult_count = $('#O_Adult').attr('value');

		var price = (parseInt(adult_count) + parseInt(children_count)) * single_price.html();
		
		$('#total_price').html(price);
		$('#totalPrice').html(price);

		
		$('.children_count').each(function(){
			$(this).html(children_count);										
		});
		
	});
	
	
}); 


