$(function(){
	//Email subscribe
	$("#subscibe").click(function(){
		var email = $("#email").val();
		var url = "Ajax-emailSubscibe";
		var regx =/^[a-zA-Z0-9]{1}[\.a-zA-Z0-9_-]*[a-zA-Z0-9]{1}@[a-zA-Z0-9]+[-]{0,1}[a-zA-Z0-9]+[\.]{1}[a-zA-Z]+[\.]{0,1}[a-zA-Z]+$/;
		if(email == ''){
			alert('a valid email address is required');
			$("#email").focus();
		}else if(!regx.test(email)){
			alert('invalid email address mode');
			$("#email").val('');
			$("#email").focus();
		}else{
			$.post(url,{'email':email},function(data){
				if(data=='empty'){
					alert('A valid email address is required!');
					$("#email").focus();
				}else if(data=='pattern'){
					alert('Invalid email address mode!2');
					$("#email").val('');
					$("#email").focus();
				}else if(data=='repeat'){
					alert('Subscribed email address!');
					$("#email").val('');
					$("#email").focus();
				}else	if(data == 'true'){
					alert('Successfully subscribed!');
					$("#email").val('');
				}else if(data == 'false'){
					alert('Fail to subscribe!');
					$("#email").val('');
					$("#email").focus();
				}
			});
		}
	});
	
	
	
	$("#fzipper,#mzipper,#faceA,#hoodD,#openeyes,#openmouth").change(function(){
		var regx = /^\d+$/;
		var unit = $("#unit").val();
		var bust = $("#bust").val();
		var waist = $("#waist").val();
            
		if($("#fzipper").attr("checked")==true){

			var fzipper = 3.94;
 
			}
            
		if($("#mzipper").attr("checked")==true){

			var mzipper = 3.94;
 
			}
                
			if($("#faceA").attr("checked")==true){
 
			var faceA = 3.94;
  
			}
   
			if($("#hoodD").attr("checked")==true){
           var hoodD = 3.94;

			   }
		   if($("#openeyes").attr("checked")==true){
           var openeyes = 2.00;

			   }
			if($("#openmouth").attr("checked")==true){
           var openmouth = 2.00;

			   }
		if(!regx.test(unit)){
			$("#unit").val("");
			$("#unit").focus();
		}
		var id = $("#productid").val();
		var url = "Ajax-onePrice"
		$.post(url,{'id':id,'num':unit,'bust':bust,'waist':waist,'fzipper':fzipper,'mzipper':mzipper,'faceA':faceA,'hoodD':hoodD,'openeyes':openeyes,'openmouth':openmouth},function(data){
			$("#loading").ajaxStart(function(){
				$(this).show();
			});
			$("#loading").ajaxStop(function(){
				$(this).hide();
			});
			$("#cost").ajaxSuccess(function(request, settings){
				$("#price").val(data);
				$(this).text(parseFloat(data*unit).toFixed(2));
				$("#total_price").val(parseFloat(data*unit).toFixed(2));
			});
		});
	});
	
	//判断商品数量格式及算出商品总价
	$("#unit,#waist,#bust,#fzipper,#mzipper,#faceA,#hoodD,#openeyes,#openmouth").keyup(function(){
		var regx = /^\d+$/;
		var unit = $("#unit").val();
		var bust = $("#bust").val();
		var waist = $("#waist").val();
              if($("#fzipper").attr("checked")==true){
                var fzipper = 3.94;
                  }
            if($("#mzipper").attr("checked")==true){
                var mzipper = 3.94;
                  }
                if($("#faceA").attr("checked")==true){
                var faceA = 3.94;
                  }
                if($("#hoodD").attr("checked")==true){
                var hoodD = 3.94;
                  }
		  if($("#openeyes").attr("checked")==true){
           var openeyes = 2.00;

			   }
			if($("#openmouth").attr("checked")==true){
           var openmouth = 2.00;

			   }
		if(!regx.test(unit)){
			$("#unit").val("");
			$("#unit").focus();
		}
		var id = $("#productid").val();
		var url = "Ajax-onePrice"
		$.post(url,{'id':id,'num':unit,'bust':bust,'waist':waist,'fzipper':fzipper,'mzipper':mzipper,'faceA':faceA,'hoodD':hoodD,'openeyes':openeyes,'openmouth':openmouth},function(data){
			$("#loading").ajaxStart(function(){
				$(this).show();
			});
			$("#loading").ajaxStop(function(){
				$(this).hide();
			});
			$("#cost").ajaxSuccess(function(request, settings){
				$("#price").val(data);
				$(this).text(parseFloat(data*unit).toFixed(2));
				$("#total_price").val(parseFloat(data*unit).toFixed(2));
			});
		});
	});
	
	//运费展示
	var country_id = $("#counties").val();
	var url = "Ajax-getExpress";
	$.post(url,{'country_id':country_id},function(data){
		$("#contry_loading").ajaxStart(function(){
			$(this).show();
			$("#dispatch").html('');
		});
		$("#contry_loading").ajaxStop(function(){
			$(this).hide();
		});
		$("#flag").attr('src','http://img1.doitstyle.com/flag/'+data.flag);
		$("#dispatch").html(data.dispatch);
	},'json');

	$("#counties").change(function(){
		var country_id = $("#counties").val();
		var url = "Ajax-getExpress";
		$.post(url,{'country_id':country_id},function(data){
			$("#contry_loading").ajaxStart(function(){
				$(this).show();
			});
			$("#contry_loading").ajaxStop(function(){
				$(this).hide();
			});
			$("#flag").attr('src','http://img1.doitstyle.com/flag/'+data.flag);
			$("#dispatch").html(data.dispatch);
		},'json');
	});
	
	
	//同类商品图片
	var product_id = $("#productid").val();
	var page = $("#page").val();
	var sort_url = "Ajax-sortPics";
	$.post(sort_url,{'id':product_id,'page':page},function(data){
		$("#page").val(data.page);
		$("#sort_pics").html(data.sort_pics);
	},'json');
	
	$("#prevpage").click(function(){
		var page = parseInt($("#page").val())-1;
		var product_id = $("#productid").val();
		$.post(sort_url,{'id':product_id,'page':page},function(data){
			$("#page").val(data.page);
			$("#sort_pics").html(data.sort_pics);
		},'json');
	});
	
	$("#nextpage").click(function(){
		var page = parseInt($("#page").val())+1;
		var product_id = $("#productid").val();
		$.post(sort_url,{'id':product_id,'page':page},function(data){
			$("#page").val(data.page);
			$("#sort_pics").html(data.sort_pics);
		},'json');
	});
	
	//图片无缝滚动
	$('#mycarousel').jcarousel();
	
});
