// The idea is that the user will be able to click on the item/category association and toggle it's status.
// The item_noble_cat_status_process_cue_array stores all the currently toggled items.
// When an item is clicked, the item_noble_cat_status_process_cue_array must be examined
//      to make sure that double entries are not being added.
// Items must also be able to be removed from item_noble_cat_status_process_cue_array.
var item_noble_cat_status_process_cue_array = new Array();

var ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_ADD = 1;
var ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_REMOVE = 2;
var ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_NONE = -1;
var ITEM_NOBLE_CAT_STATUS_INITIAL_EXISTS = 1;
var ITEM_NOBLE_CAT_STATUS_INITIAL_NOT_EXISTS = 0;

function cItemCatStatus(item_id, item_type_code, category_id, change_type_code, item_number, category_name)
{
	this._item_id = item_id;
	this._item_type_code = item_type_code;
	this._category_id = category_id;
	this._change_type_code = change_type_code;
	this._item_number = item_number;
	this._category_name = category_name;
}
cItemCatStatus.prototype._item_id;
cItemCatStatus.prototype._item_type_code;
cItemCatStatus.prototype._category_id;
cItemCatStatus.prototype._change_type_code;
cItemCatStatus.prototype._item_number;
cItemCatStatus.prototype._category_name;

function ItemNoble_ItemCatToggle(item_id, item_type_code, category_id, item_number, category_name, initial_cat_status)
{
	// get the selected object if available (or ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_NONE)
	var selected_item_cat_obj = ItemNoble_GetSelectedItem(item_id, item_type_code, category_id);

	// determine if an object was (not) returned
	if(selected_item_cat_obj == ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_NONE)
	{
		var use_cat_change_status;

		// determine if the initial status type was "exists" ie. in the category
		if(initial_cat_status == ITEM_NOBLE_CAT_STATUS_INITIAL_EXISTS)
		{
			use_cat_change_status = ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_REMOVE;
		}
		// determine if the initial status type was "not exists" ie. NOT in the category
		else if(initial_cat_status == ITEM_NOBLE_CAT_STATUS_INITIAL_NOT_EXISTS)
		{
			use_cat_change_status = ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_ADD;
		}

		item_cat_status_obj = new cItemCatStatus(item_id, item_type_code, category_id, use_cat_change_status, item_number, category_name);
		ItemNoble_SetItemCatChangeDirectDisplayArea(item_cat_status_obj._item_id, item_cat_status_obj._item_type_code, item_cat_status_obj._category_id, use_cat_change_status);
		ItemNoble_AppendCatStatusProcessCue(item_cat_status_obj);
	}
	else
	{
		ItemNoble_SetItemCatChangeDirectDisplayArea(selected_item_cat_obj._item_id, selected_item_cat_obj._item_type_code, selected_item_cat_obj._category_id, ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_NONE);
		ItemNoble_RemoveFromCatStatusProcessCue(selected_item_cat_obj);
		/*
		if(selected_item_cat_obj._change_type_code == ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_REMOVE)
		{
			ItemNoble_SetItemCatChangeDirectDisplayArea(selected_item_cat_obj._item_id, selected_item_cat_obj._item_type_code, selected_item_cat_obj._category_id, ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_ADD)
			selected_item_cat_obj._change_type_code = ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_ADD;
  		}
		else
		{
			ItemNoble_SetItemCatChangeDirectDisplayArea(selected_item_cat_obj._item_id, selected_item_cat_obj._item_type_code, selected_item_cat_obj._category_id, ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_REMOVE)
			selected_item_cat_obj._change_type_code = ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_REMOVE;
		}
  		*/

	}

	ItemNoble_RefreshDynamicCatStatusChangeCueDisplay();
}

function ItemNoble_AppendCatStatusProcessCue(item_cat_status_obj)
{
	item_noble_cat_status_process_cue_array.push(item_cat_status_obj);
}
function ItemNoble_RemoveFromCatStatusProcessCue(item_cat_status_obj)
{
	var remove_index = ItemNoble_GetItemCatProcessCueIndexFromItemCatStatusObj(item_cat_status_obj);
	item_noble_cat_status_process_cue_array.splice(key, 1);
}

function ItemNoble_GetItemCatProcessCueIndexFromItemCatStatusObj(item_cat_status_obj)
{
	for(key in item_noble_cat_status_process_cue_array)
	{
		if(item_noble_cat_status_process_cue_array[key] === item_cat_status_obj)
			return key;
	}
}

function ItemNoble_RefreshDynamicCatStatusChangeCueDisplay()
{
	var cue_display_node = document.getElementById('adminreward_dynamic_cat_status_cue_display');
	var output = '';
	for(key in item_noble_cat_status_process_cue_array)
	{
//		output += 'item_id: '+item_noble_cat_status_process_cue_array[key]._item_id;
//		output += ',item_type_code:'+item_noble_cat_status_process_cue_array[key]._item_type_code;
//		output += ',cat id:'+item_noble_cat_status_process_cue_array[key]._category_id;
//		output += ',change_code:'+item_noble_cat_status_process_cue_array[key]._change_type_code;
//		output += ',item_number:'+item_noble_cat_status_process_cue_array[key]._item_number;
//		output += ',category name:'+item_noble_cat_status_process_cue_array[key]._category_name;
//		output += '<br>';

		var display_num = Number(key) + 1;
		var display_symbol = '';

		if(item_noble_cat_status_process_cue_array[key]._change_type_code == ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_ADD)
			display_symbol = '+>';
		else
			display_symbol = '->';

		output += display_num+') '+item_noble_cat_status_process_cue_array[key]._item_number;
		output += ' '+display_symbol+' ';
		output += item_noble_cat_status_process_cue_array[key]._category_name;
		output += '<br>';
	}
	cue_display_node.innerHTML = output;


	if(item_noble_cat_status_process_cue_array.length > 0)
		document.getElementById('adminreward_dynamic_cat_status_submit').style.display = 'block';
	else
		document.getElementById('adminreward_dynamic_cat_status_submit').style.display = 'none';
}

function ItemNoble_SetItemCatChangeDirectDisplayArea(item_id, item_type_code, category_id, change_code)
{
	var direct_change_display_node = document.getElementById('adminreward_item_cat_change_display_'+item_id+'_'+category_id);
	if(direct_change_display_node)
	{
		if(change_code == ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_ADD)
			direct_change_display_node.innerHTML = '+>';
		else if(change_code == ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_REMOVE)
			direct_change_display_node.innerHTML = '->';
		else
			direct_change_display_node.innerHTML = '';
	}
}

// returns -1 if item is not found
function ItemNoble_GetSelectedItem(item_id, item_type_code, category_id)
{
	for(key in item_noble_cat_status_process_cue_array)
	{
		var cur_item_cat_status_obj = item_noble_cat_status_process_cue_array[key];
		if(cur_item_cat_status_obj._item_id == item_id && cur_item_cat_status_obj._item_type_code == item_type_code && cur_item_cat_status_obj._category_id == category_id)
		{
			return cur_item_cat_status_obj;
		}
	}
	return ITEM_NOBLE_CAT_STATUS_CHANGE_TYPE_NONE;
}


function ItemNoble_UpdateItemCat(item_type_code, g_current_general_url)
{
	item_type_code = 2;

	var post_parameter_array = Array();
	post_parameter_array[0] = Array("update_item_cat_disable","1");

	// *** add to post
	var count = 0;
	for(key in item_noble_cat_status_process_cue_array)
	{
		var cur_item_cat_status_obj = item_noble_cat_status_process_cue_array[key];
		post_parameter_array.push(new Array('item_cat['+count+'][item_id]', cur_item_cat_status_obj._item_id));
		post_parameter_array.push(new Array('item_cat['+count+'][item_type_code]', cur_item_cat_status_obj._item_type_code));
		post_parameter_array.push(new Array('item_cat['+count+'][category_id]', cur_item_cat_status_obj._category_id));
		count++;
	}

	// clear the process cue
	item_noble_cat_status_process_cue_array = new Array();

	DoHTTPRequest(g_current_general_url+'param=item_admin&operation='+item_type_code+'&ajax_request=1&', ItemNoble_ItemCatHandler, post_parameter_array);
}

function ItemNoble_RefreshItemCatArea(item_type_code, g_current_general_url)
{
	var post_parameter_array = Array();
	post_parameter_array[0] = Array("refresh_item_cat_disable","1");

	DoHTTPRequest(g_current_general_url+'param=item_admin&operation='+item_type_code+'&ajax_request=1&', ItemNoble_ItemCatHandler, post_parameter_array);
}

function ItemNoble_ItemCatHandler()
{
	var sbox = document.getElementById('adminreward_ajax_status');
	sbox.style.display = 'block';
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		sbox.innerHTML = 'Displaying... Please be patient, this may take a while.';
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;

			if(http_output.substr(0,10) == '[itemized]')
				alert('itemized');

			document.getElementById('adminreward_itemdisablearea').innerHTML = http_output;
			sbox.style.display = 'none';
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
			sbox.style.display = 'none';
		}
	}

	sbox.innerHTML = 'Loading...  Please be patient, this may take a while.';
}


function RunFullReload(item_type_code, g_current_general_url)
{
	var post_parameter_array = Array();
	post_parameter_array[0] = Array("run_full_reload","1");

	document.getElementById('adminreward_categoryflood_message_area').innerHTML = 'Reloading all items and category associations.  Please be patient, this may take a while...';
	document.getElementById('adminreward_categoryflood_message_area').style.display = 'block';

	DoHTTPRequest(g_current_general_url+'param=item_admin&operation='+item_type_code+'&ajax_request=1&', ItemNoble_ItemReloadHandler, post_parameter_array);
}

function ItemNoble_ItemReloadHandler()
{
	var sbox = document.getElementById('adminreward_ajax_status');
	sbox.style.display = 'block';
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		sbox.innerHTML = 'Displaying...';
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;

			if(http_output)
				document.getElementById('adminreward_categoryfloodarea').innerHTML += http_output;

			document.getElementById('adminreward_categoryflood_message_area').innerHTML = 'All items have been reloaded.';
			sbox.style.display = 'none';
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
			sbox.style.display = 'none';
		}
	}

	sbox.innerHTML = 'Reloading all items and category associations.  Please be patient, this may take a while...';
	document.getElementById('adminreward_categoryflood_message_area').style.display = 'block';
}











// ====================================================================================================================================================


var item_noble_preview_fieldset_title_area_global;
var item_noble_preview_image_area_global;
var item_noble_preview_title_area_global;
var item_noble_preview_value_area_global;
var item_noble_preview_description_area_global;
var instructions_area_global;

window.onload = function()
{

	// Load elements into memory to avoid doing a lookup each time
	item_noble_preview_fieldset_title_area_global = document.getElementById("item_noble_preview_fieldset_title_area");
	item_noble_preview_image_area_global = document.getElementById("item_noble_preview_image_area");
	item_noble_preview_title_area_global = document.getElementById("item_noble_preview_title_area");
	item_noble_preview_value_area_global = document.getElementById("item_noble_preview_value_area");
	item_noble_preview_description_area_global = document.getElementById("item_noble_preview_description_area");
	instructions_area_global = document.getElementById("instructions_area");

}

/*
function SetGlobals()
{
	// Load elements into memory to avoid doing a lookup each time
	item_noble_preview_fieldset_title_area_global = document.getElementById("item_noble_preview_fieldset_title_area");
	item_noble_preview_image_area_global = document.getElementById("item_noble_preview_image_area");
	item_noble_preview_title_area_global = document.getElementById("item_noble_preview_title_area");
	item_noble_preview_value_area_global = document.getElementById("item_noble_preview_value_area");
	item_noble_preview_description_area_global = document.getElementById("item_noble_preview_description_area");
	instructions_area_global = document.getElementById("instructions_area");
}
*/

function LoadInitialAdminPage(operation, g_current_general_url)
{
	var address = g_current_general_url+"param=item_admin&operation="+operation+"&ajax_initial_load=1&";
	AjaxRequest(address, ItemNoble_LoadInitialAdminPageHandler);
}
function ItemNoble_LoadInitialAdminPageHandler(xml_http_obj)
{
	if(xml_http_obj.status == 200)
	{
		//alert(xml_http_obj.responseText)
		document.getElementById('admin_body_area').innerHTML = xml_http_obj.responseText;
	}
	else
	{
		alert("There was a problem retrieving the HTTPRequestHandler data:" + xml_http_obj.statusText);
	}
	//DisplayNobleAdminReadyState((xml_http_obj.readyState==4?null:xml_http_obj.readyState));
}

function ItemNoble_HideRow(cur_checkbox, cat_id)
{
	var cur_row_table = document.getElementById("checkbox_row_cat_id_"+cat_id);
	var cur_row_header = document.getElementById("checkbox_row_header_cat_id_"+cat_id);

	if(cur_checkbox.checked)
	{
		cur_row_table.style.display = "";
		cur_row_header.style.display = "";
	}
	else
	{
		cur_row_table.style.display = "none";
		cur_row_header.style.display = "none";
	}

}

function ItemNoble_ShowItemPreview(item_number, title_string, item_value, item_description, image_thumb_path)
{

	item_noble_preview_fieldset_title_area_global.innerHTML = "&nbsp;-&nbsp; "+item_number;
	item_noble_preview_image_area_global.innerHTML = '<img src="'+image_thumb_path+'"><br />';
	item_noble_preview_title_area_global.innerHTML = title_string;
	item_noble_preview_value_area_global.innerHTML = item_value;
	item_noble_preview_description_area.innerHTML = item_description;
	instructions_area_global.style.display = "none";

/*	document.getElementById("item_noble_preview_fieldset_title_area").innerHTML = "&nbsp;-&nbsp; "+item_number;
	document.getElementById("item_noble_preview_image_area").innerHTML = '<img src="'+image_thumb_path+'"><br />';
	document.getElementById("item_noble_preview_title_area").innerHTML = title_string;
	document.getElementById("item_noble_preview_value_area").innerHTML = item_value;
	document.getElementById("item_noble_preview_description_area").innerHTML = item_description;
	document.getElementById("instructions_area").style.display = "none";*/
}

function ItemNoble_HideItemPreview()
{

	item_noble_preview_fieldset_title_area_global.innerHTML = "&nbsp;";
	item_noble_preview_image_area_global.innerHTML = "&nbsp;";
	item_noble_preview_title_area_global.innerHTML = "&nbsp;";
	item_noble_preview_value_area_global.innerHTML = "&nbsp;";
	item_noble_preview_description_area.innerHTML = "&nbsp;";
	instructions_area_global.style.display = "";

/*	document.getElementById("item_noble_preview_fieldset_title_area").innerHTML = "&nbsp;";
	document.getElementById("item_noble_preview_image_area").innerHTML = "&nbsp;";
	document.getElementById("item_noble_preview_title_area").innerHTML = "&nbsp;";
	document.getElementById("item_noble_preview_value_area").innerHTML = "&nbsp;";
	document.getElementById("item_noble_preview_description_area").innerHTML = "&nbsp;";
	document.getElementById("instructions_area").style.display = "";*/
}

function ItemNoble_AddOrRemoveCategoryItemAssociation(is_checked, operation, cat_id, item_id, order_by, g_current_general_url)
{
	//AjaxGet(address);
	if(is_checked)
	{
		var address = g_current_general_url+"param=item_admin&operation="+operation+"&is_ajax=1&sub_op=add_cat_item_assoc&cat_id="+cat_id+"&item_number="+item_id+"&order_by="+order_by+"&";
		AjaxRequest(address, ItemNoble_AddCategoryItemAssociationHandler);
	}
	else
	{
		var address = g_current_general_url+"param=item_admin&operation="+operation+"&is_ajax=1&sub_op=remove_cat_item_assoc&cat_id="+cat_id+"&item_number="+item_id+"&";
		AjaxRequest(address, ItemNoble_RemoveCategoryItemAssociationHandler);
	}
}

function ItemNoble_AddCategoryItemAssociationHandler(xml_http_obj)
{
	if(xml_http_obj.status == 200)
	{
		document.getElementById('item_noble_ajax_response_area').innerHTML += xml_http_obj.responseText + "<br />";
		//document.getElementById('scroll_box_changelog').
		var objDiv = document.getElementById("scroll_box_changelog");
		objDiv.scrollTop = objDiv.scrollHeight;

	}
	else
	{
		alert("There was a problem retrieving the HTTPRequestHandler data:" + xml_http_obj.statusText);
	}
	//DisplayReadyState((xml_http_obj.readyState==4?null:xml_http_obj.readyState));
}

function ItemNoble_RemoveCategoryItemAssociationHandler(xml_http_obj)
{
	if(xml_http_obj.status == 200)
	{
		document.getElementById('item_noble_ajax_response_area').innerHTML += xml_http_obj.responseText + "<br />";
		var objDiv = document.getElementById("scroll_box_changelog");
		objDiv.scrollTop = objDiv.scrollHeight;
	}
	else
	{
		alert("There was a problem retrieving the HTTPRequestHandler data:" + xml_http_obj.statusText);
	}
	//DisplayReadyState((xml_http_obj.readyState==4?null:xml_http_obj.readyState));
}

function ClearChangeLog()
{
	document.getElementById('item_noble_ajax_response_area').innerHTML = "";
}

function ItemNoble_HighlightCategory(cat_area_number, highlight)
{
	var cat_area_normal = document.getElementById("item_noble_cat_area_"+cat_area_number+"_normal");
	var cat_area_highlighted = document.getElementById("item_noble_cat_area_"+cat_area_number+"_highlighted");

	if(highlight)
	{
		cat_area_normal.style.display = "none";
		cat_area_highlighted.style.display = "";
	}
	else
	{
		cat_area_normal.style.display = "";
		cat_area_highlighted.style.display = "none";
	}
}

function ItemNoble_HighlightRow(item_id, highlight)
{

	var title_row = document.getElementById("item_noble_title_row_"+item_id);
	if(highlight)
	{
		title_row.bgColor = "#ffffff";
	}
	else
	{
		title_row.bgColor = "#eeeeee";
	}
}
