function checkTopLogin() {
	var f = document.ktoplogin;
	if (f.username.value == "") {
		alert("请输入您的用户名");
		f.username.focus();
		return false;
	} else if (f.password.value == "") {
		alert("请输入您的密码");
		f.password.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}
function addToFavorite(index, id) {
	$.get("/index.php?m=ajax&ac=add_to_favorite&index=" + index + "&subject_id=" + id + "&SessionId=" + Math.random(), function(data) {
		//alert(data);
		if (data == "need_login") {
			$.colorbox({href:"/index.php?m=ajax&ac=show_login"});
		} else {
			alert(data);
		}
	});
}

function addToCart(index, id) {
	$.get("/index.php?m=ajax&ac=add_to_cart&index="+index+"&id=" + id + "&num="+$("#buy_num").val()+"&SessionId=" + Math.random(), function(data) {
		//alert(data);
		if (data == "need_login") {
			$.colorbox({href:"/index.php?m=ajax&ac=show_login"});
		} else {
			alert(data);
			window.location.reload();
		}
	});
}

function removeFavorite(id) {
	if (confirm("您确定要从收藏夹中移除么")) {
		$.get("/index.php?m=ajax&ac=remove_favorite&id=" + id + "&SessionId=" + Math.random(), function(data) {
			if (data == "1") {
				alert("移除成功");
				window.location.reload();
			} else {
				alert(data);
			}
		});
	} else {
	
	}
}

function addReader(index, id, status) {
	$.get("/index.php?m=ajax&ac=add_reader&index=" + index + "&subject_id=" + id + "&status=" + status + "&SessionId=" + Math.random(), function(data) {
		if (data == "need_login") {
			$.colorbox({href:"/index.php?m=ajax&ac=show_login"});
		} else {
			alert(data);
		}
	});
}

function changeStar(num) {
	var top = - (num-1) * 30;
	document.getElementById('starRate').style.background = "url(/images/img_rate.gif) center " + top + "";
}
function rateBook(id, num) {
	alert("感谢您的参与");
	$.get("index.php?m=ajax&ac=rate_book&id=" + id + "&score=" + num + "&SessionId=" + Math.random(), function(data) {
		$("#starRate").html("");
		changeStar(num);
	});
}

function rateDisc(id, num) {
	alert("感谢您的参与");
	$.get("index.php?m=ajax&ac=rate_disc&id=" + id + "&score=" + num + "&SessionId=" + Math.random(), function(data) {
		$("#starRate").html("");
		changeStar(num);
	});
}

function checkStorage(index, subject_id, store_id) {
	var f = document.kform;
	$.get("index.php?m=ajax&ac=check_storage&index=" + index + "&subject_id=" + subject_id + "&store_id=" + store_id + "&SessionId=" + Math.random(), function(data) {
		f.storage.value = data;
		if (data > 0) {
			$("#storage_status").html("<font color=green>库存<strong>" + data + "</strong>本</font>");
		} else {
			$("#storage_status").html("<font color=red>该店暂无库存</font>");
		}
	});
}

function addSmiley(smiley, textarea) {
	var input = document.kform[textarea];
	input.value += "[s_" + smiley + "]";
	input.focus();
}

function referComment(id, username) {
	var f = document.kform;
	f.refer_id.value = id;
	$("#refer_text").html(" - 引用<strong>" + username + "</strong>的评论(<a href=\"javascript:clearReferComment();\">取消引用</a>)");
	f.content.focus();
}

function clearReferComment() {
	var f = document.kform;
	f.refer_id.value = 0;
	$("#refer_text").html("");
}

function postSpaceStatus() {
	var f = document.kstatus;
	if ($("#status").val() == "") {
		alert("请输入您的心情");
		return false;
	} else {
		f.button.disabled = "disabled";
		f.button.value = "提交中，请稍候";
	}
	$.post("/index.php?m=ajax&ac=post_space_status&SessionId=" + Math.random(), { status: f.status.value }, function(data) {
		if (data == "1") {
			alert("心情发表成功");
			window.location.reload();
		} else {
			alert(data);
			f.button.disabled = "";
			f.button.value = "发表心情";
		}
	});
	return false;
}

function postComment(index, id) {
	var f = document.kform;
	if (index == "product_question") {
		var f = document.kaform;
	}
	if (f.content.value == "") {
		alert("请输入您的评论内容");
		f.content.focus();
		return false;
	} else if (f.authcode.value == "") {
		alert("请填写您看到的图形验证码");
		f.authcode.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		f.button.value = "评论提交中，请稍候";
	}
	$.post("/index.php?m=ajax&ac=post_comment&index=" + index + "&id=" + id + "&SessionId=" + Math.random(), { authcode: f.authcode.value, title: f.title.value, content: f.content.value, refer_id:f.refer_id.value }, function(data) {
		if (data == "1") {
			alert("您的评论已成功提交，请等待本站工作人员审核后显示");
			window.location.reload();
		} else {
			alert(data);
			f.button.disabled = "";
			f.button.value = "发表评论";
		}
	});
	return false;
}

function checkJoinParty() {
	var f = document.kjoinform;
	if (f.mobile.value == "") {
		alert("请输入您的联系手机");
		f.mobile.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function showDetails(id) {
	$("#details_" + id).show();
}
function hideDetails(id) {
	$("#details_" + id).hide();
}

function showLogin() {
	$.colorbox({href:"/index.php?m=ajax&ac=show_login"});
}

function ajaxLogin() {
	var f = document.kajaxlogin;
	if (f.username.value == "") {
		alert("请输入您的用户名");
		f.username.focus();
		return false;
	} else if (f.password.value == "") {
		alert("请输入您的密码");
		f.password.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
	}
	$.post("/index.php?m=ajax&ac=do_login&SessionId=" + Math.random(), { username: f.username.value, password: f.password.value, remember: f.remember.value }, function(data) {
		if (data == "1") {
			alert("登陆成功");
			window.location.reload();
		} else {
			alert(data);
			f.button.disabled = "";
		}
	});
	return false;
}

function checkRegister() {
	var f = document.kform;
	var emailstr = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/; 
	if (getStringLength(f.username.value) > 16 || getStringLength(f.username.value) < 4) {
		alert("用户名长度应为4-16个字符或2-8个汉字");
		f.username.focus();
		return false;
	} else if (!isNaN(f.username.value)) {
		alert("用户名不能为纯数字组合");
		f.username.focus();
		return false;
	} else if (f.password.value.length > 16 || f.password.value.length < 6) {
		alert("密码长度应为6-16个字符");
		f.password.focus();
		return false;
	} else if (f.repassword.value == "" || f.repassword.value != f.password.value) {
		alert("请重复输入密码并确保两次输入的密码相符");
		f.repassword.focus();
		return false;
	} else if (emailstr.test(f.email.value) == false) {
		alert("请正确输入您的电子邮件地址");
		f.email.focus();
		return false;
	} else if (f.usercheck.value == 0) {
		alert("请检查您的用户名并确保用户名通过检测");
		f.username.focus();
		return false;
	} else if (f.authcodecheck.value == 0) {
		alert("请填写您看到的图形验证码并确认验证码正确");
		f.authcode.focus();
		return false;
	} else if (f.termofuse.checked == false) {
		alert("请阅读并同意本站服务条款");
		f.termofuse.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function checkRegisterClick(obj) {
	var text;
	switch (obj.name) {
		case "username":
		text = "4-16个字符或2-8个汉字(数字、字母和下划线)。";
		break;
		case "password":
		text = "6-16个字符(数字、大小写字母和符号)。";
		break;
		case "repassword":
		text = "请重复输入上面您输入的密码。";
		break;
		case "email":
		text = "请输入有效的电子邮箱，可用于在您忘记密码的时候寻回密码。";
		break;
		case "authcode":
		text = "请按照上图中显示字母输入验证码。";
		break;
		default:
		text = "";
	}
	try {
		document.getElementById("d_" + obj.name).className = 'hint hint2';
		document.getElementById("d_" + obj.name).innerHTML = text;
	} catch(e) {
		
	}
}

function checkRegisterBlur(obj) {
	var o = document.getElementById("d_" + obj.name);
	var f = document.kform;
	var emailstr = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/; 
	switch (obj.name) {
		case "username":
		o.innerHTML = "用户名检测中……";
		o.className = "hint hint1";
		$.post("/index.php?m=ajax&ac=check_username&SessionId=" + Math.random(), { username: f.username.value }, function(data) {
			if (data == "available") {
				f.usercheck.value = '1';
				o.innerHTML = "&nbsp;";
				o.className = "hint hint3";
			} else if (data == "error_length") {
				f.usercheck.value = '0';
				o.innerHTML = "用户名长度应为4-16个字符或2-8个汉字";
				o.className = "hint hint4";
			} else if (data == "error_numeric") {
				f.usercheck.value = '0';
				o.innerHTML = "用户名不能为纯数字组合";
				o.className = "hint hint4";
			} else {
				f.usercheck.value = '0';
				o.innerHTML = "您选择的用户名已被占用，请另外选择用户名";
				o.className = "hint hint4";
			}
		});
		break;
		case "password":
		if (f.password.value.length > 16 || f.password.value.length < 6) {
			o.innerHTML = "请填写密码，密码长度应为6-16个字符";
			o.className = "hint hint4";
		} else {
				o.innerHTML = "&nbsp;";
			o.className = "hint hint3";
		}
		return false;
		break;
		case "repassword":
		if (f.repassword.value == "" || f.repassword.value != f.password.value) {
			o.innerHTML = "请重复输入密码并确保两次输入的密码相符";
			o.className = "hint hint4";
		} else {
				o.innerHTML = "&nbsp;";
			o.className = "hint hint3";
		}
		break;
		case "email":
		if (emailstr.test(f.email.value) == false) {
			o.innerHTML = "请正确输入您的电子邮件地址";
			o.className = "hint hint4";
		} else {
				o.innerHTML = "&nbsp;";
			o.className = "hint hint3";
		}
		break;
		case "authcode":
		o.innerHTML = "验证码检测中……";
		o.className = "hint hint1";
		$.post("/index.php?m=ajax&ac=check_authcode&SessionId=" + Math.random(), { authcode: f.authcode.value }, function(data) {
			if (data == "S") {
				f.authcodecheck.value = '1';
				o.innerHTML = "&nbsp;";
				o.className = "hint hint3";
			} else {
				f.authcodecheck.value = '0';
				o.innerHTML = "验证码不正确";
				o.className = "hint hint4";
			}
		});
		break;
		default:
		return;
	}
}

function checkUserName() {
	var f = document.kform;
	$.post("/index.php?m=ajax&ac=check_username&SessionId=" + Math.random(), { username: f.username.value }, function(data) {
		if (data == "available") {
			f.usercheck.value = '1';
			$("#ajax_user_check").html("<font color=green>用户名可用</font>");
		} else if (data == "error_length") {
			f.usercheck.value = '0';
			$("#ajax_user_check").html("<font color=red>用户名长度应为4-16个字符或2-5个汉字</font>");
		} else if (data == "error_numeric") {
			f.usercheck.value = '0';
			$("#ajax_user_check").html("<font color=red>用户名不能为纯数字组合</font>");
		} else {
			f.usercheck.value = '0';
			$("#ajax_user_check").html("<font color=red>您选择的用户名已被占用，请另外选择用户名</font>");
		}
	});
}

function checkProfile() {
	var f = document.kform;
	var emailstr = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/; 
	if (emailstr.test(f.email.value) == false) {
		alert("请正确输入您的电子邮件地址");
		f.email.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function checkLogIn() {
	var f = document.klogin;
	if (f.username.value == "") {
		alert("请输入您的用户名");
		f.username.focus();
		return false;
	} else if (f.password.value == "") {
		alert("请输入您的密码");
		f.password.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function checkCheckoutConfirm() {
	var f = document.kform;
	if (f.realname.value == "") {
		alert("请输入您的收货人姓名");
		f.realname.focus();
		return false;
	} else if (f.mobile.value == "") {
		alert("请输入您的手机号码");
		f.mobile.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function checkBugReport() {
	var f = document.kform;
	if (f.content.value == "") {
		alert("请填写您的反馈意见");
		f.content.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function showProductCover(id) {
	$(".home_product_cover").hide();
	$("#product_cover_"+id).show();
	for (var i = 1; i <= 3; i ++) {
		document.getElementById('product_control_'+i).className = 'a2';
	}
	document.getElementById('product_control_'+id).className = 'a1';
}

function showProductTab(id) {
	$(".current_tab").attr("class", "normal_tab");
	$("#t_0"+id).attr("class", "current_tab");
	$(".product_details").hide();
	$("#c_0"+id).show();
}

function showPayMessage(way) {
	document.kqPay.button.disabled = "disabled";
	$.colorbox({href:"/index.php?m=ajax&ac=show_pay_tips&way="+way});
}

function sendMakeeye() {
	var f = document.kmeform;
	if (f.content.value == "") {
		alert("请输入您的内容");
		f.content.focus();
		return;
	} else {
		f.button.disabled = "disabled";
	}
	$.post("/index.php?m=ajax&ac=submit_makeeye&SessionId=" + Math.random(), { content: f.content.value }, function(data) {
		if (data == "1") {
			alert("您的暗送秋波已成功提交");
			parent.$.colorbox.close();
		} else {
			alert(data);
			f.button.disabled = '';
		}
	});
}

function sendPartyShare(party_id) {
	var f = document.kpsform;
	if (f.content.value == "") {
		alert("请输入您的内容");
		f.content.focus();
		return;
	} else {
		f.button.disabled = "disabled";
	}
	$.post("/index.php?m=ajax&ac=submit_party_share&party_id=" + party_id + "&SessionId=" + Math.random(), { content: f.content.value }, function(data) {
		if (data == "1") {
			alert("您的研习分享已成功提交");
			parent.$.colorbox.close();
		} else {
			alert(data);
			f.button.disabled = '';
		}
	});
}

function confirmInfo(info, url) {
	if (confirm(info)) {
		window.location = url;
	} else {
		
	}
}

function getStringLength(str) {
	var length = 0;
	for (var i = 0; i < str.length; i ++) {
		if (str.charCodeAt(i) > 255) {
			length += 3;
		} else {
			length ++;
		}
	}
	return length;
}

function closeAdv(id) {
	$("#dadv_" + id).hide();
}

function joinClub(club_id) {
	$.get("/index.php?m=ajax&ac=join_club&club_id=" + club_id + "&SessionId=" + Math.random(), function(data) {
		if (data == "need_login") {
			showLogin();
			return;
		} else {
			arr = data.split("|");
			alert(arr[1]);
			if (arr[0] == 1) {
				window.location.reload();
			}
		}
	});
}

function showClubPost(club_id) {
	$.get("/index.php?m=ajax&ac=show_club_post&club_id=" + club_id + "&SessionId=" + Math.random(), function(data) {
		if (data == "need_login") {
			showLogin();
			return;
		} else if (data == "non_joiner") {
			alert("您不是该小组的成员，不能发起话题");
			return;
		} else if (data == "error_club") {
			alert("您指定的小组不正确");
			return;
		} else {
			window.location = '/index.php?m=my&h=my_clubs&ac=show_add_post&club_id=' + club_id;
			return;
		}
	});
}

function postClubPostReply(reply_id) {
	var f = document.kform;
	if (f.content.value == "") {
		alert("请输入您的回应内容");
		f.content.focus();
		return false;
	} else if (f.authcode.value == "") {
		alert("请填写您看到的图形验证码");
		f.authcode.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		f.button.value = "回应提交中，请稍候";
	}
	$.post("/index.php?m=ajax&ac=post_club_post_reply&reply_id=" + reply_id + "&SessionId=" + Math.random(), { authcode: f.authcode.value, title: f.title.value, content: f.content.value }, function(data) {
		if (data == "1") {
			alert("您的回应已成功提交，请等待本站工作人员审核后显示");
			window.location.reload();
		} else {
			alert(data);
			f.button.disabled = "";
			f.button.value = "发表回应";
		}
	});
	return false;
}

// Home Focus
(function($){
	$.fn.ADRoll = function(settings) {
		settings = jQuery.extend({
        	speed : "normal",
			num : 4,
			timer : 1000,
			direction : "top",
			imgHeight : "240"
    	}, settings);
		return this.each(function() {
			$.fn.ADRoll.scllor( $(this), settings );
    	});
	};
	$.fn.ADRoll.scllor = function($this,settings) {
		var index = 0;
		var li = $(".thumbs li");
		var showBox = $(".floater")
		li.hover(function() {
			if (intervalTime) {
				clearInterval(intervalTime);
			}
			index = li.index(this);
			intervalTime = setTimeout(function() {
				ShowAD(index);
			},100);
		},function() {
			clearInterval(intervalTime);
			intervalTime = setInterval(function() {
				ShowAD(index);
				index ++;
				if(index == settings.num){
					index = 0;
				}
			},settings.timer)
		});
		showBox.hover(function() {
			if (intervalTime) {
				clearInterval(intervalTime);
			}
		},function() {
			clearInterval(intervalTime);
			intervalTime = setInterval(function() {
				ShowAD(index);
				index++;
				if (index == settings.num) {
					index = 0;
				}
			}, settings.timer);
		});
		var intervalTime = setInterval(function(){
			ShowAD(index);
			index ++;
			if(index == settings.num){
				index = 0;
			}
		},settings.timer);
		var ShowAD = function(i) {
			showBox.animate({"top":-i*settings.imgHeight},settings.speed);
			$(".thumbs li").eq(i).addClass("highlight").siblings().removeClass("highlight");
		};
	};
})(jQuery);

$(function() {
	$(".cb").colorbox();
	$(".cbr").colorbox({width:"658px", height:"500px"});
})

