//This file contains functions used for the profile section in sdet:
//Made in 12.2008 by Etucom;


////////////////////////////////////////////////////////
//Functions for the page profile_domaines_ativite.php:

/***
 * Function that hide or display one or more div element according to their id: 
 *  their id name:
 *	@idName: a substring of the name of the id of some div on the page.
 *	@displayValue: 'none', 'block' or 'normal'
 *										if 'none': it hides the div, 
 *										if 'block': it displays the div.
 *										if 'normal': it functions like an switch:
 *											=> if on then off; else on;
 ***/
function openCloseId(idName, displayValue){
	var elem = $$("div."+idName);//To do: more generic
	for(var i = 0; i < elem.length; i++){

			switch(displayValue){
				case 'none': 
					elem[i].style.display = 'none';
					break;
					
				case 'block':
					elem[i].style.display = 'block';
					break;
					
				case 'normal':
					if(elem[i].style.display == 'block'){
							elem[i].style.display = 'none';
					}else{
						elem[i].style.display = 'block';
					}
					break;
				default:
					break;
			}

	}
}


/***
 * This function checked all the checkbox according to their id:
 * 	@form: the getElementById value of the specific form we're working 
 * 	@parent: a checkbox element:
 * 					 	 When we click on it, it automaticaly click on every child.
 * 	@idName: substring of the name of the id of the checkbox we research 
 * 	@switchValue: could be 'parent' or 'child'
 * 								if 'parent': we check all child
 * 								if 'child': we count the # of child ckecked and if all are
 * 														checked, we check the parent, otherwise we don't.    
 ***/ 
function checkAllId(form, parent, idName, switchValue){
	var nbChildChecked = 0;
	elements = $$('input.'+idName);
	
	switch(switchValue){
		case 'parent':
			elements.each(function(e, index) { 
				e.checked = $$('input.'+parent)[0].checked ; 
				openCloseId(idName, 'block'); 
				});
			break;
		
		case 'child':
			nbChildChecked = elements.inject(0, function(sum, e) { return sum+(e.checked ? 1:0) ; });
			break;
		default:
			break;

	}

	if(switchValue == 'child'){
		$$('input.'+parent)[0].checked = (elements.length == nbChildChecked);

	}
}

function loadCheck(form){
	masterElements = $$('input.master');
	for(var i = 0; i < masterElements.length; i++){
		checkAllId(form, 'master_'+(i+1), 'sub_'+(i+1), 'child');
		todoOpen = $$('input.sub_'+(i+1)).inject(false, function(todoOpen, e) { return todoOpen || e.checked ; });
		if(todoOpen)openCloseId('sub_'+(i+1), 'block');
	}
}

//End Functions for profile_domaines_activite.php
//////////////////////////////////////////////////////

