/*
 * 共通 JavaScript
 *
 *
 * Copyright (c) 2003 DRECOM CO.,LTD. All rights reserved.
 * 
 * info@drecom.co.jp
 * http://www.drecom.co.jp/
 */
 
/**
 * 常に手前に表示するウィンドウを開く
 */
function openModalWindow(url, name, height, width, scrollbars, resizable, status)
{
  ModalWindow = openWindow(url, name, height, width, scrollbars, resizable, status);
  onfocus = function onFocus(){
    try {
      if (null !=ModalWindow && !ModalWindow.closed) {
        try {
          ModalWindow.focus();
	        } catch(e) {
          document.onfocus = null;
        }
      } else {
        document.onfocus = null;
      }
    } catch (ex) {
    }
  };
  document.onclick = onfocus;

  // センター寄せ
  sWidth = screen.availWidth;
  sHeight = screen.availHeight;
  x = (sWidth - width) / 2;
  y = (sHeight - height) / 2;
  ModalWindow.moveTo(x, y);

  ModalWindow.focus();

  return ModalWindow;
}

/**
 * ウィンドウを開く
 */
function openWindow(url, name, height, width, scrollbars, resizable, status)
{
    var window_condition = "height=" + height + ",width=" + width + 
	",scrollbars=" + scrollbars + ",resizable=" + resizable + ",toolbar=no,status=" + status;

    subWindow = window.open(url, name, window_condition);
    subWindow.focus();
    return subWindow;
}

/**
 * ウィンドウを後ろに開く
 */
function openWindowBack(url, name, height, width, scrollbars, resizable, status)
{
    var window_condition = "height=" + height + ",width=" + width + 
	",scrollbars=" + scrollbars + ",resizable=" + resizable + ",toolbar=no,status=" + status +
	",left=" + window.screen.width;
    subWindow = window.open(url, name, window_condition);
    subWindow.blur();
    window.focus();
    subWindow.moveTo(0,0);
    return subWindow;
}
/**
 * Alert, Confirm window
 */

// 2004-06-09 Takanori Ishikawa 
// -----------------------------------------------------------
// htmlAlert(), htmlConfirm() は html をそのまま
// 出力せず、サニタイズするようにした。

// sanitize html message
function sanitize_msg(msg)
{
    msg = msg.replace(/</g, '&lt;');
    msg = msg.replace(/>/g, '&gt;');
    msg = msg.replace(/&lt;br\s*&gt;/g, '<br>');

    return msg;
}
function closeHtmlAlert() {
  //Fadeは動かない
  infoDiv.display = '';
  document.body.removeChild( infoDiv );
  document.body.removeChild($('overlay'));
}

var ALERT_TEMPLATE_HEAD = '';
var ALERT_TEMPLATE_BOTTOM = '<p align="center"><input type="button" name="close" class="button" value="O　K" onclick="closeHtmlAlert();"></input></p>';
// alert
function htmlAlert(msg, height, width) {
  height = (null == height) ? 150: height;
  width  = (null == width) ? 300: width;
  msg = sanitize_msg(msg);
/*
  safari は表示しない？
  if (XBSUtil.appleWebKit != null) {
    alert(msg);
    return;
  }
*/
  if (defined(window.showModalDialog)) {
    showModalDialog("/html/message_box.html", msg, "dialogHeight: " + height + "px; dialogWidth: " + width + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
  } else {
    dialogArguments = msg;
    ModalWindow = open("/html/message_box.html", "sub_alert","height=" + height + ",width=" + width + 
                  ",scrollbars=no,resizable=no,toolbar=no,status=no,alwaysRaised=yes");
    // センター寄せ
    sWidth = screen.availWidth;
    sHeight = screen.availHeight;
    x = (sWidth - width) / 2;
    y = (sHeight - height) / 2;
    ModalWindow.moveTo(x, y);
    onfocus = function onFocus(){
      if (null !=ModalWindow && !ModalWindow.closed) {
        ModalWindow.focus();
      }
    }
    document.onmousemove = onfocus;
    return ModalWindow;
  }
}
// confirm
function htmlConfirm(msg, height, width)
{
  height = (null == height) ? 120: height;
  width  = (null == width) ? 300: width;
  if (XBSUtil.appleWebKit != null) {
    return window.confirm(msg.replace(/<br[^>]*\/?>/, '\n'));
  } else if (defined(window.showModalDialog)) {
    var value = showModalDialog("/html/confirm.html", sanitize_msg(msg), "dialogHeight: " + height + "px; dialogWidth: " + width + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
    if (typeof value == "undefined") {
      value = false;
    }
    return value;
  } else {
    return window.confirm(msg.replace(/<br[^>]*\/?>/, '\n'));
  }
}

/**
 * 曜日を取得する関数
 * year:  年
 * month: 月
 * day:   日
 */
function getDayOfWeek(year, month, day) {

    DateOfWeek	= new Date();
    DateOfWeek.setYear(year);
    DateOfWeek.setMonth(month - 1);
    DateOfWeek.setDate(day);
    day_of_week_number	= DateOfWeek.getDay();

    days = new Array('日','月','火','水','木','金','土');
    return days[day_of_week_number];
}


// -----------------------------------------------------------
// ポータル、ヘッダ
// -----------------------------------------------------------
/**
 * INPUT FORM の背景色変更
 */
function focusForm(obj,flag){
    if (obj == null || obj.style == null) {
	return;	
    }
    obj.style.backgroundColor = flag ? '#EEEEEE' : '#FFFFFF' 
	}
function Skinup(fnr){
    win = window.open('/contents/skinpop_'+fnr,'skin','width=455,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes');
    win.focus();
}
function imgSwap(name,gifPath) {
    document.images[name].src = gifPath ;
}
/**
 * チェックボックスをすべてチェックする/すべてチェック外す。
 */
function toggleAll(element_name) {
    var len = document.forms.length;
    for (var i=0;i<len;i++) {
      var es = document.forms[i].elements;
      var es_len = es.length;
      for (var j=0;j<es_len;j++) {
        var e = es[j];
        if (e.tagName == 'INPUT' && element_name == e.name) {
          if (e.checked) {
            e.checked = false;
          } else {
            e.checked = true;
          }
        }
      }
    }
}

/**
 * onload イベントを追加する。
 *
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    };
  }
}

function toggleByEffect( element ){
   if( Element.visible( element ) ){
      new Effect.SlideUp( element );
   }else{
      new Effect.SlideDown( element );
   }
}

function showByEffect( element ){
   new Effect.Appear( element );
}

function hideByEffect( element ){
   new Effect.Fade( element );
}

var TIME_OUT = 1000;

function setAlign(master, slave) {
   var masterElement = $(master);
   var slaveElement = $(slave);
   if( !masterElement || !slaveElement ) return;
   var selectedPosX = 0;
   var selectedPosY = 0;
   var masterHeight = masterElement.offsetHeight;
   var masterWidth = masterElement.offsetWidth;
   
   while(masterElement != null){
      selectedPosX += masterElement.offsetLeft;
      selectedPosY += masterElement.offsetTop;
      masterElement = masterElement.offsetParent;
   }
   slaveElement.style.left = selectedPosX;
   slaveElement.style.top = selectedPosY + masterHeight;
}

