function in_array(search, myArray) {
	
	if(myArray.length < 1)
		return false;
		
		
	for(var i = 0; i < myArray.length; i++) {
		
		if(myArray[i] == search) 
			return true;	
	}

	return false;
}

function array_diff(tab1, tab2) {
	
	var tabDiff = new Array();
		
	for(var i = 0; i < tab2.length; i++) {
		
		if(in_array(tab2[i] , tab1) == false)
			tabDiff[tabDiff.length] = tab2[i];
	} 
	
	return tabDiff;
}





var Checkbox = Class.create();

Checkbox.prototype = {
	
	initialize: function(idGroupe , nb) {
		
		this.idGroupe = idGroupe;
		this.nb = nb;
		this.choixMax = false;
		
		
		this.precision = new Array();
		this.precisionCache = new Array();
		
		this.inverses = new Array();
		
		this.setObserver();
		
	},
	setObserver: function() {
		
		var _this = this;
		
		for(var i = 1; i <= this.nb; i++) {
			
			var myElement = $(this.idGroupe + '_' + i);
			
			new Form.Element.Observer( myElement , 0.2, function(el) { 
						_this.check(el.value); 
				} );		
		}
	},
	setPrecision: function(numero) {
		
		var _this = this;
		
		for(var j = 0; j < this.setPrecision.arguments.length; j++) {
			
			var numero = this.setPrecision.arguments[j];
			var inputPrecision = $(this.idGroupe + '_' + numero + '_Pr');
			
			if(inputPrecision) {
				this.precision[this.precision.length] = numero;
				new Form.Element.Observer( inputPrecision , 0.2, function(el) {
						 _this.checkPrecision(el); 
					} );
			}	
		}
	},
	setInverse: function(groupe1 , groupe2) {
		
		var myGroupe1 = new Array();
		var myGroupe2 = new Array();
		

		if(groupe2 == false) {
			
			for(var i = 1; i <= this.nb; i++) {
			
				if(in_array(i , groupe1) == false)
					myGroupe2[count(myGroupe2)] = i;
			}
		
		
		} else
			myGroupe2 = groupe2;
			
		
		myGroupe1 = groupe1;
	
		var indiceCourant = this.inverses.length;
				
		this.inverses[indiceCourant] = new Array();
		this.inverses[indiceCourant]['c'] = myGroupe1;
		this.inverses[indiceCourant]['i'] = myGroupe2;
	
	},
	setMax: function(max) {
	
		this.choixMax = max;
	
	},
	check: function(numero) {
		
		if(this.choixMax != false) {
			
			var nbChecked = 0;
			
			for(var j = 1; j <= this.nb; j++) {
				if($(this.idGroupe + '_' + j).checked == true) 
					nbChecked++;
			}
			
			if(nbChecked >  this.choixMax) {
				$(this.idGroupe + '_' + numero).checked = false;
				return;
			}
		}
						
		if(in_array(numero, this.precision) == true)
			this.updatePreciser(numero);
		
		if(	$(this.idGroupe + '_' + numero).checked == true) {
			
			for(var i = 0; i < this.inverses.length; i++) {
		
				if(in_array(numero, this.inverses[i]['c'])) {
					this.deselectGroupe(this.inverses[i]['i']);
					//break;
					
				} else if(in_array(numero, this.inverses[i]['i'])) {
					this.deselectGroupe(this.inverses[i]['c']);
					//break;				
				}
				
			}
		}
		
	},
	deselectGroupe: function(groupe) {
				
		for(var i = 0; i < groupe.length; i++) 
			$(this.idGroupe + '_' + groupe[i]).checked = false;
				
	
	},
	checkPrecision: function(element) {
		
		var boxPrecisionId = element.id;
		var boxId = boxPrecisionId.substring(0 , boxPrecisionId.length - 3);		

		var box = $(boxId);
		var boxPrecision = $(boxPrecisionId);
		
		if(boxPrecision.value != '') {			
			box.checked = true;
			this.check(box.value);			
		}  
		
	},
	updatePreciser: function(numElement) {
		
		var i = numElement;
		var box = $(this.idGroupe + '_' + i);
		var boxPrecision = $(this.idGroupe + '_' + i + '_Pr');
		
		if(boxPrecision) {
					
			if(box.checked == true && boxPrecision.value == '' && this.precisionCache[numElement] != undefined ) 
				boxPrecision.value = this.precisionCache[numElement];
				
			else if(box.checked == false && boxPrecision.value != '') {
			
					this.precisionCache[numElement] = boxPrecision.value;
					boxPrecision.value = '';
			}
		}
	}
}














var Radio = Class.create();

Radio.prototype = {
	
	initialize: function(idGroupe , nb) {
		
		this.idGroupe = idGroupe;
		this.nb = nb;
		
		
		this.precision = new Array();
		this.precisionCache = new Array();
		
		this.setObserver();
		
	},
	setObserver: function() {
		
		var _this = this;
		
		for(var i = 1; i <= this.nb; i++) {
			
			var myElement = $(this.idGroupe + '_' + i);
			
			new Form.Element.Observer( myElement , 0.2, function(el) { 
						_this.check(el.value); 
				} );		
		}
	},
	setPrecision: function(numero) {
		
		var _this = this;
		
		for(var j = 0; j < this.setPrecision.arguments.length; j++) {
			
			var numero = this.setPrecision.arguments[j];
			var inputPrecision = $(this.idGroupe + '_' + numero + '_Pr');
			
			if(inputPrecision) {
				this.precision[this.precision.length] = numero;
				new Form.Element.Observer( inputPrecision , 0.2, function(el) {
						 _this.checkPrecision(el); 
					} );
			}	
		}
	},
	check: function(numero) {
		
						
		if(in_array(numero, this.precision) == true)
			this.updatePreciser(numero);
		
	},
	checkPrecision: function(element) {
		
		var boxPrecisionId = element.id;
		var boxId = boxPrecisionId.substring(0 , boxPrecisionId.length - 3);		

		var box = $(boxId);
		var boxPrecision = $(boxPrecisionId);
		
		if(boxPrecision.value != '') {			
			box.checked = true;
			this.check(box.value);			
		}  
		
	},
	updatePreciser: function(numElement) {
		
		var i = numElement;
		var box = $(this.idGroupe + '_' + i);
		var boxPrecision = $(this.idGroupe + '_' + i + '_Pr');
		
		if(boxPrecision) {
					
			if(box.checked == true && boxPrecision.value == '' && this.precisionCache[numElement] != undefined ) 
				boxPrecision.value = this.precisionCache[numElement];
				
			else if(box.checked == false && boxPrecision.value != '') {
			
					this.precisionCache[numElement] = boxPrecision.value;
					boxPrecision.value = '';
			}
		}
	}
}







function coche(elt,indice){
	if( elt.id=='Q1_'+indice ){
		// elt est le aucun
		for( var i=1 ; i<indice ; i++ ) $('Q1_'+i).checked=false;	
	}else{
		$('Q1_'+indice).checked=false;
	}
}





function updateLocation(url,num){
	var param = 'num='+num+'&url='+url;
	var ajaxRequest = new Ajax.Request(
				'http://www.easy-sondage.fr/scripts/updateLocation.ajax.php',
				{
					"method": 'post',
					"postBody": param,
					"asynchronous":false
				}
						);
		var reponseText = ajaxRequest.transport.responseText; 
}





