SMS = {
	OBJ : null,
	Validator : null,
	PageState : 0,
	Load : function( obj ){
		SMS.OBJ = obj;
		SMS.PageState = 0;
		SMS.LoadValidator();
    },
    Submit : function(){
		if( ! SMS.Validator.exec() ){ return false; }
		
		var nome = document.getElementById("FrmSMSNome");
		var telefone = document.getElementById("FrmSMSTelefone");
		
		if (SMS.PageState > 0) {
			return false;
		}
		SMS.PageState++;
		
		SMS.ViewLoading(true);
		SMS.OBJ.onSalvarSmsError = function() {
			SMS.ViewLoading(false);
			SMS.PageState--;
			window.alert('Falha na comunicação... Tente novamente daqui alguns minutos...');
		}
		SMS.OBJ.salvarsms( encode64(nome.value), encode64(telefone.value), function(rs){
			if(rs){
				window.alert("Você acaba de se cadastrar para receber Torpedos SMS... \n\nEm breve receberá novidades... \n\nObrigado(a)");
				document.getElementById("frmSMS").reset();
			}else{
				window.alert("Houve algum problema ao se cadastrar, por favor, tente daqui alguns minutos!");
			}
			SMS.PageState--;
			SMS.ViewLoading(false);
		});	
		return false;
    },
	Remove : function(){
		if( ! SMS.Validator.exec() ){ return false; }
		
		var telefone = document.getElementById("FrmSMSTelefone");
		
		if (SMS.PageState > 0) {
			return false;
		}
		SMS.PageState++;
		
		SMS.ViewLoading(true);
		SMS.OBJ.onRemoverSmsError = function() {
			SMS.ViewLoading(false);
			SMS.PageState--;
			window.alert('Falha na comunicação... Tente novamente daqui alguns minutos...');
		}
		SMS.OBJ.removersms( encode64(telefone.value), function(rs){
			if(rs){
				window.alert("Seu telefone foi removido/bloqueado da lista com sucesso.");
				document.getElementById("frmSMS").reset();
			}else{
				window.alert("Houve algum problema ao remover seu cadastro, por favor, tente daqui alguns minutos!");
			}
			SMS.PageState--;
			SMS.ViewLoading(false);
		});	
		return false;
    },
	LoadValidator : function(){
		var a_fields = {
		'FrmSMSNome':{'l':'Nome','r':true},
		'FrmSMSTelefone':{'l':'Telefone','r':true}
		},
		o_config = {
			'to_disable' : ['FrmSMSAdd'],
			'alert' : 1
		}
		SMS.Validator = new validator('frmSMS', a_fields, o_config);
    },
	ViewLoading : function( status ){
		var icoLoading = document.getElementById("smsCarregando");
		if(status == true){
			document.body.style.cursor = 'wait';
			icoLoading.style.visibility = 'visible';
		}else{
			document.body.style.cursor = '';
			icoLoading.style.visibility = 'hidden';
		}
    }
}
