Index: branches/LCSv2/controllers/default.py.bak
===================================================================
--- branches/LCSv2/controllers/default.py.bak	(revision 620)
+++ branches/LCSv2/controllers/default.py.bak	(revision 621)
@@ -19,5 +19,5 @@
 # List all the current records in the database - remove in final application
 def list():
-    highways = db().select(db.closures.ALL, orderby=db.closures.lognum)
+    highways = db().select(db.closures.ALL, orderby=db.closures.closureid)
     return dict(highways = highways)
 # Show details of a single record - remove in final application
@@ -110,5 +110,5 @@
     return dict(msg=msg, count=count, highways=hwy, table=header, multiform=multiform)
 
-# Show the status box next to each search result
+# Show a selected closure with a status update form
 def statuslist():
     closedItems = []
@@ -122,9 +122,36 @@
                 retrieved = db(db.closures.closureid == item).select().first()
                 closedItems.append(retrieved) 
-
+        # Build the table rows        
+        tblrows = TR()        
+        for row in closedItems:
+            # Construct the status radio buttons; disable if date already in database
+            statusflags = "disabled" if row.s1097date == "" else ""  
+            if row.s1097date == "":
+                btn = LABEL('1097'), INPUT(_type='radio', _name='statustype', _value='1097'+row.closureid)
+            else:
+                btn = LABEL('1097 ○',_class='labelgray'),
+            btngroup = btn
+            if row.s1098date == "":
+                btn = LABEL('1098'), INPUT(_type='radio', _name='statustype', _value='1098'+row.closureid)
+            else:
+                btn = LABEL('1098 ○',_class='labelgray'),
+            btngroup += btn
+            if row.s1022date == "":
+                btn = LABEL('1022'), INPUT(_type='radio', _name='statustype', _value='1022'+row.closureid)
+            else:
+                btn = LABEL('1022 ○',_class='labelgray'),
+            btngroup += btn
+
+            if row.s1097date != "" and row.s1098date != "" and row.s1022date != "":
+                btngroup += BR(),LABEL('Statuser:', _class='labelgray')
+            else:    
+                btngroup += BR(),LABEL('Statuser:'),INPUT(_name='statuser',_size='9'),BR(),BR(),INPUT(_type='submit',_value="submit status update",_class="submit-button")
+#            LABEL('1097 ○',_class='colorgray'), INPUT(_type='radio', _name='statustype', _value='1097'+row.closureid),LABEL('1098'), INPUT(_type='radio', _name='statustype', _value='1098'+row.closureid),LABEL('1022'), INPUT(_type='radio', _name='statustype', _value='1022'+row.closureid),BR(), LABEL('Statuser:'),INPUT(_name='statuser',_size='9')
+            tblrows += TR(TD(row.closureid,HR(),row.lognum),TD(row.route,' ',row.direction,HR(),row.closuretype),TD(row.startdate,HR(),row.enddate,HR(),row.estdelay),TD(row.supervisor,BR(),db(db.supervisors.name == row.supervisor).select().first().radiocallnum),TD(btngroup))
         form = FORM(BR(), 
                 TABLE(THEAD(TR(TH('Closure ID/',BR(),'Log No.'), TH('Route & Dir',BR(),'Type of Closure'),TH('Start Date/',BR(),'End Date/',BR(),'Est. Delay'),TH('Requestor/',BR(),'Radio Call No.'),TH('Status'))),
-                    [TR(TD(row.closureid,HR(),row.lognum),TD(row.route,' ',row.direction,HR(),row.closuretype),TD(row.startdate,HR(),row.enddate,HR(),row.estdelay),TD(row.supervisor,BR(),db(db.supervisors.name == row.supervisor).select().first().radiocallnum),TD(LABEL('1097'), INPUT(_type='checkbox', _name='statustype', _value='1097'+row.closureid),LABEL('1098'), INPUT(_type='checkbox', _name='statustype', _value='1098'+row.closureid),LABEL('1022'), INPUT(_type='checkbox', _name='statustype', _value='1022'+row.closureid),BR(), LABEL('Statuser:'),INPUT(_name='statuser',_size='9'))) for row in closedItems],
-                    _border='1', _cellpadding='5'),INPUT(_type='submit',_value="submit status form",_class="submit-button"),)
+                    tblrows,
+                     
+                    _border='1', _cellpadding='5', _width="70%"))
         
     else:
@@ -152,5 +179,5 @@
             now = datetime.datetime.today()
             fieldname = "s"+session.statustype[0:4]+"date"
-            db(db.closures.closureid == session.statustype[4:]).update(**{fieldname:now.strftime("%Y%m%d")})
+            db(db.closures.closureid == session.statustype[4:]).update(**{fieldname:now.strftime("%Y-%m-%d")})
             fieldname = "s"+session.statustype[0:4]+"time"
             db(db.closures.closureid == session.statustype[4:]).update(**{fieldname:now.strftime("%H%M")})
@@ -167,4 +194,5 @@
     else:
         return ""
+
 # Create a new record
 def submit():
@@ -188,5 +216,10 @@
         streets.append(row.street)
         streetlookup.append(row.route + ',' + row.street)
-
+    # Build the list of existing closures    
+    existingclosures = []
+    existingclosures.append("")
+    for row in db().select(db.closures.ALL, orderby=db.closures.closureid):
+        existingclosures.append(row.closureid)
+        
     form = FORM(
                 LABEL('*Route',_for='route'), SELECT(hwys,_name='route', _id='routecombo', _onchange='routechanged()', requires=IS_LENGTH(minsize=1,error_message='route cannot be empty')), XML('&nbsp;&nbsp;&nbsp;'), 
@@ -210,12 +243,22 @@
                  TR(TD(SELECT(closuretypes,_name='closuretype',requires=IS_LENGTH(minsize=1,error_message='type of closure cannot be empty'))), 
                     TD(SELECT(worktypes,_name='worktype', requires=IS_LENGTH(minsize=1,error_message='type of work cannot be empty'))), 
-                    TD(INPUT(_name='estdelay',_size='4',requires=IS_INT_IN_RANGE(1, 1440)),'minutes'), 
+                    TD(INPUT(_name='estdelay',_size='4'),'minutes'),  
                     TD(INPUT(_type='checkbox',_name='cozeep'),'CoZeep MaZeep/CHP',BR(), 
                        INPUT(_type='checkbox', _name='detour'),'Detour Available')), 
                  _width='100%' ),
     TABLE(TR(TD(LABEL('*Supervisor')),
-            TD(LABEL('Field Rep'))), 
-                  TR(TD(SELECT(supervisors,_name='supervisor', requires=IS_LENGTH(minsize=1,error_message='supervisor cannot be empty'))),
-                     TD(SELECT(crew,_name='fieldrep')))),
+            TD(LABEL('Field Rep'))
+            ), 
+          TR(TD(SELECT(supervisors,_name='supervisor', requires=IS_LENGTH(minsize=1,error_message='supervisor cannot be empty'))),
+             TD(SELECT(crew,_name='fieldrep')),
+             TD(XML("&nbsp;&nbsp;&nbsp;&nbsp;")),
+             TD('Is this an existing incident?',
+                INPUT(_type='radio',_name='existing',_value='No',_onclick='radioclicked()'),
+                'No',
+                INPUT(_type='radio',_name='existing',_value='Yes',value='Yes',_onclick='radioclicked()'),
+                'Yes'
+               )
+             ),
+          TR(TD(),TD(),TD(), TD('    Select closure ID:',SELECT(existingclosures,_name='existingid'),_id='closureselect'))),
     TABLE(TR(TD( LABEL('Meeting Place/CHP Contact')),
             TD(LABEL('Reason for Closure')),
@@ -225,14 +268,26 @@
             XML('\n'),SELECT(streetlookup,_name='stlookup', _id='stlookup', _class='hideme')) 
 
-    if form.process().accepted:
-        newID = calcNextClosure(form.vars.route)
+    if form.process(onvalidation=validate_existing_id).accepted:
+        newLognum = calcNextLogNum(form.vars.existingid)
+        if newLognum == '1':
+            newID = calcNextClosureID(form.vars.route)
+        else:
+            newID = form.vars.existingid
+        supervisor_name = form.vars.supervisor[3:]
+        fieldrep_name = form.vars.fieldrep[5:]
         # Insert the record into the database
-        newrec = db.closures.insert(closureid=newID, lognum='1', route=form.vars.route, direction=form.vars.direction, facility=form.vars.facility, startcounty=form.vars.startcounty, endcounty=form.vars.endcounty, startlocation=form.vars.startlocation, endlocation=form.vars.endlocation, startdate=form.vars.startdate, enddate=form.vars.enddate, starttime=form.vars.starttime+form.vars.starttimemin, endtime=form.vars.endtime+form.vars.endtimemin, closuretype=form.vars.closuretype, worktype=form.vars.worktype, estdelay=form.vars.estdelay, tmpcozeep=getCheckbox(form.vars.cozeep), tmpdetour=getCheckbox(form.vars.detour), supervisor=form.vars.supervisor, fieldrep=form.vars.fieldrep,  )
-        session.flash = 'New lane closure added: ' + newID
+        newrec = db.closures.insert(closureid=newID, lognum=newLognum, route=form.vars.route, direction=form.vars.direction, facility=form.vars.facility, startcounty=form.vars.startcounty, endcounty=form.vars.endcounty, startlocation=form.vars.startlocation, endlocation=form.vars.endlocation, startdate=form.vars.startdate, enddate=form.vars.enddate, starttime=form.vars.starttime+form.vars.starttimemin, endtime=form.vars.endtime+form.vars.endtimemin, closuretype=form.vars.closuretype, worktype=form.vars.worktype, estdelay=form.vars.estdelay, tmpcozeep=getCheckbox(form.vars.cozeep), tmpdetour=getCheckbox(form.vars.detour), supervisor=supervisor_name, fieldrep=fieldrep_name, s1097date='', s1098date='', s1022date='' )
+        session.flash = 'New lane closure added: ' + newID + ' ' + newLognum
         redirect(URL('search.html'))
     return dict(form=form)
 
+# Validation for existing closure button
+# If user selected Yes (it's existing incident) then they must select a closure ID
+def validate_existing_id(form):
+    if form.vars.existing == 'Yes' and form.vars.existingid == '':
+        form.errors.existingid = "Existing incidents require selecting an existing closure ID"
+
 # Calculate the closure id to assign to the new closure
-def calcNextClosure(routeNum):
+def calcNextClosureID(routeNum):
     # Retrieve any existing closures on this route
     item = db(db.closures.closureid.startswith('T'+routeNum)).select().last() # Might need to sort these
@@ -245,4 +300,15 @@
     else:
         return 'T'+routeNum+'AA'  # For a non-existing route
+    
+# If existing incident closure ID is provided, increment the log number by 1
+def calcNextLogNum(existingid):
+    if existingid != '':
+        item = db(db.closures.closureid.startswith(existingid)).select().last()
+        prevLog = int(item.lognum)
+        nextLog = prevLog + 1
+        return str(nextLog)
+    else:
+        return '1'
+
 
 # Convert checkbox value to YES/NO
