function MyJet2RemoteManagerOptions()
{
    this.MyJet2HandlerUrl = '';
    this.ReceiveLoginResponse = function(){};
    this.ReceivePointsSummaryResponse = function(){};
    this.ReceivePackageStatusResponse = function(){};
    this.ReceivePackageDetailResponse = function(){};
    this.ReceiveIsLoggedInResponse = function(){};
    this.UsernameTextBoxId = '';
    this.PasswordTextBoxId = '';
    this.AjaxTimeout = 30000;
    this.Async = true;
}

function MyJet2RemoteManager(options)
{
    this.Options=options;
};

MyJet2RemoteManager.prototype={
    //Add new holiday deal helper
    _pointsSummaryResponse:null,
    GetPointsSummary:function()
    {
        var options = this.Options;
	    var objectScope=this;
	    this._pointsSummaryResponse=null;
	    $.ajax({
	            async:true,
			    cache:false,
			    dataType:'jsonp',
			    error:function(XMLHttpRequest, textStatus, errorThrown){objectScope.HandleAjaxResponse('pointssummary',false,null,textStatus);},
			    success:function(data, textStatus){objectScope.HandleAjaxResponse('pointssummary',true,data,textStatus);},
			    timeout:options.AjaxTimeout,
			    type:'GET',
			    url:this.AppendQsParm(options.MyJet2HandlerUrl,'action=pointssummary')
		    });
		    
		if(!options.Async) return this._pointsSummaryResponse;
		else return null;
    },
    _packageStatusResponse:null,
    GetPackageStatus:function()
    {      
        var options = this.Options;
	    var objectScope=this;
	    this._packageStatusResponse=null;
	    $.ajax({
	            async:true,
			    cache:false,
			    dataType:'jsonp',
			    error:function(XMLHttpRequest, textStatus, errorThrown){objectScope.HandleAjaxResponse('packagestatus',false,null,textStatus);},
			    success:function(data, textStatus){objectScope.HandleAjaxResponse('packagestatus',true,data,textStatus);},
			    timeout:options.AjaxTimeout,
			    type:'GET',
			    url:this.AppendQsParm(options.MyJet2HandlerUrl,'action=packagestatus')
		    });
		    
		if(!options.Async) return this._packageStatusResponse;
		else return null;
        
    },
    _packageDetailResponse:null,
    GetPackageDetail:function(reference)
    {
        var options = this.Options;
	    var objectScope=this;
	    this._packageDetailResponse=null;
	    $.ajax({
	            async:true,
			    cache:false,
			    dataType:'jsonp',
			    error:function(XMLHttpRequest, textStatus, errorThrown){objectScope.HandleAjaxResponse('packagedetail',false,null,textStatus);},
			    success:function(data, textStatus){objectScope.HandleAjaxResponse('packagedetail',true,data,textStatus);},
			    timeout:options.AjaxTimeout,
			    type:'GET',
			    url:this.AppendQsParm(options.MyJet2HandlerUrl,'action=packagedetail&reference=' + reference)
		    });		    
		if(!options.Async) return this._packageDetailResponse;
		else return null;
    },
    _isLoggedInResponse:false,
    IsLoggedIn:function(reference)
    {
        var options = this.Options;
	    var objectScope=this;
	    this._isLoggedInResponse=null;
	    $.ajax({
	            async:true,
			    cache:false,
			    dataType:'jsonp',
			    error:function(XMLHttpRequest, textStatus, errorThrown){objectScope.HandleAjaxResponse('isloggedin',false,null,textStatus);},
			    success:function(data, textStatus){objectScope.HandleAjaxResponse('isloggedin',true,data,textStatus);},
			    timeout:options.AjaxTimeout,
			    type:'GET',
			    url:this.AppendQsParm(options.MyJet2HandlerUrl,'action=isloggedin')
		    });		    
		if(!options.Async) return this._isLoggedInResponse;
		else return null;
    },
    _loginResponse:false,
    Login:function()
    {
        var options = this.Options;
        var username = $('#'+options.UsernameTextBoxId).val();
	    var password = $('#'+options.PasswordTextBoxId).val();
	    var objectScope=this;
	    this._loginResponse=null;
	    $.ajax({
	            async:true,
			    cache:false,
			    dataType:'jsonp',
			    data:{un:username,pw:password},
			    error:function(XMLHttpRequest, textStatus, errorThrown){objectScope.HandleAjaxResponse('login',false,null,textStatus);},
			    success:function(data, textStatus){objectScope.HandleAjaxResponse('login',true,null,textStatus);},
			    timeout:options.AjaxTimeout,
			    type:'GET',
			    url:this.AppendQsParm(options.MyJet2HandlerUrl,'action=login')
		    });
		if(!options.Async) return this._loginResponse;
		else return null;
    },
    Logout:function()
    {
        var options = this.Options;
	    var objectScope=this;
	    $.ajax({
	            async:true,
			    cache:false,
			    dataType:'jsonp',
			    error:function(XMLHttpRequest, textStatus, errorThrown){objectScope.HandleAjaxResponse('logout',false,null,textStatus);},
			    success:function(data, textStatus){objectScope.HandleAjaxResponse('logout',true,null,textStatus);},
			    timeout:options.AjaxTimeout,
			    type:'GET',
			    url:this.AppendQsParm(options.MyJet2HandlerUrl,'action=logout')
		    });
		return null;
    },
    HandleAjaxResponse:function(action,successful,data)
    {
        if(successful==null)successful=false;
        if(data==null)data=false;
        switch(action)
        {
            case 'login':
                this._loginResponse=successful;
                if(typeof(this.Options.ReceiveLoginResponse)=="function"||typeof(this.Options.ReceiveLoginResponse)=="string") this.Options.ReceiveLoginResponse(this._loginResponse);
                break;
            case 'pointssummary':
                this._pointsSummaryResponse=data;
                if(typeof(this.Options.ReceivePointsSummaryResponse)=="function"||typeof(this.Options.ReceivePointsSummaryResponse)=="string") this.Options.ReceivePointsSummaryResponse(this._pointsSummaryResponse)
                break;
            case 'packagestatus':
                this._packageStatusResponse=data;
                if(typeof(this.Options.ReceivePackageStatusResponse)=="function"||typeof(this.Options.ReceivePackageStatusResponse)=="string") this.Options.ReceivePackageStatusResponse(this._packageStatusResponse)
                break;
            case 'packagedetail':
                this._packageDetailResponse=data;
                if(typeof(this.Options.ReceivePackageDetailResponse)=="function"||typeof(this.Options.ReceivePackageDetailResponse)=="string") this.Options.ReceivePackageDetailResponse(this._packageDetailResponse)
                break;
            case 'isloggedin':
                if(successful) this._isLoggedInResponse=data.IsLoggedIn;
                else this._isLoggedInResponse=false;
                if(typeof(this.Options.ReceiveIsLoggedInResponse)=="function"||typeof(this.Options.ReceiveIsLoggedInResponse)=="string") this.Options.ReceiveIsLoggedInResponse(this._isLoggedInResponse)
                break;
        }
    },
    AppendQsParm:function(url,nameEqValue)
    {
        if(url&&nameEqValue)
        {
            var idx = url.lastIndexOf('?');
            if(idx==-1)
                return url + '?' + nameEqValue;
            else if(idx=(url.length-1))
                return url + nameEqValue;
            else
            {
                idx = url.lastIndexOf('&');
                if(idx==(url.length-1))
                    return url + nameEqValue;
                else
                    return url + '&' + nameEqValue;
            }
        }   
    }
};