/*
  Create dynamic product options based upon 
  entered exceptions

  Copyright 2007 Derrick Knight
  derrick@freerangeminds.com

*/
// function to restore the individual select block passed into this function by id
//
function run_once() {
	for (var z in names) {
		if (isNaN(z))
			continue;
              document.getElementById("po"+z).selectedIndex = 0;
	}
}
//
function put_back(c) {
	var inc = 0;
	for (var w in names[c]['options']) {
		if (isNaN(w))
			continue;
		var optionstring = get_optioninfo(c,w);
		var disptext = optionstring[0];
		if (optionstring[1] != '') {
			disptext += " "+optionstring[1];
		}
		if (!document.getElementById("po"+c).options[inc]) {
				//test to see if the element exists, if not, create it
				document.getElementById("po"+c).options[inc] = new Option(disptext, w);
			} else {
				//existing element, jus t update contents in case of reordering due to add/delete
				document.getElementById("po"+c).options[inc].value = w;
				document.getElementById("po"+c).options[inc].text = disptext;
			}
			inc++;
		}
}

//
// primary function being called to dynamically build the option list based upon the entries made in the existing admin function for exceptions
// didn't like the fact that the user was presented with options that they couldn't actually purchase and decided that dynamic options would be a better idea
//
function dynamic_exceptions() {
	// if there aren't any exceptions, don't waste any time here
	if (!exceptions)
		return true;
	var h = 0;

	// used the existing names array as the baseline for the existing select and option blocks
	// incrementally walk through each select block
	for (var b in names) {
		if (isNaN(b))
			continue;
	// find out which option is currently selected in this block
       h = document.getElementById("po"+b).selectedIndex;
			// since the list is dynamic, you must grab the text instead of the index to re-establish the current selection
			var sel=document.getElementById("po"+b).options[h].text;
			// rebuild the options for this select from the original array captured
			put_back(b);
			// reset the current selection, can not use the index, have to compare the text due to dynamic index
			for (var s in document.getElementById("po"+b).options) {
				// ignore option characteristics
				if (isNaN(s))
					continue;
				//set the selected option
				if (document.getElementById("po"+b).options[s].text == sel) {
					document.getElementById("po"+b).selectedIndex = s;
				}
			}
	}
	// blow array is used to store all options to be removed.
	// nasty things happen in firefox/mozilla if you try to do any removes/adds inline
	var blow = [];
	var inc = 0;
	for (var x in exceptions) {
		// parse the existing exception array and compare to the current selections
		if (isNaN(x))
			continue;
		for (var c in exceptions[x]) {
			if (isNaN(c))
				continue;
			var value = getPOValue(c);
			if (value == exceptions[x][c]) {
				//if current selection is an exception, remove all exceptions components from their option lists
				for (var z in exceptions[x]) {
					if (isNaN(z))
						continue;

						for (var y in document.getElementById("po"+z).options) {
							if (isNaN(y))
								continue;
							var docvalue = document.getElementById("po"+z).options[y].value;
							if (docvalue == exceptions[x][z] && docvalue != exceptions[x][c]) {
								//can't actually delete here, as the length of the options array will change and crash the for loop
								// just save it in an array and cleanup after the loop
								blow[inc] = []
								blow[inc][0] = y;
								blow[inc][1] = z;
								blow[inc][2] = "o";
								inc++;
							}
						}

				}
			}
		}
	}
	// now purge all items flagged for deletion
	inc--;
	blow.sort(function(a,b){return b[0] - a[0]})
	if (inc >= 0) {
//		for (j=inc; j >= 0; j--)  {
		for (j=0; j <= inc; j++)  {
			if (blow[j][2] == "o") {
				//alert("REMOVING >> po"+blow[j][1]);
				var parent = document.getElementById("po"+blow[j][1]);
				var byebye = document.getElementById("po"+blow[j][1]).options[blow[j][0]];
				if (byebye)
					var outtahere = parent.removeChild(byebye);
			} else {
				//alert("REMOVING >> checkbox"+blow[j][1]);
				var byebye = document.getElementById("checkbox"+blow[j][1]);
				if (byebye) {
					var parent = byebye.parentNode;
					while( byebye.hasChildNodes() ) {
						byebye.removeChild( byebye.lastChild );
					}
				}
			}

		}
	}
}

// new function where the classid and optionid are passed and the option text and option modifier info (if it exists) are returned 
// value returned in a single element array in the format of array[text, modifierstring]
function get_optioninfo(c, w) {
	//alert("IN GET_OPTIONINFO"+c+w);
	var optiontext = names[c]['options'][w];
	var mainname = names[c].classtext;
	var optval = '';
	var dispval = '';
	if (modifiers[c]) {
		if (modifiers[c][w]) {
			var modstring = modifiers[c][w];
			var dsign = "";
			optval = modstring[0];
			if (optval < 0) {
				dsign = "-";
			} else {
				dsign = "+";
			}
			if (modstring[1] == "$") {
				optval = Math.abs(optval);
				dispval = "("+dsign+modstring[1]+optval.toFixed(2)+")";
			} else {
				dispval = "("+optval.toFixed(2)+modstring[1]+")";
			}
			if (optval == "0")
				dispval = "";
		}
	}
	return [optiontext, dispval, mainname];
}