//c http://blog.stevenlevithan.com/archives/faster-trim-javascript
function trim(str){
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
}

function cv(obj,cl){
if(cl === undefined)
    cl='closed';
var q = obj.parentNode;
if ((' '+q.className+' ').indexOf(' '+cl+' ')==-1)
    q.className=trim(q.className+' '+cl);
else
    q.className=trim(q.className.replace(cl,''));
}

function addClass(obj,cl){
var q = obj.parentNode;
if ((' '+q.className+' ').indexOf(' '+cl+' ')==-1)
    q.className=trim(q.className+' '+cl);
}

function remClass(obj,cl){
var q = obj.parentNode;
if ((' '+q.className+' ').indexOf(' '+cl+' ')!=-1)
    q.className=trim(q.className.replace(cl,''));
}

//hide div if checkbox=[ ]
function cv2(checkbox,divs){
var s;
if(document.getElementById(checkbox).checked)
    s='block';
else
    s='none';
if(typeof(divs)=='string')
    divs=Array(divs);
for(q in divs)
    document.getElementById(divs[q]).style.display=s;
}

// c http://www.somacon.com/p143.php
function getCheckedValue(radioObj) {
    if(!radioObj)
	return "e1";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
	if(radioObj.checked)
	    return radioObj.value;
	else
	    return "e2";
    for(var i = 0; i < radioLength; i++)
	if(radioObj[i].checked)
	    return radioObj[i].value;
    return "";
}

//hide divs if radio=0
function cv3(radio,divs){
var s;
if(getCheckedValue(document.forms['EditForm'].elements[radio])=='0')
    s='none';
else
    s='block';
if(typeof(divs)=='string')
    divs=Array(divs);
for(q in divs)
    document.getElementById(divs[q]).style.display=s;
}

//show divs[radio]
function cv4(radio,divs){
if(typeof(divs)=='string')
    divs=Array(divs);
for(q in divs)
    {
    if(typeof(divs[q])=='string')
	divs[q]=Array(divs[q]);
    for(w in divs[q])
	document.getElementById(divs[q][w]).style.display='none';
    }
q=getCheckedValue(document.forms['EditForm'].elements[radio]);
for(w in divs[q])
	document.getElementById(divs[q][w]).style.display='block';
}

//cv for tag t with class c ('none'<=> s)
function cv_tag_class(t,c,s)
{
v=document.getElementsByTagName(t);
for(q in v)
    if(v[q].className==c)
	v[q].style.display=(v[q].style.display=='none')?s:'none';
}






//c http://pajhome.org.uk/crypt/md5
function md5(s){ return hex_md5(s) }

function pass_hash(){
if(document.getElementById('login_edit').checked)
    {
    var str;
    str=document.getElementById('login_name').value+':phpMyID:'+document.getElementById('login_pass').value;
    document.getElementById('login_hash').value=md5(str);
    str=document.getElementById('login_name_old').value+':phpMyID:'+document.getElementById('login_pass_old').value;
    document.getElementById('login_hash_old').value=md5(str);
    str=document.getElementById('del_name').value+':phpMyID:'+document.getElementById('del_pass').value;
    document.getElementById('del_hash').value=md5(str);
    }
document.getElementById('login_pass').value='';
document.getElementById('login_pass_old').value='';
document.getElementById('del_pass').value='';
}







function abs2fix(){
if(navigator.userAgent.indexOf('Opera Mini')!=-1)
    return;
var t=document.getElementById('cols').offsetTop;
var i=document.getElementById('side');
if(navigator.userAgent.search('MSIE')==-1)
    {
    i.style.position='fixed';
    i.style.top=t;
    }
var li=i.getElementsByTagName('li');
var a;
var q;
for(q in li)
    {
    a=li[q];
    if(typeof a=='object')
	{
	if(a.className=='box')
	    {
	    //~ alert(a.firstChild);
	    addClass(a.firstChild,'closed');
	    //~ a.onmouseover="remClass(this.firstChild,'closed')";
	    //~ a.onmouseover="alert(this.firstChild)";
	    //~ a.onmouseout="addClass(this.firstChild,'closed')";
	    //~ a.onmouseout="alert(this.firstChild)";
	    }
	}
    }
ajax(putmenu,'map.php?ajax=1');
ajax(putnews,'/news.php?ajax=1');
}

function putmenu(text){
var m=document.getElementById('menu');
m.innerHTML=text;
var li=m.getElementsByTagName('li');
var a;
var q;
for(q in li)
    {
    a=li[q];
    if(typeof a=='object')
	{
	a=a.getElementsByTagName('a')[0];
	if(location.href.substr(0,a.href.length)!=a.href)
	    addClass(a,'closed');
	else
	    addClass(a.firstChild,'sel');
	}
    }
li=m.getElementsByTagName('span');
for(q in li)
    {
    a=li[q];
    if(typeof a=='object')
	a.onclick="cv(this)";
    }
}

function putnews(text){
var l=text.split('fcb4713f9f59b46d58e52d047b4dbeef');
document.getElementById('news0').innerHTML=l[0];
document.getElementById('news1').innerHTML=l[1];
document.getElementById('news2').innerHTML=l[2];
}






function toggleAs(){
if(document.getElementById('text_use').checked && document.getElementById('AS').checked)
    {
    AS_int=setInterval('as("1")',10000);
    document.getElementById('AStext').innerHTML='Последнее сохранение:';
    as('1');
    }
else
    {
    clearInterval(AS_int);
    document.getElementById('AStext').innerHTML='';
    document.getElementById('AStime').innerHTML='';
    }
}

function as(sv){
var m=getCheckedValue(document.forms['EditForm'].elements['text_mode']);
var text='';
switch(m)
    {
    case '0':
	text=document.getElementById('text_html').value;
    break;
    case '1':
    case '2':
	text=document.getElementById('text_text').value;
    break;
    case '3':
	text=tinyMCE.activeEditor.getContent();
    break;
    }
ajaxPost(asCb,'edit2.php','ajax=1&text_mode='+m+'&save='+sv+'&text='+encodeURIComponent(text));
}

function asCb(t){
document.getElementById('preview').innerHTML=t;
if(getCheckedValue(document.forms['EditForm'].elements['text_mode'])!=0)
    document.getElementById('text_html').value=t;
if(document.getElementById('text_use').checked && document.getElementById('AS').checked)
    document.getElementById('AStime').innerHTML=(new Date()).toLocaleTimeString();
else
    clearInterval(AS_int);
}




//c http://www.mredkj.com/tutorials/tableaddrow.html
function addRowToTable()
{
var tbl = document.getElementById('tbl');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(tbl.rows.length);

//path
var cell1 = row.insertCell(0);
var el1 = document.createElement('input');
el1.type = 'text';
el1.name = 'path[]';
cell1.appendChild(el1);

//to
var cell2 = row.insertCell(1);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'to[]';
cell2.appendChild(el2);

//perm
var cell3 = row.insertCell(2);
var el3 = document.createElement('input');
el3.type = 'checkbox';
el3.name = 'perm[]';
cell3.appendChild(el3);

//del
var cell4 = row.insertCell(3);
var el4 = document.createElement('input');
el4.type = 'button';
el4.value = '-';
el4.onclick = "delRowFromTable(this)";
cell4.appendChild(el4);

}

// c http://www.mredkj.com/tutorials/tabledeleterow.html
function delRowFromTable(btn)
{
var delRow = btn.parentNode.parentNode;
var tbl = delRow.parentNode;
var rIndex = delRow.sectionRowIndex;
tbl.deleteRow(rIndex);
}








function findXHR(){
    try{ return new XMLHttpRequest() }catch(e){}
    try{ return new ActiveXObject("MSXML3.XMLHTTP") }catch(e){}
    try{ return new ActiveXObject("MSXML2.XMLHTTP.3.0") }catch(e){}
    try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
    try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
    return null;
}

function ajax(who,where){
var XHR=findXHR();
if (!XHR)
    return;
XHR.onreadystatechange=function()
    {
    if(XHR.readyState==4)
	who(XHR.responseText);
    }
XHR.open("GET",where,true);
XHR.send(null);
}

function ajaxPost(who,where,params){
var XHR=findXHR();
if (!XHR)
    return;
XHR.onreadystatechange=function()
    {
    if(XHR.readyState==4)
	who(XHR.responseText);
    }
XHR.open("POST",where,true);
XHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XHR.setRequestHeader("Content-length", params.length);
XHR.setRequestHeader("Connection", "close");
XHR.send(params);
}



function checkTinyMCE(radio){
var val=getCheckedValue(document.forms['EditForm'].elements[radio]);
if(val==3)
    {
    document.getElementById('text_tinyMCE').innerHTML=document.getElementById('text_html').innerHTML;
    if(window.tinyMCELoaded == null)
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : "text_tinyMCE",
		theme : "advanced",
		relative_urls : false,
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example word content CSS (should be your site CSS) this one removes paragraph margins
		content_css : "/tiny_mce/word.css"

		// Drop lists for link/image/media/template dialogs
		//~ template_external_list_url : "lists/template_list.js",
		//~ external_link_list_url : "lists/link_list.js",
		//~ external_image_list_url : "lists/image_list.js",
		//~ media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		//~ template_replace_values : {
			//~ username : "Some User",
			//~ staffid : "991234"
		//~ }
	});
    window.tinyMCELoaded=1;
    }
if(val==0)
    document.getElementById('text_html').innerHTML=tinyMCE.activeEditor.getContent();
}

function checkopenid(){
var e=document.getElementById('openid');
if(e.value=='http://')
    {
    e.value+=location.host;
    setTimeout("document.forms['login'].submit()", 500);
    return false;
    }
return true;
}
