//Attention! this method invoked from multi pages
function addNewAddress() {
	var id_address = $('#id_address').val();
	var firstname = $('#firstname').val();
	var id_state = $('#id_state').val();
	var id_county = $('#id_county').val();
	var id_district = $('#id_district').val();
	var address1 = $('#address1').val();
	var postcode = $('#postcode').val();
	var phone_mobile = $('#phone_mobile').val();
	var area_code = $('#area_code').val();
	var phone = $('#phone').val();
	var email = $('#email').val();
	
	if(firstname == "" && firstname == null){
		alert('请填写收货人姓名');
		return false;
	}else if(address1 == "" && address1 == null){
		alert('请填写收货地址');
		return false;
	}else if(postcode == "" && postcode.length==0){
		alert('请填写邮政编码');
		return false;	
	}else if(postcode.length>0){
		reg=/[1-9]\d{5}$/;   
	    if(!reg.test(postcode)){   
	        alert("请填写正确的邮政编码"); 
	        return false;
	    }
	}
	if(phone_mobile == "" && area_code == "" && phone == ""){
		alert('手机号码和固定电话必须要填写一个');
		return false;
	}else if(phone_mobile.length>0){
		reg=/^(\+\d{2,3}\-)?\d{11}$/;   
	    if(!reg.test(phone_mobile)){   
	        alert("请填写正确的手机号码"); 
	        return false;
	    }
	}else if(area_code.length>0){
		reg=/^[0-9]\d{2,3}$/;
	    if(!reg.test(area_code)){   
	        alert("请填写正确的区号");  
	        return false;
	    }else if(phone == ""){
	    	alert("请填写电话号码");  
	        return false;
	    } 
	}else if(phone.length>0){
		reg=/^[1-9]\d{6,7}$/;   
	    if(!reg.test(phone)){   
	        alert("请填写正确的电话号码");  
	        return false;
	    }else if(area_code == ""){
	    	alert("请填写区号");  
	        return false;
	    } 
	}
	if(email.length>0){
		reg=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;   
	    if(!reg.test(email)){   
	        alert("请填写正确的电子邮件"); 
	        return false;
	    }
	}

	$.ajax({
		url: 'ajax_order.php',
		dataType: 'json',
		cache: false,
		data: encodeURI('ajaxNewAddress=1&id_address='+ id_address + '&firstname=' + firstname 
			  + '&id_state=' + id_state + '&id_county=' + id_county + '&id_district=' + id_district
			  + '&address1=' + address1 + '&postcode=' + postcode + '&phone_mobile=' + phone_mobile 
			  + '&area_code=' + area_code + '&phone=' + phone + '&email=' + email),
		success: function(j) {
			var options = '';
			if (j) {
				if (j.hasError) {
					alert(j.errors.join('\n'));
					return false;
				}	
				options ='<input name="id_sel_address" id="id_sel_address" type="radio" onclick="onAddressChange(this.value)" value="'+ j.id_address +'" checked="checked" /><a class="y">' + j.firstname + '</a><a class="y"></a><a class="y">' + j.state_name + j.county_name + j.district_name + j.address1 + '</a><a class="y">' + j.phone_mobile + '</a><a class="y">' + j.postcode + '</a>';
				
				optiond ='<a class="y">' + j.firstname + '</a><a class="y"></a><a class="y">' + j.state_name + j.county_name + j.district_name + j.address1 + '</a><a class="y">' + j.phone_mobile + '</a><a class="y">' + j.postcode + '</a>';
				
				if (id_address > 0) {
					//更新
					$('#Address_'+id_address).html(options);
					$("#newaddress").hide();
					$("#closeaddr").hide();
					$("#updateaddr").show();
					$("#Cartd_3_1").hide();
					$("#Cartd_3_d").show();
					$("#Address_d").html(optiond);
					$("#Address_x").append(optiond);
				} else {
					//追加
					$("#Cartd_3_1").html($("#Cartd_3_1").html() + '<div class="Cartd_3_1_2" id="Address_'+ j.id_address +'">' + options + '</div>');	
					$("#newaddress").hide();
					$("#closeaddr").hide();
					$("#updateaddr").show();		
					$("#Cartd_3_1").hide();
					$("#Cartd_3_d").show();
					$("#Address_d").html(optiond);
					$("#Address_x").append(optiond);
				}
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert('NewAddress ajax error: '+textStatus);
		}

	});
}


//更改了当前送货地址
function onAddressChange(id_address) {
	$.ajax({
	  url: "ajax_order.php",
	  dataType: 'json',
	  cache: false,
	  data: "ajaxGetAddress=1&no_empty=false&id_address="+id_address,
	  success: function(j){
	  	if (j) {
				$('#id_address').val(j.id_address);	  	
				$('#firstname').val(j.firstname);
				$('#id_state').val(j.id_state);
				$('#address1').val(j.address1);
				$('#postcode').val(j.postcode);
				$('#phone_mobile').val(j.phone_mobile);
				$('#area_code').val(j.area_code);
				$('#phone').val(j.phone);
				$('#email').val(j.email);
				
				populateCounties(j.id_state, j.id_county, j.id_district);
			}
	  }
	});
}

function populateCounties(id_state, id_address)
{
	var id_county = $('#id_county_' + id_address).val();
	var id_district = $('#id_district_' + id_address).val();
	$.ajax({
	  url: "ajax_order.php",
	  cache: false,
	  data: "ajaxCounties=1&no_empty=false&id_state="+id_state+"&id_county="+id_county,
	  success: function(html){
		$("#id_county").html(html);
		
		populateDistricts($("#id_county").val(), id_district);
	  }
	});
}		

function populateDistricts(id_county, id_address)
{
	var id_district = $('#id_district_' + id_address).val();
	$.ajax({
	  url: "ajax_order.php",
	  cache: false,
	  data: "ajaxDistricts=1&no_empty=false&id_county="+id_county+"&id_district="+id_district,
	  success: function(html){
		$("#id_district").html(html);
	  }
	});
}

function addCustomerFavourites()
{
	var id_product = $('#product_page_product_id').val();

	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxCustomerFavourites=1&id_product="+id_product,
	    success: function(j){
			alert(j.notice);
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}

function updateReadit(id_customer_inbox) {
	$("#div_content"+id_customer_inbox).toggle();

	
	if ($('#readit_' + id_customer_inbox).val() == 1) return;
	
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxUpdateReadit=1&id_customer_inbox="+id_customer_inbox,
	    success: function(html){
	    	if (html) {
				$('#readit_' + id_customer_inbox).val(1);

				alert('更改成功');
			} else {
				alert('更改失败');
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert('更改失败');
		}
	});		
}
	
function Readit1(id_customer_claim) {
	$("#div_content"+id_customer_claim).toggle();
	
	//if ($('#readit_' + id_customer_claim).val() == 1) return;
	
	$.ajax({
		url: "ajax_order.php",
		data: encodeURI("ajaxReadit1=1&id_customer_claim="+id_customer_claim),
		success: function(j){
			if (j) {
				$('#readit_' + id_customer_claim).val(j.id_customer_claim);
			}
		}
	});		
}
//更改了当前送货地址，addresses.tpl里面调用
function onMyAddressChange(id_address) {
	$('#id_address').val(id_address);
	
	onAddressChange(id_address);
}

function populateCounties1(id_state)
{
	$.ajax({
	  url: "ajax_order.php",
	  cache: false,
	  data: "ajaxCounties=1&no_empty=false&id_state="+id_state+"&id_county=0",
	  success: function(html){
		$("#id_county1").html(html);
		
		populateDistricts1($("#id_county1").val());
	  }
	});
}		

function populateDistricts1(id_county)
{
	$.ajax({
	  url: "ajax_order.php",
	  cache: false,
	  data: "ajaxDistricts=1&no_empty=false&id_county="+id_county+"&id_district=0",
	  success: function(html){
		$("#id_district1").html(html);
	  }
	});
}

/*
 * 送货方式
 */
function updateCarrierMethod()
{
	var id_carrier_method = $("input[name='idCarrierMethod']:checked").val();
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxCarrierMethod=1&id_carrier_method=" + id_carrier_method,
	    success: function(j){
			if (j) {
				if(j.id_carrier_method ==1){
					$('#carrierMethod').html("快递  5元");
				}else if(j.id_carrier_method ==2){
					$('#carrierMethod').html("EMS 10元");
				}else if(j.id_carrier_method ==3){
					$('#carrierMethod').html("货到付款  5元");
				}else{
				  $("#carrierMethod").html('请选择送货方式');
				  return;
				}
				
				$("#btnCloseCarrierMethod").hide();
				$("#btnShowCarrierMethod").show();
				$("#carrierMethodDiv").hide();
				$("#carrierMethodLabelDiv").show();					
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	///alert('NewAddress ajax error: '+textStatus);
		}
	});
}

/*
 * 支付方式
 */
function updatePaymentMethod(toggle)
{
	var id_payment_method = $("input[name='idPaymentMethod']:checked").val();
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxPaymentMethod=1&id_payment_method=" + id_payment_method,
	    success: function(j){
			if (j) {
				if(j.id_payment_method ==1){
					$('#paymentMethod').text("网上支付");
				}else if(j.id_payment_method ==2){
					$('#paymentMethod').text("邮局汇款");
				}else if(j.id_payment_method ==3){
					$('#paymentMethod').text("银行转账");
				}else if(j.id_payment_method ==4){
					$('#paymentMethod').text("货到付款");
				}else{
				    $("#paymentMethod").text('请选择支付方式');
				    return;
				}
				if (toggle) {
					$("#btnClosePaymentMethod").hide();
					$("#btnShowPaymentMethod").show();
					$("#paymentMethodDiv").hide();
					$("#paymentMethodLabelDiv").show();	
				}
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}

/*
 * 发票信息
 */
function updateInvoiceMethod()
{
	var id_invoice_catalog = $("input[name='idInvoiceCatalog']:checked").val();
	var invoice_title = '';
	var invoice_content = 0;
	if (id_invoice_catalog > 0) {
		invoice_title = $("#invoice_title").val();
		invoice_content = $("#invoice_content").val();
	}

	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: encodeURI("ajaxInvoiceMethod=1&id_invoice_catalog=" + id_invoice_catalog +'&invoice_title=' + invoice_title + 
	          '&invoice_content=' + invoice_content),
	    success: function(j){
			if (j) {
				//显示发票
				if(j.id_invoice_catalog ==0){
					$('#invoiceLabel').text("不要发票");
				}else if(j.id_invoice_catalog ==1){
					$('#invoiceLabel').text("个人发票");
				}else if(j.id_invoice_catalog ==2){
					$('#invoiceLabel').text("公司发票");
				}else{
				    $("#invoiceLabel").text('请选择发票信息');
				    return;
				}
				
				$("#invoiceDiv").hide();
				$("#btnCloseInvoice").hide();
				$("#btnShowInvoice").show();
				$("#invoiceLabelDiv").show();

			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}

function updateMessageMethod()
{
	var display_message = $("#xsdingdan").text();
	var gift_message = $("#gift_message").val();
	
	if(gift_message == "" && gift_message == null){
		gift_message = display_message;
	}
	gift_message = gift_message.replace(/\n/g, '<br/>');
	
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: encodeURI("ajaxMessageMethod=1&gift_message=" + gift_message),
	    success: function(j){
			if (j) {
				//var gift_message = $("#gift_message").val('');
				//显示订单备注
				$("#orderdd").hide();
				$("#updateorderdd").show();
				$("#closeorderdd").hide();
				$("#dingdan").show();
				if(j.gift_message=="" || j.gift_message==null){
					$("#xsdingdan").html();
					$('#message').html();
				}else{
					$("#xsdingdan").html(j.gift_message).show();
					$('#message').html(j.gift_message);
				}
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}
function onOptometrySlipChange(id_optometry_slip, id_product) {
	$.ajax({
	  url: "ajax_order.php",
	  dataType: 'json',
	  cache: false,
	  data: "ajaxGetOptometry=1&id_optometry_slip="+id_optometry_slip,
	  success: function(j){
	  	if (j) {
	  		var prefix = '#attr_' + id_product + '_';
			$('#id_optometry_slip').val(j.id_optometry_slip);	  	
			$('#title').val(j.title);

			$('select[id4js="l_sphere"]').val(j.l_sphere);		
			$('select[id4js="l_cylinder"]').val(j.l_cylinder);
			$('select[id4js="r_sphere"]').val(j.r_sphere);
			$('select[id4js="r_cylinder"]').val(j.r_cylinder);
			
			$('#attr_'+ id_product +'_axis').val(j.l_axis);			
			$('#attr_'+ id_product +'_corrected_version').val(j.l_corrected_version);
			$('#attr_'+ id_product +'_pupil_distance').val(j.pupil_distance);
			$('#attr_2_'+ id_product +'_axis').val(j.r_axis);
			$('#attr_2_'+ id_product +'_corrected_version').val(j.r_corrected_version);

			$('#remark').val(j.remark);
			
			var check_date = j.check_date;
			$('#years').val(check_date.substring(0,4));
			var m = check_date.substring(4,6);
			if (m.indexOf('0') == 0)
				m = m.substring(1);
			$('#months').val(m);
		}
	  }
	});
}
function Login(ctx,callback)
{
  var passwd=$('#txtPwd').val();
  var email=$('#txtEmail').val();
	$.ajax({
	    url: "authentication.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajax=1&SubmitLogin=1&passwd=" + passwd + "&email=" + email,
	   	success: function(html){	
				//alert('登录成功');
				//$('#payment').submit();
				callback.call(ctx, html.token);
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert('Email或密码输入有误');
		}
	});
}
function Register(ctx,callback)
{
		/*if($('#password').val()!=$('#repasswd').val()){
  	alert('密码不一致');
  	return false;
  	}*/
	var repasswd=$('#repasswd').val();
  var password=$('#password').val();
  var email_create=$('#email_create').val();
    var is_new_customer=1;
	$.ajax({
	    url: "authentication.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajax=1&is_new_customer=1&SubmitCreate=1&password=" +password + "&repasswd=" +repasswd+"&email_create=" +email_create,
	    success: function(html){
	    	if (html.hasError) {
	    		alert(html.errors);
	    		return;
	    	} else {
	    	 callback.call(ctx, html.token);
	   		}
	    },
  error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert('Email已存在');
		}
	});
}

function CustomerClaim(id_customer_claim_category)
{
	 $.ajax({
		  url: "ajax_order.php",
		  cache: false,
		  data: "ajaxClaim=1&no_empty=false&id_customer_claim_category="+id_customer_claim_category,
		  success: function(html){
				$("#id_sub_customer_claim_category").html(html);
		  }
	});
}

function Remarks(id_order) {
	$("#div_remarks"+id_order).toggle();
	
	//if ($('#readit_' + id_customer_claim).val() == 1) return;
	
	$.ajax({
		url: "ajax_order.php",
		data: "ajaxRemarks=1&id_order="+id_order,
		success: function(j){
			if (j) {
				$('#readit_' + id_order).val(j.gift_message);
			}
		}
	});		
}
//详细页(选择地区)
function SelectState(id_state) {

	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxSelectState=1&id_state=" + id_state,
	    success: function(j){
			if (j) {
				$("#diqu").text(j.name);
				$("#kuaidi").text("快递："+j.express_fee);
				$("#ems").text("EMS："+j.ems_fee);
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}

function onRefundChange(id_order) {
	$.ajax({
	  url: "ajax_order.php",
	  dataType: 'json',
	  cache: false,
	  data: "ajaxGetRefund=1&no_empty=false&id_order="+id_order,
	  success: function(j){
	  	if (j) {
	  			$('#id_order_return').val(j.id_order_return);
	  			if(j.received == 1){
	  				$('#received1').attr('checked',true);
	  				$("#show").show();
	  			}else if(j.received == 2){
	  				$('#received2').attr('checked',true);
	  				$("#show").hide();
	  			}	
				$('#order_no').val(j.order_no);
				$('#customer_name').val(j.customer_name);
				$('#logistics_no').val(j.logistics_no);
				if(j.refund_method == 1){
					$('#refund_method1').attr('checked',true);
	  			}else if(j.refund_method == 2){
	  				$('#refund_method2').attr('checked',true);
	  			}else if(j.refund_method == 3){
	  				$('#refund_method3').attr('checked',true);
	  			}	
				$('#question').val(j.question);
			}
	  }
	});
}
function CustomerConsulting()
{
	var content=$('#content').val();
	var customer_name=$('#customer_name').val();
	if(customer_name == ""){
		alert('请填写姓名');
		return false;
	}else if(content == ""){
		alert('请填写咨询内容');
		return false;
	}
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxsubmitCustomerConsulting=1&customer_name=" +customer_name + "&content=" +content,
	    success: function(j){
			alert('咨询提交成功，客服将在24小时内给你回复，请注意查看');
			$("#addCustomerConsultingButton").show();
			$(".details_Products_r_8_4_3").hide();
			$('#customer_name').val("");
			$('#content').val("");
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('咨询提交失败');
	    	$("#addCustomerConsultingButton").show();
			$(".details_Products_r_8_4_3").hide();
			$('#customer_name').val("");
			$('#content').val("");
		}
	});
}
function updateOrderValid(id_order)
{
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxOrderValid=1&id_order=" +id_order,
	    success: function(j){
			if(j){
				$(".ordermessage" + id_order).css("display","none");
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}
//评论咨询(评论--顶)
function CommentGood(id_product_comment)
{
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxCommentGood=1&id_product_comment=" +id_product_comment,
	    success: function(j){
			if(j){
				$("#good" + id_product_comment).text(j.good + '人');
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}
//评论咨询(评论--踩)
function CommentBad(id_product_comment)
{
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxCommentBad=1&id_product_comment=" +id_product_comment,
	    success: function(j){
			if(j){
				$("#bad" + id_product_comment).text(j.bad + '人');
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}
//评论咨询(咨询--顶)
function ConsultGood(id_customer_consult)
{
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxConsultGood=1&id_customer_consult=" +id_customer_consult,
	    success: function(j){
			if(j){
				$("#good" + id_customer_consult).text(j.good + '人');
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}
//评论咨询(咨询--踩)
function ConsultBad(id_customer_consult)
{
	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxConsultBad=1&id_customer_consult=" +id_customer_consult,
	    success: function(j){
			if(j){
				$("#bad" + id_customer_consult).text(j.bad + '人');
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}

//详细页(评论分页)
/*function DetailPage(id_product,p,n) {

	$.ajax({
	    url: "ajax_order.php",
	    dataType: 'json',
	    cache: false,
	    data: "ajaxDetailPage=1&id_product=" + id_product + "&p=" + p + "&n=" + n,
	    success: function(j){
			if (j) {
				var tr = '';
				for(var i = 0; i<j.length; i++){
					tr +='<tr><td width="3%"></td><td width="24%" height="30"><a class="ls" href="#">'+j[i].customer_name+(j[i].name)+'</a></td><td  width="73%" height="30" colspan="2"><span>发表于'+j[i].date_add+'</span></td></tr><tr><td class="td2"></td><td class="td2" colspan="3"><p class="opo">'+j[i].content+'</p></td></tr>';
				}
				$("#DetailCommentPage tr").remove();
				$("#DetailCommentPage").append(tr);
			}
	    },
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
	    	alert('NewAddress ajax error: '+textStatus);
		}
	});
}*/
