//Login Panel

LoginPanel = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapObjectId)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapObjectId);
};

LoginPanel.prototype = new GuiWidget();
LoginPanel.constructor = LoginPanel;
LoginPanel.superclass = GuiWidget.prototype;

LoginPanel.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapObjectId)
{
  LoginPanel.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "LoginPanel");

  this.id = newId;
  if (newMapObjectId!=null)
  {
    this.mapObject   = document.getGuiControl(newMapObjectId);
    this.mapObjectId = newMapObjectId;
  }
  else
  {
    this.mapObject = newMapObject;
    this.mapObjectId = this.mapObject.id;
  }

  
  this.loginForm  = new GuiWidget(this.id+"LoginForm", this.element, 0, 0, 4, this.width(), 125, true, "QueryPanel");
    this.loginCaptionElement = new GuiWidget(this.id+"loginCaptionElement", this.loginForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.loginCaptionElement.element.cbe.innerHtml("Log In To Your Profile");
    this.loginUsernameElement = new GuiWidget(this.id+"loginUsernameElement",this.loginForm.element, 0, 20, 0, this.width(), 40, true, "loginPanelForm");
    this.loginUsernameElement.element.cbe.innerHtml('Profile Name<br>'+
                                                    '<input type="text" id="'+this.id+'loginFormUsernameInputBox">'
                                                    );
    this.loginPasswordElement = new GuiWidget(this.id+"loginUsernameElement",this.loginForm.element, 0, 60, 0, this.width(), 40, true, "loginPanelForm");
    this.loginPasswordElement.element.cbe.innerHtml('Password<br>'+
                                                     '<input type="password" id="'+this.id+'loginFormPasswordInputBox" onKeyDown="document.getWidgetById(\''+this.id+'\').onKeyPress(this,\'login\',event);">'
                                                    );
    this.loginButtonElement = new GuiWidget(this.id+"loginButtonElement",this.loginForm.element, 0, 100, 0, this.width(), 25, true, "queryObjectBottom");
    this.loginButton = new ImageButton(this.id+"loginButton", this.loginButtonElement.element, 10, 0, 0, 50,20,true,'loginButton','');
    this.loginButton.loginPanel = this;
    this.loginButton.clickEvent = function(e)
    {
      this.loginPanel.runQuery('login'); 
    };

  this.loginErrorForm  = new GuiWidget(this.id+"LoginForm", this.element, 0, 125, 4, this.width(), 125, false, "QueryPanel");
    this.loginErrorCaptionElement = new GuiWidget(this.id+"loginCaptionElement", this.loginErrorForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.loginErrorCaptionElement.element.cbe.innerHtml("Error Logging In!");
    this.loginErrorTextElement = new GuiWidget(this.id+"loginErrorTextElement",this.loginErrorForm.element, 0, 20, 0, this.width(), 80, true, "loginError");
    this.loginErrorTextElement.element.cbe.innerHtml('Invalid Username<br> or Password.<br><br>Please try again.');

  this.logoutForm  = new GuiWidget(this.id+"LogoutForm", this.element, 0, 0, 2, this.width(), 65, false, "QueryPanel");
    this.logoutCaptionElement = new GuiWidget(this.id+"logoutCaptionElement", this.logoutForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.logoutCaptionElement.element.cbe.innerHtml("You are now logged in as:");
    this.logoutUsernameElement = new GuiWidget(this.id+"logoutUsernameElement",this.logoutForm.element, 0, 20, 0, this.width(), 40, true, "loginPanelForm");
    this.logoutUsernameElement.element.cbe.innerHtml('');
    this.logoutButtonElement = new GuiWidget(this.id+"logoutButtonElement",this.logoutForm.element, 0, 40, 0, this.width(), 25, true, "queryObjectBottom");
    this.logoutButton = new ImageButton(this.id+"logoutButton", this.logoutButtonElement.element, 10, 0, 0, 50,20,true,'logoutButton','');
    this.logoutButton.logoutPanel = this;
    this.logoutButton.clickEvent = function(e)
    {
      this.logoutPanel.runLogout(); 
    };

    this.changeUserForm  = new GuiWidget(this.id+"ChangeUserForm", this.element, 0, 65, 2, this.width(), 125, false, "QueryPanel");
    this.changeUserCaptionElement = new GuiWidget(this.id+"changeUserCaptionElement", this.changeUserForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.changeUserCaptionElement.element.cbe.innerHtml("Change to a Different User");
    this.changeUserUsernameElement = new GuiWidget(this.id+"changeUserUsernameElement",this.changeUserForm.element, 0, 20, 0, this.width(), 40, true, "loginPanelForm");
    this.changeUserUsernameElement.element.cbe.innerHtml('Username<br>'+
                                                    '<input type="text" id="'+this.id+'changeUserFormUsernameInputBox">'
                                                    );
    this.changeUserPasswordElement = new GuiWidget(this.id+"changeUserUsernameElement",this.changeUserForm.element, 0, 60, 0, this.width(), 40, true, "loginPanelForm");
    this.changeUserPasswordElement.element.cbe.innerHtml('Password<br>'+
                                                     '<input type="password" id="'+this.id+'changeUserFormPasswordInputBox" onKeyDown="document.getWidgetById(\''+this.id+'\').onKeyPress(this,\'changeUser\',event);">'
                                                    );
    this.changeUserButtonElement = new GuiWidget(this.id+"changeUserButtonElement",this.changeUserForm.element, 0, 100, 0, this.width(), 25, true, "queryObjectBottom");
    this.changeUserButton = new ImageButton(this.id+"changeUserButton", this.changeUserButtonElement.element, 10, 0, 0, 50,20,true,'loginButton','');
    this.changeUserButton.changeUserPanel = this;
    this.changeUserButton.clickEvent = function(e)
    {
      this.changeUserPanel.runQuery('changeUser'); 
    };
    
    this.changeUserErrorForm  = new GuiWidget(this.id+"changeUserForm", this.element, 0, 190, 2, this.width(), 125, false, "QueryPanel");
    this.changeUserErrorCaptionElement = new GuiWidget(this.id+"changeUserCaptionElement", this.changeUserErrorForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.changeUserErrorCaptionElement.element.cbe.innerHtml("Error Logging In!");
    this.changeUserErrorTextElement = new GuiWidget(this.id+"changeUserErrorTextElement",this.changeUserErrorForm.element, 0, 20, 0, this.width(), 80, true, "loginError");
    this.changeUserErrorTextElement.element.cbe.innerHtml('Invalid Username<br> or Password.<br><br>Please try again.');
    
  this.loginUsernameBox = document.getElementById(this.id+'loginFormUsernameInputBox');
  this.loginPasswordBox = document.getElementById(this.id+'loginFormPasswordInputBox');
  this.changeUserUsernameBox = document.getElementById(this.id+'changeUserFormUsernameInputBox');
  this.changeUserPasswordBox = document.getElementById(this.id+'changeUserFormPasswordInputBox');
};

LoginPanel.prototype.runQuery = function(mode)
{
  this.queryObject = this.mapObject.config.loginQuery;

  var request = new Array();
  switch(mode)
  {
    case 'login':
      request[0] = new Array('username',this.loginUsernameBox.value.toUpperCase()); //username field
      request[1] = new Array('password',this.loginPasswordBox.value); //password field
      break;
    case 'changeUser':
      request[0] = new Array('username',this.changeUserUsernameBox.value.toUpperCase()); //username field
      request[1] = new Array('password',this.changeUserPasswordBox.value); //password field
      break;
  }
  var data = makeASyncPostRequest(this,mode,XMLRPC_URL,this.queryObject.request.requestMethod,this.queryObject.request.pdqIdentifier,request);
};

LoginPanel.prototype.runLogout = function()
{
  this.logoutUsernameElement.element.cbe.innerHtml('');
  this.loginForm.show();
  this.loginErrorForm.hide();
  this.logoutForm.hide();
  this.changeUserForm.hide();
  this.changeUserErrorForm.hide();
  this.loginUsernameBox.value = '';
  this.loginPasswordBox.value = '';
  this.changeUserUsernameBox.value = '';
  this.changeUserPasswordBox.value = '';
  this.mapObject.initialize("");
};

/************************************************/

LoginPanel.prototype.onKeyPress = function(source,mode,evt)
{
  //check for enter key and run query if pressed.
  if (!evt)
    evt = window.evt; 
  else
    if (!evt.keyCode)
      evt.keyCode = evt.which;
  if (evt.keyCode == 13)
  {
    this.runQuery(mode);
    source.blur();
  }
};

/************************************************/

LoginPanel.prototype.callback = function(serverReplyDoc, requestType)
{
  /* begin error checks */
  if (serverReplyDoc == null)
  {
    return (null);
  }
  if (serverReplyDoc.xml == '') 
  {
    return (null);
  }
  if (serverReplyDoc.documentElement.nodeName == 'parsererror')
  {
    return (null);
  }
  
  var clientReply = new XMLRPCResponse();
  //clientReply.setResponseByStr(serverReplyDoc.xml);
  clientReply.setResponseByDoc(serverReplyDoc);

  if (clientReply.isFault())
  {
    var data = null;
    if (clientReply.getFaultCode()!=2005)  //suppress fault for 'not found'
      alert(this.id+'.callback:  clientReply fault:\n\n'+clientReply.getFaultCode()+'\n'+clientReply.getFaultString());
  }
  else
    var sqlData = clientReply.getObject();

  if ((sqlData == null) || (sqlData == false))
  {
    if (requestType == 'login')
      this.loginErrorForm.show();  
    if (requestType == 'changeUser')
      this.changeUserErrorForm.show();  
  }
  else
  {
    this.logoutUsernameElement.element.cbe.innerHtml(sqlData[0]['username']);
    this.loginForm.hide();
    this.loginErrorForm.hide();
    this.logoutForm.show();
    this.changeUserForm.show();
    this.changeUserErrorForm.hide();
    this.loginUsernameBox.value = '';
    this.loginPasswordBox.value = '';
    this.changeUserUsernameBox.value = '';
    this.changeUserPasswordBox.value = '';
    this.mapObject.initialize(hex_md5(sqlData[0]['username']+document.getGuiValue("ApplicationConfig").id));
  }
};

/*************************************************/

LoginPanel.prototype.resize = function(newWidth, newHeight)
{
  this.element.cbe.resizeTo(newWidth, this.height());  
  this.loginForm.resize(newWidth,this.loginForm.height());
  this.loginCaptionElement.resize(newWidth,this.loginCaptionElement.height());
  this.loginUsernameElement.resize(newWidth,this.loginUsernameElement.height());
  this.loginPasswordElement.resize(newWidth,this.loginPasswordElement.height());
  this.loginButtonElement.resize(newWidth,this.loginButtonElement.height());
  this.loginErrorForm.resize(newWidth,this.loginErrorForm.height());
  this.loginErrorCaptionElement.resize(newWidth,this.loginErrorCaptionElement.height());
  this.loginErrorTextElement.resize(newWidth,this.loginErrorTextElement.height());
  this.logoutForm.resize(newWidth,this.logoutForm.height());
  this.logoutCaptionElement.resize(newWidth,this.logoutCaptionElement.height());
  this.logoutUsernameElement.resize(newWidth,this.logoutUsernameElement.height());
  this.logoutButtonElement.resize(newWidth,this.logoutButtonElement.height());
  this.changeUserForm.resize(newWidth,this.changeUserForm.height());
  this.changeUserCaptionElement.resize(newWidth,this.changeUserCaptionElement.height());
  this.changeUserUsernameElement.resize(newWidth,this.changeUserUsernameElement.height());
  this.changeUserPasswordElement.resize(newWidth,this.changeUserPasswordElement.height());
  this.changeUserButtonElement.resize(newWidth,this.changeUserButtonElement.height());
  this.changeUserErrorForm.resize(newWidth,this.changeUserErrorForm.height());
  this.changeUserErrorCaptionElement.resize(newWidth,this.changeUserErrorCaptionElement.height());
  this.changeUserErrorTextElement.resize(newWidth,this.changeUserErrorTextElement.height());
};


