var tempZip = "";
var map;
var geocoder = new GClientGeocoder();
var marker = null;
var baseIcon;
var MarkerAddress;
var DirToThisStore = 1;
var DirFromThisStore = 0;
var BaloonTextPreAdd =
"<div id='inside_panel'>" +
    "<div id='inside_address_information'>" +
        "[ADDRESS1]" +
        "<p>get directions<br/><a id='LinkToThisStore' href='#' style='font-weight:bold' onclick='SelectToThisStore()' >to</a> / <a id='LinkFromThisStore' href='#' onclick='SelectFromThisStore()'>from</a> this store.</p>" +
        "<p id='startingAddress'>Starting Address:</p>" +
        "<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>" +
    "<div class='green_copy' id='inside_available_services'>" +
        "<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
var BaloonTextPostAdd = "";
var BaloonTextEndAdd =
                "<input type='hidden' id='MarkerAddress' value='[ADDRESS2]'> " +
            "</tbody>" +
        "</table>" +
    "</div>" +
"</div>";
var ShowHideFeatures = "";


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, hasChillzone, hasNsd, index, istwentyfourhours, hasCoffee, hasATM, isCampaign) {

    var baloonHTML = "";
    BaloonTextPostAdd = "<tbody>";
    
    //Has Gas
    if (hasGas.toLowerCase().indexOf('true') >= 0)
    BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id='gas'>&nbsp;</div></td><td>Gas available</td></tr>";
    
    //Has diesel
    if (hasDiesel >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=diesel>&nbsp;</div></td><td>Diesel available</td></tr>";
    
    //Has e85
    if (hase85.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=e85>&nbsp;</div></td><td>E85 available</td></tr>";

    //Has gulf card
    if (hasgulfcard.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=gulfcard>&nbsp;</div></td><td>Gulf Card accepted</td></tr>";

    //Has Chill Zone
    if (hasChillzone.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=chillzone>&nbsp;</div></td><td>Chill Zone available</td></tr>";

    //Has new store design
    if (hasNsd.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=newstoredesign>&nbsp;</div></td><td>New Concept Store</td></tr>";

    //Has 24 hours
    if (istwentyfourhours.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=open24hours>&nbsp;</div></td><td>24 Hours</td></tr>";

    //Has coffee
    if (hasCoffee.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=coffee>&nbsp;</div></td><td>Cumberland Farms Coffee</td></tr>";

    //Has atm
    if (hasATM.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=atm>&nbsp;</div></td><td>Citizen's Bank ATM</td></tr>";

    //Has campaign
    if (isCampaign.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=campaign>&nbsp;</div></td><td>Pediatric Care Campaign</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, hasChillzone, hasNsd, index, istwentyfourhours, hasCoffee, hasATM, isCampaign);
                  map.addOverlay(marker);
                  marker.openInfoWindow(baloonHTML);
              }
          }
        );
    }
}

function createMarker(point, address, hasGas, hasDiesel, hase85, hasgulfcard, hasChillzone, hasNsd, index, istwentyfourhours, hasCoffee, hasATM, isCampaign) {
    var baloonHTML = "";
    BaloonTextPostAdd = "<tbody>";
    //Has Gas
    if (hasGas.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id='gas'>&nbsp;</div></td><td>Gas available</td></tr>";

    //Has diesel
    if (hasDiesel.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=diesel>&nbsp;</div></td><td>Diesel available</td></tr>";

    //Has e85
    if (hase85.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=e85>&nbsp;</div></td><td>E85 available</td></tr>";

    //Has gulf card
    if (hasgulfcard.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=gulfcard>&nbsp;</div></td><td>Gulf card accepted</td></tr>";

    //Has Chill Zone
    if (hasChillzone.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=chillzone>&nbsp;</div></td><td>Chill Zone available</td></tr>";

    //Has new store design
    if (hasNsd.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=newstoredesign>&nbsp;</div></td><td>New Concept Store</td></tr>";

    //Is 24 hours
    if (istwentyfourhours.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=open24hours>&nbsp;</div></td><td>24 hours</td></tr>";

    //Has coffee
    if (hasCoffee.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=coffee>&nbsp;</div></td><td>Cumberland Farms Coffee</td></tr>";

    //Has atm
    if (hasATM.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=atm>&nbsp;</div></td><td>Citizen's Bank ATM</td></tr>";

    //Has campaign
    if (isCampaign.toLowerCase().indexOf('true') >= 0)
        BaloonTextPostAdd = BaloonTextPostAdd + "<tr><td><div id=campaign>&nbsp;</div></td><td>Pediatric Care Campaign</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 chkchillzone
        if (document.getElementById('chkchillzone').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 24 hours
        if (document.getElementById('chk24hours').checked == true) {
            ele = rows[i].cells[9].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 atm
        if (document.getElementById('chkATM').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 = '';
        }

        //Check for campaign
        if (document.getElementById('chkCampaign').checked == true) {
            ele = rows[i].cells[10].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';
}






