/*
	* @class Check
	* @description This class is used to check/uncheck an array of checkboxes
 */
    
/*
	* @function Check
	* @description This is the default constuctor of the class, 
	* @param fieldBaseId The id of the field (without the index), for example mycheckbox (discard the separator and the index, so if the name is mycheckbox__0 for one field, use only mycheckbox)
	* @param separator The separator between the name and the index of the field. Not mandatory. Defaults to __.
	* @param ruleToCheck The rule that function should use to check, uncheck the elements, for example index % 2 ==0. Not mandatory. Defaults to 1.
	* @return voide
*/
function Check(fieldBaseId, separator, ruleToCheck){
	this.fieldBaseId = fieldBaseId;
	(separator == undefined)? this.separator = "__" : this.separator = separator;
	(ruleToCheck == undefined)? this.ruleToCheck = 1 : this.ruleToCheck = ruleToCheck;
	this.check = Check_check;
	this.uncheck = Check_uncheck;
	this.selected = Check_selected;
}

/*
	* @function check
	* @description this function checks the array of checkboxes based on the ruleToCheck
	* @return void 
*/
function Check_check(){
	var index = 0;
	var currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
	if(currentElement == null){
		var index = 1;
		var currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
	}
	while (currentElement != null){
		if (eval (this.ruleToCheck)) {
			currentElement.checked = true;
		}
		currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
		index++;
	}
}


/*
	* @function check
	* @description this function checks the array of checkboxes based on the ruleToCheck
	* @return void 
*/
function Check_uncheck(){
	var index = 0;
	var currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
	if(currentElement == null){
		var index = 1;
		var currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
	}
	while (currentElement != null){
		if (eval (this.ruleToCheck)) {
			currentElement.checked = false;
		}
		currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
		index++;
	}
}

/*
	* @function selected
	* @description this function returns a string of selected indexes concatenated by __
	* @return string The string of selected indexes concatenated by __
*/
function Check_selected(){
	var index = 0;
	var currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
	var arr = new Array();
	var arrCounter = 0;
	while (currentElement != null){
		if (currentElement.checked == true) {
			arr[arrCounter++] = currentElement.value;
		}
		currentElement = document.getElementById(this.fieldBaseId + this.separator + index);
		index++;
	}
	return arr.join("__");
}
