/*
 * $Id:$
 *
 * Javascript functions to make handling forms an easier task
 */
    

/*
 * Given a form, take it's values and convert them to a query '
 * string.
 */
function formToQueryString(form) {
    var qs = "";
    var sep = "";
    for(keys in form.elements) {
        var e = form.elements[k];
        var value = e.type == 'select' ? e.options[e.selectedIndex].value : e.value;
        if(value != "") {
            qs += sep;
            qs += e.name + "=" + escape(value);
            sep = "&";
        }
    }
}

/* 
 * Given an element id, make the field into one that shows instructions in it,
 * until it is clicked.
 */
function setupInfoField(elt, disabledFields, onchange) {
    var on = function(elt) {
        $(elt).style.color = '#000000';
        $(elt).style.fontStyle = 'normal';
        disabledFields && disabledFields.each(function(f) {
                                                  $(f).disabled = false;
                                              });
    };
    var off = function(elt) {
        $(elt).style.color = '#000000';
        $(elt).style.fontStyle = 'italic';
        disabledFields && disabledFields.each(function(f) {
                                                  $(f).disabled = true;
                                              });
    }

    var infoText = $(elt).value;
    off(elt);

    $(elt).onchangeHandler = onchange;
    $(elt).onfocus = function() {
        if(this.value == infoText) {
            on(elt)
            this.value = '';
        }
    }

    $(elt).onblur = function() {
        if(this.value == '') {
            off(elt);
            this.value = infoText;
        } else {
            if(this.onchangeHandler) {
                this.onchangeHandler();
            }
        }
    }

    $(elt).realValue = function() {
        if(this.value == infoText) {
            return "";
        } else {
            return this.value.trim();
        }
    }

    $(elt).setValue = function(value) {
        if(value) {
            on(elt);
            this.value = value;
        }
    }
}

/*
 * When this function is invoked, it will click a button on the form.
 */
function clickButtonOnEnter(src, dest) {
    $(src).onkeydown = function(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            $(dest).click();
            $(src).blur();
            return true;
        }
    };
}

/*
 * Get the currently selected value from a selected value
 */
function selectedValue(id, emptyValue) {
    var opt = $(id).options[$(id).selectedIndex];
    if(emptyValue != undefined && (opt.value == emptyValue || opt.text == emptyValue)) {
        return false;
    } else {
        return opt.value ? opt.value : opt.text;
    }
}

function selectedDisplayValue(id, emptyValue) {
    var opt = $(id).options[$(id).selectedIndex];
    if(opt.value == emptyValue || opt.text == emptyValue) {
        return false;
    } else {
        return opt.text;
    }
}

/*
 * Select the right option given a value
 */
function selectOptionByValue(elt, value) {
    if(value) {
        $A($(elt).options).each(function(o) {
                                    if(o.value == value || o.text == value) {
                                        o.selected = true;
                                    }
                                });
    }
}

//
// Extract all the elements that have been filled in, producing
// a map in the form: {name0: elt0, name1: elt1, ... }
//
function filledInFormValues(form) {
    var filledIn = {};
    $A(form.elements).each(function(elt)
                       {
                           if(elt.type == 'text' && elt.value.trim() != '') {
                               filledIn[elt.name] = elt;
                           } else if(elt.type == 'radio' && elt.checked) {
                               filledIn[elt.name] = elt;
                           } else if(elt.type == 'checkbox' && elt.checked) {
                               if(!filledIn[elt.name]) {
                                   filledIn[elt.name] = [];
                               }
                               filledIn[elt.name].push(elt);
                           }
                       });
    return filledIn;
}

//
// Setup a process that will take users through
// each step of the process
function FormGuide() {
    this.chain = null;
    this.current = null;
    this.lastElt = null;
}

FormGuide.prototype.addAll = function(elts) {
    var g = this;
    elts.each(function(e) {
                  g.add(e);
              });
}

FormGuide.prototype.add = function(elt) {
    if(this.chain != null) {
        this.lastElt.nextFormElement = $(elt);
        this.lastElt = $(elt);
    } else {
        this.chain = $(elt);
        this.lastElt = $(elt);
    }
}

FormGuide.prototype.complete = function(justFinished) {
    if(this.current && this.current != justFinished) {
        this.current.style.backgroundColor = this.current.preferredBgColor ? 
            this.current.preferredBgColor : "transparent";
    }
    justFinished.style.backgroundColor = justFinished.preferredBgColor ? 
        justFinished.preferredBgColor : "transparent";
    this.current = $(justFinished).nextFormElement;
    this.start(this.current);
}

FormGuide.prototype.start = function(elt) {
    this.current = $(elt);
    if(this.current) {
        this.current.style.backgroundColor = '#FFFF80';
    }
}


