var dlgLogin;
var css = ".ux-auth-header-icon {background: url('../sistema/images/locked.gif') 0 4px no-repeat !important;}";
Ext.util.CSS.createStyleSheet(css, this._cssId); 

formPanel = new Ext.form.FormPanel({
	region: 'south',
	border: false,
	bodyStyle: "padding: 15px;",
	baseCls: 'x-plain',
	id: 'login-form',
	waitMsgTarget: true,    
	labelWidth: 80,
	labelAlign: 'right',
	height: 80,
	items: [{
		xtype: 'textfield',
		fieldLabel:'Usuário',
		id:'USU_Login',
		name:'login',
		anchor: '80%',
		allowBlank:false
	},
	{
		xtype: 'textfield',
		fieldLabel:'Senha',
		name:'senha',
		id:'USU_Senha',
		inputType:'password',
		anchor: '80%',
		allowBlank:false
	}]
});	

var ajax = {
	success:function(form,action){
		if(action.result.msg=="sistema")
			window.location = '../sistema/main.php';
		else
			window.location = '../sistema/alunos/index.php';
	},
	failure:function(form,action){
		Ext.MessageBox.show({
			title: 'Erro',
			msg: action.result.msg,
			buttons: Ext.MessageBox.OK,
			width: 500,
			icon: Ext.MessageBox.ERROR
		});
	}
};

function login(){
	window.location = 'http://www.somay2.com.br/login.php';
	/*if(!dlgLogin){
		dlgLogin = new Ext.Window({
			closable: false,
			resizable: false,
			draggable: true,
			modal: true,
		    iconCls: 'ux-auth-header-icon',
			title: 'Autenticação',
			width:270,
			height:160,
			plain: false,
			buttonAlign: 'center',
			//bodyStyle: 'padding:5px;',
			items: [formPanel],
			buttons: [{
				text: 'Entrar',
				handler: function(){
					entrar();
				}
			},{
				text: 'Fechar',
				handler: function(){ dlgLogin.hide();}	
			}],
			focus: function(){
				var map = new Ext.KeyMap("USU_Login", { 
					key: 13, // or 13
					fn: function(e){Ext.getCmp('USU_Senha').focus();}
				}); 	
				var map = new Ext.KeyMap("USU_Senha", { 
					key: 13, // or 13
					fn: function(e){entrar();}
				}); 	
	
				Ext.get('USU_Login').focus();
			}
		});
		dlgLogin.show('cabecalho');
	}	
	dlgLogin.on('hide',function(){
		window.location = 'index.php';
	});*/
};

function entrar(){
	if (Ext.getCmp('login-form').getForm().isValid()){
		var logar=true;
		if (IsNumeric(Ext.getCmp('USU_Login').getValue())){
			if (chkcpf(Ext.getCmp('USU_Login').getValue())){
				logar=true;
			} else{
				Ext.MessageBox.alert('Erro:', 'CPF Inválido, favor tentar novamente.');
				logar=false;
			}
		} else{
			logar=true;
		}
		if (logar){
			Ext.getCmp('login-form').getForm().url='http://www.somay2.com.br/url/login.php';
			Ext.getCmp('login-form').getForm().submit({
				waitTitle : 'Aguarde...',
				waitMsg:'Autenticando usuário...',
				reset:true,
				success: ajax.success,
				failure: ajax.failure
			});
		}
	} else {
		Ext.MessageBox.alert('Erro:', 'Preencha corretamente seus dados de login.');
	}		
};

function IsNumeric(strString) //  check for valid numeric strings	
{
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
};

function chkcpf(cpf){
	var digitos_iguais = 1;
	if( cpf.length != 11 ) return false;
	for( i = 0; i < cpf.length - 1; i++ ) if( cpf.charAt(i) != cpf.charAt(i + 1)) { digitos_iguais = 0; break; }
	if( !digitos_iguais )
	{
		var numeros = cpf.substring(0,9); var digitos = cpf.substring(9); var soma = 0;
		for( var i = 10; i > 1; i-- ) soma += numeros.charAt(10 - i) * i;
		var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if( resultado != digitos.charAt(0) ) return false;
		numeros = cpf.substring(0,10); soma = 0;
		for( var i = 11; i > 1; i-- ) soma += numeros.charAt(11 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if( resultado == digitos.charAt(1) ) return true;
	}
	return false;
};
