//-----------------------------------
//
//     Source: index.js
//   Revision: 0.1.0
//     Author: Hiroki Uchida
//       Date: 2010.07.26
//     Locker:
//      State:
//  Copyright: (C) goalway 2010
//
//-----------------------------------
var area       = Ext.form.ComboBox;
var elementary = Ext.form.ComboBox;
var junior     = Ext.form.ComboBox;
var elementary_store  = Ext.data.JsonStore;
var junior_store      = Ext.data.JsonStore;
var type       = Ext.form.Checkbox;
var edit_dialog  = Ext.Window;
var edit_dialog2 = Ext.Window;

Ext.onReady(function(){

      fObj = document.MyForm;

      type = new Ext.form.Checkbox({
                   applyTo: "type0",
                   boxLabel: "一戸建て"
      });
      type = new Ext.form.Checkbox({
                   applyTo: "type1",
                   boxLabel: "中古マンション"
      });
      type = new Ext.form.Checkbox({
                   applyTo: "type2",
                   boxLabel: "土地"
      });
      type = new Ext.form.Checkbox({
                   applyTo: "type4",
                   boxLabel: "投資物件"
      });
      type = new Ext.form.Checkbox({
                   applyTo: "type3",
                   boxLabel: "事業用"
      });

      area = new Ext.form.ComboBox({
               typeAhead: true,
               editable:true,
               triggerAction: 'all',
               transform:'area',
               forceSelection:true,
               width:180,
               emptyText: "地域を選択",
               resizable:true,
               listWidth:220
      });
        area.addListener("select", function(queryEvent) { 
                junior.clearValue();
                elementary.clearValue();
                return false; 
        });

      junior_store = new Ext.data.JsonStore({
            root: "junior", 
            fields: ["name"],
            url: "combo.php"
      });

      junior = new Ext.form.ComboBox({
               name: "junior",
               id: "junior",
               transform:'junior',
               allowBlank: true,
               store: junior_store,
               displayField: "name", 
               valueField: "name",
               editable: true, 
               typeAhead: true, 
               forceSelection: false,
               triggerAction: "all",
               selectOnFocus: true ,
               emptyText: "中学校を選択",
               width:120,
               resizable:true,
               listWidth:160
      });
      junior.addListener("beforeQuery", function(queryEvent) { 
               var area_value = fObj.area.value;
               if (area_value.length == 0) { 
               } else {
                   junior_store.load({params: {mode: 'm_junior', area: area_value}}); 
               } 
               return false; 
      });

      elementary_store = new Ext.data.JsonStore({
            root: "elementary", 
            fields: ["name"],
            url: "combo.php"
      });

      elementary = new Ext.form.ComboBox({
               name: "elementary",
               id: "elementary",
               transform:'elementary',
               allowBlank: true,
               store: elementary_store,
               displayField: "name", 
               valueField: "name",
               editable: true, 
               typeAhead: true, 
               forceSelection: false,
               triggerAction: "all",
               selectOnFocus: true ,
               emptyText: "小学校を選択",
               width:120,
               resizable:true,
               listWidth:160
      });
      elementary.addListener("beforeQuery", function(queryEvent) { 
               var area_value = fObj.area.value;
               if (area_value.length == 0) { 
               } else {
                   elementary_store.load({params: {mode: 'm_elementary', area: area_value}}); 
               } 
               return false; 
      });

      edit_dialog = new Ext.Window({
              title    : '物件詳細',
              modal: true,
              shadow: true,
              el:'edit-dlg',
              layout:'fit',
              width:800,
              height:490,
              resizable: true,
              constrain: true,
              closeAction:'hide',
              items:new Ext.TabPanel({
                  applyTo        : 'menu-tabs',
                  autoTabs       : false,
                  activeTab      : 0,
                  deferredRender : false,
                  border         : false
              }),
              buttons: [
                 {
                      text: '印刷画面を出力する',
                      minWidth: 120,
                      handler: function() {
                          print_create();
                      }
                 },
                 {
                      text: '閉じる',
                      minWidth: 120,
                      handler: function() {
                          close_dialog();
                      }
                 }
              ],
              keys: [{
                      key: Ext.EventObject.ESC,
                      handler: function() {
                          close_dialog();
                      }
              }]
      });

      edit_dialog2 = new Ext.Window({
              title    : '物件詳細',
              modal: true,
              shadow: true,
              el:'edit-dlg2',
              layout:'fit',
              width:800,
              height:540,
              resizable: true,
              constrain: true,
              closeAction:'hide',
              items:new Ext.TabPanel({
                  applyTo        : 'menu-tabs2',
                  autoTabs       : false,
                  activeTab      : 0,
                  deferredRender : false,
                  border         : false
              }),
              buttons: [
                 {
                      text: '印刷する',
                      minWidth: 120,
                      handler: function() {
                          print_iframe();
                      }
                 },
                 {
                      text: '閉じる',
                      minWidth: 120,
                      handler: function() {
                          close_dialog2();
                      }
                 }
              ],
              keys: [{
                      key: Ext.EventObject.ESC,
                      handler: function() {
                          close_dialog2();
                      }
              }]
      });

});
//------------------------------------
//
// Close Dialog
//
//------------------------------------
function close_dialog(){
       edit_dialog.hide();
       ds2.reload();
};
function close_dialog2(){
       edit_dialog2.hide();
};
//------------------------------------
//
// Search
//
//------------------------------------
function search(){
       fObj = document.MyForm;
       var url = "";

       value  = encodeURIComponent(fObj.area.value);
       url = url + "area=" + value + "&";

       value  = encodeURIComponent(fObj.elementary.value);
       url = url + "elementary=" + value + "&";

       value  = encodeURIComponent(fObj.junior.value);
       url = url + "junior=" + value + "&";

       var str_type = "";
       var cnt = "0";
       for( j=0;j < fObj.elements["type"].length; j++){ 
           if(fObj.elements["type"][j].checked){
              if(cnt == "0"){
                  str_type = fObj.elements["type"][j].value;
              }
              else{
                  str_type = str_type + "," + fObj.elements["type"][j].value;
              }
              cnt++;
           }
    
       }
       value  = encodeURIComponent(str_type);
       url = url + "type=" + value + "&";

       location.href = "search/list.php?" + url;
};
function close_dialog(){
       edit_dialog.hide();
};
function close_dialog2(){
       edit_dialog2.hide();
};
function detail(bukken_no){
       fObj = document.MyForm;

       document.getElementById('frame_window').innerHTML = "<iframe src='init.html' name='nm_prev' id='id_prev' width='100%' height='505' scrolling='no' style='border:none;'></iframe>";

       detail_url = "search/detail.php?bukken_no=" + bukken_no;
       document.getElementById('id_prev').src = detail_url;
       edit_dialog.setTitle("物件詳細");
       edit_dialog.setSize(880,585);
       edit_dialog.show();
}
