/**
 *Handle the register overlays - forms, opening and so on
 */
 
//=============================================================================>
//Define
function NotLoggedInOverlayRegister(){
  
  this.couponCode = false;
  this.email      = false;
}
//##############################################################################
/**
 *Instance variable
 */
NotLoggedInOverlayRegister.instance = new NotLoggedInOverlayRegister();
//##############################################################################
/**
 *Wire the events up
 */
NotLoggedInOverlayRegister.prototype.addOnLoad = function(){
  
  //Add the openers
  var openers = dojo.query( '.NotLoggedInOverlayRegister_Opener' );
  
  dojo.forEach(
    openers,
    function(entry, i){
      
      dojo.connect( entry,
                    'onclick',
                    function(/*Event*/ evnt){
                      dojo.stopEvent(evnt);
                      NotLoggedInOverlayRegister.instance.open();
                    }
                   );
    }
   );
  
  //Wire in the dialog form handlers
  dojo.connect( dojo.byId('NotLoggedIn_Dialog_Coupon_Form'      ), 'onsubmit', NotLoggedInOverlayRegister.instance.form_coupon   );
  dojo.connect( dojo.byId('NotLoggedIn_Dialog_Login_Form'       ), 'onsubmit', NotLoggedInOverlayRegister.instance.form_login    );
  dojo.connect( dojo.byId('NotLoggedIn_Dialog_Registration_Form'), 'onsubmit', NotLoggedInOverlayRegister.instance.form_register );
  dojo.connect( dojo.byId('NotLoggedIn_Dialog_Verify_Form'      ), 'onsubmit', NotLoggedInOverlayRegister.instance.form_verify   );
  
  //Resend email link
  dojo.connect( dojo.byId('NotLoggedIn_Dialog_Verify_P_SPAN_resend_email'),
                'onclick',
                NotLoggedInOverlayRegister.instance.resend_reg_email
               );
};
//##############################################################################
/**
 *Close all dialogs
 */
NotLoggedInOverlayRegister.prototype.close = function(){
  
  if( dijit.byId('NotLoggedIn_Dialog_Coupon').attr('open') )
    dijit.byId('NotLoggedIn_Dialog_Coupon').hide();
  
  if( dijit.byId('NotLoggedIn_Dialog_Login').attr('open') )
    dijit.byId('NotLoggedIn_Dialog_Login').hide();
  
  if( dijit.byId('NotLoggedIn_Dialog_Registration').attr('open') )
    dijit.byId('NotLoggedIn_Dialog_Registration').hide();
  
  if( dijit.byId('NotLoggedIn_Dialog_Verify').attr('open') )
    dijit.byId('NotLoggedIn_Dialog_Verify').hide();
};
//##############################################################################
/**
 *Process the coupon form
 */
NotLoggedInOverlayRegister.prototype.form_coupon = function( /*Event*/ evnt ){
  
  dojo.stopEvent(evnt);
  
  var args = {
    'url':    CapsoolGlobalVariables.AppRoot + '/api/register/coupon',
    'form':     dojo.byId('NotLoggedIn_Dialog_Coupon_Form'),
    'handleAs': 'json',
    'load': function(data){
              
              if( data.status == 1 ){
                NotLoggedInOverlayRegister.instance.couponCode = data.couponCode;
                NotLoggedInOverlayRegister.instance.open('NotLoggedIn_Dialog_Registration');
              }else{
                window.alert( data.message );
                NotLoggedInOverlayRegister.instance.open( 'NotLoggedIn_Dialog_Coupon' );
              }
            },
    'error': function(error){
             }
  };
  
  this.couponCode = args.form.code;
  
  var deferred = dojo.xhrPost( args );
};
//##############################################################################
/**
 *Process the login form
 */
NotLoggedInOverlayRegister.prototype.form_login = function( /*Event*/ evnt ){
  
  //Massage the form data
  var bounce   = window.location.href = CapsoolGlobalVariables.AppRoot + '/client/profile/profile';
  var password = dijit.byId('NotLoggedIn_Dialog_Login_Form_password').getValue();
  
  password = hex_sha1( password );
  
  //Set the fields
  dojo.byId('NotLoggedIn_Dialog_Login_Form_bounce').value = bounce;
  dijit.byId('NotLoggedIn_Dialog_Login_Form_password').setValue( password );
};
//##############################################################################
/**
 *Process the register form
 */
NotLoggedInOverlayRegister.prototype.form_register = function( /*Event*/ evnt ){
  
  dojo.stopEvent(evnt);
  
  var args = {
    'url':    CapsoolGlobalVariables.AppRoot + '/api/register/index/code/' + NotLoggedInOverlayRegister.instance.couponCode + '/submitFlag/1',
    'form':     dojo.byId('NotLoggedIn_Dialog_Registration_Form'),
    'handleAs': 'json',
    'load': function(data){
              
              if( data.status == 1 ){
                NotLoggedInOverlayRegister.instance.email = data.email;
                NotLoggedInOverlayRegister.instance.open('NotLoggedIn_Dialog_Verify');
              }else{
                window.alert( data.message );
                NotLoggedInOverlayRegister.instance.open( 'NotLoggedIn_Dialog_Registration' );
              }
            },
    'error': function(error){
             }
  };
  
  var deferred = dojo.xhrPost( args );
};
//##############################################################################
/**
 *Process the verify form
 */
NotLoggedInOverlayRegister.prototype.form_verify = function( /*Event*/ evnt ){
  
  dojo.stopEvent(evnt);
  
  var args = {
    'url':    CapsoolGlobalVariables.AppRoot + '/api/register/activateemail/email/' + NotLoggedInOverlayRegister.instance.email,
    'form':     dojo.byId('NotLoggedIn_Dialog_Verify_Form'),
    'handleAs': 'json',
    'load': function(data){
              
              if( data.status == 1 ){
                NotLoggedInOverlayRegister.instance.open( 'NotLoggedIn_Dialog_Login' );
              }else{
                window.alert( data.message );
                NotLoggedInOverlayRegister.instance.open( 'NotLoggedIn_Dialog_Verify' );
              }
            },
    'error': function(error){
             }
  };
  
  var deferred = dojo.xhrPost( args );
};
//##############################################################################
/**
 *Open a dialog
 */
NotLoggedInOverlayRegister.prototype.open = function( arg ){
  
  var dialogName = ( !arg ? 'NotLoggedIn_Dialog_Coupon' : arg );
  
  NotLoggedInOverlayRegister.instance.close();
  
  window.setTimeout(
  
    function(){
      
      if( dialogName && (dialogName == 'NotLoggedIn_Dialog_Coupon') ){
        dijit.byId('NotLoggedIn_Dialog_Coupon').show();
        
      }else if( dialogName && (dialogName == 'NotLoggedIn_Dialog_Login') ){
        dijit.byId('NotLoggedIn_Dialog_Login').show();
      
      }else if( dialogName && (dialogName == 'NotLoggedIn_Dialog_Registration') ){
        dijit.byId('NotLoggedIn_Dialog_Registration').show();
      
      }else if( dialogName && (dialogName == 'NotLoggedIn_Dialog_Verify') ){
        dijit.byId('NotLoggedIn_Dialog_Verify').show();
      }
    }, 750
  );
};
//##############################################################################
/**
 *Open a dialog
 */
NotLoggedInOverlayRegister.prototype.resend_reg_email = function(/*Event*/ evnt){
  
  dojo.stopEvent(evnt);
  
  var args = {
    'url':  CapsoolGlobalVariables.AppRoot + '/register/verify/id/' + NotLoggedInOverlayRegister.instance.email,
    'handleAs': 'text',
    'load': function(data){
            },
    'error': function(error){
             }
  };
  
  var deferred = dojo.xhrGet( args );
};
//##############################################################################
//=============================================================================>
//Wire up
dojo.addOnLoad(
  function(){
    
    NotLoggedInOverlayRegister.instance.addOnLoad();
  }
);
//=============================================================================>

