var tempZip = "";
var map;
var geocoder = new GClientGeocoder();
var marker = null;
var baseIcon;
var GasVisibility;
var newStoreVisibility;
var HasDieselVisibility;
var HasE85Visibility;
var HasGulfCardVisibility;
var OverrideCardVisibility;
var ChillZoneVisibility;
var MarkerAddress;
var DirToThisStore = 1;
var DirFromThisStore = 0;
var BaloonTextPreAdd = "<div id='inside_panel'>" +
    	            "<div id='inside_address_information'>[ADDRESS1]" +
        	            "<p><a class='getdirections_link' href='#' onclick='FlipDivs()'>get directions</a></p>" +
                    "</div>" +
		            "<div class='green_copy' id='inside_available_services'>" +
        	            "<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
var BaloonTextPostAdd = ""; //"<tbody>";
var BaloonTextEndAdd = "<input type='hidden' id='MarkerAddress' value='[ADDRESS2]'> " +
                        "</tbody>" +
                        "</table>" +
                    "</div>" +
                "</div>" +
                "<div id='inside_panel2' style='display:none'>" +
    	            "<div id='inside_address_information2'>[ADDRESS3]" +
        	            "<p>get directions <a id='LinkToThisStore' href='#' style='font-weight:bold' onclick='SelectToThisStore()' >to this store</a> / <a id='LinkFromThisStore' href='#' onclick='SelectFromThisStore()'>from this store</a>.</p>" +
        	            "<p id='startingAddress'>starting Address:</p>" +
                    "</div>" +
                    "<div id='findastore_address_text_box'><input type='text' onkeypress='return GetDirections(event)' id='Address_box'></div>" +
                    "<div id='findastore_newwindow'><p>(opens in a new window)</p> <a onclick='return GetDrivingDirections()' class='getdirections_btn'>Get Directions</a></div>" +
    	        "</div>";


var ShowHideFeatures = "";

function FlipDivs() {
    document.getElementById("inside_panel").style.display = 'none';
    document.getElementById("inside_panel2").style.display = ''; 
}

function SelectFromThisStore() {
    DirFromThisStore = 1;
    DirToThisStore = 0;
    document.getElementById("LinkFromThisStore").style.fontWeight = "bold";
    document.getElementById("LinkToThisStore").style.fontWeight = "";
    document.getElementById("startingAddress").innerHTML = "ending Address:"  
}

function SelectToThisStore() {
    DirFromThisStore = 0;
    DirToThisStore = 1;
    document.getElementById("LinkFromThisStore").style.fontWeight = "";
    document.getElementById("LinkToThisStore").style.fontWeight = "bold";
    document.getElementById("startingAddress").innerHTML = "starting Address:"  
}

function showAddress(address, hasGas, hasDiesel, hase85, hasgulfcard, hasOverrider, hasChillzone, hasNsd, index) {

    var baloonHTML = "";
    BaloonTextPostAdd = "<tbody>";
   //Has Gas
    hasGas = hasGas.toLowerCase();
    var hasGasIndex = hasGas.indexOf('true');
    if (hasGasIndex >= 0)
    BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id='gas'>&nbsp;</div></td><td>Gas available</td></tr>";
    
    //Has diesel
    hasDiesel = hasDiesel.toLowerCase();
    var hasDieselIndex = hasDiesel.indexOf('true');
    if (hasDieselIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=diesel>&nbsp;</div></td><td>Diesel available</td></tr>";
    
    //Has e85
    hase85 = hase85.toLowerCase();
    var hase85Index = hase85.indexOf('true');
    if (hase85Index >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=e85>&nbsp;</div></td><td>E85 available</td></tr>";

    //Has gulf card
    hasgulfcard = hasgulfcard.toLowerCase();
    var hasgulfcardIndex = hasgulfcard.indexOf('true');
    if (hasgulfcardIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=gulfcard>&nbsp;</div></td><td>Gulf card accepted</td></tr>";

    //Has Override
    hasOverrider = hasOverrider.toLowerCase();
    var hasOverriderIndex = hasOverrider.indexOf('true');
    if (hasOverriderIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=override>&nbsp;</div></td><td>Override Card accepted</td></tr>";

    //Has Chill Zone
    hasChillzone = hasChillzone.toLowerCase();
    var hasChillzoneIndex = hasChillzone.indexOf('true');
    if (hasChillzoneIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=chillzone>&nbsp;</div></td><td>Chill Zone available</td></tr>";

    //Has new store design
    hasNsd = hasNsd.toLowerCase();
    var hasNsdIndex = hasNsd.indexOf('true');
    if (hasNsdIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=newstoredesign>&nbsp;</div></td><td>New Concept Store</td></tr>";


    baloonHTML = BaloonTextPreAdd + BaloonTextPostAdd + BaloonTextEndAdd;

    baloonHTML = baloonHTML.replace('[ADDRESS1]', address);
    baloonHTML = baloonHTML.replace('[ADDRESS2]', address);
    baloonHTML = baloonHTML.replace('[ADDRESS3]', address);

        
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  //alert(address + " not found");
              } else {
                  map.setCenter(point, 13);
                  marker = createMarker(point, address, hasGas, hasDiesel, hase85, hasgulfcard, hasOverrider, hasChillzone, hasNsd, index);
                  map.addOverlay(marker);
                  marker.openInfoWindow(baloonHTML);
              }
          }
        );
    }
    
}

function createMarker(point, address, hasGas, hasDiesel, hase85, hasgulfcard, hasOverrider, hasChillzone, hasNsd, index) {
    var baloonHTML = "";
    BaloonTextPostAdd = "<tbody>";
    //Has Gas
    hasGas = hasGas.toLowerCase();
    var hasGasIndex = hasGas.indexOf('true');
    if (hasGasIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id='gas'>&nbsp;</div></td><td>Gas available</td></tr>";

    //Has diesel
    hasDiesel = hasDiesel.toLowerCase();
    var hasDieselIndex = hasDiesel.indexOf('true');
    if (hasDieselIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=diesel>&nbsp;</div></td><td>Diesel available</td></tr>";

    //Has e85
    hase85 = hase85.toLowerCase();
    var hase85Index = hase85.indexOf('true');
    if (hase85Index >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=e85>&nbsp;</div></td><td>E85 available</td></tr>";

    //Has gulf card
    hasgulfcard = hasgulfcard.toLowerCase();
    var hasgulfcardIndex = hasgulfcard.indexOf('true');
    if (hasgulfcardIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=gulfcard>&nbsp;</div></td><td>Gulf card accepted</td></tr>";

    //Has Override
    hasOverrider = hasOverrider.toLowerCase();
    var hasOverriderIndex = hasOverrider.indexOf('true');
    if (hasOverriderIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=override>&nbsp;</div></td><td>Override Card accepted</td></tr>";

    //Has Chill Zone
    hasChillzone = hasChillzone.toLowerCase();
    var hasChillzoneIndex = hasChillzone.indexOf('true');
    if (hasChillzoneIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=chillzone>&nbsp;</div></td><td>Chill Zone available</td></tr>";

    //Has new store design
    hasNsd = hasNsd.toLowerCase();
    var hasNsdIndex = hasNsd.indexOf('true');
    if (hasNsdIndex >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=newstoredesign>&nbsp;</div></td><td>New Concept Store</td></tr>";


    baloonHTML = BaloonTextPreAdd + BaloonTextPostAdd + BaloonTextEndAdd;

    baloonHTML = baloonHTML.replace('[ADDRESS1]', address);
    baloonHTML = baloonHTML.replace('[ADDRESS2]', address);
    baloonHTML = baloonHTML.replace('[ADDRESS3]', address);

    var marker = new GMarker(point);

    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://maps.gstatic.com/intl/en_us/mapfiles/icon_green" + letter + ".png";

    // Set up our GMarkerOptions object 
    markerOptions = { icon: letteredIcon };
    marker = new GMarker(point, markerOptions);
    //map.addOverlay(marker);

    GEvent.addListener(marker, 'click', function() {
    	marker.openInfoWindowHtml(baloonHTML);
    });

    return marker;
}

function filterTable(term, _id, cellNr) {
    var suche = term.checked;
    //var suche = term;
    var table = document.getElementById(_id);
    var ele;
    for (var r = 1; r < table.rows.length; r++) {
        ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g, "");
        if (ele.toLowerCase().indexOf(suche) >= 0)
            table.rows[r].style.display = '';
        else table.rows[r].style.display = 'none';
    }
}

function FilterAddresses() {

    var ele;
    
    var rows = document.getElementById('zebra').rows;
    var visibleRows = rows.length;

    // i starts from 1 to skip table header row
    for (var i = 0; i < rows.length; i++) {

        //Check for chknewstoredesign
        if (document.getElementById('chknewstoredesign').checked == true) {
            ele = rows[i].cells[2].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }


        //Check for Gas
        if (document.getElementById('chkgas').checked == true) {
            ele = rows[i].cells[3].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }

        //Check for Diesel
        if (document.getElementById('chkdiesel').checked == true) {
            ele = rows[i].cells[4].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }

        //Check for chke85
        if (document.getElementById('chke85').checked == true) {
            ele = rows[i].cells[5].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }

        //Check for Gulf Card
        if (document.getElementById('chkgulfcard').checked == true) {
            ele = rows[i].cells[6].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }

        //Check for chkoverride
        if (document.getElementById('chkoverride').checked == true) {
            ele = rows[i].cells[7].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }


        //Check for chkchillzone
        if (document.getElementById('chkchillzone').checked == true) {
            ele = rows[i].cells[8].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf('true') >= 0) {
                rows[i].style.display = '';
            }
            else {
                rows[i].style.display = 'none';
                visibleRows = visibleRows - 1;
                continue;
            }
        }
        else {
            rows[i].style.display = '';
        }

    }

	if(document.getElementById("store_brown").innerHTML != "Sorry, we couldn't find any store matching your criteria.")
	{
		tempZip = document.getElementById("store_brown").innerHTML;
	}

    if (visibleRows == 0) {
        document.getElementById("store_brown").innerHTML = "Sorry, we couldn't find any store matching your criteria."
        document.getElementById('store_information error').style.display = '';
    }
    else {
        document.getElementById("store_brown").innerHTML = tempZip;
        document.getElementById('store_information error').style.display = 'none';
    }
}


function GetDirections(e) { //e is event object passed from function invocation
    var characterCode //literal character code will be stored in this variable

   if (e && e.which) { //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else {
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }

    if (characterCode == 13) {
        
        if (DirFromThisStore == 1) {
            directionURL = 'http://maps.google.com/maps?saddr=' + document.getElementById("MarkerAddress").value + '&daddr=' + document.getElementById("Address_box").value;
        }
        else {
            directionURL = directionURL = 'http://maps.google.com/maps?saddr=' + document.getElementById("Address_box").value + '&daddr=' + document.getElementById("MarkerAddress").value;
        }
        
        window.open(directionURL);
        
        return false
    }
    return true
}



function GetDrivingDirections(StoreAddress) {
    var directionURL;
    if (DirFromThisStore == 1) {
        directionURL = 'http://maps.google.com/maps?saddr=' + document.getElementById("MarkerAddress").value + '&daddr=' + document.getElementById("Address_box").value;
    }
    else {
        directionURL = directionURL = 'http://maps.google.com/maps?saddr=' + document.getElementById("Address_box").value + '&daddr=' + document.getElementById("MarkerAddress").value;
    }
    window.open(directionURL);
}

function ShowToFromDirectionLinks() {

    document.getElementById("tofromstores").style.display = '';
    document.getElementById("inside_available_services").style.display = 'none';
}





