function getOptType(pid)
{
	var tempElem = document.getElementById('po'+pid);	
	if (tempElem.tagName.toUpperCase() == 'SELECT')
		optType = 2;
	else if (tempElem.type.toUpperCase() == 'RADIO')
		optType = 1;
	else if (tempElem.type.toUpperCase() == 'CHECKBOX')
		optType = 0;	
	else if (tempElem.type.toUpperCase() == 'HIDDEN')
		optType = 3;
	else
		optType = 5;
	return optType;
}

function updateSelect(optType,pid,cid)
{
	var theindex = pid.substring(pid.indexOf('_'));
	var rowColor = [];
	rowColor[0] = '#ffffff';
	rowColor[1] = '#cccccc';

	var theProd = document.getElementById('theProduct_'+pid);

	var skipOpt = '';

	if (exceptions.length)
	{	
		for (var j in exceptions)
		{
			var lastElem = -1;
			var lastElemVal = -1;
			var sizeExc = 0;
			var numSel = 0;

			for (var k in exceptions[j])
			{
				sizeExc++;
				
				if (typeof coOpts != 'undefined')
				{
					if (typeof coOpts[k] != 'undefined')
						continue;
				}

				if (exceptions[j][k] == 0)
				{		
					if (document.getElementById('po'+k+theindex).type.toUpperCase() != 'TEXT' && document.getElementById('po'+k+theindex).tagName.toUpperCase() != 'TEXTAREA')
					{
						if (getPOValue(theindex,getOptType(k+theindex),k) != 'toSelect')
						{
							numSel++;
							exceptions[j][k] = -1;
						}
						else
						{
							lastElem = k;
							lastElemVal = 0;
						}
					}
					else
					{
						lastElem = k;
						lastElemVal = 0;
					}
				}
				else if (getPOValue(theindex,getOptType(k+theindex),k)==exceptions[j][k])
				{
					numSel++;
					exceptions[j][k] = -1;
				}
				else
				{
					lastElem = k;
					lastElemVal = exceptions[j][k];
				}				
			}
			
			if (lastElem != -1 && lastElemVal == 0 && numSel == sizeExc-1)
			{
				var cn2 = document.getElementById('po'+lastElem+theindex+'_mainrow').getElementsByTagName('td');
				
				for (var ccc=0; ccc<cn2.length; ccc++)
					cn2[ccc].style.display = 'none';
				
				document.getElementById('po'+lastElem+theindex+'_mainrow').style.display = 'none';
				
				if (skipOpt.indexOf(lastElem+',') == -1)
					skipOpt = skipOpt + lastElem + ',';

							
				for (var a in exceptions)
				{
					if (typeof exceptions[a][lastElem] != 'undefined')
					{					
						if (exceptions[a][lastElem] == 0)
							exceptions[a][lastElem] = -1;
					}
				}
			}
			else if (lastElem != -1 && numSel == sizeExc-1)
			{
				var tmpI = lastElem;
				var elem = document.getElementById('po'+tmpI+theindex);

				if (elem.tagName.toUpperCase() == 'SELECT')
				{
					for (var z=0; z<elem.options.length; z++)
					{
						if (elem.options[z].value == lastElemVal)
						{
							elem.options[z] = null;
							break;
						}
					}			
				}
				else if (elem.type.toUpperCase() == 'RADIO') 
				{
					var tmpInd = 0;
					var numRadio = document.getElementsByName('product_options'+theindex+'['+tmpI+']').length - 1;
					
					for (var t=0; t<numRadio; t++)
					{
						if (document.getElementById('po'+tmpI+theindex+'_'+t).value == exceptions[j][tmpI])
						{
							tmpInd = t;
							break;
						}
					}

					document.getElementById('po'+tmpI+theindex+'_'+tmpInd+'_row').style.display = 'none';

					var colorIndex = (document.getElementById('po'+tmpI+theindex+'_'+tmpInd+'_row').style.backgroundColor==rowColor[0] ? 0:1);
					for (var r=tmpInd+1; r<numRadio; r++)
					{
						if (document.getElementById('po'+tmpI+theindex+'_'+r+'_row').style.display=='')
							document.getElementById('po'+tmpI+theindex+'_'+r+'_row').style.backgroundColor = (colorIndex==0 ? rowColor[colorIndex++]:rowColor[colorIndex--]);
					}
				}
				else if (elem.type.toUpperCase() == 'HIDDEN')
					document.getElementById('po'+tmpI+theindex+'_'+exceptions[j][tmpI]).style.display = 'none';
				else if (elem.type.toUpperCase() == 'CHECKBOX')
				{				
					document.getElementById('po'+tmpI+theindex).checked = (names[tmpI]['options'][lastElemVal].toUpperCase() == 'YES' ? false : true);
					//document.getElementById('po'+tmpI+theindex+'_div').style.display = 'none';	
					document.getElementById('po'+tmpI+theindex).disabled = true;
				}				
				else if (elem.type.toUpperCase() == 'TEXT' || elem.tagName.toUpperCase() == 'TEXTAREA')
				{				
					elem.style.display = 'none';
					elem.value = '';
					document.getElementById('po'+tmpI+theindex+'_mainrow').style.display = 'none';					
				}
			
				for (var a in exceptions)
				{
					if (typeof exceptions[a][tmpI] != 'undefined')
					{					
						if (exceptions[a][tmpI]==lastElemVal)
							exceptions[a][tmpI] = -1;
					}					
				}				
			}
		}

		for (var m in exceptions)
		{
			for (var n in exceptions[m])
			{			
				if (typeof coOpts != 'undefined')
				{
					if (typeof coOpts[n] != 'undefined')
						continue;
				}
				
				if (exceptions[m][n] == 0)
				{		
					var cn2 = document.getElementById('po'+n+theindex+'_mainrow').getElementsByTagName('td');

					for (var ccc=0; ccc<cn2.length; ccc++)
						cn2[ccc].style.display = '';

					document.getElementById('po'+n+theindex+'_mainrow').style.display = '';
				}
				else if (exceptions[m][n] != -1)
				{					
					var tmpI = n;
					var elem = document.getElementById('po'+tmpI+theindex);
					var tempElement;

					if (elem.tagName.toUpperCase() == 'SELECT')
					{							
						var flagged = false;
						
						for (var d=0; d<elem.options.length; d++)
						{
							if (elem.options[d].value == exceptions[m][n])
							{
								flagged = true;
								break;
							}
						}

						if (flagged) continue;	

						var tmpInd = 1;
						var startPos = 1;
						var b = names[n]['options'][exceptions[m][n]];
						for (var x in names[n]['options'])
						{
							if (b == names[n]['options'][x])
								break;
							for (var dd=startPos; dd<elem.options.length; dd++)
							{
								if (names[n]['options'][x] == names[n]['options'][elem.options[dd].value])
								{
									startPos = dd+1;
									tmpInd++;
									break;
								}
							}
						}			
						if (tmpInd < elem.options.length)
						{
							var next = elem.options[tmpInd];							
						}						
						//tempElement = elem.options[tmpInd];
						
						elem.options[tmpInd] = new Option(b,exceptions[m][n]); //in Safari, we need to somehow redraw UI or else we get blacked out options
						tempElement = elem.options[tmpInd];
					
						if (next)
						{
							var temp;
							for (var c=tmpInd+1; c<elem.options.length; c++)
							{
								temp = elem.options[c];	
								elem.options[c] = next;
								next = temp;
							}
							elem.options[elem.options.length] = next; //MAY NOT BE NEEDED, NEED TO CHECK
						}
										}
					else if (elem.type.toUpperCase() == 'RADIO') 
					{
						var tmpInd = 0;
						var numRadio = document.getElementsByName('product_options'+theindex+'['+tmpI+']').length - 1;

						for (var t=0; t<numRadio; t++)
						{
							if (document.getElementById('po'+tmpI+theindex+'_'+t).value == exceptions[m][tmpI])
							{
								tmpInd = t;
								break;
							}
						}
						
						tempElement = document.getElementById('po'+tmpI+theindex+'_'+tmpInd+'_label');
						
						document.getElementById('po'+tmpI+theindex+'_'+tmpInd+'_row').style.display = '';

						var colorIndex = 0;
						for (var r=0; r<numRadio; r++)
						{
							if (document.getElementById('po'+tmpI+theindex+'_'+r+'_row').style.display=='')
								document.getElementById('po'+tmpI+theindex+'_'+r+'_row').style.backgroundColor = (colorIndex==0 ? rowColor[colorIndex++]:rowColor[colorIndex--]);
						}
					}
					else if (elem.type.toUpperCase() == 'HIDDEN')
					{					
						tempElement = document.getElementById('po'+tmpI+theindex+'pricediff_'+exceptions[m][n]);	
					
						document.getElementById('po'+tmpI+theindex+'_'+exceptions[m][n]).style.border = '';
						document.getElementById('po'+tmpI+theindex+'_'+exceptions[m][n]).style.display = '';					
					}
					else if (elem.type.toUpperCase() == 'CHECKBOX')
					{						
						//tempElement = elem;
					
						//if (cid != tmpI && document.getElementById('po'+tmpI+theindex+'_div').style.display != '')
						if (cid != tmpI && document.getElementById('po'+tmpI+theindex).disabled != false)
							document.getElementById('po'+tmpI+theindex).checked = false;
						//document.getElementById('po'+tmpI+theindex+'_div').style.display = '';
						document.getElementById('po'+tmpI+theindex).disabled = false;
					}
					else if (elem.type.toUpperCase() == 'TEXT' || elem.tagName.toUpperCase() == 'TEXTAREA')
					{					
						elem.style.display = '';
						document.getElementById('po'+tmpI+theindex+'_mainrow').style.display = '';					
					}
					
					if (modifiers[n] && elem.type.toUpperCase() != 'CHECKBOX')
					{							
						var theOptVal = getPOValue(theindex,getOptType(tmpI+theindex),tmpI);	
						
						if (modifiers[n][theOptVal])
							var tmpVal = modifiers[n][theOptVal][0];
						else if (theOptVal == 'toSelect')
							var tmpVal = 0;			
						
						var theDiff = modifiers[n][exceptions[m][n]][0] - tmpVal;
						var tmpDisp = tempElement.innerHTML;
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						var toDisplay = tmpDisp.substring(0,tmpPos);
							
						if (theDiff == 0) tempElement.innerHTML = toDisplay;
						else tempElement.innerHTML = ''+toDisplay+' ['+(theDiff > 0 ? '+':'-')+currency_symbol+price_format(Math.abs(theDiff))+']';
					}

				}
			}			
		}	
	}	

	var toEnable = false;	
	var toDisable = false;
	var lastNonSkip = -1;
	var disableNextOnly = false;

	for (var opt in names)
	{				
		if (typeof coOpts != 'undefined')
		{
			if (typeof coOpts[opt] != 'undefined')
				continue;
		}

		var theOpt = document.getElementById('po'+opt+theindex);

		if (theOpt.type.toUpperCase() != 'CHECKBOX' && theOpt.type.toUpperCase() != 'TEXT' && theOpt.tagName.toUpperCase() != 'TEXTAREA' && skipOpt.indexOf(opt+',') == -1)
			lastNonSkip = opt;
		
		if (toEnable)
		{			
			if (theOpt.type.toUpperCase() == 'CHECKBOX' || theOpt.type.toUpperCase() == 'TEXT' || theOpt.tagName.toUpperCase() == 'TEXTAREA' || skipOpt.indexOf(opt+',') != -1)
				toEnable = true;
			else if (getPOValue(theindex,getOptType(opt+theindex),opt) != 'toSelect')
				toEnable = true;
			else
			{
				toEnable = false;
				
				if (typeof coOpts != 'undefined') // && getPOValue(theindex,getOptType(lastNonSkip+theindex),lastNonSkip) != 'toSelect') //NEED TO CHECK IF OPT HAS SOMETHING SELECTED ALREADY
					toDisable = true; //WILL EFFECT CART ONLY OPTS
			}
			
			if (skipOpt.indexOf(opt+',') == -1)
			{
				if (theOpt.type.toUpperCase() != 'RADIO')
					theOpt.disabled = false;
				else
				{
					var radios = document.getElementsByName('product_options'+theindex+'['+opt+']');
					for (var i = 0; i < radios.length; i++)
						radios[i].disabled = false;			
				}		
			}
			else
			{
				skipOpt.replace(opt+',','');
				
				if (theOpt.type.toUpperCase() != 'RADIO')
				{
					theOpt.disabled = true;
					
					if (theOpt.type.toUpperCase() == 'TEXT' || theOpt.tagName.toUpperCase() == 'TEXTAREA')
						theOpt.value = '';
					else
						theOpt.value = 'toSelect';
				}
				else
				{
					var radios = document.getElementsByName('product_options'+theindex+'['+opt+']');
					for (var i = 0; i < radios.length; i++)
						radios[i].disabled = true;			
					
					theOpt.value = 'toSelect';
				}				
			}				
		}
		else if (toDisable)
		{				
			if (typeof modifiers[opt] != 'undefined')
			{
				var tmpModIndex;
				var theDiff;
				var toDisplay;

				if (theOpt.tagName.toUpperCase() == 'SELECT')
				{
					for (var cOpt = 1; cOpt < theOpt.options.length; cOpt++)
					{				
						tmpModIndex = theOpt.options[cOpt].value;					
						theDiff = modifiers[opt][tmpModIndex][0];	
						var tmpDisp = theOpt.options[cOpt].innerHTML;	
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						toDisplay = tmpDisp.substring(0,tmpPos);	
						if (theDiff == 0) theOpt.options[cOpt].innerHTML = toDisplay;
						else theOpt.options[cOpt].innerHTML = ''+toDisplay+' [+'+currency_symbol+price_format(theDiff)+']';	
					}							
				}
				else if (theOpt.type.toUpperCase() == 'RADIO')
				{
					var opts = document.getElementsByName('product_options'+theindex+'['+opt+']');
					for (var cOpt = 1; cOpt < opts.length; cOpt++)
					{
						tmpModIndex = opts[cOpt].value;
						theDiff = modifiers[opt][tmpModIndex][0];
						var tElem = document.getElementById('po'+opt+theindex+'_'+(cOpt-1)+'_label');
						var tmpDisp = tElem.innerHTML;
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						toDisplay = tmpDisp.substring(0,tmpPos);
						if (theDiff == 0) tElem.innerHTML = toDisplay;
						else tElem.innerHTML = ''+toDisplay+' [+'+currency_symbol+price_format(theDiff)+']';
					}			
				}
				else if (theOpt.type.toUpperCase() == 'HIDDEN')
				{		
					for (var cOpt in names[opt]['options'])
					{
						var opts = document.getElementById('po'+opt+theindex+'pricediff_'+cOpt);
						tmpModIndex = cOpt;
						theDiff = modifiers[opt][tmpModIndex][0];
						var tmpDisp = opts.innerHTML;
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						toDisplay = tmpDisp.substring(0,tmpPos);

						if (theDiff == 0)
						{
							opts.innerHTML = toDisplay;
							document.getElementById('po'+opt+theindex+'_'+tmpModIndex).style.border = '';
						}
						else
						{
							opts.innerHTML = ''+toDisplay+' [+'+currency_symbol+price_format(theDiff)+']';
							document.getElementById('po'+opt+theindex+'_'+tmpModIndex).style.border = '';
						}							
					}				
				}		
			}
			
			if (theOpt.type.toUpperCase() != 'RADIO')
				theOpt.disabled = true;
				
			if (theOpt.tagName.toUpperCase() == 'SELECT')
				theOpt.selectedIndex = 0;				
			else if (theOpt.type.toUpperCase() == 'RADIO')
			{
				var radios = document.getElementsByName('product_options'+theindex+'['+opt+']');
				for (var i = 0; i < radios.length; i++)
					radios[i].disabled = true;				
				theOpt.checked = true;				
			}
			else if (theOpt.type.toUpperCase() == 'HIDDEN')
				theOpt.value = 'toSelect';									
			else if (theOpt.type.toUpperCase() == 'CHECKBOX')			
				theOpt.checked = false;		
			else if (theOpt.type.toUpperCase() == 'TEXT' || theOpt.tagName.toUpperCase() == 'TEXTAREA')
				theOpt.value = '';		
			
			if (disableNextOnly)
			{
				disableNextOnly = false;
				toDisable = false;
			}			
		}
		else if (opt==cid) //else
		{				
			if (theOpt.type.toUpperCase() == 'TEXT' || theOpt.tagName.toUpperCase() == 'TEXTAREA')	
			{
				toEnable = true;
				continue;
			}
			
			var theOptVal = getPOValue(theindex,optType,opt);
			if (theOptVal == 'toSelect')
				toDisable = true;
			else
				toEnable = true;					
			if (modifiers[cid])
			{
				if (theOptVal == 'toSelect')
					var tmpVal = 0;
				else
				{
					if (modifiers[cid][theOptVal])
						var tmpVal = modifiers[cid][theOptVal][0]; //theOptVal may be 'toSelect' which JS will complain about
					else
						var tmpVal = 0;
				}
				
				var tmpModIndex;
				var theDiff;
				var toDisplay;

				if (theOpt.tagName.toUpperCase() == 'SELECT')
				{						
					for (var cOpt = 1; cOpt < theOpt.options.length; cOpt++)
					{
						tmpModIndex = theOpt.options[cOpt].value;
						theDiff = modifiers[cid][tmpModIndex][0] - tmpVal;
						var tmpDisp = theOpt.options[cOpt].innerHTML;
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						toDisplay = tmpDisp.substring(0,tmpPos);
						if (theDiff == 0) theOpt.options[cOpt].innerHTML = toDisplay;
						else theOpt.options[cOpt].innerHTML = ''+toDisplay+' ['+(theDiff > 0 ? '+':'-')+currency_symbol+price_format(Math.abs(theDiff))+']';
					}						
				}
				else if (theOpt.type.toUpperCase() == 'RADIO')
				{
					var opts = document.getElementsByName('product_options'+theindex+'['+opt+']');
					for (var cOpt = 1; cOpt < opts.length; cOpt++)
					{
						tmpModIndex = opts[cOpt].value;
						theDiff = modifiers[cid][tmpModIndex][0] - tmpVal;
						var tElem = document.getElementById('po'+opt+theindex+'_'+(cOpt-1)+'_label');
						var tmpDisp = tElem.innerHTML;
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						toDisplay = tmpDisp.substring(0,tmpPos);
						if (theDiff == 0) tElem.innerHTML = toDisplay;
						else tElem.innerHTML = ''+toDisplay+' ['+(theDiff > 0 ? '+':'-')+currency_symbol+price_format(Math.abs(theDiff))+']';
					}					
				}
				else if (theOpt.type.toUpperCase() == 'HIDDEN')
				{
					var asdf = getPOValue(theindex,3,cid);	
					for (var cOpt in names[opt]['options'])
					{
						var opts = document.getElementById('po'+opt+theindex+'pricediff_'+cOpt);
						tmpModIndex = cOpt;
						theDiff = modifiers[cid][tmpModIndex][0] - tmpVal;
						var tmpDisp = opts.innerHTML;
						var tmpPos = (tmpDisp.indexOf(currency_symbol) > -1 ? tmpDisp.indexOf(currency_symbol)-2:tmpDisp.length); 
						toDisplay = tmpDisp.substring(0,tmpPos);

						if (theDiff == 0)
						{
							opts.innerHTML = toDisplay;
							//document.getElementById('po'+opt+theindex+'_'+tmpModIndex).style.border = '2px solid black';
						}
						else
						{
							opts.innerHTML = ''+toDisplay+' ['+(theDiff > 0 ? '+':'-')+currency_symbol+price_format(Math.abs(theDiff))+']';
							//document.getElementById('po'+opt+theindex+'_'+tmpModIndex).style.border = '';
						}
						//document.getElementById('po'+opt+theindex+'_'+tmpModIndex).style.border = (tmpModIndex==asdf ? '2px solid black' : '');
						document.getElementById('po'+opt+theindex+'_'+tmpModIndex).style.border = '';
					}
					document.getElementById('po'+opt+theindex+'_'+asdf).style.border = '2px solid black';					
				}									
			}			
		}		
	}

	if (document.getElementById('product_avail_'+pid))
	{
		if (lastNonSkip == -1)
			document.getElementById('product_avail_'+pid).disabled = false;
		else if (document.getElementById('po'+lastNonSkip+theindex).disabled == false && getPOValue(theindex,getOptType(lastNonSkip+theindex),lastNonSkip) != 'toSelect')
			document.getElementById('product_avail_'+pid).disabled = false; //need to look at previous option if its enabled
		else
		{
			document.getElementById('product_avail_'+pid).disabled = true;
			document.getElementById('product_avail_'+pid).value = min_avail;
		}
	}	
	
}
