
/*!*
 *  storelocator.js
 *  22_Webshop
 *
 *  Created on 2007-07-12.
 *  Copyright SinnerSchrader. All rights reserved.
 *
 *  Version 3.0
 */
    var map = null,
        loc = "",
        gQuery = "",
        xQuery = "",
        trafficInfo,
        storeArray = new Array(),
        eccoshops = new Array(),
        formCounter = 0,
        //apiUrl = "/xml/storelocator/index.xml";
        apiUrl = "/xml/storelocator/index.do";
    
    /**
    * Initialize and render the map
    */
    function load() {
        //console.log("load()");
        document.getElementById("noStoreResultListHeader").innerHTML = getMessagesByKey("formError");
        document.getElementById("resultError").style.display = "block";
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("googleMap"));
            map.setUIToDefault();
            map.removeMapType(G_SATELLITE_MAP);
            map.removeMapType(G_PHYSICAL_MAP);
            map.addControl(new GMapTypeControl());
                //var ov = new GOverviewMapControl(new GSize(100,100));
            //map.addControl(ov);
                //ov.hide(true);
            map.enableContinuousZoom();
            map.enableDoubleClickZoom();
            map.enableScrollWheelZoom();
            var trafficOptions = {incidents:true};
            trafficInfo = new GTrafficOverlay(trafficOptions);
            setMapToCountry();
            if(location.search.length > 0) {
                var params = location.search.split("&");
                for(var i=0; i< params.length; i++) {
                    var paramSplit = params[i].split("=");
                    var key = paramSplit[0];
                    var value = decodeURI(paramSplit[1].replace(/\+/g, " "));
                    if(key == "countryId") document.forms['storelocatorForm'].elements['countryId'].value = value;
                    if(key == "location") document.forms['storelocatorForm'].elements['location'].value = value;
                }
                window.setTimeout("waitUntilMapIsLoaded()",100);
            }
            
            //addlisteners
            GEvent.addDomListener(document.getElementById('storelocatorForm'), "submit", showEccoShops);
            GEvent.addDomListener(document.getElementById('drivingDirectionsForm'), "submit", showDrivingDirectionsForStore);
            GEvent.addDomListener(document.getElementById('backtoresults'), "click", switchToResultView);
            GEvent.addDomListener(document.getElementById('find'), "click", function(){this.blur();});
            GEvent.addDomListener(document.getElementById('startrouting'), "click", function(){this.blur();});
        }
    }
    /**
    * Checks if the map loaded to avoid JS error.
    * If the map is loaded calls showEccoMaps()
    */
    function waitUntilMapIsLoaded() {
        //console.log("waitUntilMapIsLoaded()");
        if(map.isLoaded()) showEccoShops();
        else window.setTimeout("waitUntilMapIsLoaded()",1000);
    }
    /**
    * @param {Integer} i The marker number
    * Creates a marker with number
    * @returns A GIcon object
    */
    function getMapIcon(i) {
        //console.log("getMapIcon()");
        var baseIcon = new GIcon(i);
            //baseIcon.image = "http://www.google.com/intl/de_ALL/mapfiles/marker.png";
            baseIcon.image = "/images/elements/el_gmapmarker_" + i + ".png";
            baseIcon.shadow = "/images/elements/el_gmapmarker_shadow.png";
            baseIcon.iconSize = new GSize(28.0, 47.0);
            baseIcon.shadowSize = new GSize(47.0, 47.0);
            baseIcon.iconAnchor = new GPoint(14.0, 47.0);
            baseIcon.infoWindowAnchor = new GPoint(14.0, 35.0);
        return baseIcon;
    }
    /**
    * @param {GLat} lat Latitude of a point
    * @param {Gng} lng Langitude of a point
    * @param {String} bubble HTML code for the info window
    * @param {Integer} storeIter A Id for the marker object
    * @param {Integer} markerId Id for the marker image
    * Adds an marker with info window to map by given coords and saves it to the storeArray.
    */
    function insertStoresIntoMap(lat,lng,bubble,storeIter,markerId) {
        //console.log("insertStoresIntoMap()");
        var point = new GLatLng(lat,lng);
        var marker = new GMarker(point, getMapIcon(markerId));
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(bubble);
        });
        map.addOverlay(marker);
        storeArray[storeIter] = marker;
    }
    var locationIcon = new GIcon();
        locationIcon.image = "/images/elements/el_gmapmarker_loc.png";
        //locationIcon.shadow = "/images/elements/el_gmapmarker_loc_shadow.png";
        locationIcon.iconSize = new GSize(21, 20);
        //locationIcon.shadowSize = new GSize(32, 33);
        locationIcon.iconAnchor = new GPoint(10, 10);
        locationIcon.infoWindowAnchor = new GPoint(10, 10);
    /**
    * @param {String} address An address
    * Centers the map to the given address
    */
    function centerResult(result) {
        //console.log("centerRegion()");
        var bounds = new GLatLngBounds(new GLatLng(result.getAttribute('zoomMinLatitude'), result.getAttribute('zoomMinLongitude')),
            new GLatLng(result.getAttribute('zoomMaxLatitude'), result.getAttribute('zoomMaxLongitude')));
        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    }
    function setMapToCountry() {
        var bounds = new GLatLngBounds(new GLatLng(startMinLatitude, startMinLongitude),
            new GLatLng(startMaxLatitude, startMaxLongitude));
        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    }
    /**
    * @param {Glat} lat Latitude
    * @param {GLng} long Longitude
    * Add a marker to the map with coords
    */
    function placeLocationMarker(lat,lng) {
        var marker = new GMarker(new GLatLng(lat,lng), {icon:locationIcon, clickable:false});
        map.addOverlay(marker);
    }
    
    function clearMap() {
        map.clearOverlays();
    }
    /**
    * Clears the Map and result list
    */
    function clearResults() {
        //console.log("clearResults()");
        storeArray = new Array();
        clearMap();
        while(document.getElementById("storeResultListHeader").hasChildNodes()) {
            document.getElementById("storeResultListHeader").removeChild(document.getElementById("storeResultListHeader").firstChild);
        }
        while(document.getElementById("storelocatorResults").hasChildNodes()) {
            document.getElementById("storelocatorResults").removeChild(document.getElementById("storelocatorResults").firstChild);
        }
        return true;
    }
    /**
    * @param {String} resultDirectionAddress String with coords of the start point
    * Iterates over the result xml and creates HTML LI nodes in result list
    */
    function insertStoresIntoResultListAndGoogleMap(resultDirectionAddress) {
        //console.log("insertStoresIntoResultListAndGoogleMap()");
        document.getElementById("storeResultListHeader").innerHTML = getMessagesByKey("resultIntrotext") + getMessagesByKey("resultText");
        var storeIter = 0;
            /*
                        HTML example:
                        <li class="eccoshop">
                            <h4>ECCO Store Europapassage</h4>
                            <address>
                                Ballindamm 40<br />20095 Hamburg<br />Tel: +49(0)40/40185994<br />
                                <a href="#" target="_blank">driving directions</a>
                            </address>
                            <p class="distance"><b>Distance<br />from center<br /></b>5,7 km</p>
                        </li>
            */
        for(var i=0; i<eccoshops.length; i++) {
            //insert in resultlist
            var storeResultListItem = document.createElement("LI");
                storeResultListItem.id = "storelocatorResult" + i;
                storeResultListItem.onclick = new Function("openStoreInfo('" + i + "')");
                var storeResultListItemH4 = document.createElement("H4");
                    storeResultListItemH4.appendChild(document.createTextNode(eccoshops[i].getAttribute('title')));
                if(eccoshops[i].getAttribute('eccoShop') == "true") {
                    storeResultListItem.className = "eccoshop";
                } else {
                    storeResultListItemH4.style.backgroundImage = "url(/images/icons/ic_numbers_" + (++storeIter) + ".gif)";
                }
                var storeResultListItemAddress = document.createElement("ADDRESS");
                    if(eccoshops[i].getAttribute('address1') != "") {
                        storeResultListItemAddress.appendChild(document.createTextNode(eccoshops[i].getAttribute('address1')));
                        storeResultListItemAddress.appendChild(document.createElement("BR"));
                    }
                    if(eccoshops[i].getAttribute('address2') != "") {
                        storeResultListItemAddress.appendChild(document.createTextNode(eccoshops[i].getAttribute('address2')));
                        storeResultListItemAddress.appendChild(document.createElement("BR"));
                    }
                    if(eccoshops[i].getAttribute('address3') != "") {
                        storeResultListItemAddress.appendChild(document.createTextNode(eccoshops[i].getAttribute('address3')));
                        storeResultListItemAddress.appendChild(document.createElement("BR"));
                    }
                    if(eccoshops[i].getAttribute('zip') != "" && eccoshops[i].getAttribute('city') != "") {
                        storeResultListItemAddress.appendChild(document.createTextNode(eccoshops[i].getAttribute('zip') + " "));
                        storeResultListItemAddress.appendChild(document.createTextNode(eccoshops[i].getAttribute('city')));
                        storeResultListItemAddress.appendChild(document.createElement("BR"));
                    }
                    if(eccoshops[i].getAttribute('phone') != "") {
                        storeResultListItemAddress.appendChild(document.createTextNode(getMessagesByKey("tel") + ": " + eccoshops[i].getAttribute('phone')));
                        storeResultListItemAddress.appendChild(document.createElement("BR"));
                    }
                    /* if(eccoshops[i].getAttribute('fax') != "") {
                        storeResultListItemAddress.appendChild(document.createTextNode(getMessagesByKey("fax") + ": " + eccoshops[i].getAttribute('fax')));
                    } */
                
                if(eccoshops[i].getAttribute('directionsAvailable') == "true") {
                    var storeResultListItemAnchor = document.createElement("A");
                        storeResultListItemAnchor.appendChild(document.createTextNode("driving directions"));
                        storeResultListItemAnchor.href = "javascript:showDrivingDirectionFields('"+eccoshops[i].getAttribute('latitude')+","+eccoshops[i].getAttribute('longitude')+"','"+eccoshops[i].getAttribute('title')+"','storelocatorResult"+i+"');";
                        //http://maps.google.com/maps?f=d&saddr=" + resultDirectionAddress + "&daddr=" + eccoshops[i].getAttribute('latitude') + "," + eccoshops[i].getAttribute('longitude');                        
                }
                
                var storeResultListItemP = document.createElement("P");
                    storeResultListItemP.className = "distance";
                    var storeResultListItemPB = document.createElement("B");
                        storeResultListItemPB.appendChild(document.createTextNode("Distance"));
                        storeResultListItemPB.appendChild(document.createElement("BR"));
                        storeResultListItemPB.appendChild(document.createTextNode("from center"));
                        storeResultListItemPB.appendChild(document.createElement("BR"));
                    storeResultListItemP.appendChild(storeResultListItemPB);
                    storeResultListItemP.appendChild(document.createTextNode(eccoshops[i].getAttribute('distanceKm') + " km"));
            storeResultListItem.appendChild(storeResultListItemH4);
            if(eccoshops[i].getAttribute('directionsAvailable') == "true") {
                storeResultListItemAddress.appendChild(storeResultListItemAnchor);
                storeResultListItemAddress.appendChild(document.createElement("BR"));
                storeResultListItemAddress.appendChild(document.createElement("BR"));
            }
            storeResultListItem.appendChild(storeResultListItemP);
            storeResultListItem.appendChild(storeResultListItemAddress);
            
            if( eccoshops[i].getAttribute('men') == "true" || 
                eccoshops[i].getAttribute('ladies') == "true" || 
                eccoshops[i].getAttribute('kids') == "true" || 
                eccoshops[i].getAttribute('golf') == "true" || 
                eccoshops[i].getAttribute('performance') == "true" ) {
                var storeResultListItemUL = document.createElement("UL");
                    storeResultListItemUL.className = "availcats";
                    if(eccoshops[i].getAttribute('men') == "true") {
                        var storeResultListItemLiMens = document.createElement("LI");
                            storeResultListItemLiMens.appendChild(document.createTextNode("Men"));
                        storeResultListItemUL.appendChild(storeResultListItemLiMens);
                    }
                    if(eccoshops[i].getAttribute('ladies') == "true") {
                        var storeResultListItemLiWomens = document.createElement("LI");
                            storeResultListItemLiWomens.appendChild(document.createTextNode("Women"));
                        storeResultListItemUL.appendChild(storeResultListItemLiWomens);
                    }
                    if(eccoshops[i].getAttribute('kids') == "true") {
                        var storeResultListItemLiKids = document.createElement("LI");
                            storeResultListItemLiKids.appendChild(document.createTextNode("Kids"));
                        storeResultListItemUL.appendChild(storeResultListItemLiKids);
                    }
                    if(eccoshops[i].getAttribute('golf') == "true") {
                        var storeResultListItemLiGolf = document.createElement("LI");
                            storeResultListItemLiGolf.appendChild(document.createTextNode("Golf"));
                        storeResultListItemUL.appendChild(storeResultListItemLiGolf);
                    }
                    if(eccoshops[i].getAttribute('performance') == "true") {
                        var storeResultListItemLiPerfomance = document.createElement("LI");
                            storeResultListItemLiPerfomance.appendChild(document.createTextNode("Performance"));
                        storeResultListItemUL.appendChild(storeResultListItemLiPerfomance);
                    }
                storeResultListItem.appendChild(storeResultListItemUL);
            }
            document.getElementById("storelocatorResults").appendChild(storeResultListItem);
            //insert into map
            /*var balloonView = "<div class=\"gmBubble\">";
                balloonView += "<h4>" + eccoshops[i].getAttribute('title') + "<\/h4>";
                if(eccoshops[i].getAttribute('shoppingmall') != "")
                    balloonView += "<p>" + eccoshops[i].getAttribute('address1') + "<br />";
                if(eccoshops[i].getAttribute('address2') != "")
                    balloonView += eccoshops[i].getAttribute('address2') + "<br />";
                if(eccoshops[i].getAttribute('address3') != "")
                    balloonView += eccoshops[i].getAttribute('address3') + "<br />";
                if(eccoshops[i].getAttribute('zip') != "" && eccoshops[i].getAttribute('city') != "")
                    balloonView += eccoshops[i].getAttribute('zip') + " " + eccoshops[i].getAttribute('city') + "<\/p>";
                balloonView += "<\/div>";*/
            var balloonView = '<iframe src="/storelocator/GMapPopupVPAction.do?' + eccoshops[i].getAttribute('storeid') + '" style="width:240px;height:175px;" frameborder="0" scrolling="no" ></iframe>';
            var markertype = (eccoshops[i].getAttribute('eccoShop') == "true") ? 0 : storeIter;
            insertStoresIntoMap(eccoshops[i].getAttribute('latitude'),eccoshops[i].getAttribute('longitude'),balloonView,i,markertype);
        }
        hideStartIndicator();
        openStoreInfo('0');
        return false;
    }
    /**
    * Gets the search results via AJAX and shows suggestion selector if no location a country was found.
    */
    function getStoresFromDB() {
        //console.log("getStoresFromDB()");
        var request = GXmlHttp.create();
        request.open("GET", encodeURI(apiUrl+"?"+xQuery), true);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var xmlDoc = request.responseXML;
                result = xmlDoc.documentElement;
                if (xmlDoc.documentElement.getAttribute("error") == "close") {
                    window.close();
                } else if (xmlDoc.documentElement.getAttribute("error") == "true") {
                    setMapToCountry();
                    document.getElementById("noStoreResultListHeader").innerHTML = "ECCO always wants to provide the best service.<br />Therefore we're currently updating our ECCO Store database.<br /><br />Please try again later.";
                    document.getElementById("storelocatorResults").style.display = "none";
                    document.getElementById("resultError").style.display = "block";
                    hideStartIndicator();
                } else {
                    eccoshops = xmlDoc.documentElement.getElementsByTagName("store");
                    if (eccoshops.length == 0) {
                        setMapToCountry();
                        document.getElementById("noStoreResultListHeader").innerHTML = getMessagesByKey("noResultsForAddress");
                        document.getElementById("storelocatorResults").style.display = "none";
                        document.getElementById("resultError").style.display = "block";
                        hideStartIndicator();
                    } else {
                        placeLocationMarker(result.getAttribute('latitude'),result.getAttribute('longitude'));
                        document.getElementById("resultError").style.display = "none";
                        document.getElementById("storelocatorResults").style.display = "block";
                        centerResult(result);
                        insertStoresIntoResultListAndGoogleMap(result.getAttribute('latitude') + "," + result.getAttribute('longitude'));
                    }
                    trackingCountry = xmlDoc.documentElement.getElementsByTagName("stores")[0].getAttribute("countryName");
                    trackingQuery = xmlDoc.documentElement.getElementsByTagName("stores")[0].getAttribute("location");
                    trackResults();
                }
            }
        }
        request.send(null);
    }
    
    /**
    * @param {String} targetgeo The geo address of the store
    * @param {String} targettext The address of the store
    * Doing the routing
    */
    function showDrivingDirectionsForStore() {
        if (typeof(iFrameDocument) == "undefined") {
            var context = window;
        } else {
            var context = iFrameDocument;
        }
        delete iFrameDocument;
        if(context.document.getElementById("startAddress").value == "" ) {
             // country is not selected
            //document.getElementById('mapFormErrors').style.display = "block";
            context.document.getElementById("startAddress").focus();
            context.document.getElementById("startAddress").className = "input error";
            return false;
        } else {
            context.document.getElementById("startAddress").className = "input";
        }
        var from  = "from: " + context.document.getElementById("startAddress").value + ", " + context.document.getElementById("countryId2").value;
        var to    = " to: " +  context.document.getElementById('targetGeo').value;
        var route = from + to;
        document.getElementById("startAddress").value = context.document.getElementById("startAddress").value; //save value - always ;-)
        showStartIndicator();
        switchToDrivingDirectionView();
        directionsPanel = document.getElementById("drivingDirections");
        document.getElementById("map").appendChild(document.getElementById('drivingDirectionsForm'));
        while(directionsPanel.hasChildNodes()) {
            directionsPanel.removeChild(directionsPanel.firstChild);
        }
        directions = new GDirections(map, directionsPanel);
        directions.load(route);
        GEvent.addListener(directions, "error", handleErrors);
        GEvent.addListener(directions, "load", handleSuccess);
        GEvent.addListener(directions, "addoverlay", handleLoaded);
        hideStartIndicator();        
        return false;
    }
    function handleSuccess(){
        var directionsPanelPrint = document.createElement("a");
            directionsPanelPrint.href = "javascript:window.print();"
            directionsPanelPrint.id = "print";
            directionsPanelPrint.appendChild(document.createTextNode(getMessagesByKey("print")));
            directionsPanel.appendChild(directionsPanelPrint);
            directionsPanel.appendChild(document.createElement("BR"));
            directionsPanel.appendChild(document.getElementById('drivingDirectionsForm'));
            //directionsPanel.appendChild(document.getElementById('drivingDirectionsForm').cloneNode(true));
            document.getElementById('drivingDirectionsForm').style.width = "0";     
            document.getElementById('drivingDirectionsForm').style.display = "block";
            document.getElementById('startrouting').nextSibling.nextSibling.style.display = "block";
            document.getElementById('targetName').style.display = "block";
            document.getElementById('backtoresults').style.display = "block";            
    }
    
    function handleLoaded(){
        var directionsPanelPrint = document.createElement("a");
            directionsPanelPrint.href = "javascript:window.print();"
            directionsPanelPrint.id = "print";
            directionsPanelPrint.appendChild(document.createTextNode(getMessagesByKey("print")));
            directionsPanel.appendChild(directionsPanelPrint);
            document.getElementById('drivingDirectionsForm').nextSibling.style.marginTop = "115px";       
    }
    
    function handleErrors(){
        if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
            //alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
            document.getElementById("startAddress").select();
            document.getElementById("startAddress").className = "input error";
        }
        /*else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
            alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
            alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
            alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_BAD_KEY)
            alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
            alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
        else
            alert("An unknown error occurred.");
        */
    }
    /**
    * @param {String} marker The id of a marker
    * Opens the bubble of the marker, when the user clicks a match in the result list
    */
    function openStoreInfo(marker) {
        //console.log("openStoreInfo()");
        if(storeArray[marker]) GEvent.trigger(storeArray[marker], 'click');
        return false;
    }
    /**
    * Shows the progress indikator
    */
    function showStartIndicator() {
        //console.log("showStartIndicator()");
        document.getElementById('searchstatus').style.display = "inline";
    }
    /**
    * Hides the result list of stores
    */
    function switchToDrivingDirectionView() {
        clearMap();
        map.addOverlay(trafficInfo);
        document.getElementById('storelocatorResults').style.display = "none";
        document.getElementById('drivingDirections').style.display = "block";
    }
    /**
    * Shows the driving direction fieldset
    */
    function showDrivingDirectionFields(geo,label,id) {
        document.getElementById('targetGeo').value = geo;
        document.getElementById('targetName').firstChild.data = label;
        document.getElementById('targetName').style.display = "none";
        var ddForm = document.getElementById('drivingDirectionsForm');
            //ddForm.id = "";
        document.getElementById(id).getElementsByTagName('ADDRESS')[0].appendChild(ddForm);
        ddForm.style.display = "block";
        document.getElementById('startrouting').nextSibling.nextSibling.style.display = "none";
        document.getElementById('backtoresults').style.display = "none";
        document.getElementById('startAddress').focus();
        document.getElementById('page').className = "driving";
    }  
    /**
    * Hides the drivingdirections list of stores
    */
    function switchToResultView() {
        showEccoShops();
        map.removeOverlay(trafficInfo);
        document.getElementById('drivingDirections').style.display = "none";
        document.getElementById('storelocatorResults').style.display = "block";
        document.getElementById('drivingDirectionsForm').style.display = "none";
        document.getElementById('storelocatorForm').style.display = "block";
    }
    /**
    * Hides the progress indikator
    */
    function hideStartIndicator() {
        //console.log("hideStartIndicator()");
        document.getElementById('searchstatus').style.display = "none";
        document.forms['storelocatorForm'].elements['find'].disabled = false;
    }
    /**
    * Tracks the user's search
    */
    var trackingCountry;
    var trackingQuery;
    function trackResults() {
        s.prop5 = (trackingCountry + ":" + trackingQuery).toLowerCase();
        s.prop6=(eccoshops && eccoshops.length > 0) ? "successful" : "failed";
        void(s.t());
    }
    /**
    * This function is called by the form.
    * Validates the form, assembles the AJAX request and calls further functions.
    */
    function showEccoShops() {
        //console.log("showEccoShops()");
        if(document.getElementById('suggestions')) document.getElementById('location').parentNode.removeChild(document.getElementById('suggestions'));
        document.forms['storelocatorForm'].elements['find'].disabled = true;
        var formError = false;
        loc = document.forms['storelocatorForm'].elements['location'].value;
        if(document.forms['storelocatorForm'].elements['location'].value == "") {
            // location is empty
            // or not 5 digit number ( /^([0-9])$/.test(loc.charAt(0)) && !/^([0-9]{3,5})$/.test(loc) )
            //document.getElementById("mapFormErrors").firstChild.data = getMessagesByKey("formError");
            //document.getElementById('mapFormErrors').style.display = "block";
            document.forms['storelocatorForm'].elements['location'].focus();
            document.forms['storelocatorForm'].elements['location'].className = "input error";
            formError = true;
        }
        if(document.forms['storelocatorForm'].elements['countryId'].value == "" ) {
             // country is not selected
            //document.getElementById('mapFormErrors').style.display = "block";
            document.forms['storelocatorForm'].elements['countryId'].focus();
            document.forms['storelocatorForm'].elements['countryId'].className = "select error";
            formError = true;
        }
        if(formError) {
            document.forms['storelocatorForm'].elements['find'].disabled = false;
            return false;
        } else {
            document.getElementById("mapFormErrors").firstChild.data = '';
            document.getElementById('mapFormErrors').style.display = "none";
            document.forms['storelocatorForm'].elements['location'].className = "input";
            document.forms['storelocatorForm'].elements['countryId'].className = "select";
        }
        gQuery = document.forms['storelocatorForm'].elements['location'].value + ", " + document.forms['storelocatorForm'].elements['countryId'].value;
        xQuery = "countryName=" + document.forms['storelocatorForm'].elements['countryId'].value;
        xQuery += "&location=" + document.forms['storelocatorForm'].elements['location'].value;
        //check for optione
        /*if(document.forms['storelocatorForm'].elements['womens'].checked)  xQuery += "&womens=true";
        if(document.forms['storelocatorForm'].elements['mens'].checked)    xQuery += "&mens=true";
        if(document.forms['storelocatorForm'].elements['kids'].checked)    xQuery += "&kids=true";
        if(document.forms['storelocatorForm'].elements['infants'].checked) xQuery += "&infants=true";*/
        xQuery += "&countryId=" + document.forms['storelocatorForm'].elements['countryId'].value;
        xQuery += "&language=" + document.forms['storelocatorForm'].elements['language'].value;
        //console.log(xQuery);
        //tracking: edit 081114 veileh: values come via ajax from xml now
        //trackingCountry = document.forms['storelocatorForm'].elements['countryId'].value;
        //trackingQuery = document.forms['storelocatorForm'].elements['location'].value;
        showStartIndicator();
        clearResults();
        getStoresFromDB();
        return false;
    }
    window.onload=load;
    window.onunload=GUnload;