
//////////////////////////////////////////////////////////////////////////////////////////////
//
//
//var home_url = "http://demha.com/avs";
var home_url = "http://www.artvenusstudio.com";
var cookiePath='/';
var cookieLifeInSeconds=30*60*15;

function getsession()
{
	var cookieName='session';
	var sessionid=null;
	var login='';
	var cookieValue=getCookie(cookieName);

	//if(!testCookies())
	//	return false;

	sessionid=getFieldFromCookie('session', 'sessionid:');
	login=getFieldFromCookie('session', 'login:');

	return sessionid;
}

//////////////////////////////////////////////////////////////////////////////////////////////
//
//
function updatecart()
{
	var formid='form_updatecart';
	var cookieName='cart';
	var cookieValue=getCookie(cookieName);
	var formelem;
	var itemValue;
	var itemid;
	var ret=false;
	
	formelem=document.getElementById(formid);
	if(!formelem)
		return false;
	
	try{
	_items_dropdownlist=formelem.items;
	if(!_items_dropdownlist)
		return null;
	itemid = _items_dropdownlist.options[_items_dropdownlist.selectedIndex].value;
	if(itemid>0)
	{
		formelem.action='/items/'+itemid+'/';
		//alert('/items/'+itemid+'/');
		ret=true;
	}
	}catch(e){
		alert(e.message);
	}

	return ret;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
//
function get_insurance(formelem)
{
	if(!formelem)
		return 0;
	if(!formelem.insurance)
		return 0;
	try{
		formelem.insurance.value=formelem.insurance_.value;
		
		if(formelem.insurance_option && formelem.insurance_option.value=='1')//there is option to buy insurance
		{
			if(!formelem.buy_insurance || !formelem.buy_insurance.checked)//and buy insurance option is not checked
				formelem.insurance.value=0;//user selected no insurance
		}
	}catch(e){
		alert('get_insurance error: '+e.message);
	}
	//alert(insurance);
	return 1;
}

function get_sh_charges(formelem)
{
	if(!formelem)
		return 0;
	if(!formelem.sh_charges_)
		return 0;
	try{
		var i;
		for(i=0; i<formelem.sh_charges_.length; i++)
		{
			if(formelem.sh_charges_[i].checked)
				//alert(formelem.sh_charges_[i].value);
				formelem.sh_charges.value=formelem.sh_charges_[i].value;
		}
	}catch(e){
		alert('get_sh_charges error: '+e.message);
	}
	return 1;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
//
function addtocart()
{
	var formid='form_addtocart';
	var cookieName='cart';
	var cookieValue=getCookie(cookieName);
	var formelem;

	formelem=document.getElementById(formid);
	if(!formelem)
		return false;
	
	get_insurance(formelem);
	get_sh_charges(formelem);
		
	return addtocart2(formelem, cookieValue);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
//
function addtocart2(formelem, cookieValue)
{
	var formelem;
	var isvalid;
	var insurance=0;
	var sh_charges=0;
	var q;
	var i;
	var sessionid;
	var login;
	
	if(!formelem)
		return false;
		
	//validate the quantity
	i=getNonNegInteger(formelem.itemid.value);
	if(i<=0)
	{
		alert('Itemd ID cannot be negative');
		formelem.quantity.focus();		
		return false;
	}
	q=getNonNegInteger(formelem.quantity.value);
	if(q<0)
	{
		alert('Quantity cannot be negative');
		formelem.quantity.focus();		
		return false;
	}
	
	if(formelem.insurance)
		insurance=formelem.insurance.value;
	if(formelem.sh_charges)
		sh_charges=formelem.sh_charges.value;
		
	sessionid=getFieldFromCookie('session', 'sessionid:');
	login=getFieldFromCookie('session', 'login:');
	if(!sessionid)
	{//use dummy entry for cookieValue for the first time
		if(q<=0)
			return false;
		cookieValue='|`itemid:'+i+'`sellerid:'+formelem.sellerid.value+'`quantity:'+q;
		cookieValue+='`picture:'+formelem.picture.value+'`name:'+formelem.name.value+'`sellername:'+formelem.sellername.value+'`currency:'+formelem.currency.value;
		cookieValue+='`sales_price:'+formelem.sales_price.value+'`sh_charges:'+sh_charges+'`insurance:'+insurance;
		showCart(cookieValue);
	}
	else
	{//update cookie value and use that to show cart
		cookieValue=updateCartItem(
			i, 
			formelem.sellerid.value, 
			q, 
			formelem.picture.value, 
			formelem.name.value, 
			formelem.sellername.value, 
			formelem.currency.value, 
			formelem.sales_price.value, 
			sh_charges, 
			insurance, 
			cookieLifeInSeconds, 
			cookiePath);
	
		showCart(cookieValue);
	}		
	formelem.sessionid.value=sessionid;
	formelem.login.value=login;
	return true;
}

///////////////////////////////////////////////////////////////////////////////////////
//function updateCartItem()
//
//if itemid matches with an itemid in the cookie:
//	if new quantity is +
//		quantity for the item in cart is updated by new quantity
//	if new quantity is 0
//		item is removed from cart
//	groupid and sellerid will be updated in each case
//if itemid does not match any itemid in the cart
//	if new quantity is +
//		new item is added to cart
//	if new quantity is 0 or -
//		nothing is done
//
function updateCartItem(itemid, sellerid, quantity, 
		picture, name, sellername, currency, 
		sales_price, sh_charges, insurance, 
		expire_days, path)
{
	var cookieName='cart';
	var cookieValue=null;
	var cookieValue2=null;
	var nodePos=0;
	var nodeEnd=0;
	var nodeValue=null;
	var _itemid=null;
	var _sellerid=null;
	var _quantity=null;
	var _picture=null;
	var _name=null;
	var _sellername=null;
	var _currency=null;
	var _sales_price=null;
	var _sh_charges=null;
	var _insurance=null;
	var cartItem=null;
	var itemFoundInCart=0;
	
	if(!itemid)
		return 0;

	cookieValue=getCookie(cookieName);
	
	for(;cookieValue;)
	{
		nodePos=cookieValue.indexOf('|`', nodeEnd);
		if(nodePos==-1)
			break;
		nodeEnd=cookieValue.indexOf('|', nodePos+1);
		if (nodeEnd!=-1)
			nodeValue=unescape(cookieValue.substring(nodePos, nodeEnd));
		else
		{
			nodeValue=cookieValue.substring(nodePos);
			nodeEnd=nodePos+1;
		}

		//itemid
		_itemid=getField(nodeValue, 'itemid:')
		if(!_itemid)
		{//if no items were found in the cart, then write back whateve we read
			if(cookieValue2)
				cookieValue2+=nodeValue;
			else
				cookieValue2=nodeValue;
			continue;
		}
		
		try
		{
		_sellerid=getField(nodeValue, 'sellerid:');
		_quantity=getField(nodeValue, 'quantity:');
		_picture=getField(nodeValue, 'picture:');
		_name=getField(nodeValue, 'name:');
		_sellername=getField(nodeValue, 'sellername:');
		_currency=getField(nodeValue, 'currency:');
		_sales_price=getField(nodeValue, 'sales_price:');
		_sh_charges=getField(nodeValue, 'sh_charges:');
		_insurance=getField(nodeValue, 'insurance:');
		}catch(e)
		{
			alert(e.message);
		}	   

		if(_itemid==itemid)
		//this item quantity, sh_charges, insurance will be updated
		{
			_quantity=parseInt(quantity);
			_sh_charges=parseFloat(sh_charges);
			_insurance=parseFloat(insurance);
			//var f=parseInt(fieldValue4);
			
			itemFoundInCart=1;
			if(_quantity<=0)//this item will be removed
				continue;
		}

		cartItem='|`itemid:'+_itemid+'`sellerid:'+_sellerid+'`quantity:'+_quantity;
		cartItem+='`picture:'+_picture+'`name:'+_name+'`sellername:'+_sellername+'`currency:'+_currency;
		cartItem+='`sales_price:'+_sales_price+'`sh_charges:'+_sh_charges+'`insurance:'+_insurance;

		if(cookieValue2)
			cookieValue2+=cartItem;
		else
			cookieValue2=cartItem;
	}
	
	if(!itemFoundInCart)//item was not found in the cart
	{//add a new item
		var quantity=parseInt(quantity);
		if(quantity>0)
		{
			cartItem='|`itemid:'+itemid+'`sellerid:'+sellerid+'`quantity:'+quantity;
			cartItem+='`picture:'+picture+'`name:'+name+'`sellername:'+sellername+'`currency:'+currency;
			cartItem+='`sales_price:'+sales_price+'`sh_charges:'+sh_charges+'`insurance:'+insurance;
			if(cookieValue2)
				cookieValue2+=cartItem;
			else
				cookieValue2=cartItem;
		}
	}
	
	//document.write(cookieValue2);
	setCookie(cookieName, cookieValue2, expire_days, path);
	
	return cookieValue2;
}

//////////////////////////////////////////////////////////////////////////////////////
//
function showCart(cookieValue)
{
	var cookieName='cart';
	var nodePos=0;
	var nodeEnd=0;
	var nodeValue=null;
	var _div_cart=null;
	var _div_cartitems=null;
	var divtext='';
	var total_items_in_cart=0;
	var total_quantities_in_cart=0;
	var total_cost=0;
	var currency='$';
	var name=null;
	var sales_price=null;
	var sh_charges=null;
	var insurance=null;
	var quantity=null;
	var _sales_price=null;
	var _sh_charges=null;
	var _insurance=null;
	var _quantity=null;
	var _price_of_item=null;
	var _price_of_item=null;
	var formelem=null;
	var _div_carttitle=null;
	
	var i=0;
	
	try
	{
		_div_cart=document.getElementById('div_cart');
		if(!_div_cart) {
			alert("!_div_cart");
			return null;
		}
		_div_cartitems=document.getElementById('div_cartitems');
		if(!_div_cartitems) {
			alert("!_div_cartitems");
			return null;
		}
		_div_carttitle=document.getElementById('div_carttitle');
		if(!_div_carttitle) {
			alert("!_div_carttitle");
			return null;
		}
	}
	catch(e)
	{
		alert(e.message);
		return null;
	}

	if(!cookieValue)
		cookieValue=getCookie(cookieName);
	if(!cookieValue)
	{
		_div_cart.className="div_cart_hidden";	
		return null;
	}
			
	for(i=0; cookieValue; i++)
	{
		nodePos=cookieValue.indexOf('|`', nodeEnd);
		if(nodePos==-1)
			break;
		nodeEnd=cookieValue.indexOf('|', nodePos+1);
		if (nodeEnd!=-1)
			nodeValue=unescape(cookieValue.substring(nodePos, nodeEnd));
		else
		{
			nodeValue=cookieValue.substring(nodePos);
			nodeEnd=nodePos+1;
		}

		itemid=getField(nodeValue, 'itemid:');
		if(!itemid) 
			continue;
		
		try{
		name=getField(nodeValue, 'name:');
		currency=getField(nodeValue, 'currency:');
		sales_price=getField(nodeValue, 'sales_price:');
		sh_charges=getField(nodeValue, 'sh_charges:');
		insurance=getField(nodeValue, 'insurance:');
		quantity=getField(nodeValue, 'quantity:');
		
		//alert(nodeValue);
		//alert('quantity:'+quantity+', picture:'+picture+', name:'+name+', sellername:'+sellername+', currency:'+currency);
		_sales_price=getNonNegFloat(sales_price);
		_sh_charges=getNonNegFloat(sh_charges);
		_insurance=getNonNegFloat(insurance);
		_quantity=getNonNegInteger(quantity);
		_price_of_item=_sales_price+_sh_charges+_insurance;
		_price_of_item=_price_of_item.toFixed(2);
		}catch(e){
			alert(e.message);
		}
		
		var a1='<a href="'+home_url+'/items/'+itemid+'/">';
		var a2='</a>';
//		var img='<img class="img3" src="'+home_url+'/get.php4?file=th&itemid='+itemid+'"/>';
		var img='<img class="img3" src="'+home_url+'/th/items/'+itemid+'"/>';
		divtext+='<div class="div_cartitem">'+a1+'<div class="div_cartitemimg">'+img+'</div><!--div_cartitemimg--><br/>'+name+a2+'&nbsp;&nbsp;'+currency+' '+_price_of_item+' x '+quantity+'</div><!--div_cartitem-->';

		total_items_in_cart++;
		total_quantities_in_cart+=_quantity;
		total_cost+=_price_of_item*_quantity;
		
		formelem=document.getElementById("form_addtocart");
		//also update the item page with the selected sh_charges and insurance
		if(formelem && formelem.itemid.value==itemid)
		{
			//update sh_charges on the item page from cookie value
			if(formelem.sh_charges_)
			{
				var j;
				for(j=0; j<formelem.sh_charges_.length; j++)
				{
					if(sh_charges==formelem.sh_charges_[j].value)
					{
						if(formelem.sh_charges_[j].checked==false)
							formelem.sh_charges_[j].checked=true;
						break;
					}
				}
			}
			//update insurance on the item page from cookie value
			if(formelem.insurance_option && formelem.buy_insurance && _insurance>0)
			{
				if(formelem.buy_insurance.checked==false);
					formelem.buy_insurance.checked=true;
			}
			//update quantity on the item page from cookie value
			if(formelem.quantity && _quantity!=formelem.quantity.value)
			{
				formelem.quantity.value=_quantity;
			}
		}
	}
		
	//TODO if currencies are different for different items, then what?
	if(0)//since we can't check for different currencies currently, don't show total.
		divtext+='Your total is <b>'+currency+total_cost+'</b><br/>';

	if(total_items_in_cart>0)
	{
		_div_cart.className="div_cart";
		_div_cartitems.innerHTML=divtext;
	}
	else
	{
		_div_cart.className="div_cart_hidden";
	}
	
//	if(total_items_in_cart==1)
//		_div_carttitle.innerHTML="Shopping cart contains "+total_items_in_cart+" item";
//	else
//		_div_carttitle.innerHTML="Shopping cart contains "+total_items_in_cart+" items";
		
	return 1;
}

////////////////////////////////////////////////////////////////////////
var g_cart_frame=0;
var g_cart_nframes=30;
var g_cart_SLIDEDELAY=10;
var g_cart_height=150;//should be same as in style sheet under div_cartitems2
var g_cart_hide_pending=0;
var g_cart_showing=0;
//When doing mouse rollover div, you may get unnecessary mouseout,mouseover events.
//For mouseout event, use setTimeout to call its function, and if mouseover event is triggered soon
//after, then return immediately from the mouseout function when it gets triggered. 
//
/////////////////////////////////////////////////////////////////////////////////////////
//
function slideCart()
{
	var i;
	var _div_cartitems;

	_div_cartitems=document.getElementById('div_cartitems');
	
	if(g_cart_frame==0)
	{
		_div_cartitems.style.height="0px";
		_div_cartitems.style.overflow="hidden";
	}
	else
	{
		var size= (100/g_cart_nframes) * ( (g_cart_frame*g_cart_height)/100 );
		_div_cartitems.style.height=size+"px";
	}

	if(g_cart_frame==g_cart_nframes)
	{
		g_cart_frame=0;
		_div_cartitems.style.overflow="auto";
	}
	else
	{
		g_cart_frame++;
		setTimeout('slideCart()', g_cart_SLIDEDELAY);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
//
function hideDetailCart()
{
	if(!g_cart_hide_pending)
		return;
		
	var _div_cartitems=document.getElementById('div_cartitems');
	if(!_div_cartitems) {
		alert("!_div_cartitems");
		return null;
	}

	_div_cartitems.className="div_cartitems";
	g_cart_hide_pending=0;
	g_cart_showing=0;
}
/////////////////////////////////////////////////////////////////////////////////////////
//
function showDetailCart(show)
{
	var _div_cartitems=document.getElementById('div_cartitems');
	if(!_div_cartitems) {
		alert("!_div_cartitems");
		return null;
	}

	if(show)
	{
		if(!g_cart_showing)
		{
			_div_cartitems.className="div_cartitems2";
			slideCart();
			g_cart_showing=1;
		}
		g_cart_hide_pending=0;
	}
	else
	{
		if(g_cart_showing)
		{
			//_div_cartitems.className="div_cartitems";
			setTimeout('hideDetailCart()', g_cart_SLIDEDELAY*2);
			g_cart_hide_pending=1;
		}
	}
		
}

//////////////////////////////////////////////////////////////////////////////////////
//
function hideCart()
{
	var _div_cart=document.getElementById('div_cart');
	if(!_div_cart) {
		alert("!_div_cart");
		return null;
	}
	
	_div_cart.style.display='none';
}
//////////////////////////////////////////////////////////////////////////////////////
//
function deprecated_positioncart()
{
	var div_cart;
	var div_body;
	var div_cart_height;
	var div_body_height;
	
	return true;

	_div_cart=document.getElementById("div_cart");
	if(!_div_cart)
		return false;
	_div_body=document.getElementById("div_body");
	if(!_div_body)
		return false;

    div_cart_height=parseInt(_div_cart.scrollHeight);
    div_body_height=parseInt(_div_body.scrollHeight);


    if( document.body.scrollTop+div_cart_height<div_body_height )
    {
            _div_cart.style.top=document.body.scrollTop;
    }

	return true;

}

