//search_utils.js

function search_check_all_categories(check_elt)
{
	if(!check_elt.checked)
		return;

	checkbox_container = document.getElementById('TAG_user_category_multi_search');
	
	var i;
	for(i=0; i < checkbox_container.childNodes.length; i++)
	{
		thisNode = checkbox_container.childNodes[i];
				
		if(thisNode.nodeName == "INPUT" && thisNode.id != "sc_none" && thisNode.id != "sc_other")
		{
			thisNode.checked = true;
		}
	}
}

function search_uncheck_all_categories(check_elt)
{
	checkbox_container = document.getElementById('TAG_user_category_multi_search');

	var i;
	for(i=0; i < checkbox_container.childNodes.length; i++)
	{
		thisNode = checkbox_container.childNodes[i];
				
		if(thisNode.nodeName == "INPUT")
		{
			thisNode.checked = false;
		}
	}
}

function search_check_individual_category(check_elt)
{
	if(!check_elt.checked)
	{
		all_elt = document.getElementById('sc_all');
		all_elt.checked = false;
	}
}

/**
 * encode_countryprovince()
 *
 * Reads the visible input of the two select elements for a countryprovince or countryprovince_multi
 * search field and encodes them in the hidden transmission variable corresponding to that field.
 * Normally called onchange for either of the elements. If the province select does not exist,
 * ignores that value in the encoding.
 *
 * @param	int		fid		The field ID to encode.
 * @return	void
 */
function encode_countryprovince(fid)
{
	var country_elt = document.getElementById('f' + fid + 'c');
	var province_elt = document.getElementById('f' + fid + 'p'); 
	var transmit_elt = document.getElementById('si_' + fid);
	
	var country_code = country_elt.options[country_elt.selectedIndex].value;
	var province_code = 0;
	if(province_elt)
		province_code = province_elt.options[province_elt.selectedIndex].value;
	
	if(country_code == 0 || country_code == '0')
		country_code = '';
	
	if(province_code == 0 || province_code == '0')
		province_code = '';
	
	transmit_elt.value = country_code + ":" + province_code;
}

function search_prep_for_submit()
{
	return true;
}

function init_cal(fid, which, event)
{
	var elt_prefix;
	if(which == "start") 
		elt_prefix = "sd_";
	else if (which == "end") 
		elt_prefix = "ed_";
	
	JACS.show(document.getElementById(elt_prefix + fid), event, which + 'cal' + fid); 
	JACS.next(which + 'cal' + fid, update_dates, fid, which);
}

//Called by JACS on calendar close. Prepares data for transmission to the processing scripts and sticks it
//in the hidden transmission element corresponding to this date or datetime field
function update_dates(fid, which)
{	
	transmit_data = [];
	transmit_data[0] =  document.getElementById('sd_' + fid).value;
	transmit_data[1] =  document.getElementById('ed_' + fid).value;
	
	transmit_data = php_serialize(transmit_data);
	
	document.getElementById('si_' + fid).value = transmit_data;
}

function activate_category_controls(category_selection_type)
{
	switch(category_selection_type)
	{
		case 'single':
			document.getElementById(category_control_container).style.display = 'inline';
			document.getElementById('TAG_user_limit_category').style.display = 'none';
			break;
			
		case 'multi':
			document.getElementById(category_control_container).style.display = 'block';
			document.getElementById('TAG_user_limit_category').style.display = 'none';
			
			var oc2 = document.getElementById('oc2');
			if(oc2 && oc2.value)
				document.getElementById('oc').value = oc2.value;
			break;
	}
	
	location.hash = "#sc";
}

function redirect_search()
{
	var template_select_elt = document.getElementById('tid');
	var tid = template_select_elt.options[template_select_elt.selectedIndex].value;
	
	var category_select_elt = document.getElementById('cid');
	var cid = category_select_elt.options[category_select_elt.selectedIndex].value;
	
	location.href = "index.php?module=TAG&func=search&tid=" + tid + "&lc=" + cid;
	
}

/**
 * search_other_category()
 *
 * Shows or hides the other_category_on and other_category_off elements,
 * depending on whether show is true or false.
 *
 * @param	int		show			True iff other_category_on should be displayed
 * @param	string	display_type	One of 'inline' or 'block'
 * @return	void
 */	
function search_other_category(show, display_type)
{
	if(show)
	{
		if(document.getElementById('other_category_on'))
			document.getElementById('other_category_on').style.display = display_type;
		if(document.getElementById('other_category_off'))
			document.getElementById('other_category_off').style.display = 'none';
	}
	else
	{
		if(document.getElementById('other_category_on'))
			document.getElementById('other_category_on').style.display = 'none';
		if(document.getElementById('other_category_off'))
			document.getElementById('other_category_off').style.display = display_type;
	}
}

