function url(protocol) {
    if (protocol == 'http' || protocol == 'https') {
		return protocol + "://" + location.host;
    } else {
        return '';
    }
}

function addEvent(elm,listener,fn){
	try{
		elm.addEventListener(listener,fn,false);
	}catch(e){
		elm.attachEvent("on"+listener,fn);
	}
}

function doSearch(form) {
	if (form == null) {
		form = document.forms['search'];
	}
	if (form == null || form == undefined) {
		return false;
	}
	if (form.q.value == '') {
		alert("検索キーワードを入力してください。");
		return false;
	}
	var type = "faq";
	for (i=0; i< form.search_type.length; i++) {
		if (form.search_type[i].checked) {
			type = form.search_type[i].value;
		}
	}
	// Search for Q&A
	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 = "";
		return true;
	}
	// Search for moug by Google. 
	else if (type == "inside") {
		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", "as_sitesearch");
		site.setAttribute("value", "www.moug.net");
		site.setAttribute("type", "hidden");
		form.appendChild(site);
		
		form.method = "GET";
		form.action = "http://www.google.co.jp/search";
		form.target = "_blank";
	}
}

// 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) {
           arr[i] = arr[i].replace(/\+/g, "%20");
        	return decodeURIComponent(arr[i].substring(index + 1));
        }
    }
    return "";
}

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;
}

function decodeHtmlEntity(str) {
    str = str.replace(/&amp;/g, '&');
    str = str.replace(/&quot;/g, '"');
    str = str.replace(/&gt;/g, '>');
    str = str.replace(/&lt;/g, '<');

    return str;
}

function showSpcharTable() {
    window.open('spchar_table.html', '_spchartable', 'resizable=no, scrollbars=no, status=no, HEIGHT=170, WIDTH=310');
    return false;
}
