var blink;
var blink_cnt = 0;

function  createRequest() {
	if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}	
    if (!xmlHttp) {
        return false;
    }
 } 
function alertCart() {
    if ((blink_cnt&1) == 0) {
        bgndCart(false);
        if (blink_cnt >= 6) {
            window.clearInterval(blink);
        }
    } else {
        bgndCart(true);
    }
    blink_cnt++;     
}
function bgndCart(on) {
    cart = document.getElementById("cart_alert"); 
    if (on) { 
        cart.style.visibility = "visible";
    } else {
        cart.style.visibility = "hidden";
    }
}
function rewriteCart(cart_content) {
  document.getElementById("box_shopping_cart").innerHTML = cart_content ; 
  bgndCart(true);
  blink_cnt = 0;
  blink = setInterval("alertCart()",250);
  document.getElementById('cart_loading').style.visibility = "hidden";
  cart_style = document.getElementById('box_shopping_cart').style;
  cart_style.opacity = 10;
  cart_style.filter = 'alpha(opacity=100)';
} 
function buy_now(pid) {
    animateImage("myImage_div" + pid ,"myImage" + pid,"box_shopping_cart");
    cart_style = document.getElementById('box_shopping_cart').style;
    cart_style.opacity = 0.4;
    cart_style.filter = 'alpha(opacity=40)';
    document.getElementById('cart_loading').style.visibility = "visible";
    createRequest();
    var url = "ajax/shopping_cart_ajax.php?action=buy_now&ajax=true&products_id=" + pid; 
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=function(){if(xmlHttp.readyState!=4)return;if(xmlHttp.status==200){rewriteCart(xmlHttp.responseText)}}; 
    xmlHttp.send(null); 
    return false;
}
function add_product() {
    animateImage("product_image_div","product_image1","box_shopping_cart");
    cart_style = document.getElementById('box_shopping_cart').style;
    cart_style.opacity = 0.4;
    cart_style.filter = 'alpha(opacity=40)';
    document.getElementById('cart_loading').style.visibility = "visible";
    pid = document.getElementById('products_id').value;
    prod_form = document.getElementById('cart_quantity');
    var params = "products_id=" + pid;
	for (var i=0; i<prod_form.getElementsByTagName('select').length;i++) with (prod_form.getElementsByTagName('select')[i])
	{
		params += "&" + name + "=" + value;
    }
    createRequest();
    var url = "ajax/shopping_cart_ajax.php?action=add_product&ajax=true"; 
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");    
    xmlHttp.onreadystatechange=function(){if(xmlHttp.readyState!=4)return;if(xmlHttp.status==200){rewriteCart(xmlHttp.responseText)}}; 
    xmlHttp.send(params);	
    return false;
}
/* anumate the product image to shopping cart box */
function animateImage(image_div,imageId, cartBoxId){   
    var image = $('#'+imageId).offset();
    //var cart = document.getElementById(imageId).offset();
    var cart  = $('#'+cartBoxId).offset();
    var img = document.getElementById(imageId);
    $('#'+image_div).before('<img src="' + $('#'+imageId).attr('src') + '" id="temp" style="border-style:none;z-Index:5;position: absolute; top: ' + Math.round(image.top) + 'px; left: ' + Math.round(image.left) + 'px; width:' + img.offsetWidth + 'px; height:' + img.offsetHeight + 'px;" />');
    params = {
        top : (cart.top+40) + 'px',
        left : (cart.left+90) + 'px',
        opacity : 0.0,
        width : '20px',
        height : '20px'
    };
    $('#temp').animate(params, 'slow', false, function () {
        $('#temp').remove();
    });
}


