

function get_models(make_code) {

clear_models();

if (make_code) {
    var now     = new Date();
    
    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.get_models&make_code='+escape(make_code)+'&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_models_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_GET_MODELS);
    }
/*
else {
    get_products();
    }
*/
}


function clear_models() {

//alert('clear_models');
clear_years();
var form_obj = document.getElementById('product_search');

while (form_obj.model.options.length>1) {
    form_obj.model.options[1] = null;
    }
form_obj.model.disabled = true;
}


function clear_years() {

//hide search
hide_search();

var form_obj = document.getElementById('product_search');

while (form_obj.year.options.length>1) {
    form_obj.year.options[1] = null;
    }
form_obj.year.disabled = true;

}


function parse_models_xml_data(xml_doc) {

/*
<?xml version="1.0" encoding="UTF-8"?>

<models make_code="MAKE_CODE">
    <model><![CDATA[MODEL_TRIM]]></model>
    <model><![CDATA[MODEL_TRIM]]></model>
    ...
</models>
*/
var document_root = xml_doc.documentElement;
var current_node, make_code;
var html_obj, option_obj;

//populate form
var form_obj = document.getElementById('product_search');
make_code    = document_root.getAttribute("make_code");
for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];
    
    if (current_node.nodeName == 'model') {
        form_obj.model.options[form_obj.model.options.length] = new Option(current_node.firstChild.nodeValue, make_code+"|"+current_node.firstChild.nodeValue);
        }
    }

//enabled form field
if (document_root.childNodes.length) {
    form_obj.model.disabled = false;
    }

//get_products();
}


function get_model_years(key_code) {

clear_years();

if (key_code) {
    var now     = new Date();
    
    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.get_model_years&code='+escape(key_code)+'&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_model_years_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_GET_MODEL_YEARS);
    }
/*
else {
    get_products();
    }
*/
}


function clear_years() {

var form_obj = document.getElementById('product_search');

while (form_obj.year.options.length>1) {
    form_obj.year.options[1] = null;
    }
form_obj.year.disabled = true;

//list_products('');
}


function parse_model_years_xml_data(xml_doc) {

/*
<?xml version="1.0" encoding="UTF-8"?>

<years model_code="MODEL_CODE">
    <year min="YEAR_MIN" max="YEAR_MAX" />
    <year min="YEAR_MIN" max="YEAR_MAX" />
    ...
</years>
*/
var document_root = xml_doc.documentElement;
var current_node, model_code, option_text, year_min, year_max;
var html_obj, option_obj;

//populate form
var form_obj = document.getElementById('product_search');
model_code   = document_root.getAttribute("model_code");
for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];
    
    if (current_node.nodeName == 'year') {
        year_min = current_node.getAttribute("min");
        year_max = current_node.getAttribute("max");
        if (year_min && year_max) {
            option_text = (year_min!=year_max) ? year_min+"-"+year_max:year_min;
            }
        else if (year_min && !year_max) {
            option_text = year_min+"+";
            }
        else {
            option_text = "< "+year_max;
            }
        
        form_obj.year.options[form_obj.year.options.length] = new Option(option_text, year_min+";"+year_max);
        }
    }

//enabled form field
if (document_root.childNodes.length) {
    form_obj.year.disabled = false;
    }

//get_products();
}


function check_products(value) {

if (value) {
    get_products();
    }
else {
    hide_search();
    }
} 


function run_product_search() {

form_obj = document.getElementById('product_search');
if ((form_obj.make.selectedIndex && form_obj.model.selectedIndex && form_obj.year.selectedIndex) || form_obj.q.value.split(' ').join('')!="") {
    get_products();
    }
}
                
function get_products() {

//empty rows
var html_obj = document.getElementById(get_product_list_table());
while (html_obj.rows.length>1) {
    html_obj.deleteRow(html_obj.rows.length-1);
    }

var make, model, year, q;
var form_obj = document.getElementById('product_search');

make   = form_obj.make.options[form_obj.make.selectedIndex].value;
model  = form_obj.model.options[form_obj.model.selectedIndex].value;
year   = form_obj.year.options[form_obj.year.selectedIndex].value;
q      = form_obj.q.value;

if ((make && model && year) || q.split(' ').join('').length>=3) {
    search_loading_please_wait();

    var now     = new Date();

    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.get_products';
    url_obj.url+= '&make='+escape(make);
    url_obj.url+= '&model='+escape(model);
    url_obj.url+= '&year='+escape(year);
    url_obj.url+= '&q='+escape(q);
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_products_list_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_GET_PRODUCTS_LIST);
    }
}


function parse_products_list_xml_data(xml_doc) {

var document_root = xml_doc.documentElement;
var current_node, total, current_page;
var product_node, product_code, product_model, product_sku, year_range, product_desc, product_price, product_count, retail_price, in_stock, in_cart, col;
var product_groups, group_name, group_id;
var html_obj, row, cell, output, output_top, output_bottom, row_color, style_right, is_capa;

product_groups = new Array();
group_name     = '';
product_count  = 0;
html_obj       = document.getElementById(get_product_list_table());
for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];

    //PRODUCT GROUP -- ICONS    
    if (current_node.nodeName == 'product_groups') {
        for (var p=0; p<current_node.childNodes.length; p++) {
            if (current_node.childNodes[p].nodeName=='product_group') {
                product_groups.push(current_node.childNodes[p].getAttribute('group_id'));
                }
            }
        }
    else if (current_node.nodeName == 'products') {
        group_id      = current_node.getAttribute('group_id');
        for (var p=0; p<current_node.childNodes.length; p++) {
            product_node = current_node.childNodes[p];
            if (product_node.nodeName == 'group') {
                group_name = product_node.firstChild.nodeValue;
                //insert product group in HTML
                row  = html_obj.insertRow(-1);
                row.className = 'product_group';
                cell = row.insertCell(0);
                cell.colSpan   = is_logged ? 8:6; 
                cell.innerHTML = '<a name="group_'+group_id+'"></a><div class="group">'+eval(group_name)+'</div>';
                }
            else if (product_node.nodeName == 'product') {
                product_count++;
                for (var x=0; x<product_node.childNodes.length; x++) {
                    switch (product_node.childNodes[x].nodeName) {
                        case 'model_code':
                            product_code  = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'name':
                            product_model = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'sku':
                            product_sku   = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'year_range':
                            year_range    = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'desc':
                            product_desc  = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'price':
                            product_price = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'price_1':
                            retail_price  = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'extra':
                            in_stock      = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        case 'in_cart':
                            in_cart       = product_node.childNodes[x].firstChild.nodeValue;
                            break;
						case 'pics':
                            pics          = product_node.childNodes[x].firstChild.nodeValue;
                            break;
                        }
                    }
                //insert product in HTML
                row  = html_obj.insertRow(-1);
                row_color = (product_count%2==0) ? 'clear':'dark';
                row_color = (in_cart==1) ? 'highlight':row_color;
                row.className = row_color;
                col = 0;
                //index
                //cell = row.insertCell(col++);
                //cell.innerHTML = product_count;
                //capa
                is_capa = /-[a-zA-Z0-9]+C[a-zA-Z0-9]*-/;
                cell = row.insertCell(col++);
                cell.style.textAlign = 'center';
                //cell.innerHTML = is_capa.test(product_sku) ? '<img src="images/accept.png" width="16" height="16" />':'&nbsp;';
                cell.innerHTML = is_capa.test(product_sku) ? 'CAPA':'&nbsp;';
                //sku
                cell = row.insertCell(col++);
                cell.innerHTML = highlight_search(product_sku);
                cell.innerHTML+= (pics) ? get_pictures_link(product_sku, pics):'';
                row.id = 'product_row_'+product_sku;
                //desc
                cell = row.insertCell(col++);
                cell.innerHTML = highlight_search(product_desc);
                //prices
                if (is_logged) {
                    //retail price
                    cell = row.insertCell(col++);
                    cell.style.textAlign = 'right';
                    cell.style.paddingRight = '8px';
                    cell.innerHTML = retail_price+'$';
                    //your price
                    cell = row.insertCell(col++);
                    cell.style.textAlign = 'right';
                    cell.style.paddingRight = '8px';
                    cell.innerHTML = product_price+'$';
                    }
                //in stock
                cell = row.insertCell(col++);
                cell.style.textAlign = 'center';
                //cell.innerHTML = (in_stock=='*' && parseInt(retail_price)>0) ? '<img src="images/accept.png" width="16" height="16" />':'&nbsp;';
                
                imageStr = "&nbsp;"
                switch(in_stock) {
	            case "?":
				  imageStr = '&nbsp;';
				  break;
				case "*":
				  imageStr = '<img src="images/accept.png" width="16" height="16" />';
				  break;
				case "@":
				  imageStr = '<img src="images/dispo24H.png" width="43" height="28" />';
				  break;
				}
				
				cell.innerHTML = imageStr;
                
                //ajouter
                if (in_stock=='*' && parseInt(retail_price)>0) {
                    output = '<div class="product_cell">' + "\n";
                    output+= '  <div class="product_qty_link">' + "\n";
                    output+= '    '+CART_PRODUCT_QTY+'<input class="product_qty" type="text" id="qty_'+product_sku+'" name="qty_'+product_sku+'" value="1" maxlength="4" />' + "\n";
                    output+= '    <a class="add_item" href="javascript:select_product(\''+product_sku+'\')" title="'+CART_ADD_PRODUCT+'"><img src="images/add_to_cart.png" width="22" height="14" alt="'+CART_ADD_PRODUCT+'" border="0" /></a>' + "\n";
                    output+= '  </div>' + "\n";
                    output+= '  <div id="multiple_carts_'+product_sku+'" class="multiple_carts"></div>' + "\n";
                    output+= '</div>';
                    }
                else {
                    output = phone_number;
                    }
                
                cell = row.insertCell(col++);
                cell.innerHTML = output;
                }
            }
		//reinitialize
		initLightbox();
        }
    }

//update product group links
for (var i=1; i<=nb_products_group; i++) {
    group_id = (i<10) ? '0'+i:i;
    html_obj = document.getElementById('bodypart_'+group_id);
    html_obj.className = 'bodypart disabled';
    
    html_obj = document.getElementById('bodypart_'+group_id+'_link');
    html_obj.href = 'javascript:void(0)';
    }

if (product_count) {
    found_products(true);
    
    //update link styles
    for (var i=0; i<product_groups.length; i++) {
        if (product_groups[i]!='0') {
            group_id = (product_groups[i].length==1) ? '0'+product_groups[i]:product_groups[i];
            html_obj = document.getElementById('bodypart_'+group_id);
            if (html_obj) {
                html_obj.className = 'bodypart';
                
                html_obj = document.getElementById('bodypart_'+group_id+'_link');
                html_obj.href = '#group_'+(parseInt(group_id));
                }
            else {
                alert('bodypart_'+group_id);
                }
            }
        }
    
    html_obj = document.getElementById('bodyparts');
    html_obj.style.display = 'block';
    }
else {
    found_products(false);
    
    html_obj = document.getElementById('bodyparts');
    html_obj.style.display = 'none';
    }
html_obj = document.getElementById(get_product_list_table());
html_obj.style.display = (product_count>0) ? (navigator.appName.indexOf("Microsoft")!=-1 ? 'block':'table'):'none';
}


function get_pictures_link(sku, str) {

var popup_link = '';
var pics = str.split(',');
var pic_folder = PRODUCT_IMAGE_FOLDER+sku+'/';
var output = '';
for (var i=0; i<pics.length; i++) {
	var pic_link = '<a href="'+pic_folder+pics[i]+'" rel="lightbox['+sku+']" title="Photo #'+(i+1)+'"><img src="images/loupe_sm.gif" width="20" height="14" border="0" /></a>';
	if (i==0) {
		output += ' '+pic_link+"\n";
		}
	else {
		output += '<div style="display:none">'+pic_link+"</div>\n";
		}
	}

return output;
}


function found_products(b) {

var html_obj;
//show navigation
html_obj = document.getElementById(get_product_list_table());
html_obj.style.display = b ? 'block':'none';
//hide error
html_obj = document.getElementById('no_products_found');
html_obj.style.display = b ? 'none':'block';
//display search message
html_obj = document.getElementById('search_loading');
html_obj.style.display = 'none';
}


function hide_search() {

var html_obj;
//show navigation
html_obj = document.getElementById(get_product_list_table());
html_obj.style.display = 'none';
//hide error
html_obj = document.getElementById('no_products_found');
html_obj.style.display = 'none';
//display search message
html_obj = document.getElementById('search_loading');
html_obj.style.display = 'none';
}


function select_product(code) {

var html_obj = document.getElementById('qty_'+code);

if (!isNaN(parseInt(html_obj.value))) {
    if (nb_shopping_carts==1) {
        add_product(code);
        }
    else {
        show_cart_selection(code);
        }
    }
else {
    alert(SPECIFY_A_NUMERIC_VALUE);
    html_obj.focus();
    }
}


function show_cart_selection(code) {
    
var html_obj        = document.getElementById('cart_selection_template');
var output          = html_obj.innerHTML;
var cart_names_divs = document.getElementById('shopping_carts').getElementsByTagName('div');
var link_template   = "<div class=\"select_cart\"><a href=\"javascript:add_product('PRODUCT_CODE',CART_ID)\">CART_NAME</a></div>\n";
var cart_links      = "";
var cart_link;
var cart_name;
var c_index = 0;

for (var c=0; c<SHOPPING_CARTS.length; c++) {
    if (typeof(SHOPPING_CARTS[c].name)!='undefined'){
        cart_name   = SHOPPING_CARTS[c].name;
    
        cart_link   = link_template;
        cart_link   = cart_link.split('PRODUCT_CODE').join(code);
        cart_link   = cart_link.split('CART_ID').join(c_index);
        cart_link   = cart_link.split('CART_NAME').join(cart_name);
        cart_links += cart_link;
        
        c_index++;
        }
    }
output = output.split('<!-- AVAILABLE CARTS LIST -->').join(cart_links);

//hide old popup and display new cart selection
var product_qty_divs = document.getElementById(get_product_list_table()).getElementsByTagName('input');
var product_code;
var style_top;
for (var i in product_qty_divs) {
    if (product_qty_divs[i].className=='product_qty') {
        product_code = product_qty_divs[i].id.split('qty_').join('');
        html_obj     = document.getElementById('multiple_carts_'+product_code);
        if (product_code == code) {
            style_top = 52 + 14*nb_shopping_carts;
            html_obj.innerHTML        = '<div class="cart_selection">'+output+'</div>';
            html_obj.style.top  = '8px';
            html_obj.style.left = '-145px';
            html_obj.style.display    = 'block';
            }
        else {
            html_obj.innerHTML = '';
            html_obj.style.display = 'none';
            }
        }
    }
}


function hide_cart_selection () {

//hide old popup and display new cart selection
var product_qty_divs = document.getElementById(get_product_list_table()).getElementsByTagName('input');
var product_code;
var style_top;
for (var i in product_qty_divs) {
    if (product_qty_divs[i].className=='product_qty') {
        product_code = product_qty_divs[i].id.split('qty_').join('');
        html_obj     = document.getElementById('multiple_carts_'+product_code);
        html_obj.innerHTML = '';
        html_obj.style.display = 'none';
        }
    }
}


function add_product(code, cart_index) {

var html_obj, qty;

//remove cart selection popup
if (cart_index!=undefined) {
    html_obj = document.getElementById('multiple_carts_'+code);
    html_obj.innerHTML = '';
    html_obj.style.display = 'none';
    }

//highlight selected product
html_obj = document.getElementById('product_row_'+code);
html_obj.className = 'highlight';

//get product code quantity
html_obj = document.getElementById('qty_'+code);
qty      = parseInt(html_obj.value);

var now     = new Date();

var url_obj = new Object();
url_obj.url = get_xml_data_url+'?s=xml.add_product';
url_obj.url+= '&sku='+escape(code);
url_obj.url+= '&qty='+escape(qty);
url_obj.url+= (cart_index!=undefined) ? '&cart_index='+escape(cart_index):'';
url_obj.url+= '&void='+now.getTime();

var cb_obj  = new Object();
cb_obj.func = 'parse_shopping_carts_xml_data';

get_xml_then(url_obj, cb_obj, ERROR_AJAX_GET_PRODUCTS_LIST);

//remove hide_cart_selection for document.onclick
document.onclick = null;
}


function set_product_qty(code, cart_index, qty) {

var html_obj, qty;

var confirm_qty = (qty==0);

if (confirm_qty) {
    if (confirm(CONFIRM_DELETE)) {
        confirm_qty = false;
        }
    }

if (!confirm_qty) {
    var now     = new Date();
    
    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.set_product_qty';
    url_obj.url+= '&sku='+escape(code);
    url_obj.url+= '&qty='+escape(qty);
    url_obj.url+= '&cart_index='+cart_index;
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_cart_content_qty_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_GET_PRODUCTS_LIST);
    }
}


function modify_product_qty(code, cart_index) {

var html_obj, qty, confirm_qty;

html_obj = document.getElementById('modify_qty_'+code);

if (!isNaN(parseInt(html_obj.value))) {
    qty = parseInt(html_obj.value);
    set_product_qty(code, cart_index, qty);
    }
else {
    alert(SPECIFY_A_NUMERIC_VALUE);
    html_obj.focus();
    }
}


function search_loading_please_wait() {

var html_obj;

//hide navigation
found_products(false);
//display search message
html_obj = document.getElementById('no_products_found');
html_obj.style.display = 'none';
html_obj = document.getElementById('search_loading');
html_obj.style.display = 'block';
}


function highlight_search (str) {

var form_obj = document.getElementById('product_search');
if (form_obj.q.value!='') {
    str = str.toUpperCase();
    var search_words = form_obj.q.value.split(' ');
    var search_word;
    for (var w=0; w<search_words.length; w++) {
        search_word = search_words[w].toUpperCase();
        if (search_word.length>=3) {
            str = str.split(search_word).join('<span class="search">'+search_word+'</span>');
            }
        }
    }
return str;
}


function parse_catalog_xml_data(xml_doc) {

var document_root = xml_doc.documentElement;
var current_node, html_obj;

for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];
    if (current_node.nodeName=='catalog') {
        html_obj = document.getElementById('catalog');
        html_obj.innerHTML = current_node.firstChild.nodeValue;
        }
    }
}


function show_add_cart_form() {

var html_obj;
html_obj = document.getElementById('bg_trans');
html_obj.onclick = hide_add_cart_form;
showBackground();

html_obj = document.getElementById('add_cart_block');
html_obj.style.display = 'block';

html_obj = document.getElementById('add_cart_form');
html_obj.cart_name.value = '';
html_obj.cart_name.focus();

document.OnKeyUp_ESC = hide_add_cart_form;
document.onkeyup     = check_keycode;

showScrollOffsetMargin('add_cart_block');
//scrollToTop();
}


function hide_add_cart_form() {

var html_obj = document.getElementById('add_cart_block');
hideScrollOffsetMargin('add_cart_block');
html_obj.style.display = 'none';

hideBackground();

document.onkeyup = null;
}


function add_cart() {

var form_obj = document.getElementById('add_cart_form');

if (form_obj.cart_name.value.split(' ').join('')!='') {
    var now     = new Date();

    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.add_cart';
    url_obj.url+= '&name='+escape(form_obj.cart_name.value);
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_shopping_carts_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
    
    hide_add_cart_form();
    }
else {
    alert(CART_NAME_MISSING);
    form_obj.cart_name.focus();
    }
}


function rename_cart(cart_name, cart_index) {

var html_obj;
html_obj = document.getElementById('bg_trans');
html_obj.onclick = hide_edit_cart_form;
showBackground();

html_obj = document.getElementById('edit_cart_block');
html_obj.style.display = 'block';

html_obj = document.getElementById('edit_cart_form');
html_obj.cart_index.value = cart_index;
html_obj.cart_name.value  = cart_name;
html_obj.cart_name.focus();

document.OnKeyUp_ESC = hide_edit_cart_form;
document.onkeyup     = check_keycode;

showScrollOffsetMargin('edit_cart_block');
//scrollToTop();
}


function hide_edit_cart_form() {

var html_obj = document.getElementById('edit_cart_block');
hideScrollOffsetMargin('edit_cart_block');
html_obj.style.display = 'none';

hideBackground();

document.onkeyup = null;
}


function edit_cart() {

var form_obj = document.getElementById('edit_cart_form');

if (form_obj.cart_name.value.split(' ').join('')!='') {
    var now     = new Date();

    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.edit_cart';
    url_obj.url+= '&cart_index='+escape(form_obj.cart_index.value);
    url_obj.url+= '&name='+escape(form_obj.cart_name.value);
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_shopping_carts_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
    
    hide_edit_cart_form();
    }
else {
    alert(CART_NAME_MISSING);
    form_obj.cart_name.focus();
    }
}


function delete_cart(cart_name, index) {

if (confirm(CONFIRM_DELETE_CART.split('CART_NAME').join(cart_name))) {
    delete_cart_status = true;

    var now     = new Date();

    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.delete_cart';
    url_obj.url+= '&cart_index='+index;
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_shopping_carts_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
    }
}


function parse_shopping_carts_xml_data(xml_doc) {

var document_root = xml_doc.documentElement;
var current_node, nb_items, nb_current_items, class_name, cart_name, cart_change;
var html_obj, c_index, output;

//refresh shopping cart names
SHOPPING_CARTS = new Array();

//nb_shopping_carts
cart_change        = false;
nb_current_items   = 0;
nb_shopping_carts  = parseInt(document_root.getAttribute("nb_carts"));
html_obj           = document.getElementById('shopping_carts');
html_obj.innerHTML = '';

for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];
    if (current_node.nodeName=='shopping_cart') {
        nb_items   = current_node.getAttribute("nb_items");
        c_index    = current_node.getAttribute("cart_index");
        class_name = current_node.getAttribute("selected") ? 'selected':'disabled';
        cart_name  = current_node.firstChild.nodeValue;
        SHOPPING_CARTS.push({ name:cart_name, nb_items:parseInt(nb_items) });
        
        output = "";
        output+= '<div id="cart_CART_INDEX" class="shopping_cart">' + "\n";
        output+= '  <div class="nb_items">NB_ITEMS</div>' + "\n";
        output+= '  <div class="name CLASS_NAME"><a href="javascript:view_cart_content(CART_INDEX)" title="'+VIEW_CART_CONTENT+'">CART_NAME</a></div>' + "\n";
        output+= '  <div class="edit"><a href="javascript:rename_cart(\'CART_NAME\', CART_INDEX)" title="'+EDIT_CART+'"><img src="images/edit_rename.gif" width="15" height="14" border="0" alt="'+EDIT_CART+'" /></a></div>' + "\n";
        if (nb_shopping_carts>1)
            output+= '  <div class="delete"><a href="javascript:delete_cart(\'CART_NAME\', CART_INDEX)" title="'+DELETE_CART+'"><img src="images/delete.png" width="16" height="16" border="0" alt="'+DELETE_CART+'" /></a></div>' + "\n";
        output+= '</div>' + "\n";
        output = output.split('CART_INDEX').join(c_index);
        output = output.split('NB_ITEMS').join(nb_items);
        output = output.split('CLASS_NAME').join(class_name);
        output = output.split('CART_NAME').join(cart_name);
        
        html_obj.innerHTML += output;
        
        if (class_name=='selected') {
            nb_current_items   = nb_items;
            cart_change        = current_cart_index!=c_index;
            current_cart_index = c_index;
            }
        }
    }

var btn_view_cart_content_w = (LANG=='fr') ? 144:125;
var btn_view_cart_content_h = (LANG=='fr') ? 23:24;
output  = '<span style="padding-left:4px;">'+SHOPPING_CARTS[current_cart_index].name.toUpperCase()+' '+CART_CONTAINS_NB_ITEMS+'</span><br />';
output += '<a href="javascript:view_cart_content('+current_cart_index+')"><img src="images/'+LANG+'/btn_view_cart_content.gif" width="'+btn_view_cart_content_w+'" height="'+btn_view_cart_content_h+'" border="0" /></a>';

output  = output.split('NB').join(nb_current_items) + ' ';

html_obj = document.getElementById('see_cart_content');
html_obj.innerHTML = output;
html_obj.style.display = (nb_current_items>0) ? 'block':'none';

//refresh selected rows view if there are many carts
if (cart_change || delete_cart_status) {
    delete_cart_status = false;
    get_products();
    }
}


function view_cart_content(cart_index) {

if (SHOPPING_CARTS[cart_index].nb_items) {
    current_cart_index = cart_index;
    var html_obj = document.getElementById('bg_trans');
    html_obj.onclick = hide_cart_content;
    showBackground();
    
    //assign when we show popup after AJAX call
    //document.OnKeyUp_ESC = hide_cart_content;
    //document.onkeyup     = check_keycode;
    
    document.getElementById("carre1").style.display = "none";
    //document.getElementById("carre2").style.display = "none";
    //document.getElementById("carre3").style.display = "none";
    
    var now     = new Date();
    
    //VIEW CART CONTENT
    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.get_cart_items';
    url_obj.url+= '&cart_index='+cart_index;
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_cart_content_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
    
    //CHANGE ACTIVE CART
    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.shopping_carts.list';
    url_obj.url+= '&cart_index='+cart_index;
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_shopping_carts_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
    
    //scrollToTop();
    }
else {
    alert(SHOPPING_CART_IS_EMPTY);
    }
}


function hide_cart_content() {

var html_obj = document.getElementById('cart_content_group');
hideScrollOffsetMargin('cart_content_group');
html_obj.style.display = 'none';

hideBackground();
document.OnKeyUp_ESC = null;
document.onkeyup = null;

document.getElementById("carre1").style.display = "block";
//document.getElementById("carre2").style.display = "block";
//document.getElementById("carre3").style.display = "block";

//redisplay search to highlight/unhighlight selected products
var form_obj = document.getElementById('product_search');
var make     = form_obj.make.options[form_obj.make.selectedIndex].value;
var model    = form_obj.model.options[form_obj.model.selectedIndex].value;
var year     = form_obj.year.options[form_obj.year.selectedIndex].value;
var query    = form_obj.q.value;
if ((make && model && year) || query.split(' ').join('')!='') {
    get_products();
    }
}


function parse_cart_content_qty_xml_data(xml_doc) {

var document_root = xml_doc.documentElement;
var cart_index    = parseInt(document_root.getAttribute('c_index'));
var nb_items      = parseInt(document_root.getAttribute('nb_items'));

SHOPPING_CARTS[cart_index].nb_items = nb_items;
if (nb_items==0) {
    hide_cart_content();
    
    //CHANGE ACTIVE CART
    var now     = new Date();
    var url_obj = new Object();
    url_obj.url = get_xml_data_url+'?s=xml.shopping_carts.list';
    url_obj.url+= '&cart_index='+cart_index;
    url_obj.url+= '&void='+now.getTime();
    
    var cb_obj  = new Object();
    cb_obj.func = 'parse_shopping_carts_xml_data';
    
    get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
    }
else {
    view_cart_content(cart_index);
    }
}


function parse_cart_content_xml_data(xml_doc) {

var document_root, current_node;
var html_obj, row, cell, row_count;
var sku, qty, qty_html, cart_index;

//empty product list
html_obj = document.getElementById('cart_content');
while (html_obj.rows.length>1) {
    html_obj.deleteRow(1);
    }
html_obj = document.getElementById('cart_content_logged');
while (html_obj.rows.length>1) {
    html_obj.deleteRow(1);
    }
html_obj = document.getElementById(get_cart_content_table());

//append data
row_count     = 0;
document_root = xml_doc.documentElement;
cart_index    = document_root.getAttribute('c_index');
for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];
    if (current_node.nodeName=='product') {
        row = html_obj.insertRow(-1);
        row.className = (row_count%2==0) ? 'dark':'clear';
        row_count++;
        
        cell = row.insertCell(0);
        cell.style.textAlign = "left";
        cell.innerHTML = row_count;
        
        cell = row.insertCell(1);
        cell.style.textAlign = "left";
        sku            = current_node.getAttribute('sku');
        cell.innerHTML = sku;
        
        cell = row.insertCell(2);
        cell.style.textAlign = "left";
        cell.innerHTML = current_node.firstChild.nodeValue;
        
        cell     = row.insertCell(3);
        qty      = current_node.getAttribute('qty');
        qty_html = CART_PRODUCT_QTY+'<input class="product_qty" type="text" id="modify_qty_'+sku+'" name="modify_qty_'+sku+'" value="'+qty+'" maxlength="4" /> ';
        qty_html+= "<a href=\"javascript:modify_product_qty('"+sku+"', "+cart_index+")\"><img src=\"images/accept.png\" border=\"0\" /></a>";
        cell.innerHTML = qty_html;
        
        if (is_logged) {
            cell = row.insertCell(4);
            cell.className = 'price';
            cell.innerHTML = current_node.getAttribute('price')+'$';
            
            //delete column
            cell = row.insertCell(5);
            cell.innerHTML = "<a class=\"delete_product\" href=\"javascript:set_product_qty('"+sku+"', "+cart_index+", 0)\"><img src=\"images/delete.png\" border=\"0\" /></a>";
            }
        else {
            //delete column
            cell = row.insertCell(4);
            cell.innerHTML = "<a class=\"delete_product\" href=\"javascript:set_product_qty('"+sku+"', "+cart_index+", 0)\"><img src=\"images/delete.png\" border=\"0\" /></a>";
            }
        }
    }

//display items list
html_obj = document.getElementById('current_cart_name');
html_obj.innerHTML = SHOPPING_CARTS[current_cart_index].name;
html_obj = document.getElementById('cart_content_group');
showScrollOffsetMargin('cart_content_group');
//assigning hidepopup code
document.OnKeyUp_ESC = hide_cart_content;
document.onkeyup     = check_keycode;
html_obj.style.display = 'block';
html_obj = document.getElementById(get_cart_content_table());
html_obj.style.display = (navigator.appName.indexOf('Microsoft')!=-1) ? 'block':'table';
html_obj = document.getElementById('identification_group');
html_obj.style.display = is_logged ? 'none':'none';
html_obj = document.getElementById('checkout');
html_obj.style.display = is_logged ? '':'none';
}


function get_product_list_table() {

var table_name = is_logged ? 'product_list_logged':'product_list';
return table_name;
}


function get_cart_content_table() {

var table_name = is_logged ? 'cart_content_logged':'cart_content';
return table_name;
}


function confirm_checkout() {

if (is_logged && confirm(CONFIRM_CHECKOUT)) {
    checkout();
    }
}


function checkout() {

if (is_logged) {
    hide_cart_content();
    save_cart_content();
    }
/*
else {
    //verify identification form
    var form_obj = document.getElementById('idendification_form');
    
    
    if (form_obj.firstname.value.split(' ').join('')=='') {
        alert(ERROR_FORM_FIRSTNAME);
        form_obj.firstname.focus();
        }
    else if (form_obj.lastname.value.split(' ').join('')=='') {
        alert(ERROR_FORM_LASTNAME);
        form_obj.lastname.focus();
        }
    else if (form_obj.phone.value.split(' ').join('')=='') {
        alert(ERROR_FORM_PHONE);
        form_obj.phone.focus();
        }
    else if (!is_valid_email(form_obj.email.value)) {
        alert(ERROR_FORM_EMAIL);
        form_obj.email.focus();
        }
    else {
        hide_cart_content();
        save_cart_content();
        }
    }
*/
}


function get_text_search(str) {


set_field('product_search', 'q', str, false);
}


function save_cart_content() {

var html_obj = document.getElementById('bg_trans');
html_obj.onclick = null;
showBackground();

html_obj = document.getElementById('saving_cart_group');
showScrollOffsetMargin('saving_cart_group');
html_obj.style.display = 'block';

var form_obj = document.getElementById('idendification_form');
var form_obj_comments = document.getElementById('comments');

var now     = new Date();
    
var url_obj = new Object();
url_obj.url = get_xml_data_url+'?s=xml.save_cart';
url_obj.url+= '&cart_index='+current_cart_index;
url_obj.url+= '&comments='+escape(form_obj_comments.txtcomments.value);
if (member_id) {
    url_obj.url+= '&member_id='+member_id;
    }
else {
    url_obj.url+= '&member_id=0';
    url_obj.url+= '&name='+escape(form_obj.firstname.value+' '+form_obj.lastname.value);
    url_obj.url+= '&phone='+escape(form_obj.phone.value);
    url_obj.url+= '&email='+escape(form_obj.email.value);
    }
url_obj.url+= '&void='+now.getTime();

var cb_obj  = new Object();
cb_obj.func = 'parse_cart_checked_out_xml_data';

get_xml_then(url_obj, cb_obj, ERROR_AJAX_SAVE_CART);

//empty textbox
form_obj_comments.txtcomments.value="";
}


function parse_cart_checked_out_xml_data(xml_doc) {

var document_root, current_node;
var client_name, receipt_code;

//parse receipt
document_root = xml_doc.documentElement;
for (var n=0; n<document_root.childNodes.length; n++) {
    current_node = document_root.childNodes[n];
    if (current_node.nodeName=='name') {
        client_name = current_node.firstChild.nodeValue; 
        }
    else if (current_node.nodeName=='code') {
        receipt_code = current_node.firstChild.nodeValue; 
        }
    }

//hide popup & search
var html_obj;
var obj_ids = new Array('saving_cart_group', 'see_cart_content');
obj_ids.push(get_product_list_table());
for (var i in obj_ids) {
    html_obj = document.getElementById(obj_ids[i]);
    if (html_obj) {
    	html_obj.style.display = 'none';
		}
    }

hideBackground();
hideScrollOffsetMargin('saving_cart_group');

var output = THANK_YOU_MESSAGE;
output   = output.split('CLIENT_NAME').join(client_name);
output   = output.split('RECEIPT_CODE').join(receipt_code);
html_obj = document.getElementById('confirmation_msg');
html_obj.innerHTML = output;
html_obj.style.display = 'block';

//reload new catalog
var now     = new Date();
    
var url_obj = new Object();
url_obj.url = get_xml_data_url+'?s=xml.shopping_carts.list';
url_obj.url+= '&void='+now.getTime();

var cb_obj  = new Object();
cb_obj.func = 'parse_shopping_carts_xml_data';
    
get_xml_then(url_obj, cb_obj, ERROR_AJAX_ADD_CART);
}


function show_reload_group(nb_carts, search_date) {

var html_output = RELOAD_CART_QUESTION;
html_output     = html_output.split('NB_CARTS').join(nb_carts);
html_output     = html_output.split('SEARCH_DATE').join(search_date);

var html_obj;

html_obj = document.getElementById('reloading_cart_question');
html_obj.innerHTML = html_output;

html_obj = document.getElementById('reload_cart_group');
showScrollOffsetMargin('reload_cart_group');
html_obj.style.display = 'block';

html_obj = document.getElementById('reload_cart_plural');
if (html_obj)
    html_obj.style.display = (nb_carts>1) ? 'inline':'none';

html_obj = document.getElementById('bg_trans');
html_obj.onclick = hide_reload_group;
showBackground();

document.OnKeyUp_ESC = hide_reload_group;
document.onkeyup     = check_keycode;

document.getElementById("carre1").style.display = "none";
//document.getElementById("carre2").style.display = "none";
//document.getElementById("carre3").style.display = "none";
}


function hide_reload_group() {

var html_obj = document.getElementById('reload_cart_group');
hideScrollOffsetMargin('reload_cart_group');
html_obj.style.display = 'none';

hideBackground();

document.onkeyup = null;
}


function reload_carts() {

var cart_ids = new Array();
for (var i=0; i<last_cart.length-1; i++) {
    cart_ids.push(last_cart[i]);
    }

//reload new catalog
var now     = new Date();

var url_obj = new Object();
url_obj.url = get_xml_data_url+'?s=xml.reload_carts';
url_obj.url+= '&cart_id[]='+cart_ids.join('&cart_id[]=');
url_obj.url+= '&void='+now.getTime();

var cb_obj    = new Object();
cb_obj.custom = 'window.location.href = get_xml_data_url+"?s=catalogue"';

get_xml_then(url_obj, cb_obj, ERROR_AJAX_RELOAD_CARTS);
}


function print_cart() {

var html_obj = document.getElementById(get_cart_content_table());
var nb_cols  = is_logged ? 5:4;

var col_styles = new Array();
col_styles.push(' style="text-align:left;"');
col_styles.push(' style="text-align:left;"');
col_styles.push(' style="text-align:left;"');
col_styles.push(' style="text-align:center;"');
col_styles.push(' class="price" style="text-align:center;"');

var sku             = "";
var row_count       = 0;
var product_listing = "";
for (var j=0; j<html_obj.rows.length; j++) {
    if (j==0) {
        product_listing += "<tr>\n";
        for (var i=0; i<nb_cols; i++) {
            product_listing += "<th>"+html_obj.rows[j].cells[i].innerHTML+"</th>\n";
            }
        product_listing += "</tr>\n";
        }
    else {
        product_listing += '<tr class="'+ ((row_count%2==0) ? 'dark':'clear') +'"'+">\n";
        for (var i=0; i<nb_cols; i++) {
            product_listing += '<td'+col_styles[i]+'>';
            if (i<3 || i==4) {
                product_listing += html_obj.rows[j].cells[i].innerHTML;
                if (i==1) {
                    sku = html_obj.rows[j].cells[i].innerHTML;
                    }
                }
            else if (i==3) {
                product_listing += document.getElementById('modify_qty_'+sku).value;
                }
            product_listing += '</td>';
            }
        product_listing += "</tr>\n";
        row_count++;        
        }
    }

var txt_comment = document.getElementById("txtcomments").value;

var output = "";
output += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + "\n";
output += '<html xmlns="http://www.w3.org/1999/xhtml">' + "\n";
output += '<head>' + "\n";
output += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + "\n";
output += '<title>ATPAC AUTO  514.881.8888 ou 1.800.363.9339</title>' + "\n";
output += '<style type="text/css">' + "\n";
output += 'table.cart_content { width:100%; margin:10px auto; border:2px solid white; border-collapse:collapse; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:11px; }' + "\n";
output += '.cart_content th { border:2px solid white; background-color:#002C6B; border-collapse:collapse; text-align:left; padding:8px 5px 8px 5px; color:white; }' + "\n";
output += '.cart_content tr.dark  { background-color:#E5E5E5; }' + "\n";
output += '.cart_content tr.clear { background-color:white; }' + "\n";
output += '.cart_content td { border:2px solid white; border-collapse:collapse; padding:5px; white-space:nowrap; text-align:center; }' + "\n";
output += '.cart_content td.price { text-align:right; padding-right:3px 3px 8px 3px; }' + "\n";
output += '.comments td { font-family:verdana; font-size:8pt; }' + "\n";
output += '</style>' + "\n";
output += '<script type="text/javascript">' + "\n";
output += 'window.onload = function () { window.print(); }' + "\n";
output += '</script>' + "\n";
output += '</head>' + "\n";
output += '<body>' + "\n";
output += '<table class="cart_content">'+product_listing+'</table>' + "\n";
output += '<table class="comments"><tr><td><b>Commentaires : </b>'+txt_comment+'</td></tr></table>' + "\n";
output += '</body>' + "\n";
output += '</html>' + "\n";

var win_url   = navigator.appName.indexOf("Microsoft")!=-1 ? 'about:blank':'';
var print_win = window.open(win_url, "_blank", "top=25, left=25, width=800, height=400, menubar=0, toolbar=0, location=0, status=0, scrollbars=1");
print_win.document.open("text/html", "replace")
print_win.document.write(output);
print_win.document.close();
}


var set_interval_step  = 0;
var moves = new Array();
function scrollToContent(content_position) {

var pageHeight   = document.body.scrollHeight;
var scrollHeight = f_clientHeight();
var pos_final    = (pageHeight-scrollHeight>content_position) ? content_position:(pageHeight - scrollHeight);
var distance     = pos_final - f_scrollTop();

var move = (Math.abs(distance)>5) ? Math.floor(distance/3):distance;
window.scrollBy(0, move);
if (move == distance) {
	clearInterval(timer_scroll_to_content);
	}
if (set_interval_step>100) {
	window.scrollTo(0, content_position);
	clearInterval(timer_scroll_to_content);
	}
set_interval_step++;
}


function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function scrollToTop() {
/*
set_interval_step  = 0;
moves = new Array();
timer_scroll_to_content = setInterval('scrollToContent()', '40');
*/
}

function showScrollOffsetMargin(id) {

var html_obj = document.getElementById(id);
if (html_obj) {
    html_obj.style.marginTop = f_scrollTop()+'px';
    }
}

function hideScrollOffsetMargin(id) {

var html_obj = document.getElementById(id);
if (html_obj) {
    html_obj.style.marginTop = '0px';
    }
}