// From http://xpertize.gb.int/_ajax/missions.php?showstub=true&objectname=MissionsController
if(typeof(MissionsController) == 'undefined')
  MissionsController = new Object();

Object.extend(MissionsController,
{
  _callback471f4100a3e6e: "/_ajax/missions.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);
  },

  _applyMission: function(){ this._proxy(this._callback471f4100a3e6e, '_applyMission', arguments, false); },
  _applyMission_sync: function(){ return this._proxy(this._callback471f4100a3e6e, '_applyMission', arguments, true); },

  _canApply: function(){ this._proxy(this._callback471f4100a3e6e, '_canApply', arguments, false); },
  _canApply_sync: function(){ return this._proxy(this._callback471f4100a3e6e, '_canApply', arguments, true); },

  _hasApplied: function(){ this._proxy(this._callback471f4100a3e6e, '_hasApplied', arguments, false); },
  _hasApplied_sync: function(){ return this._proxy(this._callback471f4100a3e6e, '_hasApplied', arguments, true); },

  _addBookmark: function(){ this._proxy(this._callback471f4100a3e6e, '_addBookmark', arguments, false); },
  _addBookmark_sync: function(){ return this._proxy(this._callback471f4100a3e6e, '_addBookmark', arguments, true); },

  _removeBookmark: function(){ this._proxy(this._callback471f4100a3e6e, '_removeBookmark', arguments, false); },
  _removeBookmark_sync: function(){ return this._proxy(this._callback471f4100a3e6e, '_removeBookmark', arguments, true); },

  _t: ''
});
// End generated code

Object.extend(MissionsController,
{
    _reload: false,

    addBookmark: function(missionId)
    {
         userControler.callAction(this._addBookmark(missionId, this._bookmarkAdded.bind(this, missionId, (arguments.length > 1))));
        return false;
    },
    removeBookmark: function(missionId)
    {
        this._removeBookmark(missionId, this._bookmarkRemoved.bind(this, missionId, (arguments.length > 1)));
        return false;
    },

    _bookmarkAdded: function(missionId, reload, transport)
    {
        if(transport.responseJSON.failed)
            new AlertBox(transport.responseJSON.message, {alertType: 'Error', okButtonClass: 'button'});
        else
            document.location = '/'+currentLanguage+'/jobs/bookmarks.php';
    },

    _bookmarkRemoved: function(missionId, reload, transport)
    {
        if(transport.responseJSON.failed)
        new AlertBox(transport.responseJSON.message, {alertType: 'Error', okButtonClass: 'button'});
        else if(reload)
        document.location.reload(true);
        else
        {
            $('bookmark_add_'+missionId).show();
            $('bookmark_remove_'+missionId).hide();
        }
    },

    applyToMission: function(missionId)
    {
        try
        {
           userControler.callAction(this._applyToMission.bind(this, missionId));
        }
        catch(e)
        {
            //console.debug(e);
        }
        return false;
    },

    _applyToMission: function(missionId, fromLogin)
    {
        if(fromLogin)
            this._reload = true;
        var ar = this._canApply_sync(null);
        if(ar.responseJSON.out)
        {
         /*   if(fromLogin)
                myLightWindow.openWindow($('apply_'+missionId));
            else*/
                myLightWindow.activate(null, $('apply_'+missionId));
        }
        else
            document.location = '/'+currentLanguage+'/jobs/apply.php?mid='+missionId;
    },

    applyMission: function(classCB)
    {
        var mId=$A();
        $$('.'+classCB).each(function(t)
        {
            //alert(t.checked);
            //if(t.checked)
            //{
                mId.push(t.value);
            //}
        });
        //alert(mId.toJSON());
        //return;
        this._applyMission(mId.toJSON(), this._appliedMission.bind(this));
    },

    _appliedMission: function(transport)
    {
        if(transport.responseJSON.failed)
        new AlertBox(transport.responseJSON.message, {alertType: 'Error', okButtonClass: 'button'});
        else
        new AlertBox(transport.responseJSON.out, {alertType: 'OK', okButtonClass: 'button',callBack:function()
        {
           /* myLightWindow.deactivate();*/
            window.document.location.href = '/'+currentLanguage+'/jobs/applications.php';
        }.bind(this)});
    },

    recommendMission: function(missionId)
    {
         try
        {
            userControler.callAction(this._recommendMission.bind(this, missionId));
        }
        catch(e)
        {
            console.debug(e);
        }
        return false;
    },

    _recommendMission: function(missionId, fromLogin)
    {
        /*if(fromLogin)
        {
            this._reload = true;
            myLightWindow.openWindow($('recommend_'+missionId));
        }
        else*/
            myLightWindow.activate(null, $('recommend_'+missionId));
    },
    submitSearch: function()
    {
    	var strSector = $$('#sector option[value="'+jQuery('#sector').getSetSSValue()+'"]')[0].readAttribute("rwrvalue");
    	if(!strSector)
    		strSector = "";
    	var strRegion = $$('#region option[value="'+jQuery('#region').getSetSSValue()+'"]')[0].readAttribute("rwrvalue");
    	if(!strRegion)
    		strRegion = "";
    	var strContType = jQuery('#contType').getSetSSValue();
    	var strKeywords =$F('inputkeywords');
    	var strUrl = "";
    	
    	if(strKeywords != "")
    	{
    		strUrl ='/'+currentLanguage+'/jobs/'+strSector+'/'+strRegion+'/'+strContType+'/'+escape(strKeywords);
    		
    	}
    	else if(strContType != "all")
    	{
    		strUrl ='/'+currentLanguage+'/jobs/'+strSector+'/'+strRegion+'/'+strContType+'/';
    		
    	}
    	else if (strRegion != "region-")
    	{
    		strUrl ='/'+currentLanguage+'/jobs/'+strSector+'/'+strRegion+'/';
    	}
    	else if (strSector != "sector-")
    	{
    		strUrl ='/'+currentLanguage+'/jobs/'+strSector+'/';
    	}
    	else
    	{
    		strUrl ='/'+currentLanguage+'/jobs/';
    	}
    
    	document.location = strUrl;
    	
    
    },
	applyToMissionJobat: function(missionId)
    {
        try
        {
             userControler.callAction(this._applyToMissionJobat.bind(this, missionId,true));
        }
        catch(e)
        {
            //console.debug(e);
        }
        return false;
    },

    _applyToMissionJobat: function(missionId, fromLogin)
    {
        if(fromLogin)
        {
          
           window.location.reload()
       		
       	}
       
       	window.open ('/jobs/applyjb.php?mid='+missionId);
    },
	submitQuickSearch: function()
    {
    	if(typeof($('inputQuickSearch').focusedOT) != 'undefined')
		{
			var strKeywords =$F('inputQuickSearch');
			strUrl ='/'+currentLanguage+'/jobs/sector-/region-/all/'+escape(strKeywords);
			document.location = strUrl;
		}
    	
    
    },
	submitQuickSearchKeyPress: function(e)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13)
		{
		   MissionsController.submitQuickSearch();
		}
		
	},
	clearOnfocus: function(elem)
	{
		if(typeof(elem.focusedOT) == 'undefined')
		{		
			elem.focusedOT = 1;
			elem.value='';
		}
	}

});
