// This wrapper function is used to so we don't get errors if the JavaScript 
// object is accessed before all script is loaded on the page.
function fnSaveVehicle(dlrId, vehId)
{
    if(!javaLoaded){return false;}
    
    SaveVehicleVar.Save(dlrId, vehId);
    return false;
}

function SaveVehicle()
{
	this.Init = function(id)
	{
	    this._clientId = id;
	    this._tempVehId = 0;
	    this._tempDlrId = 0;
	    this._waTrackAction = "";
   	    this._doLogin = false;

	    
	    // Add handlers so we can call functions associated with this object.
        $addHandlers($get('aClose'), {click:this.SaveVehicle_OnClose}, this);
        $addHandlers($get('aOk'), {click:this.SaveVehicle_OnOk}, this);
        $addHandlers($get('aLogin'), {click:this.SaveVehicle_OnLogin}, this);
        $addHandlers($get('aRegister'), {click:this.SaveVehicle_OnRegister}, this);
        
        // Setup a method to cleanup
        var delDispose = Function.createDelegate(this, this.Dispose);
        Sys.Application.add_unload(delDispose);
	}
	
    // Clean up after ourself
	this.Dispose = function()
	{
        $clearHandlers($get('aClose'));
        $clearHandlers($get('aOk'));
        $clearHandlers($get('aLogin'));
        $clearHandlers($get('aRegister'));
	}

    this.Save = function(dlrId, vehId)
    {
        this._tempVehId = vehId;
        this._tempDlrId = dlrId;
        this._doLogin = false;

        if ((isLoggedInVar == 'N') && (showSaveVehWarn != 'N'))
        {
            showAlertBackground()
            showDIV('saveToCookie');
        }
        else
        {
            this.SaveVehicle_OnOk();
        }
        
        return false;
    }

    this.SaveVehicle_OnClose = function()
    {
        hideDIV('saveToCookie');
        hideAlertBackground();

        var el = $get('checkCookieSave');
        if (el.checked)
        {
            Reynolds.CL.Web.WebServices.VehicleFunctions.SaveCookieNoWarn(true, WSKey);
            showSaveVehWarn = "N"
            el.checked = false;
        }
        
        return false;
    }
    
    this.SaveVehicle_OnOk = function()
    {
        this.SaveVehicle_OnClose();

        ProgressVar.Open();
        
        Reynolds.CL.Web.WebServices.VehicleFunctions.SaveVehicle(this._tempDlrId, this._tempVehId, WSKey, SaveVehicleSuccess, SaveVehicleFailure, this);
        
        return false;
    }

    this.SaveVehicle_OnLogin = function()
    {
        this._doLogin = true;
        this.SaveVehicle_OnOk();
        return false;
    }
    
    this.SaveVehicle_OnRegister = function()
    {
        this.SaveVehicle_OnOk();
        openRegister(regLink);
        return false;
    }
     
    function SaveVehicleSuccess(result, context) 
    { 
        if(0 < context._waTrackAction.length)
        {
            fnWAPageView(context._waTrackAction);
        }
    
        context._tempVehId = 0;
        context._tempDlrId = 0;
        ProgressVar.SuccessfulComplete('Vehicle Saved Successfully.', 1000);
        
        if(context._doLogin)
        {
            showLoginPopup();
        }
    } 

    function SaveVehicleFailure(err, context) 
    {
        context._tempVehId = 0;
        context._tempDlrId = 0;

        ProgressVar.FailureComplete('Error saving Vehicle. Please try again.', 1000);
    } 

}
