var xmlhttp

function loadXMLDoc(url, targetId) {
    xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest()
    } else if (window.ActiveXObject) {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    if(xmlhttp) {
      xmlhttp.onreadystatechange = getStateChangeHandler(targetId)
      xmlhttp.open("GET", url, true)
      xmlhttp.send(null)
    }
}

function getStateChangeHandler(targetId) {
    return function() {
        // if xmlhttp shows "loaded"
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                document.getElementById(targetId).innerHTML = xmlhttp.responseText
            } else {
                alert("Problem retrieving data:" + xmlhttp.statusText)
            }
        }
    }
}

function showCart() {
  loadXMLDoc("/store/cart", "the_cart");
}

// Make a portion of the page flicker to emphasize that it has changed.
var flicker_yellow = new Array('#eeeeee',
    '#eeeeee','#efefea','#f0f0e6','#f1f1e2',
    '#f2f2de','#f3f3da','#f4f4d6','#f5f5d2',
    '#f6f6ce','#f7f7ca','#f8f8c6','#f9f9c2',
    '#fafabe','#fbfbba','#fcfcb6','#fdfdb2',
    '#fefeae','#ffffaa','#ffffaa');
var flicker_red = new Array('#eeeeee',
    '#eeeeee','#efeaea','#f0e6e6','#f1e2e2',
    '#f2dede','#f3dada','#f4d6d6','#f5d2d2',
    '#f6cece','#f7caca','#f8c6c6','#f9c2c2',
    '#fabebe','#fbbaba','#fcb6b6','#fdb2b2',
    '#feaeae','#ffaaaa','#ffaaaa', '#ffaaaa');
var flicker_colors;
var flicker_end_color;
var flicker_up;
var flicker_loops;
var flicker_id;

function flicker(id, color_choice, end_color) {
    flicker_id = id;
    flicker_end_color = end_color;
    flicker_colors = flicker_yellow;
    if(color_choice == 'red') flicker_colors = flicker_red;
    flicker_loops = 2;
    flicker_up = true;
    flicker_loop(0);
}

function flicker_loop(i) {
    var delay = 40;
    if(flicker_id == '' ) return;
    var flicker_obj = document.getElementById(flicker_id);
    flicker_obj.style.backgroundColor = flicker_colors[i];
    if (flicker_up) {
        if (++i < flicker_colors.length) {
            setTimeout("flicker_loop(" + i + ")", delay);
        }
        else {
            flicker_up = false;
            flicker_loop(--i);
        }
    }
    else {
        if (--i > 0) {
            setTimeout("flicker_loop(" + i + ")", delay);
        }
        else {
            if (--flicker_loops > 0) {
                flicker_up = true;
                flicker_loop(0);
            }
            else {
                flicker_obj.style.backgroundColor = flicker_end_color;
            }
        }
    }
}
