var divSendToMobileId = "divSendToMobile";
var lnkSendToMobileId = "lnkSendToMobileButton";
var lnkSendToMobileCloseId = "lnkSendToMobileClose";
var txtSendToMobileNumberId = "txtSendToMobileNumber";
var divSendToMobileMissingFieldsId = "divSendToMobileMissingFields";
var divSendToMobileInvalidDataId = "divSendToMobileInvalidData";

function SendToMobile()
{    
    this.Init = function(id)
    {
        this._clientId = id;
        this._dlrId = -1;
        this._vehId = -1;
        this._vin = "";
        this._waTrackAction = "";
        
        $addHandler($get(lnkSendToMobileCloseId), 'click', this.Close);
        $addHandlers($get(lnkSendToMobileId), {click:this.Send}, this);
        $addHandlers($get(divSendToMobileId), {keypress:this.CheckKey}, this);
    }
    
    this.Dispose = function()
    {
        $clearHandlers($get(lnkCloseId));
        $clearHandlers($get(lnkSendToMobileId));
        $clearHandlers($get(divSendToMobileId));
    }
    
   	this.CheckKey = function(evt)
   	{
        //Hide errors
        //ClearErrBkg($get(txtSendToMobileNumberId));
        //hideDIV(divSendToMobileMissingFieldsId);
        //hideDIV(divSendToMobileInvalidDataId);
        
        //If the user hit enter, go for it
        if (mapKeyToAction(evt))
        { 
            this.Send();
        }
    
    }

    this.Open = function()
    {
        //Show alert background DIV and the send to mobile DIV
        showAlertBackground();
        showDIV(divSendToMobileId);
        
        //Set the position of the popup
        setPopupDivPosition(divSendToMobileId);
    }
    
    this.Close = function()
    {
        hideAlertBackground();
        hideDIV(divSendToMobileId);
    }

    this.Send = function()
    {
        if(this.Validate())
        {
            //Hide this
            this.Close();
            
            //Show "please wait"
            ProgressVar.Open();
            
            //Get the phone number
            var phoneNumber = $get(txtSendToMobileNumberId).value;
            
            //Call the web service
            Reynolds.CL.Web.WebServices.VehicleFunctions.SendToMobile(
                this._dlrId,
                this._vehId,
                this._vin,
                phoneNumber,
                WSKey,
                SendToMobileSuccess,
                SendToMobileFailure,
                this);
        }
    }
    
    this.Validate = function()
    {
        var validated = true;
	    var phoneNumber = $get(txtSendToMobileNumberId).value;

        //Un-error-ify
        ClearErrBkg($get(txtSendToMobileNumberId));
        addCssClass($get(divSendToMobileMissingFieldsId), "hide");
        addCssClass($get(divSendToMobileInvalidDataId), "hide");
        
        //If phone number is missing
	    if(0 == phoneNumber.length)
	    {
	        validated = false;
	        removeCssClass($get(divSendToMobileMissingFieldsId), "hide");
	        SetErrBkg($get(txtSendToMobileNumberId));
	    }
	    //If phone number isn't a phone number
	    else if (!g_regexPhone.test(phoneNumber)) 
        {
	        validated = false;
	        removeCssClass($get(divSendToMobileInvalidDataId), "hide");
	        SetErrBkg($get(txtSendToMobileNumberId));
        }
        
        return validated;
    }
}

//AJAX callback functions
function SendToMobileSuccess(result, context)
{
    //Log the action with google analytics
    if(0 < context._waTrackAction.length)
    {
        fnWAPageView(context._waTrackAction);
    }
    
    //Show a success message
    ProgressVar.SuccessfulComplete(
                "Thank you!  You'll receive a text message about this vehicle on your mobile phone momentarily.",
                3000
                );
}

function SendToMobileFailure(err, context)
{
    //Show error message for 3 seconds
    ProgressVar.FailureComplete(err.get_message(), 3000);
}
    
//Wrapper so we can call this open function without
//being worried about the javascript object not being
//instantiated
function fnOpenSendToMobile()
{
    if(SendToMobileVar != null)
    {
        SendToMobileVar.Open();
    }
}
