/*
Form Filler SCRIPT
This script will allow you to automatically remind your users that values they typed in your forms, the main advantage is that they don't need to have autocomplete browser feature enabled, and it's so easy to install, you only need to paste ONE LINE of code in the pages you want to install this script!!!

Features:
-Supports RC4 Encryption of data, so secure data like passwords or email will be protected using this encryption, also the encryption output was modified so it is made of only of letters and numbers, no by binary data that may cause bugs in browsers
-Fully customizable, check out the CUSTOMIZABLE AREA in this script
-Saves all forms values in a separate cookie for each name, so the user only needs to have cookies enabled to allow saving the forms
-Supports textboxes, textareas, radio buttons, checkboxes, and dropdown lists!!!
-It is compatible with IE4+ and NS4+ or newer browsers

Install Instructions:
To install this script in your pages copy this script to the folder where your web pages are and paste the following line of code on every page where you want to save the forms in there

<script language="JavaScript" src="FormFiller.js"></script>

And that's all!, of course, if you want to customize the way in which the script saves the form check out the CUSTOMIZABLE AREA below, read the comments on what is each variable, and change it to the values you want at runtime in the page or change them in the script so the values are for all pages.

*/

//************************************************************
//CUSTOMIZABLE AREA

//Default is that form saved values will be deleted in 3 months if the user has not revisited the page again
var timexpire=3;

//Save mode indicates when the form is saved, here are the values that can be used
//0 - Save when the page is going to unload (when it's closed, when a form is submitted, etc)
//1 - Save at every X seconds, you can specify the interval in the "saveinterval" value below
//2 - Manual Save, if you choose this, then you will need to use the "FormReminderSave" function to save all the forms used on the page
var savemode=1;

//Interval number in seconds to save the form fields, this will be used if "savemode" is set to 1
var saveinterval=5;

//END CUSTOMIZABLE AREA

//************************************************************

//This variable contains the field names that will not be saved, fields can be added using the "ExcludeField(fieldname)" function
var fieldsexcluded="";


//Cookie handling functions

function CreateCookie(name, value, monthexpire){
var edt=new Date();
edt.setMonth(edt.getMonth()+monthexpire);
edt=edt.toGMTString();
document.cookie=name+"="+escape(value)+";expires="+edt;
}

function OpenCookie(cookieName){
thisCookie=document.cookie.split("; ");
for(i=0;i<thisCookie.length;i++){
if(cookieName==thisCookie[i].split("=")[0]){
return unescape(thisCookie[i].split("=")[1]);
}
 }
return "";
}

function DeleteCookie(cookieName){
if(ExistCookie(cookieName)){
var ExpireDate=new Date();
ExpireDate.setDate(ExpireDate.getDate()-1);
ExpireDate=ExpireDate.toGMTString();
document.cookie=cookieName+"=;expires="+ExpireDate;
}
 }

function ExistCookie(cookieName){
thisCookie=document.cookie.split("; ");
for(i=0;i<thisCookie.length;i++){
if(cookieName==thisCookie[i].split("=")[0]){
return true;
}
 }
return false;
}

//RC4 Encryption and Decryption code

var sb=new Array();

//Returns a character based on its ASCII code
function chr(n){
var hexvalue=n.toString(16);
if(hexvalue.length==1) hexvalue="0" + hexvalue;
return unescape("%" + hexvalue);
}

//This functions inits the RC4 encryption and decryption table with a key or password
function InitRC4(key){
var i, j, k, kp=new Array();
k=-1;
for(j=0;j<=255;j++){
k++;
if(k>key.length-1) k=0;
kp[j]=key.charCodeAt(k);
sb[j]=j;
}
k=-1;
for(j=0;j<=255;j++){
k=(k+sb[j]+kp[j])%256;
i=sb[j];
sb[j]=sb[k];
sb[k]=i;
}
 }

//This function encrypts text using RC4 encryption and returns output made of numbers and letters, and it doubles the text size
function RC4Encrypt(txt){
var i=0, j=0, k=0, tmp, cnt, nwc, enctxt="";
for(cnt=0;cnt<txt.length;cnt++){
i=(i+1)%256;
j=(j+sb[i])%256;
tmp=sb[i];
sb[i]=sb[j];
sb[j]=tmp;
k=sb[(sb[i]+sb[j])%256];
nwc=txt.charCodeAt(cnt)^k;
nwc=nwc.toString(16);
if(nwc.length==1) nwc="0" + nwc;
enctxt+=nwc;
}
return enctxt;
}

//Decrypts text encrypted with the RC4 encryption function
function RC4Decrypt(txt){
var i=0, j=0, k=0, tmp, cnt, nwc, enctxt="";
for(cnt=0;cnt<txt.length;cnt+=2){
i=(i+1)%256;
j=(j+sb[i])%256;
tmp=sb[i];
sb[i]=sb[j];
sb[j]=tmp;
k=sb[(sb[i]+sb[j])%256];
nwc=parseInt(txt.substring(cnt, cnt+2), 16)^k;
enctxt+=chr(nwc);
}
return enctxt;
}

//This function gets the filename of URL or path
function GetFilename(file){
var filename=file.substring(file.lastIndexOf("/")+1, file.length);
return filename;
}

//This function will add a fieldname to the don't save list
function ExcludeField(fieldname){
fieldsexcluded+=fieldname + ":";
}

//This will clear the exclude field list
function ClearExcludeList(){
fieldsexcluded="";
}

function FormReminderSave(){
var docforms=document.forms, currform, currfield;
var cookiecontent="";
var urlname=GetFilename(self.location.href);
var fieldcnt=0, lastfieldname="", fieldarrayinfo="";
InitRC4(urlname);
for(i=0;i<docforms.length;i++){
currform=docforms[i].elements;
for(j=0;j<currform.length;j++){
currfield=currform[j];
currtype=currfield.type;
if(currfield.name!=lastfieldname){
fieldcnt=0;
lastfieldname=currfield.name;
}
else{
fieldcnt++;
}
fieldarrayinfo=fieldcnt.toString(16);
if(fieldsexcluded.indexOf(currfield.name)==-1){
if(currtype=="text" || currtype=="textarea"){
InitRC4(urlname);
cookiecontent+="t:" + currfield.name + ":" + RC4Encrypt(currfield.value) + ":" + fieldarrayinfo + ";";
}
if(currtype.indexOf("select")!=-1){
cookiecontent+="s:" + currfield.name + ":" + currfield.options.selectedIndex.toString(16) + ":" + fieldarrayinfo + ";";
}
if(currtype.indexOf("radio")!=-1){
cookiecontent+="r:" + currfield.name + ":" + ((currfield.checked)?"c":"n") + ":" + fieldarrayinfo + ";";
}
if(currtype.indexOf("checkbox")!=-1){
cookiecontent+="c:" + currfield.name + ":" + ((currfield.checked)?"c":"n") + ":" + fieldarrayinfo + ";";
}
}
}
cookiecontent+="; ";
}
CreateCookie(urlname, cookiecontent, timexpire); 
}

function FormReminderDelete(){
var urlname=GetFilename(self.location.href);
DeleteCookie(urlname);
window.location=self.location;
}

function FormReminderLoad(){
var urlname=GetFilename(self.location.href);
if(!ExistCookie(urlname)) return;
var cookiecontent=OpenCookie(urlname).split("; ");
var currform, currfield, formfield;
for(i=0;i<cookiecontent.length-1;i++){
currform=cookiecontent[i].split(";");
for(j=0;j<currform.length-1;j++){
currfield=currform[j].split(":");
formfield=eval("document.forms[" + i + "]." + currfield[1]);
if(new String(formfield.length)!="undefined"){
if(new String(formfield.options)=="undefined"){
formfield=eval("document.forms[" + i + "]." + currfield[1] + "[" + parseInt(currfield[3], 16) + "]");
}
 }
switch(currfield[0]){
case "t":
InitRC4(urlname);
formfield.value=RC4Decrypt(currfield[2]);
break;
case "s":
formfield.options.selectedIndex=parseInt(currfield[2], 16);
break;
case "r":
if(currfield[2]=="c") formfield.checked=true;
break;
case "c":
if(currfield[2]=="c") formfield.checked=true;
break;
}
}
}
}

window.onload=FormReminderLoad;

if(savemode==0){
window.onunload=FormReminderSave;
}
if(savemode==1){
setInterval("FormReminderSave()", saveinterval*1000);
}


