var janela_contratar;

function contratar() {

	store_cidades = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: './cidades.php',
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'dados',
			fields: ['cidade']
		}),
		listeners: {scope:this,loadexception:function(proxy,o,arg,e){
			janela_mensagem('conexao');
		}}
	});
	store_cidades.on('load',function(){
		combo_cidades.reset();
	},this);

	var combo_cidades = new Ext.form.ComboBox({
		typeAhead: true,
		triggerAction: 'all',
		fieldLabel: 'Cidade',
		forceSelection: true,
		anchor: '100%',
		allowBlank: false,
		mode: 'local',
		name: 'cidade',
		store: store_cidades,
		valueField: 'cidade',
		displayField: 'cidade'
	});

	Ext.form.JsonErrorReader = function() {   
		Ext.form.JsonErrorReader.superclass.constructor.call(this, {   
			root: 'dados'
		},[   
			{name: 'id'},   
			{name: 'msg'}   
		]);   
	};   

	Ext.extend(Ext.form.JsonErrorReader, Ext.data.JsonReader);

	 var formulario = new Ext.FormPanel({
		buttonAlign: 'left',
		url: './contratar.php',
		method: 'POST',
		bodyStyle: 'padding:5px;',
		errorReader: new Ext.form.JsonErrorReader(),
		frame: true,
		items: [{
			xtype:'fieldset',
			title: 'Dados',
			collapsible: true,
			autoHeight:true,
			items: [{
				layout:'column',
				items:[{
					labelWidth: 100,
					columnWidth:.5,
					layout: 'form',
					items: [{
						xtype:'textfield',
						fieldLabel: 'Nome',
						name: 'nome',
						maxLength: '100',
						allowBlank: false,
						anchor:'95%'
					}, {
						xtype:'textfield',
						fieldLabel: 'Endereço',
						maxLength: '100',
						allowBlank: false,
						name: 'endereco',
						anchor:'95%'
					}, {
						xtype:'textfield',
						fieldLabel: 'Número',
						maxLength: '8',
						name: 'numero',
						width: 50
					}, {
						xtype:'textfield',
						fieldLabel: 'Complemento',
						name: 'complemento',
						maxLength: '50',
						anchor:'95%'
					}, {
						xtype:'textfield',
						fieldLabel: 'Bairro',
						allowBlank: false,
						maxLength: '100',
						name: 'bairro',
						anchor:'95%'
					},{
						xtype:'textfield',
						fieldLabel: 'CEP',
						name: 'cep',
						allowBlank: false,
						plugins: [new Ext.ux.InputTextMask('99999-999', true)],
						width: 80
					}]
				},{
					labelWidth: 90,
					columnWidth:.5,
					layout: 'form',
					items: [{
						xtype: 'combo',
						fieldLabel: 'Estado',
						name: 'estado',
						anchor: '100%',
						allowBlank: false,
						forceSelection: true,
						hiddenName: 'estado',
						store: [
							['AC','Acre'],['AL','Alagoas'],['AP','Amapá'],['AM','Amazonas'],['BA','Bahia'],['CE','Ceará'],['DF','Distrito Federal'],['ES','Espírito Santo'],['GO','Goiás'],['MA','Maranhão'],['MT','Mato Grosso'],['MS','Mato Grosso do Sul'],['MG','Minas Gerais'],['PA','Pará'],['PB','Paraíba'],['PR','Paraná'],['PE','Pernambuco'],['PI','Piauí'],['RJ','Rio de Janeiro'],['RN','Rio Grande do Norte'],['RS','Rio Grande do Sul'],['RO','Rondônia'],['RR','Roraima'],['SC','Santa Catarina'],['SP','São Paulo'],['SE','Sergipe'],['TO','Tocantins']
						],
						triggerAction: 'all',
						listeners: {select:{fn:function(combo, value) {
							combo_cidades.emptyText = "Carregando...";
							combo_cidades.reset();
							store_cidades.load( { params: { uf: combo.getValue() } } );
						}}}
					},combo_cidades,{
						xtype: 'combo',
						fieldLabel: 'Fuso horário',
						name: 'timezone',
						allowBlank: false,
						forceSelection: true,
						triggerAction: 'all',
						anchor:'100%',
						store: [
							['America/Araguaina','America/Araguaina'],['America/Bahia','America/Bahia'],['America/Belem','America/Belem'],['America/Boa_Vista','America/Boa_Vista'],['America/Campo_Grande','America/Campo_Grande'],['America/Cuiaba','America/Cuiaba'],['America/Eirunepe','America/Eirunepe'],['America/Fortaleza','America/Fortaleza'],['America/Maceio','America/Maceio'],['America/Manaus','America/Manaus'],['America/Noronha','America/Noronha'],['America/Porto_Velho','America/Porto_Velho'],['America/Recife','America/Recife'],['America/Rio_Branco','America/Rio_Branco'],['America/Sao_Paulo','America/Sao_Paulo']
						]
					}, {
						xtype:'textfield',
						fieldLabel: 'Telefone',
						maxLength: '35',
						allowBlank: false,
						name: 'telefone',
						anchor:'100%'
					}, {
						xtype:'textfield',
						fieldLabel: 'CNPJ',
						vtype:'cnpj',
						plugins: [new Ext.ux.InputTextMask('99.999.999/9999-99', true)],
						name: 'cnpj',
						anchor:'100%'
					}, {
						xtype:'textfield',
						fieldLabel: 'IE',
						maxLength: '20',
						name: 'ie',
						anchor:'100%'
					}]
				}]
			}]
		},{
			xtype:'fieldset',
			title: 'Configurações',
			collapsible: true,
			autoHeight:true,
			items: [{
				layout:'column',
				items:[{
					labelWidth: 70,
					columnWidth:.5,
					layout: 'form',
					items: [{
						xtype:'textfield',
						fieldLabel: 'Domínio',
						name: 'dominio',
						maxLength: '50',
						vtype:'dominio',
						allowBlank: false,
						anchor:'95%'
					}, {
						xtype: 'combo',
						fieldLabel: 'Plano',
						name: 'plano',
						allowBlank: false,
						forceSelection: true,
						triggerAction: 'all',
						anchor:'95%',
						hiddenName: 'plano',
						store: [
							['100','100 Clientes - R$ 50,00'],
							['150','150 Clientes - R$ 75,00'],
							['200','200 Clientes - R$ 100,00'],
							['250','250 Clientes - R$ 125,00'],
							['300','300 Clientes - R$ 150,00'],
							['350','350 Clientes - R$ 175,00'],
							['400','400 Clientes - R$ 200,00'],
							['450','450 Clientes - R$ 225,00'],
							['500','500 Clientes - R$ 250,00'],
							['550','550 Clientes - R$ 275,00'],
							['ilimitado','Ilimitado - R$ 300,00']         
						]
					}]
				},{
					labelWidth: 90,
					columnWidth:.5,
					layout: 'form',
					items: [{
						xtype: 'combo',
						fieldLabel: 'Vencimento',
						name: 'vencimento',
						allowBlank: false,
						forceSelection: true,
						triggerAction: 'all',
						width: 100,
						store: [
							['01','01'],['02','02'],['03','03'],['04','04'],['05','05'],['06','06'],['07','07'],['08','08'],['09','09'],['10','10'],['11','11'],['12','12'],['13','13'],['14','14'],['15','15'],['16','16'],['17','17'],['18','18'],['19','19'],['20','20'],['21','21'],['22','22'],['23','23'],['24','24'],['25','25'],['26','26'],['27','27'],['28','28'],['30','30'],['31','31']
						]
					}]
				}]
			}]
		},{
			xtype:'fieldset',
			title: 'Informações do contato',
			collapsible: true,
			autoHeight:true,
			items: [{
				layout:'column',
				items:[{
					labelWidth: 70,
					columnWidth:.5,
					layout: 'form',
					items: [{
						xtype: 'combo',
						fieldLabel: 'Tipo',
						name: 'tipo',
						anchor: '95%',
						allowBlank: false,
						forceSelection: true,
						hiddenName: 'tipo',
						store: [['1','Sócio/Proprietário'],['2','Técnico'],['3','Outros']],
						triggerAction: 'all'
					},{
						xtype:'textfield',
						fieldLabel: 'Nome',
						maxLength: '100',
						allowBlank: false,
						name: 'contato_nome',
						anchor:'95%'
					}, {
						xtype:'textfield',
						allowBlank: false,
						fieldLabel: 'Telefone',
						maxLength: '100',
						name: 'contato_telefone',
						anchor:'95%'
					}]
				},{
					labelWidth: 50,
					columnWidth:.5,
					layout: 'form',
					items: [{
						xtype:'textfield',
						fieldLabel: 'E-mail',
						allowBlank: false,
						maxLength: '100',
						vtype:'email',
						name: 'contato_email',
						anchor:'100%'
					}, {
						xtype:'textfield',
						allowBlank: false,
						fieldLabel: 'CPF',
						vtype:'cpf',
						name: 'contato_cpf',
						plugins: [new Ext.ux.InputTextMask('999.999.999-99', true)],
						anchor:'100%'
					}, {
						xtype:'textfield',
						fieldLabel: 'RG',
						maxLength: '20',
						allowBlank: false,
						name: 'contato_rg',
						anchor:'100%'
					}]
				}]
			}]
		}],
		buttons: [{
			text: 'OK',
			handler: function(){
				if(formulario.getForm().isValid()){
					formulario.getForm().submit({
						waitTitle: 'Contratar sistema',
						waitMsg: 'Executando solicitação, aguarde...',
						success: function(formulario,action){
							var retorno = Ext.util.JSON.decode(action.response.responseText);
							store_cidades.load();
							combo_cidades.reset();
							formulario.reset();
							janela_contratar.hide();
							var janela_informacao = new Ext.Window({
								title: 'Contratar sistema',
								bodyStyle: 'padding: 5px;',
								border: false,
								modal: true,
								closable: true,
								maximizable: false,
								resizable: false,
								width: 650,
								plain: true,
								html: retorno.mensagem
							});
							janela_informacao.show();
							janela_informacao.center();
							janela_mensagem('concluido');
						},
						failure: function(formulario,action) {
							if(action.failureType=='connect') {
								janela_mensagem('conexao');
							} else {
								if (Ext.util.JSON.decode(action.response.responseText).total=='0') {
									janela_mensagem('banco');
								} else {
									janela_mensagem('formulario');
								}
							}
						}
					});
				} else {
					janela_mensagem('formulario');
				}
			}
		},{
			text: 'Cancelar',
			handler: function(){
				janela_contratar.hide();
			}
		}]
	});

	janela_contratar = new Ext.Window({
		title: 'Contratar sistema',
		border: false,
		closable: true,
		modal: true,
		maximizable: false,
		resizable: false,
		width: 700,
		plain: true,
		items: [formulario]
	});

	var janela = new Ext.Window({
		title: 'Contrato',
		border: true,
		closable: true,
		constrain: true,
		modal: true,
		autoScroll: true,
		bodyStyle: 'padding: 5px',
		maximizable: false,
		resizable: false,
		width: 650,
		height: 350,
		autoLoad: {url: 'contrato.html', nocache: true},
		bbar: [{		
			text: 'Aceito',
			iconCls:'aceitar',
			handler: function() {
				janela.close();
				janela_contratar.show();
			}
		},'-',{		
			text: 'Não aceito',
			iconCls:'nao-aceitar',
			handler: function() {
				janela.close();
			}
		}]
	});

	janela.show();
	
}