/**
 * 問い合わせ画面がloadされた際にFAXと郵送のフォームを隠す
 */
Ozein.InquireFormShow = new Class.create();
Ozein.InquireFormShow.prototype = {
  initialize: function(node, options) {
    this.setOptions(options);
    this.load();
  },
  setOptions: function(options) {
    this.options = {
      fax: 'none',
      address: 'none'
    }
    Object.extend(this.options, options || {});
  },
  load: function() {
    if (this.options.fax == 'none' && !$('howto4').checked) {
      if (!$('fax1').value && !$('fax2').value && !$('fax3').value) {
        $('areaFax').style.display = 'none';
      }
    }

    if (this.options.address == 'none' && !$('howto8').checked) {
      if (!$('zipInp1').value && !$('zipInp2').value
        && !$('address_addr1_2_id').value && !$('address_addr2_id').value && !$('address_addr3_id').value) {
        var opts = $('prefecture').options;
        var flg = false;
        for(var i = 1; i < opts.length; i++) {
          if(opts[i].selected) {
            flg = true;
            break;
          }
        }
        if (!flg) {
          $('areaAddress').style.display = 'none';
        }
      }
    }
  }
}


/**
 * Inquire
 *  (c) 2007 NEXT CO.,LTD. (http://www.next-group.jp/)
 */

/**
 * NODEが、あるIDを持つNODEの子孫であるかを判定
 *   node :
 *     チェック対象のNODE。
 *   id :
 *     チェック対象のID。
 */
Ozein.isDescendantById = function(node, id) {
  var is_match = false;
  var pointer = node;
  while (pointer) {
    if (pointer.id) {
      is_match = pointer.id.eqi(id);
      if (is_match) {
        break;
      }
    }
    pointer = pointer.parentNode;
  }
  return (is_match);
}

/**
 * 郵便番号から住所入力
 * 画面表示
 */
Ozein.ZipToAddress = new Class.create();
Ozein.ZipToAddress.prototype = {
  win: null,
  initialize: function(node, options) {
    this.id   = node.id;
    this.setOptions(options);
    Event.observe(node, 'click', this.click.bindAsEventListener(this, node));
  },
  setOptions: function(options) {
    this.options = {
      post1: 'zip1',
      post2: 'zip2',
      width: 640,
      height: 300,
      resizable: 'yes'
    }
    Object.extend(this.options, options || {});
  },
  click: function(e, node) {
    if (!Ozein.isDescendantById(node, this.id)) {
      return (true);
    }
    var anchor = node.getAttribute('href');
    var post1 = document.getElementById(this.options.post1).value;
    var post2 = document.getElementById(this.options.post2).value;
    if (!post1 || !post2) {
      alert('郵便番号を入力してください');
      Event.stop(e);
      return (false);
    }
    if (post1.length != 3) {
      alert('郵便番号(上3桁)の桁数が不正です。');
      Event.stop(e);
      return (false);
    }
    if (post2.length != 4) {
      alert('郵便番号(下4桁)の桁数が不正です。');
      Event.stop(e);
      return (false);
    }
    if (post1.match(/\D/) || post2.match(/\D/)) {
      alert('郵便番号に数値以外の値が入力されています。');
      Event.stop(e);
      return (false);
    }
    var href = node.getAttribute('href') + "/" + post1 + "/" + post2 + "/";
    if (this.win == null || this.win.closed == undefined || this.win.closed) {
      var op = 'width=' + this.options.width + ',height=' + this.options.height + ',resizable=' + this.options.resizable;
      this.win = open(href, "_blank", op);
      this.win.focus();
    } else {
        this.win.location.replace (href);
        this.win.focus();
    }
    this.win.focus();
    Event.stop(e);
    return (false);
  }
}

/**
 * 郵便番号から住所入力（ボタンバージョン）
 * 画面表示
 */
Ozein.ZipToAddressBtn = new Class.create();
Ozein.ZipToAddressBtn.prototype = {
  win: null,
  initialize: function(node, options) {
    this.id   = node.id;
    this.setOptions(options);
    Event.observe(node, 'click', this.click.bindAsEventListener(this, node));
  },
  setOptions: function(options) {
    this.options = {
      post1: 'zipInp1',
      post2: 'zipInp2',
      width: 640,
      height: 300,
      resizable: 'yes'
    }
    Object.extend(this.options, options || {});
  },
  click: function(e, node) {
    if (!Ozein.isDescendantById(node, this.id)) {
      return (true);
    }
    
    var anchor = node.getAttribute('ozeinval');
    var post1 = document.getElementById(this.options.post1).value;
    var post2 = document.getElementById(this.options.post2).value;

    if (!post1 || !post2) {
      alert('郵便番号を入力してください');
      Event.stop(e);
      return (false);
    }
    if (post1.length != 3) {
      alert('郵便番号(上3桁)の桁数が不正です。');
      Event.stop(e);
      return (false);
    }
    if (post2.length != 4) {
      alert('郵便番号(下4桁)の桁数が不正です。');
      Event.stop(e);
      return (false);
    }
    if (post1.match(/\D/) || post2.match(/\D/)) {
      alert('郵便番号に数値以外の値が入力されています。');
      Event.stop(e);
      return (false);
    }
    var href = node.getAttribute('ozeinval') + "/" + post1 + "/" + post2 + "/";
    if (this.win == null || this.win.closed == undefined || this.win.closed) {
      var op = 'width=' + this.options.width + ',height=' + this.options.height + ',resizable=' + this.options.resizable;
      this.win = open(href, "_blank", op);
      this.win.focus();
    } else {
        this.win.location.replace (href);
        this.win.focus();
    }
    this.win.focus();
    Event.stop(e);
    return (false);
  }
}

/**
 * 郵便番号から住所入力
 * 住所入力
 */
Ozein.WriteAddress = function() {
  var wo = window.opener.document.forms[0];
  var myform = document.forms[0];
  var index = myform.addr_list.selectedIndex;
  var val = myform.addr_list.options[index].value;
  var addrArr = val.split('::');
  var cnt = wo.prefecture.length;
  addrArr[0] -= 0;
  for (i = 0; i < cnt; ++i) {
    if (wo.prefecture[i].value == addrArr[0]) {
      wo.prefecture.selectedIndex = i;
    break;
    }
  }
  wo.address_addr1_2_id.value = addrArr[2];
  wo.address_addr2_id.value = addrArr[3] + addrArr[4];

  window.close();
  return (false);
}

/**
 * ページ戻る
 */
Ozein.BackInquire = function(obj_form, step) {
  obj_form.inquireStep.value = step;
  obj_form.submit();
  return true;
}

/**
 * ページ閉じる
 */
Ozein.CloseInquire = function() {
  if (window.confirm("このページを閉じますか？")) {
    window.opener = window;
    var win = window.open(location.href,"_self");
    win.close();
    return true;
  }
  return false;
}

/**
 * ブラウザに依存せずにname属性からelementリストを取得
 */
Ozein.getElementsByName = function(name, doc) {
  var _list;

  obj = (doc ? doc : document);
  if (obj.all) _list = obj.all.item(name);
  if (obj.getElementsByName) _list = obj.getElementsByName(name);
  return _list;
}

/**
 * チェックボックス未選択確認処理
 */
Ozein.inquireChk = function(pForm) {
  var nodes = $('noInquireChkBox').getElementsByTagName('input');
  var chkcnt = 0;
  for (var i=0;i<nodes.length;i++) {
    var node = nodes[i];
    if (node.type=="checkbox" && node.checked== true) {
        chkcnt++;
    }
  }
  if (chkcnt == 0) {
    if (window.confirm('全ての物件に問合せますか？')) {
      for (var i=0,len=nodes.length;i<len;i++) {
        nodes[i].checked = true;
      }
      pForm.submit();
      return true;
    } else {
        return false;
    }
  } else {
    pForm.submit();
    return true;
  }
}

