// -- tabs
// var tab_sets = new Array;
var tab_sets = [];
function tab_set_containing( tab ) {
    for ( var i = 0; i < tab_sets.length; i++ ) {
        // grep
        for ( var j = 0; j < tab_sets[i].length; j++ ) {
            if ( tab_sets[i][j] == tab ) {
                return tab_sets[i];
            }
        }
    }
    return [];
}
function add_tab_set( set ) {
// no .push in MacOS MSIE 5.2
//    tab_sets.push( set );
// still causing problems in 5.2
    tab_sets[tab_sets.length] = set;
}
function show_default_tabs() {
    for ( var i = 0; i < tab_sets.length; i++ ) {
        if ( tab_sets[i].length > 0 ) {
            show_tab( tab_sets[i][0] );
        }
    }
}
function show_tab( tab ) {
    var ids = tab_set_containing( tab );
    for (var i = 0; i < ids.length; i++ ) {
        var id = ids[i];
        var element = document.getElementById( id );

        // we don't always render all tabs, see 'ONLY_SHOW_DEFAULT'
        if ( element != null ) {
            if ( id == tab ) {
                element.style.display = '';
            } else {
                element.style.display = 'none';
            }
        }
    }

    // specific to the matrix pages
    configure_bodyshape( tab );
    filter();
}

// raise tabs based on referer
function smart_tabs() {
    if ( document.referrer ) {
        if ( document.referrer.match( /(film|video|tube|picture|pics|photo|review|news|forum)/i ) ) {
            show_tab( 'tab_reviews' );
        } else if ( document.referrer.match( /(bhp|mpg|co2|ppm|nox|emission|speed|power|performance|weight|dimension|size|length|width|height|capacity|tech|spec)/i ) ) {
            show_tab( 'tab_technical' );
        }
    }
}

// bounce to the correct whitelabel
function from_confused() {
    if ( document.referrer && document.referrer.match( /confused.com/i ) ) {
        if ( window.location ) {
            var location = window.location + '';
            if ( ! location.match( /confused/i ) ) {
                window.location = 'http://www.carpricechecker.co.uk/confused/?ref=confused.com';
            }
        }
    }
}

// change the bodyshape drop-down
// alert('create tab_data');
var tab_data = new Object;
function configure_bodyshape( tab ) {


// var properties = '';
// for ( var p in tab_data ) {
// properties = properties + ' ' + p;
// }
// alert('properties=' + properties);

    var default_bodyshapes = [ 'HATCHBACK', 'ESTATE', 'SALOON', 'COUPE', 'OTHER' ];

    var shapes = tab_data[ tab ];

// alert("cb='" + tab + "', shapes='" + shapes + "'");

    if ( shapes == undefined ) {
        shapes = default_bodyshapes;
    }

// alert('got shapes' + shapes);
    var bodyshape = document.getElementById( 'SHOW_BODYSHAPE' );

    if ( bodyshape == undefined ) { return; };

// alert('got bodyshape dropdown');

    var body = bodyshape.options[bodyshape.selectedIndex].value;
    if ( body == 'OTHER' ) {
        // db.pm expand gives a default body shape of 'unknown'
        body = 'unknown';
    }

// alert('rewrite bodyshape (' + tab + '):' + shapes); 

    // rewrite the bodyshape dropdown
    bodyshape.options.length = 0;
    var is_default = true;
    for ( var index = 0; index < shapes.length; index++ ) {
        var is_selected = ( shapes[index] == body );
        bodyshape.options[index] = new Option( shapes[index], shapes[index], is_default, is_selected );
        is_default = false;
    }
}

// -- detail overlib
var detail_overlib_cache = new Object;
var detail_current_key = null;
function detail_overlib_mouseout() {
    stop_this( 'detail_overlib' );
    detail_current_key = null;
    return nd();
}

function detail_overlib_mouseover(stock_id, cap_id, can_add_to_wishlist, wishlist_style) {
    var key = stock_id + ' ' + cap_id;

    if ( detail_overlib_cache[key] ) {
        overlib( detail_overlib_cache[key],
            FGCOLOR, '#FFFFFF',
            BGCOLOR, '#000000',
            HAUTO, VAUTO,
            WIDTH, 300, HEIGHT, 320,
            DELAY, 250, STICKY, MOUSEOFF, FOLLOWMOUSE,
            OFFSETX, 1, OFFSETY, 1 );
        return;
    }


    do_this_later( function () {

    detail_overlib_cache[key] = '<IMG ALIGN=MIDDLE WIDTH=40 HEIGHT=40 SRC=/images/cpc.gif>';
    overlib( detail_overlib_cache[key],
        HAUTO, VAUTO,
        WIDTH, 40, HEIGHT, 40,
        DELAY, 250, FULLHTML, STICKY, MOUSEOFF, FOLLOWMOUSE,
        OFFSETX, 0, OFFSETY, 0 );

    var http = xml_http_request();
    detail_current_key = key;
    http.open('GET', '/checker/overlib.cgi?can_add_to_wishlist=' + can_add_to_wishlist + '&num_alternatives=2&stock_id=' + stock_id + '&cap_id=' + cap_id + '&wishlist_style=' + wishlist_style, true);
    http.onreadystatechange = function () {
                                  if ( http.readyState == 4 ) {
                                      detail_overlib_cache[key] = http.responseText;
                                      if ( detail_current_key == key ) {
                                          overlib( detail_overlib_cache[key],
                                              FGCOLOR, '#FFFFFF',
                                              BGCOLOR, '#000000',
                                              HAUTO, VAUTO,
                                              WIDTH, 300, HEIGHT, 320,
                                              DELAY, 250, STICKY, MOUSEOFF, FOLLOWMOUSE,
                                              OFFSETX, 0, OFFSETY, 0  );
                                      }
                                  }
                              };
    http.send(null);

    }, 250, 'detail_overlib' );
}

// -- xml http request
function xml_http_request() {
    var http = false;
    try {
      http = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      http = false;
    }
    return http;
}

// -- filters
function show_tag( tag ) {
    var control = document.getElementById( 'SHOW_' + tag );
    var elements = document.getElementsByName( 'ROW_' + tag );
    var value = control.checked ? '' : 'none';
    for ( i = 0; i < elements.length; i++ ) {
        elements[i].style.display = value;
    }
}
function show_diesel() { show_tag( 'DIESEL' ) }
function show_automatic() { show_tag( 'AUTOMATIC' ) }
function filter() {
//    filters['ALL'].forEach( turn_it_off );

    if ( typeof(filters) == 'undefined' ) {
        return;
    }

// alert('turn off' + filters['ALL']);

    for_each( filters['ALL'], turn_it_off );

    var diesel    = document.getElementById( 'SHOW_DIESEL' );
    var automatic = document.getElementById( 'SHOW_AUTOMATIC' );
    var bodyshape = document.getElementById( 'SHOW_BODYSHAPE' );
//    var euro4     = document.getElementById( 'SHOW_EURO4' );

    // bodyshape.value busted in msie
//    var body = bodyshape.value;
    var body = bodyshape.options[bodyshape.selectedIndex].value;
    if ( body == 'OTHER' ) {
        // db.pm expand gives a default body shape of 'unknown'
        body = 'unknown';
    }

//    alert( 'diesel.checked='+diesel.checked + ',' + 'automatic.checked='+automatic.checked );

    var turn_on = [];
    var i = 0;
    for ( i = 0; i < filters['ALL'].length; i++ ) {
        var id = filters['ALL'][i];
        if ( (filters[ body ] && my_index_of( filters[ body ], id ) >= 0)
             &&
             ( (filters['PETROL'] && my_index_of( filters['PETROL'], id ) >= 0) ||
               ( diesel.checked && filters['DIESEL'] && my_index_of( filters['DIESEL'], id ) >= 0 ) )
             &&
             ( (filters['MANUAL'] && my_index_of( filters['MANUAL'], id ) >= 0) ||
                 ( automatic.checked && filters['AUTOMATIC'] && my_index_of( filters['AUTOMATIC'], id ) >= 0 ) )
//             &&
//             ( (filters['NON_EURO4'] && my_index_of( filters['NON_EURO4'], id ) >= 0) ||
//               ( euro4.checked && filters['EURO4'] && my_index_of( filters['EURO4'], id ) >= 0 ) )
           ) {
// no .push in MacOS MSIE 5.2
//            turn_on.push( id );
            turn_on[turn_on.length] = id;
        }
    }

    // .forEach is busted too, use a proper loop
    for_each( turn_on, turn_it_on );

    toggle_subheadings();
}

function toggle_subheadings() {
    var rows = document.getElementsByTagName( 'TR' );

    var visible_contents = 0;
    var current_subheading = '';

    // look for each subheading
    var i;
    for ( i = 0; i < rows.length; i++ ) {
        var row_id = rows[i].id;
        if ( row_id.indexOf('SUBHEADING') != -1 ) {

            if ( current_subheading ) {
                // already seen one
                current_subheading.style.display = visible_contents ? '' : 'none';
                visible_contents = 0;
            }

            // working with this one now
            current_subheading = rows[i];
        } else if ( row_id.indexOf('ROW_') != -1 ) {
            visible_contents = visible_contents || ( rows[i].style.display == '' );
        }
    }

    // last one
    if ( current_subheading ) {
        current_subheading.style.display = visible_contents ? '' : 'none';
    }
}

//function turn_it_off(e, i, a) {
function turn_it_off(e) {
    var name = 'ROW_' + e;
    var elements = document.getElementsByName( name );
    var i;
    for ( i = 0; i < elements.length; i++ ) {
        elements[i].style.display = 'none';
    }
}

//function turn_it_on(e, i, a) {
function turn_it_on(e) {
    var name = 'ROW_' + e;
    var elements = document.getElementsByName( name );
    var i;
    for ( i = 0; i < elements.length; i++ ) {
        elements[i].style.display = '';
    }
}

function for_each(l, f) {
    var i;
    for ( i = 0; i < l.length; i++ ) {
        f( l[i] );
    }
}

// .indexOf is busted on Safari
function my_index_of(l, e) {
    var i;
    for ( i = 0; i < l.length; i++ ) {
        if ( l[i] == e ) {
            return i;
        }
    }
    return -1;
}

// pause before doing something
var do_this_later_cache = new Object;
function do_this_later(action, time, tag) {
    do_this_later_cache[tag] = window.setTimeout( action, time );
}
function stop_this( tag ) {
    if ( do_this_later_cache[tag] ) {
        window.clearTimeout( do_this_later_cache[tag] );
    }
    do_this_later_cache[tag] = null;
}

function get_response( url ) {
    var http = xml_http_request();
    // blocking send
    http.open('GET', url, false);
    http.send(null);
    if ( http.status == 200 ) {
        return http.responseText;
    } else {
        return '';
    }
}

function is_msie() {
    var ua = navigator.userAgent;
    if ( ua.indexOf('MSIE') != -1 ) {
        return 1;
    } else {
        return 0;
    }
}

function rnd() {
    return 100000 + Math.round( 100000 * Math.random() );
}

// wishlist LHS column
function load_from_url( id, url ) {
    var http = xml_http_request();
    http.open('GET', url, true);
    http.onreadystatechange = function () {
                                  if ( http.readyState == 4 ) {
                                      document.getElementById( id ).innerHTML = http.responseText;
                                  }
                              };
    http.send(null);
}
function add_to_wishlist( archetype, stock_id, style, whitelabel ) {
    var url_args = [];

    url_args[url_args.length] = 'archetype=' + archetype;
    url_args[url_args.length] = 'add=' + stock_id;

    if ( style ) {
        url_args[url_args.length] = 'style=' + style;
    }

    if ( is_msie() ) {
        url_args[url_args.length] = 'random=' + rnd();
    }

    if ( whitelabel ) {
        url_args[url_args.length] = 'whitelabel=' + whitelabel;
    }

    var url = '/checker/wishlist.cgi';

    if ( url_args.length ) {
        url = url + '?' + url_args.join('&');
    }

    if ( style ) {
        window.location = url;
    } else {
        load_from_url( 'LHS', url );
    }
}
function remove_from_wishlist( row_id, style, whitelabel ) {
    var url_args = [];

    url_args[url_args.length] = 'remove=' + row_id;

    if ( style ) {
        url_args[url_args.length] = 'style=' + style;
    }

    if ( is_msie() ) {
        url_args[url_args.length] = 'random=' + rnd();
    }

    if ( whitelabel ) {
        url_args[url_args.length] = 'whitelabel=' + whitelabel;
    }

    var url = '/checker/wishlist.cgi';

    if ( url_args.length ) {
        url = url + '?' + url_args.join('&');
    }

    if ( style ) {
        window.location = url;
    } else {
        load_from_url( 'LHS', url );
    }
}
function show_wishlist( whitelabel ) {
    var url = '/checker/wishlist.cgi';

    var url_args = [];

    if ( is_msie() ) {
        url_args[url_args.length] = 'random=' + rnd();
    };

    if ( whitelabel ) {
        url_args[url_args.length] = 'whitelabel=' + whitelabel;
    }

    if ( url_args.length ) {
        url = url + '?' + url_args.join('&');
    }

    load_from_url( 'LHS', url );
}

function set_value( fieldname, value ) {
    document.getElementById( fieldname ).value = value;
}
function set_value_if( fieldname, if_value, value ) {
    var v = document.getElementById( fieldname ).value;
    if ( v == if_value ) {
        set_value( fieldname, value );
    }
}

function submit_search( f ) {
    var form = document.getElementById('MAIN_FORM');
    form.action = '/checker/index.cgi';
    form.method = 'GET';
    return true;
}

function update_dpc_and_postcode_fields( value ) {
// alert('update pc fields with ' + value);
    var fields = document.getElementsByName( 'DPC' );
    for ( i = 0; i < fields.length; i++ ) {
        fields[i].value = value;
    }
    var postcode = document.getElementById( 'postcode' );
    if ( postcode ) {
// alert('postcode value set to ' + value);
        postcode.value = value;
    }
}

function submit_search_from_link( f, postcode_field_id ) {
    var form = document.getElementById('MAIN_FORM');

    var postcode = document.getElementById( postcode_field_id ).value;

// alert('postcode=' + postcode);

    if ( ! postcode ) {
// alert('! postcode');
        return true;
    }

    var blank_postcode = ( postcode == '' ) || ( postcode == 'post code' );
    var v_postcode = valid_postcode( postcode );

    var response = '';

    if ( blank_postcode || ! v_postcode ) {
        response = 'Please use a valid post code.  It should look something like AB12 3CD';
    };

    if ( response ) {
        alert( response );
        return false;
    }

    response = get_response( '/checker/verify_postcode.cgi?postcode=' + postcode );

    if ( response != 'Ok' ) {
        alert( response );
        return false;
    }

    update_dpc_and_postcode_fields( postcode );

    form.action = '/checker/index.cgi';
    form.method = 'GET';
    form.submit();
    return true;
}

function submit_search_leasing( f ) {
    var form = document.getElementById('MAIN_FORM');
    form.action = '/checker/leasing_index.cgi';
    form.method = 'GET';
    return true;
}

// optional extras
function format_price(price) {
    var str = price.toString();
    if ( str.length > 3 ) {
        return str.substring(0, str.length - 3) + ',' + str.substring(str.length - 3);
    }
    return str;
}
function optional_extras_reset(f, base_price) {
    var elements = document.getElementsByName( 'OPTION_CHECKBOX' );
    for ( i = 0; i < elements.length; i++ ) {
        elements[i].checked = false;
    }
    sum_checkboxes(f, base_price);
    colourise(f);
    return false;
}
function option_checkbox_onclick(f,id, base_price) {
    sum_checkboxes(f, base_price);
    colourise(f);
    return true;
}

function colourise(f) {

    var elements = document.getElementsByName( 'OPTION_CHECKBOX' );
    for ( i = 0; i < elements.length; i++ ) {
        var value = elements[i].value;
        var arr = value.split(':');
        var option = arr.shift();
        var row = document.getElementById( 'OPTION_ROW_' + option );
        if ( elements[i].checked == true ) {
            row.className = 'option_selected';
        } else {
            row.className = '';
        }
    }

}

function sum_checkboxes(f, base_price) {
    var elements = document.getElementsByName( 'OPTION_CHECKBOX' );

    var options_total = 0;

    for ( i = 0; i < elements.length; i++ ) {
        var value = elements[i].value;
        var arr = value.split(':');
        var option = arr.shift();
        var price = arr.shift();

        if ( elements[i].checked == true ) {
            options_total += parseInt( price );
        }
    }

    document.getElementById( 'CALCULATOR_OPTIONS' ).innerHTML = '&pound; ' + format_price(options_total);

    var total = options_total + base_price;

    document.getElementById( 'CALCULATOR_TOTAL' ).innerHTML = '&pound; ' + format_price(total);
}

function disable_enter_key( event ) {
    var key = window.event ? event.keyCode : event.which;
    return key != 13;
}

function about_retailer( retailer_tag ) {
    window.open( '/about_retailer.cgi?retailer=' + retailer_tag,
                 'about_retailer',
                 'width=500,height=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes' );
}

function submit_comment( f ) {
    // value check

    var subject = document.getElementById('COMMENT_SUBJECT').value;
    var comment = document.getElementById('COMMENT_COMMENT').value;
    var email   = document.getElementById('COMMENT_EMAIL').value;

    if ( subject && comment ) {
        return true;
    } else {
        alert('Please make sure that none of the fields are empty.');
        return false;
    }
}

function comment_window() {
    window.open( '/comment.cgi',
                 'comment',
                 'width=425,height=460,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes' );
}

// various validation checks
function valid_phone( phone ) {
    phone = phone.replace(/[\s\+\-\.\,]/g, '');
    if ( phone.match( /^[0-9]+$/ ) ) {
        return 1;
    } else {
        return 0;
    }
}

function valid_email( email ) {
    if ( email.match( /^([a-z0-9\.\-_]+)@([a-z0-9\.\-_]+)\.([a-z]+)$/i ) ) {
        return 1;
    } else {
        return 0;
    }
}

function valid_postcode( postcode ) {
    postcode = postcode.replace(/[\s\+\-\.\,]/g, '');
    if ( postcode.match( /^[A-Z]+[A-Z0-9]+[A-Z]+$/i ) ) {
        return 1;
    } else {
        return 0;
    }
}

// lucky_*
function lucky_review( make, model, site ) {
    var url = '';
    if ( site.match(/autocar/i) ) {
    url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model + '+roadtests+site:' + site;
    } else {
    url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model + '+review+site:' + site; 
    }
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_news( make, model, site ) {
    var url = '';
    if ( site.match(/google/i) ) {
        url = 'http://news.google.co.uk/news?hl=en&as_nloc=UK&q=' + make + '+' + model;
    } else {
        url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model + '+news+site:' + site;
    }
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_photo( make, model, site ) {
    var url = '';
    if ( site.match(/yahoo/i) ) {
        url = 'http://uk.search.cars.yahoo.com/bin/search//photos_gallery_uk/cars/?p=cat:photos_cars+' + make + '+' + model + '&b=1';
    } else if ( site.match(/flickr/i) ) {
        url = 'http://www.flickr.com/search/show/?q=' + make + '+' + model;
    } else {
        url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model + '+gallery+site:' + site;
    }
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_video( make, model, site ) {
    var url = '';
    if ( site.match(/google/i) ) {
        url = 'http://video.google.co.uk/videosearch?q=' + make + '+' + model;
    } else if ( site.match(/whatcar/i) ) {
        url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=intitle:video review what car ' + make + '+' + model + '+site:' + site;
    } else {
        url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model + '+video+site:' + site;
    }
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_make( make, model ) {
    var url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model;
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_dealers() {
    var url = 'http://www.askaprice.com/car-dealers/';
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_jd() {
    var url = 'http://www.whatcar.co.uk/news-special-report.aspx?NA=225557';
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_boards( make, model ) {
    var url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+' + model + '+forum';
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_test( make ) {
    var url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+book+test+drive';
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function lucky_brochure( make ) {
    var url = 'http://www.google.co.uk/search?hl=en&btnG=Search&q=' + make + '+brochure+request';
    window.open( url, 'lucky', 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

// search row drop-down population
function populate( mf, mo ) {
    var manuf = document.getElementById('manufacturer');

    manuf.options.length = 0;
    //                             0123456789012345678901234
//    manuf.options[0] = new Option('- select a manufacturer -', '');
    var is_default = true;
    for (var index = 0; index < Manufacturers.length; index++) {
        var is_selected = ( mf && mf == Manufacturers[index] ) || is_default;
        manuf.options[index] = new Option(Manufacturers[index], Manufacturers[index], is_default, is_selected);
        is_default = false;
    }

    populate_models( mo );

}
function populate_models( mo ) {
    var manuf = document.getElementById('manufacturer');
    var model = document.getElementById('model');
    model.options.length = 0;
    //                             0123456789012345678901234
//    model.options[0] = new Option('---- select a model -----', '');

    var manufacturer = manuf.options[ manuf.selectedIndex ].value;

    var is_default = true;
    for (var index = 0; index < ModelsLookup[manufacturer].length; index++) {
        is_selected = ( mo && mo == ModelsLookup[manufacturer][index]);
        model.options[index] = new Option(ModelsLookup[manufacturer][index], ModelsLookup[manufacturer][index], is_default, is_selected);
        is_default = false;
    }
}

// Codweavers form invocation
function submit_insurance() {
    document.CODEWEAVERS_FORM.submit();
}

// Alerts

function check_alert_fields( email ) {
    var response = '';

    var blank_email = ( email == '' ) || ( email == 'email address' );

    var v_email     = valid_email( email );

    if ( blank_email || ! v_email ) {
        response = 'Please use a valid email address.  It should look something like name\@host.com';
    } 

    return response;
}

function submit_alert( f, cap_id ) {
    var email    = document.getElementById('ALERT_EMAIL').value;

    var response = check_alert_fields( email );

    if ( response ) {
        alert( response );
        return false;
    }

    var url = '/alert.cgi?type=add&email=' + email + '&cap_id=' + cap_id + '&cancel_url=close';

    window.open( url, 'alert', 'width=450,height=350,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes' );

    return false;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function submit_postcode( f, make ) {
    var postcode = document.getElementById('DPC').value;

    if ( ! postcode ) {
        return true;
    }

    var blank_postcode = ( postcode == '' ) || ( postcode == 'post code' );
    var v_postcode = valid_postcode( postcode );

    var response = '';

    if ( blank_postcode || ! v_postcode ) {
        response = 'Please use a valid post code.  It should look something like AB12 3CD';
    };

    if ( response ) {
        alert( response );
        return false;
    }

    load_from_url( 'DEALERSHIP_LIST', '/checker/dealerships.cgi?make=' + make + '&postcode=' + postcode );

    return true;
}

function onload_dealership( make ) {
    var postcode = readCookie( 'POSTCODE' );
    if ( postcode ) {
        var element = document.getElementById('DPC');
        if ( element ) {
            element.value = postcode;

            submit_postcode( null, make );
        }
    }
}

function onload_local() {
    var postcode = readCookie( 'POSTCODE' );
    if ( postcode ) {
        update_dpc_and_postcode_fields( postcode );
    }
}

function pasignup() {
    window.open( '/signup.cgi',
                 'pasignup',
                 'width=500,height=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes' );
}

function used_image_mouseover( image ) {
    var graphic = document.getElementById('USED_IMAGE');

    if ( graphic ) {
        graphic.src = image;
    }
}
