function checkFormElement(Form, FieldName) {
    return true;
}


function checkFormElements()
{
	return true;
}

function checkFormElements2()
{
	el =  document.getElementById('url');
	if (el.value=="")
	{
		alert("Нов ужас без URL не е ужас.");
		return false;
	}

	el =  document.getElementById('headline');
	if (el.value=="")
	{
		alert("Ужасите изглеждат по-хубаво, когато си имат заглавие.");
		return false;
	}
	
	return true;
}



var isie = navigator.userAgent.indexOf('MSIE') > -1;
var ttout = false;
var arrout = false;


function ttshow(e, text)
{
	var el = document.getElementById('tooltip');
	if (el)
	{
		var x = 0, y = 0;
		if (isie)
		{
			x = event.clientX + document.body.scrollLeft
			y = event.clientY + document.body.scrollTop
		}
		else
		{
			x = e.pageX
			y = e.pageY
		}
		el.innerHTML = text;
		el.style.left = (x+12)+'px';
		el.style.top = (y+8)+'px';
		ttout = false;
		arrout = false;
		el.style.visibility = 'visible';
	}
}


function tthide(e, istt)
{
	// do nothing if tt not visible
	var el = document.getElementById('tooltip');
	if (el && el.style.visibility == 'visible')
	{
		if (istt)
			ttout = true;
		else
			arrout = true;
		if (ttout && arrout)
		{
			ttout = false;
			arrout = false;
			el.style.visibility ='hidden';
		}
	}
}



function toggleDisplay(divId) {

  var div = document.getElementById(divId);

  div.style.display = (div.style.display=="block" ? "none" : "block");

  var a = document.getElementById("a_toggle");
  a.firstChild.nodeValue = (div.style.display=="block" ? "-" : "+");
}



function untoggleDisplay(divId) {

  var div = document.getElementById(divId);
  
  div.style.display = (div.style.display=="none" ? "block" : "none");
}


function ShowSubMenu(id)
{
	var element = document.getElementById('ms_'+id);
	var msujas = document.getElementById('ms_ujas');
	var mspred = document.getElementById('ms_pred');
	var msmeta = document.getElementById('ms_meta');
	var msuser = document.getElementById('ms_user');
	msujas.className = mspred.className = msmeta.className = 'menu_sub_inv';
	if (msuser)
		msuser.className = 'menu_sub_inv';
	element.className = 'menu_sub_vis';
}


var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
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);

// Define the sfcode tags
sfcode = new Array();
sftags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[q]','[/q]','[url]','[/url]', 
	':worthy:', ':stretcher:', ':eek:',':dead:',':fasttalk:', ':blah:', ':cow:', ':shocked:', ':jesus:', 
	':noway:', ':read:', ':shit:', ':wallbash:', ':worthyi:',':evil:', ':scared:',':wtf:',':omg:','[s]','[/s]');
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 sffontstyle(sfopen, sfclose) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.post.comment.value += sfopen + sfclose;
			document.post.comment.focus();
			return;
		}
		document.selection.createRange().text = sfopen + theSelection + sfclose;
		document.post.comment.focus();
		return;
	} else {
		document.post.comment.value += sfopen + sfclose;
		document.post.comment.focus();
		return;
	}
	storeCaret(document.post.comment);
}



function sfstyle(sfnumber) {

	donotinsert = false;
	theSelection = false;
	sflast = 0;

	if (sfnumber == -1) { // Close all open tags & default button names
		while (sfcode[0]) {
			butnumber = arraypop(sfcode) - 1;
			document.post.comment.value += sftags[butnumber + 1];
			buttext = eval('document.post.addsfcode' + butnumber + '.value');
			eval('document.post.addsfcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		document.post.comment.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 = sftags[sfnumber] + theSelection + sftags[sfnumber+1];
		document.post.comment.focus();
		theSelection = '';
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < sfcode.length; i++) {
		if (sfcode[i] == sfnumber+1) {
			sflast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (sfcode[sflast]) {
				butnumber = arraypop(sfcode) - 1;
				document.post.comment.value += sftags[butnumber + 1];
				buttext = eval('document.post.addsfcode' + butnumber + '.value');
				eval('document.post.addsfcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			document.post.comment.focus();
			return;
	} else { // Open tags

		if (imageTag && (sfnumber != 14)) {		// Close image tag before adding another
			document.post.comment.value += sftags[15];
			lastValue = arraypop(sfcode) - 1;	// Remove the close image tag from the list
			document.post.addsfcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		document.post.comment.value += sftags[sfnumber];
		if ((sfnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(sfcode,sfnumber+1);
		eval('document.post.addsfcode'+sfnumber+'.value += "*"');
		document.post.comment.focus();
		return;
	}
	storeCaret(document.post.comment);
}



function insertURL()
{
	var url = prompt("Въведи URL:", "http://");
	if (url != null && (url.substring(0, 7) == "http://" || url.substring(0, 6) == "ftp://"))
	{
		var txt = prompt("Текст за връзката:", url);
		if (txt == null || txt == url)
			txt = "[url]" + url + "[/url]";
		else
			txt = "[url=" + url + "]" + txt + "[/url]";
		document.post.comment.value += txt;
	}
	document.post.comment.focus();
}


function sfimage(sfnumber) {

	arraypop(sfcode);
	document.post.comment.value += sftags[sfnumber];
	document.post.comment.focus();
	storeCaret(document.post.comment);
	return;
}
	
// 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 confirmDelete(caption)
	{
	var message = "ВНИМАНИЕ Изтривате "+caption+" !";
	var text=confirm(message);
	if (text)
		return true;
	else
		return false;
	}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0');");
}


function pop_preview(news_id, wd, hg) {
        window.open('pop_preview.php?news_id=' + news_id, '', 'width=' + wd + ',height= ' + hg + ', status=no,resizable=no, alwaysRaised=yes, scrollbars=yes');
}


	    
var dumbcat;
var ro;
var ropostid;
var rating_running = false;


function rate_post(E, postid, diff)
{
	if (rating_running)
		return;
	rating_running = true;
	
	ropostid = postid;
	
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
//		if (ro.overrideMimeType) 
//            ro.overrideMimeType('text/xml');            
    }
    ro.onreadystatechange = rate_post_response; 
    ro.open('get', 'ratingupdown.php?postid=' + postid + '&diff=' + diff);
	ro.send(null);
}

function rate_post_response()
{
	if (ro != null && ro.readyState == 4) 
	{
		document.getElementById('rate' + ropostid).innerHTML = "";		
		if (ro.status == 200) 
    	{
    		var resp = ro.responseText.split("|");
    		if (resp[0] == "0")
    			alert('Вече си гласувал тук. Не може по два пъти :Р');
    		else
    		{
				if (resp[0] == "1")
					document.getElementById('rate' + ropostid).innerHTML = 
					"<span class='newsauthor'>Tочки:&nbsp;<b>" + resp[1] + "</b></span><br/>" +
					"<img src='images/vote/pluse.gif' alt='гласувал'/>&nbsp;";
				else if (resp[0] == "-1")
					document.getElementById('rate' + ropostid).innerHTML = 
					"<span class='newsauthor'>Tочки:&nbsp;<b>" + resp[1] + "</b></span><br/>" +
					"<img src='images/vote/minuse.gif' alt='гласувал'/>&nbsp;";
				else
					alert('Проблемче с гласуването :(');
    		}
    	}
        else 
   			alert('Проблемче с гласуването :(');

        ro = null;
        ropostid = null;
	}
	rating_running = false;
}


function rate_post2(E, postid, diff)
{
	if (rating_running)
		return;
	rating_running = true;
	
	ropostid = postid;
	
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
//		if (ro.overrideMimeType) 
//            ro.overrideMimeType('text/xml');            
    }
    ro.onreadystatechange = rate_post_response2; 
    ro.open('get', 'ratingupdown.php?postid=' + postid + '&diff=' + diff);
	ro.send(null);
}

function rate_post_response2()
{
	if (ro != null && ro.readyState == 4) 
	{
		if (ro.status == 200) 
    	{
    		var resp = ro.responseText.split("|");
    		if (resp[0] == "0")
    			alert('Вече си гласувал тук. Не може по два пъти :Р');
    		else
    		{
				if (resp[0] == "1" || resp[0] == "-1")
				{
					var votedup = resp[0] == "1";
					document.getElementById('rateup' + ropostid).innerHTML = 
						"<img src='images/vote/plus"+(votedup?"e":"d")+".gif' alt='гласувал'/>";
					document.getElementById('ratedown' + ropostid).innerHTML = 
						"<img src='images/vote/minus"+(votedup?"d":"e")+".gif' alt='гласувал'/>";
					document.getElementById('ratepoints' + ropostid).innerHTML = 
						resp[1] + " точк" + (resp[1] == 1 ? "а" : "и");
				}
				else
					alert('Проблемче с гласуването');
    		}
    	}
        else 
   			alert('Проблемче с гласуването');

        ro = null;
        ropostid = null;
	}
	rating_running = false;
}


