function menu_set(cfg, what, selected_menu)
{
	var elX = findPosX(what);
	var elY = findPosY(what);
	
	if(cfg == 0 && selected_menu != what.id)
	{
		what.style.backgroundColor = '';
		what.style.backgroundImage = '';
		
		if(document.getElementById('background_left_' + what.id) && document.getElementById('background_right_' + what.id))
		{
			document.body.removeChild(document.getElementById('background_left_' + what.id));
			document.body.removeChild(document.getElementById('background_right_' + what.id));
		}
	}
	else if(cfg == 1 || selected_menu == what.id)
	{
		pl = document.createElement('div');
		pl.id = 'background_left_' + what.id;
		pl.style.position = 'absolute';
		pl.style.top = elY + 'px';
		pl.style.left = elX + 'px';
		pl.style.padding = '0px';
		pl.innerHTML = '<img src="' + img_path + 'wxt_menu_left_corner.gif" alt=""/>';
		document.body.insertBefore(pl, document.body.firstChild);
		
		pr = document.createElement('div');
		pr.id = 'background_right_' + what.id;
		pr.style.position = 'absolute';
		pr.style.top = elY + 'px';
		pr.style.left = elX + parseInt(what.style.width) - 5 + 'px';
		pr.style.padding = '0px';
		pr.innerHTML = '<img src="' + img_path + 'wxt_menu_right_corner.gif" alt=""/>';
		document.body.insertBefore(pr, document.body.firstChild);
		
		what.style.backgroundColor = '#747474';
	}
}

function setSearch(cfg, what)
{
	if(cfg == 0)
	{
		if(trim(what.value) == '') what.value = 'търсене за ...';
	}
	else if(cfg == 1)
	{
		if(trim(what.value) == 'търсене за ...') what.value = '';
	}
}

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  
  //opera fix
  var agt=navigator.userAgent.toLowerCase();
  
  var is_opera = (agt.indexOf("opera") != -1);
  
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  
  var agt=navigator.userAgent.toLowerCase();
  
  var is_opera = (agt.indexOf("opera") != -1);
  var is_IE = (agt.indexOf("msie") != -1);

	if(is_opera){curtop += 0;}
	//if(is_IE){curtop += -1;}
	
  return curtop;
}

function hideValue(what, def)
{
	if(trim(what.value) == def)
	{
		what.value = "";
	}
	return;
}

function showValue(what, def)
{
	if(trim(what.value) == '')
	{
		what.value = def;
	}
	return;
}

// Removes leading whitespaces
function LTrim( value )
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value )
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value )
{
	return LTrim(RTrim(value));
	
}

function loadImages(path, pics)
{
	pics_arr = pics.split(',');
	for(i=0; i < pics_arr.length; i++)
	{
		eval('c' + i + ' = new Image();');
		eval('c' + i + '.src = path + "' + pics_arr[i] + '";');
	}
}

function changeBgck(what, img)
{
	document.getElementById(what).style.backgroundImage = "url('"+img+"')";
	//document.getElementById(what).style.backgroundPosition = 'middle';
}

function preloadImages(img_array)
{
	pics_array = img_array.split(',');
	for(i = 0; i < pics_array.length; i++)
	{
		eval("c"+i+" = new Image; c"+i+".src = pics_array[i];");
	}
}

var timer_id;
var direction=0;
var curr = 0;
function scroll_iframe(frm,inc,dir)
{
  if (timer_id) clearTimeout(timer_id);
  if (window.frames[frm])
  {
    if (direction == 0)
    {
    	curr += inc;
    	window.frames[frm].scrollBy(0, inc);
    	if(curr >= window.frames[frm].document.body.offsetHeight)
    	{
    		direction = 1;
    		curr = window.frames[frm].document.body.offsetHeight;
    	}
    }
    else if (direction == 1)
    {
    	curr -= inc;
    	window.frames[frm].scrollBy(0, -inc);
    	if(curr <= 0)
    	{
    		direction = 0;
    		curr = 0;
    	}
    }
    //else window.frames[frm].scrollBy(inc, 0);
    timer_id = setTimeout("scroll_iframe('" + frm + "'," + inc + ",'" + dir + "')", 5);
  }
}

function stopScroll()
{
	if (timer_id) clearTimeout(timer_id);
}

function change_class(tid, cfg)
{
	if(cfg == 1) newclass = 'admin_button';
	else if(cfg == 2) newclass = 'admin_button_over';
	
	//var dtarget = document.getElementById(tid);
	
	tid.className = newclass;
}

Array.prototype.in_array = function ( obj )
{
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ )
	{
		if ( this[x] == obj ) return true;
	}
	return false;
}

function closeBox(eval_script)
{
	if(document.getElementById('showMessage')) 
	{
		document.body.removeChild(document.getElementById('showMessage'));
		document.body.removeChild(document.getElementById('showMessageBox'));
		
		eval(eval_script);
	}
}

function showLightboxAlert(msg, msg_title, eval_script)
{
	var sizes = getPageSize();
	var scrollWidth = getScrollX();
	var scrollHeight = getScrollY();
	var windowWidth = winWidth();
	var windowHeight = winHeight();
	var bodyHeight = getPageSize();
	
	//main div
	dl = document.createElement('div');
	dl.id = 'showMessage';
	dl.style.position = 'absolute';
	dl.style.width = '100%';
	dl.style.height = bodyHeight[1] + 'px';
	dl.style.backgroundColor = '#000000';
	dl.style.top = 0+'px';
	dl.style.left = scrollWidth+'px';
	dl.style.textAlign = 'center';
	dl.style.filter='alpha(opacity=70)';
	dl.style.opacity='.70';
	dl.style.zIndex = 1000;
	
	//msg div
	pl = document.createElement('div');
	pl.id = 'showMessageBox';
	pl.style.width = '300px';
	pl.style.position = 'absolute';
	pl.style.backgroundColor = '#FFFFFF';
	pl.style.top = ((windowHeight/2)+scrollHeight)-100+'px';
	pl.style.left = ((windowWidth/2)+scrollWidth)-200+'px';
	pl.style.padding = '0px 0px 10px 0px';
	pl.style.textAlign = 'left';
	pl.style.border = '#000000 1px solid';
	pl.style.zIndex = 1001;
	
	//close div
	cl = document.createElement('div');
	cl.id = 'showMessageBoxClose';
	cl.style.backgroundColor = '#FFFFFF';
	cl.style.textAlign = 'left';
	cl.style.padding = '10px 5px 0px 10px';
	cl.innerHTML = '<input type="button" onclick="javascript:closeBox(\'' + eval_script + '\');" class="input-btn" value="Затвори"/>';
	
	pl.innerHTML = '<div style="float:left;width:290px;background-color:#dbdbdb;text-align:left;height:20px;padding-left:10px;padding-top:5px;" class="bgck_lightbox"><b>' + msg_title + '</b></div>';
	pl.innerHTML += '<div style="float:left;width:280px;padding:10px 10px 10px 10px;">' + msg + '</div>';
	pl.appendChild(cl);
	
	document.body.insertBefore(dl, document.body.firstChild);
	document.body.insertBefore(pl, document.body.firstChild);
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowW, windowH;
	if (self.innerHeight) {	// all except Explorer
		windowW = self.innerWidth;
		windowH = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowW = document.documentElement.clientWidth;
		windowH = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowW = document.body.clientWidth;
		windowH = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowH){
		pageHeight = windowH;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowW){	
		pageWidth = windowW;
	} else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowW,windowH) 
	return arrayPageSize;
}

function winWidth()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    myWidth = window.innerWidth;
  }
  else if( document.documentElement && document.documentElement.clientWidth )
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth )
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function winHeight()
{
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' )
  {
    //Non-IE
    myHeight = window.innerHeight;
  }
  else if( document.documentElement && document.documentElement.clientHeight )
  {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  }
  else if( document.body && document.body.clientHeight )
  {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getScrollY()
{
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' )
 	{
    //Netscape compliant
    scrOfY = window.pageYOffset;
  }
  else if( document.body && document.body.scrollTop )
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  }
  else if( document.documentElement && document.documentElement.scrollTop )
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function getScrollY()
{
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' )
 	{
    //Netscape compliant
    scrOfY = window.pageYOffset;
  }
  else if( document.body && document.body.scrollTop )
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  }
  else if( document.documentElement && document.documentElement.scrollTop )
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function getScrollX()
{
  var scrOfX = 0;
  if( typeof( window.pageXOffset ) == 'number' )
 	{
    //Netscape compliant
    scrOfX = window.pageXOffset;
  }
  else if( document.body && document.body.scrollLeft )
  {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  }
  else if( document.documentElement && document.documentElement.scrollLeft )
  {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function checkContactsForm()
{
	var reg = document.contacts_form;
	var fields_array = Array('name','email','subject','txt');
	var messages_array = Array('Моля, въведете Вашето име!','Моля, въведете Вашия e-mail адрес!','Моля, напишете в какъв аспект е Вашето запитване!','Моля, въведете Вашия текст!');
	
	for(i = 0; i < fields_array.length; i++)
	{
		eval("var curr_field = reg." + fields_array[i] + ";");
		
		if(trim(curr_field.value) == '')
		{
			curr_field.focus();
			showLightboxAlert(messages_array[i], 'Грешка', 'document.contacts_form.' + fields_array[i] + '.focus();');
			return false;
		}
		else if(fields_array[i] == 'email')
		{
			var regex = new RegExp(/^(.+){3,}\@(.+){3,}\.([a-z]+){2,}$/);
			var match = regex.exec(curr_field.value);
			if (match == null)
			{
				curr_field.focus();
				showLightboxAlert('Въведете валиден e-mail адрес!', 'Грешка', 'document.contacts_form.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	reg.submit();
	return true;
}