function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// adds/removes/swaps a class name in an object's className attribute. 
// a = action, o = the object, c1 = the first class name, c2= the second class name
function removeClassName(a,o,c1,c2) {
	switch (a){
		case 'swap':
			o.className=!jscss('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
		case 'add':
			if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className)
		break;
	}
}


function togglediv(d) {
	var c = document.getElementById(d);
	if (c.style.display=='none') {
		c.style.display = 'block';
	} else {
		c.style.display = 'none';
	}
}

function focusWindow(w) {
	w.focus();
}

function invert_all_chk(form_id, field_prefix) {
	for (i=0; i<document.forms[form_id].elements.length; i++) {
		if (document.forms[form_id].elements[i].name.indexOf(field_prefix) == 0) 
			document.forms[form_id].elements[i].checked=!document.forms[form_id].elements[i].checked;
	}
}

function invert_all_chk_byid(field_prefix) {
	var inputs = document.getElementsByTagName("input");
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].id.indexOf(field_prefix) == 0)
			inputs[i].checked=!inputs[i].checked;
	}
}

// GET X COORDINATE
function getRealLeft(id) { 
	var el = document.getElementById(id);
	if (el) { 
		xPos = el.offsetLeft;
		tempEl = el.offsetParent;
		while (tempEl != null) {
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		} 
		return xPos;
	} else {
		return 0;
	}
} 

// GET Y COORDINATE
function getRealTop(id) {
	var el = document.getElementById(id);
	if (el) { 
		yPos = el.offsetTop;
		tempEl = el.offsetParent;
		while (tempEl != null) {
			yPos += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
		}
		return (yPos+1);
	} else {
		return 0;
	}
}

// get the index page Y scroll offset
function GetIndexYOffset() {
	var x,y;
	if (parent.window.pageYOffset) // all except Explorer
	{
		x = parent.window.pageXOffset;
		y = parent.window.pageYOffset;
	}
	else if (parent.window.document.documentElement && parent.window.document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = parent.window.document.documentElement.scrollLeft;
		y = parent.window.document.documentElement.scrollTop;
	}
	else if (parent.window.document.body) // all other Explorers
	{
		x = parent.window.document.body.scrollLeft;
		y = parent.window.document.body.scrollTop;
	}
	return y;
}

// get the client inner height of the browser
function GetPageClientHeight() {
	var x,y;
	if (parent.window.innerHeight) // all except Explorer
	{
		x = parent.window.innerWidth;
		y = parent.window.innerHeight;
	}
	else if (parent.window.document.documentElement && parent.window.document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = parent.window.document.documentElement.clientWidth;
		y = parent.window.document.documentElement.clientHeight;
	}
	else if (parent.window.document.body) // other Explorers
	{
		x = parent.window.document.body.clientWidth;
		y = parent.window.document.body.clientHeight;
	}
	return y;
}

// get the client inner width of the browser
function GetPageClientWidth() {
	var x,y;
	if (parent.window.innerHeight) // all except Explorer
	{
		x = parent.window.innerWidth;
		y = parent.window.innerHeight;
	}
	else if (parent.window.document.documentElement && parent.window.document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = parent.window.document.documentElement.clientWidth;
		y = parent.window.document.documentElement.clientHeight;
	}
	else if (parent.window.document.body) // other Explorers
	{
		x = parent.window.document.body.clientWidth;
		y = parent.window.document.body.clientHeight;
	}
	return x;
}

function toggledivonoff(d, state) {
	var c = document.getElementById(d);
	if (state) {
		c.style.display = 'block';
	} else {
		c.style.display = 'none';
	}
}

function showhidediv(d, state) {
	var c = document.getElementById(d);
	if (state) {
		c.style.display = 'block';
	} else {
		c.style.display = 'none';
	}
}


// -------- functions needed for table module
function change_all_chk(form_id, field_prefix, fstate) {
	for (i=0; i<document.forms[form_id].elements.length; i++) {
		if (document.forms[form_id].elements[i].name.indexOf(field_prefix) == 0) document.forms[form_id].elements[i].checked=fstate;
	}
}

function togglefilterdiv(m) {
	var imgid = 'imgFilter_'+m;
	var divid = 'divFilter_'+m;
	var divref = document.getElementById(divid);
	
	var imx = getRealLeft(imgid);
	var imy = getRealTop(imgid);
	
	divref.style.left = imx-640+'px';
	divref.style.top = imy+12+'px';

	togglediv(divid);
	return false;
}

// Adds a row to a filter table
function addRowDOMFilter(tableID) { 
// pass every cell content as a futher arg
  var table = document.getElementById(tableID);
  if (arguments.length > 1) {
	var row = table.insertRow(table.rows.length);
	if (document.getElementById) {
	  for (var i = 1; i < arguments.length; i++) {
		var cell = row.insertCell(i - 1);
		if (i == 1) cell.style.textAlign = 'right';
		
		cell.innerHTML = arguments[i];
	  }  // for
	}
  }
}

// removes a row from a table with id
function removeRowFromTable(tableID, rowNum)
{
	var tbl = document.getElementById(tableID);
	tbl.deleteRow(rowNum);   // zero based
//  var lastRow = tbl.rows.length;
}

// removes a row from a filter table
function removeRowFromFilterTable(moduleID, rowID)
{
	var tbl = document.getElementById('tableFilter_'+moduleID);
	var k = 0;
	// search for the element
	for (i=0; i<document.forms['form_filtering_'+moduleID].elements.length; i++) {
		c = document.forms['form_filtering_'+moduleID].elements[i];
		if ( c.id.indexOf('lstFilterFields_'+moduleID) != -1 ) {   // increment row number only for a row in filter table
			k++;
		}
		if ( c.id.indexOf('btnRemoveCondition_'+moduleID+'_'+rowID) != -1 ) {   // found
			tbl.deleteRow(k);
		}
	}
}
// --------- end of functions needed for table module

var divNoticeBoxesRemovable = new Array();   // holds notice boxes ids to be auto removed by tab switches

// a third argument of true indicates that no status boxes should be removed (e.g. called by page loading or so)
function tabSwitch(d,n) {
	var IDbuttons = "TabButton";
	var IDcontents = "divTabContents";
	var i;
	for (i=1; i<=n; i++) {
		showhidediv(IDcontents+i, false);
		document.getElementById(IDbuttons+i).className = '';
		document.getElementById(IDbuttons+i).blur();
	}
	if (d>0) {   // pass 0 to d for no active button
		showhidediv(IDcontents+d, true);
		document.getElementById(IDbuttons+d).className = 'act';
	}
	
	if ( !((arguments.length > 2) && (arguments[2]==true)) ) {
		for (i=0; i<divNoticeBoxesRemovable.length; i++) {
			showhidediv(divNoticeBoxesRemovable[i], false);
		}
	}
}


function showprofilewritemessage() {
	tabSwitch(0,4);
	showhidediv('divWriteMessage', true);
}
function showprofilerecommendform() {
	tabSwitch(0,4);
	showhidediv('divRecommendForm', true);
}
function tabSwitchProfile(d,n) {
	showhidediv('divWriteMessage', false);
	showhidediv('divRecommendForm', false);
	tabSwitch(d,n);
}

function add_select_option(selectid, stext, svalue) {
	var o = document.getElementById(selectid);
	var sellen = o.options.length;
	var newOption = new Option(stext, svalue);
	var saveInd = o.selectedIndex;
	o.options[sellen] = newOption;
	o.selectedIndex = saveInd;
}

function copyMultipleSelectOptions(lstSrc, lstDst) {
	var elsrc = document.getElementById(lstSrc);
	var eldst = document.getElementById(lstDst);
	
	for (i = 0; i < elsrc.length; i++) {
		if ( elsrc.options[i].selected ) {
			// check if option already added
			var exists = false;
			for (j = 0; j < eldst.length; j++) {
				if (eldst.options[j].value == elsrc.options[i].value) exists = true;
			}
			if (!exists) add_select_option(lstDst, elsrc.options[i].text, elsrc.options[i].value);
		}
	}
}

function removeMultipleSelectOptions(lst) {
	var el = document.getElementById(lst);
	for (i = el.length-1; i >= 0; i--) {
		if (el.options[i].selected) el.options[i] = null;
	}
}

function removeMultipleSelectOptionsWithCallback(lst,c) {
	var el = document.getElementById(lst);
	for (i = el.length-1; i >= 0; i--) {
		if (el.options[i].selected) {
			c(el.options[i].value);
			el.options[i] = null;
		}
	}
}

function emptySelectOptions(lst) {
	var el = document.getElementById(lst);
	for (i = el.length-1; i >= 0; i--) {
		el.options[i] = null;
	}
}

function listSelectAll(listid) {
	var el = document.getElementById(listid);
	for (i = 0; i < el.length; i++) {
		el.options[i].selected = true;
	}
}


// Profile view gallery mouse handlers
function gallerythumbOver(id) {
	document.getElementById(id).className='mouover';
	var i;
	var lastaround = arguments.length;
	if (lastaround > 3) lastaround = 3;
	for (i=1; i<lastaround; i++) {
		if (arguments[i]!='') document.getElementById(arguments[i]).className='mouoveraround';
	}
	
	if ((arguments.length > 3) && arguments[3])
		document.getElementById('tableProfileImageThumbs').style.left = (parseInt(document.getElementById('tableProfileImageThumbs').style.left)-20)+'px';
}

function gallerythumbOut(id) {
	document.getElementById(id).className='';
	var i;
	var lastaround = arguments.length;
	if (lastaround > 3) lastaround = 3;
	for (i=1; i<lastaround; i++) {
		if (arguments[i]!='') document.getElementById(arguments[i]).className='';
	}
	
	if ((arguments.length > 3) && arguments[3])
		document.getElementById('tableProfileImageThumbs').style.left = (parseInt(document.getElementById('tableProfileImageThumbs').style.left)+20)+'px';
}


function galleryshowimg(url,approved) {
	document.getElementById('imgProfileBigger').src=url;
	document.getElementById('aProfileBigger').href=url.replace(/_thumb2/,'');
	var approved_message = '';
	if (approved=='0') approved_message = 'Снимката не е одобрена от администратор';
	document.getElementById('spanApprovedNote').innerHTML = approved_message;
}

function contactsaboutchange() {
	var ind = document.getElementById('lstAbout').selectedIndex;
	document.getElementById('spanAboutOther').style.visibility = (ind==3)?'visible':'hidden';
}


/* -------------------- Textarea manipulation routines ---------------------- */
function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
	input.focus();
	input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
	var range = input.createTextRange();
	range.collapse(true);
	range.moveEnd('character', selectionEnd);
	range.moveStart('character', selectionStart);
	range.select();
  }
}

function setCaretToEnd (input) {
	setSelectionRange(input, input.value.length, input.value.length);
}

function setCaretToBegin (input) {
	setSelectionRange(input, 0, 0);
}

function setCaretToPos (input, pos) {
	setSelectionRange(input, pos, pos);
}

// Inserts a text into a textarea at the cursor, or in place of a selection
// tagSymbolsOffset - number of symbols added to selection, needed to move caret to end of selection+tags
function insertAtCursor(myField, myValue, tagSymbolsOffset) {
  //IE support
  if (document.selection) 
  {
	myField.focus();
	sel = document.selection.createRange();
	sel.text = myValue;
	sel.scrollIntoView();
	sel.select();
	sel.collapse(false);
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') 
  {
	var startPos = myField.selectionStart;
	var endPos = myField.selectionEnd;
	myField.value = myField.value.substring(0, startPos)
				  + myValue 
				  + myField.value.substring(endPos, myField.value.length);
	setCaretToPos(myField, endPos + tagSymbolsOffset);
  } 
  else 
  {
	myField.value += myValue;
	setCaretToEnd(myField);
  }
}

function getSel(myfield) {
	if (document.selection) {
		return document.selection.createRange().text; 
	}
	else if (myfield.selectionEnd) {
		startPos = 0; endPos = 0;
		startPos = myfield.selectionStart;
		endPos = myfield.selectionEnd;
		if (startPos != endPos) {
			fldtext = myfield.value;
			return fldtext.substr(startPos, endPos-startPos);
		} else {
			return '';
		}
	}
	else {
		return;
	};
}

function taReplaceSelection(f,t) {
	var s = getSel(f);
	s = t;
	insertAtCursor(f, s, s.length);
}

function InsertEmoticon(fldid,t) {
	//var fld = document.frmWritePost.posttext;
	var fld = document.getElementById(fldid);
	taReplaceSelection(fld, t);
	fld.focus();
}

/* ------------------------------------------------------------------------------------- */


var existing_polls_count = 2;   // start with that number of poll options
var add_polls_count = 0;   // gets inceremented with every new added item

function addRowDOMPoll(tableID) { 
// pass every cell content as a futher arg
  var table = document.getElementById(tableID);
  if (arguments.length > 1) {
	var row = table.insertRow(table.rows.length);
	if (document.getElementById) {
	  for (var i = 1; i < arguments.length; i++) {
		var cell = row.insertCell(i - 1);
		
/*        if (i==1) {
			cell.className = "";
		} else if (i==2) {
			cell.className = "";
		}
*/		
		cell.innerHTML = arguments[i];
	  }  // for
	}
  }
}

function AddPollOption() {
	if ((existing_polls_count + add_polls_count) >=10) {
		alert('Можете да зададете максимум 10 избора');
		return;
	}
	add_polls_count++;
	var item_num = existing_polls_count + add_polls_count;
	
	var cell1 = (item_num);
	var cell2 = '<input type="text" name="pollTitle_'+(item_num)+'" class="inpWritePollTitles" maxlength="50" />';
	
	addRowDOMPoll('tblForumPollOpt', cell1, cell2);
	if (item_num>=10) {
		togglediv('divForumAddPollOptionLbl');
	}
}



//------------------------ functions to work with forum profile tooltips
var divProfilePreviewWidth = 300;
var divProfilePreviewHeight = 90;

function showForumProfilePreview(imgtag,nick,age,cityname,isonline, event) {
	var x, y;
	
	var divprev = document.getElementById('divProfilePreview');
	var imgpage = document.getElementById(imgtag);
	var imgPreviewProfileSrc = imgpage.src; //.replace(/_thumb.jpg/gi, '.gif');
	var imgInprev = document.getElementById('imgProfilePreview');
	var divNick = document.getElementById('divProfilePreviewNickname');
	var divAge = document.getElementById('divProfilePreviewAge');
	var divTown = document.getElementById('divProfilePreviewTown');
	var divOnOff = document.getElementById('divProfilePreviewOnOff');
	
	imgInprev.src = imgPreviewProfileSrc;
	
	divNick.innerHTML = nick;
	divAge.innerHTML = age;
	divTown.innerHTML = cityname;
	divOnOff.innerHTML = (isonline=='1')?'Онлайн':'Офлайн';
	if (isonline=='1') divOnOff.className = "online";
	
//	x = getRealLeft(imgtag);
//	y = getRealTop(imgtag);
//	x += document.getElementById(imgtag).width;
//	divprev.style.left = x+'px';
//	divprev.style.top = y+'px';
	var m_x = event.clientX+14;
	var m_y = event.clientY+14;
	
	if ( (divProfilePreviewWidth+6) > (GetPageClientWidth()-m_x) ) {
		m_x -= divProfilePreviewWidth;
		m_x -= 28;
	}
	if ( (divProfilePreviewHeight+6) > (GetPageClientHeight()-m_y) ) {
		m_y = GetPageClientHeight() - divProfilePreviewHeight - 6;
	}

	divprev.style.left = m_x+'px';
	divprev.style.top = m_y+GetIndexYOffset()+'px';
	
	divprev.style.display = 'inline';
}

function hideForumProfilePreview() {
	var divprev = document.getElementById('divProfilePreview');
	var imgInprev = document.getElementById('imgProfilePreview');
	var divNick = document.getElementById('divProfilePreviewNickname');
	var divAge = document.getElementById('divProfilePreviewAge');
	var divTown = document.getElementById('divProfilePreviewTown');
	var divOnOff = document.getElementById('divProfilePreviewOnOff');
	
	imgInprev.src = 'img/transp10x10.gif';
	divNick.innerHTML = '';
	divAge.innerHTML = '';
	divTown.innerHTML = '';
	divOnOff.innerHTML = '';
	divOnOff.className = "";
	
	divprev.style.display = 'none';
	divprev.style.left = -1000;
	divprev.style.top = -1000;
}

function moveForumProfilePreview(event) {
	var divprev = document.getElementById('divProfilePreview');
	//var imgInprev = document.getElementById('imgProfilePreview');
	var m_x = event.clientX+14;
	var m_y = event.clientY+14;
	
	if ( (divProfilePreviewWidth+24) > (GetPageClientWidth()-m_x) ) {
		m_x -= divProfilePreviewWidth;
		m_x -= 28;
	}
	if ( (divProfilePreviewHeight+6) > (GetPageClientHeight()-m_y) ) {
		m_y = GetPageClientHeight() - divProfilePreviewHeight - 6;
	}

	divprev.style.left = m_x+'px';
	divprev.style.top = m_y+GetIndexYOffset()+'px';
	
	return true;
}







