function hideBlock_x(id, noCookie)
{
    var blockName = 'left_menu_' + id;
    if(!noCookie){
        setCookie('settings[site][' + blockName + ']', 'toggle');
    }

    var show = getCookie('settings[site]['+ blockName +']');

    var objBlc = $('#' + blockName);
    var objImg = document.getElementsByName('hider_'+id);

    if(show == 1)
    {
        objBlc.animate({width: "show"}, 1);
        objImg.className = 'imHide_x';
    }
    else
    {
        objBlc.animate({width: "hide"}, 1);
        objImg.className = 'imShow_x';
    }
    return true;
}

function hideBlock_y(id, noCookie)
{
    var blockName = 'table_menu_block_' + id;
    if(!noCookie){
        setCookie('settings[site][' + blockName + ']', 'toggle');
    }

    var show = getCookie('settings[site]['+ blockName +']');

    var objBlc = $('#' + blockName);
    var objImg = document.getElementsByName('hider_'+id);

    if(show == 0)
    {
        objBlc.animate({height: "hide"}, 1);
        objImg[0].className = 'imShow_y';
    }
    else
    {
        objBlc.animate({height: "show"}, 1);
        objImg[0].className = 'imHide_y';
    }
}
/*
function addBasket(uId, pId, tId)
{
//  $('#basketCount').load('/user/basket.php?do=add&uid='+ uId +'&c='+ pId + '&i='+ tId);
    $.get('/user/basket.php?do=add&uid='+ uId +'&c='+ pId + '&i='+ tId);
    basketCount();
    //$('#basketCount').load('/user/basket_count.php');
}

function basketCount()
{
    $('#basketCount').load('/user/basket_count.php');
}
*/
//function check_all(is_checked, id)
/*
function check_all(is_checked, chk)
{
    if(chk) { var coll = document.getElementsByName(chk+'[]');  }
    else    { var coll = document.getElementsByName('check[]'); }
    if (coll!=null){
        for (i=0; i<coll.length; i++){
            coll.item(i).checked = is_checked;
        }
    }
}
*/

function check_all(is_checked, chk)
{
    if(chk) { var coll = document.getElementsByName(chk+'[]');  }
    else    { var coll = document.getElementsByName('check[]'); }
    if (coll!=null){
        for (i=0; i<coll.length; i++)
		{
            coll.item(i).checked = is_checked;
			if (coll.item(i).onchange) {
				var d = coll.item(i).onchange;
				d(coll.item(i));
			}
        }
    }
}

//------------------------------------
function showHideLaer(id, speed, type, callback)
{
    if(!type)     type = 'toggle';
    if(speed > 0) speed = speed;
    else          speed = 500;

    var oMenu = $(id);

    switch (type)
    {
        case 'show':
            oMenu.show(speed, callback);
        break;

        case 'hide':
            oMenu.hide(speed, callback);
        break;

        case 'toggle':
            oMenu.toggle(speed, callback);
        break;

        case 'slideDown':
            oMenu.slideDown(speed, callback);
        break;

        case 'slideUp':
            oMenu.slideUp(speed, callback);
        break;

        case 'slideToggle':
            oMenu.slideToggle(speed, callback);
        break;

        case 'fadeIn':
            oMenu.fadeIn(speed, callback);
        break;

        case 'fadeOut':
            oMenu.fadeOut(speed, callback);
        break;

        case 'fadeTo':
            oMenu.slideTo(speed, callback);;
        break;

        case 'haccordion':
            oMenu.animate({width: 'toggle'}, speed, callback);
        break;

        case 'accordion':
            oMenu.animate({height: 'toggle'}, speed, callback);
        break;

        default:
        break;
    }
}

//------------------------------------
function windOpn(url, w, h)
{
    if (!w) w = 500;
    if (!h) h = 550;

    window.open(url
                ,'_blank'
                ,'menubar=no,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h
            );
}

function splitArr(arr, sp)
{
    if(!sp) sp = '&';
    var urlVars='';

    if(!arr) return;
    if(arr.length > 0)
    {
        for(i=0; i < arr.length; i++)
        {
            urlVars += sp + arr[i];
        }
    }
    return urlVars;
}

function popupImg(url, arr)
{
    window.open('/popup_img.php?url=' + url + splitArr(arr)
        ,'popupWindow'
        ,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150'
    );
}

function popupDoc(url, arr)
{
    window.open('/popup_doc.php?url=' + url + splitArr(arr)
        ,'popupWindow'
        ,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150'
    );
}

//------------------------------------
function formManagerSubmit(form, variable, val)
{
    if(variable && val)
    {
        document.getElementById(variable).value = val;
    }
    document.getElementById(form).submit();
}

function checkForm(form, mark, subTrue)
{
    if(!form)    var form    = '';
    if(!mark)    var mark    = ' .check';
    if(!subTrue) var subTrue = true;

    var oForm = $(form + mark);
    var error  = 0;

    oForm.each(function() {
        this.style.background = '#ffffff';
        if(this.value == '')
        {
            error += 1;
            this.style.background = '#FF9B9D';
        }
    });

    if(error > 0)
    {
        alert('Некоторы из полей необходимые для заполнения небыли заполены!');
        return false;
    }
    if(subTrue == true){
       $(form).submit();
    }
    return true;
}

function checkMail(mail)
{
    lens = mail.value.length;
    if(lens>0)
    {
        symb=document.logform.EMail.value;
        for(i=0;i<lens;i++)
        {
            if(!(((symb.indexOf("@")!=-1 &&  symb.indexOf(".")!=-1))))
            {
                return false;
            }
        }
        return true;
    }
    return false;
}

//------------------------------------

function getMousePosition(obj)
{
    var position = new Array();

	if (document.attachEvent != null) { // Internet Explorer & Opera
		x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else if (!document.attachEvent && document.addEventListener) { // Gecko
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	} else {
		// Do nothing
	}

	position = {0:x, 1:y, x:x, y:y};
    return position;
}



function getElementX_(oElement )
{
    var iPosX = oElement.offset();
    return iPosX['left'];
}

function getElementY_(oElement )
{
    var iPosX = oElement.offset();
    return iPosX['top'];
}

function showhint(msg, parent, width_)
{
    idHint = 'hintbox';

    var oMenu        = document.getElementById(idHint);
    var oMenu_   = $('#'+idHint);

    var oDcX    = $(window.document).width();
    var oDcY    = $(window.document).height();
    var oParent = $('#'+parent);


    if(width_ > 0) oMenu.style.width = width_;
    if((oDcX - getElementX_(oParent) - oMenu_.width() ) <= 30 )
        oMenu.style.left = getElementX_(oParent) - oParent.width()- oMenu_.width();
    else
        oMenu.style.left = getElementX_(oParent) + oParent.width();

    if((oDcY - getElementY_(oParent) - oMenu_.height() ) <= 5 )
        oMenu.style.top = getElementY_(oParent)- oParent.height() - oMenu_.height();
    else
        oMenu.style.top  = getElementY_(oParent);
/*
    var parent = document.getElementById(parent);

    if((oDcX - getElementX(parent) - oMenu_.width() ) <= 30 )
        oMenu.style.left = getElementX(parent)- oMenu_.width();
    else
        oMenu.style.left = getElementX(parent);

    if((oDcY - getElementY(parent) - oMenu_.height() ) <= 5 )
        oMenu.style.top = getElementY(parent) - oMenu_.height();
    else
        oMenu.style.top  = getElementY(parent);

*/
    oMenu.innerHTML  = msg;
    $(oMenu_).show(1);
}


function hidehint()
{
    id = 'hintbox';
    $('#'+id).hide(1);
}


//------------------------------------

function getElementX_(oElement )
{
    var iPosX = oElement.offset();
    return iPosX['left'];
}

function getElementY_(oElement )
{
    var iPosX = oElement.offset();
    return iPosX['top'];
}

function getElementX(oElement )
{
    var iPosX = oElement.offsetLeft;
    while ( oElement.offsetParent != null )
    {
        oElement = oElement.offsetParent;
        iPosX += oElement.offsetLeft;
        if (oElement.tagName == 'BODY') break;
    }
    return iPosX;
}

function getElementY(oElement )
{
    var iPosY = oElement.offsetTop;
    while ( oElement.offsetParent != null )
    {
        oElement = oElement.offsetParent;
        iPosY += oElement.offsetTop;
        if (oElement.tagName == 'BODY') break;
    }
    return iPosY;
}

function submenu(parent, id, w, h)
{
    if (!w) w = 0;
    if (!h) h = 0;

    var oParent      = document.getElementById(parent);
    var oMenu        = document.getElementById(id);

    oMenu.style.left = getElementX(oParent)+w;
    oMenu.style.top  = getElementY(oParent)+h;

	_submenu(id);
}

function _submenu(id)
{
	var oSmenu = getObj(id);
    oSmenu.toggle(function(){
		$(document).bind('click',function(){
	        oSmenu.hide();
		});
	});
}



/*
function getMousPosition()
{
    document.onclick = mousPosition;
}

function mousPosition(e)
{
    x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
    y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
    document.onclick = '';

    var posionts = new Array();
    posionts[0] = x;
    posionts[1] = y;

    return posionts;
}
*/
/*------------------------------
//положение мыши на экране
function UpdateMousPosition(e) {
    //varPicDiv = document.all.picDiv;
    x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
    y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
alert(x+'+'+y);
/*
    if (picDiv != null) {
        varPicDiv.style.left = (x + 30) + "px";
        varPicDiv.style.top     = (y - 100) + "px";
    }
*//*
}
document.onmousemove = UpdateMousPosition;
*/
//============================

/*
function submenu(parent, id, w, h)
{
    if (!w) w = 0;
    if (!h) h = 0;

    var oParent      = document.getElementById(parent);
    var oMenu        = document.getElementById(id);

    oMenu.style.left = getElementX(oParent)+w;
    oMenu.style.top  = getElementY(oParent)+h;

//  
//    return showHideLaer(id);
    showHideLaer('#'+id, 'toggle', 250);

//    $('#'+id).show();

//    $(document).one('click', showHideLaer('#'+id, 'hide', 250));

return;

    showHideLaer('#'+id, '', 250);

    $('#').one('dblclick', showHideLaer('#'+id, '', 250));

    if($('#'+id).css("display") == 'block')
    {
//      $(document).one('dblclick', showHideLaer('#'+id, '', 250));
    }

}
*/
/*
function setCookie(variable, value)
{
    if(value == 'toggle')
    {
        value = ( (getCookie(name) ) > 0 ? 0 : 1 );
    }

//  toggle
    var expdate = new Date ();
    expdate.setTime(expdate.getTime() + (3650 * 24 * 60 * 60 * 1000));
    document.cookie = variable +'='+ escape (value) + "; expires=" + expdate.toGMTString() + "; path=/"; 
}

*/
function getCookie( name )
{
    var start = document.cookie.indexOf( name + '=' );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
    {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ';', len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure )
{
    if(value == 'toggle')
    {
        value = ( (getCookie(name) ) > 0 ? 0 : 1 );
    }

    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires )
    {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name+'='+escape( value ) +
        ( ( expires ) ? ';expires='
+expires_date.toGMTString() : '' ) +
//expires.toGMTString()
        ( ( path ) ? ';path=' + path : ';path=/' ) +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain )
{
    if ( getCookie( name ) ) document.cookie = name + '=' +
            ( ( path ) ? ';path=' + path : '') +
            ( ( domain ) ? ';domain=' + domain : '' ) +
            ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function echo(str)
{
    alert(str);
}

function print_r(obj)
{
//  obj = opt;
    obj_name = 'xz';
       var result = ""
       for (var i in obj) {
          result += obj_name + "." + i + " = " + obj[i] + "<BR>\n"
//       result += "<HR>\n"
       }
    alert(result);
}

/*
* разукрашиваем ROW таблицы ID
* row[i] % 2 => tr2
* else       => tr1
*/
function tableTdColor(id, tr1, tr2, tag)
{
    if(!tr1 || !id) return;
    if(!tr2) var tr3=null;
    if(!tag) var tag = 'tr';

    var oBjT = $(id);

    oBjT.each(function() {
        for(var i=1; i < this.getElementsByTagName(tag).length; i++)
        {
            if(tr2 && i % 2){
                this.getElementsByTagName(tag)[i].className = tr2;
            }
            else{
                this.getElementsByTagName(tag)[i].className = tr1;
            }
        }
    });
}

function SetCenterImgPos(id, img)
{
    var oImg   = document.getElementById(img);
    var oBlock = document.getElementById(id);

    if(oImg.src == window.location){
		oImg.src = '/images/spacer.gif';
		return;
	}

    if(oBlock.clientWidth > oImg.clientWidth){
        oImg.style.left = (oBlock.clientWidth - oImg.clientWidth) / 2;
    }
    if(oBlock.clientHeight > oImg.clientHeight){
        oImg.style.top = (oBlock.clientHeight - oImg.clientHeight) / 2;
    }
}