/**
 * JavaScript
 * プロジェクト名: クリ８
 * ファイル名: js/com.js
 * 作者: 石原
 * 更新日: 2009/07/08
 * 共通関数
 * Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 *
 * 修正履歴:
 * 2009/07/08 Kinet石原 新規作成
 */

/**
 * check_day(year, month, day)
 *
 * 日付の整合性をチェックする
 *
 * @param  integer  year    年
 * @param  integer  month   月
 * @param  integer  day     日
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_day(year, month, day)
{
    if (day_month(year, month) < day) {
        return false;
    }
    return true;
}

/**
 * day_month(year,month)
 *
 * その月の末日を返す
 *
 * @param  ingeger  year    年
 * @param  ingeger  month   月
 * @return ingeger          末日
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function day_month(year,month)
{
    day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if(month==2 && leapyear(year)) return 29;
    return day[month-1];
}

/**
 * leapyear(year)
 *
 * 閏年か判定
 *
 * @param  integer  year    年
 * @return boolean          チェック結果(true: 閏年 / false: 平年)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function leapyear(year)
{
    return year%4==0 && (year%100!=0 || year%400==0);
}

/**
 * check_year(year, msg)
 *
 * 日付の年の正当性をチェックする
 *
 * @param  integer  year    年
 * @param  string   msg     項目名
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_year(year, msg)
{
    if(!check_number(year)) {
        alert(msg + 'の年が半角数字ではありません。半角数字で再入力してください。');
        return false;
    }
    if(year.length != 4) {
        alert(msg + 'の年を西暦4桁で入力してください。');
        return false;
    }
    if(year == '0000') {
        alert(msg + 'の年を正しい日付で入力してください。');
        return false;
    }
    return true;
}

/**
 * check_month(month, msg)
 *
 * 日付の月の正当性をチェックする
 *
 * @param  integer  month   月
 * @param  string   msg     項目名
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_month(month, msg)
{
    if(!check_number(month)) {
        alert(msg + 'の月が半角数字ではありません。半角数字で再入力してください。');
        return false;
    }
    if((month < 1) || (month > 12)) {
        alert(msg + 'の月は1〜12の範囲で入力してください。');
        return false;
    }
    return true;
}

/**
 * check_day2(day, msg)
 *
 * 日付の日の正当性をチェックする
 *
 * @param  integer  day     日
 * @param  string   msg     項目名
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_day2(day, msg)
{
    if(!check_number(day)) {
        alert(msg + 'の日が半角数字ではありません。半角数字で再入力してください。');
        return false;
    }
    if((day < 1) || (day > 31)) {
        alert(msg + 'の月は1〜31の範囲で入力してください。');
        return false;
    }
    return true;
}

/**
 * check_ftype_gazo(fname)
 *
 * ファイルタイプが画像かどうかチェックする
 *
 * @param  string   fname   ファイル名
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_ftype_gazo(fname)
{
    if(fname.match(/\.(jpeg|jpg|gif|png)$/i)){
    } else {
        return false;
    }
    return true;
}

/**
 * check_number(num)
 *
 * 文字列が半角数字かどうかチェックする
 *
 * @param  string   num     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_number(num)
{
    if (num == '') {
        return true;
    }
    if (num.match(/^[0-9]+$/g) != num) {
        return false;
    }
    return true;
}

/**
 * check_number2(num)
 *
 * 文字列が半角数字かどうかチェックする(小数点,マイナスOK)
 *
 * @param  string   num     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_number2(num)
{
    if (num == '') {
        return true;
    }
    if (num.match(/^[0-9\-\.]+$/g) != num) {
        return false;
    }
    if (isNaN(num)) {
        return false;
    }
    return true;
}

/**
 * check_eisu(str)
 *
 * 半角英数字かチェックする
 *
 * @param  string   str     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_eisu(str)
{
    if (str == '') {
        return true;
    } else {
        return (str.match(/^[0-9a-zA-Z]+$/g) == str);
    }
}

/**
 * check_eisu_underbar(str)
 *
 * 半角英数字かチェックする(アンダーバー可)
 *
 * @param  string   str     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_eisu_underbar(str)
{
    if (str == '') {
        return true;
    } else {
        return (str.match(/^[0-9a-zA-Z\_]+$/g) == str);
    }
}


/**
 * check_eisukomoji(str)
 *
 * 半角英数小文字かチェックする
 *
 * @param  string   str     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2008 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_eisukomoji(str)
{
    if (str == '') {
        return true;
    } else {
        return (str.match(/^[0-9a-z]+$/g) == str);
    }
}

/**
 * check_tel(str)
 *
 * 電話番号形式(日本用)かチェックする
 *
 * @param  string   str     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_tel(str)
{
    if(!str.match(/^[0-9\-]{10,13}$/)){
        return false;
    }
    return true;
}

/**
 * check_zip(str)
 *
 * 郵便番号形式(日本用)かチェックする
 *
 * @param  string   str     チェック文字列
 * @return boolean          チェック結果(true: OK / false: NG)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function check_zip(str)
{
    if(!str.match(/^[0-9]{3}-[0-9]{4}$/)){
        return false;
    }
    return true;
}

/**
 * URLencode(str)
 *
 * URLエンコード
 *
 * @param  string   str     エンコード文字列
 * @return string           デコード文字列
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function URLencode(str)
{
    // Unicode to URL encoded UTF-8
    var i, encoded_str, char_code, padded_str;
    encoded_str = "";
    for (i = 0; i < str.length; i++){
        char_code = str.charCodeAt(i);
        if (char_code == 0x20){
            // space -> "+"
            encoded_str += "+";
        }
        else { // else 1
            if (((0x30 <= char_code) && (char_code <= 0x39)) || ((0x41 <= char_code) && (char_code <= 0x5a)) || ((0x61 <= char_code) && (char_code <= 0x7a))){
                // [0-9a-z-A-Z]
                // no escape
                encoded_str += str.charAt(i);
            }
            else if ((char_code == 0x2a) || (char_code == 0x2e) || (char_code == 0x2d) || (char_code == 0x5f)) {
                // [.-_]
                // no escape
                encoded_str += str.charAt(i);
            }
            else { // else 2
                // for internal unicode to UTF-8
                if ( char_code > 0xffff ) {
                    encoded_str += "%" + ((char_code >> 18) | 0xf0).toString(16).toUpperCase();
                    encoded_str += "%" + (((char_code >> 12) & 0x3f) | 0x80).toString(16).toUpperCase();
                    encoded_str += "%" + (((char_code >> 6) & 0x3f) | 0x80).toString(16).toUpperCase();
                    encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
                }
                else if ( char_code > 0x7ff ) {
                    encoded_str += "%" + ((char_code >> 12) | 0xe0).toString(16).toUpperCase();
                    encoded_str += "%" + (((char_code >> 6) & 0x3f) | 0x80).toString(16).toUpperCase();
                    encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
                }
                else if ( char_code > 0x7f ) {
                    encoded_str += "%" + (((char_code >> 6) & 0x1f) | 0xc0).toString(16).toUpperCase();
                    encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
                }
                else {
                    // for ascii
                    padded_str = "0" + char_code.toString(16).toUpperCase();
                    encoded_str += "%" + padded_str.substr(padded_str.length - 2, 2);
                }
            } // else 2
        } // else 1
    } // for
    return encoded_str;
}

/**
 * createXMLHttpRequest(cbFunc)
 *
 * Ajax(HTTP)
 *
 * @author     n.ishihara
 * @copyright  Copyright (c) 2009 Kawasaki Internet Co., Ltd. All Rights Reserved.
 * @version    Release: 0.1
 */
function createXMLHttpRequest(cbFunc) {
    var XMLhttpObject = null;
    try{
        XMLhttpObject = new XMLHttpRequest();
    }catch(e){
        try{
            XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){
                return null;
            }
        }
    }
    if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
    return XMLhttpObject;
}

