Listing of Actions.js
//------------------------------------------------------------------ // Actions: when user does something //------------------------------------------------------------------ //------------------------------------------------------------------ // Help //------------------------------------------------------------------ function doHelp() { printHelp(); } function doCloseHelp() { printPeople(); } //------------------------------------------------------------------ // Edit: new person //------------------------------------------------------------------ var editIndex = -1; function doAdd() { editError = ""; editName = ""; editBirthdate = ""; editPictureURL = ""; editAgeFormat = 0; editIndex = -1; // signal adding someone printEdit(); } //------------------------------------------------------------------ // Edit: current person //------------------------------------------------------------------ function doEdit(index) { editError = ""; editName = people.array[index].name; editBirthdate = people.array[index].birthdate; editPictureURL = people.array[index].pictureURL; editAgeFormat = people.array[index].ageFormat; editIndex = index; printEdit(); } function doSave() { if (!editOn) return; if (!checkEdits()) { printEdit(); return; } //------------------------------------------------------------------ // data is ok. Suppress further editing //------------------------------------------------------------------ editOn = false; if (editIndex == -1) { editIndex = people.array.length; // index of new position people.array.length ++; } people.array[editIndex] = { "name" : editName, "birthdate" : editBirthdate, "pictureURL" : editPictureURL, "ageFormat" : editAgeFormat }; printPeople(); savePeople(); } function doCancel() { printPeople(); } //------------------------------------------------------------------ // Save edits, check for errors //------------------------------------------------------------------ function checkEdits() { editName = _trim(_gel("name").value); editBirthdate = _trim(_gel("birthdate").value); editPictureURL = _trim(_gel("pictureURL").value); editAgeFormat = parseInt(_gel("ageFormat").value); editError = ""; if (editName == "") { editError += "The name cannot be blank. " } editError += checkDate(editBirthdate); if (editError != "") { editError = "<p>" + editError + "</p>"; return false; } else { return true; } } //------------------------------------------------------------------ // Check for a valid date //------------------------------------------------------------------ function checkDate(dateString) { var error = "Please enter dates as mm/dd/yyyy, with yyyy = 100 to 9999. "; //------------------------------------------------------------------ // first some basic checks on the string //------------------------------------------------------------------ var dateSep = dateString.split("/"); if (dateSep.length != 3 || dateSep[0] < 1 || dateSep[0] > 12 || dateSep[1] < 1 || dateSep[1] > 31 || dateSep[2] < 100 || dateSep[2] > 9999) { return error; } //------------------------------------------------------------------ // the acid test is the conversion to date format //------------------------------------------------------------------ var d = new Date(dateString); var today = new Date(); if (d > today) { return "Hey, that's in the future!"; } if (isNaN(d.getMonth()) || isNaN(d.getDate()) || isNaN(d.getFullYear())) { return error; } return ""; } //------------------------------------------------------------------ // Delete a person //------------------------------------------------------------------ function doDelete(index) { people.array.splice(index, 1); printPeople(); savePeople(); } //------------------------------------------------------------------ // Check for enter key on text field //------------------------------------------------------------------ function checkEnter(e) { if (!editOn) return; var characterCode; if (e && e.which) { characterCode = e.which; } else { try { characterCode = event.keyCode; } catch (x) { characterCode = 0; } } if (characterCode == 13) doSave(); }