// faste variabler

var text_enter_url      = "Website URL";
var text_enter_url_name = "Website title:";
var text_enter_image    = "Image URL:";
var text_enter_email    = "Email:";
var text_code           = "Example: [CODE] Your code here... [/CODE]";
var text_quote          = "Example: [QUOTE] Your quote here... [/QUOTE]";
var error_no_url        = "URL is not optional";
var error_no_title      = "Title is not optional";
var error_no_email      = "Email is not optional";
var error_no_width      = "Width is not optional";
var error_no_height     = "Height is not optional";
var prompt_start        = "Text:";

var help_standard				= "This line is your guide";
var help_smiley         = "Insert smiley (click the smiley)";
var help_bold           = "Insert bold text (shortcut: Alt + B)";
var help_italic         = "Insert italic text (shortcut: Alt + I)";
var help_under          = "Insert underlined text (shortcut: Alt + U)";
var help_url            = "Insert link (shortcut: Alt+ H)";
var help_img            = "Insert image (shortcut: Alt + G)";
var help_email          = "Insert email (shortcut: Alt + E)";
var help_quote          = "Insert quote (shortcut: Alt + Q)";
var help_list           = "Insert list (shortcut: Alt + L)";
var help_code           = "Indsæt neutral tekst (shortcut: Alt + P)";
var help_click_close    = "Click this button to close";
var list_prompt         = "Type in your subjects, and click \"cancel\" or leave the field empty when you're done";

// specielle tags
var B_open = 0;
var I_open = 0;
var U_open = 0;
var QUOTE_open = 0;
var CODE_open = 0;
var SQL_open = 0;
var HTML_open = 0;

var bbtags   = new Array();

// browser test - Lånt fra http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var myAgent   = navigator.userAgent.toLowerCase();
var myVersion = parseInt(navigator.appVersion);

var is_ie   = ((myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1));
var is_nav  = ((myAgent.indexOf('mozilla')!=-1) && (myAgent.indexOf('spoofer')==-1)
                && (myAgent.indexOf('compatible') == -1) && (myAgent.indexOf('opera')==-1)
                && (myAgent.indexOf('webtv') ==-1)       && (myAgent.indexOf('hotjava')==-1));

var is_win   =  ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));
var is_mac    = (myAgent.indexOf("mac")!=-1);

// hjælpe tekst
function hstat(msg)
{
	document.forumMessageForm.helpbox.value = eval("help_" + msg);
}


// smiley indsættelse
function insertSmiley(smiley) {
	
	var obj_ta = document.forumMessageForm.message;

	if ( (myVersion >= 4) && is_ie && is_win) // Ensure it works for IE4up / Win only
	{
		if(obj_ta.isTextEdit){ // this doesn't work for NS, but it works for IE 4+ and compatible browsers
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				rng.text = smiley;
			}
		}
		else{
			obj_ta.value += smiley;
		}
	}
	else
	{
		obj_ta.value += smiley;
	}

	obj_ta.focus();
	
	//document.forumMessageForm.message.value += smiley;
}


// stacksize
function stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ ) {
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) {
			return i;
		}
	}
	
	return thearray.length;
}

// pushstack
function pushstack(thearray, newval)
{
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

// Popstack
function popstack(thearray)
{
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}

// tilføj kode
function add_code(NewCode)
{
    document.forumMessageForm.message.value += NewCode;
    document.forumMessageForm.message.focus();
}


// de nemme tags - B, I, U osv.
function simpletag(thetag)
{
	var tagOpen = eval(thetag + "_open");
	
		if (tagOpen == 0) {
			if(doInsert("[" + thetag + "]", "[/" + thetag + "]", true)){
				eval(thetag + "_open = 1");
				// ændre udseende af knappen
				eval("document.forumMessageForm." + thetag + ".value += '*'");
		
				pushstack(bbtags, thetag);
				hstat('click_close');
			}
		}
		else {
			// find hvor det samme sidste tag er åbent
			lastindex = 0;
			
			for (i = 0 ; i < bbtags.length; i++ ) {
				if ( bbtags[i] == thetag ) {
					lastindex = i;
				}
			}
			
			// lukker det specielle tag
			while (bbtags[lastindex]) {
				tagRemove = popstack(bbtags);
				doInsert("[/" + tagRemove + "]", "", false)
				
				// ændre udseende af knappen
				eval("document.forumMessageForm." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			}
		}
}


// list tag
function tag_list()
{
	var listvalue = "init";
	var thelist = "[LIST]\n";
	
	while ( (listvalue != "") && (listvalue != null) ) {
		listvalue = prompt(list_prompt, "");
		if ( (listvalue != "") && (listvalue != null) ) {
			thelist = thelist+"[-]"+listvalue+"\n";
		}
	}

	doInsert(thelist + "[/LIST]\n", "", false);
}

// http tag
function tag_url()
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_url, "http://");
    var enterTITLE = prompt(text_enter_url_name, "Website URL");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
			if (FoundErrors != '') {
				FondErrors += '\n';
			}
			
      FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Fejl!"+FoundErrors);
        return;
    }

	doInsert("[URL="+enterURL+"]"+enterTITLE+"[/URL]", "", false);
}

// billede tag
function tag_image()
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_image, "http://");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }

    if (FoundErrors) {
        alert("Fejl!"+FoundErrors);
        return;
    }

	doInsert("[IMG]"+enterURL+"[/IMG]", "", false);
}

// EMail tag
function tag_email()
{
    var emailAddress = prompt(text_enter_email, "");

    if (!emailAddress) { 
		alert(error_no_email); 
		return; 
	}

	doInsert("[EMAIL]"+emailAddress+"[/EMAIL]", "", false);
}

// generel funktion til insættelse af ting
function doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = document.forumMessageForm.message;

	if ( (myVersion >= 4) && is_ie && is_win) // Ensure it works for IE4up / Win only
	{
		if(obj_ta.isTextEdit){ // this doesn't work for NS, but it works for IE 4+ and compatible browsers
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;
	
				rng.text = ibTag;
			}
		}
		else{
			if(isSingle)
				isClose = true;
	
			obj_ta.value += ibTag;
		}
	}
	else
	{
		if(isSingle)
			isClose = true;

		obj_ta.value += ibTag;
	}

	obj_ta.focus();

	return isClose;
}	

function validateForumMessageForm(intType) {
	
	error = 0;
	
	if (intType == '1') {
		
		if ((document.forumMessageForm.id.value == '0') && (error == 0)) {
			alert('Board is required.');
			error = 1;
		}
		
		if ((document.forumMessageForm.title.value == '') && (error == 0)) {
			alert('Subject is required.');
			error = 1;
		}
		
	}
	
	if ((document.forumMessageForm.message.value == '') && (error == 0)) {
		alert('Post is required.');
		error = 1;
	}
	
	/*
	if ((B_open == 1) || (I_open == 1) || (U_open == 1) || (QUOTE_open == 1) || (CODE_open == 1) || (SQL_open == 1) || (HTML_open == 1)) {
		alert('Alle tags skal lukkes!');
		error = 1;
	}
	*/
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
	
}

function validateUserMessageForm() {
	
	error = 0;
	
	if ((document.forumMessageForm.title.value == '') && (error == 0)) {
		alert('Subject is required.');
		error = 1;
	}
	
	if ((document.forumMessageForm.message.value == '') && (error == 0)) {
		alert('Message is required.');
		error = 1;
	}
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
	
}

function validateForumSearchForm() {
	
	error = 0;
	
	if ((document.forumSearchForm.search_title.value == '') && (error == 0)) {
		alert('Search box is required');
		error = 1;
	}
	
	if ((document.forumSearchForm.board_id.value == '-1') && (error == 0)) {
		alert('Board is required');
		error = 1;
	}
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
	
}

function confirmMessageReset() {
	
	if (document.forumMessageForm.message.value != '') {
		if (confirm('Are you sure you want to reset?')) {
			history.go(-1);
		}
	} else {
		history.go(-1);
	}
	
}

function confirmInboxDelete(intMessageID) {
	
	if (confirm('Are you sure you want to delete this message?')) {
		document.location = 'delete.asp?id=' + intMessageID;
	}
	
}

function confirmMessageDelete(intMessageID) {
	
	if (confirm('Are you sure you want to delete this post?')) {
		document.location = '/content/forum/delete.asp?id=' + intMessageID;
	}
	
}

function confirmMessageDeleteThread(intMessageID) {
	
	if (confirm('Are you sure you want to delete the ENTIRE THREAD?')) {
		document.location = '/content/forum/delete.asp?id=' + intMessageID;
	}
	
}

function validateBoardLockForm() {
	
	error = 0;
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
	
}

function validateThreadLockForm() {
	
	error = 0;
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
	
}

function validateThreadMoveForm() {
	
	error = 0;
	
	if ((document.moderatorForm.board_id.value == '0') && (error == 0)) {
		alert('Board is required');
		error = 1;
	}
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
	
}