function hideElement(element)
{
	//alert ("hide elem "+element);
	var o = document.getElementById(element); 
	o.style.display = "none";
}

function showElement(element)
{
	//alert ("show elem "+element);
	var o = document.getElementById(element);
	o.style.display = "";
}

//Category functions
var categories = new Array();
var categories_by_pk = new Array();
var show_category_warning = 0;

function addCategory(index,category_pk,category_name,subcategory_pk,subcategory_name,is_event,show_price,allow_forsale,allow_isrental,allow_onoffer,allow_iswanted,is_adult,registered_users_only)
{
	var oCategory = new category();
	oCategory.category_pk = category_pk;
	oCategory.category_name = category_name;
	oCategory.subcategory_pk = subcategory_pk;
	oCategory.subcategory_name = subcategory_name;		
	oCategory.is_event = is_event;
	oCategory.show_price = show_price;
	oCategory.allow_forsale = allow_forsale;
	oCategory.allow_isrental = allow_isrental;
	oCategory.allow_onoffer = allow_onoffer;
	oCategory.allow_iswanted = allow_iswanted;
	oCategory.is_adult = is_adult;
	oCategory.registered_users_only = registered_users_only;
	
	categories[index]= oCategory;			
	categories_by_pk[category_pk]= oCategory;	
}

function category()
{
	this.category_pk = 0;
	this.category_name = "";
	this.subcategory_pk = 0;
	this.subcategory_name = "";		
	this.parent_category_fk = 0;
	this.is_event = 0;
	this.show_price = 0;
	this.allow_forsale = 0;
	this.allow_isrental = 0;
	this.allow_onoffer = 0;
	this.allow_iswanted = 0;
	this.is_adult = 0;
	this.registered_users_only = 0;
	
			
}

function forceSelected(element,id)
{
	for(i=0;i<=element.childNodes.length-1;i++)
	{
		if(element.childNodes[i].value == id)
		{
			element.childNodes[i].selected = true;
		}
		else
		{
			element.childNodes[i].selected = false;
		}
	}
}

function loadCategories(selected_category_pk)
{
	//clear the category 'select' element
	var cat_element = document.getElementById('_category');
	clearSelect(cat_element);
	
	//hide the subcategory 'select' element abd the 'ad_type' element
	hideElement('_prepost_type_row'); // just in case
	hideElement('_subcategory_block'); // just in case
	hideElement('_submitPost');
		
	//add an option to the select for (Select Category...)
	addSelectOption(cat_element,0,"(Select Category...)",true);
	var _currentCategoryPK = -1;	
	for(i = 0; i<= categories.length -1;i++)
	{
		if(categories[i].category_pk != 31)
		{
			if(categories[i].category_pk != _currentCategoryPK)
			{
				
				if(categories[i].category_pk == selected_category_pk)
				{
					addSelectOption(cat_element,categories[i].category_pk , categories[i].category_name,true);
				}
				else
				{
					addSelectOption(cat_element,categories[i].category_pk , categories[i].category_name,false);
				}
			}
		}
		_currentCategoryPK = categories[i].category_pk;
	}
}

function _hide_warnings()
{
	hideElement("_prepost_warning_jobs");
	hideElement("_prepost_warning_bus_serv");
	hideElement("_prepost_warning_serv");
}

function loadSubCategories(category_pk)
{
	// clear the subcategory 'select' element
	var subcat_element = document.getElementById('_subcategory');
	clearSelect(subcat_element);
	hideElement('_prepost_type_row'); // just in case
	hideElement('_submitPost');
	
	if(category_pk>0)
	{
		//show the subcat element
		showElement('_subcategory_block');
		_hide_warnings();
				
		//add the option to the select for (Select Subcategory...)
		addSelectOption(subcat_element,0,"(Select Subcategory...)",false);
		
		//do the subcategories that dont contain 'Other' first
		for(i = 0; i<= categories.length -1;i++)
		{			
			if(categories[i].category_pk == category_pk)
			{
				if(categories[i].subcategory_name.indexOf("Other") == -1)
				{
					addSelectOption(subcat_element,categories[i].subcategory_pk , categories[i].subcategory_name,false);
				}					
			}
		}
		//now add the item/s with 'Other' in the name
		for(i = 0; i<= categories.length -1;i++)
		{			
			if(categories[i].category_pk == category_pk)
			{
				if(categories[i].subcategory_name.indexOf("Other") > -1)
				{
					addSelectOption(subcat_element,categories[i].subcategory_pk , categories[i].subcategory_name);
				}					
			}
		}

		// if we choose the 'Jobs' category, we need to show a warning before showing the 'Continue' button. 
		if (category_pk==14) //jobs
		{
			show_category_warning = 1;
		}
		else if (category_pk==35) //Business Services
		{
			show_category_warning = 2;
		}
		else if (category_pk==30) //Services
		{
			show_category_warning = 3;
		}
		else
		{
			show_category_warning = 0;
		}
	}
	else // just in case
	{
		//hide the subcat element
		hideElement('_subcategory_block');
		hideElement('_submitPost');
		_hide_warnings();
	}
}

function _show_warning(warning_ref)
{
	if (warning_ref==1)
	{
		showElement("_prepost_warning_jobs");
	}
	else if (warning_ref==2)
	{
		showElement("_prepost_warning_bus_serv");
	}
	else if (warning_ref==3)
	{
		showElement("_prepost_warning_serv");
	}
	
}

function rebuild_type_list(element,options_array)
{
	var string = "";
	var list_option_names = new Array("For Sale","To Rent","On Offer","Wanted","Job Wanted","Job On Offer");
	var list_option_values = new Array("forsale","rental","onoffer","wanted","wanted","onoffer");
	
	var onClick_func = 'showElement("_submitPost")';
	if (show_category_warning)
	{
		onClick_func='_show_warning(show_category_warning)';
	}
	
	for (i=0;i<options_array.length;i++)
	{
		var j=options_array[i];
		string = string+"<input type='radio' name='advert_type' value='"+list_option_values[j]+"' onClick='"+onClick_func+"'>&nbsp;"+list_option_names[j]+"<br>";
	}
	
	element.innerHTML=string;
}

function loadAdvertTypes(subcategory_pk)
{
	var type_element = document.getElementById('_advert_type');
	hideElement('_submitPost');
	_hide_warnings();
	
	//if the user has selected the (Select Subcategory...) option then we hide price regardless		
	if(subcategory_pk==0)
	{
		hideElement('_prepost_type_row');
		_hide_warnings();
	}
	else
	{
		for(i = 0; i<= categories.length -1;i++)
		{	
			if(categories[i].subcategory_pk == subcategory_pk)
			{
				var options_array = new Array();
				var option_pos = 0;
				if(categories[i].allow_forsale == 1)
				{
					options_array[option_pos] = 0; // for sale
					option_pos++;
				}
				if(categories[i].allow_isrental == 1)
				{
					options_array[option_pos] = 1; // is rental
					option_pos++;
				}
				if(categories[i].allow_onoffer == 1)
				{
					if (categories[i].category_pk==14) // jobs
					{
						options_array[option_pos] = 5; // job on offer
					}
					else
					{
						options_array[option_pos] = 2; // on offer
					}
					option_pos++;
				}
				if(categories[i].allow_iswanted == 1)
				{
					if (categories[i].category_pk==14) // jobs
					{
						options_array[option_pos] = 4; // job wanted
					}
					else
					{
						options_array[option_pos] = 3; //  wanted
					}
					option_pos++;
				}
			}
		}
		
		if(option_pos>0) // ie we actually have some options to display
		{
			rebuild_type_list(type_element,options_array);
			showElement('_prepost_type_row');
		}
		else // we have no options to display, show/hide the relevant elements 
		{
			hideElement('_prepost_type_row');
			_hide_warnings();
			showElement('_submitPost');
		}
						
	}
}


