var hexa = new Array(16);
for (var i = 0; i < 10; i++) {
    hexa[i] = i;
}
hexa[10]="a"; hexa[11]="b"; hexa[12]="c";
hexa[13]="d"; hexa[14]="e"; hexa[15]="f";

var http = null;

if (typeof XMLHttpRequest != "undefined") {
    http = new XMLHttpRequest();
}

if (!http && typeof ActiveXObject != "undefined") {
    try {
        http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            try {
                http = new ActiveXObject("Msxml2.XMLHTTP.4.0");
            } catch (e3) {
                http = null;
            }
        }
    }
}

function addCart(form) {
    var post = '';
    for (i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type != 'radio' || form.elements[i].checked == true) {
            if (i > 0) { post += '&'; }
            post += form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
        }
    }
    post += '&XTCsid=<?php echo xtc_session_id(); ?>';
    showAddCart(post);
    return false;
}

function showAddCart(post) {
    if (http != null) {
        //http.abort();
        http.open("POST", 'ajaxAddCart.php', true);
        http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        //http.overrideMimeType("text/xml; charset=ISO-8859-15");
        http.onreadystatechange = function() {
            if (!http) {
                return;
            }
            if (http.readyState == 4) {
                var response = http.responseText;
                var amp = new RegExp("&amp;", "g");
                document.getElementById("<?php echo ADD_CART_BOX_ID; ?>").innerHTML = response.replace(amp, '&');;
                setColor(<?php echo $from[0]; ?>, <?php echo $from[1]; ?>, <?php echo $from[2]; ?>, 'new_in_cart')
                setTimeout("fade(<?php echo $from[0]; ?>, <?php echo $from[1]; ?>, <?php echo $from[2]; ?>, <?php echo $to[0]; ?>, <?php echo $to[1]; ?>, <?php echo $to[2]; ?>, <?php echo ADD_CART_STEPS; ?>, 'new_in_cart');", <?php echo ADD_CART_DELAY; ?>);
            }
        }
        http.send(post);
    }
}

function setColor(r, g, b, element_name) {
    var hr = hex(r); var hg = hex(g); var hb = hex(b);
    element = document.getElementById(element_name);
    element.style.backgroundColor = "#"+hr+hg+hb;
    //console.log("#"+hr+hg+hb);
}

function hex(i) {
    if (i < 0)
    return "00";
    else if (i > 255)
    return "ff";
    else
    return "" + hexa[Math.floor(i/16)] + hexa[i%16];
}

function fade(sr, sg, sb, er, eg, eb, step, element) {
    for(var i = 0; i <= step; i++) {
        setTimeout("setColor(Math.floor(" + sr + " + ((( " + er + " - " + sr + " )/ " + step + ") * " + i + ")), Math.floor(" + sg + " + ((( " + eg + " - " + sg + " )/ " + step + ") * " + i + ")), Math.floor(" + sb + " + ((( " + eb + " - " + sb + " )/ " + step + ") * " + i + ")), '" + element + "');", i * step);

    }
}
