var isIE = navigator.appName.charAt(0) == "M";
var isMac = navigator.appVersion.indexOf("Mac") != -1;
var wbcode = new Array();
wbcode["０"] = "0";
wbcode["１"] = "1";
wbcode["２"] = "2";
wbcode["３"] = "3";
wbcode["４"] = "4";
wbcode["５"] = "5";
wbcode["６"] = "6";
wbcode["７"] = "7";
wbcode["８"] = "8";
wbcode["９"] = "9";
wbcode["−"] = "-";
wbcode["．"] = ".";
wbcode["。"] = ".";

/*
 * 年齢の取得
 *
 * パラメータ
 *  gengou:  元号種別(0:昭和, 1:平成)
 *  byear:   生年(和暦)
 *  bmonth:  生月
 *  bdate:   生日
 *
 * 戻り値
 *  年齢
 */
function CalculateAge(gengou, byear, bmonth, bday)
{
    var wyear;
    var today = new Date();
    // 「今年」の取得
    var thisyear = today.getYear();
    if (thisyear < 1000)
        thisyear += 1900;
    // 今年の誕生日のDate値を取得
    var birthday = new Date(thisyear, bmonth-1, bday);
    // 和暦から西暦へ
    if (gengou == 0)
        wyear = Number(byear) + 1925;
    else
        wyear = Number(byear) + 1988;
    // 年齢計算
    var age = thisyear - wyear;
    // 誕生日前なら1を引く
    if ((today.getMonth() < (bmonth - 1))
    ||  (today.getMonth() == (bmonth - 1) && today.getDate() < bday))
        age--;

    return age;
}

/*
 * 数値をコンマ区切りの書式に変換
 *
 * パラメータ
 *  val: 変換する数値
 *
 * 戻り値
 *  3桁ごとにカンマが挿入された数値文字列
 */
function CommaFormat(val)
{
    var str = String(val);
    var result;
    result = str.charAt(0);
    for (var i=1; i<str.length; i++)
    {
        if ((str.length-i)%3 == 0)
            result += ',';
        result += str.charAt(i);
    }

    return result;
}

/*
 * キーが数値キーかどうかの識別
 *
 * パラメータ
 *  e: イベントオブジェクト(NNのみ)
 *
 * 戻り値
 *  数値キーなら true
 */
function IsNumericKey(e)
{
    if (isIE)
    {
        key = event.keyCode;
        return (48 <= key && key <= 57) || (key == 0) || (key == 8) || (key == 9) || (key == 46) || (key == 127)
            || (96 <= key && key <= 105) || (189 <= key && key <= 190) || (109 <= key && key <= 110);
    }
    else
    {
        key = e.which;
        return (48 <= key && key <= 57) || (key == 0) || (key == 8) || (key == 9) || (key == 46) || (key == 127) || (189 <= key && key <= 190);
    }
}

/*
 * キータイプの取得
 *
 * パラメータ
 *  e: イベントオブジェクト(NNのみ)
 *
 * 戻り値
 *  0: リターンキー
 *  1: 数値キー(period, minus)
 *  2: コントロールキー
 *  3: その他キー
 */
function RetrieveKeyType(e)
{
    if (isIE)
    {
        key = event.keyCode;
        if ((48 <= key && key <= 57)     // 1 to 0
        ||  (96 <= key && key <= 105)    // 10 key
		||  (189 <= key && key == 190)   // minus, period
		||  (109 <= key && key == 110))  // minus, period (10 key)
            return 1;
        else if (key == 13)
            return 0;
        else if (key == 8     // Backspace
              || key == 46    // Delete
              || key == 9     // Tab
              || key == 16    // Shift
              || key == 17    // Ctrl
              || key == 18    // Alt
              || (37 <= key && key <= 40) // Arrow keys
              || key == 27)   // Esc
            return 2;
        else
            return 3;
    }
    else
    {
        key = e.which;
        if (isMac)
        {
            if (48 <= key && key <= 57)      // 1 to 0
                return 1;
            else if (key == 13 || key == 3 || key == 9)
                return 0;
            else if (key == 8     // Backspace
                  || key == 0     // Delete
//                  || key == 9     // Tab
                  || key == 127   // Del (Mac)
                  || (28 <= key && key <= 31) // Arrow Keys
                  || key == 27)   // Esc
                return 2;
            else
                return 3;
        }
        else
        {
            if (48 <= key && key <= 57)      // 1 to 0
                return 1;
            else if (key == 13)
                return 0;
            else if (key == 8     // Backspace
                  || key == 0     // Delete, Arrow Keys
                  || key == 9     // Tab
                  || key == 27)   // Esc
                return 2;
            else
                return 3;
        }
    }
}

/*
 * 日付の正当性のチェック
 *
 * パラメータ
 *  y: 西暦年
 *  m: 月
 *  d: 日
 *
 * 戻り値
 *  正当な日付なら true
 */
function IsValidDate(y, m, d)
{
    var givenDate = new Date(y, m-1, d);
    var theyear = givenDate.getYear();
    if (theyear < 1000)
        theyear += 1900;
    return theyear == y && givenDate.getMonth() == m-1 && givenDate.getDate() == d;
}

/*
 * 元号による日付の正当性のチェック
 * (昭和は64年1月7日まで、平成は1年1月8日からの日付が有効)
 *
 * パラメータ
 *  era: 昭和なら 0, 平成なら 1
 *  y  : 和暦年
 *  m  : 月
 *  d  : 日
 *
 * 戻り値
 *  元号と日付の関係が正しければ true
 */
function IsValidEra(era, y, m, d)
{
    return ! ((era == 0 && y > 64)
           || (era == 0 && y == 64 && m > 1)
           || (era == 0 && y == 64 && m == 1 && d > 7)
           || (era == 1 && y == 1 && m == 1 && d < 8));
}

/*
 * 和暦の年を西暦の年に変換
 *
 * パラメータ
 *  gengou: 昭和なら0, 平成なら1
 *  jy:     和暦の年
 *
 * 戻り値
 *  西暦の年
 */
function EraExchange(gengou, jy)
{
    if (gengou == 0)
        return Number(jy) + 1925;
    else
        return Number(jy) + 1988;
}

/*
 * 文字列中の全角数字を半角数字に変換
 *
 * パラメータ
 *  str: 変換する文字列
 *
 * 戻り値
 *  変換後の文字列
 */
function To1byte(str)
{
    for (wb in wbcode)
    {
        tmp = str.split(wb);
        str = tmp.join(wbcode[wb]);
    }
    return str;
}

/*
 * 文字列が全角文字を含んでいるかどうか
 *
 * パラメータ
 *  str: 検査対象の文字列
 *
 * 戻り値
 *  全角文字を含んでいれば true
 */
function IsInclude2byte(str)
{
    var esc = escape(str);
    return esc.indexOf("%") != -1;
}

/*
 *
 */
function InputCheck(e, isDouble, maxlen, laststr, errstr)
{
	var elem = isIE ? window.event.srcElement : e.target;
	var keytype = RetrieveKeyType(e);
    var status = false;
    // 数値キー(2byte 数値でもここにくることがある)
    if (keytype == 1)
    {
        // IME が ON の場合に elem.value には反映されず、キーイベントだけ受取る場合がある
        if (!isIE && (elem.value.length < maxlen) && (elem.value == laststr))
        {
			if (errstr != "")
            	alert(errstr+"を半角数字で正しく入力してください。");
        }
        else if (elem.value.length <= maxlen)
        {
            status = true;
        }
		else
			elem.value = laststr;
    }
    // リターンキー
    else if (keytype == 0)
    {
        // 半角数値が入力された状態でリターンキーが押されても無視
        // 全角数値が入力された状態でリターンキーが押されたら半角に変換
        if (IsInclude2byte(elem.value) || isNaN(elem.value))
        {
            elem.value = To1byte(elem.value);
            if (isNaN(elem.value))
            {
				if (errstr != "")
                	alert(errstr+"を半角数字で正しく入力してください。");
                elem.value= To1byte(laststr); // 元の文字列を復元
            }
            else
            {
                status = true;
            }
        }
    }
    // 文字キーなど
    else if (keytype == 3)
    {
		if (errstr != "")
        	alert(errstr+"を半角数字で正しく入力してください。");
        elem.value = To1byte(laststr);
    }
    // コントロールキー
    else
    {
        if (elem.value != laststr)
            status = true;
    }
    if (isMac && !isIE)
    {
        elem.value = To1byte(elem.value);
        if (status && (elem.value == laststr))
        {
			if (errstr != "")
            	alert(errstr+"を半角数字で正しく入力してください。");
        }
    }

    return status;
}

/*
 * キーの検討
 *
 * パラメータ
 *  keytype:   キー種類 (0:リターンキー, 1:数値キー, 2:コントロールキー, 3:その他のキー)
 *  laststr:   当該エレメントのキー入力前の値
 *  elem:      エレメント
 *  maxlen:    最大文字数
 *  elemjname: エレメント日本語名
 *
 * 戻り値
 *  有効なキーなら true
 */
function ExamKey(keytype, laststr, elem, maxlen, elemjname)
{
    var status = false;
    // 数値キー(2byte 数値でもここにくることがある)
    if (keytype == 1)
    {
        // IME が ON の場合に elem.value には反映されず、キーイベントだけ受取る場合がある
        if (!isIE && (elem.value.length < maxlen) && (elem.value == laststr))
        {
            alert(elemjname+"を半角数字で正しく入力してください。");
        }
        else
        {
            status = true;
        }
    }
    // リターンキー
    else if (keytype == 0)
    {
        // 半角数値が入力された状態でリターンキーが押されても無視
        // 全角数値が入力された状態でリターンキーが押されたら半角に変換
        if (IsInclude2byte(elem.value) || isNaN(elem.value))
        {
            elem.value = To1byte(elem.value);
            if (isNaN(elem.value))
            {
                alert(elemjname+"を半角数字で正しく入力してください。");
                elem.value= To1byte(laststr); // 元の文字列を復元
            }
            else
            {
                status = true;
            }
        }
    }
    // 文字キーなど
    else if (keytype == 3)
    {
        alert(elemjname+"を半角数字で正しく入力してください。");
        elem.value = To1byte(laststr);
    }
    // コントロールキー
    else
    {
        if (elem.value != laststr)
            status = true;
    }
    if (isMac && !isIE)
    {
        elem.value = To1byte(elem.value);
        if (status && (elem.value == laststr))
        {
            alert(elemjname+"を半角数字で正しく入力してください。");
        }
    }

    return status;
}