﻿/*

Created by:     Matt Bergstein
Date:           8/26/09
Description:    Used to operate functionality on the find-a-dealer.aspx page

*/

// This function will clear the text box if the default text exists or replace the default text if the text box is empty
function adjustTextBox(zipCodeTextBoxId)
{
    var zipCodeTextBox = document.getElementById(zipCodeTextBoxId);
    
    if (zipCodeTextBox.value == 'Enter ZIP Code' || zipCodeTextBox.value == 'Enter')
    {
        zipCodeTextBox.value = '';
    }
    else if (zipCodeTextBox.value == '')
    {
        zipCodeTextBox.value = 'Enter ZIP Code';
    }
}

//used to open a new window for the 2nd melody Bts dealer app
function openDealerLocator(zipCodeTextBoxId)
{   
    var errorMsg = document.getElementById('errorMsg');

    var zipCodeTextBox = document.getElementById(zipCodeTextBoxId);

    var zipCode = zipCodeTextBox.value;
    
    if (IsNumeric(zipCode) && (zipCode.length == 5))
    {
        var locatorURL =  " https://btsdealer.com/locator?zip_code=" + zipCode + "&search=";

        //var locatorURL =  "http://devel.btsdealer.com/locator2?zip_code=" + zipCode + "&search=";
        
        //var locatorURL =  "https://btsdealer.com/locator/search/zip_code/" + zipCode + "/go_search/true";

        window.open(locatorURL, "dealerLocator");
        
        errorMsg.style.display = 'none';
        
        return true;
    }
    else
    {
        errorMsg.style.display = 'block';
        
        return false;
    }
}

function IsNumeric(sValue)
{
    
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sValue.length && IsNumber == true; i++) 
    { 
        Char = sValue.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
   }
   return IsNumber;
}

