
	function connectToDropbox()
	{
		var c = gbConfirm("You can back up this outline to your Dropbox account but you must first grant GroundBooth access. Do you want to connect your accounts now?");
		if (!c)
		{
			return;
		}
		top.location.href = weburl + "dropbox/authorize/";
	}

	function singleDropboxBackup(oid)
	{
		$("#dropbox_link_" + oid).hide();
		$("#dropbox_link_backup_" + oid).show();
		
		var h = harvest(null);
		h.add("singleDropboxBackup", "1");
		h.add("outline_id", oid);
		ajax("outline", h.pJSON(), function(data)
		{
			$("#dropbox_link_backup_" + oid).hide();
			$("#dropbox_link_" + oid).show();			
		});
	}

	function fullDropboxBackup()
	{
		var dbl = $("#dropbox_backing_up")
		dbl.html("Backing up...");
		
		var h = harvest(null);
		h.add("fullDropboxBackup", "1");				
		ajax("outline", h.pJSON(), function(data)
		{
			var r = pJSON(data);
			$("#dropbox_backing_up").html(r.message);
		});	
	}


	function ajax(url, data, func)
	{		
		$.ajax({
		  url: "http://www.groundbooth.com/ajax/?url=" + url,
		  type: "POST",
		  data: data,
		  success: func
		});			
	}
	
	function harvestAdd(a, b)
	{
		// vars
		if (b==null)
		{			
			delete this.vars[a];
		}
		else
		{
			this.vars[a] = b;
		}
	}
	

	function harvestSet(a, b)
	{
		// vars
		if (b==null)
		{			
			delete this.vars[a];
		}
		else
		{
			this.vars[a] = b;
		}
	}	

	function harvestGet(a)
	{
		return this.vars[a];
	}	
	
	function pJSON(data)
	{
		if (trim(data)!="")
		{			
			try
			{
				return jQuery.parseJSON(data);
			}
			catch (e)
			{
				//alert(data);
			}
		}
		else
		{
			return null;
		}
	}
	
	function harvestParsedJson()
	{
		return jQuery.parseJSON(harvestLoopStuff(this.vars, "json"));
	}
	
	function harvestLoopStuff(vars, get)
	{
		var json = "";
		var url = "";
		var count = 0;
		
		for(var k in vars)
		{
			var v = vars[k];
			json += ("\"" + k + "\" : \"" + escape(v) + "\", ");
			url += k + "=" + escape(v) + "&";
			count++;
		}		
		
		json = "{" + json.substr(0, json.length-2) + "}";
		url = url.substr(0, url.length-1);
		
		switch(get)
		{
			case "json":
				return json;
				break;
			case "url":
				return url;
				break;
			case "count":
				return count;
				break;
		}
	}

	
	function harvestJson()
	{
		return harvestLoopStuff(this.vars, "json");
	}
	
	function harvestUrl()
	{
		return harvestLoopStuff(this.vars, "url");
	}
	
	function harvestCount()
	{
		return harvestLoopStuff(this.vars, "count");
	}
	
	function harvestRemove(k)
	{
		delete this.vars[k];
	}
	
	function harvest(h)
	{

		
		function oHarvest()
		{
			this.vars = null;	
			this.count = harvestCount;				
			this.url = harvestUrl;
			this.json = harvestJson;
			this.add = harvestAdd;
			this.get = harvestGet;
			this.set = harvestSet;
			this.remove = harvestRemove;
			this.delete = harvestRemove;						
			this.pJSON = harvestParsedJson;
		}		
		
		var r = new oHarvest();
		var i = 0;
		var divs = document.getElementsByTagName("DIV");
		var div = null;
		var validDiv = false;
		for (i = 0; i < divs.length; i++)
		{
			div = divs[i];
			if (div.getAttribute("id")==h)
			{
				validDiv = true;
				break;
			}	
		}
		
		if (h==null)
		{
			r.vars = new Array();	
			return r;			
		}
		
		if (validDiv)
		{
			var c = div.getElementsByTagName("input");		// inputs
			var c2 = div.getElementsByTagName("select");	// selects
			var c3 = div.getElementsByTagName("textarea");	// textareas
			var ci = null;
			var vars = new Array();
			var calcValue = null;
						
			//r.count = c.length + c2.length + c3.length;
			
			// gets all input boxes (not buttons)
			for (i = 0; i < c.length; i++)
			{
				ci = c[i];
				
				if (ci.type=="text" || ci.type=="password" || ci.type=="hidden")
				{
					calcValue = ci.value.replace(/'/g, "'");;
				}
				
				if (ci.type=="checkbox")
				{
					calcValue = (ci.checked) ? "1" : "0";
				}

				if (ci.type=="radio")
				{
					calcValue = ci.checked;
				}											
				
				if (ci.type!="button")
				{
					vars[ci.id] = calcValue;
				}
			}	
			
			// gets all select boxes
			for (i = 0; i < c2.length; i++)
			{
				ci = c2[i];				
				calcValue = ci.value.replace(/'/g, "'");;				
				vars[ci.id] = calcValue;
			}
			
			// gets all textareas
			for (i = 0; i < c3.length; i++)
			{
				ci = c3[i];				
				calcValue = ci.value.replace(/'/g, "'");	
				vars[ci.id] = calcValue;
			}			
			
			r.vars = vars;
			return r;
		}
		else
		{
			return null;
		}
	
	}
