// Get a cookie value...
function getCookieVal(cookieName) {
	var i, index, arr;
    arr = document.cookie.split(";");
    for(i = 0; i < arr.length; i++) {
        index = arr[i].indexOf("=");
        if(arr[i].substring(0, index) == cookieName ||
           arr[i].substring(0, index) == " " + cookieName) {
        	return decodeURI(arr[i].substring(index + 1));
        }
    }
    return "";
}

function url(protocol) {
    if (protocol == 'http' || protocol == 'https') {
        return protocol + "://www.moug.net";
    } else {
        return '';
    }
}

function writeLoginForm() {
	var uname = username();
	table = document.getElementById("login_table");
	tbody = document.createElement("tbody");
	
	tr = document.createElement("tr");
	td = document.createElement("td");
	str_f = document.createTextNode("ようこそ ");
	str_l = document.createTextNode(" さん");
	str_u = document.createTextNode(uname);
	b = document.createElement("b");
	b.appendChild(str_u);
	td.appendChild(str_f);
	td.appendChild(b);
	td.appendChild(str_l);
	tr.appendChild(td);
	
	td = document.createElement("td");
	td.align = "right";
	input = document.createElement("input");
	input.type = "submit";
	input.id = "button";
	form = document.getElementById("login_form");
	var sid = '';
	if (uname == 'ゲスト') {
		input.name = "login_btn";
		input.value = "ログイン";
		form.setAttribute("action", url('https') + "/faq/login.php");
	} else {
		input.name = "logout_btn";
		input.value = "ログアウト";
		if (getCookieVal("moug_sid") != '') {
			sid = "&sid=" + getCookieVal("moug_sid");
		}
		form.setAttribute("action", url('http') + "/faq/login.php?logout=true" + sid);
	}
	td.appendChild(input);
	tr.appendChild(td);
	tbody.appendChild(tr);
	
	tr = document.createElement("tr");
	td = document.createElement("td");
	td.setAttribute("colspan", "2");
	td.appendChild(document.createElement("br"));
	tr.appendChild(td);
	tbody.appendChild(tr);
	
	tr = document.createElement("tr");
	td = document.createElement("td");
	a = document.createElement("a");
	if (uname == 'ゲスト') {
		a.href = url('https') + "/faq/profile.php?mode=register";
		img = document.createElement("img");
		img.src = "/img/btn_entry.gif";
		img.width = "127";
		img.height = "36";
		a.appendChild(img);
		td.appendChild(a);
		tr.appendChild(td);
		
		td = document.createElement("td");
		td.setAttribute("align", "right");
		str_f = document.createTextNode("パスワードを忘れた方は");
		str_l = document.createTextNode("こちら");
		a = document.createElement("a");
		a.href = url('https') + "/faq/profile.php?mode=sendpassword";
		a.appendChild(str_l);
		td.appendChild(str_f);
		td.appendChild(document.createElement("br"));
		td.appendChild(a);
		tr.appendChild(td);
		
	} else {
		td.setAttribute("colspan", "2");
		td.setAttribute("class", "small");
		str = document.createTextNode("会員情報変更");
		a = document.createElement("a");
		a.href = url('http') + "/faq/profile.php?mode=editprofile";
		a.appendChild(str);
		td.appendChild(a);
		tr.appendChild(td);
	}
	tbody.appendChild(tr);
	table.appendChild(tbody);
	return true;
}

function username() {
	var uname = getCookieVal("uname");
	if (uname == '' || uname == null || uname == 'Anonymous') {
		return 'ゲスト';
	} else {
		return uname;
	}
}

function doSearch() {
	form = document.forms['search'];
	if (form.q.value == '') {
		alert("検索キーワードを入力してください。");
		return false;
	} else {
		var type = "";
		for (i=0; i< form.search_type.length; i++) {
			if (form.search_type[i].checked) {
				type = form.search_type[i].value;
			}
		}
		if (type == "inside") {
			googleSearch();
		} else if (type == "faq") {
			var sid = "";
			if (getCookieVal("moug_sid") != "") {
				sid = "&sid=" + getCookieVal("moug_sid");
			}
			var input = document.createElement("input");
			input.type = "hidden";
			input.name = "search_keywords";
			input.value =  form.q.value;
			form.appendChild(input);
			form.action = url('http') + "/faq/search.php?mode=results" + sid;
			form.target = "";
			form.submit();
			return false;
		}
	}
}

function googleSearch() {
	form = document.forms['search'];
	
	hl = document.createElement("input");
	hl.setAttribute("name", "hl");
	hl.setAttribute("value", "ja");
	hl.setAttribute("type", "hidden");
	form.appendChild(hl);
	ie = document.createElement("input");
	ie.setAttribute("name", "ie");
	ie.setAttribute("value", "Shift-JIS");
	ie.setAttribute("type", "hidden");
	form.appendChild(ie);
	oe = document.createElement("input");
	oe.setAttribute("name", "oe");
	oe.setAttribute("value", "Shift-JIS");
	oe.setAttribute("type", "hidden");
	form.appendChild(oe);
	site = document.createElement("input");
	site.setAttribute("name", "sitesearch");
	site.setAttribute("value", "http://www.moug.net/");
	site.setAttribute("type", "hidden");
	form.appendChild(site);
	
	form.method = "GET";
	form.action = "http://www.google.co.jp/search";
	form.target = "_blank";
	form.submit();
    return false;
}

function isHankaku(name) {
	var element = document.getElementById(name);
    var length = element.value.length;
    if (length == jstrlen(element.value)) {
        return true;
    } else {
    	alert("半角英数字で入力してください");
    	element.value = '';
        return false;
    }
}

function jstrlen(str) {
   len = 0;
   str = escape(str);
   for (i = 0; i < str.length; i++, len++) {
      if (str.charAt(i) == "%") {
         if (str.charAt(++i) == "u") {
            i += 3;
            len++;
         }
         i++;
      }
   }
   return len;
}

function checkAgreement() {
	form = document.forms['agree_form'];
	var type = "";
	for (i=0; i< form.agreement.length; i++) {
		if (form.agreement[i].checked) {
			type = form.agreement[i].value;
		}
	}
	if (type == "disagree") {
		form.action = "/";
	}
	return false;
}

function checkWithdraw() {
	var form = document.forms['withdraw_form'];
	var sid = "";
	if (window.confirm("退会してもよろしいですか")) {
		if (getCookieVal("moug_sid") != '') {
            		sid = "&sid=" + getCookieVal("moug_sid");
        	}
        	form.action = "/faq/login.php?logout=true" + sid;
		form.submit();
	}
	return false;
}

