// Copyright (C) TGMT Systems Inc. 2008 - All Rights Reserved

function LoginLayout()
{this.TableLayout();this.m_debugName="loginLayout";this.m_showRememberPassword=true;this.m_showForgotPassword=true;this.m_showForgotLogin=true;this.m_openNewWindow=false;this.m_redirect=true;this.m_loginCallBack=null;this.m_strAccountName="";this.m_keydown=true;}
copyPrototype(LoginLayout,TableLayout);LoginLayout.prototype.createLayout=function()
{var objLabel;this.m_accountRow=-1;this.m_usernameRow=-1;this.m_passwordRow=-1;this.m_emailRow=-1;this.m_rememberPwdRow=-1;this.m_btnsRow=-1;this.m_forgotPasswordVisible=false;var accountName=this.m_strAccountName;var username="";var password="";var rememberPwd="0";var cookiePrefix="";if(APP_NAME)
{cookiePrefix+=APP_NAME.replace(/ /g,"_");}
if(accountName=="")
{var savedAccount=HTTP_Utils.getCookie(cookiePrefix+"coreAccount");if(savedAccount!=null&&savedAccount.length>0)
{rememberPwd="1";accountName=savedAccount;}}
var query_vars=HTTP_Utils.read_querystring();if(query_vars["account_name"]!=undefined)
{accountName=query_vars["account_name"];username="";if(query_vars["username"]!=undefined)
{username=query_vars["username"];}
password="";if(query_vars["password"]!=undefined)
{password=query_vars["password"];}}
if(accountName==""&&window.location.href.indexOf("logout=true")==-1)
{var requestURL=location.href;var pos1=requestURL.indexOf("//")+2;var pos2=requestURL.indexOf(".",pos1);var pos3=requestURL.indexOf(".",pos2+1);var pos4=requestURL.indexOf("/",pos1);if(pos3>-1&&pos3<pos4&&pos4>pos2)
{accountName=requestURL.substring(pos1,pos2);}}
objLabel=new DocumentFieldLabel(getResource(LOGIN_ACCOUNT),getResource(LOGIN_ACCOUNT_DESC),true);objLabel.setMargin(0,0,2,0);objLabel.setPadding(0,0,2,0);this.m_accountName=new TextFieldCtrl(accountName);this.m_accountName.setMargin(2,0,2,0);this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,objLabel,DOCUMENT_FIELD_CONTROL_LABEL_WIDTH);this.addCell(this.getRowCount()-1,this.m_accountName,"200");this.addCell(this.getRowCount()-1,new Panel(),TABLE_LAYOUT_REMAINDER);this.m_accountRow=this.getRowCount()-1;objLabel=new DocumentFieldLabel(getResource(LOGIN_USERNAME),getResource(LOGIN_USERNAME_DESC),true);objLabel.setMargin(0,0,2,0);objLabel.setPadding(0,0,2,0);this.m_username=new TextFieldCtrl(username);this.m_username.setMargin(2,0,2,0);this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,objLabel,DOCUMENT_FIELD_CONTROL_LABEL_WIDTH);this.addCell(this.getRowCount()-1,this.m_username,"200");this.addCell(this.getRowCount()-1,new Panel(),TABLE_LAYOUT_REMAINDER);this.m_usernameRow=this.getRowCount()-1;objLabel=new DocumentFieldLabel(getResource(LOGIN_PASSWORD),getResource(LOGIN_PASSWORD_DESC),true);objLabel.setMargin(0,0,2,0);objLabel.setPadding(0,0,2,0);this.m_password=new Password(password);this.m_password.setMargin(2,0,2,0);this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,objLabel,DOCUMENT_FIELD_CONTROL_LABEL_WIDTH);this.addCell(this.getRowCount()-1,this.m_password,"200");this.addCell(this.getRowCount()-1,new Panel(),TABLE_LAYOUT_REMAINDER);this.m_passwordRow=this.getRowCount()-1;objLabel=new DocumentFieldLabel(getResource(LOGIN_EMAIL),getResource(LOGIN_EMAIL_DESC),false);objLabel.setMargin(0,0,2,0);objLabel.setPadding(0,0,2,0);this.m_email=new TextFieldCtrl("");this.m_email.setMargin(2,0,2,0);this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,objLabel,DOCUMENT_FIELD_CONTROL_LABEL_WIDTH);this.addCell(this.getRowCount()-1,this.m_email,"200");this.addCell(this.getRowCount()-1,new Panel(),TABLE_LAYOUT_REMAINDER);this.m_emailRow=this.getRowCount()-1;this.hideRow(this.m_emailRow);if(this.m_showRememberPassword)
{this.m_rememberPassword=new Checkbox(rememberPwd);this.m_rememberPassword.setMargin(2,0,2,0);objLabel=new DocumentFieldLabel(getResource(LOGIN_REMEMBER_PWD),getResource(LOGIN_REMEMBER_PWD_DESC),false);objLabel.setMargin(0,0,2,0);objLabel.setPadding(0,0,2,0);if(objLabel.getCaption().length>17)
{objLabel.setHeight(36);}
this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,objLabel,DOCUMENT_FIELD_CONTROL_LABEL_WIDTH);this.addCell(this.getRowCount()-1,this.m_rememberPassword,"200");this.addCell(this.getRowCount()-1,new Panel(),TABLE_LAYOUT_REMAINDER);this.m_rememberPwdRow=this.getRowCount()-1;}
if(this.m_showForgotLogin||this.m_showForgotPassword)
{if(this.m_showForgotLogin)
{this.m_loginBtn=new Button(getResource(LOGIN_LOGIN),getResource(LOGIN_LOGIN_BTN_DESC),null);this.m_loginBtn.addListener(this);this.m_loginBtn.setMargin(0,0,5,0);this.m_loginBtn.setWidth(20+this.m_loginBtn.getCaption().length*6.8);}
if(this.m_showForgotPassword)
{this.m_forgotPwdBtn=new Button(getResource(LOGIN_FORGOT_PWD_BTN),getResource(LOGIN_FORGOT_PWD_BTN_DESC),null);this.m_forgotPwdBtn.addListener(this);this.m_forgotPwdBtn.setMargin(2,0,5,0);this.m_forgotPwdBtn.setWidth(20+this.m_forgotPwdBtn.getCaption().length*6.8);}
this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,new Panel(),"50%");if(this.m_showForgotLogin)
{this.addCell(this.getRowCount()-1,this.m_loginBtn,this.m_loginBtn.getWidth());}
if(this.m_showForgotPassword)
{this.addCell(this.getRowCount()-1,this.m_forgotPwdBtn,this.m_forgotPwdBtn.getWidth());}
this.addCell(this.getRowCount()-1,new Panel(),"50%");this.m_btnsRow=this.getRowCount()-1;}
this.addRow(TABLE_LAYOUT_AUTO);this.addCell(this.getRowCount()-1,new Label(getResource(LOGIN_SUCCESS)),TABLE_LAYOUT_REMAINDER);this.hideRow(this.getRowCount()-1);}
LoginLayout.prototype.onCreate=function()
{this.createLayout();TableLayout.prototype.onCreate.apply(this,arguments);if(this.m_keydown)
{EventUtils_attachEvent(this.getHTMLContainer(),"keydown",this.onkeydown.bind(this));}
var queryStringVals=HTTP_Utils.read_querystring();if(queryStringVals["auto"]!=undefined)
{this.onButtonClick(this.m_loginBtn);}
if(SKIN_FIELD_MANDATORY_BG_COLOR!=null)
{setInputControlsBackgroundColor(this.getHTMLContainer(),SKIN_FIELD_MANDATORY_BG_COLOR);}
focusFirstInputControl(this.getHTMLContainer());}
LoginLayout.prototype.onButtonClick=function(objBtn)
{if(objBtn==this.m_loginBtn)
{this.onLogin();}
else if(objBtn==this.m_forgotPwdBtn)
{this.onForgotPassword();}}
LoginLayout.prototype.onkeydown=function(event)
{if(event.keyCode==13)
{var htmlObj=getEventSource(event);htmlObj.blur();this.onLogin();}
return true;}
LoginLayout.prototype.onLogin=function()
{if(this.m_forgotPasswordVisible)
{this.showRow(this.m_usernameRow);this.showRow(this.m_passwordRow);if(this.m_showRememberPassword)
{this.showRow(this.m_rememberPwdRow);}
this.hideRow(this.m_emailRow);this.m_loginBtn.setCaption(getResource(LOGIN_LOGIN),getResource(LOGIN_LOGIN_BTN_DESC),null);this.m_forgotPwdBtn.setCaption(getResource(LOGIN_FORGOT_PWD_BTN),getResource(LOGIN_FORGOT_PWD_BTN_DESC),null);this.m_forgotPasswordVisible=false;return;}
var account=trim(this.m_accountName.getValue());var username=trim(this.m_username.getValue());var password=trim(this.m_password.getValue());if(this.m_showRememberPassword)
{var cookiePrefix="";if(APP_NAME)
{cookiePrefix+=APP_NAME.replace(/ /g,"_");}
var date=new Date();date.setFullYear(date.getFullYear()+1);if(this.m_rememberPassword.getValue()==1)
{HTTP_Utils.setCookie(cookiePrefix+"coreAccount",account,date,"/");}
else
{HTTP_Utils.setCookie(cookiePrefix+"coreAccount"," ",date,"/");}}
var request=new AjaxRequest(APP_URL+"/login.do");if(this.m_showRememberPassword&&this.m_rememberPassword.getValue()=="1")
{request.addParam("remember","1");}
request.addParam("action","login");request.addParam("username",username);request.addParam("password",password);request.addParam("account_name",account);request.addParam("langCode",USER_LANGUAGE);request.postRequest(this.onLogin_Response.bind(this));}
LoginLayout.prototype.onForgotPassword=function()
{if(!this.m_forgotPasswordVisible)
{this.hideRow(this.m_usernameRow);this.hideRow(this.m_passwordRow);if(this.m_showRememberPassword)
{this.hideRow(this.m_rememberPwdRow);}
this.showRow(this.m_emailRow);this.m_forgotPwdBtn.setCaption(getResource(LOGIN_RESET_PWD_BTN),getResource(LOGIN_RESET_PWD_BTN_DESC),null);this.m_loginBtn.setCaption(getResource(LOGIN_RESET_CANCEL_BTN),"",null);this.m_forgotPasswordVisible=true;}
else
{var account=this.m_accountName.getValue();var email=this.m_email.getValue();var request=new AjaxRequest(APP_URL+"/login.do");request.addParam("action","forgotPassword");request.addParam("email",email);request.addParam("account_name",account);request.postRequest(this.onForgotPassword_Response.bind(this));}}
LoginLayout.prototype.onForgotPassword_Response=function(response)
{alert(getResource(LOGIN_PASSWORD_SENT));this.showRow(this.m_usernameRow);this.showRow(this.m_passwordRow);if(this.m_showRememberPassword)
{this.showRow(this.m_rememberPwdRow);}
this.hideRow(this.m_emailRow);this.m_loginBtn.setCaption(getResource(LOGIN_LOGIN),getResource(LOGIN_LOGIN_BTN_DESC),null);this.m_forgotPwdBtn.setCaption(getResource(LOGIN_FORGOT_PWD_BTN),getResource(LOGIN_FORGOT_PWD_BTN_DESC),null);this.m_forgotPasswordVisible=false;}
LoginLayout.prototype.onLogin_Response=function(response)
{if(!this.m_redirect)
{this.notifyListeners("onLoginSuccess",null);return;}
this.onLoginComplete();}
LoginLayout.prototype.onLoginComplete=function()
{if(!this.m_openNewWindow||window.location.href.indexOf("noWin")>-1)
{top.location.href=APPLICATION_URL;}
else
{this.m_applicationWin=window.open(APPLICATION_URL,"appWindow","width=1000,height=750,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,copyhistory=no");if(!this.m_applicationWin)
{alert("You must disable your popup blocker in order to login.");return;}
for(var i=0;i<this.getRowCount()-1;i++)
{this.hideRow(i);}
this.showRow(this.getRowCount()-1);setTimeout(this.checkWindowClosed.bind(this),250);}}
LoginLayout.prototype.checkWindowClosed=function()
{if(this.m_applicationWin.closed)
{this.hideRow(this.getRowCount()-1);this.showRow(this.m_accountRow);this.showRow(this.m_usernameRow);this.showRow(this.m_passwordRow);if(this.m_showRememberPassword)
{this.showRow(this.m_rememberPwdRow);}
this.showRow(this.m_btnsRow);}
else
{setTimeout(this.checkWindowClosed.bind(this),250);}}
LoginLayout.prototype.setShowRememberPassword=function(val)
{this.m_showRememberPassword=val;}
LoginLayout.prototype.setOpenNewWindow=function(val)
{this.m_openNewWindow=val;}
LoginLayout.prototype.setRedirect=function(val)
{this.m_redirect=val;}
function LoginApplication()
{this.CoreApplication();this.m_accountApp=false;this.m_loginTabLayout=null;this.m_showRememberPassword=true;this.m_openNewWindow=false;this.m_saveLastTab=false;}
copyPrototype(LoginApplication,CoreApplication);LoginApplication.prototype.initializeUI=function(response)
{this.main_panel.setBackgroundColor("white");this.createLoginTab();this.m_loginTab=new Tab(getResource(LOGIN_LOGIN),getResource(LOGIN_LOGIN),null);this.main_panel.addTab(this.m_loginTab,this.m_loginTabLayout);CoreApplication.prototype.initializeUI.apply(this,arguments);}
LoginApplication.prototype.createLoginTab=function()
{this.m_loginTabLayout=new ScrollPanel();this.m_loginLayout=new LoginLayout();this.m_loginLayout.setPadding(3,3,3,3);this.m_loginLayout.setShowRememberPassword(this.m_showRememberPassword);this.m_loginLayout.setOpenNewWindow(this.m_openNewWindow);this.m_loginTabLayout.setPanel(this.m_loginLayout);}
LoginApplication.prototype.setShowRememberPassword=function(val)
{this.m_showRememberPassword=val;}
LoginApplication.prototype.setOpenNewWindow=function(val)
{this.m_openNewWindow=val;}
