Index: trunk/src/scriptbuilder/structures/SimulationScript.java
===================================================================
--- trunk/src/scriptbuilder/structures/SimulationScript.java	(revision 24)
+++ trunk/src/scriptbuilder/structures/SimulationScript.java	(revision 30)
@@ -2,5 +2,9 @@
 
 import java.awt.Color;
+import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileWriter;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.util.ArrayList;
 import java.util.List;
@@ -25,5 +29,5 @@
  * @version 2017/06/22
  */
-public class SimulationScript extends Observable
+public class SimulationScript extends Observable implements I_XML_Writable
 {
 
@@ -45,4 +49,6 @@
     };
 
+    public String title = "";
+
     /**
      * The incidents displayed by the GUI.
@@ -93,5 +99,5 @@
         }
         /*
-         // Add some demo events
+         Add some demo events
          incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck",
          "Blueberry truck crashed on the freeway.", 8800, this));
@@ -109,10 +115,10 @@
          "Truck stalled on the freeway.", 2800, this));
          numberOfIncidents++;
-         //incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill",
-         //        "Tomato trucked spilt tomatos all over the freeway.", 3200, this));
-         //incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection",
-         //        "Crash at the intersection of Tree Road and Red Road.", 4300, this));
-         //incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat",
-         //        "Bomb threat and Road X.", 6000, this));
+         incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill",
+         "Tomato trucked spilt tomatos all over the freeway.", 3200, this));
+         incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection",
+         "Crash at the intersection of Tree Road and Red Road.", 4300, this));
+         incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat",
+         "Bomb threat and Road X.", 6000, this));
          incidents.add(null);
          incidents.add(null);
@@ -146,7 +152,7 @@
          incidents.get(4).setOffset(4400);
          incidents.get(5).setOffset(1400);
-         //incidents.get(6).setOffset(7600);
-         //incidents.get(7).setOffset(2400);
-         //incidents.get(8).setOffset(4800); 
+         incidents.get(6).setOffset(7600);
+         incidents.get(7).setOffset(2400);
+         incidents.get(8).setOffset(4800); 
          }
          */
@@ -217,3 +223,103 @@
         this.update();
     }
+
+    public void saveScriptToFile(File f)
+    {
+        try
+        {
+            f.createNewFile();
+
+            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
+            bw.write(this.toXML());
+            bw.flush();
+            bw.close();
+
+        }
+        catch (Exception ex)
+        {
+            System.out.println("ERROR SAVING SCRIPT");
+            ex.printStackTrace();
+        }
+    }
+
+    @Override
+    public String toXML()
+    {
+        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
+        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">";
+        ArrayList<TimeSlice> slices = allSlices();
+        output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\"");
+        for (TimeSlice slice : slices)
+        {
+            output += slice.toXML();
+        }
+        output += closeTag(ELEMENT.TMC_SCRIPT.tag);
+        return output;
+    }
+
+    @Override
+    public String openTag(String s)
+    {
+        return "<" + s + ">\n";
+    }
+
+    @Override
+    public String closeTag(String s)
+    {
+        return "</" + s + ">\n";
+    }
+
+    @Override
+    public String emptyTag(String s)
+    {
+        return "<" + s + "/>\n";
+    }
+
+    /**
+     * Arranges all timeslices in this script in chronological order, then by
+     * incident number.
+     *
+     * @return a list of all timeslices in the simulation script
+     */
+    public ArrayList<TimeSlice> allSlices()
+    {
+        ArrayList<TimeSlice> list = new ArrayList<TimeSlice>();
+        int length = absoluteLength();
+
+        for (int i = 0; i < length; i++)
+        {
+            for (ScriptIncident inc : incidents)
+            {
+                
+                if (inc != null && inc.slices.get(i) != null)
+                {
+                    list.add(inc.slices.get(i));
+                }
+            }
+        }
+        return list;
+    }
+
+    /**
+     * Gets the total length of the simulation in seconds.
+     *
+     * @return
+     */
+    public int absoluteLength()
+    {
+        int length = 0;
+        for (ScriptIncident inc : incidents)
+        {
+            if (inc != null)
+            {
+                int currentLength = inc.length + inc.offset;
+                if (currentLength > length)
+                {
+                    length = currentLength;
+                }
+            }
+        }
+        return length;
+    }
+
 }
Index: trunk/src/scriptbuilder/structures/TimeSlice.java
===================================================================
--- trunk/src/scriptbuilder/structures/TimeSlice.java	(revision 21)
+++ trunk/src/scriptbuilder/structures/TimeSlice.java	(revision 30)
@@ -8,4 +8,5 @@
 import java.util.TimeZone;
 import scriptbuilder.gui.ScriptBuilderGuiConstants;
+import scriptbuilder.structures.events.*;
 import scriptbuilder.structures.events.I_ScriptEvent;
 
@@ -194,6 +195,6 @@
 
     /**
-     * Converts the contents of this timeslice to a correctly
-     * formatted <ScriptEvent> XML element.
+     * Converts the contents of this timeslice to a correctly formatted
+     * <ScriptEvent> XML element.
      *
      * @return XML conversion of this timeslice.
@@ -202,4 +203,11 @@
     public String toXML()
     {
+        ArrayList<I_ScriptEvent> eventsCopy = new ArrayList<I_ScriptEvent>();
+
+        for (I_ScriptEvent e : events)
+        {
+            eventsCopy.add(e);
+        }
+
         System.out.println("Seconds:: " + seconds);
         String output = openTag(ELEMENT.SCRIPT_EVENT.tag);
@@ -212,5 +220,22 @@
         output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag);
 
-        for (I_ScriptEvent ev : events)
+        output += openTag(ELEMENT.CAD_DATA.tag);
+        if (containsCADIncidentEvent())
+        {
+            output += openTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
+            for (I_ScriptEvent ev : eventsCopy)
+            {
+                if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev))
+                {
+                    I_XML_Writable ex = (I_XML_Writable) ev;
+                    output += ex.toXML();
+                    eventsCopy.remove(ev);
+                }
+            }
+            output += closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
+        }
+        output += closeTag(ELEMENT.CAD_DATA.tag);
+
+        for (I_ScriptEvent ev : eventsCopy)
         {
             if (ev instanceof I_XML_Writable)
@@ -242,3 +267,22 @@
         return "<" + s + "/>\n";
     }
+
+    private boolean containsCADIncidentEvent()
+    {
+        for (I_ScriptEvent ev : events)
+        {
+            if (isCADIncidentEvent(ev))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean isCADIncidentEvent(I_ScriptEvent ev)
+    {
+        return ev instanceof AudioEvent || ev instanceof UnitEvent
+                || ev instanceof ParamicsEvent || ev instanceof TowEvent
+                || ev instanceof WitnessEvent;
+    }
 }
Index: trunk/src/scriptbuilder/structures/ScriptIncident.java
===================================================================
--- trunk/src/scriptbuilder/structures/ScriptIncident.java	(revision 24)
+++ trunk/src/scriptbuilder/structures/ScriptIncident.java	(revision 30)
@@ -19,5 +19,5 @@
  * @version 2017/06/29
  */
-public class ScriptIncident
+public class ScriptIncident implements I_XML_Writable
 {
 
@@ -212,4 +212,40 @@
         }
         return arr;
+    }
+
+    @Override
+    public String toXML()
+    {
+        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
+        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">";
+        output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\"");
+        
+        ArrayList<TimeSlice> s = (ArrayList) slices.values();
+        
+        for (TimeSlice slice : s)
+        {
+            output += slice.toXML();
+        }
+        output += closeTag(ELEMENT.TMC_SCRIPT.tag);
+        return output;
+    }
+
+
+    @Override
+    public String openTag(String s)
+    {
+        return "<" + s + ">\n";
+    }
+
+    @Override
+    public String closeTag(String s)
+    {
+        return "</" + s + ">\n";
+    }
+
+    @Override
+    public String emptyTag(String s)
+    {
+        return "<" + s + "/>\n";
     }
 
