function browsertype()
{
	if (navigator.userAgent.indexOf('Opera') != -1)
		return 'Opera';
	else if (navigator.userAgent.indexOf('Gecko') != -1)
		return 'Mozilla';
	else
		return 'IE';
}

function inserttext(text)
{
	if (document.writepost.text.caretPos)
		document.writepost.text.caretPos.text = document.writepost.text.caretPos.text.charAt(document.writepost.text.caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	else
		document.writepost.text.value += text;
	document.writepost.text.focus();
}

function imglink() {
  if (browsertype() == 'IE' && document.selection.type == 'Text') {
    inserttext('[imglink='+document.selection.createRange().text+']'+document.selection.createRange().text+'[/imglink]');
    document.selection.clear;
  } else {
    var url = prompt('URL link:', 'http://');
    var pic = prompt('URL image:', 'http://');
    if (url != '' && url != null)
    if (pic != '' && pic != null)
      inserttext('[imglink='+url+']'+pic+'[/imglink]');
  }
}

function dohint() {
  if (browsertype() == 'IE' && document.selection.type == 'Text') {
    inserttext('[hint='+document.selection.createRange().text+']'+document.selection.createRange().text+'[/hint]');
    document.selection.clear;
  } else {
    var name = prompt('Hint name:', 'Hint');
    var text = prompt('Hint text:', 'Text');
    if (name != '' && name != null)
    if (text != '' && text != null)
      inserttext('[hint='+name+']'+text+'[/hint]');
  }
}

      function showhint(block) {
            block.nextSibling.nextSibling.style.display ="block";
            block.parentNode.removeChild(block);
      }

      function insertimg(text) {
      	text = ' ' + text + ' ';
      	if (document.writepost.text.createTextRange && document.writepost.text.caretPos) {
      		var caretPos = document.writepost.text.caretPos;
      		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
      		document.writepost.text.focus();
      	} else {
      	document.writepost.text.value  += text;
      	document.writepost.text.focus();
      	}
      }

      function dosmilie(text) {
      	text = ' ' + text + ' ';
      	if (document.writepost.text.createTextRange && document.writepost.text.caretPos) {
      		var caretPos = document.writepost.text.caretPos;
      		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
      		document.writepost.text.focus();
      	} else {
      	document.writepost.text.value  += text;
      	document.writepost.text.focus();
      	}
      }


      function updateprogressbar(sofar)
      {
      	outerwidth = pbouter.style.width;
      	maxchars = 20000;
	
      	percent = sofar / maxchars;
      	if (percent > 1) percent = 1;
	
      	if (percent >= 0.8) color = "#FF0000"; // red
      	else if (percent >= 0.6) color= "#FFFF00"; // yellow
      	else color = "#00FF00"; // green
      	
      	if (outerwidth.indexOf("px") != -1)
      	{
      		pbinner.style.width = parseInt(percent * parseFloat(outerwidth)) + "px";
      	}
      	else // assume a percent
      	{
      		pbinner.style.width = parseInt(percent * 100) + "%";
      	}
      	pbinner.style.backgroundColor = color;
      	
      	typedsofar.innerHTML = sofar;
      	window.setTimeout("updateprogressbar(document.writepost.text.value.length)", 2000);
      }

      var postmaxchars = 20000;
      function validate(writepost) {
            if (writepost.posttitle.value=="") {
      		alert("Post title cannot be empty.\nPlease, fill it and try again.");
      		return false; }
            if (writepost.text.value=="") {
      		alert("Post message cannot be empty.\nPlease, fill it and try again.");
      		return false; }
      	if (postmaxchars != 0) {
      		if (writepost.text.value.length > 20000) {
      			alert("Your message is too long.\n\nPlease, shorten it to 20000 characters.\nCurrently it's "+writepost.text.value.length+" characters long.");
      			return false; }
      		else { return true; }
      	} else { return true; }
      }

      function canned(number)
      {
          if (number != 0) document.writepost.text.value = cannedtext[number];
      }

// script by subBlue design www.subBlue.com

// Startup variables
var imageTag = false;
var theSelection = false;

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav  = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);

bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[php]','[/php]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]','[google]','[/google]','[center]','[/center]');
imageTag = false;


// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}


function checkForm() {

	formErrors = false;

	if (document.writepost.text.value.length < 2) {
		formErrors = "You must enter a message when posting.";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		bbstyle(-1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		return true;
	}
}

function emoticon(text) {
	text = ' ' + text + ' ';
	if (document.writepost.text.createTextRange && document.writepost.text.caretPos) {
		var caretPos = document.writepost.text.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		document.writepost.text.focus();
	} else {
	document.writepost.text.value  += text;
	document.writepost.text.focus();
	}
}

function bbfontstyle(bbopen, bbclose) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.writepost.text.value += bbopen + bbclose;
			document.writepost.text.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		document.writepost.text.focus();
		return;
	} else {
		document.writepost.text.value += bbopen + bbclose;
		document.writepost.text.focus();
		return;
	}
	storeCaret(document.writepost.text);
}


function bbstyle(bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			document.writepost.text.value += bbtags[butnumber + 1];
			buttext = eval('document.writepost.addbbcode' + butnumber + '.value');
			eval('document.writepost.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		document.writepost.text.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
		theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
		document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
		document.writepost.text.focus();
		theSelection = '';
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				document.writepost.text.value += bbtags[butnumber + 1];
				buttext = eval('document.writepost.addbbcode' + butnumber + '.value');
				eval('document.writepost.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			document.writepost.text.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			document.writepost.text.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.writepost.addbbcode14.value = "Slika";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		document.writepost.text.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('document.writepost.addbbcode'+bbnumber+'.value += "*"');
		document.writepost.text.focus();
		return;
	}
	storeCaret(document.writepost.text);
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
function expandcontract(tbodyid,ClickIcon) {
            if (parent.document.getElementById(ClickIcon).innerHTML == "+"){
            parent.document.getElementById(tbodyid).style.display = "";
            parent.document.getElementById(ClickIcon).innerHTML = "-";
            } else {
            parent.document.getElementById(tbodyid).style.display = "none";
            parent.document.getElementById(ClickIcon).innerHTML = "+";
            }
            }

function opentbox(tbodyid,ClickIcon) {
            if (parent.document.writepost.isenabled.value == '0'){
            parent.document.getElementById(tbodyid).style.display = "none";
            }
            if (parent.document.writepost.isenabled.value == '1'){
            parent.document.getElementById(tbodyid).style.display = "none";
            }
            if (parent.document.writepost.isenabled.value == '2'){
            parent.document.getElementById(tbodyid).style.display = "";
            }
            if (parent.document.writepost.isenabled.value == '3'){
            parent.document.getElementById(tbodyid).style.display = "";
            }
            if (parent.document.writepost.isenabled.value == '4'){
            parent.document.getElementById(tbodyid).style.display = "";
            }
            if (parent.document.writepost.isenabled.value == '5'){
            parent.document.getElementById(tbodyid).style.display = "";
            }
            if (parent.document.writepost.isenabled.value == '6'){
            parent.document.getElementById(tbodyid).style.display = "";
            }
            }

var varSecondsRemaining = 45;
var timerId = null;

function UpdateTimer() {
	if (varSecondsRemaining > 0) {
		varSecondsRemaining--;
		timerId = setTimeout("UpdateTimer()", 1000);
	}
	else {
		clearTimeout(timerId);
		ajax_refreshshouts();
		varSecondsRemaining = 45;
	}
}

function StopTimer() {
	clearTimeout(timerId);
}

function UpdateCounter(txt) {
	var maxlength = 250;
	var txtlength = txt.value.length;

	if (txtlength > maxlength) {
		txt.value = txt.value.substr(0,maxlength);
		txtlength = maxlength;
	}

	document.getElementById("txtCounter").value = (maxlength - txtlength);
}
/*
function shoutsmile(smile) {
	frmShoutbox.message.value = frmShoutbox.message.value + smile;
}
*/


function ajax_refreshshouts() {

	var d = new Date();
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	var curr_sec = d.getSeconds();
	var curr_time = curr_hour + ':' + curr_min + ':' + curr_sec;

	var myRequest = new Request(
		{method: 'post',
		 onComplete: function(response) {
            document.getElementById('div_shoutbox').innerHTML = response;
			StopTimer();
			UpdateTimer();
			document.getElementById('div_shoutboxrefreshed').innerHTML = curr_time;
         },
		 onRequest: function() { /*document.getElementById('div_shoutbox').innerHTML = 'Nalagam';*/ },
		 url: '/new_shoutbox/pages/shoutbox/shoutboxfunctions.inc.php',
		//url: '/pages/shoutbox/shoutboxfunctions.inc.php',
		 data: 'ajax=true&action=ajax_refreshshouts'}
	);
	myRequest.send();
}

function ajax_submitshout(isfor,message) {
    if (isfor == '') {
        return false;
    }
    if (message == '') {
        return false;
    }

	var myRequest = new Request(
		{method: 'post',
		 onComplete: function(response) {
		 	StopTimer();
			UpdateTimer();
			ajax_refreshshouts();
			document.getElementById('shoutMessage').value = '';
         },
		 onRequest: function() { },
		 url: '/new_shoutbox/pages/shoutbox/shoutboxfunctions.inc.php',
		//url: '/pages/shoutbox/shoutboxfunctions.inc.php',
		 data: 'ajax=true&action=ajax_submitshout&isfor='+isfor+'&message='+message}
	);
	myRequest.send();
}

function ajax_deleteshout(id) {
    if (id == '') {
        return false;
    }

	var myRequest = new Request(
		{method: 'post',
		 onComplete: function(response) {
		 	StopTimer();
			UpdateTimer();
			ajax_refreshshouts();
         },
		 onRequest: function() { },
		 url: '/new_shoutbox/pages/shoutbox/shoutboxfunctions.inc.php',
		//url: '/pages/shoutbox/shoutboxfunctions.inc.php',

		 data: 'ajax=true&action=ajax_deleteshout&id='+id}
	);
	myRequest.send();
}
//-->
