function replaceHTML(el, html) {
    var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
    var newEl = document.createElement(oldEl.nodeName);
    // Preserve the element's id and class (other properties are lost)
    newEl.id = oldEl.id;
    newEl.className = oldEl.className;
    // Replace the old with the new
    newEl.innerHTML = html;
    oldEl.parentNode.replaceChild(newEl, oldEl);
    /* Since we just removed the old element from the DOM, return a reference
    to the new element, which can be used to restore variable references. */
    return newEl;
};

function ajax_createRequestObject()
{
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) {
	try {
    	    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
	catch (E) {
    	    xmlhttp = false;
        }
    }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
	    xmlhttp = new XMLHttpRequest();
	} 
        catch (e) {
	    xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest) {
	try {
	    xmlhttp = window.createRequest();
	} 
	catch (e) {
	    xmlhttp=false;
	}
    }
    return xmlhttp;
}

var ajax_httpprot = ajax_createRequestObject();
var ajax_destObj;

function sndReq(argumentString, destStr, url, postdata, mo){
    if(postdata){
	var method = "post";
	var topost = 'body='+encodeURIComponent(postdata);
    } else {
	var method = "get";
	var topost = null;
    }
    if(!url) url = 'ajax';
    messout = (!mo ? false : true);
    ajax_destObj = document.getElementById(destStr);
    ajax_httpprot.open(method, '/'+url+'.php?'+argumentString);
    if(postdata){
	ajax_httpprot.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax_httpprot.setRequestHeader("Content-length", postdata.length);
	ajax_httpprot.setRequestHeader("Connection", "close");
    }
    ajax_httpprot.onreadystatechange = ajax_handleResponse;
    ajax_httpprot.send(topost);

}

function ajax_handleResponse()
{
    if(!messout)
	ajax_destObj.innerHTML = stat(ajax_httpprot.readyState);

    if(ajax_httpprot.readyState == 4)
    {
	var response = ajax_httpprot.responseText;
//	alert(response);
	replaceHTML(ajax_destObj, response);
    }
}

function addSim(argstr, goodsim, badsim){
    gsobj = document.getElementById(goodsim);
    bsobj = document.getElementById(badsim);
    ajax_httpprot.open('get', '/takesim.php?'+argstr);
    ajax_httpprot.onreadystatechange = processReqChange;
    ajax_httpprot.send(null);
}

function processReqChange(){
    if(ajax_httpprot.readyState == 4){
        var response = ajax_httpprot.responseText;
	arr = response.split("|");
	if (arr[0] != 'done' && arr[0] != 'del'){
	    message = arr[0];
	    gsobj.innerHTML = arr[1];
	    bsobj.innerHTML = arr[2];
	    alert(message);
	}else if(arr[0] == 'del'){
	    gsobj.innerHTML = '';
	    bsobj.innerHTML = '';
	}else{
	    gsobj.innerHTML = arr[1];
	    bsobj.innerHTML = arr[2];
	}
    }
}

function stat(n)
{
  switch (n) {
    case 0:
      return "<center>не инициализирован</center>";
    break;
 
    case 1: 
      return "<center>загрузка...</center>";
    break;
 
    case 2: 
      return "<center>загружено</center>";
    break;
 
    case 3: 
      return "<center>в процессе...</center>";
    break;
 
    case 4: 
      return "<center>готово</center>";
    break;
 
    default:
      return "<center>неизвестное состояние</center>";  
  }  
}


//////////////////////////tracker func//////////////////////////
var pre_max_width=480;
function imgFit (img, maxW)
{
    var col = img.src.length;
    var typeimg = img.src.substring(col-3,col);
    if(typeimg == 'gif')
        return false;

    if (typeof(img.naturalWidth) == 'undefined') {
        img.naturalHeight = img.height;
        img.naturalWidth  = img.width;
    }

    if (img.width > maxW) {
        img.height = Math.round((maxW/img.width)*img.height);
        img.width  = maxW;
        img.title  = 'Нажмите для просмотра в оригинальном размере';
        img.style.cursor = 'move';
        return false;
    }
    else if (img.width == maxW && img.width < img.naturalWidth) {
        img.height = img.naturalHeight;
        img.width  = img.naturalWidth;
        img.title  = 'Нажмите для уменьшения изображения';
        return false;
    }

    return true;
}

function tag(text1, text2, form, name)
{
    myField = document.forms[form].elements[name];
    if (document.selection)
    {
        document.forms[form].elements[name].focus();
        document.forms[form].document.selection.createRange().text = text1+document.forms[form].document.selection.createRange().text+text2;
    }
    else
    {
        myfn = document.forms[form].elements[name];
        ttag = text1+text2;
        insertAtCursor(myfn, ttag);
    }
}

function insertAtCursor(myField, myValue)
{
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
        myField.selectionEnd = startPos + myValue.length;
    }
    else
    {
        myField.value += myValue;
    }
    myField.focus();
}

function SmileIT(smile, form, name)
{
    insertAtCursor(document.forms[form].elements[name], smile)
}

fadeOpacity.addRule('oR1', .5, 1, 30);

function info(inform){
    ob_in =  document.getElementById("info_but");
    ob_in.innerHTML = inform;
    ob_in.style.display='block';
}

function clos(){
    ob_in =  document.getElementById("info_but");
    ob_in.style.display='none';
}

function klappe_news(id)
{
        var klappText = document.getElementById('k' + id);
        var klappBild = document.getElementById('pic' + id);

        if (klappText.style.display == 'none') {
                klappText.style.display = 'block';
                klappBild.src = 'pic/minus.gif';
        }
        else {
                klappText.style.display = 'none';
                klappBild.src = 'pic/plus.gif';
        }
}

marker = {
    colors : ['#ff0000','#ff4808','#ff7c06','#ffc102','#ffee5f','#ebef57','#d8f051','#b7f246','#90f538','#0eff0b'],
    timeout : false,
    url : "",
    mark : 0,
    vote : function (mark) {
        var url = document.location;
            document.location = this.url+mark;
//	sndReq(this.url+mark, "newratingsystem", "vote", false, true);
    },
    move : function (mark) {
        var e;
        if (this.timeout) clearTimeout(this.timeout);
        var id = Math.floor(mark);
        for (var i=1; i<=id; i++) {
            e = document.getElementById('mark'+i);
            e.style.backgroundColor = this.colors[i-1];
            e.style.opacity = 1;
            e.style.filter = 'none';
            e.style.color = 'black';
        }
        for (var i=id+1; i<=10; i++) {
            e = document.getElementById('mark'+i);
            e.style.backgroundColor = '#FFFFFF';
            e.style.opacity = 1;
            e.style.filter = 'none';
            e.style.color = '#999999';
        }
        if (mark>id) {
            var opacity = (mark-id);
            if (opacity<0.3) opacity=0.3;
            e = document.getElementById('mark'+(id+1));
            e.style.backgroundColor = this.colors[id];
            e.style.opacity = opacity;
            e.style.filter = "alpha(opacity=" + (opacity*100) + ")";
        }
    },
    start : function () {
        this.move(this.mark);
    },
    timer : function () {
        if (this.timeout) clearTimeout(this.timeout);
        this.timeout = setTimeout( "marker.start()", 100 );
    }
}

//////////////////////////tracker func//////////////////////////
var textareafontsize = 14;      //я хуй знает чё это за переменные, код то спиздил!
var textreafontwidth = 7;       //ещё одна неизвестная хуйня
var abc1 = new Array();
var abc2 = new Array();
//массивчик со значениями, даже 2 массивчика
abc1['а'] = 'f';abc1['п'] = 'g';abc1['я'] = 'z';abc1['О'] = 'J';
abc1['б'] = ',';abc1['р'] = 'h';abc1['А'] = 'F';abc1['П'] = 'G';
abc1['в'] = 'd';abc1['с'] = 'c';abc1['Б'] = '<';abc1['Р'] = 'H';
abc1['г'] = 'u';abc1['т'] = 'n';abc1['В'] = 'D';abc1['С'] = 'C';
abc1['д'] = 'l';abc1['у'] = 'e';abc1['Г'] = 'U';abc1['Т'] = 'N';
abc1['е'] = 't';abc1['ф'] = 'a';abc1['Д'] = 'L';abc1['У'] = 'E';
abc1['ё'] = '`';abc1['х'] = '[';abc1['Е'] = 'T';abc1['Ф'] = 'A';
abc1['ж'] = ';';abc1['ц'] = 'w';abc1['Ё'] = '~';abc1['Х'] = '{';
abc1['з'] = 'p';abc1['ч'] = 'x';abc1['Ж'] = ':';abc1['Ц'] = 'W';
abc1['и'] = 'b';abc1['ш'] = 'i';abc1['З'] = 'P';abc1['Ч'] = 'X';
abc1['й'] = 'q';abc1['щ'] = 'o';abc1['И'] = 'B';abc1['Щ'] = 'O';
abc1['к'] = 'r';abc1['ъ'] = ']';abc1['Й'] = 'Q';abc1['Ш'] = 'I';
abc1['л'] = 'k';abc1['ы'] = 's';abc1['К'] = 'R';abc1['Ъ'] = '}';
abc1['м'] = 'v';abc1['ь'] = 'm';abc1['Л'] = 'K';abc1['Ы'] = 'S';
abc1['н'] = 'y';abc1['э'] = '\'';abc1['М'] = 'V';abc1['Ь'] = 'M';
abc1['о'] = 'j';abc1['ю'] = '.';abc1['Н'] = 'Y';abc1['Э'] = '"';
abc1['Ю'] = '>';abc1['Я'] = 'Z';abc2['&'] = '?';//abc1['.'] = '/';
abc2['q'] = 'й';abc2['Q'] = 'Й';abc2['g'] = 'п';abc2['G'] = 'П';
abc2['w'] = 'ц';abc2['W'] = 'Ц';abc2['h'] = 'р';abc2['H'] = 'Р';
abc2['e'] = 'у';abc2['E'] = 'У';abc2['j'] = 'о';abc2['J'] = 'О';
abc2['r'] = 'к';abc2['R'] = 'К';abc2['k'] = 'л';abc2['K'] = 'Л';
abc2['t'] = 'е';abc2['T'] = 'Е';abc2['l'] = 'д';abc2['L'] = 'Д';
abc2['y'] = 'н';abc2['Y'] = 'Н';abc2[';'] = 'ж';abc2[':'] = 'Ж';
abc2['u'] = 'г';abc2['U'] = 'Г';abc2['\''] = 'э';abc2['"'] = 'Э';
abc2['i'] = 'ш';abc2['I'] = 'Ш';abc2['z'] = 'я';abc2['Z'] = 'Я';
abc2['o'] = 'щ';abc2['O'] = 'Щ';abc2['x'] = 'ч';abc2['X'] = 'Ч';
abc2['p'] = 'з';abc2['P'] = 'З';abc2['c'] = 'с';abc2['C'] = 'С';
abc2['['] = 'х';abc2['{'] = 'Х';abc2['v'] = 'м';abc2['V'] = 'М';
abc2[']'] = 'ъ';abc2['}'] = 'Ъ';abc2['b'] = 'и';abc2['B'] = 'И';
abc2['a'] = 'ф';abc2['A'] = 'Ф';abc2['n'] = 'т';abc2['N'] = 'Т';
abc2['s'] = 'ы';abc2['S'] = 'Ы';abc2['m'] = 'ь';abc2['M'] = 'Ь';
abc2['d'] = 'в';abc2['D'] = 'В';abc2[','] = 'б';abc2['<'] = 'Б';
abc2['f'] = 'а';abc2['F'] = 'А';abc2['.'] = 'ю';abc2['>'] = 'Ю';
abc2['/'] = '.';abc2['?'] = ',';abc2['`'] = 'ё';abc2['~'] = 'Ё';


function changetext(formname,textname)
{
    get_texatrea_scroll_position(formname,textname);
    tt = window.document.forms[formname].elements[textname];
    p1 = tt.selectionStart; p2 = tt.selectionEnd;
    var preval = ""; var postval="";
    if (p1==p2) {txt = tt.value;}
    else
    {
        preval = tt.value.substring(0, p1);
        txt = tt.value.substring(p1,p2);
        postval = tt.value.substring(p2);
    }
    txtnew="";
    var symb = "";
    for (kk=0;kk<txt.length;kk++)
    {
        symb = translatesymbol(txt.substr(kk,1));
        txtnew = txtnew.substr(0,txtnew.length) + symb;
    }
    tt.value = preval+txtnew+postval;
    if (p1!=p2) {tt.setSelectionRange(p1+txtnew.length,p1+txtnew.length);}
    set_texatrea_scroll_position(formname,textname);
    setfoc(formname,textname);
    return false;
}

function changetext2(formname,textname)
{
    var is_selection_flag = 1;
    var userselection = document.selection.createRange();
    var txt = userselection.text;
    if (userselection==null || userselection.text==null || userselection.parentElement==null || userselection.parentElement().type!="textarea") 
    {
        is_selection_flag = 0;
        txt = document.forms[formname].elements[textname].value;
    }
    txtnew="";
    var symb = "";
    for (kk=0;kk<txt.length;kk++)
    {
        symb = translatesymbol(txt.substr(kk,1));
        txtnew = txtnew.substr(0,txtnew.length) + symb;
    }
    if (is_selection_flag)
    {
        userselection.text = txtnew; 
        userselection.collapse(); 
        userselection.select();
    }
    else
    {
        document.forms[formname].elements[textname].value = txtnew;
        setfoc(formname,textname);
    }
    return false;
}

function laststringlength(thetext)
{
    var t = thetext.replace(/\n/g," ");
    return thetext.replace(/\n/g," ").length-thetext.replace(/\n/g," ").lastIndexOf(" ")-1;
}

function setfoc(formname,textname)
{
    document.forms[formname].elements[textname].focus();
    return false;
}
function translatesymbol(symb)
{
    return abc1[symb]?abc1[symb]:(abc2[symb]?abc2[symb]:symb);
}

function get_texatrea_scroll_position(formname,textname)
{
    pXpix = window.document.forms[formname].elements[textname].scrollTop;
    pYpix = window.document.forms[formname].elements[textname].scrollLeft;
    return false;
}

function set_texatrea_scroll_position(formname,textname)
{
    var txtarea = document.forms[formname].elements[textname];
    var therest = txtarea.value.substr(txtarea.selectionEnd);
    var fbeg = txtarea.value.substring(0, txtarea.selectionStart);
    var vv = therest.search(/[\n\s]/);
    var r = laststringlength(fbeg)*(textreafontwidth+1)-pYpix-txtarea.clientWidth/2;
    var dd = Math.abs(2*r)<txtarea.clientWidth?0:r-txtarea.clientWidth/2*(r>0?1:-1);
    txtarea.scrollLeft = pYpix+dd+(dd==0?0:(dd>0)?2:-textreafontwidth-1);
    r = (nofstrings(fbeg+(vv==-1?therest:therest.substring(0, vv)),txtarea.clientWidth, textreafontwidth)-0.5)*(textareafontsize+3)-pXpix-txtarea.clientHeight/2;
    txtarea.scrollTop = pXpix+(Math.abs(2*r)<(txtarea.clientHeight-textareafontsize-3)?0:r-(txtarea.clientHeight-textareafontsize-3)/2*(r>0?1:-1));
    return false;
}
function nofstrings(thetext, txtareawidthpix, symbolwidth)
{
    var maxstrlengthallowed = Math.floor(txtareawidthpix/(symbolwidth+1))+1;
    var tt, pp, ppp, tuntil, ii;
    var t2 = thetext.split("\n");
    var s = t2.length;
    for (ii=0; ii<t2.length; ii++)
    {
        tt=t2[ii]+" ";
        pp = 0;
        tuntil = maxstrlengthallowed;
        while(tt.indexOf(" ",pp)!=-1)
        {
            ppp = pp;
            pp = tt.indexOf(" ",pp)+1;
            if (pp>tuntil&&pp-ppp-1)
            {
                tuntil = ppp+maxstrlengthallowed;
                if (pp<tuntil)
                {
                    pp = ppp;
                }
                s++;
            }
        }
    }
    return s;
}
function fulltime() {
var timeStr, dateStr, ostStr, x; 
    
    var time=new Date();
    var newYear=new Date("jan,01,2009,00:00:00");
    var totalRemains=(newYear.getTime()-time.getTime());

        now= new Date(); 
        ex = new Date(2009,0,1,0,0,0); 

    if (totalRemains>1){
    ostStr= ""; 
        x = (ex.getTime() - now.getTime())/1000;   
        ostStr = Math.floor(x/60/60) + ':'; 
        ostStr = ostStr + Math.floor((x/60/60 - Math.floor(x/60/60))*60) + ':'; 
        x = (((x/60/60 - Math.floor(x/60/60))*60) - Math.floor((x/60/60 - Math.floor(x/60/60))*60))*60; 
        ostStr = ostStr + Math.floor(x) + ':'; 
        x = (x - Math.floor(x))*10;
        ostStr = ostStr + Math.floor(x);
    document.getElementById("countdown").innerHTML='До Нового года осталось '+ostStr;
    var perc = 101 - (newYear.getTime() / time.getTime());
    document.getElementById("countdown2").innerHTML='Мы прожили '+perc.toFixed(9)+'% от 2008 года';
    setTimeout('fulltime()',200) 
    } else{
//	document.getElementById("till").innerHTML="C новым 2009-м годом!"; 
    }
}