/**
 * WORK@ATJ - Add new person - step 1
 *
 * @author Marcin Rzewucki
 * @copyright (c) 2009, Marcin Rzewucki
 * @date 
 * @version 
 *
 * @license The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

Ext.onReady(function(){
    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';
    var bd = Ext.getBody();
    bd.createChild({tag: 'h2', html: 'Krok 1 z 7 <small>(pola oznaczone "*" są wymagane)</small>'});
    bd.createChild({tag: 'hr'});
    bd.createChild({tag: 'br'});
    bd.createChild({html: '<div id="fpanel" class="demo-ct" style="margin-bottom:15px;"></div>'});

  var sources4 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/dzielnice.pl'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
	  },['name','id'])});

  var combo4 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Dzielnica*',
    hiddenName:'dzielnica',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    editable: false,
    allowBlank: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });

  var sources5 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/polozenia.pl'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
      },['name','id'])});

  var combo5 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	},
	'select': function(combo,e){
	  var id = this.getValue();
	  if (id == 0){
	    Ext.MessageBox.show({
	      title: 'Nowe położenie',
	      msg: 'Podaj nazwę:',
	      width: 300,
	      buttons: Ext.MessageBox.OKCANCEL,
	      prompt: true,
	      multiline: false,
	      fn: function(btn,text){
		  if (btn == 'ok'){
		    Ext.Ajax.request({
		      url: '/cgi-bin/polozenia.pl',
		      params: {
			add: 1,
			nazwa: text
		      },
		      failure:function(response,options){
			  Ext.MessageBox.alert('Problem z połączeniem do bazy','Spróbuj odświeżyć stronę');
		      },
		      success:function(response,options){
			  var resp = Ext.decode(response.responseText);
			  if (resp.success == false){
			    Ext.MessageBox.alert('Błąd',resp.msg);
			  } else{
			    if (resp.id !== ''){
			      combo.lastQuery = null;
			      combo.setValue("");
			      Ext.MessageBox.alert('Informacja','Dane zapisane w bazie.');
			    }
			  }
			}
		      });
		  }
		}
	      });
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Położenie',
    hiddenName:'polozenie',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    editable: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });

  var sources6 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/wyksztalcenie.pl'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
      },['name','id'])});

  var combo6 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	},
	'select': function(combo,e){
	  var id = this.getValue();
	  if (id == 0){
	    Ext.MessageBox.show({
	      title: 'Nowe wykształcenie',
	      msg: 'Podaj nazwę:',
	      width: 300,
	      buttons: Ext.MessageBox.OKCANCEL,
	      prompt: true,
	      multiline: false,
	      fn: function(btn,text){
		  if (btn == 'ok'){
		    Ext.Ajax.request({
		      url: '/cgi-bin/wyksztalcenie.pl',
		      params: {
			add: 1,
			nazwa: text
		      },
		      failure:function(response,options){
			  Ext.MessageBox.alert('Problem z połączeniem do bazy','Spróbuj odświeżyć stronę');
		      },
		      success:function(response,options){
			  var resp = Ext.decode(response.responseText);
			  if (resp.success == false){
			    Ext.MessageBox.alert('Błąd',resp.msg);
			  } else{
			    if (resp.id !== ''){
			      combo.lastQuery = null;
			      combo.setValue("");
			      Ext.MessageBox.alert('Informacja','Dane zapisane w bazie.');
			    }
			  }
			}
		      });
		  }
		}
	      });
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Wykształcenie*',
    hiddenName:'wyksztalcenie',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    editable: false,
    allowBlank: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });


  var sources7 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/zawody.pl'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
      },['name','id'])});

  var combo7 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	},
	'select': function(combo,e){
	  var id = this.getValue();
	  if (id == 0){
	    Ext.MessageBox.show({
	      title: 'Nowy zawód',
	      msg: 'Podaj nazwę:',
	      width: 300,
	      buttons: Ext.MessageBox.OKCANCEL,
	      prompt: true,
	      multiline: false,
	      fn: function(btn,text){
		  if (btn == 'ok'){
		    Ext.Ajax.request({
		      url: '/cgi-bin/zawody.pl',
		      params: {
			add: 1,
			nazwa: text
		      },
		      failure:function(response,options){
			  Ext.MessageBox.alert('Problem z połączeniem do bazy','Spróbuj odświeżyć stronę');
		      },
		      success:function(response,options){
			  var resp = Ext.decode(response.responseText);
			  if (resp.success == false){
			    Ext.MessageBox.alert('Błąd',resp.msg);
			  } else{
			    if (resp.id !== ''){
			      combo.lastQuery = null;
			      combo.setValue("");
			      Ext.MessageBox.alert('Informacja','Dane zapisane w bazie.');
			    }
			  }
			}
		      });
		  }
		}
	      });
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Zawód wyuczony',
    hiddenName:'zawod',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    editable: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });

  var sources8 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/dzialy.pl'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
      },['name','id'])});

  var combo8 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	},
	'select': function(combo,e){
	  var id = this.getValue();

	  if (id == 0){
	    Ext.MessageBox.show({
	      title: 'Nowy dział',
	      msg: 'Podaj nazwę:',
	      width: 300,
	      buttons: Ext.MessageBox.OKCANCEL,
	      prompt: true,
	      multiline: false,
	      fn: function(btn,text){
		  if (btn == 'ok'){
		    Ext.Ajax.request({
		      url: '/cgi-bin/dzialy.pl',
		      params: {
			add: 1,
			nazwa: text
		      },
		      failure:function(response,options){
			  Ext.MessageBox.alert('Problem z połączeniem do bazy','Spróbuj odświeżyć stronę');
		      },
		      success:function(response,options){
			  var resp = Ext.decode(response.responseText);
			  if (resp.success == false){
			    Ext.MessageBox.alert('Błąd',resp.msg);
			  } else{
			    if (resp.id !== ''){
			      combo.lastQuery = null;
			      combo.setValue("");
			      Ext.MessageBox.alert('Informacja','Dane zapisane w bazie.');
			    }
			  }
			}
		      });
		  }
		}
	      });
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Dział',
    hiddenName:'dzial',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    editable: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });

  var sources9 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/stanowiska.pl?bd=1'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
      },['name','id'])});

  var combo9 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Stanowisko*',
    hiddenName:'stanowisko',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    editable: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });

  var sources10 = new Ext.data.Store({
    proxy: new Ext.data.ScriptTagProxy({
      url:'/cgi-bin/wymiar_pracy.pl'
	  }),
    loadMask:true,
    reader: new Ext.data.JsonReader({
      root: 'data',
      totalProperty: 'total',
      id: 'id'
      },['name','id'])});

  var combo10 = new Ext.form.ComboBox({
    enableKeyEvents: true,
    width: 250,
    listeners: {
	'keypress': function(combo,e){
	  var value = String.fromCharCode(e.getCharCode());
	  var start = typeof combo.selectedIndex != 'undefined' ? combo.selectedIndex + 1 : 0;
	  if (start >= combo.store.getCount()){
	    start = 0;
	  }
	  var index = combo.store.find(combo.displayField, value, start);
	  if(index >= 0){
	    combo.select(index, true);
	  }
	},
	'select': function(combo,e){
	  var id = this.getValue();

	  if (id == 0){
	    Ext.MessageBox.show({
	      title: 'Nowy wymiar pracy',
	      msg: 'Podaj nazwę:',
	      width: 300,
	      buttons: Ext.MessageBox.OKCANCEL,
	      prompt: true,
	      multiline: false,
	      fn: function(btn,text){
		  if (btn == 'ok'){
		    Ext.Ajax.request({
		      url: '/cgi-bin/wymiar_pracy.pl',
		      params: {
			add: 1,
			nazwa: text
		      },
		      failure:function(response,options){
			  Ext.MessageBox.alert('Problem z połączeniem do bazy','Spróbuj odświeżyć stronę');
		      },
		      success:function(response,options){
			  var resp = Ext.decode(response.responseText);
			  if (resp.success == false){
			    Ext.MessageBox.alert('Błąd',resp.msg);
			  } else{
			    if (resp.id !== ''){
			      combo.lastQuery = null;
			      combo.setValue("");
			      Ext.MessageBox.alert('Informacja','Dane zapisane w bazie.');
			    }
			  }
			}
		      });
		  }
		}
	      });
	  }
	}
    },
    loadMask:true,
    loadingText: 'Wczytuję...',
    fieldLabel: 'Wymiar pracy*',
    hiddenName:'wymiar_pracy',
    displayField:'name',
    typeAhead: true,
    lazyRender:true,
    allowBlank:false,
    editable: false,
    forceSelection:true,
    triggerAction: 'all',
    selectOnFocus:true,
    valueField: 'id'
  });

  var fp = new Ext.FormPanel({
    id: 'osoba-form',
    title: 'Dane osobowe i oczekiwania',
    bodyStyle:'padding:5px 5px 0 5px;',
    loadMask:true,
    labelWidth: 130,
    labelStyle: 'font-weight:bold;',
    width: 460,
    autoShow: true,
    autoScroll: true,
    renderTo: 'fpanel',
    defaultType: 'textfield',
	items: [{
         fieldLabel: 'Płeć*',
	  xtype: 'radiogroup',
	  allowBlank:false,
	  items: [
		  {boxLabel: 'Kobieta',name: 'plec',inputValue: 'K'},
		  {boxLabel: 'Mężczyzna',name: 'plec',inputValue: 'M'}
		 ]
	 },{
         fieldLabel: 'Imię*',
	  name: 'imie',
	  maxLength: 40,
	  allowBlank:false
	 },{
         fieldLabel: 'Nazwisko*',
          name: 'nazwisko',
	  maxLength: 100,
	  width: 200,
          allowBlank:false
	 },combo4,combo5,{
         fieldLabel: 'Ulica*',
	  name: 'ulica',
	  maxLength: 255,
	  allowBlank: false,
	  width: 300
	 },{
         fieldLabel: 'Kod pocztowy',
	  width: 60,
	  name: 'kod_pocztowy',
	  maxLength: 6
	 },{
         fieldLabel: 'Telefon*',
	  name: 'tel',
	  allowBlank: false,
	  maskRe: /[0-9]/,
	  maxLength: 50
	 },{
         fieldLabel: 'Email*',
	  name: 'email',
	  maxLength: 100,
	  width: 300,
          allowBlank:false
	 },{
         fieldLabel: 'WWW',
	  name: 'www',
	  width: 300,
	  maxLength: 150
	 },{
         fieldLabel: 'Data urodzenia*',
          name: 'd_urodzenia',
          xtype: 'datefield',
          format: 'Y/m/d',
	  minValue: '1930/01/01',
          allowBlank: false,
          maxLength: 10
	 },combo6,combo7,{
         fieldLabel: 'Staż pracy',
	  width: 30,
	  name: 'staz',
	  maskRe: /[0-9]/,
	  regex: /^[1-9]/,
	  maxLength: 2
	       },{
		xtype: 'box',
	       autoEl: {tag: 'div', html: '<small><b>Oczekiwania</b></small><br><hr>'}
	 },combo8,combo9,{
         fieldLabel: 'Staż pracy na ubieganym stanowisku',
	  width: 30,
	  name: 'staz_stanowisko',
	  maskRe: /[0-9]/,
	  regex: /^[1-9]/,
	  maxLength: 2
	       },combo10,{
         fieldLabel: 'Podjęcie pracy od',
          name: 'start_pracy',
          xtype: 'datefield',
	  minValue: new Date(),
          format: 'Y/m/d',
          maxLength: 10
		      },{
		xtype: 'numberfield',
		decimalPrecision: 2,
		allowPureDecimal: true,
		fieldLabel: 'Minimalna płaca (netto)',
		width: 60,
		maxLength: 9,
		regex: /^[1-9]/,
		name: 'min_placa'
		    },{
		fieldLabel: 'służbowy samochód',
		boxLabel: 'tak',
		name: 'sam',
		xtype: 'checkbox'
		    },{
		fieldLabel: 'służbowa komórka',
		boxLabel: 'tak',
		name: 'kom',
		xtype: 'checkbox'
		    },{
		fieldLabel: 'służbowy laptop',
		boxLabel: 'tak',
		name: 'lap',
		xtype: 'checkbox'
		}
	  ],
         buttons: [{
          text: 'Wyczyść',
	  handler: function(){
	  fp.getForm().reset();
	}
      },{
          text: 'Zapisz',
	  handler: function(){
	  if (fp.getForm().isValid()){
	    fp.getForm().submit({
	      url: '/cgi-bin/zapis.pl',
	      waitMsg: 'Zapisuję dane... proszę czekać...',
	      success: function(fp,options){
		  document.location.href = '/praca/Web/krok2.html?id='+options.result.id;
	      },
	      failure: function(fp,options){
		  Ext.MessageBox.alert('Błąd',options.result.msg);
	      }
	    });
	  } else{
	    Ext.MessageBox.alert('Błąd','Wymagane pola formularza nie są wypełnione');
	  }
         }
      }]
  });

  combo4.store = sources4;
  combo4.store.load();
  combo5.store = sources5;
  combo5.store.load();
  combo6.store = sources6;
  combo6.store.load();
  combo7.store = sources7;
  combo7.store.load();
  combo8.store = sources8;
  combo8.store.load();
  combo9.store = sources9;
  combo9.store.load();
  combo10.store = sources10;
  combo10.store.load();
});
