/*
	Get product option value by class name / or class id
*/
function rebuildOptions(classid)
{
	/* Find variant */
	var viable_names=[];
	var inviable_names;
	if(cutoff == 'Y')
	   inviable_names = check_variants_onclick(classid);

	// See if the selected options have an exception
	for (var optionid in names[classid]['options'])
	{
		if(check_exceptions_onclick(classid, optionid) )
		{
			viable_names[viable_names.length] = optionid;
		}
	}
	var selected_option = getPOValue(classid);

	document.getElementById('po'+classid).options.length=0;
	//document.getElementById('po'+classid).options[0]=new Option('Select','',false,bselected);

	if(select_selectbox == 'Y')
	{
		var elOptNew = document.createElement('option');
		elOptNew.text = '--Select--';
		elOptNew.value = '';
		document.getElementById('po'+classid).options[0] = new Option(elOptNew.text,elOptNew.value,false,false);
	}


	//populate dropdown
	for (var key in viable_names) {
	   opt=viable_names[key];
	   if(inviable_names && inviable_names[opt]){}
	   else{
		   var option_text = names[classid]['options'][opt];
		   try
		   {
			if (modifiers[classid][opt][0] != 0)
			{
				option_text += " ("+currency_symbol+price_format(modifiers[classid][opt][0])+")";
			}
		   }
		   catch(err)
		   {}
		   bselected = (selected_option==opt);
		   document.getElementById('po'+classid).options[document.getElementById('po'+classid).options.length]=new Option(option_text,opt,false,bselected);
	   }
	}
}

function check_exceptions_onclick(classid, optionid) {
	if (!exceptions)
		return true;

	/* List exceptions */
	for (var x in exceptions) {
		if (isNaN(x))
			continue;

		var found = true;
        for (var c in exceptions[x]) {
			if(c != classid)
			{
				var value = getPOValue(c);
				if (!value)
					return true;

				if (value != exceptions[x][c]) {
					found = false;
					break;
				}
			}
			else
			{
				var value = optionid;
				if (!value)
					return true;

				if (value != exceptions[x][c]) {
					found = false;
					break;
				}
			}
		}
		if (found)
			return false;
	}
	return true;
}

function check_variants_onclick(vclassid) {
	inviable_names = [];
	var count_empty_variants = 0;
	var count_empty_variants_this_class = 0;
	for(variantid in variants)
	{
		if(variants[variantid][0][1] == 0)
		{
			count_empty_variants++;
			var found = 0;
			var total = 0;
			for(classid in variants[variantid][1])
		   {
			   if(classid != vclassid && getPOValue(classid) == variants[variantid][1][classid])
			   {
				   count_empty_variants_this_class++;
				   found++;
			   }
			   total++;
		   }
		    if (found == (total-1))
		   {
			   inviable_names[variants[variantid][1][vclassid]] = variants[variantid][1][vclassid];
		   }
		}
	}
	return inviable_names;
}

function check_variants_selected(){
	var can_submit=true;
	for (var classid in names)
	{
		if(document.getElementById('po'+classid).value == '')
		{
			can_submit=false;
			var this_dropdown = names[classid].class_name;
			alert("Please select a value for "+this_dropdown);
			break;
		}
	}
	return can_submit;
}

function check_exceptions2(classid)
{
        var clids=[];

        for (var clid in names)
        {
            if (!check_element(clids,clid))
            {
               clids[clids.length]=clid;
            }
        }

        bal_clids=[];

        addclass=false;
        for (i=0; i < clids.length; i++)
        {
            if (clids[i]==classid)
            {
               if (i==0)
                  break;

               addclass=true;
               continue;
            }
            if (addclass && i > 1)
            {
	            bal_clids[bal_clids.length]=clids[i];
                break;
            }
        }
       selindex=document.getElementById('po'+classid).options.selectedIndex;

        rIndex=[];
        if (bal_clids.length > 0)
        {
            for(l=0; l<bal_clids.length; l++)
            {
                clid=bal_clids[l];

                for(i=0; i<document.getElementById('po'+classid).options.length; i++)
	            {
	              document.getElementById('po'+classid).options.selectedIndex=i;

	              viable_options=[];
	              for (var optionid in names[clid]['options'])
	              {
	                if(check_exceptions_onclick(clid, optionid) )
	                {
	                    viable_options[viable_options.length] = optionid;
	                    break;
	                }
	              }
	              if (viable_options.length==0)
	                 rIndex[rIndex.length]=i;

            }
           }
        }
        if (parseInt(selindex) >= 0)
	        document.getElementById('po'+classid).options.selectedIndex=selindex;

        for(j=rIndex.length-1; j>=0; j--)
        {
            document.getElementById('po'+classid).remove(rIndex[j]);
        }
}

function check_element(a,value) {
	var i;
	for (i=0; i < a.length; i++) {
		if (a[i] === value) {
			return true;
		}
	}
  	return false;
};

