// Make sure the LeadPopupVar instance is valid before
// trying to open the popup.
function ShowLeadPopup(vehId, dlrId)
{
    if(LeadPopupVar)
    {
        LeadPopupVar.Open(vehId, dlrId);
    }
    
    return false;
}

function LeadPopup()
{
	this.Init = function(id)
	{
	    this._clientId = id;
	    
	    // Add handlers so we can call functions associated with this object.
        $addHandlers($get("aLPClose"), {click:this.Close}, this);

	    // Create a delegates that will be used by the leadForm object.
        this._delClose = Function.createDelegate(this, this.Close);
        this._delOnFail = Function.createDelegate(this, this.OnFailure);
        
        // 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("aLPClose"));
	}

    this.Open = function(vehId, dlrId)
    {
        // Set the vehicle & dealer Id's on the LeadForm.
	    leadFormVar._vehId = vehId;
	    leadFormVar._dlrId = dlrId;

        // Set what to call when the lead is sent to the server.
        leadFormVar._onSend = this._delClose;
        leadFormVar._onFailure = this._delOnFail;

        this.ExecOpen();
        return false;
    }
    
    // Open the form after the vehicle and dealer Ids have been set.
    this.ExecOpen = function()
    {
        hideDropDownVarList();
        
        showDIV('alertBackground');
        showDIV('divLeadPopup');
        
        setPopupDivPosition('divLeadPopup');
        
        leadFormVar.Focus();
        return false;
    }
    
    // OnFailure is called when the lead form has a failure and the user clicks OK on the error alert.
    this.OnFailure = function()
    {
        this.ExecOpen();
        // Most likely failure is in the image capture. Set focus there.
        leadFormVar.CaptureFocus();
    }

    this.Close = function()
    {
        showDropDownVarList();
        
        hideDIV('alertBackground');
        hideDIV('divLeadPopup');

        return false;
    }
}