﻿var map;
var geocoder;
var iData = "0";
var interval = null;
var sinfo = [];
var sdata = [];
var fullList = [];
var scounter = 0;
var active = "0";
var iscookie = null;

function load() {
    if (GBrowserIsCompatible()) {
        document.getElementById('map').innerHTML = "";
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById('map'));
        map.addControl(new GSmallMapControl());
        //map.addControl(new GMapTypeControl());
        if (document.cookie.length > 0) {
            var start = document.cookie.indexOf("tfstore");
            if (start != -1) {
                getstorelocation();
            }
            else {
                map.setCenter(new GLatLng(40, -100), 4);
            }
        }
        else {
            map.setCenter(new GLatLng(40, -100), 4);
        }
        //document.getElementById('sidebar').innerHTML = "A search has not yet been performed";
    }
}

function getstorelocation() {
    // check for a cookie
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf("tfstore");
        if (start != -1) {
            var pos = 8;
            var poe = document.cookie.indexOf(";", pos);
            var cdata;
            if (poe == -1) {
                cdata = document.cookie.substring(pos);
            }
            else {
                cdata = document.cookie.substring(pos, poe);
            }

            // 0:id | 1:name | 2:address | 3:phone
            var sdata = cdata.split("|");
            //var address = sdata[2].replace("\n", " ");
            geocoder.getLatLng(sdata[2], function(latlng) {
                if (!latlng) {
                    alert("Google Maps is unable to locate the requested address");
                } else {
                    var point = new GLatLng(latlng.lat(), latlng.lng());
                    iscookie = sdata[0];
                    getLocation(sdata[0]);
                    map.setCenter(point, 9);
                    createMarker(point, sdata[0], sdata[1], sdata[2], sdata[3]);
                }
            });
        }
    }
}

function serverCallBack(query) {
    // for non-IE browsers
    if (window.XMLHttpRequest) {
        http_req = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        try {
            http_req = new ActiveXObject("Msxml12.XMLHTTP");
        }
        catch (e) {
            try {
                http_req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
            }
        }
    }
    // verify the request variable is set
    if (!http_req) {
        return false;
    }
    else {
        // continue processing
        http_req.onreadystatechange = function() {
            if (http_req.readyState == 4) {
                if (http_req.status == 200) {
                    iData = http_req.responseText;
                }
                else {
                    return false;
                }
            }
        }
        http_req.open('GET', query, true);
        http_req.send(null);
    }
}

function getAddress() {
    if ("" != document.getElementById('zip').value) {
        // check that they didn't just enter a state
        if (!stateCheck(document.getElementById('zip').value)) {
            document.getElementById("store_details").innerHTML = "";
            sdata = [];
            active = "0";
            var query = "dbutility.aspx?reqtype=zipcode&data=" + document.getElementById('zip').value;
            serverCallBack(query);
            interval = setInterval("showAddress()", 100);
        }
        else {
            alert("You must enter a city and state to continue");
        }
    }
    else {
        alert("A zipcode, city, or city and state must be entered");
    }
}

function showAddress() {
    //load();
    //var address = document.getElementById('zip').value;
    if ("0" != iData) {
        clearInterval(interval);
        var location = iData.split(':');
        iData = "0";
        var loczip = null;

        if (1 == location.length) {
            var locsplit = location[0].split(',');
            if (locsplit[2] != null) { loczip = locsplit[2]; }
            else { loczip = locsplit[0]; }
            map.clearOverlays();
            document.getElementById('TFStores').innerHTML = "";
            if (geocoder) {
                geocoder.getLatLng(
              loczip,
              function(point) {
                  if (!point) {
                      alert("The requested location could not be located");
                  } else {
                    getLocations(loczip);
                      map.setCenter(point, 9); 
                  }
              }
            );
            }
        }
        else {
            document.getElementById('useraction').innerHTML = "";
            
            var newoptions = null;
            for (var i = 0; i < location.length; i++) {
                var locsplit = location[i].split(',');
                if (null == newoptions) {
                    newoptions = "<option value='" + locsplit[2] + "' selected>" + locsplit[0] + ", " + locsplit[1] + "  " + locsplit[2] + "</option>";
                }
                else {
                    newoptions += "<option value='" + locsplit[2] + "'>" + locsplit[0] + ", " + locsplit[1] + "  " + locsplit[2] + "</option>";
                }
            }

            document.getElementById('useraction').innerHTML = "<span style='font-size:10px;'>Zip code or city, state<br /><select id='zipopts' style='font-size:10px;' onchange='javascript:addressOptions();'>" + newoptions + "</select>&nbsp;&nbsp;<input type='button' value='Go!' onclick='javascript:addressOptions();' />";
            alert("Please choose the correct state or zipcode for your query.");
        }
    }
}

function stateCheck(state) {
    var returnvalue = false;
    state = state.toLowerCase();
    if (state == "alabama" || state == "al") { returnvalue = true; }
    else if (state == "alaska" || state == "ak") { returnvalue = true; }
    else if (state == "arizona" || state == "az") { returnvalue = true; }
    else if (state == "arkansas" || state == "ar") { returnvalue = true; }
    else if (state == "california" || state == "ca") { returnvalue = true; }
    else if (state == "colorado" || state == "co") { returnvalue = true; }
    else if (state == "connecticut" || state == "ct") { returnvalue = true; }
    else if (state == "delaware" || state == "de") { returnvalue = true; }
    else if (state == "florida" || state == "fl") { returnvalue = true; }
    else if (state == "georgia" || state == "ga") { returnvalue = true; }
    else if (state == "hawaii" || state == "hi") { returnvalue = true; }
    else if (state == "idaho" || state == "id") { returnvalue = true; }
    else if (state == "illinois" || state == "il") { returnvalue = true; }
    else if (state == "indiana" || state == "in") { returnvalue = true; }
    else if (state == "iowa" || state == "ia") { returnvalue = true; }
    else if (state == "kansas" || state == "ks") { returnvalue = true; }
    else if (state == "kentucky" || state == "ky") { returnvalue = true; }
    else if (state == "louisiana" || state == "la") { returnvalue = true; }
    else if (state == "maine" || state == "me") { returnvalue = true; }
    else if (state == "maryland" || state == "md") { returnvalue = true; }
    else if (state == "massachusetts" || state == "ma") { returnvalue = true; }
    else if (state == "michigan" || state == "mi") { returnvalue = true; }
    else if (state == "minnesota" || state == "mn") { returnvalue = true; }
    else if (state == "missouri" || state == "mo") { returnvalue = true; }
    else if (state == "montana" || state == "mt") { returnvalue = true; }
    else if (state == "nebraska" || state == "ne") { returnvalue = true; }
    else if (state == "nevada" || state == "nv") { returnvalue = true; }
    else if (state == "new hampshire" || state == "nh") { returnvalue = true; }
    else if (state == "new jersey" || state == "nj") { returnvalue = true; }
    else if (state == "new mexico" || state == "nm") { returnvalue = true; }
    else if (state == "new york" || state == "ny") { returnvalue = true; }
    else if (state == "north carolina" || state == "nc") { returnvalue = true; }
    else if (state == "north dakota" || state == "nd") { returnvalue = true; }
    else if (state == "ohio" || state == "oh") { returnvalue = true; }
    else if (state == "oklahoma" || state == "ok") { returnvalue = true; }
    else if (state == "oregon" || state == "or") { returnvalue = true; }
    else if (state == "pennsylvania" || state == "pa") { returnvalue = true; }
    else if (state == "rhode island" || state == "ri") { returnvalue = true; }
    else if (state == "south carolina" || state == "sc") { returnvalue = true; }
    else if (state == "south dakota" || state == "sd") { returnvalue = true; }
    else if (state == "tennessee" || state == "tn") { returnvalue = true; }
    else if (state == "texas" || state == "tx") { returnvalue = true; }
    else if (state == "utah" || state == "ut") { returnvalue = true; }
    else if (state == "vermont" || state == "vt") { returnvalue = true; }
    else if (state == "virginia" || state == "va") { returnvalue = true; }
    else if (state == "washington" || state == "wa") { returnvalue = true; }
    else if (state == "west virginia" || state == "wv") { returnvalue = true; }
    else if (state == "wisconsin" || state == "wi") { returnvalue = true; }
    else if (state == "wyoming" || state == "wy") { returnvalue = true; }
    else if (state == "district of columbia" || state == "dc") { returnvalue = true; }
    return returnvalue;
}

function addressOptions() {
    map.clearOverlays();
    document.getElementById('TFStores').innerHTML = "";
    var zip = document.getElementById('zipopts').options[document.getElementById('zipopts').selectedIndex].value;
    if (geocoder) {
        geocoder.getLatLng(
              zip,
              function(point) {
                  if (!point) {
                      alert(zip + " not found");
                  } else {
                      map.setCenter(point, 9);
                      getLocations(zip);
                  }
              }
            );
          }
          document.getElementById('useraction').innerHTML = "<span style='font-size:10px;'>Zip code or city, state<br /><input type='text' id='zip' name='zip' style='color:#000000;font-size:10px;' onfocus='javascript:clearzip();' size='17' />&nbsp;&nbsp;<input type='button' value='Go!' onclick='javascript:getAddress();' />";
}

function getLocations(zipcode) {
    var query = "dbutility.aspx?reqtype=map&address=" + zipcode;
    serverCallBack(query);
    interval = setInterval("receiveLocations()", 100);
}
function getLocation(storeID) {
    var query = "dbutility.aspx?reqtype=store&id=" + storeID;
    serverCallBack(query);
    interval = setInterval("receiveLocation()", 100);
}
function receiveLocations() {
    if ("0" != iData) {
        clearInterval(interval);
        var locations = iData;
        iData = "0";

        if ("false" != locations && "" != locations) {
            var locFull = locations.split("|");

            for (var i = 0; i < locFull.length; i++) {
                var locData = locFull[i].split(";");

                // 0 - account number : 1 - name : 2 - address : 3 - phone : 4 - distance
                // 5 - services : 6 - hours : 7 - after hours phone : 8 - afterhours : 10 - service calls
                // 10,11,12 - additional services : 12 - url
                if ("" != locData[2]) {
                    searchLocations(locData[0], locData[1], locData[2], locData[3], locData[5], locData[6], locData[7], locData[8], locData[9], locData[10], locData[11], locData[12], locData[13], locData[4]);
                    fullList[i] = locData[0] + "," + locData[4];
                }
            }

            var display = "<table cellpadding='0' cellspacing='0' border='0' width='470'>";
            display += "<tr><td colspan='5'><select id='filter' name='filter' style='font-size:10px;' onchange='javascript:getFilteredLocations();'><option value='0'>Filter by service</option><option value='17'>Air Conditioning</option><option value='6'>Alignments</option><option value='4'>ATV Tire</option><option value='10'>Batteries</option><option value='9'>Brakes</option><option value='12'>Chassis</option><option value='15'>Custom Wheels</option><option value='23'>Customer Shuttle</option><option value='21'>Electrical Systems</option><option value='3'>Farm Tire</option><option value='14'>Full Engine Repair</option><option value='18'>Lift Kits</option><option value='19'>Lowering</option><option value='7'>Mufflers</option><option value='24'>Nitrogen Tire Fill</option><option value='5'>Off-Road/Mining Tire</option><option value='11'>Oil Change</option><option value='2'>Over the Road Truck Tire</option><option value='20'>Quick Lube</option><option value='1'>Passenger/Light Truck Tire</option><option value='8'>Shocks/Struts</option><option value='22'>Tire Siping</option><option value='16'>Transmission</option><option value='13'>Tune-Up</option></select></td>";
            display += "<tr><td height='5' colspan='5'><img src='images/spacer.gif' height='5' width='1'></td></tr>";
            
            // display the results in the info <div>
            for (var i = 0; i < sinfo.length; i++) {
                var storeinfo = sinfo[i].split(':');
                display += "<tr id='store_" + storeinfo[0] + "' onmouseover=\"javascript:bkgrnd('" + storeinfo[0] + "',1);\" onmouseout=\"javascript:bkgrnd('" + storeinfo[0] + "',0);\" onclick=\"javascript:showdetail('" + storeinfo[0] + "');\">";
                display += "<td width='10'><img src='images/spacer.gif' width='10' height='1'></td>";
                if (storeinfo[3] == "false") {
                    display += "<td><span><b>" + storeinfo[1] + "</b>";
                    if("" != storeinfo[4]) {
                        display += " (" + storeinfo[4] + " miles)</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://www.tirefactory.com/store/" + storeinfo[0] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                    }
                    else {
                        display += "</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://www.tirefactory.com/store/" + storeinfo[0] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                    }
                }
                else {
                    display += "<td><span><b>" + storeinfo[1] + "</b>";
                    if("" != storeinfo[4]) {
                        display += " (" + storeinfo[4] + " miles)</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://" + storeinfo[3] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                    }
                    else {
                        display += "</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://" + storeinfo[3] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                    }
                }
                //display += "<td><span><b>" + storeinfo[1] + "</b></span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td>&nbsp;</td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                display += "</tr>";
                display += "<tr><td height='5' colspan='5'><img src='images/spacer.gif' height='5' width='1'></td></tr>";
            }

            display += "</table>";

            document.getElementById('TFStores').innerHTML = display;

            sinfo.length = 0;
            scounter = 0;
        }
        else {
            alert("There are no Tire Factory locations within 50 miles");
        }
    }
}

function receiveLocation() {
    if ("0" != iData) {
        var selectedStore = iData.split(";");
        iData = "0";
        // 0 - name : 1 - address : 2 - phone : 3 - services
        // 4 - hours : 5 - after hours phone : 6 - afterhours : 7 - service calls
        // 8 - add. service 1 : 9 - add. service 2 : 10 - add. service 3

        // break apart the hours
        // 0:wdopen | 1:wdclose | 2:satopen | 3:satclose | 4:sunopen | 5:sunclose
        // the values are in seconds from midnight
        var storehours = selectedStore[4].split(',');
        var mfopen = String(((parseInt(storehours[0]) / 60) / 60)) + ":00";
        if (-1 != mfopen.indexOf(".")) { mfopen = mfopen.substr(0, 1) + ":30"; }
        var mfclose = String(((parseInt(storehours[1]) / 60) / 60) - 12);
        if (mfclose == "0") { mfclose = "12:00"; }
        else if (-1 != mfclose.indexOf(".")) {
            var pos = mfclose.indexOf(".");
            mfclose = mfclose.substr(0, pos) + ":30";
        }
        else {
            mfclose += ":00";
        }
        if ("0" != storehours[2]) {
            var satopen = String(Math.round(((parseInt(storehours[2]) / 60) / 60))) + ":00";
            if (-1 != satopen.indexOf(".")) { satopen = satopen.substr(0, 1) + ":30"; }
            var satclose = String(((parseInt(storehours[3]) / 60) / 60) - 12);
            if (satclose == "0") { satclose = "12:00"; }
            else if (-1 != satclose.indexOf(".")) {
                var pos = satclose.indexOf(".");
                satclose = satclose.substr(0, pos) + ":30";
            }
            else {
                satclose += ":00";
            }
        }
        else {
            satopen = "0";
            satclose = "0";
        }
        var sunopen, sunclose;
        if ("0" != storehours[4] && "43200" != storehours[4]) {
            sunopen = String(Math.round(((parseInt(storehours[4]) / 60) / 60))) + ":00";
            if (-1 != sunopen.indexOf(".")) { sunopen = sunopen.substr(0, 1) + ":30"; }
            sunclose = String(Math.round(((parseInt(storehours[5]) / 60) / 60) - 12));
            if (sunclose == "0") { sunclose = "12:00"; }
            else if (-1 != sunclose.indexOf(".")) {
                var pos = sunclose.indexOf(".");
                sunclose = sunclose.substr(0, (pos - 1)) + ":30";
            }
            else {
                sunclose += ":00";
            }
        }
        else {
            sunopen = "0";
            sunclose = "0";
        }
        // break apart the services variable
        var tfservices = selectedStore[3];
        var services = [];
        for (var i = 0; i < tfservices.length; i++) {
            services[i] = tfservices.substr(i, 1);
        }

        // 0 - name : 1 - address : 2 - phone : 3 - services
        // 4 - hours : 5 - after hours phone : 6 - afterhours : 7 - service calls
        // 8 - add. service 1 : 9 - add. service 2 : 10 - add. service 3

        // data that will display in the info window
        var storeinfo = "<table cellpadding='0' cellspacing='0' border='0' width='300'>";
        storeinfo += "<tr><td width='10' rowspan='34'><img src='images/spacer.gif' width='10' height='1'></td><td height='10' colspan='5'><img src='images/spacer.gif' height='10' width='1'></td></tr>";
        // name
        storeinfo += "<tr><td colspan='5'><span style='font-size:15px;'><b>" + selectedStore[0] + "</b></span><br><span style='font-size:12px;'><b>" + selectedStore[1] + "<br>" + selectedStore[2] + "</b></span></td></tr>";
        storeinfo += "<tr><td height='10' colspan='5'><img src='images/spacer.gif' height='10' width='1'></td></tr>";
        // hours
        storeinfo += "<tr><td colspan='5'>";
        storeinfo += "<span>M-F: " + mfopen + "am - " + mfclose + "pm<br>";
        if ("0" != satopen) {
            storeinfo += "Sat: " + satopen + "am - " + satclose + "pm<br>";
        }
        else {
            storeinfo += "Sat: <b>CLOSED</b><br>";
        }
        if ("0" != sunopen) {
            storeinfo += "Sun: " + mfopen + "am - " + mfclose + "pm";
        }
        else {
            storeinfo += "Sun: <b>CLOSED</b>";
        }
        if ("1" == selectedStore[6]) {
            storeinfo += "<br><img src='images/spacer.gif' height='5' width='1'><br><b>After Hours Phone: </b>" + selectedStore[5];
        }
        storeinfo += "</span></td></tr>";
        // services
        storeinfo += "<tr><td height='10' colspan='5'><img src='images/spacer.gif' height='10' width='1'></td></tr>";
        storeinfo += "<tr><td><span><b>Tire Factory Services</b></span></td></tr>";
        storeinfo += "<tr><td height='5' colspan='5'><img src='images/spacer.gif' height='5' width='1'></td></tr>";
        if ("Y" == services[0]) {
            storeinfo += "<tr><td><span>Passenger &amp; Light Truck Tire</span></td></tr>";
        }
        if ("Y" == services[1]) {
            storeinfo += "<tr><td><span>Over The Road Truck Tire</span></td></tr>";
        }
        if ("Y" == services[2]) {
            storeinfo += "<tr><td><span>Farm Tire</span></td></tr>";
        }
        if ("Y" == services[3]) {
            storeinfo += "<tr><td><span>ATV Tire</span></td></tr>";
        }
        if ("Y" == services[4]) {
            storeinfo += "<tr><td><span>Off-Road &amp; Mining Tire</span></td></tr>";
        }
        if ("Y" == services[5]) {
            storeinfo += "<tr><td><span>Alignments</span></td></tr>";
        }
        if ("Y" == services[6]) {
            storeinfo += "<tr><td><span>Mufflers</span></td></tr>";
        }
        if ("Y" == services[7]) {
            storeinfo += "<tr><td><span>Shocks &amp; Struts</span></td></tr>";
        }
        if ("Y" == services[8]) {
            storeinfo += "<tr><td><span>Brakes</span></td></tr>";
        }
        if ("Y" == services[9]) {
            storeinfo += "<tr><td><span>Batteries</span></td></tr>";
        }
        if ("Y" == services[10]) {
            storeinfo += "<tr><td><span>Oil Changes</span></td></tr>";
        }
        if ("Y" == services[11]) {
            storeinfo += "<tr><td><span>Chassis</span></td></tr>";
        }
        if ("Y" == services[12]) {
            storeinfo += "<tr><td><span>Tune-up</span></td></tr>";
        }
        if ("Y" == services[13]) {
            storeinfo += "<tr><td><span>Full Engine Repair</span></td></tr>";
        }
        if ("Y" == services[14]) {
            storeinfo += "<tr><td><span>Custom Wheels</span></td></tr>";
        }
        if ("Y" == services[15]) {
            storeinfo += "<tr><td><span>Transmission</span></td></tr>";
        }
        if ("Y" == services[16]) {
            storeinfo += "<tr><td><span>Air Conditioning</span></td></tr>";
        }
        if ("Y" == services[17]) {
            storeinfo += "<tr><td><span>Lift Kits</span></td></tr>";
        }
        if ("Y" == services[18]) {
            storeinfo += "<tr><td><span>Lowering</span></td></tr>";
        }
        if ("Y" == services[19]) {
            storeinfo += "<tr><td><span>Quick Lube</span></td></tr>";
        }
        if ("Y" == services[20]) {
            storeinfo += "<tr><td><span>Electrical Systems</span></td></tr>";
        }
        if ("Y" == services[21]) {
            storeinfo += "<tr><td><span>Tire Siping</span></td></tr>";
        }
        if ("Y" == services[22]) {
            storeinfo += "<tr><td><span>Customer Shuttle</span></td></tr>";
        }
        if ("Y" == services[23]) {
            storeinfo += "<tr><td><span>Nitrogen Tire Fill</span></td></tr>";
        }
        if ("" != selectedStore[8] && "false" != selectedStore[8]) {
            var addserv = selectedStore[8].replace(/&/, "&amp;");
            storeinfo += "<tr><td><span>" + addserv + "</span></td></tr>";
        }
        if ("" != selectedStore[9] && "false" != selectedStore[9]) {
            var addserv = selectedStore[9].replace(/&/, "&amp;");
            storeinfo += "<tr><td><span>" + addserv + "</span></td></tr>";
        }
        if ("" != selectedStore[10] && "false" != selectedStore[10]) {
            var addserv = selectedStore[10].replace(/&/, "&amp;");
            storeinfo += "<tr><td><span>" + addserv + "</span></td></tr>";
        }
        storeinfo += "</table>";
        document.getElementById("store_details").innerHTML = storeinfo;
    }
}

function getFilteredLocations() {
    var select = document.getElementById('filter').selectedIndex;
    if ("0" != select) {
        var filter = document.getElementById('filter').options(document.getElementById('filter').selectedIndex).value;

        var accts = "";
        for (var i = 0; i < fullList.length; i++) {
            if (accts == "") {
                accts = filter + ";" + fullList[i];
            }
            else {
                accts += ";" + fullList[i];
            }
        }

        var query = "dbutility.aspx?reqtype=filter&accts=" + accts;
        serverCallBack(query);
        interval = setInterval("setFilteredLocations()", 100);
    }
    else {
        alert("Please select a valid option");
    }
}
function setFilteredLocations() {
    if ("0" != iData) {
        clearInterval(interval);
        var locations = iData;
        iData = "0";
        var locFull = locations.split("|");

        map.clearOverlays();
        document.getElementById('TFStores').innerHTML = "";

        for (var i = 0; i < locFull.length; i++) {
            var locData = locFull[i].split(";");

            // 0 - account number : 1 - name : 2 - address : 3 - phone :
            // 4 - services : 5 - hours : 6 - after hours phone : 7 - afterhours : 8 - service calls
            // 9,10,11 - additional services : 12 - url : 13 - distance
            if ("" != locData[2]) {
                searchLocations(locData[0], locData[1], locData[2], locData[3], locData[4], locData[5], locData[6], locData[7], locData[8], locData[9], locData[10], locData[11], locData[12], locData[13]);
            }
        }

        var display = "<table cellpadding='0' cellspacing='0' border='0' width='470'>";
        display += "<tr><td colspan='5'><select id='filter' name='filter' style='font-size:10px;' onchange='javascript:getFilteredLocations();'><option value='0'>Filter by service</option><option value='17'>Air Conditioning</option><option value='6'>Alignments</option><option value='4'>ATV Tire</option><option value='10'>Batteries</option><option value='9'>Brakes</option><option value='12'>Chassis</option><option value='15'>Custom Wheels</option><option value='23'>Customer Shuttle</option><option value='21'>Electrical Systems</option><option value='3'>Farm Tire</option><option value='14'>Full Engine Repair</option><option value='18'>Lift Kits</option><option value='19'>Lowering</option><option value='7'>Mufflers</option><option value='24'>Nitrogen Tire Fill</option><option value='5'>Off-Road/Mining Tire</option><option value='11'>Oil Change</option><option value='2'>Over the Road Truck Tire</option><option value='20'>Quick Lube</option><option value='1'>Passenger/Light Truck Tire</option><option value='8'>Shocks/Struts</option><option value='22'>Tire Siping</option><option value='16'>Transmission</option><option value='13'>Tune-Up</option></select></td>";
        display += "<tr><td height='5' colspan='5'><img src='images/spacer.gif' height='5' width='1'></td></tr>";

        // display the results in the info <div>
        for (var i = 0; i < sinfo.length; i++) {
            var storeinfo = sinfo[i].split(':');
            display += "<tr id='store_" + storeinfo[0] + "' onmouseover=\"javascript:bkgrnd('" + storeinfo[0] + "',1);\" onmouseout=\"javascript:bkgrnd('" + storeinfo[0] + "',0);\" onclick=\"javascript:showdetail('" + storeinfo[0] + "');\">";
            display += "<td width='10'><img src='images/spacer.gif' width='10' height='1'></td>";
            if (storeinfo[3] == "false") {
                display += "<td><span><b>" + storeinfo[1] + "</b>";
                if ("" != storeinfo[4]) {
                    display += " (" + storeinfo[4] + " miles)</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://www.tirefactory.com/store/" + storeinfo[0] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                }
                else {
                    display += "</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://www.tirefactory.com/store/" + storeinfo[0] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                }
            }
            else {
                display += "<td><span><b>" + storeinfo[1] + "</b>";
                if("" != storeinfo[4]) {
                    display += " (" + storeinfo[4] + " miles)</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://" + storeinfo[3] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                }
                else {
                    display += "</span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td><a href='http://" + storeinfo[3] + "'>website</a></td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
                }
            }
            //display += "<td><span><b>" + storeinfo[1] + "</b></span></td><td width='10'><img src='images/spacer.gif' width='10' height='1'></td><td>&nbsp;</td><td><a href=\"javascript:showdetail('" + storeinfo[0] + "');\">details</a></td>";
            display += "</tr>";
            display += "<tr><td height='5' colspan='5'><img src='images/spacer.gif' height='5' width='1'></td></tr>";
        }

        display += "</table>";

        document.getElementById('TFStores').innerHTML = display;

        sinfo.length = 0;
        scounter = 0;
    }
}
function searchLocations(id, name, address, phone, services, hours, ahphone, ah, servcalls, add1, add2, add3, url, distance) {

    geocoder.getLatLng(address, function(latlng) {
        if (!latlng) {
        } else {
            var point = new GLatLng(latlng.lat(), latlng.lng());
            createMarker(point, id, name, address, phone);
        }
    });
    sdata[id] = name + ":" + address + ":" + phone + ":" + services + ":" + hours + ":" + ahphone + ":" + ah + ":" + servcalls + ":" + add1 + ":" + add2 + ":" + add3 + ":" + url;
    sinfo[scounter] = id + ":" + name + ":" + address + ":" + url + ":" + distance;

    scounter++;
}
function createMarker(point, id, name, address, phone) {
    var html;
    var marker = new GMarker(point);
    map.addOverlay(marker);
    address = address.replace("\n", " ");
    var map_address = address.replace(/ /g, "+");
    // this is necessary because the browser converts &#39; into ' in the link (default_loc) when
    // the page is rendered. Seriously pisses me off.
    var sname = name.replace("'", "[]");
    var default_loc = id + "|" + sname + "|" + address + "|" + phone;
    name = name.replace("'", "&#39;");
    name = name.replace("[]", "&#39;");
    if (iscookie == id) {
        html = "<b>" + name + "</b><br>" + address + "<br>" + phone + "<br><br><a href='http://maps.google.com/maps?f=d&hl=en&daddr=" + map_address + "' target='_blank'>get directions</a>";
    }
    else {
        html = "<b>" + name + "</b><br>" + address + "<br>" + phone + "<br><br><a href=\"javascript:setDefault('" + default_loc + "');\">make this my default store</a><br><a href='http://maps.google.com/maps?f=d&hl=en&daddr=" + map_address + "' target='_blank'>get directions</a>";
    }
    GEvent.addListener(marker, 'mouseover', function() { marker.openInfoWindowHtml(html); });
    //GEvent.addListener(marker, 'mouseout', function() { marker.closeInfoWindow(); });
    //GEvent.addListener(marker, 'onClick', function() { showdetail(id); });  
}

function setDefault(val) {
    var date = new Date();
    date.setTime(date.getTime() + (1825 * 24 * 60 * 60 * 1000));
    document.cookie = "tfstore=" + val + ";expires=" + date.toGMTString() + ";path=/";
    var sdata = val.split("|");
    var name = sdata[1].replace("[]", "'");
    alert(name + " is now your default Tire Factory store");     
}

//function clearzip() {
//    document.getElementById('zip').value = "";
//    document.getElementById('zip').style.color = "#000000";
//}

function bkgrnd(id, show) {
    if (active != id) {
        if (0 == show) {
            document.getElementById("store_" + id).style.backgroundColor = "";
        }
        else if (1 == show) {
            document.getElementById("store_" + id).style.backgroundColor = "cecece";
        }
    }
}

function showdetail(id) {

    if (active != "0") {
        document.getElementById("store_" + active).style.backgroundColor = "";
        document.getElementById("store_" + id).style.backgroundColor = "cecece";
        active = id;
    }
    else {
        document.getElementById("store_" + id).style.backgroundColor = "cecece";
        active = id;
    }

    var selectedStore = sdata[id].split(":");
    // 0 - name : 1 - address : 2 - phone : 3 - services
    // 4 - hours : 5 - after hours phone : 6 - afterhours : 7 - service calls
    // 8 - add. service 1 : 9 - add. service 2 : 10 - add. service 3

    // break apart the hours
    // 0:wdopen | 1:wdclose | 2:satopen | 3:satclose | 4:sunopen | 5:sunclose
    // the values are in seconds from midnight
    var storehours = selectedStore[4].split(',');
    var mfopen = String(((parseInt(storehours[0]) / 60) / 60)) + ":00";
    if (-1 != mfopen.indexOf(".")) { mfopen = mfopen.substr(0, 1) + ":30"; }
    var mfclose = String(((parseInt(storehours[1]) / 60) / 60) - 12);
    if (mfclose == "0") { mfclose = "12:00"; }
    else if (-1 != mfclose.indexOf(".")) {
        var pos = mfclose.indexOf(".");
        mfclose = mfclose.substr(0,pos) + ":30";
    }
    else {
        mfclose += ":00";
    }
    if ("0" != storehours[2]) {
        var satopen = String(Math.round(((parseInt(storehours[2]) / 60) / 60))) + ":00";
        if (-1 != satopen.indexOf(".")) { satopen = satopen.substr(0, 1) + ":30"; }
        var satclose = String(((parseInt(storehours[3]) / 60) / 60) - 12);
        if (satclose == "0") { satclose = "12:00"; }
        else if (-1 != satclose.indexOf(".")) {
            var pos = satclose.indexOf(".");
            satclose = satclose.substr(0,pos) + ":30";
        }
        else {
            satclose += ":00";
        }
    }
    else {
        satopen = "0";
        satclose = "0";
    }
    var sunopen, sunclose;
    if ("0" != storehours[4] && "43200" != storehours[4]) {
        sunopen = String(Math.round(((parseInt(storehours[4]) / 60) / 60))) + ":00";
        if (-1 != sunopen.indexOf(".")) { sunopen = sunopen.substr(0, 1) + ":30"; }
        sunclose = String(Math.round(((parseInt(storehours[5]) / 60) / 60) - 12));
        if (sunclose == "0") { sunclose = "12:00"; }
        else if (-1 != sunclose.indexOf(".")) {
            var pos = sunclose.indexOf(".");
            sunclose = sunclose.substr(0, (pos - 1)) + ":30";
        }
        else {
            sunclose += ":00";
        }
    }
    else {
        sunopen = "0";
        sunclose = "0";
    }
    // break apart the services variable
    var tfservices = selectedStore[3];
    var services = [];
    for (var i = 0; i < tfservices.length; i++) {
        services[i] = tfservices.substr(i, 1);
    }

    // 0 - name : 1 - address : 2 - phone : 3 - services
    // 4 - hours : 5 - after hours phone : 6 - afterhours : 7 - service calls
    // 8 - add. service 1 : 9 - add. service 2 : 10 - add. service 3

    // data that will display in the info window
    var storeinfo = "<table cellpadding='0' cellspacing='0' border='0' width='300'>";
    storeinfo += "<tr><td width='10' rowspan='34'><img src='images/spacer.gif' width='10' height='1'></td><td height='10' colspan='5'><img src='images/spacer.gif' height='10' width='1'></td></tr>";
    // name
    storeinfo += "<tr><td colspan='5'><span style='font-size:15px;'><b>" + selectedStore[0] + "</b></span><br><span style='font-size:12px;'><b>" + selectedStore[1] + "<br>" + selectedStore[2] + "</b></span></td></tr>";
    storeinfo += "<tr><td height='10' colspan='5'><img src='images/spacer.gif' height='10' width='1'></td></tr>";
    // hours
    storeinfo += "<tr><td colspan='5'>";
    storeinfo += "<span>M-F: " + mfopen + "am - " + mfclose + "pm<br>";
    if ("0" != satopen) {
        storeinfo += "Sat: " + satopen + "am - " + satclose + "pm<br>";
    }
    else {
        storeinfo += "Sat: <b>CLOSED</b><br>";
    }
    if ("0" != sunopen) {
        storeinfo += "Sun: " + mfopen + "am - " + mfclose + "pm";
    }
    else {
        storeinfo += "Sun: <b>CLOSED</b>";
    }
    if ("1" == selectedStore[6]) {
        storeinfo += "<br><img src='images/spacer.gif' height='5' width='1'><br><b>After Hours Phone: </b>" + selectedStore[5];
    }
    storeinfo += "</span></td></tr>";
    // services
    storeinfo += "<tr><td height='10' colspan='5'><img src='images/spacer.gif' height='10' width='1'></td></tr>";
    storeinfo += "<tr><td><span><b>Tire Factory Services</b></span></td></tr>";
    storeinfo += "<tr><td height='5' colspan='5'><img src='images/spacer.gif' height='5' width='1'></td></tr>";
    if ("Y" == services[0]) {
        storeinfo += "<tr><td><span>Passenger &amp; Light Truck Tire</span></td></tr>";
    }
    if ("Y" == services[1]) {
        storeinfo += "<tr><td><span>Over The Road Truck Tire</span></td></tr>";
    }
    if ("Y" == services[2]) {
        storeinfo += "<tr><td><span>Farm Tire</span></td></tr>";
    }
    if ("Y" == services[3]) {
        storeinfo += "<tr><td><span>ATV Tire</span></td></tr>";
    }
    if ("Y" == services[4]) {
        storeinfo += "<tr><td><span>Off-Road &amp; Mining Tire</span></td></tr>";
    }
    if ("Y" == services[5]) {
        storeinfo += "<tr><td><span>Alignments</span></td></tr>";
    }
    if ("Y" == services[6]) {
        storeinfo += "<tr><td><span>Mufflers</span></td></tr>";
    }
    if ("Y" == services[7]) {
        storeinfo += "<tr><td><span>Shocks &amp; Struts</span></td></tr>";
    }
    if ("Y" == services[8]) {
        storeinfo += "<tr><td><span>Brakes</span></td></tr>";
    }
    if ("Y" == services[9]) {
        storeinfo += "<tr><td><span>Batteries</span></td></tr>";
    }
    if ("Y" == services[10]) {
        storeinfo += "<tr><td><span>Oil Changes</span></td></tr>";
    }
    if ("Y" == services[11]) {
        storeinfo += "<tr><td><span>Chassis</span></td></tr>";
    }
    if ("Y" == services[12]) {
        storeinfo += "<tr><td><span>Tune-up</span></td></tr>";
    }
    if ("Y" == services[13]) {
        storeinfo += "<tr><td><span>Full Engine Repair</span></td></tr>";
    }
    if ("Y" == services[14]) {
        storeinfo += "<tr><td><span>Custom Wheels</span></td></tr>";
    }
    if ("Y" == services[15]) {
        storeinfo += "<tr><td><span>Transmission</span></td></tr>";
    }
    if ("Y" == services[16]) {
        storeinfo += "<tr><td><span>Air Conditioning</span></td></tr>";
    }
    if ("Y" == services[17]) {
        storeinfo += "<tr><td><span>Lift Kits</span></td></tr>";
    }
    if ("Y" == services[18]) {
        storeinfo += "<tr><td><span>Lowering</span></td></tr>";
    }
    if ("Y" == services[19]) {
        storeinfo += "<tr><td><span>Quick Lube</span></td></tr>";
    }
    if ("Y" == services[20]) {
        storeinfo += "<tr><td><span>Electrical Systems</span></td></tr>";
    }
    if ("Y" == services[21]) {
        storeinfo += "<tr><td><span>Tire Siping</span></td></tr>";
    }
    if ("Y" == services[22]) {
        storeinfo += "<tr><td><span>Customer Shuttle</span></td></tr>";
    }
    if ("Y" == services[23]) {
        storeinfo += "<tr><td><span>Nitrogen Tire Fill</span></td></tr>";
    }
    if ("" != selectedStore[8] && "false" != selectedStore[8]) {
        var addserv = selectedStore[8].replace(/&/, "&amp;");
        storeinfo += "<tr><td><span>" + addserv + "</span></td></tr>";
    }
    if ("" != selectedStore[9] && "false" != selectedStore[9]) {
        var addserv = selectedStore[9].replace(/&/, "&amp;");
        storeinfo += "<tr><td><span>" + addserv + "</span></td></tr>";
    }
    if ("" != selectedStore[10] && "false" != selectedStore[10]) {
        var addserv = selectedStore[10].replace(/&/, "&amp;");
        storeinfo += "<tr><td><span>" + addserv + "</span></td></tr>";
    }
    storeinfo += "</table>";
    document.getElementById("store_details").innerHTML = storeinfo;
}