//-- Original:  Joe Norman (joe@wdrealty.com)

var arrRecords = new Array();
var arrCookie = new Array();
var recCount = 0;
var strRecord="";
expireDate = new Date;
expireDate.setDate(expireDate.getDate()+365);
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();
   tmp= itemChange(document.frm1,'fuelcons','0','0','');
   }
if(document.cookie ==""){  // no cookie so create a new page as record 1 and save AC 18/08/07
   document.frm1.textfield4.value="Shoreham to Manston - example";
   document.frm1.textfield.value="Shoreham";
   document.frm1.textfield2.value="Manston";
   document.frm1.FIS9.value="125.3";
   document.frm1.FIS12.value="123.18";
   document.frm1.FIS15.value="125.4";
   document.frm1.FIS13.value="126.36";
   document.frm1.FIS16.value="119.92";
   document.frm1.from1.value="Shoreham";
   document.frm1.from2.value="Lydd";
   document.frm1.to1.value="Lydd";
   document.frm1.to2.value="Manston";
   document.frm1.trueTrack1.value="078";
   document.frm1.trueTrack2.value="040";
   document.frm1.windVel1.value="240/25"
   document.frm1.distance1.value="46";
   document.frm1.distance2.value="25";
   document.frm1.FIS.value="London";
   document.frm1.FIS2.value="Manston";
   document.frm1.textfield592.value="RIS (Director) 126.36";
   document.frm1.textfield59.value="(Lydd inf. 120.7)";
   document.frm1.vor.value="114.05";
   document.frm1.vor2.value="114.05";
   document.frm1.freq.value="124.6";
   document.frm1.freq2.value="126.36";
   document.frm1.plan.value="FL30";
   document.frm1.plan2.value="FL30";
   document.frm1.safe1.value="1800";
   document.frm1.safe2.value="1900";
   document.frm1.fuelcons.value="10";
   tmp=itemChange(document.frm1, "windVel", 1,1,99);
   tmp=setRec();
   } // end AC
}
function setRec() {
strRecord = "";
for(i = 0; i < document.frm1.elements.length; i++) {
strRecord = strRecord + document.frm1.elements[i].value + "|"; //AC
}
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";
   document.frm2.resetButton.click(); // call form 1 reset (coded in html) AC 16:08:03

   break;
case "CANCEL" :
   recCount = varTemp;
   document.frm2.add.value = "  NEW  ";

   currentRecord();
   break;
   }
}
function countRecords() {
document.frm2.actual.value = "Plan " + (recCount+1)+";  "+arrRecords.length+" saved plans";
document.frm2.textfield6.value=document.frm1.textfield4.value;
}
function delRec() {
arrRecords.splice(recCount,1);
navigate("previous");
setRec();
}
function currentRecord() {
if (arrRecords.length != "") {
strRecord = arrRecords[recCount];
currRecord = strRecord.split("|");  // AC
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:
   }

}

// Splice method Protype Function
// Peter Belesis, Internet.com
// http://www.dhtmlab.com/
 
if (!Array.prototype.splice) {
function array_splice(ind,cnt) { // index and record count
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();

