// $Id: func.js,v 1.16.2.15 2007/01/05 08:06:25 max Exp $

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

var current_variant_options = {};
var current_options = {};

/*
	Get current variantid
*/
function current_variantid() {
	var variantid = false;

	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 (variantid) {
		variant_quantity_compare(variantid);
	}

	return variantid;
}

function next_quantity_variantid(o_variantid) {
	var q_class = class_hash['Quantity'];
	if (names[q_class].options.length == 0)
		return false;

	var o_hash = {};
	for (var c in variants[o_variantid][1]) {
		o_hash[c] = variants[o_variantid][1][c];
	}

	var ov = ov_o = o_hash[q_class];
	var do_nv = false;

	for (o in names[q_class].options) {
		if (do_nv) {
			ov = o;
			break;
		}

		if (o == ov)
			do_nv = true;
	}
	o_hash[q_class] = ov;


	var n_variantid = false;
	for (var x in variants) {
		if (variants[x][1].length == 0)
			continue;

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

		if (n_variantid)
			break;
	}

	return n_variantid;
}

function variant_quantity_compare(o_variantid) {
	var n_variantid = next_quantity_variantid(o_variantid);
	var o_variant = variant_compare_data(o_variantid);

	var p_diff = 0;
	var q_diff = 0;
	for (i=0; i<5; i++) {
		var n_variant = variant_compare_data(n_variantid);

		if (o_variant.rate > n_variant.rate) {
			var p_diff = n_variant.price-o_variant.price;
			var q_diff = parseInt(n_variant.quantity-o_variant.quantity);

			var o_rate = o_variant.price/o_variant.quantity;
			var n_rate = n_variant.price/n_variant.quantity;
			var m_rate = p_diff/q_diff;

			var q_diff_e = round(p_diff/q_diff*1000)/10;
			break;
		} else {
			n_variantid = next_quantity_variantid(n_variantid);
			if (!n_variantid)
				break;
		}
	}

	var qm = "";
	if (p_diff) {
		var ps = 100-Math.floor(m_rate/o_rate*100);
		if (ps >= 20) {
			var ps_s = "<span class=\""+((ps >= 70)?"q_save":"")+"\">"+ps+"%</span>";
			var qd_s = "<span class=\""+((p_diff <= o_variant.price)?"q_save":"")+"\">"+price_format(q_diff, ",", ".", 0)+"</span>";
			qm += "Save "+ps_s+" on the next "+qd_s;
		}

		var nq_s = price_format(n_variant.quantity, ",", ".", 0);
		if (p_diff <= o_variant.price) {
			if (ps >= 20)
				qm += " for just <span class=\"q_save\">$"+price_format(p_diff)+"</span>.";
			else
				qm += "Only $"+price_format(p_diff)+" for "+price_format(q_diff, ",", ".", 0)+" more. ";
		} else {
			if (ps >= 20)
				qm += ". Just <span class=\"q_save\">$"+price_format(n_variant.price)+"</span> for <span class=\"q_save\">"+nq_s+"</span>. ";
			else
				qm += nq_s+" are just $"+price_format(n_variant.price)+". ";
		}

		if (ps < 20) {
			if (q_diff_e <= 1) {
				if (q_diff_e < .51) {
					qm += "That's better than <span class=\"q_save\">"+Math.floor(1/q_diff_e)+" a penny!</span>";
				} else {
					qm += "That's <span class=\"q_save\">"+((q_diff_e < 1)?"less than":"only")+" a penny</span> each!";
				}
			} else {
				qm += "That's just <span class=\"q_save\">"+q_diff_e+"&cent;</span> each!";
			}
		}
	}

	var q_class = class_hash['Quantity'];
	var q_field = $("#po"+q_class);
	if (!$("#q_next")[0]) {
		q_field.after("<div id='q_next' />");
	}
	$("#q_next").html(qm);

	return true;
}

function variant_compare_data(variantid) {
	var q_class = class_hash['Quantity'];

	var n_optionid = variants[variantid][1][q_class];
	var n_price = parseFloat(variants[variantid][0][0]);
	var n_quantity = parseFloat(names[q_class].options[n_optionid].replace(",", "").replace(" ", ""));
	var n_pp = n_price/n_quantity;

	return {
		"quantity": n_quantity,
		"price": n_price,
		"rate": n_pp
	}
}


/*
	Next Day shipping checks
*/
function check_is_nextday() {
	var selected_turnaround = product_option_value('Turnaround');
	var is_nextday = false;
	if (selected_turnaround) {
		is_nextday = (selected_turnaround.indexOf('Next') != -1);
	}

	return is_nextday;
}

function check_preview_reviews() {
	if (!preview_reviews)
		return true;

	error = false;
	for (p in preview_reviews) {
		if (!preview_reviews[p])
			error = p;
	}

	if (error) {
		alert("Please review all previews available for this product.\r\nClick thumbnails to view larger previews.");
		thumbnailFlash(p);
		return false;
	}

	return true;
}

function check_allow_nextday() {
	var allow_nextday = true;

	var cvariantid = current_variantid();
	if (variants[cvariantid]) {
		var allow_nextday = (variants[cvariantid][0][7] == 'Y');
	} else
		return false;

	uv_selected = product_option_value('UV_Coating');
	if (!uv_selected)
		uv_selected = product_option_value('Coating');
	if (uv_selected && (uv_selected.indexOf('Both') != -1 || uv_selected.indexOf('No UV') != -1))
		allow_nextday = false;

	return allow_nextday;
}

function calculate_postage() {
	if (!class_hash["Mailing_Services"])
		return false;

	var quantity_selected = parseInt(product_option_value("Quantity").replace(",", ""));
	var mailing_selected = false;

	var amount_element = document.getElementById("product_avail");
	if (!amount_element)
		return false;
	var amount_selected = parseInt(amount_element.options[amount_element.selectedIndex].value);

	var ml_classid = class_hash["Mailing_Services"];
	var ml_class = names[ml_classid];

	var s_val;
	s_val = product_option_value(ml_classid);

	var ml_row = document.getElementById("product_mailing_row");
	var ml_val = document.getElementById("product_mailing_price");
	if (s_val != "No") {
		var postage;
		if (s_val.indexOf('Standard') === 0) {
			postage = "22&cent; - 27&cent;/piece";
		} else {
			postage = (postage_estimate)?postage_estimate+"/piece":"32&cent; - 42&cent;/piece";
		}
		ml_val.innerHTML = postage;
		ml_row.style.display = "";
	} else {
		ml_row.style.display = "none";
	}

	if (class_hash["Mailing_Services_Return"]) {
		var mle_classid = class_hash["Mailing_Services_Return"];
		var mle_class = names[mle_classid];
		var mle_row = document.getElementById("porow"+mle_classid);
		var mle_el = document.getElementById("po"+mle_classid);

		mle_row.style.display = (s_val != "No")?"":"none";

		if (s_val == "No") {
			var _options = mle_el.options;
			var si=0;
			for (i=0; i<_options.length; i++) {
				if (_options[i].value == "No")
					si=i;
			}
			mle_el.selectedIndex = si;
		}
	}

	if (class_hash["Mailing_Services_Filename"]) {
		var mlf_classid = class_hash["Mailing_Services_Filename"];
		var mlf_class = names[mlf_classid];
		var mlf_row = document.getElementById("porow"+mlf_classid);
		var mlf_el = document.getElementById("po"+mlf_classid);

		mlf_row.style.display = (s_val != "No")?"":"none";
		if (s_val == "No")
			mlf_el.value = "";
	}

	return true;
}

/*
	Rebuild page if some options is changed
*/
function check_options(rebuild) {
	var local_taxes = [];
	var is_rebuild_wholesale = false;
	var variantid = false;
	var rebuild = (rebuild)?true:false;

	var has_exceptions = false;
	if (typeof variants_exceptions != "undefined") {
		for (v in variants_exceptions) {
			has_exceptions = true;
			break;
		}
	}

	if (!has_exceptions) {
		check_exceptions_next_day();
	}
	check_exceptions_color();
	check_dropship();

	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;
	}

/* DION :: Variants Exceptions */
	current_options = {};
	for (c in names) {
		if (!names[c].options)
			continue;
		if (names[c].options.length == 0)
			continue;

		var i = 0;
		var si = 0;
		var c_options = names[c].options;

		var sel = document.getElementById("po"+c);
		var sel_val;
		if (typeof sel.selectedIndex == 'undefined') {
			sel_val = sel.value;
		} else {
			sel_val = sel.options[sel.selectedIndex].value;
		}

		current_options[c] = sel_val;
	}

	if (variantid) {
		var exceptions = false;
		var options_exceptions = {};
		var unallowed_options = {};

		if (typeof variants_exceptions != "undefined") {
			if (typeof variants_exceptions[variantid] != "undefined")
				exceptions = variants_exceptions[variantid];
		}

		if (exceptions) {
			for (e in exceptions) {
				var opts = exceptions[e];
				for (c in opts) {
					if (typeof options_exceptions[opts[c]] == "undefined")
						options_exceptions[opts[c]] = {};

					var oe = {};
					for (cc in opts) {
						oe[cc] = opts[cc];
					}
					options_exceptions[opts[c]][e] = oe;
				}
			}
		}

		for (c in current_options) {
			var o = current_options[c];
			if (typeof options_exceptions[o] == "undefined")
				continue;

			var exs = options_exceptions[o];

			for (e in exs) {
				var ex = exs[e];
				var ex_count = 0;
				var match_count = 0;
				var _uo = [];

				for (ec in ex) {
					var cv = current_options[ec];
					if (cv == ex[ec]) {
						match_count++;
					} else {
						_uo.push(ex[ec]);
					}

					ex_count++;
				}

				if (ex_count == match_count+1) {
					for (i in _uo)
						unallowed_options[_uo[i]] = 1;
				}
			}
		}
	}

/* DION :: Variants Options */
	current_variant_options = false;
	if (variantid) {
		if (typeof variants_options_details != "undefined")
			if (typeof variants_options_details[variantid] != "undefined")
				current_variant_options = variants_options_details[variantid];
	}

/* DION :: Variants Options Turnaround */
	var turnaround_range = variants[variantid][0][6];
	var turnaround_adjust = [];
			turnaround_adjust[0] = 0;
			turnaround_adjust[1] = 0;

	if (current_variant_options) {
		for (c in current_options) {
			var o = current_options[c];
			if (typeof current_variant_options[o] == "undefined")
				continue;
			if (typeof current_variant_options[o].option_turnaround == "undefined")
				continue;

			var oto = current_variant_options[o].option_turnaround;
			if (oto == "")
				continue;

			var otoc = oto.split("-");
			if (typeof otoc[1] == "undefined")
				otoc[1] = otoc[0];

			turnaround_adjust[0] += parseInt(otoc[0]);
			turnaround_adjust[1] += parseInt(otoc[1]);
		}

		if (typeof turnaround_adjust[0] != "undefined") {
			var toc = turnaround_range.split("-");
			if (typeof toc[1] == "undefined")
				toc[1] = toc[0];

			toc[0] = parseInt(toc[0])+turnaround_adjust[0];
			toc[1] = parseInt(toc[1])+turnaround_adjust[1];

			turnaround_range = toc[0];
			if (toc[0] != toc[1])
				turnaround_range += "-"+toc[1];
		}
	}

/* DION :: Variants Options */
	if (variantid) {
		if (typeof variants_options_details != "undefined") {

			for (c in names) {
				if (!names[c].options)
					continue;
				if (names[c].options.length == 0)
					continue;

				var i = 0;
				var si = 0;
				var c_options = names[c].options;

				var sel = document.getElementById("po"+c);
				var sel_val;
				if (typeof sel.selectedIndex == 'undefined') {
					sel_val = sel.value;
				} else {
					sel_val = sel.options[sel.selectedIndex].value;
					sel.options.length = 0;

					for (var o in c_options) {
						if (typeof unallowed_options[o] != "undefined") {
							continue;
						}

						if (typeof(current_variant_options[o]) == "undefined" || current_variant_options[o].option_avail == "Y") {
							var n_text = c_options[o];
							var n_price = false;
							if (typeof(current_variant_options[o]) != "undefined") {
								if (current_variant_options[o].option_charge != "0.00")
									n_price = current_variant_options[o].option_charge;
							} else if (typeof(modifiers[c]) != "undefined") {
								if (typeof(modifiers[c][o]) != "undefined" && modifiers[c][o][0] != "0.00")
									n_price = modifiers[c][o];
							}
							if (n_price) {
								n_pos = (n_price > 0);
								n_price = (n_pos)?n_price:(-1*n_price);
								if (names[c].class_name != "Coating")
									n_text += " ("+((n_pos)?"+":"-")+" $"+parseFloat(n_price).toFixed(2)+")";
							}

							var n_option = new Option(n_text, o);
							sel.options[i] = n_option;

							if (o == sel_val)
								si = i;
							i++;
						}
					}

					sel.selectedIndex = si;
				}
			}

			check_dropship();
		}
	}

	calculate_postage();

	/* If variant found ... */
	if (variantid) {
		price = variants[variantid][0][0];
		orig_price = variants[variantid][0][4];
		avail = variants[variantid][0][1];
		turnaround = variants[variantid][0][6];
		nextday = variants[variantid][0][7];
		nextday_price = variants[variantid][0][8];

		/* 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],
					[],
					variants[variantid][3][t][5]
				];

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

		var is_nextday = check_is_nextday();
		if (is_nextday && nextday =='Y') {
			price += nextday_price;
		}

		/* 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";

		if (document.getElementById('product_turnaround')) {
			document.getElementById('product_turnaround').innerHTML = (is_nextday)?'Next Business Day':(turnaround_range+" Business Days");
		}
		if (document.getElementById('product_turnaround_box'))
			document.getElementById('product_turnaround_box').style.display = (variants[variantid][0][6] != '') ? "" : "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((is_nextday)?nextday_price:0);

	/* Update form elements */
	/* Update price */
	if (document.getElementById('product_price'))
		document.getElementById('product_price').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;

	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)
				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+"<br /><br />");

	for (cname in class_hash) {
		cid = class_hash[cname];

		if (names[cid]['options']) {
			select_obj = document.getElementById('po'+cid);
			if (cname == 'FREE_Flyers') {
				display_notice = true;
			} else
				display_notice = (select_obj.selectedIndex !== 0);
			notice_obj = document.getElementById('notice_'+cname);

			if (notice_obj) {
				notice_obj.style.display = (display_notice)?'':'none';
			}
		}
	}

	return true;
}

function check_dropship() {
	if (!class_hash['Drop_Ship'])
		return true;

	is_dropship = (product_option_value('Drop_Ship') == "Yes");
	document.getElementById('Drop_Ship_Head').style.display=(is_dropship)?'':'none';

	for (cname in class_hash) {
		cid = class_hash[cname];
		if (cname.indexOf('Drop_Ship_') == -1)
			continue;

		required_classes[cid] = is_dropship;
		document.getElementById('porow'+cid).style.display=(is_dropship)?'':'none';
	}
}

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

/* DION :: Variants Options */
	var variantid = current_variantid();

	/* 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];
		if (typeof(current_variant_options[value]) != "undefined") {
			return_price += parseFloat(current_variant_options[value].option_charge);
		} else {
			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
*/
var initial_check = false;
function check_exceptions() {
	check_mailing();

	if (!initial_check) {
		initial_check = true;
	}

	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;
}

function check_exceptions_next_day() {
	allow_nextday = check_allow_nextday();

	var turnaroundid = false;
	var turnaroundrow = false;
	var proofid = false;
	var proofrow = false;
	var roundid = false;
	var roundrow = false;

	if (class_hash['Turnaround']) {
		turnaround_class = class_hash['Turnaround'];
		turnaroundid = "po"+class_hash['Turnaround'];
		turnaroundrow = "porow"+class_hash['Turnaround'];
	}

	if (class_hash['PDF_Proof']) {
		proof_class = class_hash['PDF_Proof'];
		proofid = "po"+class_hash['PDF_Proof'];
		proofrow = "porow"+class_hash['PDF_Proof'];
	}

	if (class_hash['Round_Corner']) {
		round_class = class_hash['Round_Corner'];
		roundid = "po"+class_hash['Round_Corner'];
		roundrow = "porow"+class_hash['Round_Corner'];
	}

	if (!turnaroundid)
		return false;

	if (document.getElementById(turnaroundid) && document.getElementById(turnaroundid).options) {
		if (!allow_nextday) {
			document.getElementById(turnaroundid).options.selectedIndex = 0;
		}
		document.getElementById(turnaroundrow).style.display = (allow_nextday)?'':'none';
	}

	if (turnaroundid) {
		var is_nextday = check_is_nextday();
		if (document.getElementById(proofid)) {
			if (is_nextday && document.getElementById(proofid).options.selectedIndex > 0)
				document.getElementById(proofid).options.selectedIndex = 0;
			document.getElementById(proofrow).style.display = (is_nextday)?'none':'';
		}
		if (document.getElementById(roundid)) {
			if (is_nextday && document.getElementById(roundid).options.selectedIndex > 0)
				document.getElementById(roundid).options.selectedIndex = 0;
			document.getElementById(roundrow).style.display = (is_nextday)?'none':'';
		}
	}
}

function check_exceptions_color() {
	var selected_values = new Array();

	var has_back = false;
	var color_classid = "";
	var color_options = "";
	var color_select = "";

	if (class_hash['Color']) {
		color_classid = class_hash['Color'];
		color_options = names[color_classid]['options'];
		color_select = document.getElementById('po'+color_classid);
	}

	if (class_hash['Standard_Back']) {
		has_back = true;
	}

	if (!has_back) {
		return true;
	}

	var back_selected = false;
	back_selected = (product_option_value('Standard_Back') != 'None')?true:false;
	var color_selected_value = "";
	color_selected_value = product_option_value('Color');

	color_select.options.length = 0;
	i = 0;
	for (o in color_options) {
		if (back_selected && color_options[o].indexOf('4/0') == -1) {
			color_select.options[i] = new Option(color_options[o], o);
			if (color_options[o] == color_selected_value)
				color_select.selectedIndex = i;
			i++;
		} else if (!back_selected && color_options[o].indexOf('4/0') != -1) {
			color_select.options[i] = new Option(color_options[o], o);
			if (color_options[o] == color_selected_value)
				color_select.selectedIndex = i;
			i++;
		}
	}
}

function check_required_classes() {
	var message = "";
	var error=false;

	if (!required_classes)
		return true;

	for (c in required_classes) {
		if (required_classes[c] == false)
			continue;

		var value=product_option_value(c);
		if (!value) {
			message += "\n- "+names[c]['class_name'];
			error = true;
		}
	}
	if (error) {
		alert("Please provide information for all required fields:"+message+"\nThank you.");
		return false;
	}

	return true;
}

/*
	Rebuild wholesale tables
*/
function rebuild_wholesale(nextday_price) {
	if (!nextday_price)
		nextday_price = 0;

	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 (i == 0) {
			str += '<br /><table cellpadding="2" cellspacing="1" class="ProductPricingBox">';
			str += '<tr class="ProductPricingQty">';
			str += '<td class="ProductPricingLabel">'+lbl_quantity+':&nbsp;</td>';
		}

		var qty = (product_wholesale[x][0] == 0)?1:product_wholesale[x][0];
		str += '<td>'+qty;
		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_set : lbl_sets)+'</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>';
	str += '<tr class="ProductPricingPrice">';
	str += '<td class="ProductPricingLabel">'+lbl_price+(tax_str.length > 0 ? '*' : '')+':&nbsp;</td>';

	var cvartiantid = current_variantid();
	var nextday = variants[cvartiantid][0][7];
	for (var x in product_wholesale) {
		ws_price = product_wholesale[x][2];
		if (check_is_nextday() && nextday == 'Y') {
			ws_price += (product_wholesale[x][4])?product_wholesale[x][4]:nextday_price;
		}
		str += '<td align="right">'+currency_symbol+price_format(ws_price < 0 ? 0 : ws_price)+'</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) {
	calculate_postage();

	if (product_wholesale.length == 0)
		return true;

	var wl_taxes = current_taxes.slice(0);
	var wl_price = price;
	var wl_nextday_price = 0;
	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);
			wl_nextday_price = product_wholesale[x][4];
			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 (!found && qty) {
		var idx = x-1;
		wl_price = product_wholesale[idx][2];
		wl_taxes = product_wholesale[idx][3].slice(0);
		wl_nextday_price = product_wholesale[idx][4];
		found = true;
	}

	var cvartiantid = current_variantid();
	var nextday = variants[cvartiantid][0][7];
	if (check_is_nextday() && nextday == 'Y') {
		wl_price += wl_nextday_price;
	}

	if (document.getElementById('product_price'))
		document.getElementById('product_price').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 opt_type = document.getElementById('po'+c).tagName.toUpperCase();

	if (!document.getElementById('po'+c) || (opt_type != 'SELECT' && opt_type != 'INPUT'))
		return false;

	if (opt_type == 'INPUT')
		return document.getElementById('po'+c).value;
	else
		return document.getElementById('po'+c).options[document.getElementById('po'+c).selectedIndex].value;
}

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

	if (!class_hash)
		return false;

	if (class_hash[classid])
		return document.getElementById('po'+class_hash[classid]);

	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;
}