﻿//Ids
var divCallDealerId = 'divCallDealer';
var lnkCloseId = 'clcClose';
var divCallDealerDealerNameId = 'divCallDealerDealerName';
var divCallDealerDealerLocationId = 'divCallDealerDealerLocation';
var txtCallDealerAreaCodeId = 'txtCallDealerPhoneAreaCode';
var txtCallDealerPrefixId = 'txtCallDealerPhonePrefix';
var txtCallDealerSuffixId = 'txtCallDealerPhoneSuffix';
var pCallDealerSecurityInfoId = 'pCallDealerSecurityInfo';
var lnkCallDealerSecurityInfoId = 'lnkCallDealerSecurityInfo';
var lnkCallDealerCallButtonId = 'lnkCallDealerCallButton';
var divCallDealerMissingFieldsId = 'divCallDealerMissingFields';
var divCallDealerInvalidDataId = 'divCallDealerInvalidData'; 

function CallDealer()
{
    this._clientId = null;
    this._dlrId = null;
    this._dlrPhone = null;
    this._vehId = null;
    this._waTrackAction = null;

    this.Init = function(id)
    {
        //Initialize variables
        this._clientId = id;
        this._dlrId = -1;
        this._vehId = -1;
        this._dlrPhone = '';
        this._waTrackAction = '';

        //Add event handlers
        $addHandler($get(lnkCallDealerSecurityInfoId), 'click', this.SecurityInfoClick);
        $addHandlers($get(lnkCallDealerCallButtonId), {click:this.InvokeCall}, this); 
        $addHandler($get(txtCallDealerAreaCodeId), 'keyup', this.AreaCodeKeyUp);
        $addHandler($get(txtCallDealerPrefixId), 'keyup', this.PrefixKeyUp);
        $addHandler($get(lnkCloseId), 'click', this.Close);
        $addHandlers($get(divCallDealerId), {keypress:this.CheckKey}, this); // This is set so if enter is pressed anywhere on the div, we make the call

        //Register our dispose function
        var delDispose = Function.createDelegate(this, this.Dispose);
        Sys.Application.add_unload(delDispose);
    }
    
    this.Dispose = function()
    {
        //Clean up handlers or other resources for the object
        $clearHandlers($get(lnkCallDealerSecurityInfoId));
        $clearHandlers($get(lnkCallDealerCallButtonId));
        $clearHandlers($get(txtCallDealerAreaCodeId));
        $clearHandlers($get(txtCallDealerPrefixId));
        $clearHandlers($get(divCallDealerId));
        $clearHandlers($get(lnkCloseId));
    }

	this.CheckKey = function(evt)
    {
        //Hide errors
        ClearErrBkg($get(txtCallDealerAreaCodeId));
        ClearErrBkg($get(txtCallDealerPrefixId));
        ClearErrBkg($get(txtCallDealerSuffixId));
        hideDIV(divCallDealerMissingFieldsId);
        hideDIV(divCallDealerInvalidDataId);
        
        //If the user hit enter, go for it
        if (mapKeyToAction(evt))
        { 
            this.InvokeCall();
        }
    }

    this.AreaCodeKeyUp = function()
    {
        
        //If the area code has 3 characters, set focus to the prefix
        if(this.value.length >= 3)
        {
            $get(txtCallDealerPrefixId).focus();
        }
    }

    this.PrefixKeyUp = function()
    {
        //If the prefix has 3 characters, set the focus to the phone number
        if(this.value.length >= 3)
        {
            $get(txtCallDealerSuffixId).focus();
        }
    }

    this.SecurityInfoClick = function()
    {
        //Hide the security information link and show the security info text
        showDIV(pCallDealerSecurityInfoId);
        hideDIV(lnkCallDealerSecurityInfoId);
    }

    this.Open = function()
    {
         //Make sure we aren't behind the background, possibly an outdated problem
         removeCssClass($get(divCallDealerId), "behindBkg"); 
        
        //Show alert background DIV and the call dealer DIV
        showShadowBox(divCallDealerId);
        
        //Give focus to the first textbox
        $get(txtCallDealerAreaCodeId).focus();
    }

    this.OpenEx = function(dlrId, dlrPhone, vehId, dlrName, dlrAddr)
    {
        //Set local variables
        this._dlrId = dlrId;
        this._dlrPhone = dlrPhone;
        this._vehId = vehId;
        
        //Set dealer name and address text
        $get(divCallDealerDealerNameId).innerHTML = dlrName;
        $get(divCallDealerDealerLocationId).innerHTML = dlrAddr;
        
        //Shows DIVs
        this.Open();
    }

    this.Close = function()
    {
        //Hide the alertBackground DIV and the Call dealer DIV
        hideShadowBox(divCallDealerId);
    }

    this.InvokeCall = function()
    {
        if(this.Validate())
        {
            //Close this popup
            this.Close();

            //Open "please wait" popup, should be displayed only briefly        
            ProgressVar.Open();

            //Construct phone number used for call
            var phoneNumber = $get(txtCallDealerAreaCodeId).value + 
                $get(txtCallDealerPrefixId).value + 
                $get(txtCallDealerSuffixId).value;

            //Make web service call to CallDealer(), which will set up the phone calls
            Reynolds.CL.Web.WebServices.VehicleFunctions.CallDealer(
                this._dlrId,
                this._dlrPhone,
                this._vehId,
                phoneNumber,
                WSKey,
                CallDealerSuccess, 
                CallDealerFailure, this);
        }
    }

    this.Validate = function()
    {
        var validated = false;
        
        //If prefix or phone number fields are blank, set the input field to orange and display
        // "required fields missing" in red below call dealer label
        var showRequiredMissing = false;
        if($get(txtCallDealerAreaCodeId).value.length < 3)
        {
            SetErrBkg($get(txtCallDealerAreaCodeId));
            showRequiredMissing = true;
        }
        if($get(txtCallDealerPrefixId).value.length < 3)
        {
            SetErrBkg($get(txtCallDealerPrefixId));
            showRequiredMissing = true;
        }
        if($get(txtCallDealerSuffixId).value.length < 4)
        {
            SetErrBkg($get(txtCallDealerSuffixId));
            showRequiredMissing = true;
        }
        if(showRequiredMissing) 
            showDIV(divCallDealerMissingFieldsId);

        //If there are non-numeric characters in any of the input fields, set the input field to 
        //orange and display "invalid data in fields" in red below the call dealer label
        var showInvalid = false;
        if(isNaN($get(txtCallDealerAreaCodeId).value))
        {
            SetErrBkg($get(txtCallDealerAreaCodeId));
            showInvalid = true;
        }
        if(isNaN($get(txtCallDealerPrefixId).value))
        {
            SetErrBkg($get(txtCallDealerPrefixId));
            showInvalid = true;
        }
        if(isNaN($get(txtCallDealerSuffixId).value))
        {
            SetErrBkg($get(txtCallDealerSuffixId));
            showInvalid = true;    
        }
        if(showInvalid)
            showDIV(divCallDealerInvalidDataId);
        
        //Validate
        if(!showRequiredMissing && !showInvalid) validated = true;
        
        return validated;
    }
}

//AJAX callback functions
function CallDealerSuccess(result, context)
{
    //Log the action with google analytics
    if(0 < context._waTrackAction.length)
    {
        fnWAPageView(context._waTrackAction);
    }
    
    //Show "now calling"    
    var phoneNumber = "(" + $get(txtCallDealerAreaCodeId).value + ") "
        + $get(txtCallDealerPrefixId).value + "-"
        + $get(txtCallDealerSuffixId).value;
    ProgressVar.SuccessfulComplete("Thank you!  Callbright is now calling " + phoneNumber + ".", 3000);
}

function CallDealerFailure(err, context)
{
    ProgressVar.FailureComplete("Error: " + err.get_message(), 3000);
}

//Wrapper functions, so we can always call these regardless of whether
//the actual javascript object has been created or not
function fnOpenCallDealer()
{
    if(CallDealerVar != undefined)
    {
        CallDealerVar.Open();
    }
}

function fnOpenCallDealerEx(dlrId, dlrPhone, vehId, dlrName, dlrAddr)
{
    if(CallDealerVar != undefined)
    {
        CallDealerVar.OpenEx(dlrId, dlrPhone, vehId, dlrName, dlrAddr);
    }

}
