﻿cj.mbfn = {
	pos : null,
	login : function(){
		var account = document.getElementById("mb_account");
		if (!account){ alert("account not set"); return;}
		var passwd = document.getElementById("mb_passwd");
		if (!passwd){ alert("passwd not set!"); return;}
		var mb_secnum = document.getElementById('mb_secnum');
		if(account.value == ""){
			account.focus();
			var pos = cj.util.getElementPos(account);
			pos.left += 50;
			cj.util.popMsg.init("請輸入帳號", pos, {width:100, height:20});
			return;
		}
		if (passwd.value == ""){
			passwd.focus();
			var pos = cj.util.getElementPos(passwd);
			pos.left += 50;
			cj.util.popMsg.init("請輸入密碼", pos, {width:100, height:20});
			return;
		}
		if (mb_secnum.value == ""){
			mb_secnum.focus();
			var pos = cj.util.getElementPos(mb_secnum);
			pos.left += 50;
			cj.util.popMsg.init("請輸入圖片中的數字", pos, {width:100, height:20});
			return;
		}
		cj.mbfn.loginForm = document.getElementById("box_mb").innerHTML;
		cj.xmlHttp.sendRequest("resp.php", cj.mbfn.login_act, "act=login&account="+account.value+"&passwd="+passwd.value+"&secnum="+mb_secnum.value);
	},
	logout : function(){
		cj.xmlHttp.sendRequest("resp.php", cj.mbfn.logout_act, "act=logout");
	},
	login_form : function(){
		if(typeof cj.mbfn.loginForm != "undefined"){
			document.getElementById("box_mb").innerHTML = cj.mbfn.loginForm;
		}
	},
	forget_form : function(){
		cj.mbfn.loginForm = document.getElementById("box_mb").innerHTML;
		cj.xmlHttp.sendRequest("resp.php", cj.mbfn.put, "act=forget_form");
	},
	forget : function(){
		var email = document.getElementById("email");
		if (!email){ alert("email not set!"); return;}
		var pos = cj.util.getElementPos(email);
			pos.left += 50;
		if(!cj.form.isEmail(email.value)){
			email.focus();
			cj.util.popMsg.init("請輸入正確的E-Mail!", pos, {width:150, height:20});
			return;
		}
		cj.xmlHttp.sendRequest("resp.php", cj.mbfn.login_form, "act=forget&email="+email.value);
		cj.util.popMsg.init("您的帳號/密碼已寄出!", pos, {width:150, height:20});
	},
	login_act : function(req){
		if (!req.responseText){
			return;
		}else{
			cj.mbfn.put(req);
			var pro_bonus_page = document.getElementById("pro_bonus_page");
			if (pro_bonus_page){
				window.location = 'bonus.php';
			}
		}
	},
	logout_act : function(req){
		window.location = "index.php";
		return;
		if (!req.responseText){
			box_mb.innerHTML = req.responseText;
		}else{
			var box_cart = document.getElementById("box_cart");
			if (box_cart){ box_cart.innerHTML = ''; };
			cj.mbfn.put(req);
		}
	},
	put : function(req){
		var box_mb = document.getElementById("box_mb");
		box_mb.innerHTML = req.responseText;
	},
	put1 : function(req){
		var box_mb = document.getElementById("box_mb");
		box_mb.innerHTML += req.responseText;
	},
	chkAccountExists: function(){
		var account = document.getElementById("account");
		if (!account){ return; }
		cj.mbfn.pos = cj.util.getElementPos(account);
		cj.mbfn.pos.left += 300;
		if (!cj.form.isAccount(account.value)){
			account.focus();
			cj.util.popMsg.init("請填入4個以上的字元，第一個字必需是英文字母!", cj.mbfn.pos, {width:200, height:50}, 5);
			return;
		}
		cj.xmlHttp.sendRequest("resp.php", cj.mbfn.chkAccountExists_act, "act=chkAccount&account="+account.value);
	},
	chkAccountExists_act: function(req){
		if (!req.responseText){
			return;
		}
		var chk = eval('(' + req.responseText + ')');
		if (chk){
			if(cj.mbfn.pos){
				cj.util.popMsg.init("恭喜您，這個帳號可以使用!", cj.mbfn.pos, {width:200, height:20}, 5);
			}
		}else{
			if(cj.mbfn.pos){
				var account = document.getElementById("account");
				if (!account){ return; }
				account.focus();
				cj.util.popMsg.init("個帳號已被使用，請改用別的!", cj.mbfn.pos, {width:200, height:20}, 5);
			}
		}
	}
}
cj.cart = {
	pos : null,
	add : function(id, tgt){
		cj.cart.pos = cj.util.getElementPos(tgt);
		cj.cart.pos.left += tgt.offsetWidth;
		cj.xmlHttp.sendRequest("resp.php", cj.cart.put, "act=cart_add&id="+id);
		return false;
	},
	put : function(req){
		if (req.responseText.match(/notLogin/i)){
			alert("請先登入會員!");
			return;
		}
		if (req.responseText.match(/noshop/i)){
			alert("對不起，資料重整中，暫時停止購物!");
			return;
		}
		var box_cart = document.getElementById("box_cart");
		if(box_cart){
			box_cart.innerHTML = req.responseText;
		}
		if (cj.cart.pos){
			cj.util.popMsg.init("您的購物清單已更新<br>請點選上方選單[購物車]結帳!", cj.cart.pos, {width:250, height:40});
		}
	}
}
cj.pro_search = {
	chk: function(fid){
		var f = document.getElementById(fid);
		f.keyword.value = cj.form.trim(f.keyword.value);
		if(f.keyword.value.length == 0){
			alert('請輸入關鍵字');
			return false;
		}
		if(f.keyword.value.length == 1){
			alert('關鍵字數必需是2個字元以上!')
			return false;
		}
		f.submit();
	}
}
