
/**
 *　チェックボックスmyがチェックされないと
 *　セレクトボックスfriendがdisable
 * @param {Object} my
 * @param {Object} friend
 */

function setSelByChk(my, friend)
{
    var sel = $(my).checked;
    $(friend).disabled = !sel;
    if((sel) && $(friend).selectedIndex == 0){
        $(friend).selectedIndex = 1;
    }
}

/**
 * セレクトボックスmyのIndexを0にする
 * とチェックボックスfriendのチェック
 * を外し、myはdisabled
 * @param {Object} my
 * @param {Object} friend
 */
function setChkBySel(my, friend)
{
    if(($(friend).checked) && $(my).selectedIndex == 0) {
        $(friend).checked = false;
        $(my).disabled = true;
    }
}


function setSelsBySel(my,className)
{
    if($(my).selectedIndex == 0){
        flg = true;
    }
    else {
        flg = false;
    }

    $$("." + className).each(function(ele){
        ele.disabled = flg;
    });
}




function switchClassStyle(set, className, property)
{
    $$("."+className).each(function(ele){
        switch(property){
            case 'borderColor':
                ele.style.borderColor = '#' + $(set).value;
                break;
            case 'backgroundColor':
                ele.style.backgroundColor = '#' + $(set).value;
                break;
            default:
                ele.style.clor = '#' + $(set).value;
        }
    });
}



/**
 * php rawurlencodeでエンコード後保存
 * されたクッキーの値を読み込み
 * そのクッキーを削除する
 * @param string key クッキーのキー
 * @return string クッキーの値
 */
function ReadCookie(key) {
     var sCookie = document.cookie;    // Cookie文字列
     var aData = sCookie.split(";");       // ";"で区切って"キー=値"の配列にする
     var oExp = new RegExp(" ", "g");   // すべての半角スペースを表す正規表現
     key = key.replace(oExp, "");          // 引数keyから半角スペースを除去

     var i = 0;
     while (aData[i]) {                           /* 語句ごとの処理 : マッチする要素を探す */
          var aWord = aData[i].split("=");                         // さらに"="で区切る
          aWord[0] = aWord[0].replace(oExp, "");              // 半角スペース除去
          if (key == aWord[0]){
              var str = decodeURIComponent(unescape(aWord[1]));
              var dt = new Date();
              dt.setTime(dt.getTime() - (3600 * 1000));
              var ws = key + "=;expires=" + dt.toGMTString();
              document.cookie = ws;
              return str;
          }
          if (++i >= aData.length) break;                          // 要素数を超えたら抜ける
     }
     return "";                                   // 見つからない時は空文字を返す
}


/**
 * クッキーに値を保存
 * @param string key
 * @param string value
 * @param integer days
 */
function WriteCookie(key, value, days) {
     var str = key + "=" + encodeURIComponent(escape(value)) + ";";         // 書き出す値１ : key=value
     if (days != 0) {                                                 /* 日数 0 の時は省略 */
          var dt = new Date();                                   // 現在の日時
          dt.setDate(dt.getDate() + days);                   // days日後の日時
          str += "expires=" + dt.toGMTString() + ";"; // 書き出す値２ : 有効期限
     }
     document.cookie = str;                                   // Cookie に書き出し
}

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


function strAddSpc(str, len){
    var cnt = len - jstrlen(str);
    for(var i = 0; i < cnt; i++)
        str += ' ';
    return str;
}


function getDateLong(ymd){
    var sp;
    var sWeek = new Array('(日)', '(月)', '(火)', '(水)', '(木)', '(金)', '(土)');
    if(ymd.indexOf("/") < 0){
        sp = ymd.split('-');
    }
    else{
        sp = ymd.split('/');
    }
    var dd = new Date(parseInt(sp[0], 10), parseInt(sp[1], 10) - 1, parseInt(sp[2], 10));
    var dateLong = sp[0] + '年' + sp[1] + '月' + sp[2] + '日 ' + sWeek[dd.getDay()];
    return dateLong;
}

function getWindowSize(){
    var dim = new Array()
     var ua = navigator.userAgent;       // ユーザーエージェント
     var nWidth, nHeight;                   // サイズ
     var nHit = ua.indexOf("MSIE");     // 合致した部分の先頭文字の添え字
     var bIE = (nHit >=  0);                 // IE かどうか
     var bVer6 = (bIE && ua.substr(nHit+5, 1) >= "6");  // バージョンが 6以上 かどうか
     var bStd = (document.compatMode && document.compatMode=="CSS1Compat");
                                                                           // 標準モードかどうか
     if (bIE) {
          if (bVer6 && bStd) {
               dim['width'] = document.documentElement.clientWidth;
               dim['height'] = document.documentElement.clientHeight;
          } else {
               dim['width'] = document.body.clientWidth;
               dim['height'] = document.body.clientHeight;
          }
     } else {
          dim['width'] = window.innerWidth;
          dim['height'] = window.innerHeight;
     }
    return dim;
}

function waitDivMake(){
    var ctrl = Builder.node('div', {id:'ajax-wait'});
    document.body.appendChild(ctrl);
    Element.hide($("ajax-wait"));
}

function waitDivShow(){
    var pos = getWindowSize();
    pos['width']  /= 2;
    pos['height'] /= 2;
    Element.show($("ajax-wait"));

    $("ajax-wait").style.left = Math.ceil(pos['width'] - 30) + 'px';
    $("ajax-wait").style.top = Math.ceil(pos['height'] -30 ) + 'px';
}

function waitDivClose(){
    Element.hide($("ajax-wait"));
}

function setDailyMenu(){
        var tmenus = [['m1', topUrl],
                      ['m4', basePath + 'browse/statist.php'],
                      ['m6', basePath + 'regist/dailyTop.php']];
        if($("m7")){
            tmenus.push(['m7', basePath + 'edit/managerLogout.php?mode=daily']);
        }
        else{
            tmenus.push(['m8', basePath + 'edit/masterLogin.php?returnUrl=../regist/dailyTop.php']);
        }
        tMemuLinkSet(tmenus);

        var caption = [ '登 録 (会 員)',
                        '登 録 (新 規)',
                        '変 更',
                        '削 除'
        ];

        var urls    = [ basePath + 'regist/reserveCenter.php',
                        basePath + 'regist/newmemReserveCenter.php',
                        basePath + 'regist/updReserveCenter.php',
                        basePath + 'regist/delReserveCenter.php'
        ];
        new Control.CypopupMenu("m2", caption, urls, 'hMenu');
        caption.clear();
        urls.clear();

        caption = [ '来院登録(日次明細)',
                    '未受診者一覧',
                    '予約当日通知メール',
                    '予約承認'
        ];
        urls    = [ basePath + 'browse/dialy.php',
                    basePath + 'browse/withoutCanseled.php',
                    basePath + 'regist/newsToday.php',
                    basePath + 'regist/approval.php'
        ];
        new Control.CypopupMenu("m3", caption, urls, 'hMenu');
        caption.clear();
        urls.clear();

        caption = [ '新規登録',
                    '変更・削除',
                    '一覧・メルマガ送信'
        ];
        urls    = [ basePath + 'member/memEnter.php',
                    basePath + 'member/memUpdate.php',
                    basePath + 'member/memList.php'
        ];
        new Control.CypopupMenu("m5", caption, urls, 'hMenu');

}

function setEditMenu(){

        var tmenus = [['m1', topUrl],
                      ['m2', basePath + 'edit/preferences.php'],
                      ['m5', basePath + 'edit/operatorMente.php'],
                      ['m6', basePath + 'edit/editTop.php']];
        if($("m7")){
            tmenus.push(['m7', basePath + 'edit/managerLogout.php?mode=edit']);
        }
        else{
            tmenus.push(['m8', basePath + 'edit/masterLogin.php?returnUrl=./editTop.php']);
        }
        tMemuLinkSet(tmenus);

        var caption = [ '新患予約受付',
                        '会員予約受付',
                        '予約確定通知',
                        '予約変更受付',
                        '変更確定通知',
                        'キャンセル受付通知',
                        '新患予約-自動確定',
                        '会員予約-自動確定',
                        '予約変更-自動確定',
                        '無断キャンセル確認',
                        '予約当日のお知らせ'
        ];

        var urls    = [ basePath + 'edit/mailsentence.php?mode=newAppCnf',
                        basePath + 'edit/mailsentence.php?mode=memAppCnf',
                        basePath + 'edit/mailsentence.php?mode=DeciCnf',
                        basePath + 'edit/mailsentence.php?mode=changeAppCnf',
                        basePath + 'edit/mailsentence.php?mode=changeDeciCnf',
                        basePath + 'edit/mailsentence.php?mode=cancelApp',
                        basePath + 'edit/mailsentence.php?mode=newAppAuto',
                        basePath + 'edit/mailsentence.php?mode=memAppAuto',
                        basePath + 'edit/mailsentence.php?mode=cangeAppAuto',
                        basePath + 'edit/mailsentence.php?mode=withOut',
                        basePath + 'edit/mailsentence.php?mode=newsToday'
        ];
        new Control.CypopupMenu("m3", caption, urls, 'hMenu');

        caption.clear();
        urls.clear();

        caption = [ '初期条件設定',
                    '年間スケジュール',
                    '受付一時停止登録'
        ];
        urls    = [ basePath + 'edit/configCalender.php',
                    basePath + 'edit/setYearPlan.php',
                    basePath + 'edit/spotTimeSet.php'
        ];
        new Control.CypopupMenu("m4", caption, urls, 'hMenu');

}
