
function check_options() {
var x, cnt, flag, c, t, select_avail;
var local_taxes = new Array();

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

	// Find variant
	for(x = 0; x < variants.length; x++) {
		if(variants[x][1].length == 0)
			continue;
		flag = true;
		for(c in variants[x][1])
			if(document.getElementById('po'+c)) {
				if(document.getElementById('po'+c).value != variants[x][1][c]) {
					flag = false;
					break;
				}
			}

		// If variant found ...
		if(flag) {
			price = variants[x][0][0];
			orig_price = variants[x][0][4];
			avail = variants[x][0][1];
			for(t in local_taxes)
				if(variants[x][2][t])
					local_taxes[t] = parseFloat(variants[x][2][t]);
			if(variants[x][0][2] != '' && document.getElementById('product_thumbnail'))
				document.getElementById('product_thumbnail').src = image_dir+variants[x][0][2];
			if(document.getElementById('product_weight'))
				document.getElementById('product_weight').innerHTML = variants[x][0][3];
			break;
		}
	}

	if(pconf_price > 0)
		price = pconf_price;

	// Find modifiers
	var add_price = 0;
	for(x in modifiers) {
		if(document.getElementById('po'+x).value) {
			if(modifiers[x][document.getElementById('po'+x).value]) {
				add_price += parseFloat(modifiers[x][document.getElementById('po'+x).value][1] == '$'?modifiers[x][document.getElementById('po'+x).value][0]:(price*modifiers[x][document.getElementById('po'+x).value][0]/100));
				for(t in local_taxes) {
					if(modifiers[x][document.getElementById('po'+x).value][2][t]) {
						local_taxes[t] = parseFloat(local_taxes[t])+parseFloat(modifiers[x][document.getElementById('po'+x).value][1] == '$'?modifiers[x][document.getElementById('po'+x).value][2][t]:(orig_price*modifiers[x][document.getElementById('po'+x).value][0]/100*modifiers[x][document.getElementById('po'+x).value][2][t]/100));
					}
				}
			}
		}
	}
	price += add_price;

	// Update taxes
	for(t in local_taxes) {
		if(document.getElementById('tax_'+t))
			document.getElementById('tax_'+t).innerHTML = currency_symbol+price_format(local_taxes[t], 2);
		current_taxes[t] = local_taxes[t];
	}

	// Update form elements
	// Update price
	if(document.getElementById('product_price'))
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(price, 2);
	// Update alt. price
	if(alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price'))
		document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(price*alter_currency_rate, 2)+")";
	// Update Save %
	if(document.getElementById('save_percent') && list_price > 0 && dynamic_save_money_enabled == 'Y') {
		if(price < list_price)
			document.getElementById('save_percent').innerHTML = Math.round(100-(price/list_price)*100);
		else
			document.getElementById('save_percent').innerHTML = '0';
	}

	if((mq > 0 && avail > mq) || is_unlimit == 'Y')
		avail = mq;

	// Update product quantity
	if(document.getElementById('product_avail_txt'))
		document.getElementById('product_avail_txt').innerHTML = ((avail > 0)?(flag?avail:product_avail):txt_no);
	select_avail = min_avail;
	// Update product quantity selector
	if(document.getElementById('product_avail')) {
		if(document.getElementById('product_avail').options) {
			if(!isNaN(min_avail) && !isNaN(avail)) {
				var first_value = -1;
				if(document.getElementById('product_avail').options[0])
					first_value = document.getElementById('product_avail').options[0].value;
				if(first_value == min_avail) {
					if((avail-min_avail+1) != document.getElementById('product_avail').options.length) {
						if(document.getElementById('product_avail').options.length > avail) {
							cnt = document.getElementById('product_avail').options.length;
							for(x = avail; x < cnt; x++)
								document.getElementById('product_avail').options[document.getElementById('product_avail').options.length-1] = null;
						} else {
							cnt = document.getElementById('product_avail').options.length;
							for(x = cnt+1; x <= avail; x++)
								document.getElementById('product_avail').options[cnt++] = new Option(x, x);
						}
					}
				} else {
					while(document.getElementById('product_avail').options.length > 0)
						document.getElementById('product_avail').options[0] = null;
					cnt = 0;
					for(x = min_avail; x <= avail; x++)
						document.getElementById('product_avail').options[cnt++] = new Option(x, x);
				}
				if(document.getElementById('product_avail').options.length == 0)
					document.getElementById('product_avail').options[0] = new Option(txt_out_of_stock, 0);
			}
		}
		select_avail = document.getElementById('product_avail').options[document.getElementById('product_avail').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+"<BR><BR>");

	return true;
}

function check_exceptions() {
var x, cnt;

	for(x in exceptions) {
        flag = true;
		if(exceptions[x].length != names.length)
			continue;
        for(c in exceptions[x])
            if(document.getElementById('po'+c).value != exceptions[x][c]) {
                flag = false;
                break;
            }
		if(flag)
			return false;
	}
	return true;
}

function price_format(price, precision) {
var x, cnt, top, botom;
	precision = Math.pow(10, precision);
	price = Math.round(price*precision)/precision;
	top = Math.floor(price);
	bottom = Math.round((price-top)*precision)+precision;
	top = top+"";
	bottom = bottom+"";
	cnt = 0;
	for(x = top.length; x >= 0; x--) {
		if(cnt%3 == 0 && cnt > 0 && x > 0)
			top = top.substr(0,x)+","+top.substr(x,top.length);
		cnt++;
	}
	price = top+"."+bottom.substr(1,bottom.length);
	return price;
}

function product_option(classid) {
	if(!isNaN(classid)) {
		return document.getElementById(classid);
	} else if(names) {
		var x;
		for(x in names) {
			if(names[x] == classid) {
				if(document.getElementById('po'+x)) {
					return document.getElementById('po'+x);
				}
				break;
			}
		}
	}
	return false;
}

function check_wholesale(qty) {
var x, wl_price, t, found;
var wl_taxes = new Array();

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

	wl_taxes = current_taxes;

	wl_price = price;
	found = false;
	for(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];
			found = true;
		}
		if(document.getElementById('wp'+x))
			document.getElementById('wp'+x).innerHTML = currency_symbol+price_format(price-default_price+product_wholesale[x][2], 2);
	}

	if(found) {
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(price-default_price+wl_price, 2);
		if(alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price'))
			document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format((price-default_price+wl_price)*alter_currency_rate, 2)+")";
		for(x in taxes)
			if(document.getElementById('tax_'+x) && current_taxes[x])
				document.getElementById('tax_'+x).innerHTML = currency_symbol+price_format(current_taxes[x]-taxes[x]+wl_taxes[x], 2);
	} else {
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(price, 2);
		if(alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price'))
			document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(price*alter_currency_rate, 2)+")";
		for(x in taxes)
			if(document.getElementById('tax_'+x) && current_taxes[x])
				document.getElementById('tax_'+x).innerHTML = currency_symbol+price_format(current_taxes[x], 2);
	}

	return true;
}

function product_options(name) {
var x;
	if(names)
		for(x in names)
			if(names[x] == name) {
				if(document.getElementById('po'+x)) {
					var obj = document.getElementById('po'+x);
					return (obj.type == 'select-one')?obj.options[obj.selectedIndex].text:obj.value;
				}
				break;
			}
	return false;
}
