// Depends of jquery.js
// Used to get States based on country
function loadStates()
{
	var form = document.mainform;
	var p = {
		country:form.country.value
	};
	
	$.ajax({
		type:"POST",
		url:CONTEXTPATH + "/jforum" + SERVLET_EXTENSION + "?module=ajax&action=stateLoad",
		data:p,
		dataType:"script",
		global:false
	});	
}

// Used to get Cities based on country and state
function loadCities(countryCode)
{
	var p = {
		country:countryCode,
		state:document.getElementById("cityState").value
	};
	
	$.ajax({
		type:"POST",
		url:CONTEXTPATH + "/jforum" + SERVLET_EXTENSION + "?module=ajax&action=cityLoad",
		data:p,
		dataType:"script",
		global:false
	});
	document.getElementById("city").value="ZZ";
	searchSubmit('C','','');	
}

// Used to get Category Level 2 based on selected category
function loadCategory()
{
	var form = document.forums;
	var p = {
		category:form.categoryLevel1.value
	};
	
	$.ajax({
		type:"POST",
		url:CONTEXTPATH + "/jforum" + SERVLET_EXTENSION + "?module=ajax&action=categoryLoad",
		data:p,
		dataType:"script",
		global:false
	});	
}

function loadForumId()
{
	var form = document.forums;
	var p = {
		categoryLevel1:form.categoryLevel1.value,
		categoryLevel2:form.categoryLevel2.value,
		placeId:form.placeId.value
	};
	
	$.ajax({
		type:"POST",
		url:CONTEXTPATH + "/jforum" + SERVLET_EXTENSION + "?module=ajax&action=forumIdLoad",
		data:p,
		dataType:"script",
		global:false
	});	
}

//Search Submit
function searchSubmit(forumType, cat1, cat2)
{
	if(forumType == "R")
	{
		var state = document.getElementById("regionalState").value;
		var url = CONTEXTPATH+"/regional-forums/list/0/1/7/" + state ;
		if(cat1 != "" && cat2 != "")
		{
			url = url + "/" + cat1 + "/" + cat2;
		}
		else if(cat1 != "")
		{
			url = url + "/" + cat1;
		}
		url = url + SERVLET_EXTENSION;
		document.location = url;
	}
	else if(forumType == "C")
	{
		var state = document.getElementById("cityState").value;
		var city = document.getElementById("city").value;
		var url = CONTEXTPATH + "/city-forums/list/0/1/7/" + state + "/" + city;
		if(cat1 != "" && cat2 != "")
		{
			url = url + "/" + cat1 + "/" + cat2;
		}
		else if(cat1 != "")
		{
			url = url + "/" + cat1;
		}
		url = url + SERVLET_EXTENSION;
		document.location = url;
	}
}

function expandAll(path)
{
	var divCount = document.divform.divcount.value;
	for(i=1;i<divCount;i++)
	{
		document.getElementById("div"+i).style.display='inline';
		document.getElementById("expandImage"+i).src = path +"/images/minus.gif";
	}
}

function colapseAll(path)
{
	var divCount = document.divform.divcount.value;
	for(i=1;i<divCount;i++)
	{
		document.getElementById("div"+i).style.display='none';
		document.getElementById("expandImage"+i).src = path +"/images/plus.gif";
	}
}

function showHide(path, id, currentId)
{
	var image0 = document.getElementById("expandImage"+id);
	if(document.getElementById(currentId).style.display=='none')
	{
		document.getElementById(currentId).style.display='inline';
		image0.src= path +"/images/minus.gif";
	}
	else
	{		
		document.getElementById(currentId).style.display='none';
		image0.src= path +"/images/plus.gif";
	}
}

function showHideCat(path, id, currentId)
{
	var image0 = document.getElementById("expandCatImage"+id);
	if(document.getElementById(currentId).style.display=='none')
	{
		document.getElementById(currentId).style.display='inline';
		image0.src= path +"/images/minusbottom.gif";
	}
	else
	{		
		document.getElementById(currentId).style.display='none';
		image0.src= path +"/images/plusbottom.gif";
	}
}

function showHideCat2(path, id, currentId)
{
	var image0 = document.getElementById("expandCatImage2"+id);
	if(document.getElementById(currentId).style.display=='none')
	{
		document.getElementById(currentId).style.display='inline';
		image0.src= path +"/images/minusbottom.gif";
	}
	else
	{		
		document.getElementById(currentId).style.display='none';
		image0.src= path +"/images/plusbottom.gif";
	}
}

function showHideCat3(path, id, currentId)
{
	var image0 = document.getElementById("expandCatImage3"+id);
	if(document.getElementById(currentId).style.display=='none')
	{
		document.getElementById(currentId).style.display='inline';
		image0.src= path +"/images/minusbottom.gif";
	}
	else
	{		
		document.getElementById(currentId).style.display='none';
		image0.src= path +"/images/plusbottom.gif";
	}
}

function showHideCat4(path, id, currentId)
{
	var image0 = document.getElementById("expandCatImage4"+id);
	if(document.getElementById(currentId).style.display=='none')
	{
		document.getElementById(currentId).style.display='inline';
		image0.src= path +"/images/minusbottom.gif";
	}
	else
	{		
		document.getElementById(currentId).style.display='none';
		image0.src= path +"/images/plusbottom.gif";
	}
}

var previousLayer="";
function showlayer(path,layer)
{
	var myLayer = document.getElementById(layer);
	var imageId = document.getElementById("expand"+layer);
	var myArray = new Array("table1","table2","table3","table4");
	if(myLayer.style.display=="none" || myLayer.style.display=="")
	{
		if(previousLayer!=layer && previousLayer !="")
		{		
			document.getElementById(previousLayer).style.display='none';
			document.getElementById("expand"+previousLayer).src= path +"/images/menu_plus.gif";
		}
		else
		{
			var noOfElements = myArray.length;
			for(var i=0;i<noOfElements;i++)
			{
				if(myArray[i] != layer)
				{
					document.getElementById(myArray[i]).style.display='none';
					document.getElementById("expand"+myArray[i]).src= path +"/images/menu_plus.gif";		
				}
			}
		}
		myLayer.style.display="block";
		imageId.src= path +"/images/menu_minus.gif";
	}
	else
	{
		myLayer.style.display="none";
		imageId.src= path +"/images/menu_plus.gif";
	}
	previousLayer=layer;
}

function addNewTopic(logInId, placeId, forumId, module, state, city)
{
	var check = true;
	if(module == "city-forums")
	{
		if(state == "ZZ")
		{
			alert("Please select a State");
			check = false;
		}
		else if(city == "ZZ")
		{
			alert("Please select a City");
			check = false;
		}
	}
	else if(module == "regional-forums")
	{
		if(state == "ZZ")
		{
			alert("Please select a State");
			check = false;
		}
	}
	
	if(check==true && forumId != 0)
	{
		window.location.href = CONTEXTPATH+"/posts/insert/"+forumId+SERVLET_EXTENSION;
	}
	else if(check==true)
	{
		if (document.all)
		{               
			document.body.scroll = "no";
		}
		else
		{
			var oTop = document.body.scrollTop;
			document.body.style.overflow = "hidden";
			document.body.scrollTop = oTop;
		}
			
		//document.getElementById('fade').style.height= docHeight;	
		document.getElementById('forumsLoadForm').style.display='block';
		document.getElementById('fade').style.display='block';
	}
}

function hideSearchForm()
{
	document.getElementById('forumsLoadForm').style.display='none';
	document.getElementById('fade').style.display='none'
	document.body.scroll = "";
    document.body.style.overflow = "";
}

function submitNewPostForm()
{
	var forumId = document.forums.forum_id.value;
	if(forumId != 0)
		window.location.href = CONTEXTPATH+"/posts/insert/"+forumId+SERVLET_EXTENSION;
	else
		alert("Please select the two levels of categories.");
}

function changeSortOrder(selectBox, moduleName, action, daysLimit, cat1, cat2)
{
	var selectedValue = selectBox[selectBox.selectedIndex].value;
	var path = CONTEXTPATH + "/" + moduleName + "/" + action + "/0/" + selectedValue +"/"+ daysLimit;
	if(cat1 != "")
	{
		path += "/" + cat1;
	}
	if(cat2 != "")
	{
		path += "/" + cat2;
	}
	path += SERVLET_EXTENSION;
	window.location.href = path;
}

function changeMaxDays(selectBox, moduleName, action, sortOption, cat1, cat2)
{
	var selectedValue = selectBox[selectBox.selectedIndex].value;
	var path = CONTEXTPATH + "/" + moduleName + "/" + action + "/0/" + sortOption +"/"+ selectedValue;
	if(cat1 != "")
	{
		path += "/" + cat1;
	}
	if(cat2 != "")
	{
		path += "/" + cat2;
	}
	path += SERVLET_EXTENSION;
	window.location.href = path;
}