﻿
function initCarDetails(eId, pricegroup, cargroup, pickupid) {
    if (typeof (cds) === 'undefined') {
        var cds = new CarDetails();
        cds.init(eId, pricegroup, cargroup, pickupid);
    } else {
        cds.init(eId, pricegroup, cargroup, pickupid);
    }
}

function CarDetails() {

    this.ajaxObj = null;
    this.elementId = null;
    this.pg = null;
    this.cg = null;
    this.puid = null;

    this.init = function(eId, pricegroup, cargroup, pickupid) {
        this.elementId = eId;
        if (this.$(this.elementId + '_cardetails').style.display === 'block') {
            this.$(this.elementId + '_cardetails').style.display = 'none';
        } else if (pricegroup.toString.length > 0 && cargroup.toString.length > 0 && pickupid > 0) {
            this.pg = pricegroup;
            this.cg = cargroup;
            this.puid = pickupid;
            this.sendCarDetailsRequest();
        }
    }

    this.sendCarDetailsRequest = function() {
        this.ajaxObj = this.createAjaxObj();
        var instance = this;
        this.ajaxObj.onreadystatechange = function() {
            if (this.readyState === 4) {
                if (this.status === 200) {
                    if (this.responseXML) {
                        //HIDE LOADER GIF
                        instance.$(instance.elementId + '_cardetailsloadericon').style.display = 'none';
                        if (this.responseXML.getElementsByTagName('CarDescription')) {
                            var root = this.responseXML.getElementsByTagName('CarDescription');
                            if (root[0].childNodes[0] != null) {
                                instance.$(instance.elementId + '_inclusions').innerHTML = root[0].childNodes[0].firstChild.nodeValue;
                            }
                            if (root[0].childNodes[1] != null) {
                                instance.$(instance.elementId + '_exclusions').innerHTML = root[0].childNodes[1].firstChild.nodeValue;
                            }
                            if (root[0].childNodes[2] != null) {
                                instance.$(instance.elementId + '_licence').innerHTML = root[0].childNodes[2].firstChild.nodeValue;
                            }
                            if (root[0].childNodes[3] != null) {
                                instance.$(instance.elementId + '_minmaxage').innerHTML = root[0].childNodes[3].firstChild.nodeValue;
                            }
                        }
                    }
                }
            }
        }

        this.ajaxObj.open('GET', '/utility/cardetailsxml.aspx?pricegroup=' + this.pg + '&cargroup=' + this.cg + '&pickupid=' + this.puid + '&cachekiller=' + this.cacheKiller(), true);
        this.ajaxObj.send('');
        this.displayLoaderIcon();
    }

    this.displayLoaderIcon = function() {
        this.$(this.elementId + '_cardetails').style.display = 'block';
        this.$(this.elementId + '_cardetailsloadericon').style.display = 'block';
    }

    this.createAjaxObj = function() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        } else {
            return;
        }
    }

    this.cacheKiller = function() {
        var now = new Date();
        return now.getMilliseconds();
    }

    this.$ = function() {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element === 'string')
                element = document.getElementById(element);
            if (arguments.length === 1)
                return element;
            elements.push(element);
        }
        return elements;
    }

    this.endRequest = function() {
        this.ajaxObj = null;
        CollectGarbage()
    }

    if (window.addEventListener) {
        //window.addEventListener('load', function() { this.init(); }, false);
        window.addEventListener('unload', function() { this.endRequest(); }, false);
    } else if (window.attachEvent) {
        //window.attachEvent('onload', this.init);
        window.attachEvent('onunload', this.endRequest);
    } else {
        //window.onload = function() { this.init() };
        window.onunload = function() { this.endRequest(); };
    }
    
}
