$(document).ready(function(){
	$("select[name='show_count']").change(function(){
		choose_show_count($(this).val());
	});
	$("select[name='TradeType']").change(function(){
		if($(this).val()=='mypost'){
			$("form[name='qsearch']").attr("action","/Member/search-");
		}else{
			$("form[name='qsearch']").attr("action","/search.php");
		}
	});
	if($("#spam_list li:eq(1)").html()==null){
		$("#spam_list").css("display","none");
	}
	$("#spam_list li:eq(0)").click(function(){
		if($("#open_spam").attr("src")=='/images/minus.gif'){
			$("#spam_list li").each(function(i){
				if(i>0){
					$(this).fadeOut();
				}
			});
			$("#open_spam").attr("src","/images/plus.gif")
		}else{
			$("#spam_list li").each(function(i){
				if(i>0){
					$(this).fadeIn();
				}
			});
			$("#open_spam").attr("src","/images/minus.gif")
		}
	});
});
function choose_show_count(limit){
	qs = window.location.search.substring(1);
	var newqs='';
	for (var x in qs.split("&")){
		if(qs.split("&")[x].split("=")[0]!='show_count'){
			newqs+="&"+qs.split("&")[x];
		}
	}
	location.href="http://"+location.host+location.pathname+"?"+newqs+"&show_count="+limit;
}
function select_all(){
	$("input[name='delete[]']").each(function(){$(this).attr("checked",true)});
}
function clear_all(){
	$("input[name='delete[]']").each(function(){$(this).attr("checked",false)});
}
function delete_post(){
	var selected=0;
	$("input[name='delete[]']").each(function(){
		if($(this).attr("checked")==true)selected++;
	});
	if(confirm("Do you really want to delete these message?")){
		if(selected>0){document.form1.submit();}else{alert("Please least select a message!");return false;}
	}else{
		return false;
	}
}
function check_post_msg(){
	var error=false;
	
	if($("input[name='post_subject']").val()==''){
		$("#post_subject").css('display','block');
		error=true;
	}else{
		$("#post_subject").css('display','none');
	}
	if($("input[name='chk_code']").val()==''){
		$("#chk_code").css('display','block');
		error=true;
	}else{
		$("#chk_code").css('display','none');
	}
	if($("textarea[name='post_message']").val()==''){
		$("#message").css('display','block');
		error=true;
	}else{
		$("#message").css('display','none');
	}
	if(error==true){
		alert("There is some error with form!");
		return false;
	}
}
function check_reply(){
	var reply_field=new Array("message","company","firstname","reply_email","phone_area","phone_number");
	var email=$("input[name='reply_email']").val();
	error=0;
	for(var z in reply_field){
		if($("input[name='"+reply_field[z]+"']").val()==''){
			if((/[^_]+_number$/.test(reply_field[z]))){
				$("#phone").css('display','block');
			}else{
				$("#"+reply_field[z]).css('display','block');
			}
			error=true;
		}else{
			$("#"+reply_field[z]).css('display','none');
		}
	}
	
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
		$("#reply_email").css('display','block');
		error=true;
	}
	if(error==true){
		alert("Please check your form!");
	}else{
		document.replymsg.submit();
	}
}
function report_spam(email,sn,id){
	if(confirm("Make sure spam from "+email+"'s message?")){
		$.get("spam.php",{ email: email ,sn:sn , reply_sn:id},function(data){
			if(data!=''){
				var emsg=$("emsg",data).text();
				if(emsg!=''){alert(emsg);return false;}
				var scount=$("scount",data).text();
				var sid=$("sid",data).text();
				if($("#spam_list").css("display")=='none'){
					$("#spam_list").css("display","block")
				}
				$("#spam_list li:eq(0)").after("\n<li id='sa"+sid+"'><a style='cursor:pointer;' onclick=\"allow_spam('"+email+"','"+sn+"')\">"+email+"("+scount+")</a></li>");
				$(".bgwhite#spam"+sid).remove();
			}
		});
		
	}
}
function allow_spam(email,sn){
	if(confirm("Make sure allow "+email+"'s message?")){
		$.get("spam.php",{ email: email ,act:"allow",sn:sn},function(data){
			var emsg=$("emsg",data).text();
			if(emsg!=''){alert(emsg);return false;}
			var sid=$("sid",data).text();
			$("respone",data).each(function(i){
				$("table.table-line tr:eq(3)").after($(this).text());
			});
			$("#sa"+sid).remove();
		});
		if($("#spam_list li:eq(1)").html()==null){
			$("#spam_list").css("display","none");
		}
	}
}
function test(){
	alert($("form[name='qsearch']").attr("action"));
	//return false;
}