/*
	Rebuild page if some options is changed
*/
function check_options(productid) {
	var local_taxes = [];
	var is_rebuild_wholesale = false;
	var variantid = false;

	for (var t in taxes[productid])
		local_taxes[t] = taxes[productid][t][0];
	price = default_price[productid];

	/* Find variant */
	for (var x in variants[productid]) {
		if (variants[productid][x][1].length == 0)
			continue;

		variantid = x;
		for (var c in variants[productid][x][1]) {
			if (getPOValue(c) != variants[productid][x][1][c]) {
				variantid = false;
				break;
			}
		}

		if (variantid)
			break;
	}

	/* If variant found ... */
	if (variantid) {
		price = variants[productid][variantid][0][0];
		orig_price = variants[productid][variantid][0][4];
		avail = variants[productid][variantid][0][1];

		/* Get variant taxes */
		for (var t in local_taxes) {
			if (variants[productid][variantid][2][t])
				local_taxes[t] = parseFloat(variants[productid][variantid][2][t]);
		}

		if (!product_thumbnail)
			product_thumbnail[productid] = document.getElementById('product_thumbnail' + productid);

		/* Change product thumbnail */
		if (product_thumbnail[productid]) {
			if (variants[productid][variantid][0][2].src && variants[productid][variantid][0][2].width > 0 && variants[variantid][0][2].height > 0) {
				if (product_thumbnail[productid].src != variants[productid][variantid][0][2].src) {
					product_thumbnail[productid].src = variants[productid][variantid][0][2].src;
					product_thumbnail[productid].width = variants[productid][variantid][0][2].width;
					product_thumbnail[productid].height = variants[productid][variantid][0][2].height;
				}
			} else if (document.getElementById('product_thumbnail'+productid).src != product_image[productid].src) {
				product_thumbnail[productid].src = product_image[productid].src;
				if (product_image[productid].width > 0 && product_image[productid].height > 0) {
					product_thumbnail[productid].width = product_image[productid].width;
					product_thumbnail[productid].height = product_image[productid].height;
				}
			}
		}
	}

	/* Find modifiers */
	var _tmp = modi_price(productid, price, local_taxes, orig_price[productid]);
	price = _tmp[0];
	local_taxes = _tmp[1];

	/* Update taxes */
	for (var t in local_taxes) {
		if (document.getElementById('tax_'+t+'_'+productid)) {
			document.getElementById('tax_'+t+'_'+productid).innerHTML = currency_symbol+price_format(local_taxes[t]);
		}
		current_taxes[t] = local_taxes[t];
	}

	/* Update form elements */
	/* Update price */
	if (document.getElementById('product_price'+productid))
		document.getElementById('product_price'+productid).innerHTML = currency_symbol+price_format(price);

	/* Update alt. price */
	if (alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price'+productid))
		document.getElementById('product_alt_price'+productid).innerHTML = alter_currency_symbol+" "+price_format(price*alter_currency_rate);

	/* Update Save % */
	if (document.getElementById('save_percent'+productid) && list_price[productid] > 0 && dynamic_save_money_enabled) {
		var save_percent = Math.round(100-(price/list_price[productid])*100);
		if (save_percent > 0) {
			document.getElementById('save_percent'+productid).innerHTML = " "+save_percent;
		} else {
			document.getElementById('save_percent'+productid).innerHTML = " "+'0';
		}
	}

	/* Check exceptions */
	var ex_flag = check_exceptions(productid);
	if (!ex_flag && (alert_msg == 'Y'))
		alert(exception_msg);
			
	if (document.getElementById('exception_msg'+productid))
		document.getElementById('exception_msg'+productid).innerHTML = (ex_flag ? '' : exception_msg_html+"<br /><br />");

	return true;
}

/*
	Calculate product price with price modificators 
*/
function modi_price(productid, _price, _taxes, _orig_price) {
var return_price = round(_price, 2);

	/* List modificators */
	for (var x2 in modifiers[productid]) {
		var value = getPOValue(x2);
		if (!value || !modifiers[productid][x2][value])
			continue;

		/* Get selected option */
		var elm = modifiers[productid][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] += (elm[1] == '$' ? elm[2][t2] : (_orig_price*elm[2][t2]/100));
			}
		}
	}

	return [return_price, _taxes];
}

/*
	Check product options exceptions
*/
function check_exceptions(productid) {
	if (!exceptions[productid])
		return true;

	/* List exceptions */
	for (var x in exceptions[productid]) {
		if (isNaN(x))
			continue;

		var found = true;
        for (var c in exceptions[productid][x]) {
			var value = getPOValue(c);
			if (!value)
				return true;

            if (value != exceptions[productid][x][c]) {
				found = false;
				break;
			}
		}
		if (found)
			return false;
	}

	return true;
}

/*
	Get product option value
*/
function getPOValue(c) {
	if (!document.getElementById('po' + c))
		return false;

	if (document.getElementById('po' + c).tagName.toUpperCase() == 'SELECT')
		return document.getElementById('po'+c).options[document.getElementById('po'+c).selectedIndex].value;
	else if (document.getElementById('po' + c).tagName.toUpperCase() == 'INPUT') {
		if (document.getElementById('po'+c).type == 'radio') {
			var radios = document.getElementById ('po'+c);
			var the_form = radios.form;
			var the_value;
			for(var i=0; i<the_form.length; i++) {
				var temp = the_form.elements[i].type;
				var name = the_form.elements[i].id;
				if((the_form.elements[i].checked) && (name == ('po'+c)))
					the_value = the_form.elements[i].value;
			}
			return the_value;
		}
		else return false;
	} else 
		return false;		
}

/*
    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;
}


