﻿coachSearch = {
    
    appPath: '/used-vehicles/search/_coachSearch.aspx',
    selects: null,
    button: null,
    
    make: null,
    seats: null,
    euro: null,
    price: null,
    
    getCoachesPath: '/used-vehicles/search/_coachResults.aspx',
    pnlCoachesId: 'pnlCoaches',
    pnlCoaches: null,
    
    shortlistPath: '/used-vehicles/shortlist/_shortlist.aspx',
    pnlShortlistId: 'pnlShortlist',
    pnlShortlist: null,
       
    loggedIn: null,
    
    pnlLoadingId: 'pnlLoading',
    pnlLoading: null,
    
    /**
    * Creates a cross-browser AJAX layer
    * @returns XMLHttpRequest
    */
    _createTransport: function() {
        if (typeof XMLHttpRequest != "undefined") {
            return new XMLHttpRequest();
        }
        else if (typeof ActiveXObject != "undefined") {
            var http = null;
            try {
                http = new ActiveXObject("MSXML2.XmlHttp.6.0");
                return http;
            }
            catch (ex) {
                try {
                    http = new ActiveXObject("MSXML2.XmlHttp.3.0");
                    return http;
                }
                catch (ex2) {
                    throw Error("Cannot create XHR object");
                }
            }
        }
    },
    
    init: function(containerId, loggedIn) {
        this.loggedIn = loggedIn.toLowerCase();
        
        this.pnlLoading = $(this.pnlLoadingId);
        if (this.pnlLoading) this.pnlLoading.style.display = 'block';
        
        var box = document.getElementById(containerId);
        box.style.display = 'block';
        
        this.selects = box.getElementsByTagName('SELECT');
        var links = box.getElementsByTagName('A');
        
        for (var i=0; i < links.length; i++)
        {
            if (links[i].className == 'searchButton') {
                this.button = links[i];
                this.button.onclick = function() {
                    coachSearch.searchCoaches();
                    return false;
                }
            }
        }
        
        for (var i=0; i < this.selects.length; i++)
        {
            this.selects[i].onchange = this.getNewValues;  
        }
    },
    
    getCurrentValues: function() { 
        for (var i=0; i < coachSearch.selects.length; i++)
        {
            if (coachSearch.selects[i].className.indexOf('make') != -1)
                coachSearch.make = coachSearch.urlEncode(coachSearch.selects[i].value);
                
            if (coachSearch.selects[i].className.indexOf('seats') != -1)
                coachSearch.seats = coachSearch.urlEncode(coachSearch.selects[i].value);
                
            if (coachSearch.selects[i].className.indexOf('euro') != -1)
                coachSearch.euro = coachSearch.urlEncode(coachSearch.selects[i].value);
                
            if (coachSearch.selects[i].className.indexOf('price') != -1)
                coachSearch.price = coachSearch.urlEncode(coachSearch.selects[i].value);
                
            //alert(coachSearch.selects[i].value);
        }
    },
    
    getNewValues: function() {
        coachSearch.getCurrentValues();
    
        //Disable elements
        coachSearch.button.disabled = true;
        for (var i=0; i < coachSearch.selects.length; i++)
        {
            coachSearch.selects[i].disabled = true;
            coachSearch.selects[i].style.backgroundColor = '#ffffff';
        }
        //**
        
        var url = coachSearch.appPath + '?make=' + coachSearch.make + '&seats=' + coachSearch.seats + '&euro=' + coachSearch.euro + '&price=' + coachSearch.price;
        //alert(url);
        
        var transport = coachSearch._createTransport();
        transport.open('get', url, true);
        transport.onreadystatechange = function() {
            if (transport.readyState == 4) {
                if (transport.status == 200) {
                    //alert('here');
                    coachSearch.refillLists(transport.responseText);
                } else {
                    //alert('error');
                }
            }
        }
        transport.send(null);
    },
    
    refillLists: function(response)
    {
        var resp = eval("(" + response + ")");

        for (var i=0; i < coachSearch.selects.length; i++)
        {
            var list = coachSearch.selects[i];
         
            list.options.length = '';
            var emptyOpt = document.createElement('option');
            emptyOpt.value = '0';
            emptyOpt.text = 'Select';
            list.options.add(emptyOpt);
            
            if (list.className.indexOf('make') != -1)
            {          
                for (var x=0; x < resp.make.length; x++) {                  
                    var option = document.createElement('option');
                    option.text = resp.make[x].Value;
                    option.value = resp.make[x].Value;
                    list.options.add(option);
                } 
                
                list.value = coachSearch.urlDecode(coachSearch.make);                  
            }
            else if (list.className.indexOf('seats') != -1)
            {
                for (var x=0; x < resp.seats.length; x++) {
                    var option = document.createElement('option');
                    option.text = resp.seats[x].Value;
                    option.value = resp.seats[x].Value;
                    list.options.add(option);
                }
                
                list.value = coachSearch.urlDecode(coachSearch.seats);
            }
            else if (list.className.indexOf('euro') != -1)
            {
                for (var x=0; x < resp.euro.length; x++) {
                    var option = document.createElement('option');
                    option.text = resp.euro[x].Value;
                    option.value = resp.euro[x].Value;
                    list.options.add(option);
                } 
                
                list.value = coachSearch.urlDecode(coachSearch.euro); 
            }
            else if (list.className.indexOf('price') != -1)
            {
                for (var x=0; x < resp.price.length; x++) {
                    var option = document.createElement('option');
                    option.text = resp.price[x].Text;
                    option.value = resp.price[x].Value;
                    list.options.add(option);
                }     
                
                list.value = coachSearch.urlDecode(coachSearch.price);            
            }                
        }
        
        //Enable elements
        coachSearch.button.disabled = false;
        for (var i=0; i < coachSearch.selects.length; i++)
        {
            coachSearch.selects[i].disabled = false;
        }
        //**
    },
    
    searchCoaches: function() {
        coachSearch.getCurrentValues();
        
        coachSearch.pnlCoaches = $(coachSearch.pnlCoachesId);
        
        var value = window['redirectVehicleSearch'];

        if(value != 'true')
        {
            if (coachSearch.pnlLoading) {
                coachSearch.pnlLoading.style.display = 'block';
                coachSearch.pnlCoaches.style.display = 'none';
            }    
            
            var url = coachSearch.getCoachesPath + '?make=' + coachSearch.make + '&seats=' + coachSearch.seats + '&euro=' + coachSearch.euro + '&price=' + coachSearch.price + '&timestamp=' + new Date();
            //alert(url);
            var transport = coachSearch._createTransport();
            transport.open('get', url, true);
            transport.onreadystatechange = function() {
                if (transport.readyState == 4) {
                    if (transport.status == 200) {
                        coachSearch.displayCoaches(transport.responseText);
                    } else {
                        //alert('error');
                    }
                }
            }
            transport.send(null);
        }
        else
        {            
            var url = '/used-vehicles/?doSearch=true&make=' + coachSearch.make + '&seats=' + coachSearch.seats + '&euro=' + coachSearch.euro + '&price=' + coachSearch.price;
            window.location = url;
        }
    },
    
    displayCoaches: function(response) {
        var resp = eval("(" + response + ")");
        var html = '';
        
        for (var i=0; i < resp.length; i++) {
            if ((i + 1) % 3 != 0)
                html += '<div class="coach-item">';
            else
                html += '<div class="coach-item" style="margin-right: 0;">';
                
            html += '    <div class="date">' + resp[i].RegDate + '</div>';
            html += '        <img src="' + resp[i].Img + '" alt="' + resp[i].AltText + '" />';
            html += '        <div class="name" >';
            html += '            ' + resp[i].Make + '<br />';
            html += '            Price: ' + resp[i].Price;
            html += '        </div>';
            html += '        <div class="link">';
            
            if (resp[i].Shortlisted == 'true') 
                html += '                <img src="/_common/img/shortlisted.gif" alt="Vehicle in my shortlist" />';
            
            if (resp[i].UnderOffer == 'false')
                html += '       <a href="/used-vehicles/details/?coachId=' + resp[i].CoachId + '" class="link-internal" title="View more information">View more information</a><br />';
            else
                html += '       <img src="/_common/img/under_offer.gif" alt="Under Offer" style="width: 180px; height: 30px; border: 0px;" /><br />';
            
            html += '        </div>';
                       
            if (coachSearch.loggedIn == 'true') {
                html += '        <div class="buttons">';
                html += '           <a href="/used-vehicles/details/?coachId=' + resp[i].CoachId + '&mode=edit" class="button edit" title="Edit this Coach">Edit</a>';
                html += '           <a href="/used-vehicles/details/?coachId=' + resp[i].CoachId + '&mode=delete" class="button delete" title="Delete this Coach" onclick="return confirm(\'Are you sure you want to delete this coach?\');">Delete</a>';
                html += '        </div>';
            }
            
            html += '   </div>';
        }
                           
        coachSearch.pnlCoaches.innerHTML = html;
        
        if (coachSearch.pnlLoading) {
            coachSearch.pnlLoading.style.display = 'none';
            coachSearch.pnlCoaches.style.display = 'block';
        }
    },
    
    getShortlist: function() {        
        coachSearch.pnlShortlist = $(coachSearch.pnlShortlistId);
        
        if (coachSearch.pnlLoading) {
            coachSearch.pnlLoading.style.display = 'block';
            coachSearch.pnlShortlist.style.display = 'none';
        }
        
        var url = coachSearch.shortlistPath + '?timestamp=' + new Date();
        //alert(url);
        
        var transport = coachSearch._createTransport();
        transport.open('get', url, true);
        transport.onreadystatechange = function() {
            if (transport.readyState == 4) {
                if (transport.status == 200) {
                    coachSearch.displayShortlist(transport.responseText);
                } else {
                    //alert('error');
                }
            }
        }
        transport.send(null);
    },
    
    displayShortlist: function(response) {
        var resp = eval("(" + response + ")");
        var html = '';
        
        for (var i=0; i < resp.length; i++) {
            if ((i + 1) % 3 != 0)
                html += '<div class="coach-item">';
            else
                html += '<div class="coach-item" style="margin-right: 0;">';
                
            html += '    <div class="date">' + resp[i].RegDate + '</div>';
            html += '        <img src="' + resp[i].Img + '" alt="' + resp[i].AltText + '" />';
            html += '        <div class="name" >';
            html += '            ' + resp[i].Make + '<br />';
            html += '            Price: ' + resp[i].Price;
            html += '        </div>';
            html += '        <div class="link">';
            html += '            <a href="/used-vehicles/details/?coachId=' + resp[i].CoachId + '" class="link-internal" title="View more information">View more information</a><br />';
            html += '        </div>';
            
            /*if (coachSearch.loggedIn == 'true') {
                html += '        <div class="buttons">';
                html += '           <a href="/used-vehicles/details/?coachId=' + resp[i].CoachId + '&mode=edit" class="button" title="Edit this Coach">Edit</a>';
                html += '           <a href="/used-vehicles/details/?coachId=' + resp[i].CoachId + '&mode=delete" class="button" title="Delete this Coach" onclick="return confirm(\'Are you sure you want to delete this coach?\');">Delete</a>';
                html += '        </div>';
            }*/
            
            html += '   </div>';
        }

        coachSearch.pnlShortlist.innerHTML = html;
        
        if (coachSearch.pnlLoading) {
            coachSearch.pnlLoading.style.display = 'none';
            coachSearch.pnlShortlist.style.display = 'block';
        }
    },
    
    urlEncode: function(url) {
        return escape(url).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
    },
    
    urlDecode: function(url) {
        return unescape(url.replace('+', ' '));
    }
};
