﻿function getCookie(Name) {
    var re = new RegExp(Name + "=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
        return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
}

function setCookie(name, value, days) {
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = expireDate.setDate(expireDate.getDate() + parseInt(days))
    document.cookie = name + "=" + value + " ; path=/";
}

function getindex(list, value) {
    for (i = 0; i < document.getElementById(list).length; i++) {
        if (document.getElementById(list).options[i].value == value) return i;
    }
    return 0;
}

function rememberForm(formid) { //Main remember form values object
    this.formref = document.getElementById(formid) 
    this.cookiename = formid
    this.persistdays = 0 //days to persist form values
    this.fields = new Array()
    this.cookiestr = ""
    this.recallsave = false
    var forminstance = this
    rememberForm.dotask(window, function() { forminstance.savevalues() }, "beforeunload") //save form values onsubmit
    rememberForm.dotask(window, function() { forminstance.recallvalues() }, "load") //populate form with saved values onload (body)
}

rememberForm.prototype.getfield = function(attr) { //get form field based on its ID or name attribute
    var fieldref = document.getElementById(attr) 
    return fieldref
}

rememberForm.prototype.persistfields = function() { //get form fields to persist values for
    this.fields = new Array()
    for (var i = 0; i < arguments.length; i++) {
        this.fields[i] = this.getfield(arguments[i])
        this.fields[i].fname = arguments[i] //store name or id of field in custom property
    }
}

function setField(name, field, value) {
    var cookie = getCookie(name);
    if (cookie == "") setCookie(name, field + ":" + value);
    else if (cookie.indexOf(field + ":") == -1) setCookie(name, cookie + "#" + field + ":" + value);
    else setCookie(name, cookie.replace(field + ":" + getFieldValue(name, field), field + ":" + value), 0);
}

function getFieldValue(name, field) {
    var cookie = getCookie(name);  
    var therest = cookie.substring(cookie.indexOf(field + ":") + field.length + 1, cookie.length);
    return therest.substring(0,therest.indexOf("#",0));
}

rememberForm.prototype.savevalues = function() { //get form values and store in cookie
    if (!this.recallsave) {
        for (var i = 0; i < this.fields.length; i++) {
            if (this.fields[i].type == "text")
                this.cookiestr += this.fields[i].fname + ":" + escape(this.fields[i].value) + "#";
            else if (this.fields[i].type == "select-one")
                this.cookiestr += this.fields[i].fname + ":" + this.fields[i].options[this.fields[i].selectedIndex].value + "#";
            else if (this.fields[i].type == "radio")
                this.cookiestr += this.fields[i].fname + ":" + this.fields[i].checked + "#";
        }

        this.cookiestr += "standard:" + document.getElementById("standard").style.display + "#";
        this.cookiestr += "advanced:" + document.getElementById("advanced").style.display + "#";
        this.cookiestr += "saved:" + document.getElementById("saved").style.display + "#";

        this.cookiestr = this.cookiestr.substr(0, this.cookiestr.length - 1) + ";";

        setCookie(this.cookiename, this.cookiestr, this.persistdays);
    }
    else {
        this.recallsave = false;
    }
}

rememberForm.prototype.recallvalues = function() { //populate form with saved values
    var cookievalue = getCookie(this.cookiename);
    if (cookievalue != "") { //parse cookie, where cookie looks like: field1:value1#field2:value2...
        var cookievaluepair = cookievalue.split("#")
        for (var i = 0; i < cookievaluepair.length; i++) {
            if (cookievaluepair[i].split(":")[0] == "standard" || cookievaluepair[i].split(":")[0] == "advanced" || cookievaluepair[i].split(":")[0] == "saved") {
                if (this.getfield(cookievaluepair[i].split(":")[0]) != null) this.getfield(cookievaluepair[i].split(":")[0]).style.display = cookievaluepair[i].split(":")[1];
            }
            else if (this.getfield(cookievaluepair[i].split(":")[0]) != null) {
                if (this.getfield(cookievaluepair[i].split(":")[0]).type == "text")
                    this.getfield(cookievaluepair[i].split(":")[0]).value = unescape(cookievaluepair[i].split(":")[1]);
                else if (this.getfield(cookievaluepair[i].split(":")[0]).type == "select-one")
                    this.getfield(cookievaluepair[i].split(":")[0]).selectedIndex = getindex(cookievaluepair[i].split(":")[0], cookievaluepair[i].split(":")[1]);
                else if (this.getfield(cookievaluepair[i].split(":")[0]).type == "radio")
                    this.getfield(cookievaluepair[i].split(":")[0]).checked = cookievaluepair[i].split(":")[1] == "true" ? true : false;
            }
        }
    }
}

//Call this function if you wish to clear the user's cookie of any saved values for this form instantly
rememberForm.prototype.clearcookie = function() {
    for (var i = 0; i < this.fields.length; i++) {
        if (this.fields[i].type == "text")
            this.fields[i].value = ""
        else if (this.fields[i].type == "select-one")
            this.fields[i].selectedIndex = 0
    }
    setCookie(this.cookiename, "", -1)
}

rememberForm.dotask = function(target, functionref, tasktype) {
    var tasktype = (window.addEventListener) ? tasktype : "on" + tasktype
    if (target.addEventListener)
        target.addEventListener(tasktype, functionref, false)
    else if (target.attachEvent)
        target.attachEvent(tasktype, functionref)
}

rememberForm.removetask = function(target, functionref, tasktype) {
    var tasktype = (window.removeEventListener) ? tasktype : "on" + tasktype
    if (target.removeEventListener)
        target.removeEventListener(tasktype, functionref, false)
    else if (target.detachEvent)
        target.detachEvent(tasktype, functionref)
}

rememberForm.prototype.loadsearch = function(cookie) {
    setCookie(this.cookiename,cookie,this.persistdays)
    this.recallvalues()
    this.recallsave = true
}

