function price_format(price,orig) {
	returnstr = '';
	if (orig > price) {
		returnstr = '<span class="orig">Was: '+dollarformat(orig)+'</span><br><span class="sale">Now: '+dollarformat(price)+'</span>';
	} else {
		returnstr = dollarformat(price);
	}
	return returnstr;
}

function inventory(id, size, color, price, price_orig, bo, bodate) {
	this.id = id;
	this.size = size;
	this.color = color;
	this.price = price;
	if (isNaN(price_orig)) {
		this.price_orig = 0;
	} else {
		this.price_orig = price_orig;
	}
	this.bo = bo;
	this.bodate = bodate;
}

function size(id, desc, price){
	this.id = id;
	this.desc = desc;
	this.price = price;
}

function color(id, desc, price){
	this.id = id;
	this.desc = desc;
	this.price = price;
}

function product() {
	this.style_code = '';
	this.name = '';
	this.inventory = new Array();
	this.allow_noselect = true;
	this.sizes = new Array();
	this.colors = new Array();
	this.input_size_select = false;
	this.input_size_value = false;
	this.input_color_select = false;
	this.input_color_value = false;
	this.price_output = false;
	this.price_output_default = false;
	this.input_qty = false;
	this.input_inventory_value = false;
	this.price_varies = false;
	this.price_low = false;
	this.input_none = true;
	this.error_prefix = '';
}
var product_validate = new Array();

function form_validate() {
	multierror = '';
	for(productNum = 0; productNum < product_validate.length; productNum++) {
		product_error = '';
		productObj = eval(product_validate[productNum]);
		if (productObj.selectSize && objgetbyid(productObj.input_size_value).value == 0) {
			product_error = productObj.error_prefix+'Select Amount';
		}
		if (productObj.selectColor && objgetbyid(productObj.input_color_value).value == 0) {
			if (product_error != '') product_error = product_error + '\n';
			product_error = product_error+productObj.error_prefix+'Select color';
		}
		if (productObj.input_qty) {
			quantity = objgetbyid(productObj.input_qty);
			if (isNaN(quantity.value) || quantity.value < 1) {
				if (product_error != '') product_error = product_error + '\n';
				product_error = product_error+productObj.error_prefix+'Enter quantity';
			} else if (quantity.value > 250) {
				if (product_error != '') product_error = product_error + '\n';
				product_error = product_error+productObj.error_prefix+'Please enter a valid quantity';
			}
		}
		if (product_error != '') {
			if (product_validate.length > 1) {
				if (multierror != '') multierror = multierror + '\n';
				multierror = multierror+product_error;
			} else {
				alert(product_error);
				return false;
			}
		}
	}
	if (multierror != '') {
		alert(multierror);
		return false;
	}
	return true;
}

function build_menus(productObj) {
	productObj.selectSize = false;
	productObj.selectColor = false;
	if (productObj.colors.length && productObj.input_color_select.length && productObj.input_color_value.length) {
		productObj.selectColor = true;
	}
	if (productObj.sizes.length && productObj.input_size_select.length && productObj.input_size_value.length) {
		productObj.selectSize = true;
		tmp = productObj.input_size_select;
		size_select = objgetbyid(productObj.input_size_select);
		size_value = objgetbyid(productObj.input_size_value);
		for (i=0; i < productObj.sizes.length; i++) {
			if (productObj.sizes[i].id == 0) {
				productObj.sizes[i].desc = 'Select Amount';
			}
			size_desc = productObj.sizes[i].desc;
			if (productObj.sizes[i].price) {
				size_desc = size_desc+' ($'+productObj.sizes[i].price.toFixed(2)+')';
			}
			size_select.options[i] = new Option(productObj.sizes[i].desc, i);
			if (productObj.sizes[i].id == size_value.value) {
				size_select.selectedIndex = i;
			}
		}
		if (productObj.selectColor) {
			change_size(productObj);
		}
	} else if (productObj.selectColor) {
		color_select = objgetbyid(productObj.input_color_select);
		color_value = objgetbyid(productObj.input_color_value);
		for (i=0; i < productObj.colors.length; i++) {
			if (productObj.colors[i].id == 0) {
				productObj.colors[i].desc = 'Select Color';
			}
			color_select.options[i] = new Option(productObj.colors[i].desc, i);
			if (productObj.colors[i].id == color_value.value) {
				color_select.selectedIndex = i;
			}
		}
	}
}

function change_size(productObj) {
	if (productObj.selectColor) {
		color_select = objgetbyid(productObj.input_color_select);
		color_value = objgetbyid(productObj.input_color_value);
	}
	if (productObj.price_output) {
		price_output_obj = objgetbyid(productObj.price_output);
		price_output_obj.innerHTML = productObj.price_output_default;
	}
	size_value = objgetbyid(productObj.input_size_value);
	size_select = objgetbyid(productObj.input_size_select);
	sizeIndex = size_select.options[size_select.selectedIndex].value;
	inventory_value = objgetbyid(productObj.input_inventory_value);
	if (sizeIndex == 0) {
		size_value.value = 0;
		inventory_value.value = 0;
		if (productObj.selectColor) {
			color_value.value = 0;
			color_select.options.length = 0;
			color_select.options[0] = new Option('Select Color',0);
			color_select.disabled = true;
		}
	} else {
		size_value.value = productObj.sizes[sizeIndex].id;
		if (productObj.selectColor) {
			color_select.options.length = 0;
			color_select.options[0] = new Option('Select Color',0);
			color_select.disabled = false;
			inventory_value.value = 0;
			for(invnum = 0; invnum < productObj.inventory.length; invnum++) {
				if (sizeIndex == productObj.inventory[invnum].size) {
					color_desc = productObj.colors[productObj.inventory[invnum].color].desc;
					if (productObj.price_varies) {
						color_desc = color_desc+' ($'+productObj.inventory[invnum].price.toFixed(2)+')';
					}
					if (productObj.inventory[invnum].bo == 1) {
						color_desc = color_desc+' (Ships '+productObj.inventory[invnum].bodate+')';
					}
					color_select.options[color_select.length] = new Option(color_desc, productObj.inventory[invnum].color);
					if (color_value.value == productObj.colors[productObj.inventory[invnum].color].id) {
						color_select.selectedIndex = color_select.length-1;
						inventory_value.value = productObj.inventory[invnum].id;
						if (productObj.price_output) {
							price_output_obj = objgetbyid(productObj.price_output);
							price_output_obj.innerHTML = price_format(productObj.inventory[invnum].price,productObj.inventory[invnum].price_orig);
						}
					}
				}
			}
			color_value.value = color_select.options[color_select.selectedIndex].value;
		} else {
			for(invnum = 0; invnum < productObj.inventory.length; invnum++) {
				if (sizeIndex == productObj.inventory[invnum].size) {
					inventory_value.value = productObj.inventory[invnum].id;
					if (productObj.price_output) {
						price_output_obj = objgetbyid(productObj.price_output);
						price_output_obj.innerHTML = price_format(productObj.inventory[invnum].price,productObj.inventory[invnum].price_orig);
					}
				}
			}
		}
	}
}

function change_color(productObj) {
	color_value = objgetbyid(productObj.input_color_value);
	color_select = objgetbyid(productObj.input_color_select);
	colorIndex = color_select.options[color_select.selectedIndex].value;
	inventory_value = objgetbyid(productObj.input_inventory_value);
	inventory_value.value = 0;
	if (productObj.price_output) {
		price_output_obj = objgetbyid(productObj.price_output);
		price_output_obj.innerHTML = productObj.price_output_default;
	}
	if (colorIndex == 0) {
		color_value.value = 0;
	} else {
		color_value.value = productObj.colors[colorIndex].id;
		if (productObj.selectSize) {
			size_select = objgetbyid(productObj.input_size_select);
			sizeIndex = size_select.options[size_select.selectedIndex].value;
		}
		for(invnum = 0; invnum < productObj.inventory.length; invnum++) {
			if (productObj.selectSize) {
				if (sizeIndex == productObj.inventory[invnum].size  && colorIndex == productObj.inventory[invnum].color) {
					if (productObj.price_output) {
						price_output_obj = objgetbyid(productObj.price_output);
						price_output_obj.innerHTML = price_format(productObj.inventory[invnum].price,productObj.inventory[invnum].price_orig);
					}
					inventory_value.value = productObj.inventory[invnum].id;
					return;
				}
			} else {
				if (colorIndex == productObj.inventory[invnum].color) {
					if (productObj.price_output) {
						price_output_obj = objgetbyid(productObj.price_output);
						price_output_obj.innerHTML = price_format(productObj.inventory[invnum].price, productObj.inventory[invnum].price_orig);
					}
					inventory_value.value = productObj.inventory[invnum].id;
					return;
				}
			}
		}
	}
}
