var util; // カレンダーを開いている時に要素オブジェクトを保持しておく為のクラス
util = new ExpUtil();
util_yymm = new ExpUtil();
util_dd =  new ExpUtil();
//===============================================================================
//    $function      : カレンダーウィンドウを開く
//    $functionName  : fullOpenCalender()
//    $inParam       : top_type : 初期表示位置指定フラグ
//    $outParam      : カレンダーウィンドウ
//===============================================================================
function fullOpenCalender(top_type) {
    var element;           //日付を取得＆セットする要素オブジェクト
    var date; //初期選択日付

    var el_date1,el_date2;

    if (!util) {
        return;
    }

    // 有効開始日
    element = document.trans_search.datebox;

    el_date1 = document.trans_search.val_yymm;
    el_date2 = document.trans_search.val_dd;

  date = '' + el_date1.value + el_date2.value;
  var calendar = new Calendar(date, getDate);


    //==================================================//
    //new Calendar と calendar.open の間で設定すること
    util.element = element;
//    element.style.backgroundColor='yellow';

    util_yymm.element = el_date1;
    util_dd.element = el_date2;
    //==================================================//

    if (Calendar.prototype.self) calendar = Calendar.prototype.self;

    if (calendar.NOT_ERROR) {

        var font  = "15px";           // カレンダーの文字サイズ設定
        var calendarWidth = 200;      // カレンダーの横幅

        var left;                // カレンダーの表示座標(left)
        var top;

        if (top_type == 1) {
          top = 280;                // カレンダーの表示位置(top)
        }
        else {
          top = 50;                // カレンダーの表示位置(top)
        }

       // ((ウインドウサイズ - 750) / 2) + 180pixel
        var w,h;
        var ua = navigator.userAgent.toUpperCase();

        if (ua.indexOf("SAFARI",0) != -1) {
//              return "SAFARI";
                w = document.documentElement.clientWidth
                h = document.documentElement.clientHeight
        }
        else if (ua.indexOf("FIREFOX",0) != -1) {
//              return "FIREFOX";
                w = document.documentElement.clientWidth
                h = document.documentElement.clientHeight
        }
        else if (ua.indexOf("OPERA",0) != -1) {
//              return "OPERA";
                w = document.body.clientWidth
                h = document.body.clientHeight
        }
        else if (ua.indexOf("NETSCAPE",0) != -1) {
                w = window.innerWidth;
                h = window.innerHeight;
        }
        else if (ua.indexOf("MSIE",0) != -1) {
//              return "IE";
              w = document.documentElement.clientWidth
              h = document.documentElement.clientHeight
        }
        else if (ua.indexOf("MOZILLA/4",0) != -1) {
//              return "NETSCAPE";      // ver 4
                w = window.innerWidth;
                h = window.innerHeight;
        }
        else if (ua.indexOf("MOZILLA",0) != -1) {
//              return "MOZILLA";
                w = document.documentElement.clientWidth
                h = document.documentElement.clientHeight
        }

        left = (w - 750) / 2;
        left = left + 180;

      if(document.getElementById('CalLink')){

        var point = getPoint( document.body, document.getElementById('CalLink'));
        
        top = point.y;
        left = point.x;
      }
      

        calendar.setFontSize("15px");
        calendar.setPoint(left, top);
        calendar.setWidth(calendarWidth);
        // カレンダーの高さ
//        var height=220;
        var height=270;
        calendar.setHeight(height);
        calendar.open();
    }
}
function getPoint(screenObj, elementObj) {
  var point = {x:0, y:0};

  if(arguments.length == 1 || elementObj == null) {
    while (screenObj){
      point.x += screenObj.offsetLeft;
      point.y += screenObj.offsetTop;
      screenObj = screenObj.offsetParent;
    }
    
  } else {
    while (elementObj && elementObj != screenObj){
      point.x += elementObj.offsetLeft;
      point.y += elementObj.offsetTop;
                elementObj = elementObj.offsetParent;
    }
  }
  
  return point;
};


//===============================================================================
//    $function      : 取得した年月日を表示用にリプレイスする
//    $functionName  : getDate(calendar)
//    $inParam       : calendar
//    $outParam      : なし
//===============================================================================
function getDate(calendar){
    var value;
    var dataArray;
    var elementName;

    var sel_yymm,sel_dd;
    var idx_yymm,idx_dd;

//    util.element.style.backgroundColor='#ffffff';

    if(util.element == null) return;
    if(calendar == null) return;

    // 表示フォーマット
//    value = calendar.getValue("9999年 9月 9日")+"("+ calendar.weekDay +")"; //yyyy年 m月 d日(ｗ)
//    value = calendar.getValue()+"("+ calendar.weekDay +")";                 //yyyy.mm.dd(ｗ)
    value = calendar.getValue();                 //yyyy.mm.dd

    // 表示項目にセット
    util.element.value = value;
    elementName = util.element.name;
    util.element = null;

    sel_yymm = value.substr(0,4);
    sel_yymm = sel_yymm + value.substr(5,2);
    sel_dd   = value.substr(8,2);

//document.write("value[" + value + "][" + sel_yymm + "][" + sel_dd + "]");

    for (i=0;i<3;i++) {
        if (util_yymm.element[i].value == sel_yymm) {
            idx_yymm = i;
            break;
        }
    }

    for (i=0;i<31;i++) {
        if (util_dd.element[i].value == sel_dd) {
            idx_dd = i;
            break;
        }
    }

    util_yymm.element.selectedIndex = idx_yymm;
    util_dd.element.selectedIndex   = idx_dd;
}
