var connections = 0;

function jsDebug(msg){
	if(0){
		document.getElementById("jsDebug").style.display = "block";
		document.getElementById("jsDebug").innerHTML = document.getElementById("jsDebug").innerHTML + "<li>" + msg;
	}
}

// Shows loading meter in target space
function getAHAH(l_args, divID){
   return getAHAHm(l_args, divID, 1); 
}
// Does not show a loading meter in the target space
function getAHAH2(l_args, divID){
	return getAHAHm(l_args, divID, 0); 
}
// Does not clear the target area content
function getAHAH3(l_args, divID){
	return getAHAHm(l_args, divID, 2); 
}
// Shows cancel request option instead of standard loading bar
function getAHAH4(l_args, divID){
	return getAHAHm(l_args, divID, 4); 
}

function getAHAH5(l_args, divID){
	return getAHAHm(l_args, divID, 99); 
}
function getAHAHForm(obj, divID) {
   //var l_args = Form.serialize(obj);
   var l_args = $(obj).serialize();
	return getAHAHm(l_args, divID, 109); 
}

function getAHAHm(l_args, divID, showLoading)
{
jsDebug("Doing AHAH - loading results to " + divID);
jsDebug(l_args);

	if(showLoading != 2 && showLoading != 99)
		$('#'+divID).html("");

	if(showLoading == 1)
	{
		$('#'+divID).html("<center><img src='/community/images/wait2.gif'></center>");	
	}

	if(showLoading == 4)
		$('#'+divID).html("<img onclick='hideModal();' align='absmiddle' src='/community/images/closebutton.gif'> Cancel Request<hr><center><P><img src='images/wait2.gif'><BR>Loading Content, Please Wait...</center>");	
	
	var url = "/community/includes/ahah.php";
	connections++;
	if(connections > 0 && showLoading != 99)
	{
		$('#statDivID').show();
	}

	// We are submitting a form via AHAH
	if(showLoading == 109)
	{
		$.ajax({
			type:		"POST",
			url:		url,
			data:		l_args,
			dataType:	"html",
			success: function(msg) 
			{
				$('#'+divID).html(msg);
				connections--;
				if(connections <= 0)
				{
					$('#statDivID').hide();
				}
			},			
			
			error: function(msg) 
			{
				$('#'+divID).html("Error loading content.  If errors persist, please contact <a href='mailto:support@pathtags.com'>support@pathtags.com</a>");
				connections--;
				if(connections <= 0)
				{
					$('#statDivID').hide();
				}
			}
		});
	}
	// It's not a form, just some action
	else
	{
		$.ajax({
			type:		"POST",
			url:		url,
			data:		'args=' + l_args,
			dataType:	"html",
			success: function(msg) 
			{
				if(divID != "AJAXDIVRESP")
					$("#"+divID).html(msg);
				else if(msg != "")
				{
					$.jGrowl(msg, {life: 10000});
				}
				
				connections--;
				if(connections <= 0)
				{
					$('#statDivID').hide();
				}
			},			
			
			error: function(msg) 
			{
				$('#'+divID).html("Error loading content.  If errors persist, please contact <a href='mailto:support@pathtags.com'>support@pathtags.com</a>");
				connections--;
				if(connections <= 0)
				{
					$('#statDivID').hide();
				}
			}
		});
		
	
	}

	return false;
}


function getAJAX(obj)
{
	return getAJAX_m(obj, 0, 0);
}
function getAJAXcond(obj)
{
	return getAJAX_m(obj, 1, 0);
}
function getAJAXsticky(obj)
{
	return getAJAX_m(obj, 0, 1);
}
function getAJAX_m(obj, conditional, stickyvar) 
{
	var l_args = $(obj).serialize();
	var url = "/community/includes/ajax.php";
	connections++;
	if(connections > 0 && conditional == 0)
	{
		$('#statDivID').show();
	}

	$.ajax({
		type:		"POST",
		url:		url,
		data:		l_args,
		dataType:	"html",
		success: function(msg) 
		{
			if(msg != "false" || conditional == 0)
			{
				if(stickyvar == 1)
					$.jGrowl(msg,{life: 10000, sticky: true});
				else
				{
					if(msg.length > 0)
					{
						$.jGrowl(msg,{life: 10000, theme: 'test'});
					}
				}
			}
			connections--;
			if(connections <= 0)
			{
				$('#statDivID').hide();
			}
			hideModal();
			return false;
		},			
		
		error: function(msg) 
		{
			mkdialog("Error loading content", "If errors persist, please contact <a href='mailto:support@pathtags.com'>support@pathtags.com</a>");
			connections--;
			if(connections <= 0)
			{
				$('#statDivID').hide();
			}
			return false;
		}
	});
	return false;
}


function loadModal(ahahLoad)
{
	$('#modalDiv').html('<div id="modalcontent"></div>');
	$('#modalDiv').dialog({
					width: 700,
					minheight: 555,
					resizeable: true,
					modal: true,
					position: 'top',
					close:	function(event,ui) {
						$('#modalcontent').remove();
					}
				});
	getAHAH(ahahLoad, "modalDiv");
}	
function hideModal()
{
	$("#modalDiv").dialog("close");
}

function hide(theDiv){
	theDiv.style.visibility = "hidden";	
}
function submitKeyword(keyword){ 
	document.keywordsuggest.keyword.value = keyword;
}

// For auto complete
function lookup(inputString, action) {
	if (action == null) {
		action = "nothing";
	}
	var url = "/community/includes/ahah.php";
	if (inputString.length < 4) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.post(url, {
			queryString : "" + inputString + "",
			action : "" + action + ""
		}, function(data) {
			if (data.length > 0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} // lookup

function fill(thisValue) {
	$('#inputString').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}

function mkdialog(vtitle,vtext)
{
	var $dialog = $('<div id="modalcontent"></div>')
	.html(vtext)
	.dialog({
		title: vtitle,
		width: 700,
		height: 550,
		resizeable: true,
		modal: true,
		close:	function(event,ui) {
			$('#modalcontent').remove();
		}
	});
}

function checkForSubjective(comments)
{
	var subjective = false;
	
	if(comments.indexOf("lighter") != -1)
		subjective = true;
	if(comments.indexOf("darker") != -1)
		subjective = true;
	if(comments.indexOf("bigger") != -1)
		subjective = true;
	if(comments.indexOf("smaller") != -1)
		subjective = true;
	if(comments.indexOf("brighter") != -1)
		subjective = true;

	if(subjective)
	{
		return confirm("Your comments appears to contain subjective phrases like \"lighter\" or \"darker\".  Our orders team will not accept subjective terminology for order revision requests.  If your comments use subjective terms, please cancel and revise your comments before rejecting.  If they do not, please forgive this notice and simply select \"ok\" to submit your rejection.");
	}
	else
	{
		return true;
	}
}

function adjustQty(divname, qty)
{
	var qtystring = $('#'+divname).html();
	var qtyvalue = parseFloat(qtystring);
	$('#'+divname).html(qtyvalue + qty);
}


function showGalleryBox(name)
{
	var startState = $('#' + name).css("display");
	$('.gallerybox').hide();
	if(startState == "none")
		$('#' + name).show();
}

function toggleBookmark(id)
{
	if($('#bmlink'+id).hasClass('galleryLinkOn') || $('#bmlink'+id).text() == "Add Bookmark")
	{
		// Turn if off
		if($('#bmlink'+id).text() != "Add Bookmark")
		{
			$('#bmlink'+id).removeClass('galleryLinkOn');
			$('#bmlink'+id).addClass('galleryLinkOff');
			$('#icon_'+id).attr("src","/community/images/icons/bookmarks_delete2.png");
		}
		else
		{
			$('#bmlink'+id).text("Un-Bookmark");
		}
	}
	else
	{
		// Turn if on
		if($('#bmlink'+id).text() != "Un-Bookmark")
		{
			$('#bmlink'+id).removeClass('galleryLinkOff');
			$('#bmlink'+id).addClass('galleryLinkOn');
			$('#icon_'+id).attr("src","/community/images/icons/bookmarks_add2.png");
		}
		else
		{
			$('#bmlink'+id).text("Add Bookmark");
		}
	}

	$('#bookmarkform'+id).submit();
	return false;
}

