var cart = 
{
	xhr:null, // used to controll the upload, use one so we dont double save.
	imgObj:null,
	lang:{
		mailsent : "Huskelisten er sendt til <e-post adressen>",
		invalidEmail : "E-post adressen er ikke gyldig",
		szerror : "Feil",
                    smssent : "Sms er sendt til <sms number>",
                    invalidphone : "Du må skrive inn telefonnummer"
	},
	// Adds a product to the cart
	addProduct:function(id,unid)
	{
		if (cart.xhr != null)
		{
			cart.error();
		}else
		{	
			cart.imgObj = SYM.util.dom.getObject("img_" + unid);
			cart.imgObj.src = "/" + dbfp + "/progress.gif" 
			cart.xhr = new SYM.util.xhr("/" + cartdb + "/cart_?open&method=add"+(cart!=null&&cartid==""?"":"&cart=" + cartid )+"&date="+ new Date() +"&prod=" + id,"cart.gotResponse( 		this.getText() )");
			cart.xhr.init();
		}		
	},
 	
	print : function()
	{	
		szurl = "/" + dbfp + "/pages/cartPrint?opendocument&qnfl=none&cart=" + cartid;
		window.open (szurl,
"printWindow","menubar=0,resizable=1,width=600,height=350"); 
	},
	email : {
		send : function()
		{
			var v = SYM.form.getItemValue( "mail" , false , "shoplistform" );
			if ( v == "" || !SYM.form.validEmail(v) )
			{alert(cart.lang.invalidEmail );return;}
			if (cartid  == null || cartid  == "")
			{return false;}
 
			SYM.util.dom.clonePos( SYM.util.dom.getObject("right") , SYM.util.dom.getObject("shield") );
			SYM.util.dom.show("shield")
			cart.xhr = new SYM.util.xhr("/" + cartdb + "/memo?createdocument&cartid="+cartid ,"cart.email.response(this.getText())" , SYM.form.serialize("shoplistform") );
			cart.xhr.init();
			return false;	
		},
		response : function( sztxt )
		{	
			SYM.util.dom.hide("shield");
			if ( sztxt.indexOf("MAIL-SENT") == -1 )
			{
				cart.error();
			}else
			{
				var itempos = SYM.util.dom.findPos("right");
				var aa = SYM.util.dom.getObject("saved");
				if (aa != null)
				{
					aa.style.left = (itempos[0] + 50) + "px";
					aa.style.top = (itempos[1] + 150) + "px";
					aa.style.height = "40px";
					aa.style.display= "";
					aa.innerHTML = cart.lang.mailsent.replace("<e-post adressen>" , "<br>" +SYM.form.getItemValue( "mail")); 
					setTimeout( "SYM.util.dom.hide('saved')" , 3500);
				}
                                            SYM.form.setItemValue( "subject_mail" , "" , "shoplistform" );
                                            SYM.form.setItemValue( "ingress_mail" , "" , "shoplistform" );
				SYM.form.setItemValue( "mail" , "" , "shoplistform" );
				SYM.form.setItemValue( "mailtxt" , "" , "shoplistform" );
			}
		}
	},
	sms : {
		send : function()
		{
			var v = SYM.form.getItemValue( "sms" , false , "smslistform" );
			if ( v == "" )
			{alert(cart.lang.invalidphone );return;}
			if (cartid  == null || cartid  == "")
			{return false;}
 
			SYM.util.dom.clonePos( SYM.util.dom.getObject("right") , SYM.util.dom.getObject("shield") );
			SYM.util.dom.show("shield")
			cart.xhr = new SYM.util.xhr("/" + cartdb + "/sms?createdocument&cartid="+cartid ,"cart.sms.response(this.getText())" , SYM.form.serialize("smslistform") );
			cart.xhr.init();
			return false;	
		},
		response : function( sztxt )
		{	
			SYM.util.dom.hide("shield");
			issmssent++;
			if ( sztxt.indexOf("SMSSENT") == -1 )
			{
				cart.error();
			}else
			{
				var itempos = SYM.util.dom.findPos("right");
				var aa = SYM.util.dom.getObject("saved");
				if (aa != null)
				{
					aa.style.left = (itempos[0] + 50) + "px";
					aa.style.top = (itempos[1] + 150) + "px";
					aa.style.height = "40px";
					aa.style.display= "";
					aa.innerHTML = cart.lang.smssent.replace("<sms number>" , "<br>" +SYM.form.getItemValue( "sms" , false , "smslistform")); 
					setTimeout( "SYM.util.dom.hide('saved')" , 3500);
				}
				SYM.form.setItemValue( "sms" , "" , "smslistform" );
				SYM.form.setItemValue( "smstxt" , "" , "smslistform" );
if (issmssent >= 3)
{
				SYM.util.dom.hide("smssend");
				SYM.util.dom.show("smssent");
}
			}
		}
	},
	
  
	updateCart : function()
	{
		SYM.util.xhr.load("/" + dbfp + "/pages/cart?open&cart="+ cartid +"&now="+ new Date() +"&qnfl=none&ce=1","shoppinglist");	
	},
 	
	error : function()
	{
		SYM.util.dom.hide("shield")
		alert( cart.lang.szerror );
	},
 
 	gotResponse : function( sz , doupdate)
	{
		cart.xhr = null;				
		// Reset the image
		if (cart.imgObj != null)
		{
			var pos = SYM.util.dom.findPos(cart.imgObj);
			var sobj = SYM.util.dom.getObject("saved");
			sobj.style.top = (pos[1]-10) + "px";
			sobj.style.left = (pos[0]+40) + "px";
			sobj.style.display = "";
			cart.imgObj.src = "../media/ico_add_cart_bw.gif/$file/ico_add_cart_bw.gif"
			cart.imgObj = null;
			setTimeout("SYM.util.dom.hide('saved')",1500);
		}
		sz = SYM.util.text.trim(sz)
		szvalues = sz.split(",");
		if (szvalues.length  >= 2 && szvalues[0] == 1)
		{
			document.cookie = "cartid=" + szvalues[1] +";";
			cartid = szvalues[1];
		}else
		{
			if ( sz == "0" )
			{
				cart.error();
			}else
			{
				if( doupdate )
				{cart.updateCart();}
			} 
		}
	},
 
	clear : function()
	{
			SYM.util.dom.clonePos( SYM.util.dom.getObject("shoppinglist") , SYM.util.dom.getObject("shield") );
			SYM.util.dom.show("shield")
			cart.xhr = new SYM.util.xhr("/" + cartdb + "/cart_?open&method=clear"+(cart!=null&&cartid==""?"":"&cart=" + cartid ) + "&now="+new Date(),"cart.gotResponse(this.getText() ,true)");
			cart.xhr.init();
	},
 
	removeProduct:function(id)
	{
		if (cart.xhr != null || cartid == "")
		{
			cart.error();
		}else
		{	
			SYM.util.dom.clonePos( SYM.util.dom.getObject("shoppinglist") , SYM.util.dom.getObject("shield") );
			SYM.util.dom.show("shield")
			cart.xhr = new SYM.util.xhr("/" + cartdb + "/cart_?open&method=remove"+(cart!=null&&cartid==""?"":"&cart=" + cartid )+"&prod=" + id + "&now=" + new Date(),"cart.gotResponse( 		this.getText() , true )");
			cart.xhr.init();
		}		
	},
 
	updateProduct:function(id,item,org)
	{
		// check if item has changed before posting.
		var obj = document.getElementById('qu_' + item);
		var intB = parseInt(obj.value);
		if( isNaN(intB))
		{return}
 		
		if (intB != org)
 		{
			SYM.util.dom.clonePos( SYM.util.dom.getObject("shoppinglist") , SYM.util.dom.getObject("shield") );
			SYM.util.dom.show("shield")
	
			if (intB == 0)
			{
				cart.removeProduct(id); // Delete it instead.
			}else
			{
				cart.xhr = new SYM.util.xhr("/" + cartdb + "/cart_?open&method=update&q="+ intB +(cart!=null&&cartid==""?"":"&cart=" + cartid )+"&prod=" + id + "&now=" + new Date(),"cart.gotResponse( this.getText() , true )");
cart.xhr.init();
			}
			
		}
	}
	
}

