Advertisement
Guest User

Untitled

a guest
May 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.56 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script>
  4. const EARLIEST_TIME = 0800;
  5. const LATEST_TIME = 1800;
  6. //constants used for isValidTime()
  7. const Months30 = [4, 6, 9, 11];
  8. const Months31 = [1, 3, 5, 7, 8, 10, 12];
  9. //constants used for isValidDate()
  10. const tableAppsHeader = ["Priority","Subject","Date","Start Time", "End Time"];
  11.  
  12.  
  13. var aryAppointments = []
  14. var testCount = 0;
  15. var aryPriority = []
  16. var arySubject = []
  17. var aryDates = [];
  18. var aryStartTime = []
  19. var aryEndTime = []
  20. //major arrays used for storing details of appointments
  21. var startTime;
  22. var endTime;
  23. //variables necessary for isValidTime() function
  24.  
  25. var dateValidity;
  26. //variables used for isValidDate()
  27. var tableRow = document.createElement("tr");
  28. var tableData = document.createElement("td");
  29. var tableText = document.createTextNode("");
  30. var tableApps;
  31. var displayedAppCount = 0;
  32. //showapps?
  33. var inputPriority;
  34. var inputSubject;
  35. var inputStartTime;
  36. var inputEndTime;
  37. var formBody;
  38. var inputCount = 0;
  39. //
  40.  
  41.  
  42. function isValidTime(){
  43. startTime = document.getElementById('startTime').value;
  44. endTime = document.getElementById('endTime').value;
  45. if (EARLIEST_TIME <= startTime < LATEST_TIME && EARLIEST_TIME <= endTime < LATEST_TIME && startTime < endTime){
  46. return isValidTime;
  47. }
  48. else{
  49. alert("Invalid time has been entered.");
  50. }
  51.  
  52. }
  53. //function designed to ensure the time is valid per requirements for the schedule
  54.  
  55.  
  56. function isValidDate() {
  57. dateInput = document.getElementById('appDate').value;
  58. //Take userinput and break it up into substrings
  59. subStrings = (dateInput).split('/');
  60. var dateDay = parseInt(subStrings[0]);
  61. var dateMonth = parseInt(subStrings[1]);
  62. var dateYear = parseInt(subStrings[2]);
  63. var startTime = document.getElementById('startTime').value;
  64. var leapYear;
  65. var daysInMonth = false;
  66. //Variables to calculate if the user input exceeds the current date/time
  67. var currentDate = new Date().getTime();
  68. var oldDateObject = new Date(subStrings[2], subStrings[1] - 1, subStrings[0]);
  69. alert(startTime)
  70. var dateObject = oldDateObject.getTime() + startTime*60000
  71. alert(currentDate + " " + dateObject)
  72. //Calculate if user input is leap year
  73. if (((dateYear % 4 == 0) && (dateYear % 100 != 0)) || (dateYear % 400 == 0)) {
  74. leapYear = true;
  75. } else {
  76. leapYear = false;
  77. }
  78.  
  79. // Validate amount of days in month of user input
  80. if (dateMonth === 2) {
  81. if (leapYear) {
  82. if (dateDay <= 29) {
  83. daysInMonth = true;
  84. }
  85. } else if (!leapYear) {
  86. if (dateDay <= 28) {
  87. daysInMonth = true;
  88. }
  89. }
  90. } else {
  91. if (Months30.indexOf(dateMonth) > -1) {
  92. if (dateDay <= 30) {
  93. daysInMonth = true;
  94. }
  95. } else if (Months31.indexOf(dateMonth) > -1) {
  96. if (dateDay <= 31) {
  97. daysInMonth = true;
  98. }
  99. }
  100. }
  101.  
  102. if (dateMonth <= 12 && dateMonth >= 1) {
  103. if (dateYear > 1583) {
  104. if (daysInMonth) {
  105. if (currentDate < dateObject) {
  106. dateValidity = true;
  107. } else {
  108. alert("Invalid date. Date/time has already passed.")
  109. }
  110. }else{
  111. alert("Invalid date. (Days < Days in month)")
  112. }
  113. } else {
  114. alert("Invalid date(dateYear > 1583)")
  115. }
  116. } else {
  117. alert("Invalid date(1 <= dateMonth <= 12)")
  118. }
  119. return dateValidity;
  120. }
  121.  
  122.  
  123. function isConcurrentAppintment() {
  124. var i = 0 // Iterative counter
  125. var indexOfDates = [];
  126.  
  127. dateInput = document.getElementById('appDate').value;
  128. startTime = document.getElementById('startTime').value;
  129. endTime = document.getElementById('endTime').value;
  130.  
  131. // Examine each element of arrayDates for dateInput and record indexes
  132. for(i=0; i<aryDates.length; i++) {
  133. if(aryDates[i] == dateInput)
  134. indexOfDates.push(aryDates.indexOf(dateInput))
  135. }
  136. // Check if appointment time is concurrent with another appointment
  137. i = 0
  138. while (i < indexOfDates.length) {
  139. if (startTime < aryStartTime[indexOfDates[i]] || aryEndTime[indexOfDates[i]] < endTime) {
  140. i++;
  141. return false
  142. } else {
  143. alert("Appointment's details are concurrent with another appointment")
  144. return true
  145. }
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. function addAppointments(){
  160. var check1 = document.getElementById("priority1").checked;
  161. var check2 = document.getElementById("priority2").checked;
  162. var check3 = document.getElementById("priority3").checked;
  163. if(isValidTime()) {
  164. if(isValidDate()){
  165. if (!isConcurrentAppintment()){
  166.  
  167. if (check1== true){
  168. inputPriority = document.getElementById("priority1").value;
  169. }
  170. if (check2== true){
  171. inputPriority = document.getElementById("priority2").value;
  172. }
  173. if (check3== true){
  174. inputPriority = document.getElementById("priority3").value;
  175. }
  176. dateInput = document.getElementById('appDate').value;
  177.  
  178. inputSubject = document.getElementById('subject').value;
  179.  
  180. inputStartTime = document.getElementById("startTime").value;
  181. inputEndTime = document.getElementById("endTime").value;
  182. aryDates.push(dateInput);
  183. aryPriority.push(inputPriority);
  184. arySubject.push(inputSubject);
  185. aryStartTime.push(inputStartTime);
  186. aryEndTime.push(inputEndTime);
  187. aryAppointments.push([aryPriority[inputCount],arySubject[inputCount],aryDates[inputCount],aryStartTime[inputCount],aryEndTime[inputCount]])
  188. alert("Your appointment has successfully been added.")
  189. showAppointments();
  190. inputCount++;
  191. }
  192. }
  193. }
  194. else{
  195. alert("Appointment's details are incorrect, please try again.")
  196. }
  197. formBody = document.getElementById('formBody').reset();
  198. }
  199.  
  200. function showAppointments(){
  201. var tableData1;
  202. var tableData2;
  203. var tableData3;
  204. var tableData4;
  205. var tableData5;
  206. var innerData1;
  207. var innerData2;
  208. var innerData3;
  209. var innerData4;
  210. var innerData5;
  211. var testData = [];
  212. var tableReset;
  213. var runCount= 0
  214.  
  215. if (testCount > 0){
  216. for (runCount = 0; runCount <aryAppointments.length;runCount++){
  217.  
  218. tableApps=document.getElementById("tableApps");
  219. tableApps.deleteRow(-1);
  220.  
  221. }
  222.  
  223. for(runCount= 0 ;runCount < aryAppointments.length;runCount++){
  224. if (runCount = 0){
  225. tableData1 = tableRow.insertCell(1)
  226. tableData2 = tableRow.insertCell(2);
  227. tableData3 = tableRow.insertCell(3);
  228. tableData4 = tableRow.insertCell(4);
  229. tableData5 = tableRow.insertCell(5);
  230.  
  231. innerData1 = "<strong>Priority</strong>";
  232. innerData2 = "<strong>Subject</strong>";
  233. innerData3 = "<strong>Date</strong>";
  234. innerData4 = "<strong>Start Time</strong>";
  235. innerData5 = "<strong>End Time</strong>";
  236. }
  237. }
  238. }
  239. else{
  240. tableRow = tableApps.insertRow(-1);
  241.  
  242. tableData1 = tableRow.insertCell(0);
  243. tableData2 = tableRow.insertCell(1);
  244. tableData3 = tableRow.insertCell(2);
  245. tableData4 = tableRow.insertCell(3);
  246. tableData5 = tableRow.insertCell(4);
  247.  
  248. innerData1 = tableData1.innerHTML = aryAppointments[runCount][0];
  249. innerData2 = tableData2.innerHTML = aryAppointments[runCount][1];
  250. innerData3 = tableData3.innerHTML = aryAppointments[runCount][2];
  251. innerData4 = tableData4.innerHTML = aryAppointments[runCount][3];
  252. innerData5 = tableData5.innerHTML = aryAppointments[runCount][4];
  253. }
  254. testCount++
  255.  
  256. else{
  257. for (runCount = 0; runCount < aryAppointments.length + 1; runCount++){
  258.  
  259. tableApps
  260. tableApps = document.getElementById("tableApps");
  261. tableRow = tableApps.insertRow(-1);
  262.  
  263. if (runCount = 0){
  264. tableData1 = tableRow.insertCell(1)
  265. tableData2 = tableRow.insertCell(2);
  266. tableData3 = tableRow.insertCell(3);
  267. tableData4 = tableRow.insertCell(4);
  268. tableData5 = tableRow.insertCell(5);
  269.  
  270. innerData1 = "<strong>Priority</strong>";
  271. innerData2 = "<strong>Subject</strong>";
  272. innerData3 = "<strong>Date</strong>";
  273. innerData4 = "<strong>Start Time</strong>";
  274. innerData5 = "<strong>End Time</strong>";
  275. }
  276. else{
  277.  
  278. tableData1 = tableRow.insertCell(0);
  279. tableData2 = tableRow.insertCell(1);
  280. tableData3 = tableRow.insertCell(2);
  281. tableData4 = tableRow.insertCell(3);
  282. tableData5 = tableRow.insertCell(4);
  283.  
  284. innerData1 = tableData1.innerHTML = aryAppointments[runCount][0];
  285. innerData2 = tableData2.innerHTML = aryAppointments[runCount][1];
  286. innerData3 = tableData3.innerHTML = aryAppointments[runCount][2];
  287. innerData4 = tableData4.innerHTML = aryAppointments[runCount][3];
  288. innerData5 = tableData5.innerHTML = aryAppointments[runCount][4];
  289. }
  290.  
  291. }
  292. testCount++
  293. }
  294. }
  295.  
  296.  
  297.  
  298. function shuffleAppointments(){
  299. alert(aryAppointments);
  300. var shuffleArray = aryAppointments.length;
  301. var displayCount = shuffleArray;
  302. var tempValue;
  303. var randIndex;
  304. var tableApps;
  305. var tableRows;
  306. var htmlChange;
  307.  
  308.  
  309. while (shuffleArray!==0){
  310. randIndex = Math.floor(Math.random() * shuffleArray);
  311. shuffleArray-=1;
  312.  
  313. tempValue = aryAppointments[shuffleArray];
  314. aryAppointments[shuffleArray] = aryAppointments[randIndex];
  315. aryAppointments[randIndex] =tempValue;
  316. alert(aryAppointments);
  317. }
  318. }
  319.  
  320. function tallyAppointments() {
  321.  
  322. var tally = [];
  323. var formatDates = [];
  324. var max;
  325. var min;
  326. var i;
  327. var count; //Iterative counter
  328. var arrayLength;
  329. var output = '';
  330.  
  331.  
  332. //Split each date in array and rearrange so correct dates may be obtained when using Date.parse
  333. for(var i=0; i<aryDates.length; i++) {
  334. var subStrings = aryDates[i].split('/')
  335. var dateObject = new Date(subStrings[2], subStrings[1] - 1, subStrings[0]); // month is 0-based
  336. formatDates.push(dateObject.getTime())
  337. }
  338.  
  339. // Calculate the min date and max date, record the difference between the two (the difference will be the length of the other arrays)
  340. //Min
  341. min = Number.POSITIVE_INFINITY;
  342. for (i=0; i<formatDates.length; i++)
  343. { if (formatDates[i] < min)
  344. min = formatDates[i];
  345. }
  346.  
  347. //Max
  348. max = Number.NEGATIVE_INFINITY;
  349. for (i=0; i<formatDates.length; i++)
  350. { if (formatDates[i] > max)
  351. max = formatDates[i];
  352. }
  353.  
  354.  
  355. arrayLength = ((max - min) / (1000 * 60 * 60 * 24) + 1) //convert length milliseconds to days
  356. //tally = new Array(arrayLength) //Tally length is number of days between min and max
  357.  
  358. for(i=0; i<arrayLength; i++){
  359. count = 0
  360. index = ((formatDates[i] - min) / (1000 * 60 * 60 * 24))
  361. for(j=0; j<aryDates.length; j++){
  362. if (aryDates[i] === aryDates[j]) {
  363. count++
  364. }
  365. tally[index] = count
  366. }
  367. }
  368.  
  369. // Output into table. Converts index back to date dd/mm/yyyy.
  370. tableSummary = document.getElementById("tableSummary");
  371. tableData = tableSummary.insertRow(-1);
  372.  
  373. output += '<table border="1">';
  374. output += '<tr><th>Date</th><th>Count</th></tr>';
  375. for(i=0; i<tally.length; i++){
  376. var date = new Date(min + (i *(1000 * 60 * 60 * 24)));
  377. var date = date.getDate() + "/" + (parseInt(date.getMonth())+1) + "/" + date.getFullYear()
  378. output += '<tr><td>' + date + '</td><td>';
  379.  
  380. for(var j=0; j<tally[i]; j++){
  381. output += '#';
  382. }
  383. output += '</td></tr>';
  384. }
  385. output += '</table>';
  386. document.writeln(output);
  387.  
  388. }
  389.  
  390.  
  391.  
  392.  
  393.  
  394. </script>
  395. </head>
  396. <body>
  397. <section>
  398. <h1>Schedule Editor</h1>
  399.  
  400.  
  401. <form id='formBody'>
  402. <p>Priority:</p>
  403. <input type="radio" name="priority" id="priority1" value="high" >High</input>
  404. <input type="radio" name="priority" id="priority2" value="moderate" checked>Moderate</input>
  405. <input type="radio" name="priority" id="priority3" value="low">Low</input>
  406. <!--priority radio buttons section-->
  407.  
  408. <p>Subject:</p>
  409. <input type='text' size='30' id='subject'/>
  410. <!--Subject input section-->
  411.  
  412. <p>Date:</p>
  413. <input type='text' size='30' id='appDate'/>
  414. <!--Date input-->
  415.  
  416. <p>Start Time:</p>
  417. <select id='startTime'>
  418. <option value=0800>0800</option>
  419. <option value=0900>0900</option>
  420. <option value=1000>1000</option>
  421. <option value=1100>1100</option>
  422. <option value=1200>1200</option>
  423. <option value=1300>1300</option>
  424. <option value=1400>1400</option>
  425. <option value=1500>1500</option>
  426. <option value=1600>1600</option>
  427. <option value=1700>1700</option>
  428. <option value=1800>1800</option>
  429. </select>
  430.  
  431.  
  432. <p>End Time:</p>
  433. <select id='endTime'>
  434. <option value=0800>0800</option>
  435. <option value=0900>0900</option>
  436. <option value=1000>1000</option>
  437. <option value=1100>1100</option>
  438. <option value=1200>1200</option>
  439. <option value=1300>1300</option>
  440. <option value=1400>1400</option>
  441. <option value=1500>1500</option>
  442. <option value=1600>1600</option>
  443. <option value=1700>1700</option>
  444. <option value=1800>1800</option>
  445. </select>
  446. <input type='button' onclick='addAppointments()' value='Add appointment' name='addAppointment'>
  447. <hr>
  448. </section>
  449.  
  450.  
  451. <section>
  452. <table border='1' id='tableApps'>
  453. <tr>
  454. <td><strong>Priority</strong></td>
  455. <td><strong>Subject</strong></td>
  456. <td><strong>Date</strong></td>
  457. <td><strong>Start Time</strong></td>
  458. <td><strong>End Time</strong></td>
  459. </tr>
  460. </table>
  461.  
  462. <form>
  463. <input type='button' onclick='shuffleAppointments()' value='Randomise Appointments' name='randomApps'>
  464. <input type='button' onclick='sortAppointments()' value='Sort Appointments' name='sortApps'>
  465. <select>
  466. <option value='priority'>Priority</option>
  467. <option value='date'>Date</option>
  468. <option value='startTime'>Start Time</option>
  469. <option value='endTime'>End Time</option>
  470. <option value='subject'>Subject</option>
  471. </select>
  472. </form>
  473. <hr>
  474. </section>
  475.  
  476.  
  477. <section>
  478. <table border='1' id='tableSummary'>
  479. <tr>
  480. <td><strong>Date</strong></td>
  481. <td><strong>Name</strong></td>
  482. </tr>
  483. </table>
  484. <form>
  485. <input type='button' onclick='tallyAppointments()' value='Summary' name='tallyApps'>
  486. </form>
  487. <hr>
  488. </section>
  489.  
  490.  
  491. <section>
  492. <table border='1'>
  493. <tr>
  494. <td><strong>Priority</strong></td>
  495. <td><strong>Date</strong></td>
  496. <td><strong>Start Time</strong></td>
  497. <td><strong>End Time</strong></td>
  498. <td><strong>Subject</strong></td>
  499. </tr>
  500. </table>
  501.  
  502. <form>
  503. <input type='button' onclick='doit()' value='Search' name='searchName'>
  504. <input type='text' size='15'>
  505. </form>
  506.  
  507.  
  508.  
  509.  
  510.  
  511. </body>
  512. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement