﻿/* Dealer finder Lexus v1.0 RC */
/* Based on Toyota dealer maps script for Google maps  */

var G={gecko:navigator.product=='Gecko',
ie6:(!!document.all && !(typeof window.XMLHttpRequest=='object')),
init:function(){try{document.execCommand('BackgroundImageCache', false, true)}catch(err){};for(var n in G)if(G[n].init)G[n].init()},
set:function(a,b){for(var o in b)a[o]=b[o];return a},
create:function(a,b,c,d){var o=(d||document).createElement(a);G.set(o,b);G.set(o.style,c);return o},
append:function(a,b,c,d){var o=b.tagName?b:G.create(b,c,d,a.ownerDocument);a.appendChild(o);return o},
middle:function(o,a,b){o=o.split(a);return(o.length<2)?'':o[1].split(b)[0]},
expand:function(a){((a.nodeName)?a:$get(a)).style.display='block'},
collapse:function(a){((a.nodeName)?a:$get(a)).style.display='none'},
_:function(){var w=window;return w.XMLHttpRequest?new XMLHttpRequest():w.ActiveXObject?new ActiveXObject('Microsoft.XMLHTTP'):null},
get:function(a){var r=this._();r.open('GET',a,false);r.send(null);return r}
};function $get(a){return document.getElementById(a)};function $$(a,b){return (b||document).getElementsByTagName(a)}

G.map = { onRoute: false, allChecked: false, step: 0,
    servicePath: '/site-tools/MapTools.aspx',
    icoBase: '/images/',
    wPath: '/site-tools/dealer-maps/',
    wOpt: 'menubar=1,resizable=1,scrollbars=1,width=800,height=600',
    init: function() {
        var m = this, a, b
        //if(location.search.indexOf('ui=blankpage')==-1)if(window.top.location==location){location.replace('/index.aspx?form='+location.href);return}
        m.activeMsg = $get('msg0')
        document.onkeypress = m.handleKey
        m.prepare()
        a = m.qVal('city'); if (a) { $get('fAddress').value = a; m.evalStep1() }
    },

    getCountry: function(p) {
        var i, t, r = ''
        for (i = 0; i < p.length; i++) {
            t = p[i].types[0]
            if (t == 'country')
                r = p[i].short_name
        }
        return r
    },


    evalStep1: function() {
        var m = G.map, vA = $get('fAddress').value, s, o, v
        m.SearchValue = vA
        G.collapse('destination')
        if (vA == '') { m.setMsg(1); $get('eLabel1').className = 'error'; return }
        $get('eLabel1').className = ''
        if (m.hasRadio && !m.getRadioVal($get('form1').radio1)) { m.setMsg(3); $get('eLabel2').className = 'error'; return }
        $get('eLabel1').className = ''
        if (m.hasRadio) $get('eLabel2').className = ''
        m.setMsg(0)
        var geocoder = new google.maps.Geocoder()
        geocoder.geocode({ 'address': vA, country: G.map.country }, function(results, status) {
            var m = G.map
            if (status == google.maps.GeocoderStatus.OK) { m.evalSearch(results) }
            else { alert("L’indirizzo inserito non è valido") }
        })
    },
    evalSearch: function(o) {
        var m = G.map, i, oo = $get('fDestination'), i, q = m.starts = [], p, r = new RegExp(m.country, 'i'), n, c
        for (i = 0; i < o.length; i++) {
            p = o[i].address_components; n = p.length
            c = m.getCountry(p)
            if (n > 1 && r.test(c)) m.starts.push(o[i])
        }
        if (m.starts.length == 0) { m.setMsg(2); $get('eLabel1').className = 'error'; return }
        m.setMsg(0); $get('eLabel1').className = ''
        if (m.starts.length > 1) {
            for (i = oo.options.length; i >= 0; i--) oo.options[i] = null
            oo.options[0] = new Option(m.destMsg, '')
            for (i = 0; i < m.starts.length; i++) oo.options[i + 1] = new Option(i + ': ' + m.starts[i].formatted_address, i)
            G.expand('destination')
        }
        else { G.collapse('destination'); m.goStep2(0) }
    },
    goStep2: function(a) {
        var m = G.map, x = $get('form1').Option1, i, ff = [], p, v, sf, tm, o
        m.index = a; G.collapse('step1'); m.copyVals(); G.expand('step2')
        $get('fAddress2').value = m.starts[a].formatted_address
        m.step = 1
        p = m.starts[m.index].geometry.location
        if (m.hasRadio) x = $get('form1').radio1
        if (x) {
            for (i = 0; i < x.length; i++) { if (x[i].checked) ff.push(x[i].value) }
            sf = m.SearchFilter = ff.join('~')
        }
        v = m.servicePath + '?xmlid=true&action=nearest&lat=' + p.lat() + '&lon=' + p.lng()
        if (sf > '') v += '&filter=' + sf
        o = G.get(v).responseXML
        m.results = $$('dealer', o)
        m.displayResult()
    },

    /* step 2 search*/
    evalStep1b: function() {
        var m = G.map, vA = $get('fAddress2').value, s, o
        m.SearchValue = vA
        if (vA == '') { $get('errLabel2').className = 'error'; G.expand('errMsg1'); return }
        $get('errLabel2').className = ''; G.collapse('errMsg1')
        var geocoder = new google.maps.Geocoder()
        geocoder.geocode({ 'address': vA, country: G.map.country }, function(results, status) {
            var m = G.map
            if (status == google.maps.GeocoderStatus.OK) { m.evalSearch2(results) }
            else { alert("L’indirizzo inserito non è valido") }
        })
    },
    evalSearch2: function(o) {
        var m = G.map, i, oo = $get('fDestination2'), i, q = m.starts = [], p, r = new RegExp(m.country, 'i'), n, c
        for (i = 0; i < o.length; i++) {
            p = o[i].address_components; n = p.length
            c = m.getCountry(p)
            if (n > 1 && r.test(c)) m.starts.push(o[i])
        }
        if (m.starts.length == 0) { $get('errLabel2').className = 'error'; G.expand('errMsg1'); return }
        $get('errLabel2').className = ''; G.collapse('errMsg1')
        if (m.starts.length > 1) {
            for (i = oo.options.length; i >= 0; i--) oo.options[i] = null
            oo.options[0] = new Option(m.destMsg, '')
            for (i = 0; i < m.starts.length; i++) oo.options[i + 1] = new Option(i + ': ' + m.starts[i].formatted_address, i)
            G.expand('alertMsg1')
        }
        else { G.collapse('alertMsg1'); m.goStep2b(0) }
    },
    goStep2b: function(a) {
        var m = G.map, x = $get('form2').Option2, i, ff = [], v, sf, p
        m.index = a
        $get('fAddress2').value = m.starts[a].formatted_address
        G.collapse('alertMsg1')
        p = m.starts[m.index].geometry.location
        if (m.hasRadio) x = $get('form2').radio2
        if (x) { for (i = 0; i < x.length; i++) { if (x[i].checked) ff.push(x[i].value) } }
        sf = m.SearchFilter = ff.join('~')
        v = m.servicePath + '?xmlid=true&action=nearest&lat=' + p.lat() + '&lon=' + p.lng()
        if (sf > '') v += '&filter=' + sf
        //debug m.startTime=new Date().getTime()
       o = G.get(v).responseXML
        m.results = $$('dealer', o)
        //m.endTime=new Date().getTime();tm=m.endTime-m.startTime;$get('debug').value=tm
        m.displayResult()
    },

    displayResult: function() {
        var m = G.map, o = m.results, i, p, q, ico, pp, om, xt, xy, marker, image, shadow, ob, p1, p2, v = [], oa, ob
        var xx = m.mileageTxt, pp, mm, g = google.maps, mla = 1
        m.map = new g.Map($get('map01'), {
            zoom: 8,
            center: m.starts[m.index].geometry.location,
            mapTypeId: g.MapTypeId.ROADMAP,
            mapTypeControl: true,
            mapTypeControlOptions: { style: g.MapTypeControlStyle.DROPDOWN_MENU }
        })
        mm = m.markers = []
        dcsTrk('WT.pn=search results&WT.si_x=2&SearchMethod=Address&SearchValue=' + m.SearchValue + '&SearchFilter=' + m.SearchFilter)
        if (o.length < 1) { alert('Nessun rivenditore Lexus trovato'); return }
        xt = G.ie6 ? '.png' : '.png'
        ob = new g.LatLngBounds()
        p1 = new g.Point(0, 0)
        p2 = new g.Point(16.5, 41)
        shadow = new g.MarkerImage(m.icoBase + 'shadow-markerSP.png', new g.Size(54, 41), p1, p2)
        for (i = 0; i < o.length; i++) {
            pp = o[i]
            xy = new g.LatLng(pp.getAttribute('lat'), pp.getAttribute('lon'))
            image = new g.MarkerImage(m.icoBase + 'markerSP' + (i + 1) + xt, new g.Size(33, 41), p1, p2)
            ob.extend(xy)
            marker = new g.Marker({ map: m.map, position: xy, icon: image, shadow: shadow, title: pp.getAttribute('name') })
            marker.indexNum = i
            m.markers.push(marker)
            google.maps.event.addListener(marker, 'click', m.click)
        }
        m.map.fitBounds(ob)
        for (i = 0; i < o.length; i++) {
            pp = o[i]
            oa = pp.getAttribute('name')
            if (m.mileageFlg == 'km') { mla = 1 }
            if (m.mileageFlg == 'mi') { mla = 1.601 }
            ob = Math.round((pp.getAttribute('distance') / mla) * 10) / 10 + ' ' + m.mileageTxt

                                                if (!m.hideFirstDealerAddress) { v.push('<a href="javascript:void null" onclick="G.map.showDetail(' + i + ',this,0)"><i>' + ob + '</i>' + m.fm(i + 1) + oa + '</a>') }
                                                else { v.push('<a href="javascript:void null" onclick="G.map.showDetail(' + i + ',this,1)"><i>' + ob + '</i>' + m.fm(i + 1) + oa + '</a>') }
        }
        $get('rNum').innerHTML = o.length
        $get('rList').innerHTML = v.join('')

        var detailsHidden = false;
        if (!m.hideFirstDealerAddress) { m.showDetail(0, $$('a', $get('rList'))[0], 0) }
        else {
            if (detailsHidden != true) {
//MS                                                     document.getElementById("rDetailHeader").style.display = "none";
                document.getElementById("rDetail").style.display = "none";
                                                                document.getElementById("rList").style.height = "100%";
                                                                document.getElementById("rList").style.overflowY = "hidden";
                detailsHidden == true;
            }
        }

    },
    click: function() { G.map.setDetail(this.indexNum) },
    fm: function(a) { return ((a > 9) ? '' : '&nbsp;') + a + '&nbsp;' },
    setDetail: function(a) { G.map.showDetail(a, $$('a', $get('rList'))[a]) },
    showDetail: function(a, b, c) {

        var m = G.map, o = m.results[a], v = [], h2, vv, u

                                if (c){
//MS                                     document.getElementById("rDetailHeader").style.display = "block"; 
                                                document.getElementById("rList").style.position = "relative";
                                                document.getElementById("rList").style.height = "170px";
                                                document.getElementById("rList").style.overflowY = "visible";

//                                            document.getElementById("rDetail").style.height="0px";
                                                document.getElementById("rDetail").style.display = "block";
                                                //animate('rDetail', 226, 'height');
                                }
       
        if (m.active) m.active.className = ''
        m.active = b; m.selected = a; b.className = 'on'
        v.push('<div id="dTitle"><h2>' + o.getAttribute('name') + '</h2></div>')
        v.push('<p class="intro">' + o.getAttribute('address') + '<br />' + o.getAttribute('zip') + ' ' + o.getAttribute('city') + '</p>')
        v.push('<p><strong>' + m.tel + ': </strong>' + o.getAttribute('phone') + '<br />')
        v.push('<strong>' + m.fax + ': </strong>' + o.getAttribute('fax') + '<br />')
        v.push('<strong>' + m.email + ': </strong><a href="mailto:' + o.getAttribute('email') + '">' + o.getAttribute('email') + '</a></p>')
        m.addServices(o, v)
        v.push('<p class="btnDir"><a class="btn" href="javascript:G.map.startRoute()"><b>' + m.directionsBtn + '</b><i></i></a><a class="btn gps" href="javascript:G.map.toggleExport()"><b>' + m.exportBtn + '</b><i></i></a>')
        vv = o.getAttribute('website')
        u = m.getLink(vv)
        if (vv) {
            if (vv != 'null') {
                if ((u.indexOf("http://") == -1) && (u.indexOf("/") != 0)) { u = 'http://' + u }
                v.push('<a class="btnSm light-grey" href="' + u + '" target="_blank" onclick="G.map.track()"><b>' + m.websiteBtn + '</b><i></i></a>')
            }
        }
        v.push('<a class="btnSm" href="javascript:G.map.printDetail(\'' + o.getAttribute('key') + '\')"><b>' + m.printBtn + '</b><i></i></a>')
        v.push('</p>')
        $get('rDetail').innerHTML = v.join('')
        h2 = $$('h2', $get('rDetail'))[0]
       if (h2.offsetHeight > 40) h2.style.fontSize = '11px'
        m.map.panTo(m.markers[a].getPosition())
    },
    getLink: function(a) { if (a != null) { if (a.indexOf('javascript:window.open(') > -1) return a.split("'")[1].split("'")[0]; else return a } else return a },
    track: function() {
        dcsTrk('WT.pn=open retailer site&WT.si_n=dealerfinder open site&WT.si_cs=1')
    },
    panToDetail: function() {
        var m = G.map
        try { m.map.panTo(m.panCoords) }
        catch (e) { setTimeout(m.panToDetail, 500) }
    },
    addServices: function(o, v) {
        var m = this, i, p, oo = m.cats, q = $$('service', o), vv = []
        for (i = 0; i < q.length; i++) vv.push(q[i].getAttribute('code'))
        vv = vv.join('~')
        v.push('<p id="services">')
        for (i in oo) {
            p = oo[i].split('|')
            if (vv.indexOf(p[2]) > -1) { v.push('<i class="i' + p[1] + '" title="' + p[0] + '"><span class="hide">' + p[0] + '</span></i>') }
        }
        v.push('</p>')
    },
    printDetail: function(a) {
        var m = G.map
        dcsTrk('WT.pn=print retailer details&WT.si_n=dealerfinder print dealer')
        window.open(m.wPath + 'printdealer.aspx?open&id=' + a, '', m.wOpt)
    },

    // routeplanner

    startRoute: function(a) {
        var m = G.map
        $get('fAddress3').value = $get('fAddress2').value
        G.expand('getDir')
        dcsTrk('WT.pn=directions form&WT.si_x=3')
    },
    evalRoute: function() {
        var m = G.map, v = $get('fAddress3').value
        if (v == '') { alert('Inserisci l’indirizzo di partenza'); return }
        var geocoder = new google.maps.Geocoder()
        geocoder.geocode({ 'address': v, country: G.map.country }, function(results, status) {
            var m = G.map
            if (status == google.maps.GeocoderStatus.OK) { m.evalStartAddress2(results) }
            else { alert("L’indirizzo inserito non è valido") }
        })
    },
    evalStartAddress2: function(a) {
        var m = G.map, o, i, oo = $get('fDestination3')
        o = m.routeStart = a

        if (o.length == 0) { $get('errLabel2').className = 'error'; G.expand('errMsg1'); return }
        $get('errLabel2').className = ''; G.collapse('errMsg1')
        if (o.length > 1) {
            m.onRoute = true
            for (i = oo.options.length; i >= 0; i--) oo.options[i] = null
            for (i = 0; i < o.length; i++) oo.options[i + 1] = new Option(o[i].formatted_address, i)
            G.expand('alertMsg2')
        }
        else m.calcRoute(0)
    },
    calcRoute: function(a) {
        var m = G.map, p1, p2, o, q, ds, p, g = google.maps
        m.startIndex = a
        p1 = m.center = m.routeStart[a].geometry.location
        p = m.results[m.selected]
        p2 = p.getAttribute('address') + ' ' + p.getAttribute('zip') + ' ' + p.getAttribute('city')
        ////JPrimmer:Temporary solution: Until Google support for APIv3
        //Remove vars 'orig' and 'bEdit' where used. Remove 'b' and 'c' param from 'displayRoute'
        var bEdit = false;
        var orig = p2;
        var rValue = 'Bolmensvägen 51 120 50 Stockholm - Årsta'
        var rWith = 'Bolmensvägen 53 120 50 Stockholm - Årsta';
        if (p2 == rValue) { p2 = rWith; bEdit = true }
        //\\JPrimmer:Temporary solution
        ds = new google.maps.DirectionsService()
        q = { origin: p1, destination: p2, travelMode: g.DirectionsTravelMode.DRIVING, language: m.lang, unitSystem: g.DirectionsUnitSystem.METRIC }
        ds.route(q, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) G.map.displayRoute(result, orig, bEdit)
            else alert('Nessun risultato corrispondente all’indirizzo inserito')
        })
    },
    displayRoute: function(a, b, c) {
        ////JPrimmer:Temporary solution: Until Google support for APIv3
        if (c) { a.trips[0].legs[0].end_geocode.formatted_address = b; }
        //\\JPrimmer:Temporary solution: Until Google support for APIv3
        var m = G.map, dd, g = google.maps
        G.collapse('block2a'); G.expand('block2b');
        G.collapse('step2a'); G.expand('step2b')
        G.collapse('map01'); G.expand('map02')
        dcsTrk('WT.pn=directions results&WT.si_x=4')
        m.map = new g.Map($get('map02'), {
            zoom: 8,
            center: m.center,
            mapTypeId: g.MapTypeId.ROADMAP,
            mapTypeControl: true,
            mapTypeControlOptions: { style: g.MapTypeControlStyle.DROPDOWN_MENU }
        })
        dd = new g.DirectionsRenderer()
        dd.setMap(m.map)
        $get('theRoute').innerHTML = ''
        dd.setPanel($get('theRoute'))
        dd.setDirections(a)
    },
    changeMapScale: function(a) {
        var o = G.vcm.map, s = o.getMapScale()
        if (a == 'Min') { if (s < 16) o.changeMapScale(s + 1) }
        else { if (s() > 0) o.changeMapScale(s - 1) }
    },
    showDir: function() {
        var m = G.map, o = m.map, v = false
        o.hideControls()
    },
    showZoom: function() {
        var m = G.map, o = m.map
        m.modeFlag = !m.modeFlag
        o.activateSatelliteHybrid(m.modeFlag)
        o.showMapTools()
    },
    printRoute: function() {
        var m = G.map, a = m.center, b = m.results[m.selected].getAttribute('key'), v
        v = m.wPath + 'printRoute.aspx?open&id=' + b + '&lon=' + a.lng() + '&lat=' + a.lat()
        dcsTrk('WT.pn=print direction&WT.si_x=5')
        window.open(v, '', m.wOpt)
    },
    getRouteQuery: function() {
        var m = G.map, a = m.center, b = m.results[m.selected].getAttribute('key')
        return m.wPath + 'printRoute.aspx?open&id=' + b + '&lon=' + a.lng() + '&lat=' + a.lat()
    },
    emailRoute: function() { G.collapse('block2b'); dcsTrk('WT.pn=email direction form&WT.si_n=dealerfinder email directions'); G.expand('block2c') },
    doSendMail: function() {
        var m = G.map, v = true, v2, vv = [], x, r
        vv.push(m.servicePath + '?xmlid=true&action=mail')
        v2 = $get('fEmailFrom').value
        v = v && m.evalMail('EmailFrom', v2)
        vv.push('from=' + encodeURIComponent(v2))
        v2 = $get('fEmailTo').value
        v = v && m.evalMail('EmailTo', v2)
        vv.push('to=' + encodeURIComponent(v2))
        v2 = $get('fEmailSubject').value
        v = v && m.evalFilled('EmailSubject', v2)
        if (!v) return
        dcsTrk('WT.pn=email direction submit&WT.si_n=dealerfinder email directions&WT.si_x=2&WT.si_cs=1')
        vv.push('subj=' + encodeURIComponent(v2))
        x = 'http://' + G.middle(location.href, '//', '/') + m.getRouteQuery()
        vv.push('body=' + encodeURIComponent($get('fEmailBody').value + '\n\n' + x))
        r = G.get(vv.join('&')).responseXML
        alert($get('hEmailSuccess').innerHTML)
        m.doSendCancel()
    },
    evalFilled: function(a, b) { var v = (b != ''); $get(a).className = v ? '' : 'error'; return v },
    evalMail: function(a, b) {
        var v = !!b.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)
        $get(a).className = v ? '' : 'error'
        return v
    },
    doSendCancel: function() { G.collapse('block2c'); G.expand('block2b') },

    cancelRoute: function() { G.collapse('getDir') },
    back: function() {
        var m = G.map
        G.expand('block2a')
        G.collapse('block2b')
        G.collapse('block2c')
        G.collapse('step2b')
        G.collapse('alertMsg1')
        G.collapse('getDir')
        G.collapse('alertMsg2')
        G.collapse('map02')
        G.expand('step2a')
        G.expand('map01')
    },
    toggleExport: function() {
        var o = $get('expGps')
        if (o.className == '') dcsTrk('WT.pn=gps export menu&WT.si_n=dealerfinder gps export')
        o.className = (o.className == 'on') ? '' : 'on'
    },
    doExport: function(a) {
        var m = G.map, b = m.results[m.selected], v, sf
        dcsTrk('WT.pn=gps download&WT.si_n=dealerfinder gps export&WT.si_x=2&WT.si_cs=1&GPSFormat=' + a)
        v = m.servicePath + '?xmlid=true&action=export&format=' + a
        sf = b.getAttribute('name')
        while (sf != (sf = sf.replace(/<[^<>]*>/g, "")));
        v += '&lon=' + b.getAttribute('lon') + '&lat=' + b.getAttribute('lat') + '&name=' + sf
        window.open(v)
        m.toggleExport()
    },
    close: function() { try { window.top.Tme.cta.close() } catch (e) { } },
    getRadioVal: function(a) { for (var i = 0; i < a.length; i++) { if (a[i].checked) return a[i].value }; return null },
    qVal: function(a) { return unescape(G.middle(location.search, a + '=', '&')) },
    handleKey: function(e) {
        var m = G.map, ev = (e) ? e : window.event
        if (ev.keyCode != 13) return
        if ($get('getDir').style.display == 'block') m.evalRoute()
        else if (m.step == 1) m.evalStep1b()
        else m.evalStep1()
        return false
   },
    getClass: function(a) {
        var m = this, o = m.cats, i, p
        for (i = 0; i < o.length; i++) { p = o[i].split('|'); if (p[2] == a) return 'i' + p[1] }
        return ''
    },
    prepare: function() {
        var m = this, o = m.items = [], i, q = m.cats, oo = [], o2 = [], v, p, x, xx, x2, o1, o2, qq, q2, qt
        if ($get('fFilterA')) m.hasRadio = $get('fFilterA').value != ''
        if (m.hasRadio) {
            if (m.preselect) $$('input', $get('fFI'))[m.preselect - 1].checked = 'checked'
            qq = ($$('label', $get('fFI')))
            q2 = ($$('label', $get('check2')))
            $get('filterAll').style.display = 'none'
            qt = m.getClass($get('fFilterA').value)
            qq[0].className = qt
            q2[0].className = qt
            qt = m.getClass($get('fFilterB').value)
            qq[1].className = qt
            q2[1].className = qt
        }
        else {

            try { $get('eLabel2').style.display = 'none'; $get('fFI').style.display = 'none'; $get('fRadio0').style.display = 'none'; $get('fRadio1').style.display = 'none' }

            catch (e) { }
            for (i = 0; i < 9; i++) { if (m.cats[i].split('|')[3] == 'TRUE') o.push(m.cats[i]) }
            if (o.length == 0) {
                $get('filterAll').style.display = 'none'
                m.hasServ = false
            }
            else {
                m.hasServ = true
                o2.push('<ul class="check" id="check2">')
                for (i = 0; i < o.length; i++) {
                    v = o[i].split('|'), p = i + 1
                    var ch = v[4]; if (ch == undefined) ch = "";
                    if (i % 3 == 0) oo.push('<ul class="check">')
                    oo.push('<li><input type="checkbox" name="Option1" id="fOption' + p + '" value="' + v[2] + '" ' + ch + ' /><label for="fOption' + p + '" class="i' + v[1] + '">' + v[0] + '</label></li>')
                    if (i % 3 == 2) oo.push('</ul>')
                    o2.push('<li><input type="checkbox" name="Option2" id="fOption2' + p + '" value="' + v[2] + '" ' + ch + ' /><label for="fOption2' + p + '" class="i' + v[1] + '" title="' + v[0] + '"></label></li>')
                }
                if (i % 3 != 2) oo.push('</ul>')
                o2.push('</ul>')
                $get('checks01').innerHTML = oo.join('\n')
                $get('checks02').innerHTML = o2.join('\n')
            }
        }
        G.expand('step1')
    },
    selectAll: function(a) {
        var m = G.map, o = $get('form1').Option1, i, v
        v = m.allChecked = !m.allChecked
        for (i = 0; i < o.length; i++) o[i].checked = v
    },
    copyVals: function() {
        var m = this, o, t, i
        $get('fAddress2').value = $get('fAddress').value
        if (m.hasRadio) { o = $get('form1').radio1; t = $get('form2').radio2 }
        else { o = $get('form1').Option1; t = $get('form2').Option2 }
        if (o) { for (i = 0; i < o.length; i++) t[i].checked = o[i].checked }
    },
    radioChange: function() {
        var m = G.map, vZip = $get('fZip2').value, vCity = $get('fTown2').value
        if (vZip + vCity != '') m.evalStep1b()
    },
    setMsg: function(a) { var m = this, o = $get('msg' + a); G.collapse(m.activeMsg); G.expand(o); m.activeMsg = o },
    radioVal: function(a) { for (var i = 0; i < a.length; i++) { if (a[i].checked) return a[i].value }; return null },
    initPrintDealer: function() {
        var m = this, g = google.maps, p, mm, im, sh, p1, p2, o, dlr, v = [], vv
        dlr = G.get(m.servicePath + '?xmlid=true&action=dealer&id=' + G.middle(location.search, 'id=', '&')).responseXML
        o = $$('dealer', dlr)[0]
        v.push('<div id="dTitle"><h2>' + o.getAttribute('name') + '</h2></div>')
        v.push('<p class="intro">' + o.getAttribute('address') + '<br />' + o.getAttribute('zip') + ' ' + o.getAttribute('city') + '</p>')
        v.push('<p><strong>' + m.tel + ': </strong>' + o.getAttribute('phone') + '<br />')
        v.push('<strong>' + m.fax + ': </strong>' + o.getAttribute('fax') + '<br />')
        v.push('<strong>' + m.email + ': </strong><a class="mailto-print" href="mailto:' + o.getAttribute('email') + '">' + o.getAttribute('email') + '</a>')
        vv = o.getAttribute('website')

        if (vv) { if (vv != 'null') { v.push('<br /><strong>' + m.websiteBtn + ': </strong>' + m.getLink(vv)) } }
        v.push('</p>')
        m.addServices(o, v)
        $get('rDetail').innerHTML = v.join('')
        h2 = $$('h2', $get('rDetail'))[0]
        if (h2.offsetHeight > 40) h2.style.fontSize = '11px'
        p = new g.LatLng(o.getAttribute('lat'), o.getAttribute('lon'))
        m.map = o = new g.Map($get('map01'), { zoom: 13, center: p, mapTypeId: g.MapTypeId.ROADMAP, disableDefaultUI: false })
        p1 = new g.Point(0, 0); p2 = new g.Point(16.5, 41)
        im = new g.MarkerImage(m.icoBase + 'markerSP.png', new g.Size(33, 41), p1, p2)
        sh = new g.MarkerImage(m.icoBase + 'shadow-markerSP.png', new g.Size(54, 41), p1, p2)
        m.mark = new g.Marker({ map: o, position: p, icon: im, shadow: sh })
    },
    initRsbDealer: function() {
        var m = this, g = google.maps, p, mm, im, sh, p1, p2, o, dlr, v = [], vv, u, w
        alert(m.servicePath);
        dlr = G.get(m.servicePath + '?xmlid=true&action=dealer&id=' + G.middle(location.search, 'id=', '&')).responseXML
        o = $$('dealer', dlr)[0]
        v.push('<div class="noprint"><a href="javascript:window.print()" class="btnSm" id="printBtnRSB"><b>Print</b></a><a href="javascript:G.map.RouteEmail()" class="btnSm" id="emailBtnRSB"><b>E-mail</b></a><a href="javascript:G.map.GPSExport()" class="btnSm" id="emailBtnRSB"><b>GPS Export</b></a><a href="javascript:G.map.Directions()" class="btnSm" id="emailBtnRSB"><b>Directions</b></a></div>')
        v.push('<p></p>')
        v.push('<div id="dTitle"><h2>' + o.getAttribute('name') + '</h2></div>')
        v.push('<p class="intro">' + o.getAttribute('address') + '<br />' + o.getAttribute('zip') + ' ' + o.getAttribute('city') + '</p>')
        v.push('<p><strong>' + m.tel + ': </strong>' + o.getAttribute('phone') + '<br />')
        v.push('<strong>' + m.fax + ': </strong>' + o.getAttribute('fax') + '<br />')
        w = o.getAttribute('email')
        v.push('<strong>' + m.email + ': </strong><a href="mailto:' + w + '" title="' + w + '">' + w + '</a>')
        vv = o.getAttribute('website')
        u = m.getLink(vv)

        if (vv) {
            if (vv != 'null') {
                if ((u.indexOf("http://") == -1) && (u.indexOf("/") != 1)) { u = 'http://' + u }
                v.push('<br /><strong>' + m.websiteBtn + ': </strong>' + '<a href="' + u + '" target="_blank">' + m.getLink(vv) + '</a>')
            }
        }

        v.push('</p>')
        m.addServices(o, v)
        $get('rDetail').innerHTML = v.join('')
        h2 = $$('h2', $get('rDetail'))[0]
        if (h2.offsetHeight > 40) h2.style.fontSize = '11px'
        p = new g.LatLng(o.getAttribute('lat'), o.getAttribute('lon'))
        m.map = o = new g.Map($get('map01'), { zoom: 13, center: p, mapTypeId: g.MapTypeId.ROADMAP, disableDefaultUI: false })
        p1 = new g.Point(0, 0); p2 = new g.Point(16.5, 41)
        im = new g.MarkerImage(m.icoBase + 'markerSP.png', new g.Size(33, 41), p1, p2)
        sh = new g.MarkerImage(m.icoBase + 'shadow-markerSP.png', new g.Size(54, 41), p1, p2)
        m.mark = new g.Marker({ map: o, position: p, icon: im, shadow: sh })
    },
    initPrintRoute: function() {
        var m = this, g = google.maps, p, mm, ds, p, ss = location.search, dlr, o
        m.lat = G.middle(ss, 'lat=', '&')
        m.lon = G.middle(ss, 'lon=', '&')
        m.dlKey = G.middle(ss, 'id=', '&')
        dlr = G.get(m.servicePath + '?xmlid=true&action=dealer&id=' + G.middle(location.search, 'id=', '&')).responseXML
        o = m.target = $$('dealer', dlr)[0]
        p = new g.LatLng(m.lat, m.lon)
        p2 = new g.LatLng(o.getAttribute('lat'), o.getAttribute('lon'))
        m.map = new g.Map($get('map01'), { zoom: 13, center: p, mapTypeId: g.MapTypeId.ROADMAP, disableDefaultUI: false })
        ds = new g.DirectionsService()
        q = { origin: p, destination: p2, travelMode: g.DirectionsTravelMode.DRIVING }
        ds.route(q, function(result, status) {
            var m = G.map, g = google.maps, dd, r, v = m.target
            if (status == g.DirectionsStatus.OK) {
                dd = new g.DirectionsRenderer()
                dd.setMap(m.map)
                dd.setPanel($get('pr_dt'))
                dd.setDirections(result)

	r = result.routes[0].legs[0]
	$get('gd1').innerHTML = r.start_address
	$get('gd2').innerHTML = m.getAdr(v) 
                $get('gd3').innerHTML = r.duration.text
                $get('gd4').innerHTML = r.distance.text
            }
            else alert('Nessun risultato corrispondente all’indirizzo inserito')
        })
    },
    getAdr: function(a) {
        var v = []
        v.push(a.getAttribute('name'))
        v.push(a.getAttribute('address'))
        v.push(a.getAttribute('zip') + ' ' + a.getAttribute('city'))
        return v.join(', ')
    }
}

function animate(a,b,animType) {
                var time = 250;
                var e = document.getElementById(a);
                if (e == null) {return;}
                var c = parseInt(e.style.height);
                if (animType == 'top')
                                c = parseInt(e.offsetTop);
                var tf = 1;
                if (time > 0) {tf = time / 40;}
                var f = b - c;
                if (f != 0) {f /= tf;}
                doFrame(a,c,b,f,animType);
}
function doFrame(e, c, n, f, animType) {
                var el = document.getElementById(e);
                if (el == null) return;
                c = moveSingleVal(c, n, f);
                if (animType == 'top')
                                {el.style.top = Math.round(c) + 'px';}
                else 
                                {el.style.height = Math.round(c) + 'px';}
                if (c == n) return;
                setTimeout('doFrame("' + e + '",' + c + ',' + n + ',' + f + ',"' + animType + '");', 40);
}
function moveSingleVal(curVal, fVal, frmAmt) {
                if (frmAmt == 0 || curVal == fVal) return fVal;
                curVal += frmAmt;
                if ((frmAmt > 0 && curVal >= fVal) || (frmAmt < 0 && curVal <= fVal)) { return fVal; }
                return curVal;
}
