var glMarkCount=5;

function defined(obj)
{
    return (  (typeof(obj)!="undefined") && (obj!=null)  );
}

function absPosition( calcOffset, ctrl, destDIV, leftPlus, topPlus)
{
    if (ctrl==null) return;
    if (calcOffset)
    {
        var aTag = ctrl;
        var leftpos=0;
        var toppos=0;
        do 
        {
            aTag = aTag.offsetParent;
            leftpos	+= aTag.offsetLeft;
            toppos += aTag.offsetTop;
        } while (aTag.tagName!="BODY");
        destDIV.style.left=leftpos+leftPlus;
        destDIV.style.top=toppos+topPlus;
    } else 
    {
        destDIV.style.left=leftPlus;
        destDIV.style.top=topPlus;
    }
}

function getFieldIndex(name, formName)
{
    var all=document.forms[formName].elements["editFields"].value.split(",");
    for(var i=0;i<all.length;i++)
        if (all[i]==name) return(i);
    return(-1);
}


function select_picture(file_obj,pic_obj)
{
    var newimg=file_obj.value.replace(/\//g,"\\");
    if (newimg.indexOf("\\")>-1) pic_obj.innerHTML="<img src='file://"+newimg+"'/>";
}


function fileUploadOk(java, type, objectid, fname)
{
    switch(type)
    {
        case 0:
            var fieldObj=document.forms["blogForm"].elements["f"+getFieldIndex("[ext]","blogForm")+"_"+objectid];
            fieldObj.value="."+fname.split(objectid+".")[1];
            break;
    
    }
}



//работа с главным меню



function selectMenu1(id)
{
    var c1=document.all["c1_"+id];
    if ((typeof(c1)!="undefined")&&(c1!=null))
    {
//        c1.innerHTML='<img src="images/menu_left.gif">';
//        c1.nextSibling.bgColor="#e1e2e6";
//        c1.nextSibling.nextSibling.innerHTML='<img src="images/menu_right.gif">';
        c1.className="activeMenu";
        c1.childNodes[0].className="act1";
//        c1.nextSibling.className="activeMenu";
//        c1.nextSibling.nextSibling.className="activeMenu";
        
        if (c1.childNodes.length>1) document.all["navPath_1"].innerHTML=" / "+c1.childNodes[1].outerHTML.split("menuLine1").join("");
    
        var subMenu=document.all.item("submenu_"+id);
        subMenu.style.display="";
    }
}

function selectMenu2(id, image)
{
    if (oldSub!=null) 
    {
        var clr=oldSub;
        oldSub=null;
        selectMenu2(clr, "menu_bulet_white.gif");
    }
    oldSub=id;
    var c1=document.all.item("sub_img"+id);
    if (defined(c1)) 
    {
        document.all["navPath_2"].innerHTML=" / "+c1.nextSibling.innerHTML.split("menuLine2").join("");
        c1.innerHTML='<img src="images/'+image+'">';
    }
}




//работа с меню рейтинга

var oldRatingSelect=null;


function initRating(mode, service)
{
    var blogVars=document.forms["blogForm"];
    if ((defined(blogVars))&&(blogVars.length>4)) 
    {
        var type=(mode==1) ? blogVars.elements.__type.value : blogVars.elements.__type.value;
        var obj=document.all["ra_"+type];
        if (defined(obj)) selectRatingMenu(obj.parentNode,"bulet_smal_blue.gif", mode);
        
        if (service==1)
        {
            obj=document.getElementById("cf_"+blogVars.__blogtype.value);
            if (obj) obj.className="activeR";
            obj=document.getElementById("rf_"+blogVars.__rectype.value);
            if (obj) obj.className="activeR";
        }            
    }
}

function clickRatingMenu(mode,evn)
{
    var evn=evn || window.event;
    var target=evn.target || evn.srcElement;
    if (target.id.substring(0,3)=="ra_") selectRatingMenu(target.parentNode, "bulet_smal_blue.gif", mode);
}

function selectRatingMenu(obj, image, mode)
{
    if (oldRatingSelect!=null) 
    {
        var clr=oldRatingSelect;
        oldRatingSelect=null;
        selectRatingMenu(clr, "bulet_smal_gray.gif", mode);
    }
    oldRatingSelect=obj;
    
    var c1=obj.previousSibling;
    if ((typeof(c1)!="undefined")&&(c1!=null)) 
    {
        c1.innerHTML='<img width=11 height=11 src="images/'+image+'">';
//        if (image=="bulet_smal_gray.gif") c1.nextSibling.style.fontWeight=""; else c1.nextSibling.style.fontWeight="bold";
        if (image=="bulet_smal_gray.gif") c1.nextSibling.className=""; else c1.nextSibling.className="activeR";
    }
}





function openContact(url, name)
{
    var w=window.open(url,name,
        "width=500,height=550,resizable=1,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,top=0,left=0");
	w.focus();
}

function messageDialog(mode, id1, id2)
{
    openContact("messages.aspx?userid="+id1, "wmes_"+id1 );
}

function newFrame(div, url, kx, ky)
{
    div.innerHTML="<iframe class='frameStyle' width='"+kx+"' height='"+ky+"' frameborder='0' scrolling='no' src='"+url+"' />";
}

function setMessageLoad(divName)
{
    window.setTimeout( "messagesLoad('"+divName+"');", 4000);
}

function messagesLoad(divName)
{
    var newM=document.all[divName];
    
    if (defined(newM))
    {
        var txt=loadTEXT("query.aspx?type=1&rnd="+Math.round( 100000*Math.random() )  );
        var oldText=newM.innerHTML;
        if ((oldText!=txt)&&(txt*1>oldText*1)) 
        {
            playMusic(document.all.musicDIV, "music/children.mid");
            newM.innerHTML=txt;
        }
        setMessageLoad(divName);
//      window.setTimeout( "window.frames['"+frameName+"'].location.assign('query.aspx?type=1&rnd="+Math.round( 100000*Math.random() )+"');", 4000);
    }
}

function playMusic(div, music)
{
if (defined(div))
    div.innerHTML="<EMBED SRC='"+music+"' HIDDEN='True' type='audio/x-midi' height=1 width=1 autostart='true' loop='1'/>";
}

function initMenu(mode)
{
    var blogVars=document.forms['blogForm'].elements;
    if (defined(blogVars.__navPath_3))
    {
        var p2=blogVars.__navPath_2;
        if ( (defined(p2)) && (p2.value.length>0) ) document.all.navPath_2.innerHTML=p2.value;
        document.all.navPath_3.innerHTML=blogVars.__navPath_3.value;
        document.all.navPath_4.innerHTML=blogVars.__navPath_4.value;
        document.all.navPath_5.innerHTML=blogVars.__navPath_5.value;
    }
}


var glServiceMode=0;
var glHost="";
function initPage(mode, catBook, host)
{
//    debugM("cookie: "+document.cookie);
    glServiceMode=mode;

    switch(mode)
    {
        case 1:  case 2:  //blog (2 - pers)
            if (defined(printCalendar)) printCalendar(mode, mode);
            if (defined(document.all.ra_div)) initRating(1,mode);
            initMenu(mode);
            initFormElements(document.forms['blogForm']);
            if (catBook<2) showAllCatalog(catBook,false);
            break;
            
        case 10: //user
            var bF=document.forms['blogForm'];
            if (defined(document.all.ra_div)) initRating(3,mode);
            initMenu(mode);
            initFormElements(bF);
            if (typeof(advInit1)=="function") advInit1();
            if (typeof(advInit2)=="function") advInit2();
//            if (bF.elements['__type'].value==261) switchSearchForm('открыть', 'закрыть', 'content003', 'img003');
            break;
            
        case 20: //foto
            if (defined(printCalendar)) printCalendar(3, 3);
            if (defined(document.all.ra_div)) initRating(3,mode);
            initMenu(mode);
            initFormElements(document.forms['blogForm']);
            break;       
            
           
        case 40: //forum
            catalogMode=6; //для каталога рубрик
            if (defined(document.all.ra_div)) initRating(3,mode);
            initMenu(mode);
            initFormElements(document.forms['blogForm']);
            break;
    }
    
    setMessageLoad("newMessage");
}





function switchSearchForm(show_title, hide_title, nameCont,nameImg)
{

	var obDiv = document.all[nameCont];
	var obImg = document.all[nameImg];
    
	if(obDiv.style.display!="none")
	{
		obDiv.style.display = "none";
		obImg.src = "images/user_page_open.gif";
		obImg.alt = obImg.title = show_title;
	}
	else
	{
		obDiv.style.display = "block";
		obImg.src = "images/user_page_close.gif";
		obImg.alt = obImg.title = hide_title;
	}
}




//
//begin работа с рейтингами
//

function initTop(postID, postrating1count, rait, destHTML, actType, viewTop )
{
    if (defined(destHTML))
    {
        var isComment=((actType>13)&&(actType<16)) ? "C" : "";
        var srait=(rait==0) ? "0" : ( (rait>0) ? "+"+rait : "-"+rait );
        var res="<table cellspacing=0 cellpadding=0 class='raitTable'><tr><td><div id='topM_"+isComment+postID+"' class='markedText big1'>"+srait+"</div></td>";
        if (viewTop==1) res+="<td><a class='post_header' href='javascript:doAction("+actType+","+postID+",1);'>+</a>"+
            "<a class='post_header' href='javascript:doAction("+actType+","+postID+",-1);'>-</a></td>";
        res+="</tr>";
        if (actType!=16) res+="<tr><td colspan=2 style='font-size: 60%;' id='topC_"+isComment+postID+"'>голосов: "+postrating1count+"</td></tr>";
        res+="</table>";
        
        destHTML.innerHTML=res;
    }
}

function iT(comID, rating1count, rait, viewTop)
{
    initTop(comID, rating1count, rait, document.getElementById("comTop_"+comID), 14, viewTop);
}

//
//end работа с рейтингами
//



function setSelectValue(obj, value)
{
    if (typeof(obj)!="undefined")
    {
        for(var i = 0; i < obj.options.length; i++)
            if (obj.options[i].value==value) 
            {
                obj.selectedIndex=i;
                return(i);
            }
    }
}

function getSelectValue(obj, notFind)
{
    return( defined(obj) ? (obj.options[obj.selectedIndex].value) : notFind);
}

function initFormElements(frm)
{
    var te,tv,nname,debug="";
    for(var i=0;i<frm.elements.length;i++)
    {
        te=frm.elements[i];
        nname=te.nodeName.toUpperCase();
        if (nname=="SELECT")
        {
            tv=te.getAttribute("selvalue");
            if ((tv)&&(tv.length>0)&&(te.options.length>0)&&(tv!=te.options[te.selectedIndex].value)) 
                setSelectValue(te, tv);
        } else if ( (nname=="INPUT")&&(te.type.toUpperCase()=="CHECKBOX") )
        {
            te.checked=(te.value=="1");
        }
    }
}


function getAllCheck(elems, prefix)
{
    var pl=prefix.length,te,tn;
    var res="";
    for(var i=0;i<elems.length;i++)
    {
        te=elems[i];
        tn=te.name;
        if ((tn.length>pl)&&(tn.substring(0,pl)==prefix)&&(te.checked)) 
            res+=","+te.name.substring(pl);
    }
    return( (res.length>0) ? res.substring(1) : "" );
}

function setAllCheck(elems, prefix, val)
{
    var pl=prefix.length,te,tn;
    var res="";
    for(var i=0;i<elems.length;i++)
    {
        te=elems[i];
        tn=te.name;
        if ( (tn.length>pl)&&(tn.substring(0,pl)==prefix) ) 
            te.checked=val;
    }
}

function viewFullMessage(mode, destDIV, id1, id2)
{
    switch(mode)
    {
        case 51:
            glPageMode=10021;
            loadObj( document.all.loadF, destDIV, "modules/message.aspx?mode="+mode+"&id1="+id1+"&id2="+id2);
            break;
    }
}


function searchClick(service)
{
try
{
    var search=document.all.searchStr.value;
    var what=1*getSelectValue(document.all.whatSearch,0);
    var where=1*getSelectValue(document.all.whereSearch,0);
    
    var blogVars=document.forms['blogForm'].elements;

    if ((what>=1000)&&(what<=1200))
    {
        blogVars.__type.value=310;
        blogVars.__tagid.value=what-1000;
        blogVars.__tag.value=search;
        reloadBlogs(232);
    } else switch(what)
    {
        case 52:
        case 54:
        case 82:
            blogVars.__type.value=what+( (what==54)? 0 : where );
            blogVars.__idlist.value=search; 
            if (service=="blog") reloadBlogs(82); else reloadUser(82);
            break;
    }
} 
catch(e) { debugE(e,2006); }
}


function fillSelectHTML( from, to, selectVal)
{
    var res="";
    var val=(defined(selectVal)) ? selectVal : 0;
    for(var i=from;i<=to;i++)
        res+="<option value='"+i+"' "+((val==i) ? "selected " : "")+">"+i+"</option>";
    return(res);
}

function fillSelectArrayHTML(from, to, selectVal, arr)
{
    var res="";
    var val=(defined(selectVal)) ? selectVal : 0;
    for(var i=from;i<=to;i++)
        res+="<option value='"+arr[i]+"' "+((val==i) ? "selected " : "")+">"+arr[i]+"</option>";
    return(res);
}


function fillSelect(obj, from, to, selectVal)
{
    obj.innerHTML=fillSelectHTML(from, to, selectVal);
}

function fillSelectArray(obj, from, to, selectVal, arr)
{
    obj.innerHTML=fillSelectArrayHTML(from, to, selectVal, arr);
}


function countryChange(country, cityEl, adv)
{
    var cCode=1*country.options[country.selectedIndex].value;
    var cityObj=country.form.elements[cityEl];
    cityObj.innerHTML=loadTEXT("query.aspx?type=22&val0="+cCode) + ( (adv==1) ? "<option value='0'>Другой город</option>" : "");
    
    document.all["_row_"+cityEl].style.display=(cCode>0) ? "" : "none";
}


function cityChange(city, citySpan)
{
    var cCode=1*city.options[city.selectedIndex].value;
    citySpan.style.display=(cCode==0) ? "" : "none";
    
    cityObj.innerHTML=loadTEXT("query.aspx?type=22&val0="+cCode) + ( (adv==1) ? "<option value='0'>Другой город</option>" : "");
    
    document.all["_row_"+cityEl].style.display=(cCode>0) ? "" : "none";
}

function markWords(words, selectClass, destDiv)
{
try
{
    if (!defined(words)) return;
    if ( (defined(destDiv)) && (words.length>0) )
    {
//        alert(words);
        var src=destDiv.innerHTML;
        var patt="([\\s,:-><])("+words+")([\\s,:-><])";
        destDiv.innerHTML=src.replace(new RegExp(patt,"g"), "$1<span class='"+selectClass+"'>$2</span>$3");
//    alert(patt);
    }
} catch (e) { debugE(e,2007); }
}

var glCatBook=0;
function openCat2(show_title, hide_title, catBook)
{
	var obDiv = document.all.cat_ext;
	var obImg = document.all.cat_line;
	if (obDiv.style.display=="block")
	{
		obDiv.style.display = "none";
		obImg.title = show_title;
		obImg.innerHTML="&raquo;";
	    doAction(201,2);
	}
	else
	{
		obDiv.style.display = "block";
		obImg.title = hide_title;
		obImg.innerHTML="&laquo;";
		
		updateCatBook(catBook);
	}
	document.all.subCat.style.display=obDiv.style.display;
}


function showBlogCat(newm, label, catBook)
{
    var oldMode=catalogMode;
    catalogMode=newm;
    var catDiv=document.all.cat_ext;
    
    document.all.cat_label0.innerHTML=label;
    if (catDiv.style.display=="none") 
        openCat2('Показать каталог', 'Скрыть каталог', catBook);
    else if (catalogMode!=oldMode)
    {
        var blogVars=document.forms['blogForm'].elements;
        if (blogVars.__partid.value*1>0) reloadUser(catalogMode+(blogVars.__type.value*1)%10, blogVars.__partid.value);
    }
}



function showAllCatalog(catBook, doSave)
{
    glCatBook=catBook;
    var cd=document.getElementById("cat_ext");
    if (!defined(cd)) return;
    var n,el;

    if (cd.style.display=="none") showBlogCat(catalogMode, document.getElementById("cat_label0").innerHTML, catBook);
     
    if (doSave) doAction(201,catBook);

    var allA=cd.getElementsByTagName("A");
    for (var i=0;i<allA.length;i++)
    {
        el=allA[i];
        n=el.getAttribute("check");
        if (defined(n)) 
        {
            el.parentNode.style.display=( (catBook==1) || (n=="1")&&(catBook==0) ) ? "" : "none";   

            n=(el.previousSibling.previousSibling) ?  el.previousSibling.previousSibling.getAttribute("name") : null;
            if ((defined(n)) && (n.substring(0,4)=="_up_") ) el.previousSibling.previousSibling.style.display=( (catBook==1) ) ? "" : "none";   
        }
    }
    
    updateCatBook(catBook);
    
    if ((catBook==0)&&(doSave)) viewCat(); 
}

function updateCatBook(catBook)
{
    document.all["mCatalog"+catBook].className="act1";
    document.all["mCatalog"+(1-catBook) ].className="pas1";
    document.all["cat_label"+catBook].className="catalogBg cHead";
    document.all["cat_label"+(1-catBook) ].className="cHead";  
    
    document.all.catSelect.style.display=(catBook==1) ? "" : "none";  
}

function clearSelectCat()
{
    var cd=document.getElementById("cat_ext"), el;
    doAction(203,0);

    var allI=cd.getElementsByTagName("INPUT");
    for (var i=0;i<allI.length;i++)
    {
        el=allI[i];
        if ( (defined(el.name)) && (el.name.substring(0,4)=="_up_") ) el.checked=false;
    }
    
    var cs=document.getElementById("catSelect2");
    if (cs) 
    {   
        cs.innerHTML="";
        checkTitleCats(cs);
    }    
}

function catDivClick(evn, Div)
{
    var evn=evn || window.event;
    var obj=evn.target || evn.srcElement;
    var n=obj.name;
    var destDiv=document.all.catSelect2;
    
    if ( (defined(n)) && (n.substring(0,4)=="_up_") )
    {
        var code=n.substring(4);
        doAction(202,code, (obj.checked) ? 1:0 );
        
        if (obj.checked) 
        {
            var e1=obj.nextSibling;
            if (defined(e1.nextSibling)) addSelectCat(code, e1.nextSibling.innerHTML, destDiv, true,obj);
            else addSelectCat(code, e1.innerHTML, destDiv, true,obj);
        }
        else 
        {
            var e1=document.getElementById('sc_'+code);
            if (defined(e1)) e1.parentNode.removeChild(e1);
        }
        
        checkTitleCats(destDiv);
    }
}

function refreshSelectCat()
{
    var resH="",n="",el;
    var Div=document.getElementById("cat_ext"), cs=document.getElementById("catSelect2");
    cs.innerHTML="";

    var allI=Div.getElementsByTagName("INPUT");
    for (var i=0;i<allI.length;i++)
    {
        el=allI[i];
        n=el.name;
        if ( (defined(n)) && (n.substring(0,4)=="_up_") ) addSelectCat(n.substring(4), el.nextSibling.nextSibling.innerHTML, cs, false, el);
    }

    checkTitleCats(cs);    
}

function addSelectCat(code, name, destObj, checkVis, inputEl)
{
    var advTitle=(checkVis) ? deselectParent(inputEl,true,true) : "";
    if (!defined(document.getElementById("sc_"+code))) 
        destObj.innerHTML=destObj.innerHTML+" <span class='markedText sc' id='sc_"+code+"'>"+advTitle+name+"</span> ";
    if (checkVis) checkTitleCats(destObj);
} 

function deselectParent(inputEl, pParent, pChild)
{
    var res="";

    var ap0=inputEl.getAttribute("p");
    if ((pParent)&&(ap0!="0")) 
    {
        var p0=document.all["_up_"+ap0]; 
        
        var ap1=p0.getAttribute("p");
        var p1=document.all["_up_"+ap1];
        
        if (defined(p1)) { p1.checked=false; p1=p1.nextSibling; res+=(defined(p1.nextSibling)) ? p1.nextSibling.innerHTML+" / " : p1.innerHTML+" / "; }
        if (defined(p0)) { p0.checked=false; p0=p0.nextSibling; res+=(defined(p0.nextSibling)) ? p0.nextSibling.innerHTML+" / " : p0.innerHTML+" / "; }
        
        var n=document.all["sc_"+ap0];
        var n1=document.all["sc_"+ap1];
        if (defined(n)) n.parentNode.removeChild(n);
        if (defined(n1)) n1.parentNode.removeChild(n1);
    }
    
    if (pChild)
    {
        var cd=document.getElementById("catSelect2"), tname, code, ne=inputEl.nextSibling;
        var cname=(defined(ne.nextSibling)) ? ne.nextSibling.innerHTML+" /" : ne.innerHTML+" /";
        var allSpan=cd.getElementsByTagName("SPAN");
        for (var i=0, j=allSpan.length;i<j;i++)
        {
            tname=allSpan[i];
            if (tname.innerHTML.indexOf(cname)>-1) 
            {
                code=allSpan[i].id.substring(3);
                tobj=document.getElementById("_up_"+code);
                if (defined(tobj)) tobj.checked=false;
                cd.removeChild(allSpan[i--]);
                j--;
            }
        }
    }
    
    return(res);
}
    
function checkTitleCats(obj)
{
    if (obj.childNodes.length>0) obj.parentNode.style.display=""; else obj.parentNode.style.display="none";
}


function viewCat()
{
    reloadUser(3,1);
}

function openDialog1(url, name)
{
    var w=window.open(url,"",
        "width=540,height=300,resizable=1,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,top=240,left=400");
	w.title=name;
	w.focus();
}

function getEvent(evn)
{
    var res=new Object();
    var evn=evn || window.event;
    res.evn=evn;
    if (document.attachEvent != null) //IE & OPERA
    {
        res.x=evn.clientX+document.body.scrollLeft;
        res.y=evn.clientY+document.body.scrollTop;
        res.Obj=evn.srcElement;
    } else 
    {
        res.x = evn.clientX+window.scrollX;
        res.y = evn.clientY+window.scrollY;
        res.Obj=evn.target;
    }
    return(res);
}


//0 - ничего, 1 - ждем вывода, 2 - выведено, 3 - ждем скрытия
var glMenuTime=null, glMenuObj=null, glStateMenu=0;
function u1(evn, uid)
{
    var e=getEvent(evn);
    if (glMenuObj!=e.Obj)
    {
        if (glMenuTime) window.clearTimeout(glMenuTime);
        glMenuObj=e.Obj;
        glMenuTime=window.setTimeout("uMenu("+( (false) ? "true" : "false")+","+e.x+","+e.y+","+uid+");", 500);
        glStateMenu=1;
    }
}


function u2(evn, uid)
{
    if ((glMenuTime)&&(glStateMenu==1)) 
    {
        window.clearTimeout(glMenuTime);
        glMenuObj=null;
        glStateMenu=0;
    }
    
    if (glStateMenu==2) outW(evn); 
}

function uMenu(evn,x,y,uid)
{
    var div1=document.getElementById("wid");
    if ((glMenuObj)&&(div1))
    {
        div1.innerHTML=loadTEXT("query.aspx?type=114&val0="+uid);
        absPosition(evn,glMenuObj, div1, x, y);
        div1.style.display="block";        
        glStateMenu=2;
    }
}

function overW(evn)
{
    if (glMenuTime) 
    {
        glStateMenu=2;
        window.clearTimeout(glMenuTime);
    }
}

function outW(evn)
{
    if (glStateMenu!=3)
    {
        glStateMenu=3;
        glMenuTime=window.setTimeout("hideW()", 1000);    
    }
}

function hideW()
{
    var div1=document.getElementById("wid");
    if (div1) div1.style.display="none";
    glStateMenu=0;
    glMenuObj=null;
}

function showQuery( qtype, t1, t2, t3 )
{
    div1.innerHTML=loadTEXT("query.aspx?type=114&val0="+uid);
}

function hideQuery()
{
   
}