// $Id: func.js,v 1.16.2.17 2007/09/25 13:23:12 ferz Exp $

var current_taxes = [];
var product_thumbnail = document.getElementById('product_thumbnail');
var availObj = document.getElementById('product_avail');

/*
	Rebuild page if some options is changed
*/
function check_options(doresize) {
	var local_taxes = [];
	var is_rebuild_wholesale = false;
	var variantid = false;
	var tmp_visibility;

	for (var t in taxes)
		local_taxes[t] = taxes[t][0];
	price = default_price;

	/* Find variant */
	for (var x in variants) {
		if (variants[x][1].length == 0)
			continue;

		variantid = x;
		for (var c in variants[x][1]) {
			if (getPOValue(c) != variants[x][1][c]) {
				variantid = false;
				break;
			}
		}

		if (variantid)
			break;
	}

	/* If variant found ... */
	if (variantid) {
		price = variants[variantid][0][0];
		orig_price = variants[variantid][0][4];
		avail = variants[variantid][0][1];

		/* Get variant wholesale prices */
		if (variants[variantid][3]) {
			product_wholesale = [];
			for (var t in variants[variantid][3]) {
				var _tmp = modi_price(variants[variantid][3][t][2], cloneObject(variants[variantid][3][t][3]), variants[variantid][3][t][4]);
				product_wholesale[t] = [
					variants[variantid][3][t][0], 
					variants[variantid][3][t][1], 
					_tmp[0],
					[]
				];

				/* Get variant wholesale taxes */
				for (var c in _tmp[1]) {
					product_wholesale[t][3][c] = _tmp[1][c];
				}
			}
			is_rebuild_wholesale = true;
		}

		/* Get variant taxes */
		for (var t in local_taxes) {
			if (variants[variantid][2][t])
				local_taxes[t] = parseFloat(variants[variantid][2][t]);
		}

		if (!product_thumbnail)
			product_thumbnail = document.getElementById('product_thumbnail');

		/* Change product thumbnail */
		if (product_thumbnail) {
			if (variants[variantid][0][2].src && variants[variantid][0][2].width > 0 && variants[variantid][0][2].height > 0) {
				if (product_thumbnail.src != variants[variantid][0][2].src) {
					product_thumbnail.src = variants[variantid][0][2].src;
					product_thumbnail.width = variants[variantid][0][2].width;
					product_thumbnail.height = variants[variantid][0][2].height;
				}
			} else if (document.getElementById('product_thumbnail').src != product_image.src) {
				product_thumbnail.src = product_image.src;
				if (product_image.width > 0 && product_image.height > 0) {
					product_thumbnail.width = product_image.width;
					product_thumbnail.height = product_image.height;
				}
			}
		}

		/* Change product weight */
		if (document.getElementById('product_weight'))
			document.getElementById('product_weight').innerHTML = price_format(variants[variantid][0][3]);
		if (document.getElementById('product_weight_box'))
			document.getElementById('product_weight_box').style.display = parseFloat(variants[variantid][0][3]) > 0 ? "" : "none";

		/* Change product code */
		if (document.getElementById('product_code'))
			document.getElementById('product_code').innerHTML = variants[variantid][0][5];

	}

	if (pconf_price > 0)
		price = pconf_price;

	/* Find modifiers */
	var _tmp = modi_price(price, local_taxes, orig_price);
	price = _tmp[0];
	local_taxes = _tmp[1];
	if (!variantid) {
		product_wholesale = [];
		for (var t in _product_wholesale) {
			_tmp = modi_price(_product_wholesale[t][2], _product_wholesale[t][3].slice(0), _product_wholesale[t][4]);
			product_wholesale[t] = [
				_product_wholesale[t][0],
				_product_wholesale[t][1],
				_tmp[0],
				_tmp[1]
			];
		}
		is_rebuild_wholesale = true;
	}

	/* Update taxes */
	for (var t in local_taxes) {
		if (document.getElementById('tax_'+t)) {
			document.getElementById('tax_'+t).innerHTML = currency_symbol+price_format(local_taxes[t] < 0 ? 0 : local_taxes[t]);
		}
		current_taxes[t] = local_taxes[t];
	}

	if (is_rebuild_wholesale)
		rebuild_wholesale();

	/* Update form elements */
	tmp_visibility = displayInvisible();
	doresize = doresize || tmp_visibility;
	check_mandatory();
	tmp_visibility = check_child_products();
	doresize = doresize || tmp_visibility;
	/* Update price */
	change_parent_price();
	calculate_child_modifiers();
	applySKUModifiers();
	if (document.getElementById('product_price'))
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(price < 0 ? 0 : price);
	if (document.getElementById('product_price2'))
                document.getElementById('product_price2').innerHTML = currency_symbol+price_format(price < 0 ? 0 : price);

	/* Update alt. price */
	if (alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price')) {
		var altPrice = price*alter_currency_rate;
		document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(altPrice < 0 ? 0 : altPrice)+")";
	}

	/* Update Save % */
	if (document.getElementById('save_percent') && document.getElementById('save_percent_box') && list_price > 0 && dynamic_save_money_enabled) {
		var save_percent = Math.round(100-(price/list_price)*100);
		if (save_percent > 0) {
			document.getElementById('save_percent_box').style.display = '';
			document.getElementById('save_percent').innerHTML = save_percent;
		} else {
			document.getElementById('save_percent_box').style.display = 'none';
			document.getElementById('save_percent').innerHTML = '0';
		}
	}

	/* Update product quantity */
	if (document.getElementById('product_avail_txt')) {
		if (avail > 0) {
			document.getElementById('product_avail_txt').innerHTML = substitute(txt_items_available, "items", (variantid ? avail : product_avail));
		} else {
			document.getElementById('product_avail_txt').innerHTML = lbl_no_items_available;
		}
	}

	if ((mq > 0 && avail > mq+min_avail) || is_unlimit)
		avail = mq+min_avail-1;

	avail = avail > mq ? mq : avail;

	var select_avail = min_avail;
	/* Update product quantity selector */
	if (!availObj)
		availObj = document.getElementById('product_avail');

	if (availObj && availObj.tagName.toUpperCase() == 'SELECT') {
		if (!isNaN(min_avail) && !isNaN(avail)) {
			var first_value = -1;
			if (availObj.options[0])
				first_value = availObj.options[0].value;

			if (first_value == min_avail) {

				/* New and old first value in quantities list is equal */
				if ((avail-min_avail+1) != availObj.options.length) {
					if (availObj.options.length > avail) {
						var cnt = availObj.options.length;
						for (var x = (avail < 0 ? 0 : avail); x < cnt; x++)
							availObj.options[availObj.options.length-1] = null;
					} else {
						var cnt = availObj.options.length;
						for (var x = cnt+1; x <= avail; x++)
							availObj.options[cnt++] = new Option(x, x);
					}
				}
			} else {

				/* New and old first value in quantities list is differ */
				while (availObj.options.length > 0)
					availObj.options[0] = null;
				var cnt = 0;
				for (var x = min_avail; x <= avail; x++)
					availObj.options[cnt++] = new Option(x, x);
			}
			if (availObj.options.length == 0 || min_avail > avail)
				availObj.options[0] = new Option(txt_out_of_stock, 0);
		}
		select_avail = availObj.options[availObj.selectedIndex].value;
	}
	check_wholesale(select_avail);

	if ((alert_msg == 'Y') && (min_avail > avail))
		alert(txt_out_of_stock);
	
	/* Check exceptions */
	var ex_flag = check_exceptions();
	if (!ex_flag && (alert_msg == 'Y'))
		alert(exception_msg);
			
	if (document.getElementById('exception_msg'))
		document.getElementById('exception_msg').innerHTML = (ex_flag ? '' : exception_msg_html);

	if (doresize) {
		//$.fn.colorbox.resize();
        gboxResizeFunc();
		setTimeout('restoreCheckBoxes()', 310);
	}
	return true;
}

/*
 * Shows/Hides child products depending on the parent options
*/

function check_child_products()
{
	var tmp_visibility = false;
	if (typeof(child_products_avail)!="undefined") {
		var j, k, found, selects = document.getElementById('productOptions').getElementsByTagName('span');
		var to_hide = new Array();
		for (var i in child_products_avail) {
			found = (child_products_avail[i][j] == '0');
			if (!found)
				for (j=0; j<child_products_avail[i].length; ++j) {
					for (k=0; k<selects.length; ++k)
						if (selects[k].id && (child_products_avail[i][j] == extractValueFromId(selects[k].id))) {
							found = true;
							break;
						}
				}
			if (!found)
				to_hide.push(i);
		}
		var child_products_rows = document.getElementById('child_products_tbl').getElementsByTagName('div');
		var input;
		for (i=0; i<child_products_rows.length; ++i) {
			if (child_products_rows.id && child_products_rows.id.indexOf('child_product_row_') != 0)
				continue;
			found = false;
			for (j=0; j<to_hide.length; ++j)
				if (child_products_rows[i].id && child_products_rows[i].id == 'child_product_row_'+to_hide[j]) {
					found = true;
					break;
				}
			if (child_products_rows[i].id && child_products_rows[i].id.indexOf("child_product_row_") == 0) {
				tmp_visibility = tmp_visibility || ((found && child_products_rows[i].style.display == '') || (!found && child_products_rows[i].style.display == 'none'));
				child_products_rows[i].style.display = found?'none':'';
			}
			if (found) {
				input = child_products_rows[i].getElementsByTagName('ul');
				for (j=0; j<input.length; ++j)
					if (input[j].id && input[j].id.match(/^po[0-9]$/))
						resetUlSelect(input[j]);
				input = child_products_rows[i].getElementsByTagName('input');
				for (j=0; j<input.length; ++j) {
					if (input[j].type.toLowerCase() == 'checkbox')
						input[j].checked = false;
					else if (input[j].type.toLowerCase() == 'hidden')
						input[j].value = '';
				}
				input = child_products_rows[i].getElementsByTagName('ul');
				for (j=0; j<input.length; ++j)
					if (input[j].id && extractValueFromId(input[j].id))
						resetUlSelect(input[j]);
			}
		}
		return tmp_visibility;
	}
	else
		return false;
}

/*
 * Changes the parent product price based on the selected child products & options
*/

function change_parent_price()
{
	var parent = document.getElementById('child_products_tbl');
	if (!parent)
		return false;
	var j, child_products_inputs = parent.getElementsByTagName('input');
	for (var i=0; i<child_products_inputs.length; ++i)
		if (child_products_inputs[i].checked)
			for (j=0; j<child_products.length; ++j)
				if (child_products_inputs[i].id && child_products_inputs[i].id == 'child_product_input_'+child_products[j].productid) {
					price+= child_products[j].price;
					break;
				}	
}

/*
 * Shows/Hides options depending on the selected control option
*/

function check_mandatory(child_product)
{
	var j, k, found = false;
	var display_switch = false;
	if (child_product) {
		var tmp_mandatory_optionids = child_mandatory_optionids[child_product];
	}
	else {
		var tmp_mandatory_optionids = mandatory_optionids;
	}
	for (var i in tmp_mandatory_optionids)
		if (tmp_mandatory_optionids[i]>0) {
			display_switch = true;
			break;
		}
	if (!display_switch)
		return true;
	
	var parent_div = document.getElementById('selectOptWin');
	if (!parent_div)
		return true;
		
	var selects = parent_div.getElementsByTagName('ul');
	
	if (child_product) {
		var options = document.getElementById('child_product_options_tbl_'+child_product).getElementsByTagName('li');
		var selects = document.getElementById('child_product_options_tbl_'+child_product).getElementsByTagName('ul');
	}
	else {
		var options = document.getElementById('productOptions').getElementsByTagName('li');
		var selects = document.getElementById('productOptions').getElementsByTagName('ul');
	}
	
	var opt_value;
	for (i=0; i<options.length; ++i) {
		if (!options[i].id)
			continue;
		opt_value = extractValueFromId(options[i].id);
		if (options[i] && opt_value>-1 && options[i].parentNode.id && options[i].parentNode.id.indexOf('po')==0 && tmp_mandatory_optionids[opt_value]) {
			found = false;
			for (j=0; j<selects.length; ++j)
				if (tmp_mandatory_optionids[opt_value]==extractValueFromId(getUlSelIndex(selects[j]).id)) {
					found = true;
					break;
				}
			if (found)
				options[i].style.display = '';
			else {
				if (extractValueFromId(getUlSelIndex(options[i].parentNode).id).toString()==opt_value.toString())
					resetUlSelect(options[i].parentNode);
				options[i].style.display = 'none';
			}
		}
	}
}

/*
 * Returns the span element according to the passed ul element from the same select
*/

function getUlSelIndex(ul) {
	if (!ul)
		return false;
	var spans = ul.parentNode.getElementsByTagName('span');
	if (!spans || spans.length == 0)
		return false;
	for (var i in spans)
		if (spans[i].id && spans[i].id.indexOf('span') == 0)
			return spans[i];
	return false;
}

/*
 * Sets the select to the first available (not hidden) option
*/

function resetUlSelect(ul) {
	if (!ul)
		return false;
	var options = ul.getElementsByTagName('li');
	if (!options || options.length == 0)
		return false;
	var span = getUlSelIndex(ul);
	if (!span)
		return false;
	for (var i in options)
		if (options[i].id && options[i].style.display != 'none') {
			span.id = options[i].id.replace("opt", "span");
			span.innerHTML = options[i].innerHTML;
			break;
		}
	return true;
}

/*
 * Extracts option value from a string like span_1232_34234
*/
function extractValueFromId(id) {
	return id.toString().match(/-?[0-9]+$/);
}

/*
 * Shows/Hides selects depending on the selected options
*/
function displayInvisible(child_product)
{
	var visibility_mod = false;
	var tmp_display_classids = new Array();
	if (child_product) {
		var selects = document.getElementById('child_product_options_tbl_'+child_product).getElementsByTagName('ul');
		if (child_display_classids[child_product])
			tmp_display_classids = child_display_classids[child_product];
	}
	else {
		var selects = document.getElementById('productOptions').getElementsByTagName('ul');
		tmp_display_classids = display_classids;
	}
	var j, k;
	var to_show = new Array();
	
	for (var i in tmp_display_classids) {
		for (j=0; j<selects.length; ++j) {
			if (extractValueFromId(getUlSelIndex(selects[j]).id) == i) {
				to_show.push(tmp_display_classids[i]);
				break;
			}
		}
	}
	var options_row_id = 'options_row_';
	if (child_product)
		options_row_id = 'child_options_row_'+child_product+'_';
	var options_row_elem;
	for (i in tmp_display_classids) {
		options_row_elem = document.getElementById(options_row_id+tmp_display_classids[i]);
		if (options_row_elem) {
			found = false;
			for (j=0; j<to_show.length; ++j)
				if (to_show[j] == tmp_display_classids[i]) {
					found = true;
					break;
				}
			visibility_mod = visibility_mod || ((options_row_elem.style.display == '' && !found) || (options_row_elem.style.display == 'none' && found));
			if (!found) {
				selects = options_row_elem.getElementsByTagName('ul');
				for (k=0; k<selects.length; ++k)
					if (selects[k].id && selects[k].id.match(/^po[0-9]+$/))
						resetUlSelect(selects[k]);
			}
			options_row_elem.style.display=found?'':'none';
		}
	}
	return visibility_mod;
}

/*
	Calculate product price with price modificators 
*/
function modi_price(_price, _taxes, _orig_price) {
var return_price = round(_price, 2);

	/* List modificators */
	for (var x2 in modifiers) {
		var value = getPOValue(x2);
		if (!value || !modifiers[x2][value])
			continue;

		/* Get selected option */
		var elm = modifiers[x2][value];
		return_price += parseFloat(elm[1] == '$' ? elm[0] : (_price*elm[0]/100));

		/* Get tax extra charge */
		for (var t2 in _taxes) {
			if (elm[2][t2]) {
				_taxes[t2] += parseFloat(elm[1] == '$' ? elm[2][t2] : (_orig_price*elm[2][t2]/100));
			}
		}
	}

	return [return_price, _taxes];
}

/*
	Check product options exceptions
*/
function check_exceptions() {
	if (!exceptions)
		return true;

	/* List exceptions */
	for (var x in exceptions) {
		if (isNaN(x))
			continue;

		var found = true;
        for (var c in exceptions[x]) {
			var value = getPOValue(c);
			if (!value)
				return true;

            if (value != exceptions[x][c]) {
				found = false;
				break;
			}
		}
		if (found)
			return false;
	}

	return true;
}

/*
	Rebuild wholesale tables
*/
function rebuild_wholesale() {

	var obj = document.getElementById('wl_table');
	if (!obj)
		return false;

	/* Clear wholesale span object if product wholesale prices service array is empty */
	if (!product_wholesale || product_wholesale.length == 0) {
		obj.innerHTML = "";
		return false;
	}

	/* Display headline */
	var str = '';
	var i = 0;
	for (var x in product_wholesale) {
		if (product_wholesale[x][0] == 0)
			continue;

		if (i == 0)
			str += '<br /><table cellpadding="2" cellspacing="2"><tr class="TableHead"><td align="right"><b>'+lbl_quantity+':&nbsp;</b></td>';

		str += '<td>'+product_wholesale[x][0];
		if (x == product_wholesale.length-1) {
			str += '+';
		} else if (product_wholesale[x][0] < product_wholesale[x][1]) {
			str += '-'+product_wholesale[x][1];
		}
		str += '&nbsp;'+(product_wholesale[x][0] == 1 ? lbl_item : lbl_items)+'</td>';
		i++;
	}

	if (i == 0)
		return false;

    /* Display wholesale prices taxes */
	var tax_str = '';
    if (taxes.length > 0) {
        for (var x in taxes) {
            if (current_taxes[x] > 0)
                tax_str += substitute(lbl_including_tax, 'tax', taxes[x][1])+'<br />';
        }
    }

	/* Display wholesale prices */
	str += '</tr><tr bgcolor="#EEEEEE"><td align="right"><b>'+lbl_price+(tax_str.length > 0 ? '*' : '')+':&nbsp;</b></td>';
	for (var x in product_wholesale) {
		if (product_wholesale[x][0] == 0)
			continue;
		str += '<td>'+price_format(product_wholesale[x][2] < 0 ? 0 : product_wholesale[x][2])+'</td>';
	}

	str += '</tr></table>';

	if (tax_str.length > 0)
		str += '<br /><table><tr><td class="FormButton" valign="top"><b>*'+txt_note+':</b>&nbsp;</td><td nowrap="nowrap" valign="top">'+tax_str+'</td></tr></table>';

	str += '<br />';
	obj.innerHTML = str;

	return true;
}

/*
	Display current wholesale price as product price
*/
function check_wholesale(qty) {
	if (product_wholesale.length == 0)
		return true;

	var wl_taxes = current_taxes.slice(0);
	var wl_price = price;
	var found = false;
	for (var x = 0; x < product_wholesale.length; x++) {
		if (product_wholesale[x][0] <= qty && (product_wholesale[x][1] >= qty || product_wholesale[x][1] == 0)) {
			wl_price = product_wholesale[x][2];
			wl_taxes = product_wholesale[x][3].slice(0);
			found = true;
		}
		if (document.getElementById('wp'+x)) {
			var wPrice = price-default_price+product_wholesale[x][2];
			document.getElementById('wp'+x).innerHTML = currency_symbol+price_format(wPrice < 0 ? 0 : wPrice);
		}
	}

	if (document.getElementById('product_price'))
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(wl_price < 0 ? 0 : wl_price);
	if (document.getElementById('product_price2'))
                document.getElementById('product_price2').innerHTML = currency_symbol+price_format(wl_price < 0 ? 0 : wl_price);
	if (alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price')) {
		var altPrice = wl_price*alter_currency_rate;
		document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(altPrice < 0 ? 0 : altPrice)+")";
	}

	/* Update Save % */
	if (document.getElementById('save_percent') && document.getElementById('save_percent_box') && list_price > 0 && dynamic_save_money_enabled) {
		var save_percent = Math.round(100-((wl_price < 0 ? 0 : wl_price)/list_price)*100);
		if (save_percent > 0) {
			document.getElementById('save_percent_box').style.display = '';
			document.getElementById('save_percent').innerHTML = save_percent;
		} else {
			document.getElementById('save_percent_box').style.display = 'none';
			document.getElementById('save_percent').innerHTML = '0';
		}
	}


	for (var x in taxes) {
		if (document.getElementById('tax_'+x) && wl_taxes[x] && current_taxes[x]) {
			document.getElementById('tax_'+x).innerHTML = currency_symbol+price_format(wl_taxes[x] < 0 ? 0 : wl_taxes[x]);
		}
	}

	return true;
}

/*
	Get product option value
*/
function getPOValue(c) {
	var select = document.getElementById('po'+c);
	if (!select || select.tagName.toUpperCase() != 'UL')
		return false;
	return extractValueFromId(getUlSelIndex(select).id);
}

/*
    Get product option object by class name / class id
*/
function product_option(classid) {
	if (!isNaN(classid))
		 return document.getElementById("po"+classid);

	if (!names)
		return false;

	for (var x in names) {
		if (names[x]['class_name'] != classid)
			continue;
		return document.getElementById('po'+x);
    }

	return false;
}

/*
	Get product option value by class name / or class id
*/
function product_option_value(classid) {
	var obj = product_option(classid);
	if (!obj)
		return false;

	if (obj.type != 'select-one')
		return obj.value;

	var classid = parseInt(obj.id.substr(2));
	var optionid = parseInt(obj.options[obj.selectedIndex].value);
	if (names[classid] && names[classid]['options'][optionid])
		return names[classid]['options'][optionid];

	return false;
}

function child_check_options(child_product, doresize) {
	var tmp_visibility = displayInvisible(child_product);
	doresize = doresize || tmp_visibility;
	check_mandatory(child_product);
	check_options(doresize);
	return true;
}

function calculate_child_modifiers() {
	var parent = document.getElementById('child_products_tbl');
	if (!parent)
		return false;
	var found, j, k, select, inputs, child_price, child_productid, child_price_elem, rows = parent.getElementsByTagName('tr');
	for (var i=0; i<rows.length; ++i) {
		if (rows[i].id.indexOf('child_product_table_row_') == 0) {
			child_productid = rows[i].id.match(/[0-9]+$/);
			child_price = 0;
			for (k=0; k<child_products.length; ++k)
				if (child_products[k].productid == child_productid) {
					child_price = child_products[k].price;
					break;
				}
			select = rows[i].getElementsByTagName('ul');
			for (k=0; k<select.length; ++k) {
				inputs = rows[i].getElementsByTagName('input');
				found = false;
				for (j=0; j<inputs.length; ++j)
					if (inputs[j].type=='checkbox' && inputs[j].checked) {
						found = true;
						break;
					}
				if (!found)
					continue;
				for (j in child_modifiers)
					if (extractValueFromId(getUlSelIndex(select[k]).id) == j) {
						price+= child_modifiers[j];
						child_price+= child_modifiers[j];
					}
				child_price_elem = document.getElementById('child_product_price_'+child_productid);
				if (child_price_elem && child_price_elem.innerHTML)
					child_price_elem.innerHTML = currency_symbol+price_format(child_price < 0 ? 0 : child_price)
			}
		}
	}
}

/*
 * Restores checkboxes state after the lightbox show/hide
*/

function restoreCheckBoxes()
{
	var parent = document.getElementById('child_products_tbl');
	if (!parent)
		return false;
	var rows = parent.getElementsByTagName('tr');
	var inputs, hidden, checkbox, j;
	for (var i=0; i<rows.length; ++i) {
		inputs = rows[i].getElementsByTagName('input');
		hidden = '';
		checkbox = null;
		for (j=0; j<inputs.length; ++j)
			if (inputs[j].type.toLowerCase() == 'hidden')
				hidden = inputs[j].value;
			else if (inputs[j].type.toLowerCase() == 'checkbox')
				checkbox = inputs[j];
		if (checkbox)
			checkbox.checked = (checkbox.value == hidden);
	}
}

function checkHidden(elem, productid) {
	document.getElementById('child_product_hidden_'+productid).value=elem.checked?elem.value:'';
}

function applySKUModifiers() {
	var table = document.getElementById('productOptions');
	var selects, optionid, value, i;
	var sku_div = document.getElementById('product_code');
	if (table && sku_div) {
		selects = table.getElementsByTagName('span');
		value = sku_modifiers['0'];
		for (i=0; i<selects.length; ++i)
			if (selects.id) {
				optionid = extractValueFromId(selects.id);
				if (optionid.length>0 && sku_modifiers[optionid])
					value = sku_modifiers[optionid]['productcode'];
			}
		sku_div.innerHTML = value;
	}
	table = document.getElementById('child_products_tbl');
	if (table) {
		var productid, j, rows = table.getElementsByTagName('tr');
		for (i=0; i<rows.length; ++i)
			if (rows[i].id && rows[i].id.indexOf('child_product_table_row_')==0) {
				productid = extractValueFromId(rows[i].id);
				sku_div = document.getElementById('child_product_sku_'+productid);
				if (sku_div && child_sku_modifiers[productid]) {
					value = child_sku_modifiers[productid]['0']['productcode'];
					selects = rows[i].getElementsByTagName('span');
					for (j=0; j<selects.length; ++j)
						if (selects[j].id) {
							optionid = extractValueFromId(selects[j].id);
							if (optionid.length>0 && child_sku_modifiers[productid][optionid])
								value = child_sku_modifiers[productid][optionid]['productcode'];
						}
					sku_div.innerHTML = value;
				}
			}
	}
}

