The Doc Dialer, Part 2: A Browser Independent Version: The Code | WebReference

The Doc Dialer, Part 2: A Browser Independent Version: The Code


The Doc Dialer, Part 2: A Browser Independent Version

The Code

<HTML>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<HEAD>
<H1>The Doc Dialer</H1>
<SCRIPT LANGUAGE="JavaScript">
<!--
// by Yehuda Shiran. v2.0
// Copyright (c) 2000 Yehuda Shiran. All Rights Reserved.
// See http://www.docjavascript.com for more information
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
function bName() {
  if (navigator.appName == "Microsoft Internet Explorer")
    return 1;
  if (navigator.appName == "Netscape")
    return 2;
  return 0;
}
function bVer() {
  // return version number (e.g., 4.03)
  msieIndex = navigator.appVersion.indexOf("MSIE") + 5;
  return(parseFloat(navigator.appVersion.substr(msieIndex,3)));
}
var nameCode = bName();
var versionCode = bVer();
var agt = navigator.userAgent.toLowerCase(); 
var mac = (agt.indexOf("mac")!=-1); 
// -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
onload = start;
function start() {
  outputStringToDisplay(welcomeStr);
}
function numPressed(digit) {
  if (digit == 0) {  // clear
    currentTrie = tree;
    outputStringToDisplay(welcomeStr);
  }
  else if (digit == 10) {  // enter new
         enterNewName();
		 currentTrie = tree;
       }
       else if (digit >= 2 && digit <= 9) {
               if (currentTrie[digit]) {
			     currentTrie = currentTrie[digit];
                 updateBoard(currentTrie);
                 printDisplay();
			   }
            }
  clearDisplayIndex();
}
function enterNewName() {
  var aDuplicate = true;
  while (aDuplicate) {
    var newName = prompt("Enter A New Name, First Name followed by Last Name, blank separated:", "Theodore Roosevelt");
    if (!newName) return;
    var firstName = extractFirstName(newName);
    var lastName = extractLastName(newName);
    while (true) {
      var phoneExt = parseInt(prompt("Enter employee's 4-digit extension number:", "0000"));
      if (phoneExt >= 1000 && phoneExt <= 9999) break;
    }
	checkNewName = firstName + " " + lastName;
	aDuplicate = verifyNewName(checkNewName, phoneExt);
  }
  lastEmp++;
  empList[lastEmp] = firstName + " " + lastName;
  empPhone[lastEmp] = phoneExt;
  addToTrie(firstName + lastName, lastEmp);
  addToTrie(lastName + firstName, lastEmp);  
}
function initializePresidents() {
  for (var i = 1; i <= lastEmp; i++) {
    var newName = empList[i];
    var firstName = extractFirstName(newName);
    var lastName = extractLastName(newName);
    addToTrie(firstName + lastName, i);
    addToTrie(lastName + firstName, i);
  }  
}
function verifyNewName(checkNewName, phoneExt) {
  currentTrie = tree;
  checkNewName = checkNewName.toUpperCase();
  for (var i = 0; i < checkNewName.length; i++) {
    if (checkNewName.charAt(i) == " ") continue; // a blank
    var digit = parseInt(keyMap[checkNewName.toUpperCase().charAt(i)]);
	pushOneLevel(digit);
  }
  return checkLeafRecord(checkNewName, phoneExt);
}
function checkLeafRecord(newNameToCheck, phoneExtToCheck) {
  var prevNamePtr, nextNamePtr;
  var nextNamePtr = currentTrie[1];
  if (!nextNamePtr) {
    return false;
  }
  else {
    while (nextNamePtr) {
	  prevNamePtr = nextNamePtr;
	  nextNamePtr = nextNamePtr.next;
	  if (empList[prevNamePtr.empIndex].toUpperCase() == newNameToCheck && 
	      empPhone[prevNamePtr.empIndex] == phoneExtToCheck) {
		  return true;
	  }
	}
	return false;
  }
}
function addToTrie(newEntry, empIndex) {
  currentTrie = tree;
  newEntry = newEntry.toUpperCase();
  for (var i = 0; i < newEntry.length; i++) {
    var digit = parseInt(keyMap[newEntry.charAt(i)]);
	descendOneLevel(digit);
  }
  addNameToLeafRecord(empIndex);
}
function addNameToLeafRecord(empNo) {
  var prevNamePtr, nextNamePtr;
  var tmpRecordPtr = new createRecord(empNo);
  var nextNamePtr = currentTrie[1];
  if (!nextNamePtr) {
	currentTrie[1] = tmpRecordPtr;
  }
  else {
    while (nextNamePtr) {
	  prevNamePtr = nextNamePtr;
	  nextNamePtr = nextNamePtr.next;
	}
	prevNamePtr.next = tmpRecordPtr;
  }
}
function createRecord(empNumber) {
  this.empIndex = empNumber;
  this.next = null;
}
function descendOneLevel(digit) {
  if (!currentTrie[digit]) {
    var tmpArray = new Array(10);
	currentTrie[digit] = tmpArray;
	currentTrie = tmpArray;
  } 
  else {
    currentTrie = currentTrie[digit];
  }
}
 
function pushOneLevel(digit) {
  if (currentTrie[digit]) {
    currentTrie = currentTrie[digit];
  }
}
function updateBoard(trieNode) {
  if (trieNode[1]) {
    addEmployeeToDisplay(trieNode[1]);
  }
  for (var i=2; i<=9; i++) {
    memberNode = trieNode[i];
    if (memberNode) {
      updateBoard(memberNode);
    }
  }
  return;
}
function addEmployeeToDisplay(leafNode) {
  while (leafNode) {
    printList[leafNode.empIndex] = true;
	leafNode = leafNode.next;
  }
}
function clearDisplayIndex() {
  for (i=0; i<=lastEmp; i++) {
    printList[i] = false;
  }
}
function printDisplay() {
  var str = "";
  for (i=0; i<=lastEmp; i++) {
    if (printList[i] == true) {
      var fileName = extractFileName(empList[i]).toLowerCase();
	    str += '<A HREF=' + '"' + fileName + '.html"' + '>' + empList[i] + '</A>' + '  ' + 
	         '<A HREF="calling.html" >' + empPhone[i] + '</A>' + '<BR>';
	  }
  }
  if (str != "") {
    outputStringToDisplay(str);
  }
}
function outputStringToDisplay(str) {
  if (nameCode == 2) {
    with (document.displayID.document) {
      open();
	  str = '<SPAN STYLE="position: absolute; border: 2px red solid; width: 235">' + str + '</SPAN>';
      write(str);
      close();
    }
  } 
  else {
    document.all.foundSoFar.innerHTML = str;
  }
}
function extractFirstName(empName) {
  if (nameCode == 1 && versionCode >= 5.5) {
    var regExp = /(\w+)\s*(\w+)/g;
    return empName.replace(regExp, matchFirstName);
  }
  else {
    blankPos = empName.indexOf(" ");
    firstName = empName.substr(0, blankPos);
    return firstName;
  }
}
function extractLastName(empName) {
  if (nameCode == 1 && versionCode >= 5.5) {
    var regExp = /(\w+)\s*(\w+)/g;
    return empName.replace(regExp, matchLastName);
  }
  else {
    blankPos = empName.indexOf(" ");
    lastName = empName.substr(blankPos + 1, empName.length - blankPos);
    return lastName;
  }
}
function extractFileName(empName) {
  if (nameCode == 1 && versionCode >= 5.5) {
    var regExp = /(\w+)\s*(\w+)/g;
    return empName.replace(regExp, matchingFunction);
  }
  else {
    blankPos = empName.indexOf(" ");
    firstName = empName.substr(0, blankPos);
    lastName = empName.substr(blankPos + 1, empName.length - blankPos);
    return firstName + lastName;
  }
}
function matchFirstName(matchedString, subMatch1, subMatch2, matchPos, source) {
  return (subMatch1)
}
function matchLastName(matchedString, subMatch1, subMatch2, matchPos, source) {
  return (subMatch2)
}
function matchingFunction(matchedString, subMatch1, subMatch2, matchPos, source) {
  return (subMatch1 + subMatch2)
}
// -->
</SCRIPT>
<FORM NAME="phonePlate">
<TABLE BORDER=2WIDTH=150 HEIGHT=60CELLPADDING=1 CELLSPACING=5>
<TR>
  <TD><INPUT NAME="button1" TYPE="BUTTON" VALUE=" 1       "
       onclick="numPressed(1)"></TD>
  <TD><INPUT NAME="button2" TYPE="BUTTON" VALUE=" 2 ABC "
       onclick="numPressed(2)"></TD>
  <TD><INPUT NAME="button3" TYPE="BUTTON" VALUE=" 3 DEF "
       onclick="numPressed(3)"></TD>
</TR>
<TR>
  <TD><INPUT NAME="button4" TYPE="BUTTON" VALUE=" 4 GHI "
       onclick="numPressed(4)"></TD>
  <TD><INPUT NAME="button1" TYPE="BUTTON" VALUE=" 5 JKL  "
       onclick="numPressed(5)"></TD>
  <TD><INPUT NAME="button1" TYPE="BUTTON" VALUE=" 6 MNO"
       onclick="numPressed(6)"></TD>
</TR>
<TR>
  <TD><INPUT NAME="button7" TYPE="BUTTON" VALUE=" 7 PRS"
       onclick="numPressed(7)"></TD>
  <TD><INPUT NAME="button8" TYPE="BUTTON" VALUE=" 8 TUV "
       onclick="numPressed(8)"></TD>
  <TD><INPUT NAME="button9" TYPE="BUTTON" VALUE=" 9 WXY"
       onclick="numPressed(9)"></TD>
</TR>
<TR>
  <TD><INPUT NAME="button*" TYPE="BUTTON" VALUE=" *        "
       onclick="numPressed(10)"></TD>
  <TD><INPUT NAME="button0" TYPE="BUTTON" VALUE=" 0        "
       onclick="numPressed(0)"></TD>
  <TD><INPUT NAME="button#" TYPE="BUTTON" VALUE=" #        "
       onclick="numPressed(11)"></TD>
</TR>
</TABLE>
</FORM>
<DIV ID="displayID" STYLE="position: absolute"> <SPAN ID="foundSoFar" STYLE="position: absolute; border: 2px red solid; width: 250"></SPAN> </DIV>
</HEAD>
<BODY>
<SCRIPT>
<!--
var EMPMAX = 10000;
var empList = new Array(EMPMAX);
var keyMap = new Array(24);
keyMap["A"] = "2";
keyMap["B"] = "2";
keyMap["C"] = "2";
keyMap["D"] = "3";
keyMap["E"] = "3";
keyMap["F"] = "3";
keyMap["G"] = "4";
keyMap["H"] = "4";
keyMap["I"] = "4";
keyMap["J"] = "5";
keyMap["K"] = "5";
keyMap["L"] = "5";
keyMap["M"] = "6";
keyMap["N"] = "6";
keyMap["O"] = "6";
keyMap["P"] = "7";
keyMap["R"] = "7";
keyMap["S"] = "7";
keyMap["T"] = "8";
keyMap["U"] = "8";
keyMap["V"] = "8";
keyMap["W"] = "9";
keyMap["X"] = "9";
keyMap["Y"] = "9";
empList[1] = "Ronald Reagan"; 
empList[2] = "Jimmy Carter";  
empList[3] = "Richard Nixon";
empList[4] = "Bill Clinton"; 
empList[5] = "Gerald Ford"; 
empList[6] = "George Bush";  
empList[7] = "Lyndon Johnson"; 
empList[8] = "John Kennedy";  
var empPhone = new Array(EMPMAX);
empPhone[1] = 5808;
empPhone[2] = 5919;
empPhone[3] = 5303;
empPhone[4] = 5606;
empPhone[5] = 5707;
empPhone[6] = 5313;
empPhone[7] = 5838;
empPhone[8] = 5006;
var tree = new Array(10);
var lastEmp = 8;
initializePresidents();
var currentTrie = tree;
var printList = new Array(EMPMAX);
clearDisplayIndex();
var welcomeStr = "Welcome to The Doc Dialer. <BR>Please key in a president name.<BR>Press 0 to clear, * to enter new names";
outputStringToDisplay(welcomeStr);
// -->
</SCRIPT>
</BODY>
</HTML>

Next: A final word

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: February 28, 2000
Revised: April 26, 2000

URL: http://www.webreference.com/js/column58/***PASTE FILENAME HERE***