/* Copyright 2006 por Connecty Networks. Todos os direitos reservados */
function Auth() { this.init(); this.cfg = { hasIcon: true, hasSizeBtn: false, hasCloseBtn: true, hasRefreshBtn: false, hasSettingsBtn: false, hasDrag: false, hasOnCloseConfirm: false, isOpenHidden: false, isSystem: true, title: "Perfil de utilizador", module: "Auth"
}
this.user = { id: null, email: null, password: null}
this.domContent = [ { tag: "div", className: "menu_panel", style: {textAlign: "center"}, innerHTML: "<h1>Login/Registo</h1>"}, { tag: "div", className: "menu_panel", style: {textAlign: "center"}, innerHTML: "<b>O seu e-mail</b><br>", childs: [{ tag: "input", type: "text", size: "50", id: "selectEmail" }]
}, { tag: "div", className: "menu_panel", style: {textAlign: "center"}, innerHTML: "<b>A sua Palavra-Chave</b><br>", childs: [{ tag: "input", type: "password", size: "50", id: "selectPassword" }]
}, { tag: "div", className: "menu_panel", style: {textAlign: "center"}, childs: [{ tag: "input", type: "button", id: "submitButton", value: " Enviar ", events: {onclick: "registerUser()"}}]
}, { tag: "div", className: "menu_panel", style: {textAlign: "center"}, id: "messages", style: {paddingTop: "10px", height: "50px"}}
]
this.getSignature = function() { return SYSWIDGET_WKEY+2;}
this.onBuildInterface = function() { this.buildDomModel(this.elements.content, this.domContent); this.buildDomModel(desktop.elements.authButton, [{ tag: "span", id:"signinBtn", childs: [createButtonDom("Login/Registo", "signIn()", "widgets/auth/img/sign_in.gif")] }, { tag: "span", id:"signoutBtn", childs: [createButtonDom("Sair", "logout()", "widgets/auth/img/sign_out.gif")] }]); if(this.isLogged()) { hideEl(this.elements.signinBtn);} else { hideEl(this.elements.signoutBtn);}
}
this.close = function() { desktop.closeCurrentPage();}
this.signIn = function() { desktop.showPage('auth');}
this.isLogged = function() { return this.user.email != '';}
this.logout = function() { request.send({act: "logout"}, this);}
this.loadUser = function() { this.anonymusId = getCookie("anonymusid"); this.user.id = getCookie("userid"); this.user.email = getCookie("useremail"); this.user.password = getCookie("userpassword");}
this.saveUser = function() { setCookie("userid", this.user.id); setCookie("useremail", this.user.email); setCookie("userpassword", this.user.password);}
this.start = function() { this.loadUser(); if(this.user.email && this.user.password) { request.send({act: "start_user", user_id: this.user.id, email: this.user.email, password: this.user.password}, this);} else { request.send({act: "start_anonymus", user_id: this.user.id}, this);}
}
this.dispatchMsg = function(msg) { switch(msg.status) { case 'start_result':
this.user = msg.user; this.saveUser(); this.onLoad(); break; case 'register_new':
this.user = msg.user; this.elements.messages.innerHTML = "<b>Obrigado pelo seu registo!</b>"; showEl(this.elements.submitButton); break; case 'user_ok':
if(!this.user.email) { setCookie("anonymusid", this.user.id);}
this.user = msg.user; this.saveUser(); location.reload(); break; case 'wrong_password':
this.elements.messages.innerHTML = "<b>Palavra-Chave incorrecta para o endere&ccedil;o "+msg.email+"</b>"; showEl(this.elements.submitButton); break; case 'logout_ok':
var anonymusId = getCookie("anonymusid"); if(anonymusId) { this.user = {id: anonymusId, email: "", password: ""}; this.saveUser();} else { delCookie("userid"); delCookie("useremail"); delCookie("userpassword");}
location.reload(); break;}
}
this.onLoad = function() {}
this.registerUser = function() { var errors = []; email = trim(this.elements.selectEmail.value); password = trim(this.elements.selectPassword.value); if(!this.checkEmail(email)) { errors[errors.length] = "<b>E-mail inv&aacute;lido.</b>";}
if(!this.checkPassword(password)) { errors[errors.length]= "<b>Palavra-Chave inv&aacute;lida.</b> A sua palavra-chave requer um m&iacute;nimo de 6 caracteres.";}
if(errors.length > 0) { this.elements.messages.innerHTML = errors.join("<BR>");} else { hideEl(this.elements.submitButton); this.elements.messages.innerHTML = "A processar pedido..."; request.send({act: "register_user", email: email, password: password}, this);}
}
this.checkEmail = function(str) { return /^[a-zA-Z][a-z0-9\-\_]{1,20}@[a-z0-9\-\_\.]{5,30}$/.test(str);}
this.checkPassword = function(str) { return (str.length > 5);}
}
Auth.prototype = new Widget();