function iBabyIgotMoney(x)
{
	if(x == 'f')
	{
		document.getElementById('ifix').style.display = '';
		document.getElementById('ikey').style.display = 'none';
	}
	if(x == 'k')
	{
		document.getElementById('ifix').style.display = 'none';
		document.getElementById('ikey').style.display = '';
	}
}


function popWin1(page, wi, he, wi2, he2) {

	w = screen.width;
	h = screen.height;
	sx = w/2-wi2;
	sy = h/2-he2;
	win = window.open(page,'pic_win','status=no,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,menubar=no,width='+wi+',height='+he+',top='+sy+',left='+sx+',screenX='+sx+',screenY='+sy+'');
	win.resizeTo(wi+7, he+50);
	win.moveTo(sx, sy);
	win.focus();
}



function show_hide(divName) {
	if (document.getElementById) {
		obj=document.getElementById(divName)
	} else if (document.all) {
		obj=document.all[divName]
	}

	if (obj) {
		if (obj.style.display == "none") {
			obj.style.display = '';
		}
		else {
			obj.style.display = 'none';
		}
	}
}

function favorit() {
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {

	url="http://comfort.bg";
	title="comfort.bg";

	document.write('<a href="javascript:window.external.AddFavorite(url,title)"><img src="/webi/ico4.gif" border="0" alt="" align="middle"/></a>');
	} else {
		var msg = '<img src="/webi/ico4.gif" border="0" style="vertical-align:middle;padding-bottom:1px;" alt="Добавете в любими сайтове" title="Добавете в любими сайтове (CTRL-D)"/>';
		document.write(msg);
	}
}

function validateForm(form_id) {
	var frm = document.getElementById(form_id);
	switch (form_id) {
		case 'newsaddform':
			var fields = ['id_1', 'id_2', 'id_3', 'id_4', 'id_5', 'id_6'];
			break;
		case 'form2': //news, fin news
			var fields = ['news_title'];
			break;
		case 'form3': //dmc
			var fields = ['dmc_title'];
			break;
		case 'form4': //press release
			var fields = ['release_title'];
			break;
		case 'form5': //newsletter
			var fields = ['item_title'];
			break;
		case 'form6': //add research reports
			var fields = ['report_file', 'report_demo'];
			break;
		case 'form7': // add financial reports
			var fields = ['report_file', 'fin_report_year'];
			break;
		case 'form10': // edit financial reports
			var fields = ['fin_report_year'];
			break;
		case 'form8': // companies
			var fields = ['co_name', 'co_code'];
			break;
		case 'form9': // companies
			var fields = ['old_password', 'password', 'password_2'];
			break;
		default:
			break;
	}

//	for(var j = 0; j < document.forms[0].length; j++) {
		document.write(document.forms[0].langs[0].value.length);
//	}
	return false;
	for(var i=0; i<fields.length; i++)
	{
		if (frm[fields[i]].value.length < 1)
		{
			alert ('Please, fill all Required fields.');

			frm[fields[i]].focus();
			return false;
		}
	}

	return true;
}


function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }

  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();

  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }

  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  var toh = document.getElementById('sydyrjanie');

  if (!toh || typeof(toh.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }

  var bodyText = toh.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }

  toh.innerHTML = bodyText;
  return true;
}


var checkflag = "false";
function ch_all(field, f) {
	if (checkflag == "false") {

		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
		}
		document.f.all_mid.checked = true;
		document.f.mid.checked = true;
		checkflag = "true";
	} else {

		for (i = 0; i < field.length; i++) {
			field[i].checked = false;
		}
		document.f.all_mid.checked = false;
		document.f.mid.checked = false;
		checkflag = "false";
	}
}

function check(v){
	for (i in document.forms.f)
		if (i.substr(0, 3)=='fid')
			document.forms.f[i].checked=v;
}

function sendform()
{
    document.history_form.submit();
}


function checkAll() {
	for (var i=0;i<document.view.elements.length;i++) {
		if (document.view.elements[i].name.substring(0,5) == "print") {
		   document.view.elements[i].checked = document.view.all.checked;
		}
	}
}

function confirmDelete()
{
  return confirm('Натиснете ОК за потвърждаване.');
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function toggleEnq(){
  var el = getElementsByClassName('approve_unapprove', 'div');
  for(i = 0, j = el.length; i<j; i++){
    var obj = el[i];
      if(obj.style.display=='none'){
        obj.style.display = '';
      }else{
        obj.style.display = 'none';
      }
  }
}

function previewAlbum(base){
  var paper_link = document.getElementById('preview_link');
  var preview_paper = document.getElementById('preview_paper');
  var title = document.getElementById('title_bg').value;
  var image = document.getElementById('image').value;
  
  
  paper_link.innerHTML = title;
  preview_paper.style.backgroundImage = 'url('+base+image+')';
  
}

function pitai(question, url){
  var pitane = confirm(question);
  
  if(pitane){
    window.location = url;
  }
}

function previewInput(object){
  if(object.value!=''){
    document.getElementById('text_'+object.id).innerHTML = object.value;
  }
}

function gallerySlide(key){
  for (i=0;i<=3;i++)
  {
    document.getElementById('galleryPicture'+i).style.display = 'none';
    document.getElementById('galleryLink'+i).className = '';
  }
  document.getElementById('galleryPicture'+key).style.display = 'block';
  document.getElementById('galleryLink'+key).className = 'active';
  text = document.getElementById('galleryPictureText'+key).innerHTML;
  document.getElementById('gallerySlideDescription').innerHTML = text;
}

function getPosition() {
  return window.event.pageX;
}

function vncRefresh(){
  jQuery(document).ready(function() {
  var tempCursorX = cursorX-documentX;
  var tempCursorY = cursorY;
  //alert('y:'+tempCursorY);
  return xajax_vnchost(tempCursorX, tempCursorY, document.location.href);
  });
}

function vncGuestRefresh(pin){
  jQuery(document).ready(function() {
  //  $('#cursorDiv').animate({top: "+=20px"});
  });
  return xajax_vncguest(pin, document.location.href);
}

var cursorX;
var cursorY;
var documentX;
var documentY;
var nulaX;
var nulaY;
var lastSync = 0;

function makeCursorMovement(y, x, lsync){
  if(lsync < lastSync){  }
  lastSync = lsync;
  jQuery(document).ready(function() {
    //$('#cursorDiv').toggle('slow');
    //alert('dx:'+documentX);
    //alert('dy:'+documentY);
    x = parseInt(x);
    documentX = parseInt(documentX);
    var tempx = (documentX+x)-10;
    var tempy = parseInt(y);
    var windowHeight = $(window).height();
    var scrollTop = $('html,body').scrollTop();
    var windowScroll = scrollTop+windowHeight;
    
    if((tempy+100) > windowScroll){
      scrollTop = tempy-(windowHeight/2);
      $('html,body').animate({scrollTop: scrollTop}, 1000);
    }else{
      if((tempy-100) < scrollTop){
        scrollTop = tempy-(windowHeight/2);
        $('html,body').animate({scrollTop: scrollTop}, 1000);      
      }
    }

      $("#cursorDiv").animate({top: tempy+"px", left:tempx+"px"}, 1000);
    //document.getElementById('cursorDiv').style.top =  x+'px';
    //document.getElementById('cursorDiv').style.left = y+'px';
  });
}

function prepareXY(){
  var xy = $('table.head').offset();
  documentX = xy.left;
}

function showOtherCities(object){
  jQuery(document).ready(function() {  
    $('ul#listCities li a.active').removeClass('active');
    object.className = 'active';
    $('#categoryTitle').html($('#otherCitiesList').html());
  });
}


