// 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 fnOpenEmailVehicle()
{
    if(!javaLoaded){return false;}

    EmailVehicleVar.Open();
    return false;
}


function EmailVehicle()
{
	this.Init = function(id)
	{
	    this._clientId = id;
	    this._vehId = 0;
	    this._waTrackAction = "";
	    
	    // Add handlers so we can call functions associated with this object.
        $addHandlers($get('eClose'), {click:this.Close}, this);
        $addHandlers($get(this._clientId + '_aSend'), {click:this.Send}, this);
        // Handlers for key press to map "enter" to the send button.
        $addHandlers($get("divEmailPopupInner"), {keypress:this.CheckKey}, 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('eClose'));
        $clearHandlers($get(this._clientId + '_aSend'));
        
        $clearHandlers($get("divEmailPopup"));
	}
	
	this.CheckKey = function(evt)
    {
        // evt is a Sys.UI.DomEvent object
        if (mapKeyToAction(evt))
        { 
            this.Send();
            setFixValidation();
        }
    }

    this.Open = function()
    {
        showShadowBox('divEmailPopup');
        setFocus($get(this._clientId + '_txtToEmail'));
        
        return false;
    }
    
    this.Close = function()
    {
        hideShadowBox('divEmailPopup');
        
        return false;
    }

    this.Send = function()
    {
        // redefine the scroll to function because someone at ms decided it was a good idea to 
        // force the page to scroll all the way back up when clientside validation fails
//        var saveScroll = window.scrollTo;
//        window.scrollTo = tempScroll; //function( x,y ) { return true; }
    
        if(this.Validate())
        {
//        debugger;
//        alert("test");
            Reynolds.CL.Web.WebServices.VehicleFunctions.EmailVehicle(this._vehId, 
                $get(this._clientId + "_txtFromEmail").value, $get(this._clientId + "_txtToEmail").value, 
                $get(this._clientId + "_txtName").value,
                $get(this._clientId + "_txtMessage").value, 
                $get(this._clientId + "_cbSendSelf").checked,
                WSKey,
                EmailVehicleSuccess, 
                EmailVehicleFailure, this);
        
            this.Close();
            
            // Show the progress popup.
            ProgressVar.Open();
        }
        
//        window.scrollTo = saveScroll;
        return false;
    }
    
    function EmailVehicleSuccess(result, context)
    {
        if(0 < context._waTrackAction.length)
        {
            fnWAPageView(context._waTrackAction);
        }
    
        ProgressVar.SuccessfulComplete(result, 3000);
    } 

    function EmailVehicleFailure(err, context)
    {
        ProgressVar.FailureComplete(err.get_message(), 3000);
    }
    
    this.Validate = function()
    {
	    valid = true;
	    var elm;
	    var elmFocus = null;
	    
	    // Clear the error messages.
   	    addCssClass($get(this._clientId + "_everrReq"), "hide");
   	    addCssClass($get(this._clientId + "_everrFormat"), "hide");	    

	    // Check the To Email value.
	    elm = $get(this._clientId + "_txtToEmail");
	    ClearErrBkg(elm);
	    if(0 == elm.value.length)
	    {
	        valid = false;
	        removeCssClass($get(this._clientId + "_everrReq"), "hide");
	        SetErrBkg(elm);
            if(!elmFocus){elmFocus = elm;}
	    }
	    else if (!g_regexEmail.test(elm.value)) 
        {
	        valid = false;
	        removeCssClass($get(this._clientId + "_everrFormat"), "hide");
	        SetErrBkg(elm);
            if(!elmFocus){elmFocus = elm;}
        }
	    
	    // Check the From Email value.
	    elm = $get(this._clientId + "_txtFromEmail");
	    ClearErrBkg(elm);
	    if(0 == elm.value.length)
	    {
	        valid = false;
	        removeCssClass($get(this._clientId + "_everrReq"), "hide");
	        SetErrBkg(elm);
            if(!elmFocus){elmFocus = elm;}
	    }
	    else if (!g_regexEmail.test(elm.value)) 
	    {
	        valid = false;
	        removeCssClass($get(this._clientId + "_everrFormat"), "hide");
	        SetErrBkg(elm);
            if(!elmFocus){elmFocus = elm;}
	    }
	    
	    if(!valid)
	    {
	        // If there was a validation error, be sure the error messages are setup. We don't include them in the 
	        // HTML because we don't want these words high on the Search Engine content rankings.
            $get(this._clientId + "_everrReq").innerHTML = "Required field(s) missing.";
            $get(this._clientId + "_everrFormat").innerHTML = "Invalid format on field(s).";
	    
            setFocus(elmFocus);
	    }
	    
	    return valid;
    }
    
}

//function tempScroll(x, y)
//{ return true; }

