var myRandomListRequest = null;
function setRandomListQueryString() {
    var queryString = '';
    var frm = document.random_list;
    var numberElements = frm.elements.length;
    for( var i = 0; i < numberElements; i++ ) {
        if( i < numberElements - 1 ) {
            queryString += frm.elements[i].name + '=' +
            encodeURIComponent(frm.elements[i].value) + '&';
        } else {
            queryString += frm.elements[i].name + '=' +
            encodeURIComponent(frm.elements[i].value);
        }
    }
    return queryString;
}

function sendRandomListData() {
    document.getElementById('random_list_loading').style.display = 'block';
    document.getElementById('random_list_location').style.display = 'none';
    var queryString = setRandomListQueryString();
    var url = '/cgi-bin/ajax';
    myRandomListRequest = initHttpRequest();
    httpRequest(myRandomListRequest,'POST',url,true,handleRandomListResponse,queryString);
}

function handleRandomListResponse() {
    if( myRandomListRequest.readyState == 4 ) {
        if( myRandomListRequest.status == 200 ) {
            var doc = myRandomListRequest.responseXML;
            extractRandomListXML(doc);
        } else {
            alert('A problem occurred with the XMLHttpRequest Object');
        }
    }
}

function extractRandomListXML(doc) {
    var root = doc.documentElement;
    var tmp = document.getElementById('random_list_location');
    var html = '';

    if( root.hasChildNodes() ) {
        var nodes = root.childNodes;
        for( var i = 0; i < nodes.length; i++ ) {
            if( nodes[i].nodeName == 'vehicle' ) {
                var img = 'http://' + sub + '.' + dom + '/photos/' + nodes[i].getAttribute('dealer_id') + '_' + nodes[i].getAttribute('id') + '/95/' + nodes[i].getAttribute('ebay_pic');
                html += '<div id="random_list_item"><a href="/cgi-bin/showroom.cgi?d_id=' 
                    + dealer_id + '&mode=show&id=' + nodes[i].getAttribute('id') + '"><img src="' + img
                    + '"><br>' + nodes[i].getAttribute('year') + ' ' + nodes[i].getAttribute('make') + ' ' + nodes[i].getAttribute('model')
                    + '</a></div>';
            }
        }
    }
    tmp.innerHTML = html;
    document.getElementById('random_list_location').style.display = 'block';
    document.getElementById('random_list_loading').style.display = 'none';
}
