function chooseLayout(id)
{
	var layout_ctl = document.getElementById('layout_ctl');
	var last_layout_ctl = document.getElementById('last_layout_ctl');

// Turn off the old layout
	if (last_layout_ctl.value)
	{
		var layout_box = document.getElementById('layout'+last_layout_ctl.value+'_box');
		set_style_attribute(layout_box,'border','3px solid white');
	}

// Turn on the selected layout
	if (id)
	{
		var layout_box = document.getElementById('layout'+id+'_box');
		set_style_attribute(layout_box,'border','3px solid black');
	}

// Set the new layout
	if (layout_ctl.value != id)
	{
		layout_ctl.value = id;
		if (layout_ctl.type != 'hidden')
		{
			for (var i=0;i<layout_ctl.options.length;i++)
			{
				if (layout_ctl.options[i].value == id)
				{
					layout_ctl.selectedIndex = i;
					break;
				}
			}
		}
	}
	if (last_layout_ctl.value != id)
	{
//		updateSoftwareMenu(layout_ctl.value);
		updateLayout();
		last_layout_ctl.value = id;
	}
}

function open_larger()
{
	var layout_larger = document.getElementById('layout_larger');
	lightbox_global.start(layout_larger);

 }
/*
function updateLayoutDisplay(start)
{
	var layout = new Array();
	var z = 0;
	for (var x in layout_xref)
	{
		if (!isNaN(x))
			layout[z++] = x;
	}

	for (var i=start;i<7;i++)
	{
		var link1 = document.getElementById('pn'+i+'link1');
		var link2 = document.getElementById('pn'+i+'link2');
		var text2 = document.getElementById('pn'+i+'text2');
		var img = document.getElementById('pn'+i+'img');
		var j = i-1;
		var product_number = layout_xref['pn'+start][layout[j]];
		link1.href = link2.href = '/images/282/'+product_number+'.jpg';
		text2.innerText=  product_number;
		img.src = '/images/282/thumbnails/'+product_number+'.jpg';
		img.alt = product_number;
	}
}

function updateLayoutGroup(c)
{
// Update styles
	updateLayoutDisplay(c.substring(0,c.indexOf('to')));

// Turn all of the others off
	var hasControl = false;
	var i=1;
	do {
		var j = i+5;
		var off = document.getElementById(i+'to'+j);
		hasControl = (off != null);
		if (hasControl && get_style_attribute(off,'display') != 'none')
		{
			var on = document.getElementById(i+'to'+j+'link');
			set_style_attribute(off,'display','none');
			set_style_attribute(on,'display','inline');
		}
		i = j+1;
	} while (hasControl);

// Get the tags to work on
	var on = document.getElementById(c);
	var off = document.getElementById(c+'link');

// Turn the current one on
	set_style_attribute(off,'display','none');
	set_style_attribute(on,'display','inline');
}
*/
function updateLayout()
{
	var layout_ctl = document.getElementById('layout_ctl');
	var layout_img = document.getElementById('layout_img');
	var layout_title = document.getElementById('layout_title');
	var layout_larger = document.getElementById('layout_larger');
	var layout_larger2 = document.getElementById('layout_larger2');
	var layout_pdf = document.getElementById('layout_pdf');
//	var layout_desc = document.getElementById('layout_desc');
	layout_img.src =  (layout_ctl.value == '' ) ? '/images/spacer.gif' : img_xref[layout_ctl.value].src;
	var display = (layout_ctl.value == '') ? 'none' : 'inline';
	set_style_attribute(layout_larger,'display',display);
	set_style_attribute(layout_pdf,'display',display);
	set_style_attribute(layout_title,'display',display);
	layout_larger.href = layout_img.src.replace('/thumbnails','').replace('.jpg','a.png');
	layout_larger2.href = layout_img.src.replace('/thumbnails','').replace('.jpg','b.png');
	layout_pdf.href = layout_img.src.replace('/images/282/thumbnails','/downloads/fp').replace('jpg','pdf');
//	layout_desc.innerText = (layout_ctl.value == '') ? '' : layout_xref[layout_ctl.value];
}

function reconcileColor(color_ctl_x,color_ctl_y,color_ctl_z,xref)
{
// Make sure x does not contain y's value
//	if (!color_ctl_y.value && !color_ctl_z.value)
	if (!color_ctl_y.value)
		return;
	var bak = color_ctl_x.value;
	var y = color_ctl_y.options[color_ctl_y.selectedIndex].text;
//	var z = color_ctl_z.options[color_ctl_z.selectedIndex].text;
	var o = 1;
	var selected = 0;
	var re = /c(\d+)$/;
	for (var k in xref)
	{
		var m = re.exec(k);
		if (m == null) continue;
		var id = m[1];
//		if (y == xref[k] || z == xref[k]) continue;
		if (y == xref[k]) continue;
		color_ctl_x[o++] = new Option(xref[k],id);
		if (id == bak)
			selected = o;
	}
	color_ctl_x.length = o;
	color_ctl_x.selectedIndex = selected;
	color_ctl_x.value = bak;
}

function reconcileColors()
{
	var color_ctl_x = document.getElementById('back_color_ctl');
	var color_ctl_y = document.getElementById('color1_ctl');
	var color_ctl_z = document.getElementById('color2_ctl');
/*
	reconcileColor(color_ctl_x,color_ctl_y,color_ctl_z,back_color_xref);
	reconcileColor(color_ctl_y,color_ctl_x,color_ctl_z,color_xref);
	reconcileColor(color_ctl_z,color_ctl_x,color_ctl_y,color_xref);
*/
	reconcileColor(color_ctl_y,color_ctl_z,null,color_xref);
	reconcileColor(color_ctl_z,color_ctl_y,null,color_xref);
}

function updateBackColor()
{
// Change the image
	var back_color_ctl = document.getElementById('back_color_ctl');
	var back_pms_ctl = document.getElementById('back_pms_ctl');
	var back_color_swatch = document.getElementById('back_color_swatch');
//	var back_color_img = document.getElementById('back_color_img');
	var back_color_label = document.getElementById('back_color_label');
	if (back_color_ctl.value)
	{
//		back_color_img.src = (img_xref[back_color_ctl.value]) ? img_xref[back_color_ctl.value].src : '/images/spacer.gif';
		var c = (ink_xref[back_color_ctl.value] != undefined && ink_xref[back_color_ctl.value] != '') ? ink_xref[back_color_ctl.value] : '#FFFFFF';
		set_style_attribute(back_color_swatch,'background-color',c);
		back_color_label.innerText = back_color_ctl.options[back_color_ctl.selectedIndex].text;
		back_color_label.innerHTML = back_color_ctl.options[back_color_ctl.selectedIndex].text;
	}
	else
	{
//		back_color_img.src = '/images/spacer.gif';
		set_style_attribute(back_color_swatch,'background-color','#FFFFFF');
		back_color_label.innerText = (back_pms_ctl.value) ? 'PMS '+back_pms_ctl.value : '';
		back_color_label.innerHTML = (back_pms_ctl.value) ? 'PMS '+back_pms_ctl.value : '';
	}

// Disable the other control
	back_color_ctl.disabled = (back_pms_ctl.value != '');
	back_pms_ctl.disabled = (back_color_ctl.value != '');
	back_color_ctl.className = (back_color_ctl.disabled) ? 'disabled' : 'mx';
	back_pms_ctl.className = (back_pms_ctl.disabled) ? 'disabled' : 'mx';

// Make sure the choices are mutally exclusive
//	reconcileColors();
}

function updateColor(n)
{
// Change the image
	var color_ctl = document.getElementById('color'+n+'_ctl');
	var pms_ctl = document.getElementById('pms'+n+'_ctl');
//	var color_img = document.getElementById('color'+n+'_img');
	var color_swatch = document.getElementById('color'+n+'_swatch');
	var color_label = document.getElementById('color'+n+'_label');
	if (color_ctl.value)
	{
//		color_img.src = (img_xref[color_ctl.value]) ? img_xref[color_ctl.value].src : '/images/spacer.gif';
		var c = (ink_xref[color_ctl.value] != undefined && ink_xref[color_ctl.value] != '') ? ink_xref[color_ctl.value] : '#FFFFFF';
		set_style_attribute(color_swatch,'background-color',c);
		color_label.innerText = color_ctl.options[color_ctl.selectedIndex].text;
		color_label.innerHTML = color_ctl.options[color_ctl.selectedIndex].text;
	}
	else
	{
		set_style_attribute(color_swatch,'background-color','#FFFFFF');
		color_label.innerText = (pms_ctl.value) ? 'PMS '+pms_ctl.value : '';
		color_label.innerHTML = (pms_ctl.value) ? 'PMS '+pms_ctl.value : '';
	}

// Disable the other control
	color_ctl.disabled = (pms_ctl.value != '');
	pms_ctl.disabled = (color_ctl.value != '');
	color_ctl.className = (color_ctl.disabled) ? 'disabled' : 'mx';
	pms_ctl.className = (pms_ctl.disabled) ? 'disabled' : 'mx';

// Make sure the choices are mutally exclusive
	reconcileColors();
}

function checkFormFields()
{
// Get the fields
	var layout_ctl = document.getElementById('layout_ctl');
	var color1_ctl = document.getElementById('color1_ctl');
	var pms1_ctl = document.getElementById('pms1_ctl');
	var color2_ctl = document.getElementById('color2_ctl');
	var pms2_ctl = document.getElementById('pms2_ctl');
	var back_color_ctl = document.getElementById('back_color_ctl');
	var back_pms_ctl = document.getElementById('back_pms_ctl');
	var proof_ctl = document.getElementById('proof_ctl');
	var qty_ctl = document.getElementById('qty');
	var i = 0;
	var error_string = '';
	var layout_box = document.getElementById('layout_box');
	var color_box = document.getElementById('color_box');
	var proof_box = document.getElementById('proof_box');
	var qty_box = document.getElementById('qty_box');

// Make sure all required fields are filled in
	if (layout_ctl.value == '')
	{
		i++;
		error_string += i + ") Please select a layout.\n";
		layout_box.style.border = '1px solid red';
	}
	else
		layout_box.style.border = 'none';
	if (color1_ctl.value == '' && pms1_ctl.value == '' && 
		color2_ctl.value == '' && pms2_ctl.value == '' && 
		back_color_ctl.value == '' && back_pms_ctl.value == '')
	{
		i++;
		error_string += i + ") Please select at least one ink.\n";
		color_box.style.border = '1px solid red';
	}
	else
		color_box.style.border = 'none';
	if (proof_ctl.value == '')
	{
		i++;
		error_string += i + ") Please select a proof option.\n";
		proof_box.style.border = '1px solid red';
	}
	else
		proof_box.style.border = 'none';
	if (qty_ctl.value == '' || isNaN(qty_ctl.value) || qty_ctl.value < 1000 || qty_ctl.value > 100000)
	{
		i++;
		if (qty_ctl.value == '' || isNaN(qty_ctl.value))
			error_string += i + ") Please enter a quantity.\n";
		else if (qty_ctl.value < 1000)
			error_string += i + ") Please enter a quantity no less than 1,000.\n";
		else if (qty_ctl.value > 100000)
			error_string += i + ") Please enter a quantity no more than 100,000.\n";
		qty_box.style.border = '1px solid red';
	}
	else
		qty_box.style.border = 'none';
	if (error_string != "")
	{
		var fieldCount = (i == 1) ? 'field' : i+' fields';
		alert(error_string + "\nThe error/missing required " + fieldCount + " will be marked"); 
		return false;
	}

	return true;
}

function updateLayoutSelect(layout_ctl,software_ctl)
{
// Populate the layout menu based on the selected software
	var bak = layout_ctl.value;
	var x = 1;
	var selected = 0;
	var converter = document.createElement('textarea');
	var layout = (software_ctl.value) ? software_xref[software_ctl.value]['layout'] : null;

// Iterate over the layouts
	for (var v in layout_xref)
	{
		var t = desc_xref[v];
		if (!t || isNaN(v)) continue;

	// If software is selected, check the list
		if (layout)
		{
			var found = false;
			for (var i=0;i<layout.length;i++)
			{
				if (v == layout[i])
				{
					found = true;
					break;
				}
			}
			if (!found) continue;
		}

	// If this is the current selection, mark it
		if (bak == v)
			selected = x;

	// Convert the text (html entities)
		converter.innerHTML = t.replace(/</g,'&lt;').replace(/>/g,'&gt;');
		var text = converter.innerText;

	// Add the option
		layout_ctl.options[x++] = new Option(text,v);
	}

// Set the menu length and selection
	layout_ctl.length = x;
	layout_ctl.selectedIndex = selected;
}

function updateLayoutChoices(layout_ctl,software_ctl)
{
// Turn on/off display based on software choice
	var layout = (software_ctl.value) ? software_xref[software_ctl.value]['layout'] : null;
	for (var v in layout_xref)
	{
		var t = desc_xref[v];
		if (!t || isNaN(v)) continue;

	// If software is selected, check the list
		var allowed = true;
		if (layout)
		{
			var found = false;
			for (var i=0;i<layout.length;i++)
			{
				if (v == layout[i])
				{
					found = true;
					break;
				}
			}
			if (!found)
				allowed = false;
		}

	// Set the display
		var box = document.getElementById('layout'+v+'_box');
		var display = (allowed) ? 'table-cell' : 'none';
		set_style_attribute(box,'display',display);

	// If this is not allowed and it's currently selected, clear the selection
		if (layout_ctl.value == v && !allowed)
			chooseLayout('');
	}
}

function updateLayoutMenu()
{
	var software_ctl = document.getElementById('software_ctl');
	var layout_ctl = document.getElementById('layout_ctl');
	if (layout_ctl.type == 'hidden')
		updateLayoutChoices(layout_ctl,software_ctl);
	else
		updateLayoutMenu(layout_ctl,software_ctl);
}

function updateSoftwareMenu(layout_value)
{
	var software_ctl = document.getElementById('software_ctl');
	var compatible_software = document.getElementById('compatible_software');

// Change the label
	compatible_software.innerText = (layout_value) ? desc_xref[layout_value]+' Compatible Software' : 'Compatible Software';
	compatible_software.innerHTML = (layout_value) ? desc_xref[layout_value]+' Compatible Software' : 'Compatible Software';

// Populate the software menu based on the selected layout
	var bak = software_ctl.value;
	var x = 1;
	var selected = 0;
	var converter = document.createElement('textarea');
	for (var v in software_xref)
	{
	// Get the text and the layouts for this software
		var t = software_xref[v]['text'];
		var layout = software_xref[v]['layout'];
		if (!t || !layout) continue;

	// If a layout is selected, check the list
		if (layout_value)
		{
			var found = false;
			for (var i=0;i<layout.length;i++)
			{
				if (layout[i] == layout_value)
				{
					found = true;
					break;
				}
			}
			if (!found) continue;
		}

	// If this is the current selection, mark it
		if (bak == v)
			selected = x;

	// Convert the text (html entities)
		converter.innerHTML = t.replace(/</g,'&lt;').replace(/>/g,'&gt;');
		var text = converter.innerText;

	// Add the option
		software_ctl.options[x++] = new Option(text,v);
	}

// Set the menu length and selection
	software_ctl.length = x;
	software_ctl.selectedIndex = selected;

// If layout_value is blank, hide the show all link
	var show_all_software = document.getElementById('show_all_software');
	set_style_attribute(show_all_software,'display',(layout_value) ? 'inline' : 'none');
}
