var mainForm = {
    fieldList : ["housingNotes", "monitorService", "workshopOffer",
                 "instrumentOffer", "workshopRequest", "dishwashing",
                 "travelingMonitor", 
                 "arrival3_6", "arrivalForDinner", "arrivalAfterDinner", "arrivalOther",
                 "otherArrivalTime",
                 "departBeforeDinner", "offerRide", "needRide",
                 "otherNotes"],
   
    initialize : function () {
        $("addCamper").onclick = application.addCamper;
        $("mainCancel").onclick = application.cancel;
        $("mainFinish").onclick = mainForm.validateAndSaveForm;
        $("confirmationCancel").onclick = application.cancel;
        $("confirmationBack").onclick = application.confirmationBack;
        $("confirmationForm").onsubmit = application.submit;
    },
    
    load : function() {
        var i = 0;
        var fieldName = "";
        var radioNodes = [];
        var node;
        var done = false;
        
        mainForm.writeCamperTable();
        for (i = 0; i < mainForm.fieldList.length; i++) {
            fieldName = mainForm.fieldList[i];
            Form.Element.setValue(fieldName, applicationData[fieldName]);
        }
        
        radioNodes = $$("input[name='arrival']");
        for (i = 0; i < radioNodes.length && !done; i++) {
            node = radioNodes[i];
            if (node.value === applicationData["arrival"]) {
                node.checked = true;
                done = true;
            }
        }
        $("otherArrivalTime").disabled = !applicationData.arrivalOther;
    },
    
    writeCamperTable : function() {
        var row = "";
        var cell = "";
        var tbody = "";
        var camper = null;
        var isContact = false;
        var addressLine = "";
        var fee = 0;
        var deposit = 0;
        var feeTotal = 0;
        var depositTotal = 0;
      
        application.deleteChildren("camperEntries");
      
        for (var i = 0; i < applicationData.campers.length; i++) {
            isContact = 0 === i;
            camper = applicationData.campers[i];
            fee = application.calculateFee(camper.age, camper.member);
            deposit = application.calculateDeposit(camper.age, camper.member);
            feeTotal += fee;
            depositTotal += deposit;

            row = "<tr class='camperRow'>";

            cell = "<td class='camperName'>" +
            camper.firstName + " " + camper.lastName;
            if (isContact) {
                cell += " <b>(Contact)</b>";
            }
            if (isContact || application.camperHasContactInfo(camper)) {
                if (0 !== camper.street.length) {
                    cell += "<br />" + camper.street;
                }
                if (0 !== camper.city.length) {
                    addressLine += "<br />" + camper.city;
                }
                if (0 !== camper.state.length) {
                    if (!addressLine) {
                        addressLine += "<br />";
                    }
                    else {
                        addressLine += ", ";
                    }
                    addressLine += camper.state;
                }
                if (0 !== camper.zip) {
                    if (!addressLine) {
                        addressLine += "<br />";
                    }
                    else {
                        addressLine += " ";
                    }
                    addressLine += camper.zip;
                }
                if (addressLine) {
                    cell += addressLine;
                }
                if (0 !== camper.phone) {
                    cell += "<br />" + camper.phone;
                }
                if (0 !== camper.email) {
                    cell += "<br />" + camper.email;
                }
            }
            cell += "</td>";
            row += cell;

            cell = "<td>";
            switch(parseInt(camper.age, 10)) {
                case 0:  cell += "< 1";   break;
                case 18: cell += "Adult"; break;
                default: cell += camper.age.toString();
            }
            cell += "</td>"; 
            row += cell;
            row += "<td>" + camper.gender + "</td>";
            row += "<td>" + camper.food + "</td>";
            row += "<td>" + (camper.member ? "Yes" : "&nbsp;") + "</td>";
            row += "<td class='camperFee'>$" + fee + "</td>";
            row += "<td class='camperFee'>$" + deposit + "</td>";
            cell = "<td class='camperLinks'><a href='#' onclick='application.editCamper(" + i + ")'>Edit</a>";
            if (!isContact) {
                cell += " <a href='#' onclick='application.deleteCamper(" + i + ");'>Delete</a>";
            }
            cell += "</td>";
            row += cell + "</tr>";
            tbody += row;
        }
        row = "<tr class='camperFeeTotals'><td colspan='5'>Total</td>";
        row += "<td id='feeTotal'>$" + feeTotal + "</td>";
        row += "<td id='depositTotal'>$" + depositTotal + "</td>";
        row += "<td>&nbsp;</td></tr>";
        tbody += row;
        $("camperEntries").insert(tbody);
    },

    disableOtherTime : function() {
        var node = $("otherArrivalTime");
        node.disabled = true;
        node.value = "";
    },
    
    enableOtherTime : function() {
        var node = $("otherArrivalTime");
        node.disabled = false;
        Form.Element.focus(node);
    },
    
    setNeedRide : function(node) {
        if (node.checked) {
            $("offerRide").checked = false;
        }
    },
    
    setOfferRide : function(node) {
        if (node.checked) {
            $("needRide").checked = false;
        }
    },
    
    validateAndSaveForm : function() {
        var retVal = false;
        var fieldName = "";
        if (applicationData.campers.length === 0) {
            alert("Ummm... There needs to be at least one camper.");
        }
        else if ((!$("arrival3_6").checked &&
                  !$("arrivalForDinner").checked &&
                  !$("arrivalAfterDinner").checked &&
                  !$("arrivalOther").checked) ||
                 ($("arrivalOther").checked && !$("otherArrivalTime").present())) {
            alert("Please specify and arrival time in the Travel section.");
        }
        else {
            for (var i = 0; i < mainForm.fieldList.length; i++) {
                fieldName = mainForm.fieldList[i];
                applicationData[fieldName] = $(fieldName).getValue();
            }
            application.saveApplicationData();
            application.finish();
            retVal = true;
        }
        return retVal;
    }
};

