function numberOnlyInput(ctl)
{
	if (!ctl || !ctl.value)
		return;

// Assign to a temp variable in order to preserve selection
	var x = ctl.value.replace(/([^0-9])/g, "");
	if (x != ctl.value)
		ctl.value = x;
}

function checkFormFields(form)
{
// Get the controls to check
	var face_and_back_ctl = document.getElementById('face_and_back_ctl');
	var size_ctl = document.getElementById('size_ctl');
	var width_face_ctl = document.getElementById('width_face_ctl');
	var height_face_ctl = document.getElementById('height_face_ctl');
	var width_back_ctl = document.getElementById('width_back_ctl');
	var height_back_ctl = document.getElementById('height_back_ctl');
	var material_ctl = document.getElementById('material_ctl');
	var lamination_ctl = document.getElementById('lamination_ctl');
	var proof_ctl = document.getElementById('proof_ctl');
	var numbering_ctl = document.getElementById('numbering_ctl');
	var starting_number_ctl = document.getElementById('starting_number_ctl');
	var qty_ctl = document.getElementById('qty_ctl');

	var size_box = document.getElementById('size_box');
	if (size_box)
		size_box.style.border = 'none';
	var material_box = document.getElementById('material_box');
	if (material_box)
		material_box.style.border = 'none';
	var proof_box = document.getElementById('proof_box');
	if (proof_box)
		proof_box.style.border = 'none';
	var qty_box = document.getElementById('qty_box');
	if (qty_box)
		qty_box.style.border = 'none';
	var numbering_box = document.getElementById('numbering_box');
	if (numbering_box)
		numbering_box.style.border = 'none';

// Make sure the required fields are filled in
	var i = 0;
	var error = '';
	if (size_ctl && !size_ctl.value)
	{
		i++;
		error += i+') Please select a Size.\n';
		if (size_box)
			size_box.style.border = '1px solid red';
	}
	var customError = false;
	if (size_ctl && size_ctl.value == '-1')
	{
	// Make sure all custom sizes are filled in
		if ((width_face_ctl && !width_face_ctl.value) || 
			(height_face_ctl && !height_face_ctl.value))
		{
			i++;
			error += i+') Please enter a custom width and height for your face label.\n';
			if (size_box)
				size_box.style.border = '1px solid red';
			customError = true;
		}
		else if (width_face_ctl.value  > 11)
		{
			i++;
			error += i+') Please enter a custom width less than 11" for your face label.\n';
			if (size_box)
				size_box.style.border = '1px solid red';
			customError = true;
		}
		else if (height_face_ctl.value  > 11)
		{
			i++;
			error += i+') Please enter a custom height less than 11" for your face label.\n';
			if (size_box)
				size_box.style.border = '1px solid red';
			customError = true;
		}
		if (face_and_back_ctl && face_and_back_ctl.checked)
		{
			if ((width_back_ctl && !width_back_ctl.value) || 
				(height_back_ctl && !height_back_ctl.value))
			{
				i++;
				error += i+') Please enter a custom width and height for your back label.\n';
				if (size_box)
					size_box.style.border = '1px solid red';
				customError = true;
			}
			else if (width_back_ctl.value > 11)
			{
				i++;
				error += i+') Please enter a custom width less than 11" for your back label.\n';
				if (size_box)
					size_box.style.border = '1px solid red';
				customError = true;
			}
			else if (height_back_ctl.value > 11)
			{
				i++;
				error += i+') Please enter a custom height less than 11" for your back label.\n';
				if (size_box)
					size_box.style.border = '1px solid red';
				customError = true;
			}
		}
	}
	if (material_ctl && !material_ctl.value)
	{
		i++;
		error += i+') Please select a Material.\n';
		if (material_box)
			material_box.style.border = '1px solid red';
	}
	if (lamination_ctl && !lamination_ctl.value)
	{
		i++;
		error += i+') Please select a Laminate or Varnish.\n';
		if (material_box)
			material_box.style.border = '1px solid red';
	}
	if (numbering_ctl && numbering_ctl.checked && starting_number_ctl)
	{
		if (starting_number_ctl.value == '' || isNaN(starting_number_ctl.value))
		{
			i++;
			error += i+') Please enter a Starting Number.\n';
			if (numbering_box)
				numbering_box.style.border = '1px solid red';
		}
	}
	if (proof_ctl && proof_ctl.value == '')
	{
		i++;
		error += i+') Please select a Proof.\n';
		if (proof_box)
			proof_box.style.border = '1px solid red';
	}
	if (qty_ctl)
	{
		var qty_error = '';
		if (qty_ctl.value < 250)
			qty_error = 'Please enter a Quantity from 250 to 25,000.';
		else if (qty_ctl.value > 25000)
			qty_error = 'For quantities of 25,000 or more with this material and these dimensions. Please contact a Customer Care Rep (800) 537-8816.';
		if (qty_error != '')
		{
			i++;
			error += i+') '+qty_error+'\n';
			if (qty_box)
				qty_box.style.border = '1px solid red';
		}
	}

// Return the result
	if (i > 0)
	{
		alert(error+'\n\n The error/missing required '+i+' fields will be marked.');
		return false;
	}
	else
	{
	// Passed validation; fix custom controls
		if (size_ctl && size_ctl.value == '-1')
			size_ctl.value = '';
		else
		{
			width_face_ctl.value = '';
			height_face_ctl.value = '';
			width_back_ctl.value = '';
			height_back_ctl.value = '';
		}
		if (face_and_back_ctl && !face_and_back_ctl.checked)
		{
			width_back_ctl.value = '';
			height_back_ctl.value = '';
		}
	}
	return true;
}

function toggleStartingNumber()
{
	var numbering_ctl = document.getElementById('numbering_ctl');
	var starting_number_ctl = document.getElementById('starting_number_ctl');
	var starting_number_label = document.getElementById('starting_number_label');
	if (starting_number_ctl)
	{
		starting_number_ctl.disabled = (numbering_ctl.value == '1910' || numbering_ctl.value == '');
		set_style_attribute(starting_number_ctl,'display',(starting_number_ctl.disabled) ? 'none' : 'block');
	}
	if (starting_number_label)
		set_style_attribute(starting_number_label,'display',(starting_number_ctl.disabled) ? 'none' : 'block');
}

function updateQuantity()
{
	var qty = document.getElementById('qty');
	var dynamic_qty = document.getElementById('dynamic_qty');
	dynamic_qty.value = '';
	if (!qty.value)
		return;
	for (var i=0;i<qty.options.length;i++)
	{
		if (parseInt(qty.value) < parseInt(qty.options[i].value))
			dynamic_qty.value += '/'+qty.options[i].value;
	}
}

function formatEnlargeSize(value)
{
	return value.replace('/Preview/','/Preview/Large/');
}

function formatTemplateSize(value)
{
	var pos = value.lastIndexOf('/');
	return '/downloads/wine-labels/'+value.replace('-small','-large').substring(pos+1,value.length-4)+'.pdf';
}

function formatEnlargeMaterial(value)
{
	return value.replace('SmallSwatches','PopupLargeSwatches').replace('-swatch-','-material-');
}

function updateImage(id,value,enlarge_format,template_format)
{
// Get the image
	var block = document.getElementById(id.replace('_ctl','_block'));
	var img = document.getElementById(id.replace('_ctl','_image'));
	if (img)
	{
	// Set the image filename
		var new_image = img_xref[value];
		if (!new_image)
			new_image = img_xref['spacer'];
		block.className = (new_image.src == img_xref['spacer'].src) ? 'imageBlockHidden' : 'imageBlockShow';
		img.src = new_image.src;
	}

// Update the enlarge link
	var enlarge_link = document.getElementById(id.replace('_ctl','_enlarge'));
	if (enlarge_link)
	{
	// Set the href
		enlarge_link.href = (enlarge_format) ? enlarge_format(new_image.src) : new_image.src;
		set_style_attribute(enlarge_link,'display',(new_image.src == img_xref['spacer'].src) ? 'none' : 'block');
	}

// Update the template link
	var template_link = document.getElementById(id.replace('_ctl','_template'));
	if (template_link)
	{
	// Set the href
		template_link.href = (template_format) ? template_format(new_image.src) : new_image.src;
		set_style_attribute(template_link,'display',(new_image.src == img_xref['spacer'].src) ? 'none' : 'block');
	}

// If there's a label, update it
	var desc = document.getElementById(id.replace('_ctl','_desc'));
	if (!desc)
		return;
	if (desc_xref[value])
		desc.innerHTML = desc.innerText = desc_xref[value];
	else
		desc.innerText = desc.innerHTML = '';
}

function updateNumbering(numbering_ctl)
{
// Get the numbering fields
	var prefix_ctl = document.getElementById('prefix_ctl');
	var starting_number_ctl = document.getElementById('starting_number_ctl');
	var suffix_ctl = document.getElementById('suffix_ctl');
	var backup_prefix_ctl = document.getElementById('backup_prefix_ctl');
	var backup_starting_number_ctl = document.getElementById('backup_starting_number_ctl');
	var backup_suffix_ctl = document.getElementById('backup_suffix_ctl');

// Back up
	var disable_numbering = (numbering_ctl.value != '1911');
	if (disable_numbering)
	{
		backup_prefix_ctl.value = prefix_ctl.value;
		backup_starting_number_ctl.value = starting_number_ctl.value;
		backup_suffix_ctl.value = suffix_ctl.value;
		prefix_ctl.value = '';
		starting_number_ctl.value = '';
		suffix_ctl.value = '';
	}
	else
	{
		prefix_ctl.value = backup_prefix_ctl.value;
		starting_number_ctl.value = backup_starting_number_ctl.value;
		suffix_ctl.value = backup_suffix_ctl.value;
	}

// Enable/disable
	prefix_ctl.disabled = disable_numbering;
	starting_number_ctl.disabled = disable_numbering;
	suffix_ctl.disabled = disable_numbering;
}

function updateQtyWarning(lots)
{
/*
	var qtyWarning = document.getElementById('qtyWarning');
	if (!qtyWarning)
		return;
	qtyWarning.className = (lots > 1) ? 'quoterRedWarning' : 'quoterRedWarning hide';
*/
// Set the lot warning
	var qty_ctl = document.getElementById('qty_ctl');
	var lot_warning = document.getElementById('lot_warning');
	if (lot_warning)
	{
		lot_warning.className = (qty_ctl && qty_ctl.value > 250 && lots > 1) ? 'quoterRedWarning' : 'quoterRedWarning hide';
		if (lot_warning.className == 'quoterRedWarning')
		{
			var qty_total = document.getElementById('qty_total');
			if (qty_total)
				qty_total.innerText = format_qty(qty_ctl.value);
			var lot_total = document.getElementById('lot_total');
			if (lot_total)
				lot_total.innerText = format_qty(lots);
		}
	}

// Set the front/back warning
	var face_and_back_ctl = document.getElementById('face_and_back_ctl');
	var front_back_qty_warning = document.getElementById('front_back_qty_warning');
	if (front_back_qty_warning)
	{
		front_back_qty_warning.className = (face_and_back_ctl && qty_ctl && face_and_back_ctl.checked && qty_ctl.value > 250) ? 'quoterRedWarning' : 'quoterRedWarning hide';
		if (front_back_qty_warning.className == 'quoterRedWarning')
		{
			var qty_front_total = document.getElementById('qty_front_total');
			if (qty_front_total)
				qty_front_total.innerText = format_qty(qty_ctl.value);
			var qty_back_total = document.getElementById('qty_back_total');
			if (qty_back_total)
				qty_back_total.innerText = format_qty(qty_ctl.value);
			var qty_front_back_total = document.getElementById('qty_front_back_total');
			if (qty_front_back_total)
				qty_front_back_total.innerText = format_qty(qty_ctl.value*2);
		}
	}
}

function updateStyle(value)
{
	updateSizeMenu();
	var custom_back_block = document.getElementById('custom_back_block');
	if (custom_back_block)
		custom_back_block.className = (value == '1818') ? 'customSizeBoxShow' : 'customSizeBoxHide';

// Update the warning
	updateQtyWarning(document.getElementById('lot_ctl').value);

// Update the face and back warnings
	updateFaceAndBackWarning();
}

function updateMaterial(material_ctl)
{
	updateImage(material_ctl.id,material_ctl.value,formatEnlargeMaterial,null);
	var lamination_ctl = document.getElementById('lamination_ctl');
	if (lamination_ctl)
	{
		apa_save_required['8'] = '';
		lamination_ctl.selectedIndex = -1;
		lamination_ctl.value = '';
	}
	updateAvailableProductAttributesWithCallback(material_ctl.form,'updateSizeMenu');
}

function updateFoil(foil_ctl)
{
	updateImage(foil_ctl.id,foil_ctl.value,null);
	var foil_ctl = document.getElementById('foil_ctl');
	var foil_production_warning = document.getElementById('foil_production_warning');
	if (foil_ctl && foil_production_warning)
		foil_production_warning.className = (foil_ctl.value == '') ? 'quoterRedWarning hide' : 'quoterRedWarning';
	updateAvailableProductAttributesWithCallback(foil_ctl.form,'updateSizeMenu');
}

function updateLaminate(lamination_ctl)
{
	updateAvailableProductAttributesWithCallback(lamination_ctl.form,'updateSizeMenu');
}

function updateCustomBlock(showCustomBlock)
{
	var custom_size_block = document.getElementById('custom_size_block');
	custom_size_block.className = (showCustomBlock) ? 'customSizeBoxShow' : 'customSizeBoxHide';
}

function updateFaceAndBackWarning()
{
	var face_and_back_ctl = document.getElementById('face_and_back_ctl');
	if (!face_and_back_ctl)
		return;
	var alternating_warning1 = document.getElementById('alternating_warning1');
	if (alternating_warning1)
		alternating_warning1.className = (face_and_back_ctl.checked) ? 'quoterRedWarning' : 'quoterRedWarning hide';
	var alternating_warning2 = document.getElementById('alternating_warning2');
	if (alternating_warning2)
		alternating_warning2.className = (face_and_back_ctl.checked) ? 'quoterRedWarning' : 'quoterRedWarning hide';
}