$(document).ready(function(){
	get_cart_num();
	})
	
function sub_serach()
{
	var category=$("#category_1").val();
	var k=$.trim($("#k").val());
	if(k=="")
	{
		alert("Please input Keyword");
		$("#k").focus();
		return false;
	}
	
}

function showhide(a,b)
{
	$("#"+a).slideToggle("600");
	($("#"+b).attr("src")=="images/+.gif") ? $("#"+b).attr("src","images/-.gif") : $("#"+b).attr("src","images/+.gif")
}

function mail_list()
{
		var mail=$.trim($("#mail").val());
		if(mail=='')
		{
			alert('Please input your email');	
			return false;
		}
		var reg =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(!reg.test(mail))
			{
			alert('Please input correct mail');
			$("#mail").val("");
			return false;
			} 
		window.location.href="add_mail.php?email="+mail;
}

function chk_email(e)
{
var reg =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if(!reg.test(e))
	{
	alert('Please input correct mail');
	return false;
	} 
}

//登录检测
function sub_sign()
{
	var email=$.trim($("#email").val());
	var password=$.trim($("#password").val());
	var now_url=$("#now_url").val();
	var state;
	($("#state").attr("checked")==true)?state="1":state="";
	
	if(email=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please input your email address.");
		$("#email").focus();
		return false;
	}
	var reg =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if(!reg.test(email))
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'>Please input correct email. ");
		$("#email").focus();
	return false;
	} 	
	if(password=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please input your password");
		$("#password").focus();
		return false;
	}
	var poster="email="+email+"&password="+password+"&state="+state;
	$.ajax({
		type:"POST",
		url:"inc/chk_sign.php",
		data:poster,
		beforeSend:function()
		{
			$("#reg_note").show();
			$("#reg_note").html("<img src='images/indicator_medium.gif'> Loading, please wait...");
			},
		success:function(data){
			//alert(data)
			if(data=='yes')
				{
					$("#reg_note").show();
					$("#reg_note").html("<img src='images/true.gif'> ...");
				window.location.href=now_url;
				}else{
					$("#reg_note").show();
					$("#reg_note").html("<img src='images/warning_small.gif'> E-mail or password is error, please re-enter.");
				}
			}	
		});
}

function sign_up()
{
	var email=$.trim($("#email").val());
	var password=$.trim($("#password").val());
	var re_password=$.trim($("#re_password").val());
	var full_name=$.trim($("#full_name").val());
  var country=$.trim($("#country").val());
  var verify_code=$.trim($("#verify_code").val());
	
	if(email=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please input your email address.");
		$("#email").focus();
		return false;
	}
	var reg =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if(!reg.test(email))
	{
	$("#reg_note").show();
	$("#reg_note").html("<img src='images/warning_small.gif'> Please input correct email.");
	$("#email").focus();
	return false;
	} 	
	if(password=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please input your password");
		$("#password").focus();
		return false;
	}
	if($("#password").val().length<6)
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> the length of password is not enough, at least 6.");
		$("#password").focus();
		return false;
	}
	if(re_password=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please enter your password again.");
		$("#re_password").focus();
		return false;
	}
	if(password!=re_password)
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> It is not in accordance with new password to confirm the password.");
		$("#password").focus();
		return false;
	}
	if(full_name=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please enter your full name");
		$("#full_name").focus();
		return false;
	}
if(country=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please select your country.");
		$("#country").focus();
		return false;
	}
	if(verify_code=='')
	{
		$("#reg_note").show();
		$("#reg_note").html("<img src='images/warning_small.gif'> Please enter the Verify code");
		$("#verify_code").focus();
		return false;
	}
	var poster="email="+email+"&password="+password+"&full_name="+full_name+"&country="+country+"&verify_code="+verify_code;
	$.ajax({
		type:"POST",
		url:"inc/chk_sign_up.php",
		data:poster,
		beforeSend:function()
		{
			$("#signup_btn").attr("disabled","true");
			$("#reg_note").html("<img src='images/indicator_medium.gif'> Loading, please wait...");
		},
		success:function(data){
			//alert(data);
			switch (data)
				{
					case "1":
						$("#reg_note").show();
						$("#reg_note").html("<img src='images/warning_small.gif'> The Verify code is error");
						$("#signup_btn").attr("disabled","");
						break;
					case "2":
						$("#reg_note").show();
						$("#reg_note").html("<img src='images/warning_small.gif'> The e-mail is exists.");
						$("#signup_btn").attr("disabled","");
						break;
					case "3":
						$("#reg_note").hide();
						$("#sign_ok").show();
						$("#reg").hide();	
						break;	
				}			
			}	
		});
	
	}

//选择尺码的
function select_size(s_id,s_count,v) //
{
	for (var i=0;i<=s_count;i++)
	{
			document.getElementById('s_'+i).className='border_o';
	}
	document.getElementById(s_id).className='border_f';
	$("#p_size").val(v);
	$("#size_t").html(v);
	document.getElementById('s_bor').className="";
}

//添加到购物车
function add_to_cart(pid)
{
	var p_size=$.trim($("#p_size").val());
	var quantity=$("#quantity").val();
	//alert(p_size);
	if(p_size=='')
	{
		document.getElementById('s_bor').className="s_bor";
		return false;
	}
	var poster="pid="+pid+"&size="+p_size+"&quantity="+quantity;
	$.ajax({
		url:"/order/to_cart.php",
		type:"get",
		data:poster,
		beforeSend:function(){
			$("#cart_status").html("Loading...");
			},
		success:function(data)
		{
			//alert(data);
			$("#cart_status").html("");
			//$("#cart_status").hide();
			(data=='true')?show_nt1("<img src='/images/true.gif' style='vertical-align:bottom;'>&nbsp; It has been added to the cart."):show_nt1("<img src='/images/warning_small.gif' style='vertical-align:bottom;'>&nbsp;The goods has existed.");
			get_cart_num();
		}	
	});
		
}

function show_nt(tt)		//显示提示
{
	$("#nt").fadeIn("100");
	$("#nt").html(tt);
	document.getElementById('nt').style.left=document.body.scrollWidth/2-150+"px";
	document.getElementById('nt').style.top=document.body.scrollTop+300+"px";
	setTimeout("hide_nt()",2000);	
}

function hide_nt()		//关闭提示
{
	$("#nt").fadeOut("500");	
}

function show_nt1(tt)		//显示提示
{
	$("#nt1").show();
	$("#overall").show();
	$("#nt2_t").html(tt);
	document.body.style.overflow="hidden";
	document.getElementById('nt1').style.left=document.body.scrollWidth/2-150+"px";
	document.getElementById('nt1').style.top=document.body.scrollTop+300+"px";
	//setTimeout("hide_nt1()",2000);	
}

function hide_nt1()		//关闭提示
{
	$("#nt1").hide();	
	$("#overall").hide();
	document.body.style.overflow="auto";
}


//用法:javascript:return isNumber(event); 
isNumber = function (e) {   
    if ($.browser.msie) {   
        if ( ((event.keyCode > 47) && (event.keyCode < 58)) ||   
              (event.keyCode == 8) ) {   
            return true;   
        } else {   
            return false;   
        }   
    } else {   
        if ( ((e.which > 47) && (e.which < 58)) ||   
              (e.which == 8) ) {   
            return true;   
        } else {   
            return false;   
        }   
    }   
} 

//图片自动缩放
function Img_Load(ImgD,ImgW,ImgH)
{
 if (ImgD.height>ImgH)
 {
  ImgD.width=ImgH/ImgD.height*ImgD.width;ImgD.height=ImgH;
 }
 if (ImgD.width>ImgW)
 {
  ImgD.height=ImgW/ImgD.width*ImgD.height;ImgD.width=ImgW;
 }
}


function get_cart_num()
{
	$.get("/order/get_cart.php",function(data){
		$("#cart_num").html(data);
		});	
}

function del_cart(v,cell)
{
$.get("del_cart.php?act=del&rid="+v,function(data){
if(data=='true')
	{
	window.location.href="shopping_cart.php";
	}
})
}

function RefreshImage()
  {
      $("#code_1").attr("src",$("#code_1").attr("src")+"?");
  }
  
function chk_preview()
{
		if($.trim($("#rec_name").val())=='')	
		{
				$("#rec_name").css({"border":"2px solid red"});
				
				return false;
		}
		if($.trim($("#phone").val())=='')	
		{
				$("#phone").css({"border":"2px solid red"});
				
				return false;
		}
		if($.trim($("#msn").val())=='')	
		{
				$("#msn").css({"border":"2px solid red"});
				
				return false;
		}
		e=$.trim($("#msn").val());
		var reg =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(!reg.test(e))
		{
		$("#msn").css({"border":"2px solid red"});
		return false;
		} 	
		
		if($.trim($("#street").val())=='')	
		{
				$("#street").css({"border":"2px solid red"});
				
				return false;
		}
		if($.trim($("#city").val())=='')	
		{
				$("#city").css({"border":"2px solid red"});
				 
				return false;
		}
		if($.trim($("#state").val())=='')	
		{
				$("#state").css({"border":"2px solid red"});
				 
				return false;
		}
		if($.trim($("#zip").val())=='')	
		{
				$("#zip").css({"border":"2px solid red"});
				 
				return false;
		}
		if($.trim($("#payment").val())=='Paypal')	
		{
				if($.trim($("#paypal").val())==''){
						$("#paypal").css({"border":"2px solid red"});
						return false;
					}					 
		}
	
}  

function re_store(id)
{
	$("#"+id).css({"border":"1px solid #acb5ec"});
}

//会员send msg
function send_msg()
{
var msg_title=$.trim($("#msg_title").val());
var msg_content=$.trim($("#msg_content").val());
var user_id=$.trim($("#user_id").val());
var admin_id=$.trim($("#admin_id").val());
if(msg_title=="")
{
	alert('The title is required!')
	$("#msg_title").focus();
	return false;
}
if(msg_content=="")
{
	alert('The content is required!')
	$("#msg_content").focus();
	return false;
}
if(user_id=="")
{
	alert('Please select the receiver!')
	$("#user_id").focus();
	return false;
}
if(admin_id=="")
{
	alert('Operation is refused!')
	$("#admin_id").focus();
	return false;
}
document.getElementById('send_btn').disabled="true";
window.location.href="member/save_member.php?cl=c2VuZF9tc2c=&msg_title="+msg_title+"&msg_content="+msg_content+"&user_id="+user_id+"&admin_id="+admin_id;
}

//供会员下单时更换地址列表
function change_address(s_id)
{	
	if(s_id!=="")
	{
		$.get("inc/get_address_list.php?s_id="+s_id,function(data){
			var w_obj=data.split("|");
			$("#receiver").val(w_obj[0]);
			$("#phone").val(w_obj[1]);
			//$("#fax").val(w_obj[2]);
			$("#street").val(w_obj[2]);
			$("#city").val(w_obj[3]);
			$("#state").val(w_obj[4]);
			$("#zip").val(w_obj[6]);
			jsSelectItemByValue(document.getElementById('country'),w_obj[5]); //设置国家
		
		
		
		});
		//取得数据并分解,赋值给页面元素
	}else{
		var default_country_id=$("#default_country_id").val();
		var default_id=default_country_id.split("|");
		var def_country_id=default_id[0];
		$("#receiver").val("");
		$("#phone").val("");
		$("#fax").val("");
		$("#street").val("");
		$("#city").val("");
		$("#state").val("");
		$("#zip").val("");
		jsSelectItemByValue(document.getElementById('country'),def_country_id); //设置国家
	
	}
}

function jsSelectItemByValue(objSelect,objItemValue)   
{      
     //判断是否存在   
     //var isExit = false;   
     for(var i=0;i<objSelect.options.length;i++)   
     {   
     			var ab=objSelect.options[i].value;
     			var bc=ab.split("|");     		
         if(bc[0] == objItemValue)   
         {   
             objSelect.options[i].selected = true;   
             //isExit = true;   
             break;   
         }   
     }        
}  


//Feedback
function post_feedback()
{
		var full_name=$.trim($("#full_name").val());
		var email=$.trim($("#email").val());
		var content=$.trim($("#content").val());
		if(full_name=='')
		{
			show_nt("Please input your full name");
			$("#full_name").focus();
			return false;
		}
		if(email=='')
		{
			show_nt("Please input your email");
			$("#email").focus();
			return false;
		}
		
		var reg =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(!reg.test(email))
		{
		show_nt("Please input correct email");
		$("#email").focus();
		return false;
		} 	
		if(content=='')
		{
			show_nt("Please input Content");
			$("#content").focus();
			return false;
		}
		
}

function sub_reply()
{
	var fid=$("#f_id").val();
	var reply=$("#reply_content").val();
	if(reply=='')
	{
		show_nt("Please input Content");
		$("#reply_content").focus();
		return false;
	}
	poster="fs=rep&fid="+fid+"&reply="+reply;
	$.ajax({
		type:"POST",
		url:"inc/post_reply.php",
		data:poster,
		beforeSend:function(){
			show_nt("Loading, please wait...");
			},
		success:function(data){
			if(data=='operation is refused')
					{
					show_nt("Please login first!");
					}else{
					window.location.href=window.location.href;
					}
			}		
		})
}


function delete_reply(id) //删除feedback
{
	if(confirm('Are you sure?')==true)
	{
		window.location.href="inc/post_reply.php?fs=del&fid="+id;
	}
}