
my_list_callback_get =
{
	success: my_list_get_success,
	failure: my_list_get_failure
};

my_list_callback_add=
{
	success: my_list_add_success,
	failure: my_list_add_failure
};

my_list_callback_del =
{
	success: my_list_del_success,
	failure: my_list_del_failure
};

my_list_callback_del_2 =
{
	success: my_list_del_2_success,
	failure: my_list_del_2_failure
};

my_list_callback_del_all =
{
	success: my_list_del_all_success,
	failure: my_list_del_all_failure
};

my_list_callback_check =
{
	success: my_list_check_success,
	failure: my_list_check_failure
};

function my_list_get_success(o)
{
	try
	{
		var list = eval("("+o.responseText+")");
	}
	catch(e)
	{
		alert("Failed to parse response!.");
		return;
	}
	my_list_display(list);
}

function my_list_get_failure(o)
{
}

function my_list_display(list)
{
	var my_list_total = 0;
	
	for(i=1;i<=5;i++)
	{
		if (document.getElementById("list_display_"+i).hasChildNodes())
		{
			tmpEl1 = document.getElementById("list_display_"+i).firstChild;
			while(tmpEl1)
			{
				tmpEl2 = tmpEl1.nextSibling;
				tmpEl1.parentNode.removeChild(tmpEl1);
				tmpEl1 = tmpEl2;
			}
		}
	}
	
	for(i=0;i<5;i++)
	{
		document.getElementById("list_count_"+(i+1)).innerHTML = list[i].length;
		my_list_total += list[i].length;
		
		for(j=0;j<list[i].length;j++)
		{
			tmpList = document.getElementById("list_display_"+(i+1));
			
			tmpListItem = document.createElement("LI");
		
			tmpDiv = document.createElement("DIV");
			tmpDiv.setAttribute("class", "listing-box");
			
			tmpSpan = document.createElement("SPAN");
			tmpSpan.setAttribute("title", "Delete this listing");
			tmpSpan.setAttribute("class", "delete-listing");
			tmpSpan.onclick = new Function("my_list_del_2("+i+", "+list[i][j].id+", "+list[i][j].type+");");
			tmpDiv.appendChild(tmpSpan);
			
			tmpSpan = document.createElement("SPAN");
			tmpSpan.setAttribute("title", list[i][j].name);
			tmpSpan.setAttribute("style", "display:block");
			tmpSpan.setAttribute("class", "hyperlink");
			
			tmpTxt = document.createTextNode(list[i][j].name);
			tmpSpan.appendChild(tmpTxt);
			tmpDiv.appendChild(tmpSpan);
			
			tmpInput = document.createElement("INPUT");
			tmpInput.setAttribute("class", "manage-listing");
			tmpInput.setAttribute("name", "");
			tmpInput.setAttribute("type", "checkbox");
			tmpInput.setAttribute("value", "");
			tmpInput.onclick = new Function("my_list_check(this, "+i+", "+j+");");
			tmpDiv.appendChild(tmpInput);
			if (list[i][j].checked == true)
				tmpInput.checked = true;			
			
			tmpListItem.appendChild(tmpDiv);
			tmpList.appendChild(tmpListItem);
		}
	}
	document.getElementById("my_list_total_header").innerHTML = my_list_total;
	document.getElementById("my_list_total_footer").innerHTML = my_list_total;
}

function my_list_add_success(o)
{
	my_list_get();
	
	category = o.argument[0];
	id = o.argument[1];
	type = o.argument[2];	

	if (document.getElementById("del_"+category+"_"+id+"_"+type))
	{
		document.getElementById("add_"+category+"_"+id+"_"+type).style.display = 'none';		
		document.getElementById("del_"+category+"_"+id+"_"+type).style.display = 'block';
	}
	
//	if (typeof(del_func) != "undefined")
//	{
//		e = document.getElementById("adddelbutton");
//		e.setAttribute("class", "removefromlist");
//		e.onclick = new Function(del_func);
//	}
}

function my_list_add_failure(o)
{
}
var tmp_del_cat = 0;
var tmp_del_loc = 0;
var tmp_del_type = 0;

function my_list_del_success(o)
{
	my_list_get();
	
	if (typeof(add_func) != "undefined" && tmp_del_cat == del_func_cat && tmp_del_loc == del_func_loc)
	{
		e = document.getElementById("adddelbutton");
		e.setAttribute("class", "addtomylist");
		e.onclick = new Function(add_func);
	}
}

function my_list_del_failure(o)
{
}

function my_list_del_2_success(o)
{
	my_list_get();
	
	category = o.argument[0];
	id = o.argument[1];
	type = o.argument[2];
	
	if (document.getElementById("add_"+category+"_"+id+"_"+type))
	{
		document.getElementById("del_"+category+"_"+id+"_"+type).style.display = 'none';		
		document.getElementById("add_"+category+"_"+id+"_"+type).style.display = 'block';
	}
	
//	if (typeof(add_func) != "undefined" && tmp_del_cat == del_func_cat && tmp_del_loc == del_func_loc)
//	{
//		e = document.getElementById("adddelbutton");
//		e.setAttribute("class", "addtomylist");
//		e.onclick = new Function(add_func);
//	}
}

function my_list_del_2_failure(o)
{
}

function my_list_del_all_success(o)
{
	my_list_get();
	
	add_buttons = document.getElementsByName("addbuttons");
	for(i=0;i<add_buttons.length;i++)
		add_buttons[i].style.display = "block";
	del_buttons = document.getElementsByName("delbuttons");
	for(i=0;i<del_buttons.length;i++)
		del_buttons[i].style.display = "none";	
}

function my_list_del_all_failure(o)
{
}

function my_list_check_success(o)
{
}

function my_list_check_failure(o)
{
}

function my_list_initialize()
{
	my_list_get();
}

function my_list_get()
{
	YAHOO.util.Connect.asyncRequest("POST", "my_list_get.php", my_list_callback_get);
}

function my_list_add(category, id, type, name)
{
	my_list_callback_add.argument = [category, id, type];
	YAHOO.util.Connect.asyncRequest("POST", "my_list_add.php", my_list_callback_add, "type="+type+"&id="+id+"&name="+name+"&category="+category);
}

function my_list_del(loc, id, type)
{
	tmp_del_cat = loc;
	tmp_del_loc = id;
	tmp_del_type = type;
	YAHOO.util.Connect.asyncRequest("POST", "my_list_del.php", my_list_callback_del, "loc="+loc+"&id="+id+"&type="+type);
}

function my_list_del_2(category, id, type)
{
	my_list_callback_del_2.argument = [category, id, type];
	YAHOO.util.Connect.asyncRequest("POST", "my_list_del_2.php", my_list_callback_del_2, "category="+category+"&id="+id+"&type="+type);	
}

function my_list_del_all()
{
	YAHOO.util.Connect.asyncRequest("POST", "my_list_del_all.php", my_list_callback_del_all);	
}

function my_list_check(e, type, loc)
{
	check = e.checked;
	YAHOO.util.Connect.asyncRequest("POST", "my_list_checked.php", my_list_callback_check, "type="+type+"&id="+loc+"&checked="+check);
}

function compare(type)
{
	count = Number(document.getElementById("list_count_"+type).innerHTML);
	if (count == 0)
	{
		alert("You have nothing to compare in that category.  Please add some items to your list and try again.");
		return false;
	}
	
	// Check to make sure there are at least 2 items checked
	
	// Go to the sbs page.
	window.location = "sbs.php?type="+type;
}

function my_list_view_all()
{
	total = Number(document.getElementById("my_list_total_header").innerHTML);
	
	if (total == 0)
	{
		alert("Your MyList is empty.  Please add items before using this function.");
		return false;
	}
	window.location = "search_results.php?view=all";
}

YAHOO.util.Event.addListener(window, "load", my_list_initialize);