function Item_v2_ChangeMainImageSRC(new_src)
{
	var image_obj = document.getElementById('item_main_image');
	var na_image_obj = document.getElementById('image_na_class_large');
	
	if(na_image_obj)
	{
		na_image_obj.style.display = 'none';
	}
	if(image_obj)
	{
		image_obj.src = new_src;
		image_obj.style.display = 'inline';
	}
}


function Item_v2_ShowXLWindow(file, window_width, window_height)
{
	window.open(file,'XL','resizable=no,scrollbars=no,status=no,menubar=no,width=' + window_width + ',height='+window_height+', left=0, top=0');
}

function Item_v2_ShowImageNotFound()
{
	var image_obj = document.getElementById('item_main_image');
	var na_image_obj = document.getElementById('image_na_class_large');

	if(image_obj)
		image_obj.style.display = 'none';
		
	if(na_image_obj)
		na_image_obj.style.display = 'inline';

	
}

function Item_v2_ShowXLLink()
{
	var xl_link_obj = document.getElementById('item_larger_image_link');
	if(xl_link_obj)
	{
		//alert('show xl link');
			xl_link_obj.style.display = 'inline';
	}
}
function Item_v2_HideXLLink()
{
	var xl_link_obj = document.getElementById('item_larger_image_link');
	if(xl_link_obj)
	{
		//alert('hide xl link');
			xl_link_obj.style.display = 'none';
	}
}

function Item_v2_GetSizeNameById(size_id)
{
	if(!item_v2_size_name_keyed_array || item_v2_size_name_keyed_array.length < 1 || !item_v2_size_name_keyed_array[size_id])
		return '';
	return item_v2_size_name_keyed_array[size_id];
}

function GetColorNameArray()
{
	if(!item_v2_color_name_array || item_v2_color_name_array.length < 1)
		return new Array();
	return item_v2_color_name_array;
}

function GetColorLabel(is_plural)
{
	if(is_plural)
		return 'colors';
	else
		return 'color';
}

function GetSizeLabel(is_plural)
{
	if(is_plural)
		return 'sizes';
	else
		return 'size';
}

// =============================================================================
// item logo
// =============================================================================
var admin_item_logo_is_active = 0;
function AdminItemLogo_CollapseView()
{
	admin_item_logo_is_active = 0;
	document.getElementById('logo_admin_title').className = 'logo_admin_title_inactive';
	document.getElementById('logo_admin_body').className = 'logo_admin_body_inactive';
}
function AdminItemLogo_ExpandView(item_id, item_type_code)
{
	admin_item_logo_is_active = 1;
	AdminItemLogo_LoadBody(item_id, item_type_code);
	document.getElementById('logo_admin_title').className = 'logo_admin_title_active';
	document.getElementById('logo_admin_body').innerHTML = 'loading...';
	document.getElementById('logo_admin_body').className = 'logo_admin_body_active';
}
function AdminItemLogo_ToggleUse(item_id, item_type_code)
{
	if(admin_item_logo_is_active)
	{
		AdminItemLogo_CollapseView();
	}
	else
	{
		AdminItemLogo_ExpandView(item_id, item_type_code);
	}
}

function AdminItemLogo_LoadBody(item_id, item_type_code)
{
	var ajax_obj = new Class_Ajax();
	ajax_obj.data_obj.get_admin_logo_form_inner_body = 1;
	ajax_obj.SendRequest('index.php?param=item_admin&operation='+item_type_code+'&item_id='+item_id+'&');
}

function AdminItemLogo_SubmitNew(item_id, item_type_code)
{
	var logo_name = document.getElementById('admin_new_logo_name').value;
	
	if(logo_name.length > 0)
	{
		var ajax_obj = new Class_Ajax();
		ajax_obj.data_obj.submit_new_logo_name = 1;
		ajax_obj.data_obj.logo_name = logo_name;
		ajax_obj.SendRequest('index.php?param=item_admin&operation='+item_type_code+'&item_id='+item_id+'&');
		document.getElementById('admin_new_logo_name').value = '';
		document.getElementById('admin_new_logo_submit').style.visibility = 'hidden';
	}
	else
	{
		alert('You must enter a logo name');
	}
}
function AdminItemLogo_DeleteLogo(item_logo_id, item_id, item_type_code)
{
	if(item_logo_id)
	{
		if(confirm('Are you sure you want to delete '+item_logo_id+'?'))
		{
			var ajax_obj = new Class_Ajax();
			ajax_obj.data_obj.delete_logo_from_item = 1;
			ajax_obj.data_obj.item_logo_id = item_logo_id;
			ajax_obj.SendRequest('index.php?param=item_admin&operation='+item_type_code+'&item_id='+item_id+'&');
		}
	}
	else
	{
		alert('Unrecognized Logo ID');
	}
}

function Item_v2_GetLogoIdByName(logo_name)
{
	if(!item_v2_logo_name_keyed_array || item_v2_logo_name_keyed_array.length < 1)
		return new Array();
	return item_v2_logo_name_keyed_array;
}
// =============================================================================




// ========================================================================
// option denial
// ------------------------------------------------------------------------
function AdminItemOptionDenial_LoadBody(item_id, item_type_code)
{
	document.getElementById('option_denial_admin_body').innerHTML = 'Loading...';

	var ajax_obj = new Class_Ajax();
	ajax_obj.data_obj.get_item_admin_option_denial_body = 1;
	ajax_obj.SendRequest('index.php?param=item_admin&operation='+item_type_code+'&item_id='+item_id+'&');
}
function AdminItemOptionDenial_SaveChanges(item_id, item_type_code)
{
	var content = '';
	if(document.getElementById('option_denial_content'))
	{
		content = document.getElementById('option_denial_content').value;
	}

	document.getElementById('option_denial_admin_body').innerHTML = 'Saving...';

	var ajax_obj = new Class_Ajax();
	ajax_obj.data_obj.get_item_admin_option_denial_save = 1;
	//ajax_obj.data_obj.content = content.replaceAll('\n', '<~!~break~!~>').toString();
	ajax_obj.data_obj.content = JSFeature.Base64.encode(content);
	ajax_obj.SendRequest('index.php?param=item_admin&operation='+item_type_code+'&item_id='+item_id+'&');
}

function Item_v2_GetOptionDenialArray()
{
	return item_v2_option_denial_array;
}

function Item_v2_IsOptionDenialDisplayOn()
{
	if(Item_v2_GetOptionDenialArray() && Item_v2_GetOptionDenialArray().length > 0)
	{
		return true;
	}
	return false;
}



function Item_v2_IsSizeIdAndColorAvailable(size_id, color, logo_id, is_size_exact_only, is_color_exact_only, is_logo_exact_only)
{
	var i=0;
	var cur_color, cur_logo_name, cur_size_id;
	var is_color_match, is_size_match, is_logo_match;
	
	color = color.replace(/^\s+|\s+$/g, '');

	option_denial_array = Item_v2_GetOptionDenialArray();
	
	if(option_denial_array && option_denial_array.length > 0)
	{
		for(i=0; i<option_denial_array.length; i++)
		{
			cur_size_id = option_denial_array[i].ext.size_id;
			cur_color = option_denial_array[i].match.color;
			cur_logo_name = option_denial_array[i].match.logo;
			
			is_color_match = false;
			is_size_match = false;
			is_logo_match = false;
			
			// *** match size
			if(	!is_size_exact_only 
				&&
				!cur_size_id
				||
				size_id == cur_size_id
				)
			{
				is_size_match = true;
			}
			
			// *** match color
			if(	!is_color_exact_only 
				&&
				!cur_color
				||
				color.toLowerCase() == cur_color.toLowerCase()
				)
			{
				is_color_match = true;
			}
			
			// *** match logo
			if(	!is_logo_exact_only 
				&&
				!cur_logo_name
				||
				logo_id == Item_v2_GetLogoIdByName(cur_logo_name)
				)
			{
				is_logo_match = true;
			}
			
			if(is_size_match && is_color_match && is_logo_match)
			{
				return false;
			}
		}
	}
	return true;
}
function Item_v2_DisplayOptionDenialFromSelection()
{
	if(!Item_v2_IsOptionDenialDisplayOn())
	{
		return;
	}
	
	var i=0;
	var html_content = '';

	var content_node = document.getElementById('item_v2_option_denial_chart_content');
	var size_input_node = document.getElementById('size_id');
	var color_input_node = document.getElementById('color');
	var color_name_array = GetColorNameArray();

	var available_color_name_array = new Array();

	var selected_size_id = size_input_node?size_input_node.value:0;
	var selected_color_name = color_input_node?color_input_node.value:'';
	var warning_node = document.getElementById('item_v2_option_denial_warning_content');

	// *** show warning if needed
	if(warning_node && selected_size_id && selected_color_name && !Item_v2_IsSizeIdAndColorAvailable(selected_size_id, selected_color_name, '', 0,0,1))
	{
		warning_node.style.display = '';
		warning_node.className = 'error';
		warning_node.innerHTML = 'Warning: '+GetSizeLabel()+' '+Item_v2_GetSizeNameById(selected_size_id)+' is currently not available in '+GetColorLabel()+' '+selected_color_name+'.  Please select a different '+GetSizeLabel()+' or '+GetColorLabel()+'.';
	}
	else if(warning_node)
	{
		warning_node.innerHTML = '';
		warning_node.style.display = 'none';
	}

	// *** available color list
	if(content_node && selected_size_id && color_name_array)
	{
		dojo.forEach(color_name_array, function(color_name){
			console.log(color_name);
			if(Item_v2_IsSizeIdAndColorAvailable(selected_size_id, color_name, '', 0,0,1))
			{
				available_color_name_array.push(color_name);
			}
		});

		html_content += '<div style="padding-top:5px; padding-bottom:5px;"><b>Note: </b>';

		if(available_color_name_array.length > 0)
		{

			if(available_color_name_array.length == color_name_array.length)
			{
				html_content += 'Size <b>'+Item_v2_GetSizeNameById(selected_size_id)+'</b> is available in all colors.';
			}
			else if(available_color_name_array.length > 0)
			{
				html_content += 'Size <b>'+Item_v2_GetSizeNameById(selected_size_id)+'</b> is available in ';

				for(i=0; i<available_color_name_array.length; i++)
				{
					html_content += available_color_name_array[i];

					if(i < available_color_name_array.length-2)
						html_content += ', ';
					else if(i == available_color_name_array.length-2)
					{
						if(i > 2)
						{
							html_content += ', ';
						}
						html_content += ' and ';
					}
				}
			}

		}
		else
		{
			html_content += 'Size <b>'+Item_v2_GetSizeNameById(selected_size_id)+'</b> is not currently available.';
		}

		html_content += '</div>';
	}
	content_node.innerHTML = html_content;

	//ItemLogo_RefreshSelectionExceptionDisplay();
}
// ========================================================================
