

//this function is used to simulate the behaviour of a combo box
//input: formname   -> string containing the form name
//       textname   -> text field name in the form
//       selectname -> select field name in the form
//this callback must be called in the text field on a keyup event:
//onKeyUp="return changedText('FrmName', 'TextFieldName', 'SelectFieldName');"
function changedText(formname, textname, selectname) {
    var textref = eval("document." + formname + "." + textname);
    var selectref = eval("document." + formname + "." + selectname);
    var i = 0;
    var pos = 0;
    for (i=0; i<selectref.length; i++){
        if (selectref.multiple)
            selectref.options[i].selected=false;
        if ((textref.value <= selectref.options[i].text) && pos == 0)
            pos = i;
    }
    if (pos==0)
        pos = i;
    if (pos==selectref.length || (textref.value != selectref.options[pos].text && pos != 0))
        pos--;
    if (pos<selectref.length-1)
        pos++;
    selectref.options[pos].selected=true;
    return false;
}

//this function changes set the focus to a form field
//input: formname  -> string containing the form name
//       fieldname -> field that has to receive the focus
function setFocus(formname, fieldname, selected) {
    var fieldref = eval("document." + formname + "." + fieldname);
    if (selected)
        fieldref.select();
    else
        fieldref.focus();
    return false;
}

//this function set the values selected in inputfield in outputfield and in hiddenfield.
//In outputfield set the description of the selected field (the text value of an option tag)
//while in hiddenfield set the value of the selected field
//input: formname  -> string containing the form name
//       inputfield -> select field that has the selected values
//       outputfield -> text field where put the description of the selected values separated by "; "
//       hiddenfield -> hidden field where put the selected values separated by "; "
function setValue(formname, inputfield, outputfield, hiddenfield) {
    var inputref = eval("document." + formname + "." + inputfield);
    var outputref = eval("document." + formname + "." + outputfield);
    var hiddenref = eval("document." + formname + "." + hiddenfield);
    var value=outputref.value;
    for (var i=0; i<inputref.length; i++) {
        if (inputref.options[i].selected==true) {      //set description in outputref and value in hiddenref
            if (value == "") {
                value = inputref.options[i].text;
                hiddenref.value = inputref.options[i].value;
             } else {
                value += "; " + inputref.options[i].text;
                hiddenref.value += "; " + inputref.options[i].value;
            }
        }
    }
    //alert(hiddenref.value);
    outputref.value = value;
}

//TODO: documentare
function resetValue(formname, inputfield) {
    var inputref = eval("document." + formname + "." + inputfield);
    inputref.value = "";
}

//TODO: documentare
function submitForm(formname, action) {
    var formref = eval("document." + formname);
    //TODO: qui va fatto il check della form
    formref.action = action;
    formref.submit();
    return false;
}

//TODO: documentare
function selectAll(formname, checkboxfield, sel_desel) {
    var checkref = eval("document." + formname + "." + checkboxfield);
    for (var i=0; i<checkref.length; i++) {
        if (sel_desel)
            checkref[i].checked = true;
        else
            checkref[i].checked = false;
    }
    return false;
}

//trim method for strings
// create the prototype on the String object
String.prototype.trim = function() {

 // skip leading and trailing whitespace
 // and return everything in between
  return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");

}

//isEmpty method for strings. Check if a string is Empty or contains only spaces
String.prototype.isEmpty = function() {
    if (this.length == 0 || this.trim() == "")
        return true;
    else
        return false;
}

//check if all form fields are empty
function checkForm(formname) {
    var formref = eval("document." + formname);
    for (i=0; i<formref.elements.length; i++) {
        if (formref.elements[i].value.isEmpty() && formref.elements[i].name != "SubActivityCode") { //SubactivityCode e' un campo non obbligatorio per la tabella sottoattivitą
            alert("Inserire un valore nel campo " + formref.elements[i].name)
            formref.elements[i].focus();
            return false;
        }
    }
    return true;
}


/*
 *  DoubleSelect Object:
 *  In the first select I have all the elements and add or delete some of them to the second select
 */

//add the elemnts selected in the first select to the second if not already present
function DoubleSelect_add() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    
    for (i=0; i<selectref1.options.length; i++) {
        if (selectref1.options[i].selected) {
            inner_text = selectref1.options[i].text;
            sel_value = selectref1.options[i].value;
            found = 0;
            for (j=0; j<selectref2.options.length; j++) {
                if (sel_value == selectref2.options[j].value) {
                    found = 1;
                    break
                }
            }
            if (found == 0)
                selectref2.options[selectref2.options.length] = new Option(inner_text, sel_value, false, false);
        }
    }
}

//del the selected elements from the second select
function DoubleSelect_del() {
    var selectref = eval("document." + this.form + "." + this.select2);
    
    for (i=0; i<selectref.options.length; i++) {
        if (selectref.options[i].selected == true) {
            selectref.options[i] = null;
            i--;
        }
    }
}

//add all the elements of the first select to the second
function DoubleSelect_addall() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    
    for (i=0; i<selectref1.options.length; i++) {
        inner_text = selectref1.options[i].text;
        sel_value = selectref1.options[i].value;
        found = 0;
        for (j=0; j<selectref2.options.length; j++) {
            if (sel_value == selectref2.options[j].value) {
                found = 1;
                break
            }
        }
        if (found == 0)
            selectref2.options[selectref2.options.length] = new Option(inner_text, sel_value, false, false);
    }
}

//delete all the the elements from the second select
function DoubleSelect_delall() {
    var selectref = eval("document." + this.form + "." + this.select2);
    selectref.options[0] = new Option("", "0", false, false);
    var old_len = selectref.options.length;
    for (i=0; i<old_len; i++) {
        selectref.options[0] = null;
    }
}

//select all the the elements from the second select
function DoubleSelect_selectall() {
    var selectref = eval("document." + this.form + "." + this.select2);
    
    for (i=0; i<selectref.options.length; i++) {
        selectref.options[i].selected = true;
    }
}

//object costructor
function DoubleSelect(formname, select1, select2) {
    this.form = formname;
    this.select1 = select1;
    this.select2 = select2;
    this.add = DoubleSelect_add;
    this.del = DoubleSelect_del;
    this.addAll = DoubleSelect_addall;
    this.delAll = DoubleSelect_delall;
    this.selectAll = DoubleSelect_selectall;
}

/*
 *  SelectKeyOption Object:
 *  object containing a key and an array of Option Objects
 */

//add an Option to the array option
function SelectKeyOption_addOption(anOption) {
    this.options[this.options.length] = anOption;
}

//get all the Options
function SelectKeyOption_getOptions() {
    return this.options;
}

//get the key value
function SelectKeyOption_getKey() {
    return this.key;
}

//object costructor
function SelectKeyOption(key_value, option_array) {
    this.key = key_value;
    this.options = option_array;
    this.addOption = SelectKeyOption_addOption;
    this.getOptions = SelectKeyOption_getOptions;
    this.getKey = SelectKeyOption_getKey;
}

/*
 *  ConbinedSelect Object:
 *  Combines the elements of the first select whith tose of the second. Changing the selected element in the first, changes,
 *  the options of the second select.
 */

//called when the value of the first select changes, update all the elements in the second select
function CombinedSelect_changedvalue() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    
    //del all the old elemnts
    selectref2.options[0] = new Option("", "0", false, false);
    var old_len = selectref2.options.length;
    for (i=1; i<old_len; i++) {
        selectref2.options[1] = null;
    }
    //add the new elements based on the selected value of the first select
    selected_value = selectref1.value;
    for (var i=0; i<this.options.length; i++) {
        if (this.options[i].getKey() == selected_value) {
            var new_options = this.options[i].getOptions();
            for (var j=0; j<new_options.length; j++) {
                selectref2.options[selectref2.options.length] = new_options[j];
            }
        }
    }
}

//add the Options passed to the second select the options are shown when an element in the first select is selected.
function CombinedSelect_addOptions(sel_key_option_array) {
    this.options[this.options.length] = sel_key_option_array;
}

//object costructor
function CombinedSelect(formname, select1, select2) {
    this.form = formname;
    this.select1 = select1;
    this.select2 = select2;
    this.options = [];
    this.change = CombinedSelect_changedvalue;
    this.addOptions = CombinedSelect_addOptions;
}


/*
 *  DoubleSelectDati Object:
 *  In the first select I have all the elements and add or delete some of them to the second select, and on the
 *  related hiddent fake vector
 */

//add the elemnts selected in the first select to the second if not already present
function DoubleSelectDati_add() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);

    for (i=0; i<selectref1.options.length; i++) {
        if (selectref1.options[i].selected == true) {
            inner_text = selectref1.options[i].text;
            sel_value = selectref1.options[i].value;
            found = 0;
            for (j=0; j<selectref2.options.length; j++) {
                if (sel_value == selectref2.options[j].value) {
                    found = 1;
                    break
                }
            }
            if (found == 0) {
                selectref2.options[selectref2.options.length] = new Option(inner_text, sel_value, false, false);
                if (selectref3.value.length==0) {
                	selectref3.value+=sel_value;
                } else {
                	selectref3.value+=','+sel_value;
                }
            }
        }
    }
}

//del the selected elements from the second select
function DoubleSelectDati_del() {
var selectref2 = eval("document." + this.form + "." + this.select2);
var selectref3 = eval("document." + this.form + "." + this.select3);

    selectref3.value='';
    for (i=0; i<selectref2.options.length; i++) {	
	if (selectref2.options[i].selected == true) {
	    selectref2.options[i] = null;
	    i--;
	} else {
            if (selectref3.value.length==0) {
                selectref3.value+=selectref2.options[i].value;
            } else {
                selectref3.value+=','+selectref2.options[i].value;
            }
	}
    }
}

//add all the elements of the first select to the second
function DoubleSelectDati_addall() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);
    
    for (i=0; i<selectref1.options.length; i++) {
        inner_text = selectref1.options[i].text;
        sel_value = selectref1.options[i].value;
        found = 0;
        for (j=0; j<selectref2.options.length; j++) {
            if (sel_value == selectref2.options[j].value) {
                found = 1;
                break
            }
        }
        if (found == 0)
            selectref2.options[selectref2.options.length] = new Option(inner_text, sel_value, false, false);
            if (selectref3.value.length==0) {
                selectref3.value+=sel_value;
            } else {
                selectref3.value+=','+sel_value;
            }
    }
}

//delete all the the elements from the second select
function DoubleSelectDati_delall() {
    var selectref = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);
    
    selectref.options[0] = new Option("", "0", false, false);
    var old_len = selectref.options.length;
    
    selectref3.value='';
    for (i=0; i<old_len; i++) {
        selectref.options[0] = null;
    }
}

//object costructor
function DoubleSelectDati(formname, select1, select2, listahidden) {
    this.form = formname;
    this.select1 = select1;
    this.select2 = select2;
    this.select3 = listahidden;
    this.add = DoubleSelectDati_add;
    this.del = DoubleSelectDati_del;
    this.addAll = DoubleSelectDati_addall;
    this.delAll = DoubleSelectDati_delall;
}

//object costructor
function DoubleSelectDatiAlbo(formname, select1, select2, listahidden) {
    this.form = formname;
    this.select1 = select1;
    this.select2 = select2;
    this.select3 = listahidden;
    this.add = DoubleSelectDatiAlbo_add;
    this.del = DoubleSelectDatiAlbo_del;
    this.addAll = DoubleSelectDatiAlbo_addall;
    this.delAll = DoubleSelectDatiAlbo_delall;
}

//add all the elements of the first select to the second
function DoubleSelectDatiAlbo_addall() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);
    var text;
    
    for (i=0; i<selectref1.options.length; i++) {
        inner_text = selectref1.options[i].text;
        sel_value = selectref1.options[i].value;
        text='DataPubblicazione' + sel_value + 'GG'
        document.frmGare.elements[text].readOnly=false;
        text='DataPubblicazione' + sel_value + 'MM'
        document.frmGare.elements[text].readOnly=false;
        text='DataPubblicazione' + sel_value + 'AA'
        document.frmGare.elements[text].readOnly=false;
        found = 0;
        for (j=0; j<selectref2.options.length; j++) {
            if (sel_value == selectref2.options[j].value) {
                found = 1;
                break
            }
        }
        if (found == 0)
            selectref2.options[selectref2.options.length] = new Option(inner_text, sel_value, false, false);
            if (selectref3.value.length==0) {
                selectref3.value+=sel_value;
            } else {
                selectref3.value+=','+sel_value;
            }
    }
}

//delete all the the elements from the second select
function DoubleSelectDatiAlbo_delall() {
    var selectref = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);
    var valore, text;
    
    selectref.options[0] = new Option("", selectref.options[0].value, false, false);
    var old_len = selectref.options.length;
    
    selectref3.value='';
    for (i=0; i<old_len; i++) {
    	    valore=selectref.options[0].value;
	    text='DataPubblicazione' + valore + 'GG'
	    document.frmGare.elements[text].value='';
	    document.frmGare.elements[text].readOnly=true;
	    text='DataPubblicazione' + valore + 'MM'
	    document.frmGare.elements[text].value='';
	    document.frmGare.elements[text].readOnly=true;
	    text='DataPubblicazione' + valore + 'AA'
	    document.frmGare.elements[text].value='';
            document.frmGare.elements[text].readOnly=true;
    	//alert(selectref.options[0].value);
        selectref.options[0] = null;
    }
}

//add the elemnts selected in the first select to the second if not already present
function DoubleSelectDatiAlbo_add() {
    var selectref1 = eval("document." + this.form + "." + this.select1);
    var selectref2 = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);
    var text;

    for (i=0; i<selectref1.options.length; i++) {
        if (selectref1.options[i].selected == true) {
            inner_text = selectref1.options[i].text;
            sel_value = selectref1.options[i].value;
            found = 0;
            for (j=0; j<selectref2.options.length; j++) {
                if (sel_value == selectref2.options[j].value) {
                    found = 1;
                    break
                }
            }
            if (found == 0) {
                selectref2.options[selectref2.options.length] = new Option(inner_text, sel_value, false, false);
                if (selectref3.value.length==0) {
                	selectref3.value+=sel_value;
                } else {
                	selectref3.value+=','+sel_value;
                }
            }
            text='DataPubblicazione' + sel_value + 'GG'
            document.frmGare.elements[text].readOnly=false;
            text='DataPubblicazione' + sel_value + 'MM'
            document.frmGare.elements[text].readOnly=false;
            text='DataPubblicazione' + sel_value + 'AA'
            document.frmGare.elements[text].readOnly=false;
        }
    }
}

//del the selected elements from the second select
function DoubleSelectDatiAlbo_del() {
    var selectref2 = eval("document." + this.form + "." + this.select2);
    var selectref3 = eval("document." + this.form + "." + this.select3);
    var valore, text;

    selectref3.value='';
    for (i=0; i<selectref2.options.length; i++) {	
	if (selectref2.options[i].selected == true) {
	    valore=selectref2.options[i].value;
	    text='DataPubblicazione' + valore + 'GG'
	    document.frmGare.elements[text].value='';
	    document.frmGare.elements[text].readOnly=true;
	    text='DataPubblicazione' + valore + 'MM'
	    document.frmGare.elements[text].value='';
	    document.frmGare.elements[text].readOnly=true;
	    text='DataPubblicazione' + valore + 'AA'
	    document.frmGare.elements[text].value='';
	    document.frmGare.elements[text].readOnly=true;
	    selectref2.options[i] = null;
	    i--;
	} else {
            if (selectref3.value.length==0) {
                selectref3.value+=selectref2.options[i].value;
            } else {
                selectref3.value+=','+selectref2.options[i].value;
            }
	}
    }
}