// Add an event handler to a document element function addEvent(elm, evType, fn, useCapture) { if (elm.addEventListener) { elm.addEventListener(evType, fn, useCapture); return true; } else if (elm.attachEvent) { var r = elm.attachEvent('on' + evType, fn); return r; } else { elm['on' + evType] = fn; } } // Climb up the tree to the supplied tag. function ascendDOM(e, target) { while (e.nodeName.toLowerCase() != target && e.nodeName.toLowerCase() != 'html') { e = e.parentNode; } return (e.nodeName.toLowerCase() == 'html') ? null : e; } // Create and return a new DIV element containing the name, locale // and comment for the given user function createInfoBox(user) { var info = document.createElement("div"); info.className = "infoBlock"; info.appendChild(document.createTextNode(user.name)); info.appendChild(document.createElement("br")); info.appendChild(document.createTextNode(user.city)); if (user.state != "") { info.appendChild(document.createTextNode(", ")); info.appendChild(document.createTextNode(user.state)); } info.appendChild(document.createTextNode(", ")); info.appendChild(document.createTextNode(user.country)); info.appendChild(document.createElement("br")); var span = document.createElement("span"); span.innerHTML = user.comment; info.appendChild(span); return info; } function userClick(e) { if (window.event) { var anchor = window.event.srcElement; } else { var anchor = e.target; } var row = ascendDOM(anchor, "tr"); var user = row.user; row.marker.openInfoWindow(createInfoBox(user)); return false; } function addMarker(user) { var marker = new GMarker(new GPoint(user.longitude, user.latitude)); map.addOverlay(marker); var info = document.createElement("div"); info.id = user.id; info.className = "infoBlock"; var text = document.createTextNode(user.name); info.appendChild(text); GEvent.addListener(marker, "click", function() { marker.openInfoWindow(createInfoBox(user)); } ); return marker; } function processUsers() { var table = document.getElementById("userTable"); var tbody = table.tBodies[0]; var row, cell, anchor, text; var caption = table.createCaption(); text = document.createTextNode("Total registered: " + users.length); caption.appendChild(text); for (i = 0; i < users.length; i++) { var marker = window.parent.addMarker(users[i]); row = tbody.insertRow(tbody.rows.length); row.user = users[i]; row.marker = marker; cell = row.insertCell(row.cells.length); anchor = document.createElement("a"); anchor.href = "javascript:show_marker"; addEvent(anchor, "click", userClick, false); cell.appendChild(anchor); text = document.createTextNode(users[i].name); anchor.appendChild(text); var location = users[i].city; if (users[i].state != "") { location += ", "; location += users[i].state; } location += ", "; location += users[i].country; cell = row.insertCell(row.cells.length); text = document.createTextNode(location); cell.appendChild(text); } } function showZipField(e) { var usPanel = document.getElementById("usPanel"); usPanel.style.display = "block"; var intlPanel = document.getElementById("intlPanel"); intlPanel.style.display = "none"; } function showCityCountryFields(e) { var intlPanel = document.getElementById("intlPanel"); intlPanel.style.display = "block"; var usPanel = document.getElementById("usPanel"); usPanel.style.display = "none"; } function submitForm(e) { var errorMsg = validateForm(); if (errorMsg != "") { alert("The following errors occurred, please fix and try again:\n\n" + errorMsg); } else { var myAjax = new Ajax.Request( "isd_form_submit.php", { method: 'get', parameters: Form.serialize("newUserForm"), onComplete: handleResponse }); } if (e && e.stopPropagation && e.preventDefault) { e.stopPropagation(); e.preventDefault(); } if (window.event) { window.event.cancelBubble = true; window.event.returnValue = false; return false; } } function validateForm() { var errorMsg = ""; var nameField = document.getElementById("nameField"); var locationField = document.getElementById("locationField_US"); var zipCodeField = document.getElementById("zipCodeField"); var cityField = document.getElementById("cityField"); var countryField = document.getElementById("countryField"); if (nameField.value == "") { errorMsg += "Must supply a name\n"; } if (locationField.checked) { if (zipCodeField.value == "") { errorMsg += "Must supply a ZIP code\n"; } else { if (!zipCodeField.value.match(/[0-9][0-9][0-9][0-9][0-9]/)) { errorMsg += "ZIP code must be a 5 digit number"; } } } else { if ((cityField.value == "") || (countryField.selectedIndex == 0)) { errorMsg += "Must supply a city and country\n"; } } return errorMsg; } function handleResponse(result) { if (result.responseText.match(/false/)) { alert("Sorry, the location you specified could not be located. Please try again."); } else { window.location.reload(true); } } var map; function init() { addEvent(document.getElementById("newUserForm"), "submit", submitForm, false); addEvent(document.getElementById("locationField_US"), "click", showZipField, false); addEvent(document.getElementById("locationField_Other"), "click", showCityCountryFields, false); map = new GMap(document.getElementById("map")); map.addControl(new GLargeMapControl()); map.centerAndZoom(new GPoint(-94.5703125, 39.70718665682654), 14); processUsers(); } addEvent(window, "load", init, false); var users = [ { id: 1632, name: "CB", comment: "", city: "SHAWNEE MISSION", state: "KS", country: "US", latitude: 38.960029, longitude: -94.740662 } ];