// 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); },

  _t: ''
});


Object.extend(userControler,
{
    _request: null,
    isLogged: false,
    _isCompanyPage: false,
    _callback: null,

    loginAndCallback: function(callback)
    {
        this._callback = callback;
        myLightWindow.activateWindow({href: '/lw/login.php', title: '', type: 'page', width: 520, height: 220});
    },

    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)
    {
        $('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
        {
            myLightWindow.options.deactivateCallback = function(proceed)
            {
                document.location.reload(true);
            };
            if(!this._callback)
            {
                  //new AlertBox(message, {alertType: 'OK', okButtonClass: 'button',callBack:function(){ document.location.reload(true);}});
                //Ouverture parrainage
                $('lightwindow_loading').hide();
                myLightWindow.activateWindow({href: '/sponsorship.php', title: '', type: 'page', width: 700, height: 500});
             }
            else
            {
                $('lightwindow_loading').hide();
                this._callback(this);
            }
        }
    },

    dropACV: function(fromLogin)
    {
        if(this.isLogged)
        {
            if(fromLogin)
            myLightWindow.openWindow($('b_dropCv'));
            else
            myLightWindow.activate(null, $('b_dropCv'));

        }
        else
        this.loginAndCallback(this.dropACV.bind(this));
    },
	
	sponsorShip: function()
	{
		if (this.isLogged) {
            myLightWindow.activateWindow({href: '/sponsorship.php', title: '', type: 'page', width: 700, height: 500});
		}
		else
		{
            this.loginAndCallback(this.sponsorShip.bind(this));
		}
	},
	
    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();
			}
		}
		
		
	}
    
});
