window.formFieldNames = new Array("firstName",
"lastName",
"street",
"city",
"state",
"zip",
"country",
"email",
"phone",
"mobilePhone",
"notes");
window.formResetValues = new Array(window.formFieldNames.length);
window.idNum = 0;
window.rowID = "";
function getFormValues()
{
var values = "";
for(var i = 0; i < formFieldNames.length; i++)
{
var v = encodeURIComponent(Trim(T$(formFieldNames[i]).value));
if(v.length > 0)
{
values += (formFieldNames[i] + "=" + v + "&");
}
}
return values;
}
function saveCredentials(name)
{
var form = document.loginForm;
var user = form.user.value;
var password = form.password.value;
if(form.saveCred.checked == true && user.length > 0 && password.length > 0)
{
setCookie(name, user + " " + password, window.cookieDaysExpire);
}
else
{
setCookie(name, "", 0); // cookie expires after the current session
}
}
function capLock(e) // displays message if caps lock is on
{
var kc = e.keyCode?e.keyCode:e.which;
var sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
T$('caps_lock_check').style.visibility = 'visible';
else
T$('caps_lock_check').style.visibility = 'hidden';
}
function show_help(help_file, section)
{
var url = help_file + "#" + section;
var newWindow = window.open(url,'popup','width=600,height=425,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no');
if(window.focus) newWindow.focus();
}
function getXMLHttpRequest()
{
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
return null;
}
function getXHR() // put XMLHttpRequest a global object
{
return (window.xmlhttp = getXMLHttpRequest()) ? true : false;
}
function show_keys(field)
{
var container = T$("ajaxResults");
if(field.checked)
{
if(window.xmlhttp || getXHR() && window.xmlhttp.readyState == 0)
{
window.xmlhttp.onreadystatechange = function() {}
window.xmlhttp.open("POST","contacts3Ajax.php", false); // synchronous request
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("keys=Keys"); // blocks till Ajax results returned
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message at end of responseText
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// load new search results
container.innerHTML = html.substr(0, n);
}
statusMsg(status, true);
}
else
{
statusMsg("AJAX nor ready for Keys", false);
}
}
else
{
statusMsg("AJAX request unavailable for Keys", false);
}
}
else // clear out the
{
clearDiv(container);
statusMsg("Table of Keys in Notes Closed", false);
}
}
function statusMsg(message, bool)
{
if(bool)
{
var t = getTick() - window.startTick;
message += " [ " + t + " ms.]";
}
T$("status").value = message;
showLoader(false);
}
function showPrint(field)
{
T$("printLine").style.display = field.checked ? "block" : "none";
show_head_check();
}
function mOverName(field)
{
if(T$("search_body").rows.length > 2) // more than 1 contact
{
window.origHeaderText = field.innerHTML;
field.innerHTML = "Click Sort Last Name";
window.origCursor = field.style.cursor;
field.style.cursor = "pointer";
field.style.color = "red";
}
}
function mOutName(field)
{
if(T$("search_body").rows.length > 2) // more than 1 contact
{
field.innerHTML = window.origHeaderText;
field.style.cursor = window.origCursor;
field.style.color="black";
}
}
function flip() // reverse order of contacts in search table
{
var tBody = T$("search_body");
var rows = tBody.rows;
var lastIndex = rows.length - 1;
for(var i = 0; i < lastIndex; i += 2)
{
tBody.insertBefore(rows[lastIndex], rows[i]);
tBody.insertBefore(rows[lastIndex], rows[i]);
}
hilite();
statusMsg("Order of " + rows.length / 2 + " contacts flipped", false);
}
function hilite()
{
var rows = T$("search_body").rows;
for(var i = 0, j = 0; i < rows.length; i += 2, j++)
{
rows[i + 1].className = rows[i].className = (j & 1) == 1 ? "hilite" : "";
}
}
window.locationIndex = 0;
window.locationNames = Array("Click Sort City", "Click Sort State", "Click Sort Zip");
function mOverLocation(field)
{
if(T$("search_body").rows.length > 2) // more than 1 contact
{
window.origHeaderText = field.innerHTML;
field.innerHTML = window.locationNames[window.locationIndex];
window.origCursor = field.style.cursor;
field.style.cursor = "pointer";
field.style.color = "red";
}
}
function mOutLocation(field)
{
if(T$("search_body").rows.length > 2) // more than 1 contact
{
field.innerHTML = window.origHeaderText;
field.style.cursor = window.origCursor;
field.style.color="black";
}
}
function sortName(field) // sorts rows by last name
{
var tBody = T$("search_body");
var rows = tBody.rows;
var len = rows.length;
if(len > 2) // more than 1 contact
{
var addrArray = new Array();
for(var i = 0, j = 0; i < len; i += 2)
{
var id = rows[i].id;
var html = rows[i].cells[1].innerHTML.toLowerCase(); // break apart using as separator
var start = html.indexOf(" ") + " ".length;
html = html.substr(start);
addrArray[j] = html + " " + id; // append id with separator
j++;
}
addrArray.sort();
len = addrArray.length;
for(j = 0; j < len; j++) // remove all but id in sorted array
{
addrArray[j] = addrArray[j].substr(addrArray[j].lastIndexOf(" ") + (" ").length);
}
for(j = 0; j < len; j++)
{
var id1 = addrArray[j], id2 = "_" + id1;
tBody.appendChild(tBody.removeChild(T$(id1)));
tBody.appendChild(tBody.removeChild(T$(id2)));
}
hilite();
statusMsg(len + " Contacts sorted by Last Name", false);
}
}
function sortLocation(field)
{
var tBody = T$("search_body");
var rows = tBody.rows;
var len = rows.length;
if(len > 2) // more than 1 contact
{
var addrArray = new Array();
for(var i = 0, j = 0; i < len; i += 2)
{
var id = rows[i].id;
if(id.indexOf("ID") == 0 && id.length > 2 && (id = id.substr(2)) > 0)
{
var html = rows[i].cells[3].innerHTML.toLowerCase(); // break apart using as separator
if(window.locationIndex == 0)
{
var stop = html.indexOf(" ");
if(stop > 0 && html[stop -1] == ",") stop--;
html = html.substr(0, stop);
var message = "City";
}
else if(window.locationIndex == 1)
{
start = html.indexOf(" ") + (" ").length;
stop = html.indexOf(" ");
html = html.substr(start, stop - start);
var message = "State";
}
else if(window.locationIndex == 2)
{
start = html.indexOf(" ") + (" ").length;;
html = html.substr(start);
var message = "Zip";
}
message = len / 2 + " contacts sorted by " + message;
addrArray[j] = html + " " + id; // append id with separator
j++;
}
else
{
statusMsg("Sort aborted !", false);
return;
}
}
addrArray.sort();
len = addrArray.length;
for(j = 0; j < len; j++) // remove all but id in sorted array
{
addrArray[j] = addrArray[j].substr(addrArray[j].lastIndexOf(" ") + (" ").length);
}
for(j = 0; j < len; j++)
{
var id1 = "ID" + addrArray[j], id2 = "_" + id1;
tBody.insertBefore(T$(id2), rows[j * 2]);
tBody.insertBefore(T$(id1), T$(id2));
}
window.locationIndex = ++window.locationIndex %3;
field.innerHTML = window.locationNames[window.locationIndex];
hilite();
statusMsg(message, false);
}
}
function clearHilight()
{
// clear yellow background error indicators
for(var i = 0; i < formFieldNames.length; i++)
{
T$(formFieldNames[i]).style.backgroundColor = "";
}
}
function clearForm()
{
for(var i = 0; i < formFieldNames.length; i++)
{
T$(formFieldNames[i]).value = "";
}
}
function clearReset(field)
{
if(field.value == "Clear")
{
if(getFormValues().length > 0)
{
clearForm();
}
else if(T$("search_table") && T$("search_table").rows.length > 2)
{
if(window.confirm("All form entries are blank !\n\nClear the search table below instead?"))
{
clearDiv(T$("searchResults"));
show_head_check();
statusMsg("", false);
}
}
else
{
alert("All form entries are blank !");
}
}
else if(field.value == "Reset")
{
resetData()
statusMsg("Reset to original data", false);
}
clearHilight()
}
function clearDiv(container)
{
var id = container.id;
if(id == "searchResults") window.locationIndex = 0;
var newDiv = document.createElement("div");
newDiv.id = id;
container.parentNode.replaceChild(newDiv, container);
return newDiv;
}
function showLoader(bool)
{
T$("ajaxLoader").style.display = bool ? "inline" : "none";
}
function searchUpdate(field)
{
if(field.value == "Search" || field.value == "Find All")
{
if(field.value == "Find All")
{
if(window.xmlhttp || getXHR() && window.xmlhttp.readyState == 0)
{
window.xmlhttp.onreadystatechange = function(){}
window.xmlhttp.open("POST","contacts3Ajax.php", false); // synchrous request
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("numContacts=Count"); // blocks till Ajax response returned
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var count = Trim(window.xmlhttp.responseText);
var status = "There are " + count + " contacts in the database.";
statusMsg(status, true);
if(count > window.numToQueryShowAll && !confirm(status + "\n\nDo you really want to show them all ?"))
{
field.checked = false;
return;
}
}
else
{
statusMsg("AJAX not ready for Count Contacts", false);
}
}
else
{
statusMsg("AJAX request unavailable for Count Contacts", false);
}
}
var v = getFormValues();
if(v.length > 0 || field.value == "Find All")
{
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// clear out old search results
var container = T$("searchResults");
container = clearDiv(container);
// load new search results
container.innerHTML = html.substr(0, n);
// uncheck Show All checkbox if here because it was checked
if(field.value == "Find All")
{
field.checked = false;
}
show_head_check();
// put table at mid screen
if(T$("search_table"))
{
var y = findPosY(T$("search_table"));
window.scroll(0, y - getWindowHeight() / 2);
}
else
{
window.scroll(0, 0);
}
}
statusMsg(status, true);
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send(v + "searchUpdate=" + field.value);
}
else
{
statusMsg("AJAX request unavailable for Search or Find All", false);
}
}
else
{
alert("All form entries are blank !");
}
}
else if(field.value == "Update" && verify())
{
T$("query").style.backgroundColor = "";
T$("query").disabled = false;
var v= getFormValues();
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status line message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// get new table's html and add contact
html = html.substr(0, n);
replaceContact(window.rowID, html);
clearHilight();
clearForm();
window.rowID = "";
window.idNum = 0;
T$("undelete").style.display = window.undeleteAvailable ? "inline" : "none";
T$("searchUpdate").value = "Search";
T$("addCancel").value = "Add";
T$("clearReset").value = "Clear";
}
statusMsg(status, true);
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send(v + "idNum=" + window.idNum + "&searchUpdate=" + "Update");
}
else
{
statusMsg("AJAX request unavailable for Update", false);
}
}
}
function replaceContact(rowID, html)
{
if(html.length > 0)
{
if(T$(rowID)) // replace existing contact
{
// update contact info and make body IDs unique;
html = html.replace("search_body", "temp_body");
// load new table with updated contact into temp div
var body = T$$("body", document)[0];
var temp = body.appendChild(document.createElement("div"));
temp.innerHTML = html;
// get parent nodes of rows for both tables
var sb = T$("search_body");
var tb = T$("temp_body");
// update contact's second row including attributes
var row = T$("_" + rowID);
var newRow = tb.rows[1];
newRow.style.display = row.style.display;
sb.replaceChild(newRow, row);
// update contact's first row including attributes
row = T$(rowID);
newRow = tb.rows[0];
newRow.style.display = row.style.display;
sb.replaceChild(newRow, row);
// remove downloaded table
body.removeChild(temp);
}
else if(T$("search_body")) // place at head of existing table
{
// update contact info and make body IDs unique;
html = html.replace("search_body", "temp_body");
// load new table with updated contact into temp div
var body = T$$("body", document)[0];
var temp = body.appendChild(document.createElement("div"));
temp.innerHTML = html;
// get parent nodes of rows for both tables
var sb = T$("search_body");
var tb = T$("temp_body");
// insert dummy contact at top of table, 1-contact = 2-rows
if(sb.rows.length >= 2)
{
sb.insertBefore(document.createElement("tr"), sb.rows[0]);
sb.insertBefore(document.createElement("tr"), sb.rows[0]);
}
else // insert into an enpty table
{
sb.appendChild(document.createElement("tr"));
sb.appendChild(document.createElement("tr"));
}
// update contact's first row including attributes
var row = sb.rows[0];
var newRow = tb.rows[0];
sb.replaceChild(newRow, row);
// rows[1] drops down to rows[0] & update contact's 2nd
row = sb.rows[1];
newRow = tb.rows[0];
sb.replaceChild(newRow, row);
// remove downloaded table
body.removeChild(temp);
rowID = sb.rows[0].id;
}
else // insert as new table
{
T$("searchResults").innerHTML = html;
var sb = T$("search_body");
rowID = sb.rows[0].id;
}
// alternate white-green row hilight and then flash new contact
hilite();
show_head_check();
if(T$(rowID)) // put contact an mid screen
{
var y = findPosY(T$(rowID));
window.scroll(0, y - getWindowHeight() / 2);
}
flashContact(rowID);
}
}
function flashContact(rowID) // flash the contact row and its notes row that was just updated
{
if(T$(rowID))
{
killFlashing();
window.flashRow1 = T$(rowID);
window.flashRow2 = T$("_" + rowID);
window.origColor = window.flashRow1.style.color;
window.flashRow1.style.color =
window.flashRow2.style.color = "red";
window.nFlashed = 0;
window.int = window.setInterval(flashing, 250); // flash rew-white every half second
}
}
function flashing() // flash red-white 3 times
{
if(window.int)
{
if(++window.nFlashed > 4) // quit and restore original text color
{
killFlashing();
}
else // alternate text color
{
window.flashRow1.style.color =
window.flashRow2.style.color =
(nFlashed & 1) == 1 ? window.origColor : "red";
}
}
}
function killFlashing()
{
if(window.int)
{
window.int = window.clearInterval(window.int);
window.flashRow1.style.color =
window.flashRow2.style.color =
window.origColor;
window.origColor = "";
window.flashRow1 =
window.flashRow2 = null;
}
}
function verify()
{
clearHilight();
var highlightColor = "yellow";
var success = true;
var firstName = Trim(T$("firstName").value);
var lastName = Trim(T$("lastName").value);
var street = Trim(T$("street").value);
var city = Trim(T$("city").value);
var state = Trim(T$("state").value);
var zip = Trim(T$("zip").value);
var email = Trim(T$("email").value);
var phone = Trim(T$("phone").value);
var mobilePhone = Trim(T$("mobilePhone").value);
var notes = Trim(T$("notes").value);
var country = Trim(T$("country").value);
if((firstName+lastName+street+city+state+zip+email+phone+mobilePhone+notes+country).length == 0)
{
alert("All form entries are blank !");
success = false;
}
else
{
// regular expressions
var emailPattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
var zipPattern = /^\d{5}([\-]\d{4})?$/;
var phonePattern = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
// following includes Canada
var states = ["AB","BC","MB","NB","NL","NT","NS","NU","ON","PE","QC","SK","YT","AL","AK","AS","AZ","AR","CA","CO","CT","DE","DC","FM","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY"];
if(firstName.length == 0)
{
T$("firstName").style.backgroundColor = highlightColor;
success = false;
}
if(lastName.length == 0)
{
T$("lastName").style.backgroundColor = highlightColor;
success = false;
}
if(email.length > 0 && !emailPattern.test(email))
{
T$("email").style.backgroundColor = highlightColor;
success = false;
}
if(state != "")
{
var len = states.length;
state = state.toUpperCase();
for(var i = 0; i < len; i++)
{
if(state == states[i]) break;
}
if(i == len)
{
T$("state").style.backgroundColor = highlightColor;
success = false;
}
}
if(zip.length > 0 && !zipPattern.test(zip))
{
T$("zip").style.backgroundColor = highlightColor;
success = false;
}
if(phone.length > 0 && (!phonePattern.test(phone) || phone == mobilePhone))
{
T$("phone").style.backgroundColor = highlightColor;
success = false;
}
if(mobilePhone.length > 0 && (!phonePattern.test(mobilePhone) || mobilePhone == phone))
{
T$("mobilePhone").style.backgroundColor = highlightColor;
success = false;
}
if(!success && window.confirm("Errors are Hilighted !\n\nContinue Anyway ?"))
{
clearHilight();
success = true;
}
}
return success;
}
function deleteContact(rowID)
{
if(T$("searchUpdate").value == "Update" && window.rowID == rowID)
{
alert("Finish editing \"" + T$("firstName").value + " " + T$("lastName").value + "\" first!\n\nThen, Update or Cancel.");
}
else
{
var my_row = T$(rowID);
var my_cell = my_row.cells[1];
var name = my_cell.innerText || my_cell.textContent;
window.myRowID = rowID; // make global so it's available in call back function
if(window.confirm("Delete " + name + " ?"))
{
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
var tBody = T$("search_body");
tBody.removeChild(T$(myRowID));
tBody.removeChild(T$("_" + myRowID));
// show Undelete button
window.undeleteAvailable = true;
T$("undelete").style.display = "inline";
hilite();
show_head_check();
}
statusMsg(status, true);
}
}
closeUp();
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("deleteContact=" + rowID.substr(2)); // convert rowID => ID### to ###
}
else
{
statusMsg("AJAX request unavailable for Delete", false);
}
}
}
}
function addCancel(field)
{
if(field.value == "Add" && verify())
{
var v= getFormValues();
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// put new contact to top of table
replaceContact("", html.substr(0, n));
show_head_check();
}
statusMsg(status, true);
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send(v + "addCancel=" + "Add");
}
else
{
statusMsg("AJAX request unavailable for Add", false);
}
}
else if(field.value == "Cancel")
{
T$("query").style.backgroundColor = "";
T$("query").disabled = false;
clearHilight();
clearForm();
window.idNum = 0;
statusMsg("Edit canceled", false);
T$("searchUpdate").value = "Search";
T$("addCancel").value = "Add";
T$("clearReset").value = "Clear";
T$("undelete").style.display = window.undeleteAvailable ? "inline" : "none";
}
}
function undelete(field)
{
if(field.value == "Undelete")
{
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// put new contact to top of table
replaceContact("", html.substr(0, n));
// hide Undelete button
window.undeleteAvailable = false;
T$("undelete").style.display = "none";
show_head_check();
}
statusMsg(status, true);
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("undelete=Undelete");
}
else
{
statusMsg("AJAX request unavailable for Undelete", false);
}
}
}
function resetData()
{
for(var i = 0; i < formFieldNames.length; i++)
{
T$(formFieldNames[i]).value = formResetValues[i];
}
clearHilight();
}
function editContact(rowID)
{
// first close up any name search window
T$("query").style.backgroundColor = "#ece9d8";
T$("query").disabled = true;
closeUp();
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
for(var i = 0; i < formFieldNames.length - 1; i++)
{
formResetValues[i] = html.slice(html.indexOf("&" + formFieldNames[i] + "=") + ("&" + formFieldNames[i] + "=").length, html.indexOf("&" + formFieldNames[i + 1] + "="));
}
// this is notes which may contain = and &
formResetValues[i] = html.slice(html.indexOf("&" + formFieldNames[i] + "=") + ("&" + formFieldNames[i] + "=").length, html.indexOf("&message="));
resetData();
T$("searchUpdate").value = "Update";
T$("addCancel").value = "Cancel";
T$("clearReset").value = "Reset";
T$("undelete").style.display = "none";
}
statusMsg(status, true);
window.scroll(0,0); // scroll to top of page to edit
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.idNum = rowID.substr(2); // save as global for Update
window.xmlhttp.send("editContact=" + window.idNum);
window.rowID = rowID;
}
else
{
statusMsg("AJAX request unavailable for Edit", false);
}
}
function print(field)
{
var checkContact = document.getElementsByName("checkContact");
for(var query = "", i = 0, count = 0; i < checkContact.length; i++)
{
if(checkContact[i].checked)
{
query += ("&idNum" + count + "=" + checkContact[i].value);
count++;
}
}
if(count > 0)
{
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// number of label files
var nFiles = "nFiles=";
var start = html.indexOf(nFiles, 0);
start += nFiles.length;
var stop = html.indexOf("&", start);
var len = stop - start;
nFiles = html.substr(start, len);
for(var file = "file=", i = 0; i < nFiles; i++)
{
start = html.indexOf(file, stop);
start += file.length;
stop = html.indexOf("&", start);
len = stop - start
// open server label files
window.open(html.substr(start, len));
}
// Ajax request to delete server label files
if(!window.xmlhttp) window.xmlhttp = getXMLHttpRequest();
window.xmlhttp.onreadystatechange = function(){}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.xmlhttp.send("unlinkLabels=Yes");
}
statusMsg(status, true);
}
}
query += ("&numContacts=" + count);
query += ("&startRow=" + T$("startRow").value);
query += ("&startCol=" + T$("startCol").value);
query += ("&font=" + T$("font").value);
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("print=" + field.value + query);
statusMsg("Printing labels. Please wait!", false);
}
else
{
statusMsg("AJAX request unavailable for Print Labels", false);
}
}
else
{
alert("Nothing checked for labels");
}
}
function showNotes(rowID) // open notes row for contact
{
var row = T$("_" + rowID);
if(row) // toggle visible state
{
row.style.display = row.style.display == 'none' ? '' : 'none';
}
}
function allCheck(bool)
{
var checkContact = document.getElementsByName("checkContact");
for(var i = 0; i < checkContact.length; i++)
{
checkContact[i].checked = bool;
}
}
function show_head_check()
{
var bShowPrint = T$("showPrint").checked;
var bIsTable = T$("search_table") ? true : false;
var nRows = bIsTable ? (T$("search_table").rows.length - 2) / 2: 0;
if(bIsTable)
{
var checkContact = document.getElementsByName("checkContact");
for(var i = 0; i < checkContact.length; i++)
{
checkContact[i].style.display = bShowPrint ? "inline" : "none";
}
T$("upper_head").style.display = nRows > 0 ? "" : "none";
T$("lower_head").style.display = nRows > window.numToDisplayFooter ? "" : "none";
}
T$("upper_check").style.display = bShowPrint && nRows > 0 ? "block" : "none";
T$("lower_check").style.display = bShowPrint && nRows > window.numToDisplayFooter ? "block" : "none";
}
function putPersistanceInCookie(cookieName) // persistent settings put into cookie
{
var query = "";
query = insertInQuery(query, "StartRow", T$("startRow").value);
query = insertInQuery(query, "StartCol", T$("startCol").value);
query = insertInQuery(query, "Font", T$("font").value);
query = insertInQuery(query, "ShowPrint", T$("showPrint").checked ? "Checked" : "Unchecked");
query = insertInQuery(query, "ShowKeys", T$("showKeys").checked ? "Checked" : "Unchecked");
if(T$("search_body")) // get the IDs for contacts in results table
{
var rows = T$("search_body").rows;
if(rows.length >= 2) // each contact has 2 rows
{
for(var n = rows.length, ids = "", i = 0; i < n; i += 2)
{
ids += rows[i].id;
}
query = insertInQuery(query, "RestoreContacts", ids);
}
}
query = insertInQuery(query, "ScrollPos", getScrollX() + "," + getScrollY());
setCookie(cookieName, query, window.cookieDaysExpire);
}
function insertInQuery(query, name, value) // appends or replaces Name=Value pair in query string
{
var start = query.indexOf(name);
if(start >= 0) // replace value already in query
{
var end = query.indexOf("&", start);
if(end < 0) end = query.length;
return query.slice(0, start) + name + "=" + value + query.slice(end, query.length);
}
else // append to query
{
if(query.length > 0 && query.charAt(query.length - 1) != "&") // append to end of query
{
query += "&";
}
query += (name + "=" + value);
}
return query;
}
function setPersistanceFromCookie(name) // persistent settings obtained from cookie
{
var query = getCookie(name);
var value = "";
if(value = getQueryValue(query, "StartRow"))
{
T$("startRow").value = value;
}
if(value = getQueryValue(query, "StartCol"))
{
T$("startCol").value = value;
}
if(value = getQueryValue(query, "Font"))
{
T$("font").value = value;
}
if(value = getQueryValue(query, "ShowPrint"))
{
T$("showPrint").checked = value == "Checked" ? true : false;
showPrint(T$("showPrint"));
}
if(value = getQueryValue(query, "ShowKeys"))
{
T$("showKeys").checked = value == "Checked" ? true : false;
show_keys(T$("showKeys"));
}
// restore previous results table
if(value = getQueryValue(query, "RestoreContacts"))
{
if(window.xmlhttp || getXHR() && window.xmlhttp.readyState == 0)
{
window.xmlhttp.onreadystatechange = function(){}
window.xmlhttp.open("POST","contacts3Ajax.php", false); // synchronous request
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("restoreContacts=" + value); // blocks till Ajax result recieved
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
// clear out old search results
var container = T$("searchResults");
container = clearDiv(container);
// make invisible and load new search results
html = html.substr(0, n);
html = html.replace(/'search_table'>/, "'search_table' style='display:none;'>");
container.innerHTML = html;
// sort contacts into saved order (mysql puts them in idNum order)
value = value.replace(/ID/g, ",ID"); // add "," as deliminator between each ID###
value = value.substr(1); // remove first deliminator
var idArray = value.split(",");
for(var body = T$("search_body"), i = 0; i < idArray.length; i++)
{
if(T$(idArray[i])) // may have been deleted between being saved and now
{
body.appendChild(body.removeChild(T$(idArray[i])));
body.appendChild(body.removeChild(T$("_" + idArray[i])));
}
}
hilite();
T$("search_table").style.display = "";
show_head_check();
}
statusMsg(status, true);
}
else
{
statusMsg("AJAX not ready for Restore Contacts", false);
}
}
else
{
statusMsg("AJAX request unavailable for Restore Contacts", false);
}
}
if(value = getQueryValue(query, "ScrollPos"))
{
var pos = value.split(",");
window.scroll(pos[0], pos[1]);
}
}
function getQueryValue(query, name) // return null or the "value" of "name" in query="name1=value1&name2=value2&...."
{
name += "=";
var offset = query.indexOf(name);
if(offset >= 0)
{
offset += name.length;
var end = query.indexOf("&", offset);
if(end < 0) end = query.length;
return query.slice(offset, end);
}
return null;
}
// --------------------- quick search by first and last name -------------------
function closeUp()
{
T$("results").style.display = "none";
T$("results").innerHTML = "";
T$("query").value = "";
}
function clickContact(field)
{
if(field.id) // not previously clicked
{
window.rowID = field.id;
var name = field.innerText || field.textContent;
// change ID and color to indicate already clicked on
if(T$$("div", field.parentNode).length <= 2)
{
closeUp();
}
else
{
field.style.color = "green";
field.style.cursor = "";
field.id = "";
}
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = Trim(window.xmlhttp.responseText);
var err = "error=";
var n = html.indexOf(err);
if(n >= 0)
{
var status = html.substr(n + err.length);
}
else
{
// extract status message
var message = "&message=";
n = html.indexOf(message);
var status = html.substr(n + message.length);
replaceContact(window.rowID, html.substr(0, n));
show_head_check();
}
statusMsg(status, true);
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("QuickID=" + window.rowID.substr(2));
}
else
{
statusMsg("AJAX request unavailable for Click Contact", false);
}
}
}
function queryChange(field)
{
if(T$("searchUpdate").value == "Search")
{
var query = Trim(T$("query").value);
if(query.length >= 2)
{
if(window.xmlhttp || getXHR())
{
window.xmlhttp.onreadystatechange = function()
{
if(window.xmlhttp.readyState == 4 && window.xmlhttp.status == 200)
{
var html = window.xmlhttp.responseText;
if(html.indexOf("
= 0) // contacts to display
{
// get lower bottom corner of query window
var query = T$("query");
var h = query.offsetHeight;
var x = findPosX(query);
var y = findPosY(query) + h;
// position results window
just below query window
var results = T$("results");
results.style.top = y + "px";
results.style.left = x + "px";
results.style.display = "";
results.innerHTML = html;
}
else // no contacts found
{
T$("results").style.display = "none";
}
statusMsg("Searching for contacts", true);
}
}
window.xmlhttp.open("POST","contacts3Ajax.php", true);
window.xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
window.startTick = getTick();
showLoader(true);
window.xmlhttp.send("Query=" + query);
}
else
{
statusMsg("AJAX request unavailable for Name Search", false);
}
}
else
{
T$("results").style.display = "none";
}
}
}
function mOverSearch(field)
{
if(field.id) // already clicked on ?
{
field.style.color = "red";
field.style.cursor = "pointer";
}
}
function mOutSearch(field)
{
if(field.id) // already clicked on ?
{
field.style.color = "";
field.style.cursor = "";
}
}
// --------------------- general utility functions -------------------
function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}
function LTrim(value) // Removes leading whitespaces characters(\r \n \t " ")
{
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
function RTrim(value) // Removes ending whitespaces
{
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
function Trim(value) // Removes leading and ending whitespaces
{
return LTrim(RTrim(value));
}
function setCookie(c_name, value, expire_days)
{
var date = new Date();
date.setTime(date.getTime() + (expire_days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = c_name + "=" + escape(value) + expires;
}
function getCookie(c_name)
{
if(document.cookie.length > 0)
{
c_start = document.cookie.indexOf(c_name + "=");
if(c_start != -1)
{
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if(c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
function getTick()
{
return new Date().getTime();
}
function ignoreCr(event)
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
return keyCode == 13 ? false : true;
}
function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
{
while(1)
{
curtop += obj.offsetTop;
if(!obj.offsetParent) break;
obj = obj.offsetParent;
}
}
else if(obj.y)
{
curtop += obj.y;
}
return curtop;
}
function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
{
while(1)
{
curleft += obj.offsetLeft;
if(!obj.offsetParent) break;
obj = obj.offsetParent;
}
}
else if(obj.x)
{
curleft += obj.x;
}
return curleft;
}
function getWindowHeight()
{
var windowHeight = 0;
if(typeof(window.innerHeight) == 'number')
{
windowHeight = window.innerHeight;
}
else if(document.documentElement && document.documentElement.clientHeight)
{
windowHeight = document.documentElement.clientHeight;
}
else if(document.body && document.body.clientHeight)
{
windowHeight = document.body.clientHeight;
}
return windowHeight;
}
function getScrollY() // return vertical scroll position
{
var scrOfY = 0;
if(typeof(window.pageYOffset) == 'number') // Netscape compliant
{
scrOfY = window.pageYOffset;
}
else if(document.body && document.body.scrollTop) // DOM compliant
{
scrOfY = document.body.scrollTop;
}
else if(document.documentElement && document.documentElement.scrollTop) // IE6 standards compliant mode
{
scrOfY = document.documentElement.scrollTop;
}
return scrOfY;
}
function getScrollX()
{
var scrOfX = 0;
if(typeof(window.pageXOffset ) == 'number') //Netscape compliant
{
scrOfX = window.pageXOffset;
}
else if(document.body && document.body.scrollLeft) //DOM compliant
{
scrOfX = document.body.scrollLeft;
}
else if(document.documentElement && document.documentElement.scrollLeft) //IE6 standards compliant mode
{
scrOfX = document.documentElement.scrollLeft;
}
return scrOfX;
}