﻿/*************************************************************
// Comments: Javascript class for the location object
*************************************************************/
       
    var LOCATION_TYPE_ACCOUNT = 0;
    var LOCATION_TYPE_SEARCH = 1;
    
    
    function Location()
    {
        // Nothing here as all instances prototyped.
    }
    
    
    // Event handlers.
    Location.prototype.Load = Location_OnLoad;
    
    // Validation properties.
    Location.prototype.IsValid = false;
    Location.prototype.ErrorMessage = "";
    
    Location.prototype.LocationUseType = false;
    
    // Control ids.
    Location.prototype.CityControlId = "";
    Location.prototype.StateListControlId = "";
    Location.prototype.ZipControlId = "";
    Location.prototype.RadiusControlId = "";
        
    Location.prototype.CityErrorControlId = "txtCityErrorUI";
    Location.prototype.StateListErrorControlId = "lstStateErrorUI";
    Location.prototype.ZipErrorControlId = "txtZipErrorUI";    
    
    Location.prototype.Validate = Location_Validate;
    Location.prototype.DisplayErrorUI = Location_DisplayErrorUI;
    Location.prototype.SetError = Location_SetError;
    Location.prototype.ValidateForAccount = Location_ValidateForAccount;
    Location.prototype.ValidateForSearch = Location_ValidateForSearch;
    
    
    ///<summary>Initializes the UI.</summary>
    function Location_OnLoad()
    {   
    }
    
    
    function Location_Validate()
    {
        if( this.LocationUseType == LOCATION_TYPE_ACCOUNT )
        {
            this.ValidateForAccount();
        }
        else
        {
            this.ValidateForSearch();
        }
    }
    
    
    function Location_ValidateForSearch()
    {        
        
    }
    
    
    ///<summary>The error span tag around the fields are named 
    /// with the name
    ///</summary>
    function Location_ValidateForAccount()
    {
        this.IsValid = true;
        this.SetError(this.CityErrorControlId, "", false);
        this.SetError(this.ZipErrorControlId, "", false);
        this.SetError(this.StateListErrorControlId, "", false);
        
        var zip = document.getElementById(this.ZipControlId).value;
        
        // Make sure that the city, state & zip is not empty.
        if(document.getElementById(this.CityControlId).value == "")
        {
           this.SetError(this.CityErrorControlId, "City field is required", true);
           this.IsValid = false;
        }
        if( zip == "")
        {
            this.SetError(this.ZipErrorControlId, "Zip field is required", true);
            this.IsValid = false; 
        }
        else if( !isInteger(zip))
        {
            this.SetError(this.ZipErrorControlId, "Zip field is invalid", true);
            this.IsValid = false; 
        }
        
        var stateVal = document.getElementById(this.StateListControlId).value;
        if( stateVal == "" || stateVal == "0")
        {
            this.SetError(this.StateListErrorControlId, "State field is required", true);
            this.IsValid = false;
        }
    }
    
    
    function Location_SetError(controlId, errorMessage, hasError)
    {
        this.DisplayErrorUI(controlId, hasError);
        this.ErrorMessage = errorMessage;
    }
    
    
    ///<summary>Displays the red border around the control</summary>
    function Location_DisplayErrorUI(errorUIControlId, hasError)
    {
       var visibility = 'hidden';
       if(hasError) { visibility = 'visible'; }
       
       var errorUIControl = document.getElementById(errorUIControlId);  
       errorUIControl.style.visibility = visibility;
       errorUIControl.style.display = 'block';
    } 
    
