﻿var _requestFrame = null;

vGrid = {
    load : function() 
    {
        vGrid.sendRequest(null);
    },
    
    sendRequest : function(request) 
    {
        if(_requestFrame == null) {
            _requestFrame = $('requestFrame');
        }
        
        _requestFrame.src = '/vehicles/VGrid.aspx?' + request;
    },
    
    search : function() 
    {
        var url = 'p=0&make=' + $(window.inputMake).value;
        url += '&body=' + $(window.inputBody).value;
        url += '&gvw=' + $(window.inputGVW).value;
        
        vGrid.sendRequest(url);
        
        //alert('here');
        
        return false;
    }
};

function vGrid_pageResults(pageId) {

    var url = 'p=' + pageId + '&make=' + $(window.inputMake).value;
    url += '&body=' + $(window.inputBody).value;
    url += '&gvw=' + $(window.inputGVW).value;

    window.location = '#page=' + pageId;
    vGrid.sendRequest(url);
}

function receiveRequest(html) {
    if(_requestFrame == null) {
        _requestFrame = $('requestFrame');
    }
    
    $('vGridContainer').innerHTML = html;
}

function storeDDlValue(obj_id, target_id) {
    
    if($(obj_id) != null) {
        $(target_id).value = $(obj_id).value;
    }
    
    getSearchData();

}

function getSearchData() {
    var make = $(window.ddlMake).value;
    var body = $(window.ddlBody).value;
    var gvw = $(window.ddlGVW).value;

    var _req = xeAjaxRequest.createRequest();
    
    _req.open('GET', '/_common/controls/_search.aspx?body=' + body + '&make=' + make + '&gvw=' + gvw, true);
    
    _req.onreadystatechange = function() {
        if(_req.readyState == 4) {
            if(_req.status == 200) {
                processSearchData(_req.responseText);
            }
            else {
                alert('error');
            }
        }
    };
    
    _req.send(null);
}

function processSearchData(o) {
    var obj = eval("(" + o + ")");
    var ddl_id = null;
    
    // makes
    clearOptions(window.ddlMake);
    insertOption(window.ddlMake, 'All makes', '-1');
    
    for(var i=0; i<obj.makes.length; i++)
    {
        insertOption(window.ddlMake, obj.makes[i].Text, obj.makes[i].Value);
    }
    
    // bodies
    clearOptions(window.ddlBody);
    insertOption(window.ddlBody, 'All body types', '-1');
    
    for(var i=0; i<obj.bodies.length; i++)
    {
        insertOption(window.ddlBody, obj.bodies[i].Text, obj.bodies[i].Value);
    }
    
    // gvw
    clearOptions(window.ddlGVW);
    insertOption(window.ddlGVW, 'All GVWs (weight)', '-1');
    
    for(var i=0; i<obj.gvw.length; i++)
    {
        insertOption(window.ddlGVW, obj.gvw[i].Text, obj.gvw[i].Value);
    }
}

function clearOptions(ddl) {
    var _select = $(ddl);
    
    for (var x = (_select.options.length - 1); x >= 0; x--) {
        _select.options[x] = null;
    }
}

function insertOption(ddl, text, value) {
    var _op = document.createElement('option');
        
    _op.text = text;
    _op.value = value;
    
    if( ($(ddl).id).indexOf(window.ddlMake) > -1 ) {
        if($(window.inputMake).value == value) {
            _op.selected = true;
        }
    }
    
    if( ($(ddl).id).indexOf(window.ddlBody) > -1 ) {
        if($(window.inputBody).value == value) {
            _op.selected = true;
        }
    }
    
    if( ($(ddl).id).indexOf(window.ddlGVW) > -1 ) {
        if($(window.inputGVW).value == value) {
            _op.selected = true;
        }
    }

    $(ddl).options.add(_op);
}

function registerSafeLoad(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
