String.prototype.namespace = function(separator) {var ns = this.split(separator || '.'), p = window;   for (i = 0; i < ns.length; i++) {p = p[ns[i]] = p[ns[i]] || {};   }   };  
"Forums.Editor".namespace();
"Forums.Messages".namespace();

Forums.Messages.ShowResponse = function(sender, id)
{
	document.getElementById("Response" + id).style.display = "";
	sender.className = "Less";
}

Forums.Editor.InsertSmiley = function (code)
{
	Forums.Editor.Form = document.getElementById("formAddMessage");
	Forums.Editor.AddTxt = " " + code;
	Forums.Editor.AddText(Forums.Editor.AddTxt);
}
Forums.Editor.InsertCode = function (code)
{
	Forums.Editor.Text = null;
	Forums.Editor.Form = document.getElementById("formAddMessage");
	Forums.Editor.GetActiveText();
	
	if (this.Text != null)
	{
		if (this.Text.substring(0, code.length + 2 ) == "[" + code + "]" && Forums.Editor.Text.substring(Forums.Editor.Text.length - code.length - 3, Forums.Editor.Text.length) == "[/" + code + "]")
		{
			Forums.Editor.AddTxt = Forums.Editor.Text.substring(code.length + 2, Forums.Editor.Text.length - code.length - 3);
		}
		else
		{
			Forums.Editor.AddTxt = "[" + code + "]" + Forums.Editor.Text + "[/" + code + "]";
		}
		Forums.Editor.AddText(Forums.Editor.AddTxt);
	}
	else if ((Forums.Editor.Form.message.selectionStart || Forums.Editor.Form.message.selectionStart == '0') && Forums.Editor.Form.message.selectionStart != Forums.Editor.Form.message.selectionEnd)
	{ 
		var start_selection = Forums.Editor.Form.message.selectionStart;
		var end_selection = Forums.Editor.Form.message.selectionEnd;

		var start = (Forums.Editor.Form.message.value).substring(0, start_selection);
		var middle = (Forums.Editor.Form.message.value).substring(start_selection, end_selection);
		var end = (Forums.Editor.Form.message.value).substring(end_selection, Forums.Editor.Form.message.textLength);

		if (middle.substring(0, code.length + 2 ) == "[" + code + "]" && middle.substring(middle.length - code.length - 3, middle.length) == "[/" + code + "]")
		{
			middle = middle.substring(code.length + 2, middle.length - code.length - 3);
		}
		else
		{
			middle = "[" + code + "]" + middle + "[/" + code + "]";
		}

		Forums.Editor.Form.message.value = start + middle + end;
		Forums.Editor.SetFocus();
		Forums.Editor.Form.message.selectionStart = end_selection + middle.length;
		Forums.Editor.Form.message.selectionEnd = start_selection + middle.length;
		return false;
	}
}

Forums.Editor.GetActiveText = function()
{
	Forums.Editor.SetFocus();
	if (!document.selection)
	{
		return false;
	}
	var sel = document.selection;
	var rng = sel.createRange();

	if (rng != null && (sel.type == "Text" || sel.type == "None"))
	{
		Forums.Editor.Text = rng.text;
	}
	if (rng != null && Forums.Editor.Form.message.createTextRange)
	{
		Forums.Editor.Form.message.caretPos = rng.duplicate();
	}
	return true;
}

Forums.Editor.AddText = function (NewCode)
{
	if (typeof(Forums.Editor.Form.message.createTextRange) != "undefined" && Forums.Editor.Form.message.caretPos)
	{
		var caretPos = Forums.Editor.Form.message.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
		caretPos.select();
	}
	else if (Forums.Editor.Form.message.selectionStart || Forums.Editor.Form.message.selectionStart == '0')
	{ 
		var start_selection = Forums.Editor.Form.message.selectionStart;
		var end_selection = Forums.Editor.Form.message.selectionEnd;

		var start = (Forums.Editor.Form.message.value).substring(0, start_selection);
		var middle = NewCode;
		var end = (Forums.Editor.Form.message.value).substring(end_selection, Forums.Editor.Form.message.textLength);

		Forums.Editor.Form.message.value = start + middle + end;
		Forums.Editor.SetFocus();
		Forums.Editor.Form.message.selectionStart = end_selection + middle.length;
		Forums.Editor.Form.message.selectionEnd = start_selection + middle.length;
		Forums.Editor.GetActiveText();
		Forums.Editor.AddTxt = "";
		return;
	}
	else
	{
		Forums.Editor.Form.message.value += NewCode;
	}
	Forums.Editor.SetFocus();
	Forums.Editor.GetActiveText();
	Forums.Editor.AddTxt = "";
}

Forums.Editor.SetFocus = function()
{
	Forums.Editor.Form.message.focus();
}

function AddMessage(){
	if(document.getElementById('forumSubject').value!=""&&document.getElementById('forumMessage').value!="")
		document.getElementById('formAddMessage').submit();
	else
		document.getElementById('errormessage').style.visibility="visible";
}

function ValidateAlias(){
	if(document.getElementById('txtAlias').value.length >=4 && document.getElementById('txtAlias').value.indexOf(' ')==-1)
		document.getElementById('aliasForm').submit();
	else
		document.getElementById('errormessage').style.visibility="visible";

}

function ConfirmReport(url,message)
{
	if(confirm(message))
	{
		window.location=url;
	}
}

function SubmitWhenSearchIsNotEmptyNl()
{
	if(document.getElementById('forumsearch').value.length > 0)
	
	{	
		var form = document.getElementById("forumSearchForm");
		form.action = "/map/show/175787/q/" + document.getElementById("forumsearch").value + "/site/GSA_TestAnkoop_Forums/frontend/dutch_frontend/resultpage/175787/page/ForumSearchResult/.htm";
		window.location.href = form.action;
		return false;
	}
	else
	{
		form.search.focus();
		return true;
	}
}

function SubmitWhenSearchIsNotEmptyFr()
{
	if(document.getElementById('forumsearch').value.length > 0)
	
	{	
		var form = document.getElementById("forumSearchForm");
		form.action = "/map/show/175812/q/" + document.getElementById("forumsearch").value + "/site/GSA_TestAchats_Forums/frontend/french_frontend/resultpage/175812/page/ForumSearchResultFr/.htm";
		window.location.href = form.action;
		return false;
	}
	else
	{
		form.search.focus();
		return true;
	}
}
