﻿// This script file controls the client-side behavior for the main navigation
// control.

function controls_MainNavigation_showView( idOfViewToShow )
{

    // Figure out the control prefix.
    var parts = idOfViewToShow.split( '_' );
    
    parts.pop();
    
    var prefix = parts.join( '_' );
    
    // Hide all controls
    document.getElementById( prefix + '_searchView' ).style.display = 'none';
    document.getElementById( prefix + '_neighborhoodView' ).style.display = 'none';
    document.getElementById( prefix + '_realtorView' ).style.display = 'none';
    document.getElementById( prefix + '_contactView' ).style.display = 'none';
    
    // Show the desired control.
    document.getElementById( idOfViewToShow ).style.display = 'block';
}

// Returns all of the digits in inputString, without any of the other stuff.
function controls_MainNavigation_justTheDigits( inputString )
{
    var outputString = "";
    
    for( var i = 0; i < inputString.length; i++ )
    {
        var c = inputString.charAt( i );
        if( c >= '0' && c <= '9' ) { outputString += c; }
    }
    
    return outputString;
}

// Redirects the user's browser to the search page as defined by the options.
function controls_MainNavigation_doSearch( minPriceID, maxPriceID, amenitiesID, searchUrlBase )
{
    var minPrice = parseInt( controls_MainNavigation_justTheDigits( document.getElementById( minPriceID ).value ) );
    var maxPrice = parseInt( controls_MainNavigation_justTheDigits( document.getElementById( maxPriceID ).value ) );
    var amenities = document.getElementById( amenitiesID );
    
    var queryString = "";
    if( minPrice != NaN && minPrice > 0 ) { queryString += "&price-min=" + minPrice; }
    if( maxPrice != NaN && maxPrice > 0 ) { queryString += "&price-max=" + maxPrice; }
    if( amenities.value !== "" ) { queryString += "&amenities=" + amenities.value; }
    
    var searchUrl = searchUrlBase;
    if( queryString !== "" ) { searchUrl += "?" + queryString.substring( 1 ); }
    
    window.location.href = searchUrl;
}

// Redirects the user's browser to the selected subdivision page.
function controls_MainNavigation_gotoSubdivision( listBoxID )
{
    var listBox = document.getElementById( listBoxID );
    window.location.href = listBox.options[ listBox.selectedIndex ].value;
}

// Redirects the user's browser to the selected realtor page.
function controls_MainNavigation_gotoRealtor( listBoxID )
{
    var listBox = document.getElementById( listBoxID );
    window.location.href = listBox.options[ listBox.selectedIndex ].value;
}

// Redirects the user's browser to the contact page.
function controls_MainNavigation_gotoContact( contactType, urlBase )
{
    var firstName = document.getElementById( "nav_firstname" ).value;
    var lastName = document.getElementById( "nav_lastname" ).value;
    
    var url = urlBase + "&firstname=" + encodeURIComponent( firstName ) +
                        "&lastname=" + encodeURIComponent( lastName ) +
                        "&contacttype=" + encodeURIComponent( contactType );
                        
    window.location.href = url;
}