PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/widgets/resources/.parsers/phoneBookParser.js

http://kludgets.googlecode.com/
JavaScript | 153 lines | 127 code | 17 blank | 9 comment | 20 complexity | dad896d4b70fb4da9675fc9e16b8677d MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*
  2. Copyright (c) 2005, Apple Computer, Inc. All rights reserved.
  3. NOTE: Use of this source code is subject to the terms of the Software
  4. License Agreement for Mac OS X, which accompanies the code. Your use
  5. of this source code signifies your agreement to such license terms and
  6. conditions. Except as expressly granted in the Software License Agreement
  7. for Mac OS X, no other copyright, patent, or other intellectual property
  8. license or right is granted, either expressly or by implication, by Apple.
  9. */
  10. var gLastSearchRequest;
  11. var gLastValidationRequest;
  12. function sendAsyncGetRequest(url, loadHandler) {
  13. var req = new XMLHttpRequest();
  14. req.onload = function(evt) {loadHandler(evt, req);};
  15. req.onerror = function(evt) {alert("Phone Book Widget: xml request failed.");};
  16. req.open("GET", url, true);
  17. req.overrideMimeType("application/xml");
  18. req.setRequestHeader("Cache-control", "no-cache");
  19. req.setRequestHeader("Pragma", "no-cache");
  20. req.send(null);
  21. return req;
  22. }
  23. function sendSearchRequest(searchCommand) {
  24. if (gLastSearchRequest) {
  25. gLastSearchRequest.abort();
  26. gLastSearchRequest = null;
  27. }
  28. var url = (searchCommand.isKeyword) ?
  29. "http://wu.apple.com/apple/GetResults.aspx?SearchType=Business&sic=" :
  30. "http://wu.apple.com/apple/GetResults.aspx?SearchType=Business&BusName=";
  31. url += encodeURIComponent(searchCommand.searchString)
  32. + "&RecordsFrom=" + searchCommand.startIndex + "&RecordsTo=" + (searchCommand.startIndex + searchCommand.itemsPerPage)
  33. + "&miles=" + searchCommand.searchRadius;
  34. var location = searchCommand.location;
  35. if (isFinite(location)) {
  36. url += "&Zip=" + encodeURIComponent(location);
  37. } else {
  38. var i = location.lastIndexOf(",");
  39. var city = location.substring(0, i);
  40. var state = location.substring(i+1);
  41. url += "&City=" + encodeURIComponent(trim(city))
  42. + "&State=" + encodeURIComponent(trim(state));
  43. }
  44. gLastSearchRequest = sendAsyncGetRequest(url, searchLoaded);
  45. }
  46. function searchLoaded(evt, req) {
  47. var doc = req.responseXML;
  48. if (doc) {
  49. try {
  50. gTotalResults = doc.evaluate("number(/AppleResults/total[1])",
  51. doc, null, XPathResult.NUMBER_TYPE, null).numberValue;
  52. } catch (e) {
  53. gTotalResults = 0;
  54. }
  55. }
  56. handleSearchResults(doc);
  57. }
  58. function sendValidationRequest(str) {
  59. if (gLastValidationRequest) {
  60. gLastValidationRequest.abort();
  61. gLastValidationRequest = null;
  62. }
  63. var match = str.match(/(\W+)\w*\s*$/);
  64. var city, state;
  65. if (match) {
  66. var i = match.index;
  67. city = str.substring(0, i);
  68. state = str.substring(i+1);
  69. } else {
  70. showLocationValidationResults(null);
  71. return;
  72. }
  73. var url = "http://www.daplus.us/apple/GetResults.aspx?SearchType=Business&validate=true&City="
  74. + encodeURIComponent(trim(city));
  75. if (state) {
  76. url += "&State=" + encodeURIComponent(trim(state));
  77. }
  78. gLastValidationRequest = sendAsyncGetRequest(url, validationLoaded);
  79. }
  80. function validationLoaded(evt, req) {
  81. var doc = req.responseXML;
  82. var names;
  83. if (doc) {
  84. var cityEls = doc.getElementsByTagName("City");
  85. var stateEls = doc.getElementsByTagName("State");
  86. if (cityEls && cityEls.length) {
  87. names = new Array(cityEls.length);
  88. for (var i=0; i < cityEls.length; i++) {
  89. var cityEl = cityEls[i];
  90. var stateEl = stateEls[i];
  91. var name = convertToInitialCaps(cityEl.textContent) + ", "
  92. + stateEl.textContent.toUpperCase();
  93. names[i] = name;
  94. }
  95. }
  96. } else {
  97. names = [];
  98. }
  99. showLocationValidationResults(names);
  100. }
  101. function getBizProfileUrl(bizId) {
  102. return "http://www.daplus.us/ViewBusinessProfile.aspx?recordid=" + bizId + "&Partner=400119";
  103. }
  104. function getVendorHomepageUrl() {
  105. return "http://www.daplus.us";
  106. }
  107. function getBizMapUrl(bizId) {
  108. gCurrentBizId = bizId;
  109. var addr = getCurrentAddressData();
  110. var bizName = $("name-" + bizId).textContent;
  111. return "http://www.daplus.us/showmap.aspx"
  112. + "?businessname=" + encodeURIComponent(bizName)
  113. + "&address=" + encodeURIComponent(addr.street)
  114. + "&city=" + encodeURIComponent(addr.city)
  115. + "&state=" + encodeURIComponent(addr.state)
  116. + "&zip=" + encodeURIComponent(addr.postalCode)
  117. + "&Partner=appleyp";
  118. }
  119. function convertToInitialCaps(str) {
  120. var words = str.split(/\s+/);
  121. for (var i = 0; i < words.length; i++) {
  122. var len = words[i].length;
  123. if (0 == len) {
  124. continue;
  125. } else if (1 == len) {
  126. words[i] = words[i].toUpperCase();
  127. } else {
  128. words[i] = words[i].toLowerCase();
  129. words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
  130. }
  131. }
  132. return words.join(' ');
  133. }
  134. function trim(str) {
  135. return str.replace(/^\s*/, "").replace(/\s*$/, "");
  136. }