Can anybody help me with some javascript. I got this script off the web and edited as much as I can to suit my page, but I only know so much javascript. Can anybody help me? The problem is that there is an error that was with the original script that I still can't fix, you'll see it when you view the page. My second problem is that when its on its first entry and there are more then 1, the delete button will delete the one after the first one, and if there is only one entry, that entry won't delete at all... can somebody help me with my address book script?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Addresses</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
<SCRIPT LANGUAGE="JavaScript">
var arrRecords = new Array();
var arrCookie = new Array();
var recCount = 0;
var strRecord="";
expireDate = new Date;
expireDate.setDate(expireDate.getDate()+730);
function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i = 0; i < thisCookie.length; i++) {
if (cookieName == thisCookie[i].split("=")[0]) {
return thisCookie[i].split("=")[1];
}
}
return 0;
}
function loadCookie() {
if(document.cookie != "") {
arrRecords = cookieVal("Records").split(",");
currentRecord();
}
}
function setRec() {
strRecord = "";
for(i = 0; i < document.frm1.elements.length; i++) {
strRecord = strRecord + document.frm1.elements[i].value + ":";
}
arrRecords[recCount] = strRecord;
document.frm2.add.value = " NEW ";
document.cookie = "Records="+arrRecords+";expires=" + expireDate.toGMTString();
}
function newRec() {
switch (document.frm2.add.value) {
case " NEW " :
varTemp = recCount;
for(i = 0; i < document.frm1.elements.length; i++) {
document.frm1.elements[i].value = ""
}
recCount = arrRecords.length;
document.frm2.add.value = "CANCEL";
break;
case "CANCEL" :
recCount = varTemp;
document.frm2.add.value = " NEW ";
currentRecord();
break;
}
}
function countRecords() {
document.frm2.actual.value = "Record " + (recCount+1)+"; "+arrRecords.length+" saved records";
}
function delRec() {
arrRecords.splice(recCount,1);
navigate("previous");
setRec();
}
function currentRecord() {
if (arrRecords.length != "") {
strRecord = arrRecords[recCount];
currRecord = strRecord.split(":");
for(i = 0; i < document.frm1.elements.length; i++) {
document.frm1.elements[i].value = currRecord[i];
}
}
}
function navigate(control) {
switch (control) {
case "first" :
recCount = 0;
currentRecord();
document.frm2.add.value = " NEW ";
break;
case "last" :
recCount = arrRecords.length - 1;
currentRecord();
document.frm2.add.value = " NEW ";
break;
case "next" :
if (recCount < arrRecords.length - 1) {
recCount = recCount + 1;
currentRecord();
document.frm2.add.value = " NEW ";
}
break;
case "previous" :
if (recCount > 0) {
recCount = recCount - 1;
currentRecord();
}
document.frm2.add.value = " NEW ";
break;
default:
}
}
if (!Array.prototype.splice) {
function array_splice(ind,cnt) {
if (arguments.length == 0) return ind;
if (typeof ind != "number") ind = 0;
if (ind < 0) ind = Math.max(0,this.length + ind);
if (ind > this.length) {
if (arguments.length > 2) ind = this.length;
else return [];
}
if (arguments.length < 2) cnt = this.length-ind;
cnt = (typeof cnt == "number") ? Math.max(0,cnt) : 0;
removeArray = this.slice(ind,ind+cnt);
endArray = this.slice(ind+cnt);
this.length = ind;
for (var i = 2; i < arguments.length; i++) {
this[this.length] = arguments[i];
}
for(var i = 0; i < endArray.length; i++) {
this[this.length] = endArray[i];
}
return removeArray;
}
Array.prototype.splice = array_splice;
}
recCount = 0;
loadCookie();
countRecords();
</script>
</head>
<body>
<form name="frm1">
Name:
<input type="box" name="name" size="50" id="textbox">
Address:
<input type="box" name="address" size="50" id="textbox">
Phone:
<input type="box" name="phone" size="50" id="textbox">
Comments:
<input type="box" name="comment1" size="50" id="textbox">
<input type="box" name="comment2" size="50" id="textbox">
<input type="box" name="comment3" size="50" id="textbox">
<input type="box" name="comment4" size="50" id="textbox">
<input type="box" name="comment5" size="50" id="textbox">
<div id="who">
Starting date:
<input type="box" name="starttime" size="48" id="textbox2">
Who's working:
<input type="box" name="person" size="48" id="textbox2">
</div>
</form>
<form name="frm2">
<input type="button" name="first" value="|<< " onClick="navigate('first');countRecords()" id="caption1">
<input type="button" name="previous" value=" < " onClick="navigate('previous');countRecords()" id="caption2">
<input type="button" name="next" value=" > " onClick="navigate('next');countRecords()" id="caption3">
<input type="button" name="last" value=" >>|" onClick="navigate('last');countRecords()" id="caption4">
<input type="box" name="actual" size=23 id="record">
<input type="button" name="add" value=" NEW " onClick="newRec();countRecords()" id="caption5">
<input type="button" name="set" value="SAVE RECORD" onClick="setRec();countRecords()" id="caption6">
<input type="button" name="del" value="DELETE" onClick="delRec();countRecords()" id="caption7">
</form>
<p id="info">Insert the name, address, phone number, and any other information needed. You cannot delete the first entry, therefore, insert an entry and save over it.
</body>
</html>