var cityString='    ';
var countyString='    ';
var whereToSuggest;
var suggestDiv='';
var leadSelect='';
var wEvents= new Object();
var sourceSelected=false;
var tacontacts;
var eventStack = new Object();
var eventList;
var death = 4;
var injury = 8;
var lightning=1;
var funnel=2;
var names=[];
var rows=new Object();
var eventImage = new Object();
eventImage.T = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/red.png\" name=\"red\" alt=\"red blob\" align=\"bottom\">";
eventImage.W = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/pur1.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.P = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/gry.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.F = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/grn.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.H = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/blu.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.U = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/ugrn.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.L = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/yel.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.G = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/grn.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";
eventImage.FU = "<img src=\"images/blk.png\" align=\"bottom\"><img src=\"images/fun.png\" name=\"grn\" alt=\"Green blob\" align=\"bottom\">";

dojo.addOnLoad(
    function() {
    var a = new Object;
    a['loadlocations']=1;
    a['loadcontacts']=1;
    a['loadevents']=1;
    console.debug("cookie is "+dojo.cookie("skywarn.cookie"));
    if(typeof dojo.cookie("skywarn.cookie") =='undefined' ) {
        console.debug("no cookie");
    }
    var x = dijit.byId('eventtype');
    dojo.connect(x, 'onChange', doEvent); 
    dojo.connect(x.domNode, 'onclick', doDeselect); 
    var x = dijit.byId('eventtime');
    dojo.connect(x, 'onChange', checkTime); 

    var j_a = dojo.toJson(a);
    doGet("load.php",j_a);

    var all = dijit.byId('all');
//    console.dir(all);
//    console.dir(all.containerNode.id);
    all.containerNode.id.value="-1";
    }
); // and/or pass an anonymous function

function doGet(script,j_param)
{
    console.log(j_param);
    dojo.xhrGet({
            url: script,
            content: {
                'applicationdata': j_param
            },
            handleAs: "json-comment-filtered",
            load: function(responseObject, args) {
                clearForm();
                var cities = responseObject['cities'];
                for(var c in cities) {
                    cityString += "| "+cities[c].toLowerCase();
                    cityString += ";"+cities[c];
                }
                var counties = responseObject['counties'];
                for(var c in counties) {
                    countyString += "| "+counties[c].toLowerCase();
                    countyString += ";"+counties[c];
                }
                var contacts = responseObject['contacts'];
                if(typeof contacts == 'undefined') {
                    var a = new Object();
                    var b = [];
                    a.call='none';
                    a.name='none';
                    b[0]=a;
                    tacontacts = new TAFFY(b);
                } else {
                    tacontacts = new TAFFY(responseObject.contacts);
                }
                // var mycall='K4GVO';
                // var cl = tacontacts.get({call:{equal:myCall}});
                var events = responseObject['events'];
                if(typeof events != 'undefined') {
                    taevents = new TAFFY(responseObject.events);
                }
    
                if(typeof dojo.cookie("skywarn.cookie") =='undefined' ) {
                    console.debug("no cookie");
                    var dia = dijit.byId("operatorlogin");
                    dia.show();
                } else {
                    var operator=dojo.cookie("skywarn.cookie");
                    var cl = tacontacts.get({call:{equal:operator}});
                    if(typeof cl != "undefined")
                    {
                        var a = dojo.byId("Hi");
                        if(typeof cl[0] != 'undefined')
                            a.innerHTML="Hi "+cl[0].name;
                    }
                }
                eventList=taevents.get();
                eventList=eventList.sort(sortRecordDateDescendingHandler);
                console.log("Building table");
                buildTable();
            },
            error: function(error, args) {
                console.log("error "+error);
            }
        }); // end of xhrGet
}

function doDeselect(n)
{
//    console.dir(n);
}

function doEvent(n)
{
    //var x = n.getValue();
//    console.dir(n);
    x=dijit.byId('eventtype').getValue();
    switch (x) {
        case "H":
            var dia = dijit.byId("hail");
            dia.show();
            console.log("Showing hail");
            break;
        case "O":
            var dia = dijit.byId("otherevent");
            dia.show();
            console.log("Showing other");
            break;
        case "F":
            var dia = dijit.byId("flooding");
            dia.show();
            console.log("Showing flood");
            break;
        case "W":
            var dia = dijit.byId("wind");
            dia.show();
            console.log("Showing wind");
            break;
        case "T":
            wEvents['T'] ='T';
//            var dia = dijit.byId("tornado");
//            dia.show();
        break;
    }
}

function doSearch(val,loc)
{
    //console.log("doSearch called with "+loc);
    names=[];
    val=val.toLowerCase();
    var x = "| "+val+"";

    var k=0;
    var f;
    whereToSuggest=loc;
    //console.log("Setting "+loc);
    if(loc.search(/_/) != -1) {
        leadSelect=loc.substr(0,2);
    } else {
        leadSelect='';
    }
    if(loc.search(/county/)>=0) {
        f=countyString.indexOf(x); 
    //    console.log("county Searchin for "+x+" "+f);
    } else {
        f=cityString.indexOf(x); 
    //    console.log("city Searchin for "+x+" "+f);
    }
    var suggest=dojo.byId(loc);
    var newStart=0;
    while(f>0) 
    {
        if(f==-1) break;
        var fields;
        if(loc.search(/county/)>=0) 
           fields = countyString.substr(f,80).split("|",3);
        else
           fields = cityString.substr(f,80).split("|",3);
        var strings = fields[1].split(";");
        var n=fields[1].length;
        names.push(strings[1]);
        if(k++ >3) break;
        if(loc.search(/county/)>=0) 
            f=countyString.indexOf(x,f+n); 
        else
            f=cityString.indexOf(x,f+n); 
    }
    var astr=[];
    var ele = htmlFormat(names);
    suggest.style.display='inline';
    suggest.innerHTML=ele;
}

function doClear(node) {
    suggestDiv=dojo.byId(whereToSuggest);
    if(typeof suggestDiv != "undefined") {
        // give it time to set the data
     //   console.log("Waiting to clear "+suggestDiv.id);
     console.log("Name length"+names.length);
        if(names.length ==1) {
            addName(names[0]);
        }
        window.setTimeout("laterDoClear()", 300); 
    }
}

function laterDoClear(node)
{
    if(typeof suggestDiv != "undefined") {
        suggestDiv.innerHTML='';
    //    console.log("clearing "+suggestDiv.id);
        suggestDiv.style.display='none';
        suggestDiv=undefined;
    }
}

function getData(node)
{
    var id = node.id;
    var name=id.substr(2);
    addName(name);
}

function addName(name) {
    var suggest=dojo.byId(whereToSuggest);
    suggest.innerHTML='';
    //console.log('w '+whereToSuggest);
    if(whereToSuggest.search(/county/)>=0) {
        var place=dijit.byId(leadSelect+"county");
//        console.log("Setting to "+name);
        place.setValue(name);
    } else {
        //console.log("Using <"+leadSelect+"city>");
        var place=dijit.byId(leadSelect+"city");
        place.setValue(name);
    }
}

function addWindEvent(t)
{
    var dia = dijit.byId("wind");
    dia.hide();
    var val = dijit.byId('windForm').getValues();
    var xval = dijit.byId('wid');
    xval.setValue("1");
    wEvents['W'] ='W';
}

function addHailEvent(t)
{
    var dia = dijit.byId("hail");
    dia.hide();
    var hval = dijit.byId('hid');
    hval.setValue("1");
    wEvents['H'] ='H';
}

function cancel()
{
    var stay=confirm("Do you really want to quit and lose all your data? Please press 'ok' to quit, 'cancel' to continue editing and/or save your data!")
    dojo.cookie("skywarn.cookie","Value Doesn't matter",{expires: -1});
    if(stay)
        clearForm();
}

function saveData(what) 
{
    var all = dijit.byId(what);
    var vals = all.getValues();
    //console.log("event "+vals['eventtime'].length);
    if(!isNumeric(vals['eventtime'])) {
        alert("Event Time must be a valid time of the form NNNN without punctuation");
        return;
    }
    if(!objLength(wEvents)) {
        alert("Please select at least one event");
        return;
    }
    vals['report_date']=makeDate(vals['eventtime']);
    delete vals['eventtime'];
    var hev ='';
    for(var h in wEvents) {
        hev+=h;
    }
    vals['eventtype']=hev;
    if(typeof wEvents['T'] != 'undefined' ) {
        vals['tornado']="1";
    }

    var met = vals['met_advised'];
    if(met.length) { 
        vals['met_advised']=1;
    } else  {
        vals['met_advised']=0;
    }

    var source = dijit.byId('sourceotherForm');
    var sourceVals = source.getValues();
    if(sourceVals['source'] == 'undefined') {
    }
    vals['source_other']=sourceVals['source_other'];

    var a = new Object();
    vals['id']="-1";
    a['eventrecord']=vals;
//    console.dir(windVals);
    if(dijit.byId('wid').getValue() == '1') {
        var wind = dijit.byId('wind');
        var windVals = wind.getValues();
        console.log('Windy '+windVals['power_lines']);
//        console.dir(windVals['power_lines']);
//        console.dir(windVals['structures']);
        if(windVals.power_lines=='yes') windVals['power_lines']=1;
        else windVals.power_lines=0;
        if(windVals.structures=='yes') windVals['structures']=1;
        else windVals.structures=0;
        delete windVals['windForm'];
        delete windVals['id'];
        delete windVals['wid'];
        a['windevent']=windVals;
    }
    if(dijit.byId('hid').getValue() == '1') {
        var hail = dijit.byId('hail');
        var hailVals = hail.getValues();
//        console.dir(hailVals);
        delete hailVals['hailForm'];
        delete hailVals['id'];
        delete hailVals['hid'];
        a['hailevent']=hailVals;
    }
    if(dijit.byId('fid').getValue() == '1') {
        var typ=0;
        var f = dijit.byId("flood_type");
        var f1=f.getSelected();
        for(var k in f1) {
            if(typeof f1[k].value != 'undefined') {
                if(f1[k].value =='U') {
                    typ |= 2;
                } else if(f1[k].value =='F') {
                    typ |= 1;
                }
            }
        }
        var fv = new Object();
        fv['flood_type']=typ;

        a['floodevent']=fv;
        f.reset();
    }
    if(dijit.byId('oid').getValue() == '1') {
        var typ=0;
        var f = dijit.byId("other_type");
        var f1=f.getSelected();
        for(var k in f1) {
            if(typeof f1[k].value != 'undefined') {
                switch(f1[k].value) {
                    case 'L':
                        typ |=1;
                        break;
                    case 'F':
                        typ |=2;
                        break;
                    case 'D':
                        typ |=4;
                        break;
                    case 'I':
                        typ |=8;
                        break;
                }
            }
        }
        var fv = new Object();
        fv['other_type']=typ;

        a['otherevent']=fv;
        f.reset();
    }
    eventStack['loadevents']=1;
    eventStack['storedata']=a;
    var j_a = dojo.toJson(eventStack);
    doGet("load.php",j_a);
    eventStack = new Object();
    clearForm();
}

function doCallSearch()
{
    var cid = dijit.byId('l_call');
    var myCall=cid.getValue();
    var cl = tacontacts.get({call:{equal:myCall}});
    if(typeof cl !='undefined') {
        if(typeof cl[0] != 'undefined') {
            var name = cl[0].name;
            var nameNode=dijit.byId("name");
            nameNode.setValue(name);
            var city = cl[0].city;
            var cityNode=dijit.byId("l_city");
            cityNode.setValue(city);
            var county = cl[0].county;
            var countyNode=dijit.byId("l_county");
            countyNode.setValue(county);
        }
    }
}

function doSourceCheck(tag)
{
    var x = tag.getValue();
    if(x == 'O') {
            var dia = dijit.byId("sourceother");
            dia.show();
    }
}

function addOtherSource(tag)
{
    var dia = dijit.byId("sourceother");
    var v = dijit.byId("source_other"); // text box from pop up
    var val = v.getValue();
    var os = dojo.byId("othersource");
    os.innerHTML=val;
    dia.hide();
}

function addFlooding(tag)
{
    var dia = dijit.byId(tag);
    dia.hide();
    var fval = dijit.byId('fid');
    fval.setValue("1");
    wEvents['F'] ='F';
}

function addOther(tag)
{
    var dia = dijit.byId(tag);
    dia.hide();
    var fval = dijit.byId('oid');
    fval.setValue("1");
    wEvents['O'] ='O';
}

function clearForm()
{
    wEvents=new Object();
    sourceSelected=false;
    var flood_type = dijit.byId('flood_type');
    flood_type.reset();
    var other_type = dijit.byId('other_type');
    other_type.reset();
    var size = dijit.byId('size');
    size.reset();
    var trees = dijit.byId('trees');
    trees.reset();
    var pl = dijit.byId('power_lines');
    pl.reset();
    var str = dijit.byId('structures');
    str.reset();
    var met = dijit.byId('met_advised');
    met.reset();
    var nar = dijit.byId('narrative');
    nar.reset();
    var et = dijit.byId('eventtype');
    et.reset();
    var sou = dijit.byId('source');
    sou.reset();
    var souoth = dijit.byId('source_other');
    souoth.reset();
    var otherSize = dijit.byId('other_size');
    otherSize.reset();
    var windSpeed = dijit.byId('wind_speed');
    windSpeed.reset();
    var xval = dijit.byId('wid');
    xval.setValue("0");
    var hval = dijit.byId('hid');
    hval.setValue("0");
    var fval = dijit.byId('fid');
    fval.setValue("0");
    var oval = dijit.byId('oid');
    oval.setValue("0");
    var inp = document.getElementsByTagName('input');
    var clr=0;
    clearForm1('all');
    /*
    for(var i = 0; i < inp.length; i++) {
        if( dojo.byId(inp[i].id) != null) {
            //DumperAlert(dojo.byId(inp[i].id));
            //            console.dir(dojo.byId(inp[i].id));

           // alert(dojo.byId(inp[i].id));
            if( dojo.byId(inp[i].id).type=='text'){
                dojo.byId(inp[i].id).value='';
                clr++;
            }
        }
    }
    */

}

function htmlFormat(arr) {
    // formats arr as an HTML table
    var output = '';
    for (var i=0;i<arr.length;++i) {
        output += '<div id="n_'+arr[i]+'" style="background-color: white;" class="countySuggestList"';
        output = output + ' onMouseover="this.style.backgroundColor=\'lightblue\';" onMouseout="this.style.backgroundColor=\'white\';" onclick="getData(this)">' + '' + '';
        //output = output + ' onmouseover="this.style.backgroundColor=0x000000" onmouseout="this.style.backgroundColor=0xffffff;" onclick="getData(this)">' + '' + '';
        output = output + ' ' + arr[i] + '</div> ';
    }
    return output;
}

function isNumeric(sText)
{
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;

    if(parseInt(sText) == -1) {
        return true;
    }
    if(isNaN(sText) ) 
        return false;

    if(sText.length ==0) return false;
    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function zeroPad4(str)
{
    str +="";
    switch (str.length)
    {
        case 0:
            return "0000";
            break;
        case 1:
            return "000"+str;
            break;
        case 2:
            return "00"+str;
            break;
        case 3:
            return "0"+str;
            break;
    }
    return str;
}
function zeroPad2(str)
{
    str +="";
    switch (str.length)
    {
        case 0:
            return "00";
            break;
        case 1:
            return "0"+str;
            break;
    }
    return str;
}

function trimNumber(s) {
  while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
  return s;
}

function makeDate(time)
{
    if(time == -1) {
        var then = new Date();
        then.setTime(0);
        return parseInt(then.getTime()/1000);
    }
    time=zeroPad4(time);
    var hr = parseInt(trimNumber(time.substr(0,2)));
    var mn = parseInt(trimNumber(time.substr(2,2)));
    var then = new Date();
    var now = new Date();
    then.setHours(hr);
    then.setMinutes(mn);
    then.setSeconds(0,0);
    if(then.getTime()>now.getTime()) {
        var jt = then.getTime()-24*60*60*1000;
        then.setTime(jt);
    }
    return parseInt(then.getTime()/1000);
}

function login(tag)
{
    var formData=dijit.byId(tag);
//    console.dir(formData.getValues());
    var vals = formData.getValues();
    var data=new Object();
    data.call=vals.l_contact_call;
    data.city=vals.l_city;
    data.county=vals.l_county;
    data.name=vals.l_contact_name;
    var dia = dijit.byId("operatorlogin");
    dia.hide();
    dojo.cookie("skywarn.cookie",vals['l_contact_call']);
    var a = dojo.byId("Hi");
    a.innerHTML="Hi "+vals['l_contact_name'];
    eventStack['contact']=data;
    eventStack['loadcontacts']=1;
    var j_a = dojo.toJson(eventStack);
    doGet("load.php",j_a);
    eventStack = new Object();
}

function displayLast()
{
    doEvent('');
}

function objLength(obj)
{
    var n=0;
    for(var i in obj) {
        ++n;
    }
    return n;
}

function sortReportDateAscendingHandler(thisObject,thatObject) {	
    if (thisObject.unixt > thatObject.unixt)
    {
        return 1;
    }
    else if (thisObject.unixt < thatObject.unixt)
    {
        return -1;
    }
    return 0;
}

function sortReportDateDescendingHandler(thisObject,thatObject) {	
    if (thisObject.unixt < thatObject.unixt)
    {
        return 1;
    }
    else if (thisObject.unixt > thatObject.unixt)
    {
        return -1;
    }
    return 0;
}

function sortRecordDateDescendingHandler(thisObject,thatObject) {	
    if (thisObject.record_date < thatObject.record_date)
    {
        return 1;
    }
    else if (thisObject.record_date > thatObject.record_date)
    {
        return -1;
    }
    return 0;
}

function buildTable()
{
    var tbody = document.getElementById("grid").getElementsByTagName("tbody")[0];
    var otable=document.getElementById("grid"); 
    var cnt=0;
    while(otable.rows.length>1) {
        otable.deleteRow(otable.rows.length-1); 
        cnt++;
    }

    var rownum=0;
    for(var k in eventList) {
        var wxEvent=eventList[k];
        var uEntryDate=wxEvent['unixt'];
        var strTime="Unk";
        if(!parseInt(uEntryDate)==0) {
            //        if(uEntryDate ==0) continue;
            var eDate = new Date();
            eDate.setTime(uEntryDate*1000);
            h = zeroPad2(eDate.getHours());
            m = zeroPad2(eDate.getMinutes());

            strTime=h+""+m;
        }
        var row = document.createElement("TR");
        //row.class = "alt2";
        var cell12 = document.createElement("TD");
        cell12.appendChild(document.createTextNode(strTime));
        row.appendChild(cell12);
        //        console.log(h+" "+strTime);
        cell13 = document.createElement("TD");
        cell13.appendChild(document.createTextNode(''));
        var img;
        img="";
        var info='';
        if(parseInt(wxEvent.other_type) & lightning) {
            img += eventImage.L;
            info += " Lightning;";
        }
        if(parseInt(wxEvent.other_type ) & funnel) {
            img += eventImage.FU;
            info += " Funnel Cloud;";
        }
        if(parseInt(wxEvent.tornado)>0 || wxEvent.other_type & (death|injury)) {
            img += eventImage.T;
            if(parseInt(wxEvent.tornado)>0 ) {
                info +="Tornado ";
            }
            if(wxEvent.other_type & death) {
                info +="Death(s) ";
            }

            if( wxEvent.other_type & injury) {
                info +="Injuries ";
            }
            info+="; ";

        }
        if( wxEvent['size'] != 'P' && wxEvent['size'].length>0 )  {
            img += eventImage.H;
        }
        if( wxEvent['size'].length>0) {
            info +="Hail size:";
            switch (wxEvent['size']) {
            case 'H':
                info +="Half Dollar";
                break;
            case 'P':
                info +="Pea";
                break;
            case 'E':
                info +="Penny";
                break;
            case 'D':
                info +="Dime";
                break;
            case 'N':
                info +="Nickle";
                break;
            case 'Q':
                info +="Quarter";
                break;
            case 'G':
                info +="Golf Ball";
                break;
            case 'O':
                info +=wxEvent['other_size'];
                break;
            }
            info+='; ';

        } 
        if(wxEvent['size'] == 'P')  {
            img += eventImage.P;
        }
        if( wxEvent['structures'] || wxEvent['trees'] || wxEvent['power_lines']) {
            img += eventImage.W;
            if(parseInt( wxEvent['structures'] ) >0) {
                info += 'Structures';
            }
            if(parseInt( wxEvent['power_lines'] ) >0) {
                info += 'Power Lines ';
            }
            wxEvent['trees'] = trim( wxEvent['trees']);
            if(wxEvent['trees'].length>0) {
                console.log(wxEvent['trees']);
                info += 'Trees: ';
                switch (wxEvent['trees'])
                {
                case 'C': 
                    info += "Couple";
                    break;
                case 'F': 
                    info += "Few";
                    break;
                case 'S': 
                    info += "Several";
                    break;
                case 'M': 
                    info += "Many";
                    break;
                case 'N': 
                    info += "Numerous";
                    break;
                case 'W': 
                    info += "Widespread";
                    break;
                }
            }
            info += "; ";
        }
        if( parseInt(wxEvent['flood_type']) > 0 )  {
            info += "Flood type:";
            if( parseInt(wxEvent['flood_type']) & 1 )  {
                img += eventImage.F;
                info += "Flash";
            }
            if( parseInt(wxEvent['flood_type']) & 2 )  {
                img += eventImage.U;
                info += " Urban & Street";
            }
            info += "; ";
        }
        var link = "<a href=\"#\" id=\"x"+k+"\">"+img+"</a>";
        cell13.innerHTML=link;
        rows["x"+k]=1;
        row.appendChild(cell13);

        cell13 = document.createElement("TD");
        cell13.appendChild(document.createTextNode(''));
        cell13.innerHTML=info;
        row.appendChild(cell13);

        cell13 = document.createElement("TD");
        cell13.appendChild(document.createTextNode(''));
        if(parseInt( wxEvent['met_advised'])==0)
        {
            info="N";
        } else{
            info="Y";
        }
        if(info.length==0)
            info="&nbsp;";
        cell13.innerHTML=info;
        row.appendChild(cell13);
        tbody.appendChild(row); 

        cell13 = document.createElement("TD");
        cell13.appendChild(document.createTextNode(''));
        var test=wxEvent['city']+"/"+wxEvent['county'];
        cell13.innerHTML=wxEvent['city']+"/"+wxEvent['county'];
        if(test.length ==1) {
            cell13.innerHTML="Missing";
        }
        row.appendChild(cell13);
        info='';
        if(wxEvent['source'].length>0) {
            switch(wxEvent['source']) {
            case 'S':
                info += "Self";
                break;
            case 'T':
                info += "To PubSrv";
                break;
            case 'B':
                info += "By PubSrv";
                break;
            case 'L':
                info += "Local News";
                break;
            case 'O':
                info += wxEvent['source_other'];
                break;
            default:
                break;

            }
        }
        if(info.length==0)
                info += "Missing";
        cell13 = document.createElement("TD");
        cell13.appendChild(document.createTextNode(''));
        cell13.innerHTML=info;
        row.appendChild(cell13);

        cell13 = document.createElement("TD");
        cell13.appendChild(document.createTextNode(''));
        info = wxEvent['contact_call'];
        if(info.length==0)
            info="&nbsp;";
        cell13.innerHTML=info;
        row.appendChild(cell13);
        tbody.appendChild(row); 
        // new row
        row = document.createElement("TR");
        cell13 = document.createElement("TD");
        cell13.colSpan='7';
        cell13.appendChild(document.createTextNode(''));
        info = wxEvent['narrative'];
        if(info.length==0)
            info="&nbsp;";
        cell13.innerHTML=info;
        row.appendChild(cell13);


        tbody.appendChild(row); 
        rownum++;
    }
    post();
}
function dumpx(x)
{
    var n = parseInt(this.id.substr(1));
    DumperAlert(eventList[n]);return false;
    
}

function post()
{
    for(var i in rows) {
        var x = dojo.byId(i);
        dojo.connect(x, 'onclick',dumpx); 
    }
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

function clearForm1(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
		}
	}

	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
		}
	}
}

function checkTime()
{
    var time =this.getValue();
    time = zeroPad4(time);
    var m = time.substr(2,2);
    var h = time.substr(0,2);
    if(h.substr(0,1) == '0') 
        h=h.substr(1,1);
    if(m.substr(0,1) == '0') 
        m=m.substr(1,1);
    if(parseInt(m)>59 || parseInt(h)>23) {
//        alert("Time format error");
        this.setValue("-1");
    }
}
	
