// $Id$
//
// Store and restore a form from a named cookie.
//
// Note that if there is a form with a property 'ignore'
// set to true, it will ignore save/restore of that form.
// The same holds true for individual form elements as well.
//

function SaveToCookie(aForm, aCookieName, aDocument, aSecureBoolean)
{
    var savec = new CCookie(aDocument, aCookieName, 0,
                            '/', null, aSecureBoolean);
    if (aForm.ignore) {
        savec.removeFromDocument();
        return;
    }

    var formitr = new CFormIterator(aForm);
    var formvisitor = new CFormVisitor(savec, true);
    formitr.iterate(formvisitor);
    savec.storeInDocument();
}

function LoadFromCookie(aForm, aCookieName, aDocument, aSecureBoolean)
{
    var loadc = new CCookie(aDocument, aCookieName, 0,
                            '/', null, aSecureBoolean);
    loadc.loadFromDocument();
    var formitr = new CFormIterator(aForm);
    var formvisitor = new CFormVisitor(loadc, false);
    formitr.iterate(formvisitor);
}

// This class is used to visit the form through the iterator.
function CFormVisitor(aCCookie, isSaveModeBoolean)
{
    this.m_ccookie = aCCookie;
    this.m_save=isSaveModeBoolean;
    return this;
}

function _CFormVisitor_radio(anElement, aForm)
{
    if (anElement.ignore) { return true; }
    if (this.m_save) {
        if (anElement.checked) {
            this.m_ccookie[aForm.name+'|rb|'+anElement.name+'|'+anElement.value]='1';
        }
    }
    else {
        if (this.m_ccookie[aForm.name+'|rb|'+anElement.name+'|'+anElement.value] == '1') {
            anElement.checked = true;
        }
    }
    return true;
}

function _CFormVisitor_checkbox(anElement, aForm)
{
    if (anElement.ignore) { return true; }

    if (this.m_save) {
        if (anElement.checked) {
            this.m_ccookie[aForm.name+'|cb|'+anElement.name+'|'+anElement.value]='1';
        }
    }
    else {
        if (this.m_ccookie[aForm.name+'|cb|'+anElement.name+'|'+anElement.value] == '1') {
            anElement.checked = true;
        }
    }
    return true;
}

function _CFormVisitor_select(anElement, aForm)
{
    if (anElement.ignore) { return true; }

    if (this.m_save) {
        for (var i=0; i<anElement.options.length; i++) {
            if (anElement.options[i].selected) {
                this.m_ccookie[aForm.name+'|op|'+anElement.name+'|'+anElement.options[i].value]='1';
            }
            else {
            // for safety's sake, zero out any old entries
                this.m_ccookie[aForm.name+'|op|'+anElement.name+'|'+anElement.options[i].value]=null;
            }
        }
    }
    else {
        for (var i=0; i<anElement.options.length; i++) {
            if (this.m_ccookie[aForm.name+'|op|'+anElement.name+'|'+anElement.options[i].value]=='1') {
                anElement.options[i].selected = true;
            }
        }
    }

    return true;
}

function _CFormVisitor_text(anElement, aForm)
{
    if (anElement.ignore) { return true; }

    if (this.m_save) {
        if (anElement.value) {
            this.m_ccookie[aForm.name+'|tx|'+anElement.name]=anElement.value;
        }
    }
    else {
        if (this.m_ccookie[aForm.name+'|tx|'+anElement.name]) {
            anElement.value = this.m_ccookie[aForm.name+'|tx|'+anElement.name];
        }
    }
    return true;
}
new CFormVisitor();
CFormVisitor.prototype.checkbox = _CFormVisitor_checkbox;
CFormVisitor.prototype.radio = _CFormVisitor_radio;
CFormVisitor.prototype['select-one'] = _CFormVisitor_select;
CFormVisitor.prototype['select-multiple']=_CFormVisitor_select;
CFormVisitor.prototype['text']=_CFormVisitor_text;
CFormVisitor.prototype['password']=_CFormVisitor_text;
// Don't store textarea's until the need arises.
// This is to prevent accidental storage of "read-only" sections
// of information like tnc, etc.
// Should I even bother to do this?
