
function RTrim(s) { return s.replace(/ +$/,"") }
function LTrim(s) { return s.replace(/^ +/,"") }
function Trim(s) { return RTrim(LTrim(s)) }

function AlertTabCountdown()
{
	document.getElementById('alerttab_button').value = 'Закрыть ('+cnt+')';
	t = window.setTimeout('AlertTabCountdown ();', 1000); cnt--;
	if (cnt<0) { clearTimeout(t); HideAlerttab();}
}

function ShowAlert(timeout, type, title, message)
{
	cnt = timeout;

	altab 		= document.getElementById('alerttab');
	altitle 	= document.getElementById('alert_title');
	almessage 	= document.getElementById('alert_message');
	albutton 	= document.getElementById('alert_button');

    if (type == 'error') {
        $("#alerttab").attr("class", "terr").show();
	} else if (type == 'notice') {
        $("#alerttab").attr("class", "tnot").show();
	} else {
        $("#alerttab").attr("class", "tdef").show();
	}

    altitle.innerHTML = title;
    almessage.innerHTML = message;
    $('#alerttab').bgIframe();
	AlertTabCountdown();
}

function HideAlerttab()
{
    document.getElementById('alerttab').style.display='none';
//    WCH.Discard('alerttab');
}

function ShowConfirm(title, message, yes_but_onclick, no_but_onclick)
{
    no_but_onclick += 'HideConfirmtab();'

	cnftab 		= document.getElementById('confirmtab');
	cnftitle 	= document.getElementById('confirm_title');
	cnfmessage 	= document.getElementById('confirm_message');
	cnfbutton 	= document.getElementById('confirm_button');

	cnftab.style.display = 'block';
	cnftitle.innerHTML = title;
	cnfmessage.innerHTML = message;
	cnfbutton.innerHTML = '<input type="button" id="confirm_yes" class="but" value="Да" onclick="'+yes_but_onclick+'"><input type="button" id="confirm_no" class="but" value="Нет" onclick="'+no_but_onclick+'">';
    $('#confirmtab').bgIframe();
}

function HideConfirmtab()
{
    document.getElementById('confirmtab').style.display='none';
//    WCH.Discard('confirmtab');
}

function show_calendar(source, e)
{
    eval('cur = '+source+';');

	xajax_doLoadCalendar(source, cur);

	mousex = e.clientX;
	mousey = e.clientY;

	obj 	= document.getElementById('worktab');
	alcnt 	= document.getElementById('body');

	obj.innerHTML = '';

	obj.style.display = 'block';

	obj.style.width = '200px';
	obj.style.height = '200px';

    if ((alcnt.clientHeight-mousey) < 220) {
        obj.style.top = mousey - 200 + 'px';
   	} else {
        obj.style.top = mousey + 10 + 'px';
    }

    if ((alcnt.clientWidth-mousex) < 220) {
	    obj.style.left = mousex - 200 - 10 + 'px';
    } else {
        obj.style.left = mousex + 10 + 'px';
   	}

//    WCH.Apply('worktab');
    $('#worktab').bgIframe();
}

function hide_calendar()
{
    HideWorktab();
}

function clear_calendar(form_name)
{
    HideWorktab();
	document.getElementById(form_name).start_date.value = '00.00.0000';
	document.getElementById(form_name).stop_date.value = '00.00.0000';
}

function show_actions(type, params)
{
    xajax_doShowEnterReasonTab(type, params);

	obj = document.getElementById('worktab');

	obj.innerHTML = '';

	obj.style.display = 'block';
	obj.style.width = '300px';
	obj.style.height = '80px';

//    WCH.Apply('worktab');
    $('#worktab').bgIframe();
}

function show_action(width, height)
{
	alcnt 	= document.getElementById('body');
	obj     = document.getElementById('worktab');

	obj.style.display = 'block';
	obj.style.width   = width + 'px';
	obj.style.height  = height + 'px';

	obj.style.left = ((alcnt.clientWidth/2)-(width/2)) + 'px';
	obj.style.top = ((alcnt.clientHeight/2)-(height/2)) + 'px';

//    WCH.Apply('worktab');
    $('#worktab').bgIframe();
}

function HideWorktab()
{
    document.getElementById('worktab').style.display='none';
//    WCH.Discard('worktab');
    $('#worktab').bgIframe();
}

function CategoryMakeChanges(type, formid, catlist, input, catid, title, id)
{
	if (type == 'd') {
		document.getElementById(input).style.display='none';
		document.getElementById(catlist).style.display='block';
	}
	if (type == 's') {
		document.getElementById(catid).value = id;
		document.getElementById(input).innerHTML = '<a href="#" class="lnk2" onclick="CategoryMakeChanges(\'d\', \''+formid+'\', \''+catlist+'\', \''+input+'\', \''+catid+'\', \'0\', \'0\')" alt="Нажмите, чтобы выбрать или сменить категорию" title="Нажмите, чтобы выбрать или сменить категорию">'+title+'</a>';
		document.getElementById(input).style.display='block';
		document.getElementById(catlist).style.display='none';
	}
	return false;
}

function getObject(sId)
{
    if (!sId) return null;
	var returnObj = document.getElementById(sId);
	if (!returnObj && document.all) {
		returnObj = document.all[sId];
	}
	return returnObj;
}

function showProperties (obj)
{
    var result = "The preperties:\n";
	for (var i in obj) {
//		result += i + "=" + obj.i + ", ";
		result += i + ", ";
	}
	return result;
}

isDOM=document.getElementById //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
isNetscape4=document.layers //Netscape 4.*
isOpera=window.opera //Opera
isOpera5=isOpera && isDOM //Opera 5+
isOpera6 = isOpera && window.print;
isOpera7 = isOpera && document.readyState;
isMSIE = document.all && document.all.item && !isOpera;
isMSIE5 = isDOM && isMSIE;
isMozilla=isNetscape6=isDOM && !isMSIE && !isOpera

var obj;

function getLayer(layerName, parentLayerName)
{
  if(isDOM){ return document.getElementById(layerName); }
  if(isMSIE){ return document.all[layerName]; }
  if(isNetscape4){ return eval('document.layers[layerName]'); }
  return false;
}

function ts(e, what, right, sdvig)
{
	mousex = e.clientX;
	mousey = e.clientY;

	pagexoff = 0;
	pageyoff = 0;

	if(isMSIE5){
		pagexoff = document.body.scrollLeft;
		pageyoff = document.body.scrollTop;
	} else {
		pagexoff = window.pageXOffset;
		pageyoff = window.pageYOffset;
	}

	if (getLayer(what)) {

		if(isNetscape4) {
			obj = getLayer(what);
		} else {
			obj = getLayer(what).style;
		}

		if(obj){
		    if (right) {
				rightoff = 200;
				obj.right = 200;
			} else {
				leftoff = mousex-pagexoff;
				obj.left = (mousex + pagexoff + 10);
			}

			topoff = mousey-pageyoff;

			if(isOpera && topoff <= 30) {
				obj.top = (mousey + pageyoff) + sdvig;
			} else {
				if( mousey <= 30) {
					obj.top = (mousey + pageyoff) + sdvig;
				} else {
					if (isOpera) {
						obj.top = (mousey + pageyoff) + sdvig;
					} else {
						obj.top = (mousey + pageyoff) + sdvig;
					}
				}
			}

			if(isNetscape4) {
				obj.visibility = 'show';
			} else {
				obj.visibility = 'visible';
			}
		}
	}
  return true;
}

function tc()
{
	if (obj) {
		if (isNetscape4) {
			obj.visibility = 'hide';
		} else {
			obj.visibility='hidden';
		}
	}
    return true
}

function nwindow(url, wd, hd, left, top, scroll, wname)
{
    try {
        if (window.ddd) { ddd.close(); }
  	} catch (err) {
  	}
    alcnt = document.getElementById('body');
	ddd=window.open(url,wname,'width='+wd+',height='+hd+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scroll+',resizable=yes,left='+((alcnt.clientWidth-wd)/2)+',top='+((alcnt.clientHeight-hd)/2));
}

var offsetfrommouse=[15,25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.

var defaultimageheight = 40;	// maximum image size.
var defaultimagewidth = 40;	// maximum image size.

var timer;

function gettrailobj()
{
	if (document.getElementById)
	return document.getElementById("preview_div").style
}

function gettrailobjnostyle()
{
	if (document.getElementById)
	return document.getElementById("preview_div")
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail()
{
	gettrailobj().display= "none";
	document.onmousemove=""
	gettrailobj().left="-1000px"
	clearTimeout(timer);
}

function showtrail(imagename,title,width,height)
{
	i = imagename
	t = title
	w = width
	h = height
	timer = setTimeout("show('"+i+"',t,w,h);",200);
}

function show(imagename,title,width,height)
{
    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if( (navigator.userAgent.indexOf("Konqueror")==-1  || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) && (docwidth>650 && docheight>500)) {
		( width == 0 ) ? width = defaultimagewidth: '';
		( height == 0 ) ? height = defaultimageheight: '';

		width+=30
		height+=25

		defaultimageheight = height
		defaultimagewidth = width

		document.onmousemove=followmouse;
		newHTML = '';

		newHTML = newHTML + '<div class="border_preview" style="width:'+ width +'px;height:'+ height +'px">';
    	newHTML = newHTML + '<div class="preview_temp_load"><img src="' + imagename + '" border="0"></div>';
		newHTML = newHTML + '</div>';

		if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
			newHTML = newHTML+'<iframe src="about:blank" scrolling="no" frameborder="0" width="'+width+'" height="'+height+'"></iframe>';
		}

		gettrailobjnostyle().innerHTML = newHTML;
		gettrailobj().display="block";
	}
}

function followmouse(e)
{
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = e.pageX - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < defaultimageheight + 2*offsetfrommouse[1]){
			ycoord += e.pageY - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (defaultimageheight + 2*offsetfrommouse[1])){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}

function addtobasket(id, type, back, path) {
	document.location.href = path + '/addtobasket.php?id='+id+'&tp='+type+'&bback='+back;
}

function addtofavor(id, type, back, path) {
	document.location.href = path + '/addtofavor.php?id='+id+'&tp='+type+'&bback='+back;
}

function openWin(sURL,sWN,L,T,W,H) {
	nwin=window.open(sURL,sWN,'resizable=1,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+W+',height='+H+',top='+T+',left='+L);
	nwin.focus();
}

function getClientCenterX() {
    return parseInt(getClientWidth()/2)+getBodyScrollLeft();
}

function getClientCenterY() {
    return parseInt(getClientHeight()/2)+getBodyScrollTop();
}

function PutCenter(idobj)
{
    var scrollX, scrollY;
    if (document.all) {
        if (!document.documentElement.scrollLeft) scrollX = document.body.scrollLeft;
        else scrollX = document.documentElement.scrollLeft;
        if (!document.documentElement.scrollTop) scrollY = document.body.scrollTop;
        else scrollY = document.documentElement.scrollTop;
    } else {
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    }
    var myWidth = 0, myHeight = 0;
    if (typeof( window.innerWidth ) == 'number') {
        //Non-IE
        myWidth = window.innerWidth+scrollX;
        myHeight = window.innerHeight+scrollY;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth+scrollX ;
        myHeight = document.documentElement.clientHeight+scrollY;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
        //IE 4 compatible
        myWidth = document.body.clientWidth+scrollX;
        myHeight = document.body.clientHeight+scrollY;
    }
    if (document.getElementById) {
        $(idobj).style.top=(myHeight/2 - 80)+ 'px';
        $(idobj).style.left=(myWidth/2 - 70)+ 'px';
    }
}

function explode(delimiter, string)     // Split a string by string
{
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    var emptyArray = { 0: '' };
    if (arguments.length != 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') return null;
    if (delimiter === '' || delimiter === false || delimiter === null) return false;
    if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object' ) return emptyArray;
    if (delimiter === true) delimiter = '1';
    return string.toString().split(delimiter.toString());
}

function closenyro() {
    jQuery(document).ready(function($) { $.nyroModalRemove(); });
}

function getAbsoluteDivs()
{
    var arr = new Array();
    var all_divs = document.body.getElementsByTagName("DIV");
    var j = 0;
    for (i = 0; i < all_divs.length; i++) {
//        if (all_divs.item(i).style.position=='absolute') {
            arr[j] = all_divs.item(i);
            j++;
//        }
    }
    return arr;
}

function bringToFront(id)
{
    if (!document.getElementById || !document.getElementsByTagName) return;
    var obj = document.getElementById(id);
    var divs = getAbsoluteDivs();
    var max_index = 0;
    var cur_index;
    // Compute the maximal z-index of other absolute-positioned divs
    for (i = 0; i < divs.length; i++) {
        var item = divs[i];
        if (item == obj || item.style.zIndex == '') continue;
        cur_index = parseInt(item.style.zIndex);
        if (max_index < cur_index) { max_index = cur_index; }
    }
    obj.style.zIndex = max_index + 1;
}

function ordonoff(oid) {
    eval("var obj=document.getElementById('ord"+oid+"');");
    eval("var img=document.getElementById('img"+oid+"');");
    if (obj) {
        osd = obj.style;
        if (osd.display != 'none') {
            osd.display = 'none';
            if (img) img.src = $url_tpl_img+'pl.gif';
        } else {
            osd.display = 'block';
            if (img) img.src = $url_tpl_img+'mi.gif';
        }
    }
}

function preloadImg(image) {
    var img = new Image();
    img.src = image;
}

function collapseCheck(id, def)
{
    var box=$('#'+id);
    if (box) {
        var c=$('#'+id+" .cfb");
        if (c) {
            var k=$.cookie('remix'+id);
            if (k!='1') {
                if (k==null){
                    if (def!=1) {
                        $(c[0]).slideUp();
                        $(box).find('.flexHeader').parent().toggleClass('bOpen').toggleClass('bShut');
                    }
                } else {
                    $(c[0]).slideUp();
                    $(box).find('.flexHeader').parent().toggleClass('bOpen').toggleClass('bShut');
                }
            }
        }
    }
}

function collapseDo(id, direct)
{
    var box=$('#'+id);
    if (box) {
        var c=$('#'+id+" .cfb");
        if (c) {
            if (direct == 1) {
                $(c[0]).slideDown();
                $(box).find('.flexHeader').parent().toggleClass('bShut').toggleClass('bOpen');
            } else {
                $(c[0]).slideUp();
                $(box).find('.flexHeader').parent().toggleClass('bOpen').toggleClass('bShut');
            }
        }
    }
}

function collapseChecks()
{
    collapseCheck('quick_go', 1);
    collapseCheck('find_on_site');
    collapseCheck('vitrina', 1);
    collapseCheck('make_zakaz');
    collapseCheck('news', 1);
    collapseCheck('help', 1);
    collapseCheck('cart', 1);
    collapseCheck('rating');
    collapseCheck('auditoria');
    collapseCheck('target', 1);
    collapseCheck('territoria');
    collapseCheck('user_kabx',1);
}

function collapseBox(id,_44, dopen, dclose)
{
    var box=$('#'+id);
    if(!box) return;
    var c=$('#'+id+" .cfb");
    if(c.length>0&&!c[0].blinding){
        c[0].blinding=true;
        if(c[0].style.display=="none"){
            $(c[0]).slideDown('fast',function(){c[0].blinding=false;});
            _44.parentNode.className="bOpen";
            $.cookie('remix'+id, '1');
        }else{
            $(c[0]).slideUp('slow',function(){c[0].blinding=false;});
            _44.parentNode.className="bShut";
            $.cookie('remix'+id, '0');
        }
    }
    return false;
}

// Some Class
function MyClass() {
	this.UpdateEditorFormValue = function()	{
		for ( i = 0; i < parent.frames.length; ++i ) {
			if ( parent.frames[i].FCK ) {
				parent.frames[i].FCK.UpdateLinkedField();
			}
		}
	}
}

// instantiate the class
var MyObject = new MyClass();

function UpdateProtectImage()
{
    $("#zaglushka").animate({ opacity: 0.2 }, 'fast');
    xajax_UpdateProtectImage();
}

function UpdateProtectImageAfter(tim)
{
    $('#zaglushka').attr('src', $url+"/protect.php?time="+tim).animate({ opacity: 1 }, 'slow');
}

function setcs(url)
{
    var x1 = document.getElementsByTagName("link");
    var ii = x1.length;
    for (var i=0; i<ii; i++) {
        var oo = x1[i];
        if(oo.id == "cssu") oo.href = url;
    }
}


function email (login, sc){emylo = login +  "[зобак]" + sc;return emylo;}
function mylo (login, sc){emylo = login +  "[зобак]" + sc;document.write (emylo);}
function namylo (login, sc){emylo = "mailto:" + email(login, sc);window.location.href = emylo;}

function change_status(index) {	check = document.getElementById('access_active_check_'+index).checked;
	if (check) {		document.getElementById('access_'+index+'_0').disabled = false;
		document.getElementById('access_'+index+'_1').disabled = false;
		document.getElementById('access_'+index+'_999').disabled = false;
	} else {		document.getElementById('access_'+index+'_0').disabled = true;
		document.getElementById('access_'+index+'_1').disabled = true;
		document.getElementById('access_'+index+'_999').disabled = true;
	}
}