var myReloadingNow=false;


function StrRepeat(cnt, c)
{
    if (!c) c=' ';
    var res="";
    while(cnt-->0)
        res+=c;
    return res;
}

function trim(str)
{
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}


//-------------------------------------------------------------------------------------------------
//class for actions list. Used for saving actions, that will
//be executed after page loaded
function ActionsList()
{
    var list=new Array();
    this.AddAction=function(act)
    {
        list.push(act);
    };
    this.ExecAll=function()
    {
        for(var i in list)
        {
            try
            {        
                list[i]();
            }
            catch(e)
            {
                alert(e);
            }
        }        
    };
}
var al_acts=new ActionsList();


//--------------------------------------------------------------------
//function for document reload in specifed window
function DocReload(win, path, params)
{
    //if already reloading - exit
    if (myReloadingNow) return;
    var loc=win.location.protocol+"//"+win.location.host;
    if (path)
        loc=loc+path;
    else
        loc=loc+win.location.pathname;
    if (params)
        loc=loc+"?"+params;
    else
        loc=loc+win.location.search;
    myReloadingNow=true;
    win.location.href=loc;
}

//функция, вызываемая после загрузки страницы целиком
function AL()
{    
    /*var res=StrSplitTag("<script type=\"text/javascript\">document.write(\"<a href='hp://t.ru/ck' \"+\"target=_blank><img src='hp:r\"+Math.random()+\"' alt='' title='LI: по 24\"+\" чя' \"+\"br=0 wh=88 ht=31><\/a>\")</script>");
    
    alert(res[0]);
    alert(res[1]);
    alert(res[2]);    
    alert(res[3]);
    alert(res[4]);*/
    //исполнение списка действий
    al_acts.ExecAll();        
}

function goto_pg(pg)
{
}

var nodebug=true;

//--------------------- debug console --------------------------
var gDebugWin;
function PrintDebug(msg)
{
    if (nodebug) return;
    if (!gDebugWin) ShowDebugWindow();
    var node=null;
    try
    {
        node=gDebugWin.document.createTextNode(msg);
    }catch(e)
    {
        gDebugWin=null;
        ShowDebugWindow();
        node=gDebugWin.document.createTextNode(msg);
    }
    var p=gDebugWin.document.createElement("p");
    p.appendChild(node);
    gDebugWin.document.body.appendChild(p);
}

function ShowDebugWindow()
{
    if (!gDebugWin)
    {
        gDebugWin=open("", "debug_win");
        gDebugWin.document.write("<HTML><BODY></BODY></HTML>");
    }
}

function DumpValue(val, ret, num)
{
    if (nodebug) return;
    if (!num) num=0;
    if (num>3) return "(...)";
    try
    {
        if (typeof(val)=="object")
        {
            var res=" {";
            for(var i in val)
            {
                res+=i+': '+DumpValue(val[i], true, num+1);
            }
            res+="} ";        
        }
        else if (typeof(val)=="array")
        {
            var res=" [";
            for(var i in val)
            {
                res+=' '+i+': '+DumpValue(val[i], true, num+1);
            }
            res+="] ";        
        }
        else
        {
            res='"'+val+'"';
        }
    }
    catch(e)
    {
        res+="(exeption)";
    }
    if (!ret) 
        PrintDebug(res);
    return res;
}
