/* **************************************************************************
 * Ajaxとレイヤーを使って新しいウィンドウを開かず,現行のウィンドウの上に
 * フォームの内容を表示します。
 *
 * 必要ライブラリ:
 *  prototype.js
 *****************************************************************************/

/*
 * 新しいdivを追加して、指定されたURLの中身を表示する
 */ 
function modal_open(url, visible){

  /* 既に、divのモーダルフォームを開いていたら、何もしない */
  if (  parent.document.getElementById("modalForm" ) != null ){
    return;
  }
  // IE6なら既存のdivで重ねても上に表示されるコントールを非可視にする
  visible_controlls(window.document,'hidden');

  // bodyタグを取得
  var body = document.getElementsByTagName("body")[0];
  // div で新しいレイヤーを追加
  var baseDiv = document.createElement("div");
  // styleを設定
  baseDiv.style.position="absolute";
  baseDiv.style.zIndex = 1;
  baseDiv.style.left=0;
  baseDiv.style.top=0;
  baseDiv.style.backgroundColor = "#AAA";
  baseDiv.style.filter = "alpha(opacity=80)"; /* 透明度 for IE */
  baseDiv.style.opacity = "0.8"; /* 透明度 for other */
  baseDiv.style.visibility = visible;
  baseDiv.style.width = "100%";
  baseDiv.style.height = getBodyHeight() +"px";
  baseDiv.id ="modalBase";

  // bodyにフォームを追加
  body.appendChild(baseDiv);

  // iframe で指定されたURLのコンテンツを取得
  var iframe = document.createElement("iframe");
  iframe.src = url;
  iframe.style.visibility = visible;
  // 中央に配置されるように調整
  iframe.style.position="absolute";
  iframe.style.width = "600px";
  iframe.style.height = "400px";
  iframe.style.top="50%";
  iframe.style.left="50%";
  iframe.style.marginTop = "-240px";
  iframe.style.marginLeft= "-320px";
  iframe.style.zIndex = 10;
  iframe.style.borderStyle="solid";
  iframe.style.borderWidth="2px;"

  iframe.style.backgroundColor= "#ffffff";
  iframe.id="modalForm";
  iframe.name="modalForm";
  body.appendChild(iframe);

}

/**
 * モーダルダイアログの表示を設定します
 */
function setmodelVisible(visible) {
	var baseDiv = parent.document.getElementById("modalBase");
	baseDiv.style.visibility = visible;
	var iframe = parent.document.getElementById("modalForm");
	iframe.style.visibility = visible;
}


/**
 * bodyタグの高さを得る
 */
function getBodyHeight(){
    var height;
    if (window.innerHeight && window.scrollMaxY) {    
        height = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        height = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        height = document.body.offsetHeight;
    }
    
    return height;

}

/*
 * モーダルとして表示したフォームを閉じる
 */
function modal_close(callback){

  // IE6なら非可視にしたコントロールを戻す
  visible_controlls(parent.document,'visible');

  if ( this.frameElement != null && this.frameElement.id == "modalForm" ){
    // iframeの場合
    _deleteElementById("modalBase");
    _deleteElementById("modalForm");
  }else{
    // iframeではない場合
    window.close();
  }

  if(callback) {
	  // コールバックを実行します
	  callback();
  }
}

/*
 * 指定されたidを持つelementを削除する
 *
 * @param id elementのID 
 */
function _deleteElementById(id){
  var e = parent.document.getElementById(id);
  var p = e.parentNode;
  p.removeChild(e);
}


/*
 * モーダルでフォームをリセットする
 */ 
function modal_formreset(element){

 var parElement = element;
 // 上位エレメントへ遡って最初に見つけたフォームに対してresetを行う
 while (parElement != null ) {
   if ( parElement.tagName == "FORM" ){
     parElement.reset();
     parElement = null;
   }else{
     parElement = parElement.offsetParent; // until no more offset parents exist
   }
  }

}

/**
 * IE6であるか判断
 *
 * @return true:ie6である/false:ie6ではない
 */
function is_ie6(){
  // IE7ではFirefox,opera,safari互換のXMLHttpRequest が実装されている
  // なので、これがなければ　IE6とみなす
  return !(window.XMLHttpRequest);
}
/**
 * IE6の場合だけ、すべてのselectタグを不可視にする
 */
function visible_controlls(doc,visibility){
  if ( ! is_ie6() ){
    return;
  }
  var elements = doc.getElementsByTagName('select');
  var max=elements.length;
  for(var i=0; i<max; i++){
    var e = elements[i];
    if ( e.style.visibility != visibility || e.style.visibility == '' ){
      e.style.visibility = visibility
    }
  }
}
