/*
 * Vroomtrap
 * Copyright(c) 2008, Nubesoft, LLC.
 */
 Ext.namespace('Ext.addtrapwindow');
 Ext.addtrapwindow.addtrapwindow = new Ext.Window({
       title    : 'Add Trap',
       closable : true,
       closeAction: 'hide',
       draggable: false,
       resizable: false,
       width    : 484,
       height   : 390,
       plain    : true,
       layout   : 'border',
       items    : [
       new Ext.FormPanel({
           region: 'center',
           labelWidth: 100, // label settings here cascade unless overridden
           id: 'addTrapFormPanel',
           monitorValid:true,
           frame:false,
           labelAlign: 'right',
           buttonAlign: 'right',
           margins     : '0 0 0 0',
           cmargins    : '0 0 0 0',
           bodyStyle:'padding:4px 4px 4px 4px',
           items: [{
               xtype:'fieldset',
               title: 'Required Information',
               autoHeight:true,
               collapsed: false,
               items: [{
                   xtype:"combo",
                   fieldLabel: '<font color="red"><b>*</b></font>Unit Type',
                   id: 'addTrapUnitType',
                   emptyText:'Select a unit type...',
                   store: new Ext.data.JsonStore({
                        root: 'unit',
                        fields:['v', 'd'],
                        data: Trap.constants.unit
                    }),
                   mode: 'local',
                   displayField: 'd',
                   valueField: 'v', 
                   triggerAction: 'all',
                   forceSelection:true,
                   editable:false,
                   selectOnFocus:false,
                   allowBlank:false
               }, {
                   xtype:"combo",
                   fieldLabel: '<font color="red"><b>*</b></font>Time',
                   id: 'addTrapTime',
                   emptyText:'Select a time...',
                   store: new Ext.data.JsonStore({
                         root: 'hour',
                         fields:['v', 'd'],
                         data: Trap.constants.tm12
                     }),
                   mode: 'local',
                   displayField:'d',
                   valueField:'v',
                   triggerAction: 'all',
                   forceSelection:true,
                   editable:false,
                   selectOnFocus:true,
                   allowBlank:false
               },{
                  xtype:'hidden',
                  id: 'addTrapLat',
                  value: 0
               },{ 
                 xtype:'hidden',
                 id: 'addTrapLng',
                 value: 0
               },{ 
                 xtype:'hidden',
                 id: 'addTrapUser'   
               }]
           },{
               xtype:'fieldset',
               title: 'Optional Information',
               autoHeight:true,
               collapsed: false,
               items: [{
                   xtype:"combo",
                   fieldLabel: 'Technology',
                   id: 'addTrapTech',
                   emptyText:'Select a technology...',
                   store: new Ext.data.JsonStore({
                         root: 'tech',
                         fields:['v', 'd'],
                         data: Trap.constants.tech
                     }),
                   mode: 'local',
                   displayField: 'd',
                   valueField: 'v', 
                   triggerAction: 'all',
                   forceSelection:true,
                   editable:false,
                   selectOnFocus:true
               }, {
                 
                    layout: 'column',
                    items:[{
                      width: 200,
                      layout: 'form',
                      items :[{
                        xtype: 'textfield',
                        fieldLabel: 'Your Speed',
                        id: 'addTrapUSpeed',
                        regex: /^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-9][0-9])$/,
                        regexText: 'Must be an number between 0 and 299',
                        msgTarget: 'title',
                        anchor: "99%"
                      }]
                    },{
                      labelWidth: 1, 
                      width: 79,
                      layout: 'form',
                      items :[{             
                        xtype:"combo",
                        id: 'addTrapUSpeedUnits',
                        value: 'mph',
                        store: new Ext.data.JsonStore({
                               root: 'sput',
                               fields:['v', 'd'],
                               data: Trap.constants.sput
                           }),
                        mode: 'local',
                        displayField: 'd',
                        valueField: 'v', 
                        triggerAction: 'all',
                        forceSelection:true,
                        editable:false,
                        selectOnFocus:true,                     
                        anchor: "100%"
                      }]
                    }]
                 }, {
                   layout: 'column',
                   items:[{
                     width: 200,
                     layout: 'form',
                     items :[{
                       xtype: 'textfield',
                       fieldLabel: 'Speed Limit',
                       id: 'addTrapSLimit',
                       regex: /^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-9][0-9])$/,
                       regexText: 'Must be an number between 0 and 299',
                       msgTarget: 'title',
                       anchor: "99%"
                     }]
                   },{
                     labelWidth: 1, 
                     width: 79,
                     layout: 'form',
                     items :[{             
                       xtype:"combo",
                       id: 'addTrapLSpeedUnits',
                       value: 'mph',
                       store: new Ext.data.JsonStore({
                              root: 'sput',
                              fields:['v', 'd'],
                              data: Trap.constants.sput
                          }),
                       mode: 'local',
                       displayField: 'd',
                       valueField: 'v', 
                       triggerAction: 'all',
                       forceSelection:true,
                       editable:false,
                       selectOnFocus:true,                     
                       anchor: "100%"
                     }]
                   }]
               }, {
                   xtype:"combo",
                   fieldLabel: 'Action',
                   id: 'addTrapAction',
                   emptyText:'Select an action...',
                   store: new Ext.data.JsonStore({
                          root: 'actn',
                          fields:['v', 'd'],
                          data: Trap.constants.actn
                      }),
                   mode: 'local',
                   displayField: 'd',
                   valueField: 'v', 
                   triggerAction: 'all',
                   forceSelection:true,
                   editable:false,
                   selectOnFocus:true
               }]            
           }],        
           buttons: [{
               text: 'Close',
               handler  : function(){
                 Ext.addtrapwindow.addtrapwindow.hide();
           }
           },{
               text: 'Add Trap',
               formBind:true,
                handler  : function(){
                  Ext.addtrapwindow.addtrapwindow.hide();
                  Ext.Ajax.request({
                  	url : 'addtrap' , 
                  	params : { devi : 'ws',
                  	           user : Ext.getCmp('addTrapUser').getValue(),
                  	           lat  : Ext.getCmp('addTrapLat').getValue(),
                  	           lng  : Ext.getCmp('addTrapLng').getValue(),
                  	           unit : Ext.getCmp('addTrapUnitType').getValue(),
                  	           hour : Ext.getCmp('addTrapTime').getValue(),
                  	           gmto : new Date().getTimezoneOffset()/-60,
                  	           tech : Ext.getCmp('addTrapTech').getValue(),
                  	           uspe : Ext.getCmp('addTrapUSpeed').getValue(),
                  	           uspu : Ext.getCmp('addTrapUSpeedUnits').getValue(),
                  	           spel : Ext.getCmp('addTrapSLimit').getValue(),
                  	           splu : Ext.getCmp('addTrapLSpeedUnits').getValue(),
                  	           actn : Ext.getCmp('addTrapAction').getValue()
                  	           },
                  	method: 'POST',
                  	success: function ( result, request ) { 
                  	  message = result.responseText.split(":")
                  	  Ext.popup.msg(message[0], message[1]);
                  	  nubesoft.google.Vroomtrap.loadMarkers();
                  	},
                  	failure: function ( result, request) { 
                  	  Ext.addtrapwindow.addtrapwindow.hide();
                  		Ext.MessageBox.alert('Failed', result.responseText); 
                  	} 
                  });
               }
           }]
       }), 
       new Ext.Panel({
           region      : 'south',
           closeAction: 'hide',
           split       : false,
           width       : 468,
           height      : 60,
           collapsible : false,
           margins     : '0 0 0 0',
           cmargins    : '0 0 0 0',
           bodyStyle:'padding:0px 0px 0px 0px',
           html        : '<iframe width="468px" height="60px" align="left" scrolling="no" frameborder="0" src="/ads/adblock468.html"/>'
       })
       ]
   });