

// AJAX STARTS HERE
// PRODUCTS WISHLIST: ADD TO
function ajax_add_to_wishlist(ADDRESS)
{	
	$.ajax({ 
		url: ADDRESS, 
		success: function(data){
        	if(data=="err1") alert("Eroare!");
        	else alert(data);
      	}});
}

// PRODUCTS WISHLIST: DELETE
function ajax_delete_from_wishlist(DIV, ADDRESS)
{	
	$.ajax({ 
		url: ADDRESS, 
		success: function(data){
        	if(data=="err1") alert("Eroare!");
        	else $("#"+DIV).hide("blind");
      	}});
	return false;
}

// PRODUCTS WISHLIST: UPDATE RATING
function ajax_update_rating_wishlist(ID, RATING, ADDRESS)
{	
	$.ajax({ 
		url: ADDRESS, 
		success: function(data){
        	if(data=="err1") alert("Eroare!");
        	else {
        		var html = "";
		      	for(i=1;i<=RATING;i++)
		      		html += "<img src='" + SITE_ROOT + "static/image/star_red.png" + "' alt='' style='width: 16px; height: 15px;' />";
		      	for(i=1;i<=5-RATING;i++)
		      		html += "<img src='" + SITE_ROOT + "static/image/star_grey.png" + "' alt='' style='width: 16px; height: 15px;' />";
		      	
		      	$("#wishlist_stars_"+ID).html(html);
		      	$("#wishlist_voted_"+ID).html("<small>Ati acordat " + RATING + " stelute!</small>");
        	}
      	}});
	return false;	
}

// PRODUCTS WISHLIST: UPDATE DESCRIPTION
function ajax_update_description_wishlist(INPUT, ADDRESS)
{	
	var text = $("#" + INPUT).val();

	$.ajax({ 
		url: ADDRESS + "&description=" + text, 
		success: function(data){
	       	if(data=="err1") alert("Eroare!");
	       	else {};
  	}});

	return false;	
}

// SHOPPING CART ADD FROM PRODUCTS DETAIL PAGE
function ajax_shopcart_add_set()
{
	var ID 	= $("#idProduct").val();
	var Q 	= 1;
	
	var ADDRESS = SITE_ROOT + "shopping_cart/?action=add&idProduct="+ID+"&quantity="+Q;
	
	$.ajax({ 
		url: ADDRESS, 
		success: function(data){
        	if(data=="err1") alert("Stoc insuficient!");
        	else {
        	
				var info = data.split(" ");
				var total = info[1];
				var quantity = info[0];
				var uniqueQ = info[2];
				
				$("#shopBarQ").text(uniqueQ);
				$("#shopBarV").text(formatCurrency(total*CURS_BNR,2) + " RON");
				$("#slideTotal").text(formatCurrency(total*CURS_BNR,2));
				
				if(uniqueQ>3) $("#slideOther").html("In cosul tau de cumparaturi mai sunt alte <br />" + (uniqueQ-3) + " produse"); 
				
				// incarca lista de produse pt drop-down (ultimele 3 prod)
				var ADDRESS = SITE_ROOT + "shopping_cart/?printShop="+Q;
				//alert(ADDRESS);
				$.ajax({ 
					url: ADDRESS, 
					success: function(data){
			        	$("#slidePop").html(data);
						$("#cos_dropdown").slideDown();
			        }});
        	};
      	}});
}

// ADD TO CART FROM LIST
function ajax_shopcart_add_set2(ID)
{
	var Q 	= 1;
	
	var ADDRESS = SITE_ROOT + "shopping_cart/?action=add&idProduct="+ID+"&quantity="+Q;
	
	$.ajax({ 
		url: ADDRESS, 
		success: function(data){
        	if(data=="err1") alert("Stoc insuficient!");
        	else {
        	
				var info = data.split(" ");
				var total = info[1];
				var quantity = info[0];
				var uniqueQ = info[2];
				
				$("#shopBarQ").text(uniqueQ);
				$("#shopBarV").text(formatCurrency(total*CURS_BNR,2) + " RON");
				$("#slideTotal").text(formatCurrency(total*CURS_BNR,2));
				
				if(uniqueQ>3) $("#slideOther").html("In cosul tau de cumparaturi mai sunt alte <br />" + (uniqueQ-3) + " produse"); 
				
				// incarca lista de produse pt drop-down (ultimele 3 prod)
				var ADDRESS = SITE_ROOT + "shopping_cart/?printShop="+Q;
				//alert(ADDRESS);
				$.ajax({ 
					url: ADDRESS, 
					success: function(data){
			        	$("#slidePop").html(data);
						$("#cos_dropdown").slideDown();
			        }});
        	};
      	}});
}

//SHOPPING CART: ADD
function ajax_shopcart_add()
{
	var ID 	= $("#idProduct").val();
	var Q 	= $("#quantitySelect").val();
	var V 	= $("#sizeSelect").val();
	
	var ADDRESS = SITE_ROOT + "shopping_cart/?action=add&idProduct="+ID+"&idVersion="+V+"&quantity="+Q;
	
	$.ajax({ 
		url: ADDRESS, 
		success: function(data){
     	if(data=="err1") alert("Stoc insuficient!");
     	else {
     	
				var info = data.split(" ");
				var total = info[1];
				var quantity = info[0];
				var uniqueQ = info[2];
				
				$("#shopBarQ").text(uniqueQ);
				$("#shopBarV").text(formatCurrency(total,2) + " RON");
				$("#slideTotal").text(formatCurrency(total,2));
				
				if(uniqueQ>3) $("#slideOther").html("In cosul tau de cumparaturi mai sunt alte <br />" + (uniqueQ-3) + " produse"); 
				
				// incarca lista de produse pt drop-down (ultimele 3 prod)
				var ADDRESS = SITE_ROOT + "shopping_cart/?printShop="+Q;
				
				$.ajax({ 
					url: ADDRESS, 
					success: function(data){
			        	$("#slidePop").html(data);
			        	
			        	$(".shoppingCart_box").css({"background-image":"url('"+SITE_ROOT+"static/image/dropdown_background.jpg')"});
						$("#cos_dropdown").slideDown();
						$(".green").css({"color":"#ffffff"});
			        }});
     	};
   	}});
}

//SHOPPING CART: CHANGE QUANTITY
function ajax_shopcart_quantity(ID, TYPE, PRICE)
{	
	var q = parseInt($("#shoppingProductQuantity_"+ID).val());
	var err = 0; 
	//var total = 0; var totalFinal = 0; var discount = 0;
	
	if(TYPE=="+")  	q = q+1;
	else 			q = q-1;	
	
	if(q<=0) {
		var ADDRESS = SITE_ROOT + "shopping_cart/?action=delete&idProduct="+ID;
		ajax_shopcart_delete(ADDRESS, ID);
		return;
	}
	else
	{
		var ADDRESS = SITE_ROOT + "shopping_cart/?action=setquantity&idProduct="+ID+"&quantity="+q;
		
		$.ajax({ 
			url: ADDRESS, 
			success: function(data){
     	if(data=="err1") alert("Stoc insuficient!");
     	else {
		    	// get discount value & total value
				var temp = data;
				var info = data.split(" ");
				var total = info[1];
				var discount = info[0];
				var totalFinal = total * (1-discount/100);
				var thisTotal = info[2];
				
				$("#shoppingProductQuantity_"+ID).val(q); 
				$("#shoppingProductTotal_"+ID).text(thisTotal); 
	
				if(discount>0) {
					$("#has_discount").show();
					$("#total_initial").text(formatCurrency(total,2));
					$("#total_discount").text(discount);
				}
				else if(discount==0) $("#has_discount").hide();

				$("#total_value").text(formatCurrency(totalFinal,2));
				
		    }
		  }
		});
	}
}

//SHOPPING CART: DELETE
function ajax_shopcart_delete(ADDRESS, ID)
{	
	$.ajax({ 
			url: ADDRESS, 
			success: function(data){
     	if(data=="err1") alert("Eroare!");
     	else {
				var temp = data;
				var info = data.split(" ");
				var total = info[1];
				var discount = info[0];
				var totalFinal = total * (1-discount/100);
				
				if(discount>0) {
					$("#has_discount").show("blind");
					$("#total_initial").text(formatCurrency(total,2));
					$("#total_discount").text(discount);
				}
				else if(discount==0) $("#has_discount").hide();
				
				$("#total_value").text(formatCurrency(totalFinal,2));
				
				$("#shoppingProductLine_"+ID).hide("blind");
				
				if(total==0) {
					$("#nextStep1").hide("fade");
					$("#nextStep2").hide("fade");
				}	
		    }
		  }
	});
}

//PRODUCTS STAR RATING
function ajax_rate_product(ADDRESS)
{	
	$('#product_rating_message').innerHTML = "<center>Please wait ...</center>";	
	//new Ajax.Updater('product_rating_message', ADDRESS, { method:'get' });*/
	
	$.ajax({ 
			url: ADDRESS, 
			success: function(data){
     	if(data=="err1") alert("Eroare!");
     	else { $("#product_rating_message").text(data); }
     }});
}
//END AJAX


function formatCurrency(amount, noDecimals)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(noDecimals!=1){
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	}
	s = minus + s;
	return s;
}

// WISHLIST CHANGE STARR
function changeStar(id)
{	
	for(i=1;i<=id;i++)
		$('#big_star_'+i).attr("src", SITE_ROOT + "static/image/star_yellow.jpg");
	for(i=5;i>id;i--)
		$('#big_star_'+i).attr("src", SITE_ROOT + "static/image/star_grey.jpg");
}

