/**
 *	Form Support : Binary Set Element Checkbox Groups
 *	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  Copyright : Isotope Communications 2006
 *	
 *	WARNING: This code is for use with prototype 1.5x  
 *			 Use set-1.6.js for Prototype 1.6 support
 */

var setEleDS = new Object();

function binUpdate(sObj,oChk,iValue,bBinMode){
	if ( oObj = $(sObj) ) {
		// ~~ 3.9 compat; &-prefix
		
		if ( bBinMode && ( oObj.name.indexOf('_filterBy_') != -1 ) ) {
			oObj.value = oObj.value.substr(1);
		}
		if ( bBinMode ) {
			if ( oChk.checked || oChk.selected ){
				oObj.value |= iValue;
			} else {
				oObj.value &= ~iValue;
			}
		} else {
			if ( !setEleDS[sObj] ) {
				setEleDS[sObj] = $H();
				if ( oObj.value ) {
					aTmp = oObj.value.split(',');
					for (i=0;i<aTmp.length;i++) {
						setEleDS[sObj].set(aTmp[i], 1);
					}
				}
				//alert('Created hash:' + setEleDS[sObj].keys());
			}
			if ( oChk.checked || oChk.selected ){
				setEleDS[sObj].set(iValue, 1);
			} else {
				setEleDS[sObj].unset(iValue);
			}
			oObj.value = setEleDS[sObj].keys();
			//alert('Set hash +['+iValue+']:' + oObj.value);
		}
		if ( bBinMode && ( oObj.name.indexOf('_filterBy_') != -1 ) ) {
			oObj.value = '&' + oObj.value;
		}
	} else {
		//alert('Binary sets require prototype.');
	}
	//alert('Set binVal:' + oObj.value);
}

function mSetVal(o,n, sObj){
	v = 0;
	if ( oObj = $(sObj) ) {
		// Reset the current value bfore cycling through
		// selected items
		oObj.value = '';
		if( setEleDS[sObj] ) { 
			setEleDS[sObj] = $H();
		}
		
		if ((o.options.length > 31) || ( parseInt(o.options[0].value)!=o.options[0].value ) ) {
			iBinMode = 0;
		} else {
			iBinMode = 1;
		}
		//alert(iBinMode);
		for ( x = 0; x < o.options.length; x ++ ) {
			if ( o.options[x].selected ) {
				//v |= o.options[x].value;
				binUpdate(sObj, o.options[x], o.options[x].value, iBinMode);
			}
		}
	
	}
}


function chk_minMax( oArgs, sColumn ){
	if ( oArgs ) {
		//alert('Checking minMax of ' + sColumn + ' is between ' + oArgs.min + ' and ' + oArgs.max );
		if ( oEnclosure = $('chkEnclosure_'+sColumn) ) {
			checks = oEnclosure.getElementsBySelector('input');
			lables = oEnclosure.getElementsBySelector('label');
			checked = 0;
			for ( x = 0; x < checks.length; x ++ ) {
				if ( checks[x].checked ) {
					checked++;
				}
			} 
			disable = ( checked >= oArgs.max );
			/*
			if ( disable ) {
				alert("Disabling " + sColumn + ", (Max:"+oArgs.max+", count:"+checked+")");
			} else {
				alert("Not disabling " + sColumn + ", (Max:"+oArgs.max+", count:"+checked+")");
			}
			*/
			for ( x = 0; x < checks.length; x ++ ) {
				checks[x].disabled = ( checks[x].checked ) ? 0 : disable;
				( disable && !checks[x].checked ) ? lables[x].addClassName('disabled') : lables[x].removeClassName('disabled');
			} 
			if ( !checks[0].form.unlock ) {
				checks[0].form.unlock = new Object();
				Event.observe( checks[0].form, 'submit', form_checks.unlock.bindAsEventListener(form_checks));
			}
			checks[0].form.unlock[sColumn] = checks;
		} else {
			alert('Error');
		}
	}
}

var form_checks = {
	unlock: function(e){
		f = Event.element(e);
		if ( f.unlock ) {
			for ( i in f.unlock ) {
				if ( f.unlock[i] ) {
					for ( x = 0; x < f.unlock[i].length; x ++ ) {
						f.unlock[i][x].disabled = false;
					}
				}
			}
		}
	}
}