/*
	rev2
	
	TODO:
		store a 'rollup' of each mask grouping
		remove the type checking from the controls and roll them.
			
	Working
		
	Win
	---
	IE6.0
	NS7.0
	OP7.0+
		-- doesn't like the "label for" tag, or rather it doesn't trigger the onclick like it should
	
	Mac
	---
	NS7.0

*/
var OP_OR = 0;
var OP_AND = 1;

var MOP_EQ = 0;
var MOP_GTE = 1;
var MOP_LTE = 2;

var oMaskSum = null;

function specSearch(src)
{			
	var i,j,k,l;
	var c,cbc,tc;
	
	var oOne = new bitMask(1,bitCount);
	var oZero;
	var oS;
	
	var t1 = getTime();
	var t2;
	var t3;
			
	// init the sums	
	if (isNull(oMaskSum))
	{
		oMaskSum = new Array(S.length);
								
		for (i=0;i<S.length;i++)
			oMaskSum[i] = new bitMask(1,bitCount);
	}
	
	// get the LOD
	var q = parseInt(xx_getElementById('lod').value);
									
	// TODO: use rollup in here to eliminate this loop, I don't need to check everything, only the thing which has changed
	for (c=0;c<S.length;c++)
	{						
		if (L[c]<=q)	// only things at our level
		{	
		
			oS = xx_getElementById('S' + S[c]);	// get the first element					
															
			if (oS)
			{									
				oZero = new bitMask(0,bitCount);
									
				/* DROP DOWN BOXES */
				if (xx_tagName(oS)=='select')
				{	
					oS = xx_getElementsByName(oS.name); // by name											
					/* standard */
					if (oS.length==1)
					{
						if (oS[0].selectedIndex!=0)					
							oZero = new bitMask(SB[c][oS[0].selectedIndex -1].getMask());
						else
							oZero = new bitMask(1,bitCount);																							
					}
					else
						alert('RANGED TYPES NOT IMPLEMENTED');
				}			
				else						
				/* RADIO BUTTONS + CHECK BOXES */			
				if (xx_tagName(oS)=='input')
				{							
					szType = xx_inputType(oS);																																																
					tc = 0;
													
					if (szType=='checkbox'||szType=='radio')
					{																																								
						oS = xx_getElementsByName(oS.name);
																					
						if (oS.length>=0)
						{						
							// if ANY is checked, then I can just bypass this, it's not affecting it anyway
							if (
									(isNull(src)||isUndefined(src))
									||							
									(((src.id!=src.name) && (src.name=='S' + S[c]))||oS[0].checked==false)
								)
							{ 	
								//alert('RUNNING: ' + src.id);					
								//alert('ischecked: ' + oS[0].checked + '\n sourceid: ' + src.id + '\n sourcename: ' + src.name + '\n control: ' + S[c]);
								// oS contains an array of each one, if it's an opAnd, then it's an opAnd..., only works for standard,,																															
								for (cbc=(oS.length-1);cbc>0;cbc--)																								
								{									
									if(oS[cbc].checked==true)								
									{
										tc = tc + 1;
																	
										// CBC - 1 is the offset
										
										// generate LTR or RTR mask if needed
										if (C[c]==MOP_LTE)
										{									
											// RTL
											oZero.opOr(SB[c][cbc-1].getMask());
										
											l = cbc;
																																						
											for(;l>0;l--)				
												oZero.opOr(SB[c][l-1].getMask());																															
										}							
										else if (C[c]==MOP_GTE)
										{
											// LTR
											oZero.opOr(SB[c][cbc-1].getMask());																
																			
											for(l=(oS.length-1);l>cbc;l--)
												oZero.opOr(SB[c][l-1].getMask());																																							
										}
										else
										{
											// Standard		
											if ((O[c]==OP_OR)||(tc==1))
												oZero.opOr(SB[c][cbc-1].getMask());							
											else
												oZero.opAnd(SB[c][cbc-1].getMask());										
										}
									

										setNodeColor(oS[cbc],'blue');
									}												
									else
									{	
										if (oS[cbc].disabled == true)	// perform this test better
										{
											setNodeColor(oS[cbc],'#999999');
										}
										else
										{								
											setNodeColor(oS[cbc],'black');
										}
									}
								}																	
							} // if check test
							else
							{																																							
								// any is checked
								/*oZero = new bitMask(1,bitCount);tc=1;	*/								
							}
						}																				
											
						// if none are checked, any must be checked and then we need ALL
						if (tc==0)
						{					
							oZero = new bitMask(1,bitCount);			
														
							if (oS[0].checked==false)													
								if (!isNull(src))
									unChecker(src.name);															
						}
						else
							oS[0].checked=false;
					}							
					else
						oZero = new bitMask(1,bitCount);
				}
			
				// store intermediate results -- this will be used to optimize the runtime
				oMaskSum[c] = oZero;
													
				// and with the previous results
				oOne.opAnd(oZero.getMask());			
			} // if oS
			else
				alert('no element');
				
		} // if q
	}
	
	/* NS4 needs to be fixed */
	/* oOne now contains our bitMask, is's getSetBitCount is the # of matching products */
	var oE = xx_getElementById('lblCount0'); 
	var oE2 = xx_getElementById('lblTotal0');
					
	oE.innerHTML = oOne.getSetBitCount();	
	oE2.innerHTML = bitCount;
	
	oE = xx_getElementById('lblCount1'); 
	oE2 = xx_getElementById('lblTotal1');
					
	oE.innerHTML = oOne.getSetBitCount();	
	oE2.innerHTML = bitCount;

	
		
	t2 = getTime();
			
	/* disable boxes which would result in zeros */	
	setControlState(src,oOne.getMask());
	t3 = getTime();
	//alert((t2-t1) + ' ' + (t3-t2) + ' ' + (t3-t1));
							
	return oOne.getSetBitCount();
}


function setNodeColor(o,c)
{
	//HACK: this is a temp kludge to colorize selections		
	if (o.parentNode)
	{					
		if (xx_tagName(o.parentNode)=='td')
			o.parentNode.parentNode.style.color = c;
		else
			o.parentNode.style.color = c;
	}
}

/* -- */
function setControlState(src,maskState)
{	
	var i,j,k;
	var oS;
		
	for (i=0;i<S.length;i++)
	{						
		var oMask = new bitMask(1,bitCount);
		var oS = xx_getElementById('S' + S[i]);
						
		// get mask rollup, excluding the current one
		for (j=0;j<SB.length;j++)
		{
			if (j!=i||O[i]==OP_AND) // skip the one we're on for now unless it's an AND op
				oMask.opAnd(oMaskSum[j].getMask());
		}
																									
		if (src==null||(src.name!=oS.name)||O[i]==OP_AND)
		{
			var szType = xx_inputType(oS);		
																																																		
			if (szType=='checkbox'||szType=='radio')
			{					
				var oS = xx_getElementsByName(oS.name);
				var oM;
																								
				if (oS.length>0)
				{
					if (C[i]==MOP_GTE)
					{
					/* MOP_GTE*/						
						for (j=oS.length-1;j>1;j--)
						{
							oM = new bitMask(SB[i][j-1].getMask());																																
							if ( (oM!=null) && (SB[i][j-1]!=null) ) {						
								if (oM.opAnd(oMask.getMask()).getSetBitCount()==0)
									disableControl(oS[j]);
								else
								{
									for (k=1;k<j+1;k++)
									enableControl(oS[k]);
																									
									break; // exit the outer for
								}																			
							} // if != null
						}	
							
					}
					else
					/* MOP_EQ */												
					for (j=1;j<oS.length;j++)
					{	
						oM = new bitMask(SB[i][j-1].getMask());																																
													
						if (oM.opAnd(oMask.getMask()).getSetBitCount()==0)
							disableControl(oS[j]);						
						else							
							enableControl(oS[j]);
					}
				}		
			//}
			}
			else if (szType=='select')
			{
				// this doesn't have the desired effect of course								
				for (j=1;j<oS.length;j++)
				{				
					oM = new bitMask(SB[i][j-1].getMask());
																				
					if (oM.opAnd(maskState).getSetBitCount()==0)						
						disableControl(oS.options[j]);						
					else						
						enableControl(oS.options[j]);
				}				
			}
		}	
	} /* for i->SB */
}

/* control toggly bits */
function enableControl(o) {toggleControl(o,false);}

function disableControl(o) {toggleControl(o,true);}

function toggleControl(o,b)
{
	if (isDOM||isIE4)
	{
		var type = xx_inputType(o);
		o.disabled = b;
						
		// find associated control and set text coloring as needed, sorta kludge-o-matic but ok
		if (type=='radio'||type=='checkbox')
		{
			if (o.parentNode)
			{
				if (o.parentNode.style)										
				{
					if (b==true)
						setNodeColor(o,'#999999');							
					else
						if (o.checked==true)
							setNodeColor(o,'blue');
						else
							setNodeColor(o,'black');
				}
			}
		}									
		else
		{
			// doesn't work any way IE doesn't obey disabled for option, go figure
		}
	} // DOM
	else
	{
		// implement me with angry stuff		
	}			
}

function unChecker(id)
{
	var oS = xx_getElementsByName(id);
	var sc;
	var j;
						
	if(oS)
	{
		var type = xx_inputType(oS[0]);
		
		if (type=='radio'||type=='checkbox')
		{
			oS[0].checked = true;			
			
			for (sc=1;sc<oS.length;sc++)
			{
				oS[sc].checked = false;																										
				toggleControl(oS[sc],oS[sc].disabled);
			}
		}
		else
		{
			oS = oS[0]; // only 1 select box	
			for (sc=1;sc<oS.options.length;sc++)
			{
				oS.options[sc].selected = false;
				toggleControl(oS.options[sc],oS.options[sc].disabled);						 						
			}
			
			oS.selectedIndex = 0; //any
		}
	}
}

function specUncheck(id)
{	
	unChecker(id); //undo the boxes
	specSearch(xx_getElementsByName(id));
}

function clearAll()
{
	// clear all selection criteria
	var i;
	var oS;
	
	for (i=0;i<S.length;i++)
			unChecker('S' + S[i]);
				
	specSearch(null);
}



// ugh, what am I writing in here =)
function toggleTier(src)
{	
	var o = xx_getElementById('lod');
	
	if (o)
	{	
		var q =	parseInt(o.value);
				
		if (q==1)
			q = 2;
		else
			q = 1;
					
		o.value = q;
				
						
		if (q==2) 
		{
			xx_getElementById('imgMore0').src = '/images/filter/lesst.gif';
			xx_getElementById('imgMore1').src = '/images/filter/lessb.gif';				
		}			
		else
		{
			xx_getElementById('imgMore0').src = '/images/filter/moret.gif';
			xx_getElementById('imgMore1').src = '/images/filter/moreb.gif';				
		}
		
						
		for (i=0;i<L.length;i++)
		{
			if ((L[i]-1)<=(q-1))
				showObject(xx_getElementById('T' + S[i]));
			else
			{
				unChecker('S' + S[i]);
				hideObject(xx_getElementById('T' + S[i]));		
			}
		}
	}
	
	// run spec search again	
	specSearch(null);	
}