// From http://xpertize.gb.int/_ajax/users.php?showstub=true&objectname=userControler
if(typeof(userControler) == 'undefined')
  userControler = new Object();

Object.extend(userControler,
{
  _callback471f412e84a1d: "/_ajax/users.php",

  _proxy: function(callbackurl, methodName, args, sync)
  {
    var targs = $H();
    for(var i = 0; i < args.length-1; i++){ targs.set('args[' + i + ']', args[i]); }
    callback = args[args.length-1];
    var ar = new Ajax.Request(callbackurl + '?method=' + methodName,
    {
      method: 'post',
      asynchronous: (!sync),
      parameters: targs,
      onComplete: (sync)?null:callback,
      onException: function(request, e){console.debug(request, e);}
    });
    if(sync)
      return new Ajax.Response(ar);
  },

  _resetPassword: function(){ this._proxy(this._callback471f412e84a1d, '_resetPassword', arguments, false); },
  _resetPassword_sync: function(){ return this._proxy(this._callback471f412e84a1d, '_resetPassword', arguments, true); },

  _checkLogin: function(){ this._proxy(this._callback471f412e84a1d, '_checkLogin', arguments, false); },
  _checkLogin_sync: function(){ return this._proxy(this._callback471f412e84a1d, '_checkLogin', arguments, true); },

  _loginExists: function(){ this._proxy(this._callback471f412e84a1d, '_loginExists', arguments, false); },
  _loginExists_sync: function(){ return this._proxy(this._callback471f412e84a1d, '_loginExists', arguments, true); },

  _registerCompany: function(){ this._proxy(this._callback471f412e84a1d, '_registerCompany', arguments, false); },
  _registerCompany_sync: function(){ return this._proxy(this._callback471f412e84a1d, '_registerCompany', arguments, true); },
	
  _finalizeRegistration: function(){ this._proxy(this._callback471f412e84a1d, '_finalizeRegistration', arguments, false); },
  _finalizeRegistration_sync: function(){ return this._proxy(this._callback471f412e84a1d, '_finalizeRegistration', arguments, true); },
  
  _t: ''
});


Object.extend(userControler,
{
    _request: null,
    isLogged: false,
	 needFinalizeRegistration: false,
    _isCompanyPage: false,
    _callback: null,
	actionNeededBeforeCallback: function()
	{
		if(!userControler.isLogged)
		{
			return "loginAndCallback";
		}
		if(userControler.isLogged && userControler.needFinalizeRegistration)
		{
			return "finalizeRegistrationAndCallback";
		}
		return 'none';
	},
	callAction: function(callback)
	{
		this._callback = callback;
		var action = this.actionNeededBeforeCallback();
		if(action == "none")
		{
			this._callback(this);
		}
		if(action == "loginAndCallback")
		{
			this.loginAndCallback(this._callback);
		}
		if(action == "finalizeRegistrationAndCallback")
		{
		this.finalizeRegistrationAndCallback(this._callback);
		}
	},
    loginAndCallback: function(callback)
    {
        this._callback = callback;
        myLightWindow.activateWindow({href: '/'+currentLanguage+'/lw/login.php', title: '', type: 'page', width: 450, height: 380});
	   	$('lightwindow').addClassName("noBg_noBorder");
		$('lightwindow').addClassName("close20pxright");
		myLightWindow.options.deactivateCallback = function(proceed)
        {
			$('lightwindow').removeClassName("noBg_noBorder");
			$('lightwindow').removeClassName("close20pxright");
        };
    },

    checkLogin: function(login,pass,remember)
    {
        $('lightwindow_loading').show();
        this._checkLogin(login, pass, remember, this._loginChecked.bind(this))
    },

    checkLoginCompany: function(login, pass, remember)
    {
        this._isCompanyPage=true;
        $('lightwindow_loading').show();
        this._checkLogin(login, pass, remember, 'client', this._loginChecked.bind(this));
    },

    _loginChecked: function(transport)
    {
        if(!transport.responseJSON.failed)
        {
            this.isLogged = true;
            if(!this._callback)
            {
                if(this._isCompanyPage)
                {
                    document.location='/company/edit.php';
                }
                else
                {
                    document.location.reload(true);
                }
            }
            else
            {
                $('lightwindow_loading').hide();
                myLightWindow.options.deactivateCallback = function(proceed)
                {
                    document.location.reload(true);
                };
                this._callback(this);
            }
        }
        else
        {
            $('lightwindow_loading').hide();
            new AlertBox(transport.responseJSON.message, {alertType: 'Error', okButtonClass: 'button'});
        }
    },

    resetPass: function(login)
    {
        $('lightwindow_loading').show();
        this._resetPassword(login,this.passReseted.bind(this));


        /* this.request = new Ajax.Request(webRoot+'/_ajax/users.php',
        {
        method: 'get',
        parameters: 'method=resetPassword&args[]='+login,
        onSuccess: this.passReset.bind(this)

        });*/
    },
    passReseted: function(transport, json)
    {
        $('lightwindow_loading').hide();
        if(json.out.status)
        {
            new AlertBox(json.out.error, {alertType: 'OK', okButtonClass: 'button'});
        }
        else
        {
            new AlertBox(json.out.error, {alertType: 'Error', okButtonClass: 'button'});
        }
    },
    register: function()
    { 
		//$('lightwindow_loading').show();
        $('register_form').submit();
    },
    registerFinish:function(error,message,code,lang)
    {
        //$('lightwindow_loading').hide();
        if(error)
        {
            $('code').value=code;
            $('codeUser').value="";
            GlobuleCaptcha.getAnother();
            /*$('imgCode').src="http://xpertize.gb.int/lw/register.php?captcha="+code;*/
            new AlertBox(message, {alertType: 'Error', okButtonClass: 'button'});
        }
        else
        {
            document.location.href = '/' + lang + '/profile/edit.php';
        }
    },

    dropACV: function(fromLogin)
    {
        
        userControler.callAction(
        		function(e)
          		{
            if(fromLogin)
           		myLightWindow.openWindow($('b_dropCv'));
            else
            	myLightWindow.activateWindow({href: '/'+currentLanguage+'/lw/dropCv.php', title: '', type: 'page', width: 620, height: 410});

        });
    },
	
	sponsorShip: function()
	{
		 userControler.callAction(
        		function(e)
          		{
            myLightWindow.activateWindow({href: '/'+currentLanguage+'/sponsorship.php', title: '', type: 'page', width: 700, height: 500});
		});
	},
	widget: function()
	{
		userControler.callAction(
        		function(e)
          		{
            myLightWindow.activateWindow({href: '/'+currentLanguage+'/stayInTouch/blogmyspace.php', title: '', type: 'page', width: 700, height: 600});
		});
	},
    registerCompany : function()
    {
        $('lightwindow_loading').show();

        var o =
        {
            companyName: $F('company_name'),
            sector: $F('sector'),
            website: $F('website'),
            firstName: $F('first_name'),
            lastName: $F('last_name'),
            email: $F('email'),
            companyPosition: $F('companyPosition'),
            phone: $F('phone'),
            password: $F('password'),
            confirm: $F('confirm')
        }
        this._registerCompany(Object.toJSON(o), $F('codeUser'), $F('code'), this._companyRegistered.bind(this));
    },
    _companyRegistered:function(transport, json)
    {
        $('lightwindow_loading').hide();
        if(json.failed)
        {
            var data=json.message.evalJSON();
            $('code').value = data["code"];
            $('codeUser').value="";
            GlobuleCaptcha.getAnother();
            new AlertBox(data["message"], {alertType: 'Error', okButtonClass: 'button'});
        }
        else
        {
            myLightWindow.options.deactivateCallback = function(proceed)
            {
                document.location.reload(true);
            };
            if(!this._callback) {
               /*
			    new AlertBox(json.out, {
					   alertType: 'OK', 
					   okButtonClass: 'button', 
					   onButtonPressed: function(){document.location='/company/edit.php';}
                });
				*/
				document.location='/company/edit.php';
            }
            else
            {
                $('lightwindow_loading').hide();
                this._callback(this);
            }

        }

    },
    showSectorSkill : function(elem,init)
	  {
	  	if (init==undefined) param2 = false;
		$$('.skillsTab').invoke('hide');
	  	$$('#'+elem.id+'> option').each(function(e)
	  	{
	  		if(e.selected)
	  		{
				if(e.value)
				{
	  				$('skills_list'+e.value).show();
					if(!init)
						$('skills').select('.ckbSkill').each(function(e){e.checked=false});
				}
	  		}
	  	});
	  	
	  }
    ,
    toogleSkill: function(elem)
	{
		var parentSkill=$(elem);
		var childSkill=parentSkill.next('ul');
		if(childSkill)
		{
			if(childSkill.visible())
			{
				childSkill.hide();
				childSkill.select('.ckbSkill').each(function(e){e.checked=false});
				childSkill.select('.skillsList').invoke("hide");
			}
			else
			{
				childSkill.show();
			}
		}
		
		
	},
	finalizeRegistration:function(elem)
	{
		 this._finalizeRegistration($F(elem), this._registrationFinished.bind(this));
	},
	_registrationFinished:function()
	{
		if(!this._callback) 
		{
        	document.location.reload(true);
        }
        else
        {
        	$('lightwindow_loading').hide();
        	this._callback(this);
        }
		 
	},
	finalizeRegistrationAndCallback: function(callback)
    {
        this._callback = callback;
        myLightWindow.activateWindow({href: '/'+currentLanguage+'/lw/user-mail.php', title: '', type: 'page', width: 425, height:100}); 
    }
    
});
