var ieversion = "0";
function dealerAddress (guid, name, nameCont, industry_code, houseNum, street, streetType, city, phone, fax, faxExt, email, region, county, country, zipcode, distTo, index, lat, lng, ranking){
     try{
         this.arrayIndex = index;
         this.guid = guid;
         this.fullName = stripNullfromName(name+' '+nameCont);
         this.locationType = industry_code;
         this.fullAddress = stripNullfromName(houseNum+' '+street+' '+streetType+' '+city+', '+region+' '+zipcode+' ');
         this.county = county;
         this.country = country;
         this.phone = stripNullfromName(phone);
         var faxExt_ = trim(faxExt);
         if (faxExt_ !='' && faxExt_ != 'null' ){
             this.fax = stripNullfromName(fax+' X: '+faxExt);
         }else{
             this.fax =stripNullfromName(fax);
         }
         this.email = stripNullfromName(email);
         this.lat = lat;
         this.lng = lng;
         this.ranking = ranking
         this.distanceTo = roundNumber(distTo,2)+' mi';
     }catch(err){
         //alert(err);
     }
}
function repAddress (buPartner, name, nameCont, industry_code, houseNum, street, street4, city, phone, fax, faxExt, email, region, county, country, zipcode, distTo, index, lat, lng){
     try{
         this.arrayIndex = index;
         this.buPartner = buPartner;
         this.fullName = stripNullfromName(name+' '+nameCont);
         this.locationType = industry_code;
         this.fullAddress = stripNullfromName(houseNum+' '+street+' '+street4+' '+city+', '+region+' '+zipcode+' ');
         this.county = county;
         this.country = country;
         this.phone = stripNullfromName(phone);
         var faxExt_ = trim(faxExt);
         if (faxExt_ !='' && faxExt_ != 'null' ){
             this.fax = stripNullfromName(fax+' X: '+faxExt);
         }else{
             this.fax =stripNullfromName(fax);
         }
         this.email = stripNullfromName(email);
         this.lat = lat;
         this.lng = lng;
         this.distanceTo = roundNumber(distTo,2)+' mi';
     }catch(err){
         //alert(err);
     }
}
function initialize() {
     idIE();
     parseQueryStringForParams();
     setSearchDisplayFlag();
     if (GBrowserIsCompatible()) {
         determineZoomLevel();
         geocoder = new GClientGeocoder();
         if(searchDisplayFlag == false){
             printLocationLists();
             adjustDisplay();
         }
     }
}
function setSearchDisplayFlag(){
     if((lat == null & lng == null & distance == null) || (lat == '' & lng == '' & distance == '' & industry == '')){
         searchDisplayFlag = new Boolean(true);
         document.getElementById('loc_list_dist').style.display='none';
     }else{
         searchDisplayFlag = new Boolean(false);
         document.getElementById('loc_list_dist').style.display='block';
     }
}
function determineZoomLevel(){
     if (distance == 25){
         zoomLevel = 9;
     }else if (distance == 50){
         zoomLevel = 9;
     }else if (distance == 75){
         zoomLevel = 8;
     }else if (distance == 100){
         zoomLevel = 7;
     }else{
         zoomLevel = 4;
     }
}
function geocodeStartAddress(address) {
     if (searchDisplayFlag && !showDirections){
         geocoder.getLocations(address, processAddress);
     }
}
function showUserLocation(){
     var point = new GLatLng(lat,lng);
     var text = 'Point Of Origin';
     pointOfOriginMarker = sloanMarker(point,text);
     map.addOverlay(pointOfOriginMarker);
}
function printLocationLists(){
     var content;
     var tmpString;
     var loc_list_dist = document.getElementById('loc_list_dist');
     for (var x = 0; x < dealerAddresses.length; x++){
         var p = document.createElement('p');
         var strong = document.createElement('strong');
         strong.appendChild(document.createTextNode(dealerAddresses[x].fullName));
         p.appendChild(strong);
         p.appendChild(document.createElement('br'));         
         if(trim(dealerAddresses[x].country) != 'US' && trim(dealerAddresses[x].country) != ''){
             tmpString = dealerAddresses[x].fullAddress + dealerAddresses[x].country;
         }else{
             tmpString = dealerAddresses[x].fullAddress;
         }
         content = document.createTextNode(tmpString);
         p.appendChild(content);
         p.appendChild(document.createElement('br'));
         content = document.createTextNode('P: '+dealerAddresses[x].phone);
         p.appendChild(content);
         p.appendChild(document.createElement('br'));
         if(trim(dealerAddresses[x].fax) != ''){
             content = document.createTextNode('F: '+dealerAddresses[x].fax);
             p.appendChild(content);
             p.appendChild(document.createElement('br'));
         }
         if(trim(dealerAddresses[x].email) != ''){
             content = document.createTextNode('Email: ');
             p.appendChild(content);
             content = document.createElement('a');
             content.setAttribute('href', 'mailto:'+dealerAddresses[x].email);
             content.appendChild(document.createTextNode(dealerAddresses[x].email));
             p.appendChild(content);
             p.appendChild(document.createElement('br'));
         }         if(trim(dealerAddresses[x].distanceTo) != ''){
             content = document.createTextNode('Distance: '+dealerAddresses[x].distanceTo);
             p.appendChild(content);
         }         loc_list_dist.appendChild(p);
     }
     var loc_list_reps = document.getElementById('loc_list_reps');
        for (var x = 0; x < repAddresses.length; x++){
         var p = document.createElement('p');
         var strong = document.createElement('strong');
         strong.appendChild(document.createTextNode(repAddresses[x].fullName));
         p.appendChild(strong);
         p.appendChild(document.createElement('br'));
         if(trim(repAddresses[x].country) != 'US' && trim(repAddresses[x].country) != ''){
             tmpString = repAddresses[x].fullAddress + repAddresses[x].country;
         }else{
             tmpString = repAddresses[x].fullAddress;
         }         
         content = document.createTextNode(tmpString);
         p.appendChild(content);
         p.appendChild(document.createElement('br'));
         content = document.createTextNode('P: '+repAddresses[x].phone);
         p.appendChild(content);
         p.appendChild(document.createElement('br'));
         if(trim(repAddresses[x].fax) != ''){
             content = document.createTextNode('F: '+repAddresses[x].fax);
             p.appendChild(content);
             p.appendChild(document.createElement('br'));
         }
         if(trim(repAddresses[x].email) != ''){
             content = document.createTextNode('Email: ');
             p.appendChild(content);
             content = document.createElement('a');
             content.setAttribute('href', 'mailto:'+repAddresses[x].email);
             content.appendChild(document.createTextNode(repAddresses[x].email));
             p.appendChild(content);
             p.appendChild(document.createElement('br'));
         }         if(trim(repAddresses[x].distanceTo) != ''){
             content = document.createTextNode('Distance: '+repAddresses[x].distanceTo);
             p.appendChild(content);
         }            loc_list_reps.appendChild(p);
        }
    }
function processAddress (address){
     if (!address){
         alert('No corresponding geographic location could be found for the specified address.\nThis may be due to the fact that the address is relatively new, or it may be incorrect.');
     }else if(address.Status.code != G_GEO_SUCCESS){
         alert('No corresponding geographic location could be found for the specified address.\nThis may be due to the fact that the address is relatively new, or it may be incorrect.');
     }else{
         setLatLngFields(address);
     }
}

//Change to function setLatLngFields by Lee Mazurek on 12/07/10
function setLatLngFields (address){
    var county_flag = false;
    document.addressInput.country.value = address.Placemark[0].AddressDetails.Country.CountryNameCode;
    if(address.Placemark[0].AddressDetails.Country.AdministrativeArea != null){
        document.addressInput.region.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
        if(address.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null){
            document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;    
            county_flag = true;
        }else{
            if(address.Placemark[0].AddressDetails.Country.CountryNameCode == 'US'){  
                if (address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality != null ){
                    var newAddressStr = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName+' '+address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;    
                    geocodeStartAddress(newAddressStr);
                    return false;
                }else if (address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality != null){
                    var newAddressStr = address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality.DependentLocalityName+' '+address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;    
                    document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality.DependentLocalityName;    
                    county_flag = true;
                    /*
                    geocodeStartAddress(newAddressStr);
                    return false;
                    */
                }else{
                    alert("Google Geocoder could not resolve the address! Try City/State combination instead");
                }    
            }else{
                document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
            }    
        }                
    }
    if(county_flag){
        document.addressInput.lat.value = address.Placemark[0].Point.coordinates[1];
        document.addressInput.lng.value = address.Placemark[0].Point.coordinates[0];
        document.addressInput.submit();
    }
}
/*
function setLatLngFields (address){
    document.addressInput.country.value = address.Placemark[0].AddressDetails.Country.CountryNameCode;
    try{
        document.addressInput.lat.value = address.Placemark[0].Point.coordinates[1];
        document.addressInput.lng.value = address.Placemark[0].Point.coordinates[0];
    }catch(err){
        if(address.Placemark[0].AddressDetails.Country.AdministrativeArea != null){
            document.addressInput.region.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            if(address.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null){
                document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
            }else{
                if(address.Placemark[0].AddressDetails.Country.CountryNameCode == 'US'){
                    if (address.Placemark[0].AddressDetails.Country.AdministrativeArea != null && address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality != null ){
                        var newAddressStr = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName+' '+address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                        geocodeStartAddress(newAddressStr);
                        return false;
                    }else if (address.Placemark[0].AddressDetails.Country.AdministrativeArea != null && address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality != null){
                        var newAddressStr = address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality.DependentLocalityName+' '+address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
                        geocodeStartAddress(newAddressStr);
                        return false;
                    }else{
                        alert("Google Geocoder could not resolve the address. Try City/State combination instead");
                    }
                }else{
                    document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                }
            }
        }
    }
    document.addressInput.submit();
}*/

/*
function setLatLngFields (address){
    document.addressInput.country.value = address.Placemark[0].AddressDetails.Country.CountryNameCode;
    if(address.Placemark[0].AddressDetails.Country.AdministrativeArea != null){
        document.addressInput.region.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
        if(address.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null){
            document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;    
        }else{
            if(address.Placemark[0].AddressDetails.Country.CountryNameCode == 'US'){  
                if (address.Placemark[0].AddressDetails.Country.AdministrativeArea != null && address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality != null ){
                    var newAddressStr = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName+' '+address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;    
                    geocodeStartAddress(newAddressStr);
                    return false;
                }else if (address.Placemark[0].AddressDetails.Country.AdministrativeArea != null && address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality != null){
                    var newAddressStr = address.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName+' '+address.Placemark[0].AddressDetails.Country.AdministrativeArea.DependentLocality.DependentLocalityName;    
                    geocodeStartAddress(newAddressStr);
                    return false;
                }else{
                    alert("Google was not able to find a location from the information provided.\nPlease try a city, state or province instead.");
                }    
            }else{
                document.addressInput.county.value = address.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
            }    
        }                
    }
    document.addressInput.lat.value = address.Placemark[0].Point.coordinates[1];
    document.addressInput.lng.value = address.Placemark[0].Point.coordinates[0];                
    document.addressInput.submit();
}
*/
function parseQueryStringForParams(){
     lat = '';
     lng = '';
     distance = '';
     industry = '';
     startAddress = '';
     var qs = location.search.substring(1,location.search.length);
     var valuePairs = qs.split('&');
     var pair;
     for (var x = 0; x < valuePairs.length; x++){
         pair = valuePairs[x].split('=');
         if (pair[0].substr(0,3) == 'sta'){
             startAddress = pair[1];
         }
         if (pair[0].substr(0,3) == 'lng'){
             lng = pair[1];
         }
         if(pair[0].substr(0,3) == 'lat'){
             lat = pair[1];
         }
         if(pair[0].substr(0,3) == 'dis'){
             distance = pair[1];
         }
         if(pair[0].substr(0,3) == 'ind'){
             industry = pair[1];
         }
         if(pair[0].substr(0,3) == 'zip'){
             document.getElementById('startAddress').value = pair[1];
         }
     }
}
function stripNullfromName(input){
     //special chars as well
     return input.replace(new RegExp('null', 'g'),'')
     .replace(new RegExp('&#34;', 'g'),'\"')
     .replace(new RegExp('&#35;', 'g'),'#')
     .replace(new RegExp('&#39;', 'g'),'\'')
     .replace(new RegExp('&#45;', 'g'),'-')
     .replace(new RegExp('&#47;', 'g'),'/')
     .replace(new RegExp('&#60;', 'g'),'<')
     .replace(new RegExp('&#62;', 'g'),'>');
}
function idIE(){
     if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
         ieversion = new Number(RegExp.$1);
     }
}
function roundNumber(num, dec){
     return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}
function trim(stringToTrim){
     if(stringToTrim != null){
         return stringToTrim.replace(/^\s+|\s+$/g,"");
     }else{
         return '';
     }
}
function adjustDisplay(){
   document.getElementById('paragraph_reps').style.display='none';
   document.getElementById('paragraph_dist').style.display='none';
}
