//prepare craft data to upload as an update to an existing craft on KerbalX
        private void update_craft()
        {
            clear_errors();
            if (ok_to_send())
            {
                before_upload("Updating....");
                int craft_id = selected_craft_id;

                WWWForm craft_data = new WWWForm();
                craft_data.AddField("craft_name", craft_name);
                craft_data.AddField("craft_file", craft_file());
                craft_data.AddField("part_data", JSONX.toJSON(part_info()));

                KerbalX.api.update_craft(craft_id, craft_data, (resp, code) => {
                    var resp_data = JSON.Parse(resp);
                    if (code == 200)
                    {
                        KerbalX.log("craft update OK");
                        update_message = "Craft Updated";
                    }
                    else if (code == 422)
                    {
                        KerbalX.log("craft update failed!");
                        KerbalX.log(resp);
                        string resp_errs = resp_data["errors"];
                        errors           = resp_errs.Split(',').ToList();
                    }
                    after_upload();
                });
            }
        }
        //Prepare craft data to upload as new craft on KerbalX
        private void upload_craft()
        {
            clear_errors();
            if (ok_to_send())
            {
                before_upload("Uploading....");

                WWWForm craft_data = new WWWForm();
                craft_data.AddField("craft_name", craft_name);
                craft_data.AddField("craft_style", craft_styles[selected_style_index]);
                craft_data.AddField("craft_file", craft_file());
                craft_data.AddField("part_data", JSONX.toJSON(part_info()));
                craft_data.AddField("action_groups", JSONX.toJSON(action_groups));
                craft_data.AddField("hash_tags", hash_tags);


                int pic_count = 0;
                int url_count = 0;
                foreach (PicData pic in pictures)
                {
                    if (pic.file != null)
                    {
                        craft_data.AddField("images[image_" + pic_count++ + "]", Convert.ToBase64String(read_as_jpg(pic)));
                    }
                    else
                    {
                        craft_data.AddField("image_urls[url_" + url_count++ + "]", pic.url);
                    }
                }

                KerbalX.api.upload_craft(craft_data, (resp, code) => {
                    var resp_data = JSON.Parse(resp);
                    if (code == 200)
                    {
                        KerbalX.log("craft uploaded OK");
                        show_upload_compelte_dialog(resp_data["url"]);
                        reset();
                        fetch_existing_craft();
                    }
                    else if (code == 422)
                    {
                        KerbalX.log("craft upload failed!");
                        KerbalX.log(resp);
                        string resp_errs = resp_data["errors"];
                        errors           = resp_errs.Split(',').ToList();
                    }
                    after_upload();
                });
            }
        }
        public static string toJSON(Dictionary <string, object> data, params object[] opts)
        {
            int  indent    = 0;
            bool do_indent = false;

            if (opts.Length == 1)
            {
                if (opts[0] is int)
                {
                    indent    = (int)opts[0];
                    do_indent = true;
                }
                else
                {
                    do_indent = (bool)opts[0];
                }
            }
            indent++;
            object arg;

            if (do_indent)
            {
                arg = (int)indent;
            }
            else
            {
                arg = (bool)do_indent;
            }  //yes it's a if else block on one line, deal with it, C# refused to let me use a ternary with mixed types, fussy lang.

            List <string> objects = new List <string>();

            foreach (KeyValuePair <string, object> entry in data)
            {
                if (entry.Value is Dictionary <string, object> )
                {
                    var t = (Dictionary <string, object>)data[entry.Key];
                    objects.Add(String.Format("\"{0}\":{1}", entry.Key, JSONX.toJSON(t, arg)));
                }
                else if (entry.Value is String)
                {
                    objects.Add(String.Format("\"{0}\":\"{1}\"", entry.Key, entry.Value));
                }
                else
                {
                    //mutha of asumptions: if it's not a string or dict then it's a numeric, cos seriously C#, you don't have a numeric class? You want me to test for each
                    //numerical type individualy? yeah....I'm to lazy for that...oh Ruby...I miss you so.
                    try{
                        objects.Add(String.Format("\"{0}\":{1}", entry.Key, entry.Value));
                    } catch {
                        objects.Add(String.Format("\"{0}\":\"{1}\"", entry.Key, entry.Value.ToString()));
                    }
                }
            }

            //Poor man's String.join becuase I couldn't get the line below to function in unity
            //string json_string = "{" + String.Join (",", objects) + "}";
            //also adds in spaces and new lines if do_indent is true.
            string json_string = "{";

            foreach (string obj in objects)
            {
                if (do_indent)
                {
                    json_string = json_string + "\n";
                    for (int i = 0; i < indent; i++)
                    {
                        json_string = json_string + "    ";
                    }
                }
                json_string = json_string + obj;
                if (obj != objects.Last())
                {
                    json_string = json_string + ",";
                }
            }
            if (do_indent)
            {
                json_string = json_string + "\n";
                for (int i = 0; i < indent - 1; i++)
                {
                    json_string = json_string + "    ";
                }
            }
            json_string = json_string + "}";

            return(json_string); //hey look, some JSON!
        }