//--- INSTANCE METHODS -----------------------------------------------------------------------

//Initialise: Get and Store Form Object with given Name
function Form_initialise(form_name, special_checkboxes) {
	//Initialise Form
	this.FormName 		= form_name;
	this.FormContainer 	= _getForm(form_name);
 	this.FormCheckboxes	= this.getCheckboxes(special_checkboxes);
}


//Return the Element specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_getElement(name_element) {
	if (name_element == null) {
		alert("No element name given.");
		return;
	}
	
	//Code for W3 compatible browsers
	var Element = null;
    if (document.getElementById) {
		Element = document.getElementById(name_element);
	}

	//Code for IE browser
	else if (document.forms) {
		Element = this.FormContainer.elements[name_element];
	}

	else {
		for (e = 0; e < this.FormContainer.elements.length; e++) { 
			if (Form.elements[e].name == name_element) {
				Element = this.FormContainer.elements[e];
				break;
			}
		}
	}
	
	//Check if Element has been found	
	if (Element == null) {
		alert("Form element '" + name_element + "' does not exist.");
	}
	
	//Return Element Object
	return (Element);
}

//Return Array of Elements specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_getElements() {
		var list_elements   = new Array();
		var N_list_elements = 0;
		
		//Get all Elements
		var Element = null;
        for (var e = 0; e < this.FormContainer.elements.length; e++) { 
			Element = this.FormContainer.elements[e];
			if (Element.type == "checkbox") {
				list_elements[N_list_elements] = Element;
				N_list_elements++;
			}
		}

		//Return List of Elements
		return (list_elements);
}


//--- METHODS FOR SINGLE CHECKBOXES ----------------------------------------------------------

//Return the Checkbox specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_getCheckbox(name_checkbox) {
	if (name_checkbox == null) {
		alert("No checkbox name given2.");
		return;
	}

	//Get Element
	var Element = this.getElement(name_checkbox);
	
	//Check if Element found is a Checkbox
	if (Element.type == "checkbox") {
		return (Element);
	} else {
		alert("Checkbox '" + name_checkbox + "' does not exist.");
		return;
	}
}

//Enable Checkbox specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_enableCheckbox(name_checkbox) {
	var Checkbox = this.getCheckbox(name_checkbox);
	Checkbox.disabled = false;
} //function

//Disable Checkbox specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_disableCheckbox(name_checkbox) {
	var Checkbox = this.getCheckbox(name_checkbox);
	Checkbox.disabled = true;
} //function

//Tick Checkbox specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_tickCheckbox(name_checkbox) {
	var Checkbox = this.getCheckbox(name_checkbox);
	Checkbox.checked = true;
} //function

//Untick Checkbox specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_untickCheckbox(name_checkbox) {
	var Checkbox = this.getCheckbox(name_checkbox);
	Checkbox.checked = false;
} //function

//Toogle Checkbox specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_toggleCheckbox(name_checkbox) {
	var Checkbox = this.getCheckbox(name_checkbox);
	if (Checkbox.checked == true) {
		Checkbox.checked = false;
	} else {
		Checkbox.checked = true;
	}
} //function


//--- METHODS FOR MULTIPLE CHECKBOXES ----------------------------------------------------------

//Return the Checkboxes specified by "Name"
//Cross-browser: "Name" can be an "ID"
function Form_getCheckboxes(special_checkboxes) {
    var list_checkboxes = new Array();
    
    //Include Checkboxes if requested
    if ((special_checkboxes != null) && 
        (special_checkboxes.include != null) && 
        (special_checkboxes.include.length != 0)) {

        //Special Checkboxes
        var include_checkboxes = special_checkboxes.include;

        //Include given Checkboxes
        for (var c = 0; c < include_checkboxes.length; c++) {
            var Checkbox_Include = this.getCheckbox(include_checkboxes[c]);
            list_checkboxes[list_checkboxes.length] = Checkbox_Include;
        }
        //Return Checkboxes
        return (list_checkboxes);
    } else {
       //Get all Elements
        var list_elements = this.getElements();
        
        //Check if Element found is a Checkbox
        for	(var e = 0; e < list_elements.length; e++) {
        	var Element = list_elements[e];
        	if (Element.type == "checkbox") {
        		list_checkboxes[list_checkboxes.length] = Element;
        	} else {
        		alert("Checkbox '" + name_checkbox + "' does not exist.");
        		return;
        	}
        }
        
        //Exclude Checkboxes if requested
        if (special_checkboxes != null) {
            //Special Checkboxes
            var exclude_checkboxes = special_checkboxes.exclude;
        
            //Exclude given Checkboxes
            if (exclude_checkboxes != null) {
         		for (var c = 0; c < exclude_checkboxes.length; c++) {
        			if (exclude_checkboxes[c] == null) continue;
        			var Checkbox_Exclude = this.getCheckbox(exclude_checkboxes[c]);
        			list_checkboxes = _excludeCheckbox(list_checkboxes, Checkbox_Exclude);
        		}
            }
        }
    }

 	//Return Checkboxes
	return (list_checkboxes);
}

//No Parameter: Return all Checkboxes
//Modifier  'ticked':  Return those Checkboxes that are either ticked (true) or unticked (false)
//Modifier 'enabled': Return those Checkboxes that are either enabled (true) or disabled (false)
function Form_findCheckboxes(Modifiers) {
	//Modifiers
	if (Modifiers != null) {
		var boolTicked  = Modifiers.ticked;
		var boolEnabled = Modifiers.enabled;
	}
	
	//Get Checkboxes (which are not excluded)
	var list_checkboxes = this.FormCheckboxes;

	//Find enabled/disabled Checkboxes
	list_checkboxes = _findEnabledCheckboxes(list_checkboxes, boolEnabled);
	
	//Find ticked/unticked Checkboxes
	list_checkboxes = _findTickedCheckboxes(list_checkboxes, boolTicked);

	//Return Array of Checkboxes
	return (list_checkboxes);
}

//Tick all Checkboxs
function Form_tickAllCheckboxes(Modifiers) {
	//Modifiers
	if (Modifiers != null) {
		var boolEnabled = Modifiers.enabled;
	}

	//Get all unticked Checkboxes
	var checkboxes = new Array();
	checkboxes = this.findCheckboxes({ticked:false, enabled:boolEnabled});
	
	//Tick all unticked Checkboxes
	for (var c=0; c < checkboxes.length; c++)	{
		checkboxes[c].checked = true;
	}
	
	//Return Checkboxes
	return (checkboxes);
}

//Untick all Checkboxs
function Form_untickAllCheckboxes(Modifiers) {
	//Modifiers
	if (Modifiers != null) {
		var boolEnabled  = Modifiers.enabled;
		var Checkbox_excluded = Modifiers.exclude;
	}

	//Get all ticked Checkboxes
	var checkboxes = new Array();
	checkboxes = this.findCheckboxes({ticked:true, enabled: boolEnabled, exclude: Checkbox_excluded});
	
	//Untick all ticked Checkboxes
	for (var c=0; c < checkboxes.length; c++) {
		checkboxes[c].checked = false;
	}
	
	//Return Checkboxes
	return (checkboxes);
}

//Toogle all Checkboxes
function Form_toggleAllCheckboxes(Modifiers) {
	//Get all Checkboxes
	var checkboxes = new Array();
	checkboxes = this.findCheckboxes(Modifiers);
	
	//Untick all ticked Checkboxes
	for (var c=0; c < checkboxes.length; c++) {
		var Checkbox = checkboxes[c];
		if (Checkbox.checked == true) {
			Checkbox.checked = false;
		} else {
			Checkbox.checked = true;
		}
	}
	
	//Return Checkboxes
	return (checkboxes);
}

//Tick OR untick all Checkboxes according to the Status of a Reference Checkbox
function Form_adaptAllCheckboxes(Checkbox_Reference, Modifiers) {
	if (Checkbox_Reference == null) {
		alert("No reference checkbox name given.");
		return;
	}
	
	//Modifiers
	if (Modifiers != null) {
		var boolTicked  = Modifiers.ticked;
		var boolEnabled = Modifiers.enabled;
	}
	
	//Get Status of the Reference Checkbox
	var checkbox_Status = Checkbox_Reference.checked;
	
	//Get all Checkboxes with a different Status than the Reference Checkbox
	var checkboxes = new Array();
	checkboxes = this.findCheckboxes({ticked:!checkbox_Status, enabled:boolEnabled, exclude: Checkbox_Reference});
	
	//Set Status of Checkboxes
	for (var c=0; c < checkboxes.length; c++) {
		checkboxes[c].checked = checkbox_Status;
	}
	
	//Return Checkboxes
	return (checkboxes);
}





//--- CONSTRUCTUR AND PROTOTYPING ------------------------------------------------------------

//Constructor
function Form(form_name, exclude_checkboxes) {
	if (form_name != null) {this.initialise(form_name, exclude_checkboxes);}
}

//Prototyping
new Form (null);
Form.prototype.initialise	= Form_initialise;

Form.prototype.getElement	= Form_getElement;
Form.prototype.getElements	= Form_getElements;

Form.prototype.getCheckbox		= Form_getCheckbox;
Form.prototype.tickCheckbox		= Form_tickCheckbox;
Form.prototype.untickCheckbox	= Form_untickCheckbox;
Form.prototype.toggleCheckbox	= Form_toggleCheckbox;
Form.prototype.enableCheckbox	= Form_enableCheckbox;
Form.prototype.disableCheckbox	= Form_disableCheckbox;

Form.prototype.getCheckboxes		= Form_getCheckboxes;
Form.prototype.findCheckboxes		= Form_findCheckboxes;
Form.prototype.tickAllCheckboxes	= Form_tickAllCheckboxes;
Form.prototype.untickAllCheckboxes	= Form_untickAllCheckboxes;
Form.prototype.toggleAllCheckboxes	= Form_toggleAllCheckboxes;
Form.prototype.adaptAllCheckboxes	= Form_adaptAllCheckboxes;
			




//--- PRIVATE METHODS ------------------------------------------------------------------------

//Return form specified by "Name"
//Cross-browser: "Name" can be an "ID"
function _getForm(name_form) {
	if (name_form == null) {
		alert("No form name given.");
		return;
	}
	
	//Code for W3 compatible browsers
    var Form = null;
	if (document.getElementById) {
		//Determine Form
		Form = document.getElementById(name_form);
	} 
	
	//Code for IE browser
	else if (document.forms[name_form]) {
		//Determine Form
		Form = document.forms[name_form]; 
	}

	else {
		//Determine Form
		for (var f = 0; f < document.forms.length; f++) { 
			if (document.forms[f].name == name_form) {
				Form = document.forms[f];
				break;
			}
		}
	}
	
	//Return Form Object
	if (Form == null) {
		alert("Form '" + name_form + "' does not exist.");
		return;
	} else {
		return (Form);
	}
}

//Exclude given checkboxes from given list
function _excludeCheckbox(checkboxes_in, Checkbox_Exclude) {
    if (checkboxes_in == null) {
        alert("No checkboxes given.");
        return;
    }
    
    //Take complete List of Checkboxes if Checkbox_Excluded is not defined
    if (Checkbox_Exclude == null) {
        return (checkboxes_in);
    } else {
        var checkboxes_out   = new Array;
		var N_checkboxes_out = 0;
				
        //Check Type of Object to be excluded
        if (Checkbox_Exclude.type != 'checkbox') {
			alert("Cannot exclude given object. Not a checkbox: " + Checkbox_Exclude);
        }
        
        //Exclude given Checkbox
		for (var c = 0; c < checkboxes_in.length; c++) {
			var Checkbox_in = checkboxes_in[c];
			if (Checkbox_in != Checkbox_Exclude) {
				checkboxes_out[N_checkboxes_out] = Checkbox_in;
				N_checkboxes_out++;
			} 
		}
        
        //Return Array of Ticked Checkboxes
        return (checkboxes_out);
    }
}

//Find all ticked/unticked Checkboxes in the given List
function _findTickedCheckboxes(checkboxes_in, boolTicked) {
    if (checkboxes_in == null) {
        alert("No checkboxes given.");
        return;
    }

    //Take complete List of Checkboxes if boolTicked is not defined
    if (boolTicked == null) {
        return (checkboxes_in);
    } else {
        var checkboxes_out   = new Array;
        var N_checkboxes_out = 0;

        //Find all Ticked Checkboxes
        if (boolTicked == true) {
            for (var c = 0; c < checkboxes_in.length; c++) {
                var Checkbox_out = checkboxes_in[c];
                if (Checkbox_out.checked) {
                		checkboxes_out[N_checkboxes_out] = Checkbox_out;
                		N_checkboxes_out++;
                } 
            }
        } 
        
        //Find all Unticked Checkboxes
        else {
            for (var c = 0; c < checkboxes_in.length; c++) {
                var Checkbox_out = checkboxes_in[c];
                if (!Checkbox_out.checked) {
                		checkboxes_out[N_checkboxes_out] = Checkbox_out;
                		N_checkboxes_out++;
                } 
            }
        }
        
        //Return Array of Ticked Checkboxes
        return (checkboxes_out);
    }
}

//Find all enabled/disabled Checkboxes in the given List
function _findEnabledCheckboxes(checkboxes_in, boolEnabled) {
    if (checkboxes_in == null) {
        alert("No checkboxes given.");
        return;
    }

    //Take complete List of Checkboxes if boolEnabled is not defined
    if (boolEnabled == null) {
        return (checkboxes_in);
    } else {
        var checkboxes_out 	 = new Array;
		var N_checkboxes_out = 0;

        //Find all Enabled Checkboxes
        if (boolEnabled == true) {
            for (var c = 0; c < checkboxes_in.length; c++) {
                var Checkbox_out = checkboxes_in[c];
                if (!Checkbox_out.disabled) {
            		checkboxes_out[N_checkboxes_out] = Checkbox_out;
            		N_checkboxes_out++;
                } 
            }
        } 
        
        //Find all Disabled Checkboxes
        else {
            for (var c = 0; c < checkboxes_in.length; c++) {
                var Checkbox_out = checkboxes_in[c];
                if (Checkbox_out.disabled) {
            		checkboxes_out[N_checkboxes_out] = Checkbox_out;
            		N_checkboxes_out++;
                } 
            }
        }
        
        //Return Array of Ticked Checkboxes
        return (checkboxes_out);
    }
}


