/*
 * common.js各種共通JavaScript
 */

/*
 * イベントの追加
 */
function addEvent(elm,listener,fn){
	try{
		elm.addEventListener(listener,fn,false);
	}catch(e){
		elm.attachEvent("on"+listener,fn);
	}
}

// IEのチェック
var isIE6 = false;
if (navigator.userAgent.match(/MSIE (\d\.\d+)/)) {
	if (RegExp.$1 == "6.0") isIE6 = true;
}

// 定数
var SCREEN_ID = "overlayscreen";
var FORM_ID = "overlaylogin";
var CLOSEBUTTON_ID = "overlayclosebtn";

var obScreen = null;
var obForm = null;

var fadeSec = 25;	// 1ステップの間隔（ミリ秒）
var fadeOpacity = 10;	// 1ステップあたりの透過度（%）


/*
 *  ブラウザ互換を持たせたキーコードの取得
 */
function getKeyCode(e){
	if(document.all)
			return  e.keyCode;
	else if(document.getElementById) 
			return (e.keyCode)? e.keyCode: e.charCode;
	else if(document.layers)
			return  e.which;
}

/*
 *  問い合わせフォームの入力チェック
 */
function inquiryCheck(formobject) {
	var rtn = true;
	var name = $("#iname").val();
	var addr = $("#iaddr").val();
	var body = $("#ibody").val();
	
	// エラー内容を消去
	$("#iname_err").html("");
	$("#iaddr_err").html("");
	$("#ibody_err").html("");

	if (name.length < 1) {
		$("#iname_err").html("氏名を入力してください。");
		rtn = false;
	}
	
	if (addr.length < 1) {
		$("#iaddr_err").html("メールアドレスを入力してください。");
		rtn = false;
	} else if (!addr.match(/^[A-Za-z0-9]+[\w\.-]+@[\w\.-]+\.\w{2,}$/)){
		$("#iaddr_err").html("メールアドレスをご確認ください。");
		rtn = false;
	}
	
	if (body.length < 1) {
		$("#ibody_err").html("お問い合わせ内容を入力してください。");
		rtn = false;
	}
	
	if (rtn == true) {
		formobject.submit();
	}
	return rtn;
}


/*
 * ログインフォームの非表示処理
 */
function closeLoginForm() {
	$(obScreen).unbind("click");
	$(obScreen).remove();
	obScreen = null;

	$(obForm).remove();
	obForm = null;

	// IE6 の場合はスクロールを有効にする
	if (isIE6) {
		$("body","html").css({
			"height": "auto",
			"width": "auto"
		});
		$("html").css("overflow","");

		// 親HTMLにSELECTがある場合は、非表示を解除する
		$("select").css("visibility", "visible");
	}
}

/*
 * 記事ランキングのタブ切り替え処理
 */
function changeRankingInfo(obj, focusId) {
	$("ul", $("#newsranking")).each(function() {
		var getId = $(this).attr("id");
		if (getId == "tabwrap") {
			// タブを一旦初期化する
			$("li", $(this)).attr("class", "");
		} else {
			if (getId == focusId) {
				$(this).attr("class", "visible");
			} else {
				$(this).attr("class", "hidden");
			}
		}
	});

	// クリックしたタブをフォーカス状態にする
	$(obj).parent().attr("class", "focus");

}


/*
 * スクロール量を取得する
 */
function getScrollPosition() { 
	var obj = new Object(); 
	obj.x = document.documentElement.scrollLeft || document.body.scrollLeft; 
	obj.y = document.documentElement.scrollTop || document.body.scrollTop; 
	return obj; 
}


function htmlEncode(str){
  str = str.replace(/&/g, "&amp;");
  str = str.replace(/"/g, "&quot;");
  str = str.replace(/</g, "&lt;");
  str = str.replace(/>/g, "&gt;");
  str = str.replace(/"/g, "&quot;");
  str = str.replace(/'/g, "&#39;");
	return str;
}

function htmlDecode(str){
  str = str.replace(/&amp;/g, "&");
  str = str.replace(/&quot;/g, "\"");
  str = str.replace(/&lt;/g, "<");
  str = str.replace(/&gt;/g, ">");
  str = str.replace(/&#39;/g, "\'");
	return str;
}

//URL Encode (UTF-8)
function encodeURL(str) {
  var character = '';
  var unicode   = '';
  var string    = '';
  var i         = 0;

  for (i = 0; i < str.length; i++) {
    character = str.charAt(i);
    unicode   = str.charCodeAt(i);

    if (character == ' ') {
      string += '+';
    } else {
      if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
        string = string + character;
      } else {
        if ((unicode >= 0x0) && (unicode <= 0x7f)) {
          character   = '0' + unicode.toString(16);
          string += '%' + character.substr(character.length - 2);
        } else if (unicode > 0x1fffff) {
          string += '%' + (oxf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
          string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else if (unicode > 0x7ff) {
          string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else {
          string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        }
      }
    }
  }

  return string;
}


/**
 * クッキーを操作するための汎用クラス
 */
function Cookie() {
}

Cookie.prototype.get = function(name) {
	var regexp = new RegExp("\\b" + name + "=([^;]*)");
	return regexp.exec(document.cookie) ?
		this._unescape(RegExp.$1) : "";
};

Cookie.prototype.set = function(name, value, days, path, domain, days_direct) {
	//alert('Cookie.set(' + name + ',' + value + ',' + days + ',' + path + ',' + domain + ',' + days_direct + ')');

	var         temp  =   name + "=" + this._escape(value);
	if (days) {
		if (days_direct)
			temp += "; expires=" + days;
		else
			temp += "; expires=" + this._expires(days);
	}
	if (path)   temp += "; path="    + path;
	if (domain) temp += "; domain="  + domain;
	document.cookie = temp;
};

Cookie.prototype._expires = function(days) {
	var date = new Date();
	var days_sec = days*24*60*60*1000;
	//alert(days_sec);
	date.setTime(date.getTime() + days_sec);
	return date.toGMTString();
};

/* MSIE5.5未満?は代用品 */
Cookie.prototype._escape   = window.encodeURIComponent || escape;
Cookie.prototype._unescape = window.decodeURIComponent || unescape;

/*
 * メインルーチン
 */
var cookie      = new Cookie();

