jQuery(document).ready(function(){
	var select_all_for_usual = true;
	var select_all_for_other = true;
	jQuery("#saleType").bind("change",function(){
		switch(this.value){
			case "0":
				jQuery("#priceLabel").html("最低价");
				jQuery("#bargainTip").show();
				break;
			case "1":
				jQuery("#priceLabel").html("价格");
				jQuery("#bargainTip").hide();
				break;
		}
		jQuery("#price_label_div").height(jQuery("#price_field_div").height()-6);
	}); 
	jQuery("a.remove_group_link").bind("click",function(){
		if(jQuery(this).attr("groupId") == jQuery("#defaultGroupId").val()){
			alert("不能删除默认分组");
			return false;
		}
		if(jQuery(this).parents("tr:first").find("td:eq(1)").html() > "0"){
			alert("请先删除或者转移此分组下的所有域名");
			return false;
		}
		return confirm("确认删除此分组?");
	});
	
	jQuery("a.remove_domain_link").bind("click",function(){
		return confirm("确认删除此域名?");
	});
	
	jQuery("#select_status").bind("change",function(){
		jQuery("#filter_by_status").click();
	});
	
	jQuery("#select_page_size").bind("change",function(){
		jQuery("#submit_page_size").click();
	});
	
	jQuery("#refresh_bid_page").bind("click",function(){
		location.reload();
	});
	
	jQuery("input:submit.handle_more_domain").bind("click",function(){
		var type = jQuery(this).attr("handleType");
		jQuery("#handle_type").val(type);
		if(type == "0"){
			return confirm("确认删除所有选中域名?");
		}
	});
	jQuery("input:.buy_bgBT_chushou").bind("click",function(){
			return confirm("确认出售域名?");
	});
	
	jQuery("#fake_email").focus(function(){
		jQuery(this).hide();
		jQuery("#email").show().css({color:"#000"}).focus();
	});
	
	jQuery("#email").blur(function(){
		var email = jQuery(this).val();
		if(!email){
			jQuery(this).hide().css({color:"#ccc"});
			jQuery("#fake_email").show();
		}
	});
	
	jQuery("#fake_password").focus(function(){
		jQuery(this).hide();
		jQuery("#password").show().css({color:"#000"}).focus();
	});
	
	jQuery("#password").blur(function(){
		var password = jQuery(this).val();
		if(!password){
			jQuery(this).hide().css({color:"#ccc"});
			jQuery("#fake_password").show();
		}
	});
	
	jQuery("#remove_messages").bind("click",function(){
		return confirm("确认删除所有选中消息?");
	});
	
	jQuery("a.auction_info_title").toggle(function(){
		jQuery(this).parents('span:first').next("p:first").show();
	},function(){
		jQuery(this).parents('span:first').next("p:first").hide();
	});
	
	jQuery("#add_domains_button").mouseover(function(){
		jQuery(this).css({"background":"url('/themes/ejee/images/NetStep.png') repeat scroll 0 0 transparent"});
	}).mouseout(function(){
		jQuery(this).css({"background":"url('/themes/ejee/images/NetStep_hover.png') repeat scroll 0 0 transparent"});
	});
	
	jQuery("#reject_auction_button").bind("click",function(){
		if(confirm("您确认要拒绝卖家发起的公开拍卖的请求吗？")){
			jQuery("#auction_action_type").val(1);
			return true;
		}else {
			return false;
		}
	});
	jQuery("#agree_auction_button").bind("click",function(){
		if(confirm("你确定在同意卖家提出的公开拍卖请求？")){
			jQuery("#auction_action_type").val(0);
			return true;
		}else {
			return false;
		}
	});
	
	jQuery(".handle_more_domain").bind("mouseover",function(){
		jQuery(this).css({"background-color":"#2268A3","color":"#fff"});
	}).bind("mouseout",function(){
		jQuery(this).css({"background-color":"#fff","color":"#2268A3"});
	});
	
	jQuery("#payway_options").find(":radio:first").bind("click",function(){
		jQuery("#balance_option").show();
		jQuery("#bank_option").hide();
	}).end().find(":radio:eq(1)").bind("click",function(){
		jQuery("#balance_option").hide();
		jQuery("#bank_option").show();
	});
	jQuery("#select_extensions").bind("click",function(e){
		e.stopPropagation();
		var o = jQuery(this).offset();
		var selectedExt = jQuery.trim(jQuery("#selected_extensions").val());
		jQuery("#extensions").find("input[type='checkbox']").attr('checked',false);
		if(selectedExt){
			var exts = selectedExt.split(",");
			jQuery("#extensions").find("input[type='checkbox']").each(function(i,n){
				var v = jQuery(this).val();
				if(jQuery.inArray(v,exts) >= 0){
					jQuery(this).attr("checked",true);
				}
			});
		}
		jQuery("#extensions").show().css({"left":o.left-230,"top":o.top-174});
		
		jQuery("#select_all_usual_extensions").html(select_all_for_usual ? "»选择所有" : "»取消选择");
		jQuery("#select_all_other_extensions").html(select_all_for_other ? "»选择所有" : "»取消选择");
		
		jQuery("#extensions").click(function(e){
			e.stopPropagation();
		});
		jQuery(document).click(function(){
			jQuery("#extensions").hide();
		});
	});
	
	jQuery("#close_extensions").bind("click",function(){
		jQuery("#extensions").hide();
	});
	
	jQuery("#cmd_extensions").bind("click",function(){
		var arr = [];
		jQuery("#extensions").find("input:checkbox").each(function(i,n){
			if(jQuery(this).attr("checked")){
				arr.push(this.value);	
			}
		});
		jQuery("#selected_extensions").val(arr.join(","));
		jQuery("#extensions").hide();
	});
	
	jQuery("#select_all_usual_extensions,#select_all_other_extensions").toggle(function(){
		var id = this.id.replace("select_","");
		jQuery("#"+id).find("input:checkbox").attr("checked",true);
		jQuery(this).html("»取消选择");
		if(this.id.indexOf("usual") > 0){
			select_all_for_usual = false;
		}else {
			select_all_for_other = false;
		}
	},function(){
		var id = this.id.replace("select_","");
		jQuery("#"+id).find("input:checkbox").attr("checked",false);
		jQuery(this).html("»选择所有");
		if(this.id.indexOf("usual") > 0){
			select_all_for_usual = true;
		}else {
			select_all_for_other = true;
		}
	});
	
	jQuery("#select_all_categories").bind("click",function(){
		jQuery("#categories").val("");
	});
	
	jQuery("#search_result_page_size").bind("change",function(){
		jQuery("#submit_search").click();
	});
	
	jQuery("#add-comment-form").find(":radio:first").bind("click",function(){
		jQuery("#accountId_panel").show();
		jQuery("#password_panel").hide();
	}).end().find(":radio:eq(1)").bind("click",function(){
		jQuery("#accountId_panel").hide();
		jQuery("#password_panel").show();
	});
	
	jQuery("#show_history").bind("click",function(){
		var s = jQuery("#bid_histories").css("display");
		if(s == "none"){
			jQuery("#show_bid_histories").trigger("click");
		}
	});
	
	jQuery("#transfer_ways").find(":radio:first").bind("click",function(){
		jQuery("#registrar_info").show();
		jQuery('#transfer_notice').hide();
	}).end().find(":radio:eq(1)").bind("click",function(){
		jQuery("#registrar_info").hide();
		jQuery('#transfer_notice').show();
	});
	
	jQuery("#set_withdraw_way").bind('change',function(){
		if(this.value == 0){
			jQuery("#paypal_panel").show();
			jQuery("#bank_panel").hide();
		}else if(this.value == 1){
			jQuery("#paypal_panel").hide();
			jQuery("#bank_panel").show();
		}
	});
	
	jQuery("#exchange_amount").keyup(exchange);
	jQuery("#exchange_type").bind("change",function(){
		
		if(this.value == "0"){
		jQuery("#exchange_amount").css({"background":"url(/themes/ejee/images/RMB.png) no-repeat scroll 0 0 transparent"});
		}else if(this.value == "1"){
			jQuery("#exchange_amount").css({"background":"url(/themes/ejee/images/meiyuan.png) no-repeat scroll 0 0 transparent"});
		}
		exchange();
	});
	
	function exchange(){
		var amount = jQuery("#exchange_amount").val();
		var type = jQuery("#exchange_type").val();
		var dom = jQuery("#exchange_info");
		var patrn=/^[1-9]*[1-9][0-9]*jQuery/;
		if (!patrn.exec(amount)) {
			dom.html("<span style='color:red'>请输入合法的数字</span>");
		}else { 
			jQuery.ajax({
				url : "/site/exchange?amount="+amount+"&type="+type,
				type:"GET",
				dataType:"text",
				error:function(){},
				success:function(data){
					if(data){
						dom.html(data);
					}
				}
			});
		}
	}
	
	jQuery("#verify_methods").change(function(){
		var dom = jQuery("#html_verify_content");
		if(jQuery(this).val() == "3"){
			dom.show();
		}else {
			dom.hide();
		}
	});
	
	jQuery("#choose_reserved_price").find("input[type='radio']:first").bind("click",function(){
		var dom = jQuery("#reserved_price");
		if(dom){
			jQuery("#reserved_price").hide();
			jQuery("#reserved_price").find("input[type='text']:first").val("");
		}
	});

	jQuery("#choose_reserved_price").find("input[type='radio']:eq(1)").bind("click",function(){
		var dom = jQuery("#reserved_price");
		if(dom){
			jQuery("#reserved_price").show();
		}
	});
	
	
	
	jQuery("#auth_methods").bind("change",function(){
		if(jQuery(this).val() == 1){
			jQuery("#individual").show();
			jQuery("#enterprise").hide();
		}else if(jQuery(this).val() == 2){
			jQuery("#individual").hide();
			jQuery("#enterprise").show();
		}
	});
	
	jQuery("#show_bid_histories").toggle(function(){
		jQuery("#bid_histories").show();
		var over = parseInt(jQuery("#over").val());
		if(over == 0){
			jQuery("#refresh_icons").show();
			_refresh_histories();
		}
		jQuery(this).html("隐藏竞价历史");
	},function(){
		jQuery("#bid_histories").hide();
		jQuery("#refresh_icons").hide();
		jQuery(this).html("显示竞价历史");
	});
	
	jQuery("#refresh_histories").bind('click',function(){
		_refresh_histories();	
	});			

	jQuery(".cancel_transaction").bind("click",function(){
		return confirm("您确定要撤销出售此域名?");
	});
	
	//------------注册-------------------
	jQuery("#reg_email").bind("blur",function(){
		var _this = jQuery(this);
		jQuery.ajax({
			url : "/site/checkRegisterEmail?email="+_this.val()+"&_="+Math.round(Math.random()*10000),
			dataType:"html",
			error:function(){
			},
			beforeSend:function(){
				_this.next("span.msg").html('<img src="/themes/ejee/images/loading_re.gif">');
			},
			success:function(data){
				_this.next("span.msg").html(data);
			}
		});
	});
	
	
	
//	jQuery("#reg_password").bind("blur",function(){
//		var _this = jQuery(this);
//		jQuery.ajax({
//			url : "/site/checkRegisterPassword?password="+_this.val()+"&_="+Math.round(Math.random()*10000),
//			dataType:"html",
//			error:function(){
//			},
//			beforeSend:function(){
//				_this.next("div.msg").html('<img src="/themes/ejee/images/loading_re.gif">');
//			},
//			success:function(data){
//				_this.next("div.msg").html(data);
//			}
//		});		
//	});

//	jQuery("#reg_repassword").bind("blur",function(){
//		var _this = jQuery(this);
//		var pass = jQuery("#reg_password").val();
//		jQuery.ajax({
//			url : "/site/checkRegisterRepassword?password=" + pass + "&repassword="+_this.val()+"&_="+Math.round(Math.random()*10000),
//			dataType:"html",
//			error:function(){
//			},
//			beforeSend:function(){
//				_this.next("span.msg").html('<img src="/themes/ejee/images/loading_re.gif">');
//			},
//			success:function(data){
//				_this.next("span.msg").html(data);
//			}
//		});		
//	});

	jQuery("#reg_password").keyup(function(){
		var $this = jQuery(this), pwd = jQuery.trim($this.val()), repwd = jQuery.trim(jQuery('#reg_repassword').val());
		if(pwd == '') {
			$this.next().html('<span class="cuohao_span"><img src="/themes/ejee/images/cuohao.gif"/>密码不能为空</span>');
		} else if(!/^[A-Za-z0-9]{6,16}$/.test(pwd)){
			$this.next().html('<span class="cuohao_span"><img src="/themes/ejee/images/cuohao.gif"/>密码必须为6-16位的字母或数字组成</span>');
		} else {
			var strength = 0, width = 0, degree = '';
			if(/[a-z]/.test(pwd)) {
				strength++;
			}
			if(/[A-Z]/.test(pwd)) {
				strength++;
			}
			if(/[0-9]/.test(pwd)) {
				strength++;
			}
			strength += pwd.length - 6;
			switch(strength) {
				case 1:width = 10;degree = '很弱';break;
				case 2:width = 20;degree = '弱';break;
				case 3:width = 30;degree = '中';break;
				case 4:width = 40;degree = '中';break;
				case 5:width = 50;degree = '良';break;
				case 6:width = 60;degree = '高';break;
				case 7:width = 70;degree = '高';break;
				case 8:width = 80;degree = '高';break;
				case 9:width = 90;degree = '极高';break;
				default:width = 100;degree = '极高';break;
			}
			$this.next().html('<div class="password_rank"><div style="width:'+width+'%; overflow:hidden;"><img src="/themes/ejee/images/mimatiao.gif"/></div></div><div class="password_rank_text"><span>密码强度：'+degree+'</span></div>');
		}
		if(repwd != '') {
			if(pwd != repwd) {
				jQuery('#reg_repassword').next().html('<span class="cuohao_span"><img src="/themes/ejee/images/cuohao.gif">确认密码不正确</span>');
			} else {
				jQuery('#reg_repassword').next().html('<span class="duihao_span"><img src="/themes/ejee/images/duihao.gif">确认密码正确</span>');
			}
		}
	});
	jQuery("#reg_repassword").keyup(function(){
		if(jQuery(this).val() != jQuery('#reg_password').val()) {
			jQuery(this).next().html('<span class="cuohao_span"><img src="/themes/ejee/images/cuohao.gif">确认密码不正确</span>');
		} else {
			jQuery(this).next().html('<span class="duihao_span"><img src="/themes/ejee/images/duihao.gif">确认密码正确</span>');
		}
	});
	jQuery("#submit_bid").bind('click',function(){
		bid();
	});

	jQuery("#input_price").bind('keypress',function(event){
		if(event.keyCode == 13){
			bid();
		}
	});

	jQuery("#view_broker_info").bind('mouseover',function(){

	});

	jQuery("#domain_keyword,#selected_extensions").keypress(function(e){
		if(e.which == 13) jQuery("#submit_search").click();
	});
	jQuery("#update_search").bind("click",function(){
		jQuery("#submit_search").trigger("click");
	});
	jQuery("#submit_search").bind("click",function(){
		//关键字
		var kw = jQuery.trim(jQuery("#domain_keyword").val());
		if(kw != '' && !kw.match(/^[0-9a-zA-Z\.\-]{1,64}$/)){
			alert('输入的信息不符合规则');
			return false;
		}
		//后缀
		var ex = jQuery("#selected_extensions").val();
		//长度
		var len = jQuery("#domain_length").val();
		//价格范围
		var minPrice = jQuery("#minPrice").val(), maxPrice = jQuery("#maxPrice").val();
		//分类
		var cats = jQuery("#categories").val() || [];
		//添加时间
		var at = jQuery("#add_time").val();
		//排除
		var no_number = jQuery("#no_number").attr('checked');
		var no_connect = jQuery("#no_connect").attr('checked');
		var ps = 0;
		var pinyin = [];
		var link = "/domain/search?";
		jQuery("#pinyin > :checkbox").each(function(){
			pinyin[pinyin.length] = jQuery(this).val() & (jQuery(this).attr('checked') == 'checked');
		});
		pinyin = pinyin.join(',');
		jQuery("input[name='ps']").each(function(){
			if(jQuery(this).attr("checked")){
				ps = jQuery(this).val();
				return false;
			}
		});
		if(ex){
			//ex = ex.replace(",","_");
			link += "&ex="+ex;
		}

		if(len > 0){
			link += "&len="+len;
		}

		if(minPrice > 0) {
			link += '&minPrice=' + minPrice;
		}

		if(maxPrice > 0) {
			link += '&maxPrice=' + maxPrice;
		}

		if(pinyin != '0,0,0,0,0'){
			link += '&pinyin=' + pinyin;
		}

		if(cats && cats.length > 0){
			cats = cats.join("_");
			link += "&cats="+cats;
		}

		if(at > 0){
			link += "&at="+at;
		}

		if(no_number){
			link += "&nnu=1";
		}

		if(no_connect){
			link += "&ncon=1";
		}

		if(kw && jQuery.trim(kw)){
			link += "&key="+kw;
		}

		link += "&ps="+ps;
		link = link.replace('?&','?');

		window.location.href = encodeURI(link);
	});
});

	function bid(){
		var p = jQuery("#input_price").val();
		var ai = jQuery("#auctionId").val();

		jQuery.ajax({
			'url':'/domain/ajaxBidAuction?id='+ai+"&_="+ (new Date()).valueOf(),
			'type':'post',
			'data':{price:p,auctionId:ai},
			'dataType':'json',
			'async':false,
			'error':function(){},
			'beforeSend':function(){
				//jQuery("#input_price:input:disabled");
				jQuery("#input_price:input").attr("disabled","disabled");
				jQuery("#submit_bid:input").attr("disabled","disabled");
				jQuery("#submit_bid:input").css("background-image","url(/themes/ejee/images/fast_give_hui.png)");
				jQuery("#load_quan").css("display","inline");

			},
			//'':function(){
			//},
			'success':function(data){
				if(data.code == 0){
					jQuery("div.tab_BuyUse_1").hide().eq(0).show();
					jQuery("#error").empty().html(data.msg);
				}else if(data.code == 1){
					jQuery("div.tab_BuyUse_1").hide().eq(1).show();
					jQuery("#right").empty().html(data.msg);
					jQuery("#input_price").val(data.cp);
					_refresh_histories();
				}else if(data.code == 2){
					jQuery("div.tab_BuyUse_1").hide().eq(0).show();
					jQuery("#error").empty().html(data.msg);
					jQuery("#input_price").val(data.cp);
					_refresh_histories();
				}
			},
			'complete':function(){
				jQuery("#submit_bid:input").removeAttr("disabled");
				jQuery("#input_price:input").removeAttr("disabled");
				jQuery("#submit_bid:input").css("background-image","");
				jQuery("#load_quan").css("display","none");
			}
		});
	}


function _refresh_histories(){
	var auction_id = jQuery("#auction_id").val();
  	jQuery.ajax({
		url : "/domain/refreshHistories?id="+auction_id+"&_="+Math.round(Math.random()*10000),
		dataType:"html",
		error : function(){
		},
		beforeSend :function(){
			jQuery("#loading_icon").show();
		},
		success:function(data){
			jQuery("#loading_icon").hide();
			jQuery("#bid_histories").html(data);
		}
	});
}


