﻿function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function checkForTAC(chkTC) {
    if (chkTC.checked == true) {
        document.getElementById("ctl00_Main_submitQuote").disabled = false;
    }
    else {
        document.getElementById("ctl00_Main_submitQuote").disabled = true;
    }


}

function initiateRedirect() {
    setTimeout("redirect()", 5000);
}

function redirect() {
    window.location = "retail.aspx";
}



//DROPDOWN
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id) {
    // cancel close timer
    mcancelclosetime();

    // close old layer
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    // get new layer and show it
    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.visibility = 'visible';
    ddmenuitem.style.zIndex = 10000;

}
// close showed layer
function mclose() {
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
    closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}


// close layer when click-out
document.onclick = mclose;

//*Clear the vehicle search form*//
function clearform() {
    document.getElementById("ctl00_Main_txtIDSearch").value = "";
    document.getElementById("ctl00_Main_grid").style.visibility = "hidden";
}



///**Office Selection JavaSript**///
var selectedOffice = "office1"

function newOfficeSelected(newSelectedOffice) {
    if (newSelectedOffice.id != selectedOffice) {
        document.getElementById(selectedOffice).className = "officeContact"
        newSelectedOffice.className = "officeContactSelected"
        selectedOffice = newSelectedOffice.id;
        initialize(newSelectedOffice.id);
    }
}

/*Google Maps*/


var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;

/*MTR geolocation -36.93014468208821, 174.83687634736026*/
var mtr = {
    id:'Head Office',
    lat: -36.93014468208821,
    lon: 174.83687634736026,
    content: "<div id='office4' class='officeMarker'><strong>Head Office:</strong><br />8 - 14 Mt Richmond Drive<br />PO Box 22-740<br />Otahuhu, Auckland<br /><strong>T</strong> 09-270-6990 <strong>F</strong> 09-276-6498<br /><strong>E</strong> <strong><a href='#'>sales@thecdg.co.nz</a></strong></div>"
}

/*K&N - Tahuna geolocation -37.50023584241674, 175.49311863690946*/
var morr = {
    id:'Waikato',
    lat: -37.50023584241674,
    lon: 175.49311863690946,
    content: "<div id='office4' class='officeMarker'><strong>Waikato:</strong><br />Tahuna-Morrinsville Road<br />P O Box 235<br />Morrinsville, Waikato<br /><strong>T</strong> 07-887-5800 <strong>F</strong> 07-889-0050<br /><strong>E</strong> <strong><a href='#'>transport@thecdg.co.nz</a></strong></div>"
}


/*WELL geolocation -41.138717171151605, 174.83229623538*/
var well = {
    id:'Wellington',
    lat: -41.138717171151605,
    lon: 174.83229623538,
    content: "<div id='office4' class='officeMarker'><strong>Wellington:</strong><br />Central Park Business Centre<br />P O Box 50002<br />Porirua, Wellington<br /><strong>T</strong> 04-237-8079 <strong>F</strong> 04-237-2405 <br /><strong>E</strong> <strong><a href='#'>wellops@thecdg.co.nz</a></strong></div>"
}

/*CHC geolocation -43.53863908680189, 172.69937436441808*/
var chc = {
    id:'Christchurch',
    lat: -43.53863908680189,
    lon: 172.69937436441808,
    content: "<div id='office4' class='officeMarker'><strong>Christchurch:</strong><br />59 Newton Street Bromley<br />PO BOX 19524<br />Woolston, Christchurch<br /><strong>T</strong> 03-384-2671 <strong>F</strong> 03-384-4356 <br /><strong>E</strong> <strong><a href='#'>chops@thecdg.co.nz</a></strong></div>"
}



function initialize(selectedOffice) {
    
    var latlng = null;
    var office = null;

    switch (selectedOffice) {
        case "office1":
            latlng = new google.maps.LatLng(mtr.lat, mtr.lon);
            office = mtr;
            break;
        case "office2":
            latlng = new google.maps.LatLng(morr.lat, morr.lon);
            office = morr;
            break;
        case "office3":
            latlng = new google.maps.LatLng(well.lat, well.lon);
            office = well;
            break;
        case "office4":
            latlng = new google.maps.LatLng(chc.lat, chc.lon);
            office = chc;
            break;
        default:
            alert("There is no office at this location!");

    }
    
    var myOptions = {
        zoom: 11,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    geocoder = new google.maps.Geocoder(office);


    setupEvents();
    centerChanged();
    addMarkerAtCenter(office);
}

function setupEvents() {
    reverseGeocodedLast = new Date();
    centerChangedLast = new Date();

    setInterval(function() {
        if ((new Date()).getSeconds() - centerChangedLast.getSeconds() > 1) {
            if (reverseGeocodedLast.getTime() < centerChangedLast.getTime())
                reverseGeocode();
        }
    }, 1000);

    google.maps.event.addListener(map, 'center_changed', centerChanged);

}

function getCenterLatLngText() {
    return '(' + map.getCenter().lat() + ', ' + map.getCenter().lng() + ')';
}

function centerChanged() {
    centerChangedLast = new Date();
    var latlng = getCenterLatLngText();
    currentReverseGeocodeResponse = null;
}

function reverseGeocode() {
    reverseGeocodedLast = new Date();
    geocoder.geocode({ latLng: map.getCenter() }, reverseGeocodeResult);
}

function reverseGeocodeResult(results, status) {
    currentReverseGeocodeResponse = results;
}


function geocode() {
    var address = document.getElementById("address").value;
    geocoder.geocode({
        'address': address,
        'partialmatch': true
    }, geocodeResult);
}

function geocodeResult(results, status) {
    if (status == 'OK' && results.length > 0) {
        map.fitBounds(results[0].geometry.viewport);
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
}

function addMarkerAtCenter(officeMarker) {
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        map: map
    });

    var infowindow = new google.maps.InfoWindow({ content: officeMarker.content });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
    });
}
