//this function returns quantity of goodie 
function getQuantity( id ) {
	var cart = getCookie("cart");
	var goody_prefix = "goody"+id+":";
		
	//no cart at all
	if(!cart)
		return 0;

	
	//no fucking id - bail out!
	if(!id)
		return;


		
	goody_start_index = cart.indexOf(goody_prefix);
	if(goody_start_index == -1){
		//no such goody in our cart. We shoud add it
		return 0;
	}else{
		goody_end_index = cart.indexOf(";", goody_start_index + goody_prefix.length );
		if( goody_end_index == -1 )
			goody_end_index = cart.length;
		return cart.substring(goody_start_index + goody_prefix.length, goody_end_index);
	}	
	return;
}		
	
	
	
//this function will add good to users cart
//make sure that cookies.js are included somwhere before this script
function addGood( id ){
	var final_qty = 1 + getQuantity(id)*1;
	var cart = getCookie("cart");
	var goody_prefix = "goody"+id+":";
	var goody_str = goody_prefix+final_qty;
	
	
	//no fucking id - bail out!
	if(!id)
		return;


	if(!cart)
		cart="";
	//we should check if there is already a good with such an ID
	goody_start_index = cart.indexOf(goody_prefix);
	if(goody_start_index == -1){
		//no such goody in our cart. We shold add it
		goody_str="goody"+id+":"+final_qty;
		if(cart!="") goody_str = ";"+goody_str;
		setCookie( "cart", cart+goody_str);
	}else{
		goody_end_index = cart.indexOf(";", goody_start_index + goody_prefix.length );
		if( goody_end_index == -1 )
			goody_end_index = cart.length;
		cart_begin = cart.substring(0, goody_start_index);
		cart_end = cart.substring(goody_end_index, cart.length);
		setCookie( "cart", cart_begin+goody_str+cart_end );
	}	
	return;
}		   

//this function dletes the fucking goodie from the cart
//let them fuck up!
function delGood( id ){
	var cart = getCookie("cart");
	var goody_prefix = "goody"+id+":";
	
	//no fucking id - bail out!
	if(!id)
		return;

	//no cart - get the fuck up!!!
	if(!cart)
		return;

	goody_start_index = cart.indexOf(goody_prefix);
	if(goody_start_index == -1){
		//no such goody in our cart. We shold fuck the world up!!!
		return;
	}else{
		goody_end_index = cart.indexOf(";", goody_start_index + goody_prefix.length );
		if( goody_end_index == -1 ){
			goody_end_index = cart.length;
			cart_end = "";
		}else{
			cart_end = cart.substring(goody_end_index+1, cart.length);
		}
		cart_begin = cart.substring(0, goody_start_index);
		cart_new = cart_begin+cart_end;			
		last_semi_index = cart_new.lastIndexOf(";");
		if(last_semi_index==cart_new.length-1){
//			alert (cart_new.substring(0,cart_new.length-1));
			if (!cart_new) deleteCookie("cart");
			else setCookie( "cart", cart_new.substring(0,cart_new.length-1));
//			setCookie( "cart", "");
		}else{
			setCookie( "cart", cart_new);
		}
	}	
	return;
}	


//this unction will set quantity of goody id in the cart
//make sure that cookies,js is included BEFORE this scripts
function setQuantity( id, qty ){
	var final_qty = ((qty) ? qty : 1);	//if there is no qty - set it to 1
	var cart = getCookie("cart");
	var goody_prefix = "goody"+id+":"; //if there is no id - bail out
	var goody_str = goody_prefix+final_qty;
	
	//no fucking id - bail out!
	if(!id)
		return;

	//check if there is some legal cart
	if(!cart)
		cart="";
	
	if(final_qty == 0){
		delGood( id );
		return;
	}
	
	goody_start_index = cart.indexOf(goody_prefix);
	if(goody_start_index == -1){
		//no such goody in our cart. We shold add it
		goody_str="goody"+id+":"+final_qty;
		if(cart!="") goody_str = ";"+goody_str;
		setCookie( "cart", cart+goody_str);
	}else{
		goody_end_index = cart.indexOf(";", goody_start_index + goody_prefix.length );
		if( goody_end_index == -1 )
			goody_end_index = cart.length;
		cart_begin = cart.substring(0, goody_start_index);
		cart_end = cart.substring(goody_end_index, cart.length);
		setCookie( "cart", cart_begin+goody_str+cart_end );
	}	
	return;
}

