function QuickLoginForm(label,start,onLogin){
  this.LOGIN=0;
  this.CREATE=1;
  this.FORGOT=2;
  this.WAITING=3;
  
  this.email=el('quicklogin_email');
  this.password=el('quicklogin_passwd');
  this.container=el('quicklogin_box');
  
  this.onLogin=onLogin;
  
  this.activeElement=null;
  
  var instance=this;

  this.initialLoadFunction=null;
  if(start=="login"||PREFS.BLOCK_Create_New_Account||PREFS.BLOCK_Quick_Create_New_Account) this.initialLoadFunction=function(){instance.showLogin()};
  else if(start=="new") this.initialLoadFunction=function(){instance.showNewForm()};
  
  var params=new AjaxFormContent();
  params.addNameValue("DEBUG",0);
  if(PREFS.cXML_ID&&PREFS.cXML_ID>0) params.addNameValue('cXML_ID',PREFS.cXML_ID);    
  params.addNameValue("LABEL",label);
  params.addNameValue("mdl","quick_login/resource.aj");  
  ajaxPostFunction(params,function(sc,st,rt,rxml){    
       instance.res=new HTMLParse(rt);
	  if(!PREFS.BLOCK_Create_New_Account&&!PREFS.BLOCK_Quick_Create_New_Account){
       var required=instance.res.element[instance.CREATE].required;
       for(var i=0; i<required.length; i++){
          if(required[i].node.nodeName=="INPUT"){
             add_required_field("ACCOUNT", required[i].node, required[i].node.getAttribute('warning'), required[i].node.getAttribute('lengthmin'), required[i].node.getAttribute('mustcontain'));
          }else{
             add_required_select_field("ACCOUNT", required[i].node, required[i].node.getAttribute('warning'));
          }   
       }
	  }
       if(instance.initialLoadFunction) instance.initialLoadFunction();       
     },"ajax.iml");  
}

QuickLoginForm.prototype.showForm=function(start,onLogin){
  var instance=this;
  this.onLogin=onLogin;
  if(start=="login"||PREFS.BLOCK_Create_New_Account||PREFS.BLOCK_Quick_Create_New_Account) this.initialLoadFunction=function(){instance.showLogin()};
  else if(start=="new") this.initialLoadFunction=function(){instance.showNewForm()};
  if(this.res){
    this.initialLoadFunction();
  }
  refreshElements();
}


QuickLoginForm.prototype.setReturnFunction=function(func){
  var x=this.activeElement.node.getElementsByTagName('input');
  for(var i=0;i<x.length; i++){
    x[i].onkeypress=function(event){var evt=new Evt(event);if(evt.key==13){func();return false;} return true;};
  }
}


QuickLoginForm.prototype.showPopup=function(e){
  if(this.activeElement)this.container.removeChild(this.activeElement.node);
  this.activeElement=this.res.element[e];
  this.container.appendChild(this.activeElement.node);
}

QuickLoginForm.prototype.hidePopup=function(){
  if(this.activeElement) this.container.removeChild(this.activeElement.node);
}

QuickLoginForm.prototype.showLogin = function(){ 
  var instance=this;
  this.initialLoadFunction=function(){instance.showLogin();};
  this.showPopup(this.LOGIN);  
  this.activeElement.invalid[0].node.style["display"]="none";
  this.activeElement.passwordsent[0].node.style["display"]="none";
  this.activeElement.notfound[0].node.style["display"]="none";
  this.setReturnFunction(function(){instance.login();});
  //setMouseOver(this.activeElement.button[0].node,'ff');
//  this.activeElement.lastinput[0].node.onkeypress=function(){if(!document.all)kc=event.which;else kc=event.keyCode; if(kc==13)instance.login();return true;};
  this.activeElement.button[1].node.onclick=function(){instance.login();return false;};  // login  
  this.activeElement.button[2].node.onclick=function(){instance.showForgotPassword();return false;}; // forgot
  if(!PREFS.BLOCK_Create_New_Account&&!PREFS.BLOCK_Quick_Create_New_Account)  
     this.activeElement.button[0].node.onclick=function(){instance.showNewForm();return false;};  // new account

  this.activeElement.invalid[0].button[0].node.onclick=function(){instance.showForgotPassword();return false;}; // forgot
  this.activeElement.notfound[0].button[0].node.onclick=function(){instance.showNewForm();return false;}; // new account
  refreshElements();  
}

QuickLoginForm.prototype.showNewForm = function(){
  if(PREFS.BLOCK_Create_New_Account||PREFS.BLOCK_Quick_Create_New_Account) return;
  var instance=this;
  this.initialLoadFunction=function(){instance.showNewForm();};
  this.showPopup(this.CREATE);
  this.activeElement.invalid[0].node.style["display"]="none";
  this.setReturnFunction(function(){instance.createAccount();});
  //setMouseOver(this.activeElement.button[0].node,'ff');
  this.activeElement.invalid[0].button[0].node.onclick=function(){instance.showForgotPassword();return false;}; // forgot
  this.activeElement.button[0].node.onclick=function(){instance.showLogin();return false;}; // create
  this.activeElement.button[1].node.onclick=function(){instance.createAccount();return false;}; // create
  refreshElements();  
}

QuickLoginForm.prototype.showForgotPassword = function(){
  var instance=this;
  this.showPopup(this.FORGOT);
//  this.activeElement.notfound[0].style["display"]="none";
  this.setReturnFunction(function(){instance.sendPassword();});
//  this.activeElement.lastinput[0].node.onkeypress=function(){if(!document.all)kc=event.which;else kc=event.keyCode; if(kc==13)instance.sendPassword();return true;};
  this.activeElement.button[0].node.onclick=function(){instance.sendPassword();return false;};  // send password
  this.activeElement.button[1].node.onclick=function(){instance.showLogin();return false;};  // cancel (back to login)
  this.activeElement.button[2].node.onclick=function(){instance.showNewForm();return false;};  // cancel (back to login)
  refreshElements();
}

QuickLoginForm.prototype.sendPassword = function(){
  var instance=this;
  var params=new AjaxFormContent();
  params.fromNode(this.res.element[this.FORGOT].node);
  params.addNameValue("DEBUG",0);
  if(PREFS.cXML_ID&&PREFS.cXML_ID>0) params.addNameValue('cXML_ID',PREFS.cXML_ID);  
  params.addNameValue("mdl","quick_login/sendPassword.aj");  
  ajaxPostFunction(params,function(sc,st,rt,rxml){    
       if(getTagValue(rxml,'status')=="okay"){
         var email=getTagValue(rxml,'email');
         instance.showLogin();
         instance.activeElement.passwordsent[0].node.innerHTML="Great news! Your password is on the way! It was just sent to "+email+". Please check your e-mail, and then login.";
         instance.activeElement.passwordsent[0].node.style["display"]="";
       }else{
         instance.showLogin();
         instance.activeElement.notfound[0].node.style["display"]="";     
       }
     },"ajax.iml");    
}

QuickLoginForm.prototype.login = function(){
  var instance=this;
  var params=new AjaxFormContent();
  params.fromNode(this.res.element[this.LOGIN].node);
  params.addNameValue("DEBUG",0);
  if(PREFS.cXML_ID&&PREFS.cXML_ID>0) params.addNameValue('cXML_ID',PREFS.cXML_ID);    
  params.addNameValue("mdl","quick_login/login.aj");  
  ajaxPostFunction(params,function(sc,st,rt,rxml){    
       if(getTagValue(rxml,'status')=="okay"){
         instance.email.value=getTagValue(rxml,'email');
         instance.password.value=getTagValue(rxml,'password');
         // instance.message.innerHTML=getTagValue(rxml,'alert');
         instance.hidePopup();
         if(instance.onLogin) instance.onLogin();
       }else{
         instance.showLogin();
         instance.activeElement.invalid[0].node.style["display"]="";
       }
     },"ajax.iml");    
}

QuickLoginForm.prototype.createAccount = function(){
  if(PREFS.BLOCK_Create_New_Account||PREFS.BLOCK_Quick_Create_New_Account) return;
  if(!check_required_fields("ACCOUNT")) return;
  var instance=this;
  var params=new AjaxFormContent();
  params.fromNode(this.res.element[this.CREATE].node);
  params.addNameValue("DEBUG",0);
  if(PREFS.cXML_ID&&PREFS.cXML_ID>0) params.addNameValue('cXML_ID',PREFS.cXML_ID);    
  params.addNameValue("mdl","quick_login/create.aj");  
  ajaxPostFunction(params,function(sc,st,rt,rxml){    
       if(getTagValue(rxml,'status')=="okay"){
         instance.email.value=getTagValue(rxml,'email');
         instance.password.value=getTagValue(rxml,'password');
         // instance.message.innerHTML=getTagValue(rxml,'alert');
         instance.hidePopup();
         if(instance.onLogin) instance.onLogin();
       }else if(getTagValue(rxml,'status')=="waiting"){
         instance.showWaiting();
       }else{
         instance.showNewForm();
         instance.activeElement.invalid[0].node.style["display"]="";         
       }
     },"ajax.iml");    
}

QuickLoginForm.prototype.showWaiting = function(){
  var instance=this;
  this.showPopup(this.WAITING);
//  this.activeElement.notfound[0].style["display"]="none";
//  this.activeElement.lastinput[0].node.onkeypress=function(){if(!document.all)kc=event.which;else kc=event.keyCode; if(kc==13)instance.sendPassword();return true;};
  this.activeElement.button[0].node.onclick=function(){instance.showLogin();return false;};  // cancel (back to login)
  refreshElements();
}

