var tinyMCEmode = false;
var doLoadForm = false;
var unsetClass = false;
var reloadAfter = false; 

if (document.getElementById) {
		document.onkeydown = escapekey;
}

function func(){}  

function escapekey(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if ((code == 27) && (e.ctrlKey == false) && (e.altKey == false)) document.getElementById('Ausweis').style.display='none';
}

function doLoad(path, arr, elm){
	if (!path) return;
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
			if (req.responseJS) {
				if (!elm || elm=='none' || !$(elm))
					elm = 'result';
				if (doLoadForm) {
					$(elm).value = req.responseJS.wf_main;
					doLoadForm = false;
				} else {
                    if ($(elm)){
                        $(elm).update(req.responseJS.wf_main);
                        new Element.show(elm);
                    }
                    if (elm == 'popUpContent' && popUpJustify == true){
                        var dimensions = $(elm).parentNode.getDimensions();
                        $(elm).parentNode.style.marginLeft = (dimensions.width/2)*(-1);
                        $(elm).parentNode.style.marginTop = (dimensions.height/2)*(-1);
                        popUpJustify = false;
                    }
                }
                if (req.responseJS.iface_title)
                    $('popUpTitle').update(req.responseJS.iface_title);
				if (tinyMCEmode == true)
					tinyMCE.updateContent(elm);
                if (unsetClass){
                    $(unsetClass).className = '';
                    unsetClass = false;
                }
                if (reloadAfter)
                	document.location.reload();
	      	}
	      	if (req.responseText.length > 0)
				alert(req.responseText);
	    }
	}
	req.caching = false;
	req.open('POST', path, true);
	req.send(arr);
}

function popUp(){
    if ($('popUpShell'))
        popUpClose();
    new Insertion.Bottom(document.body, "<div class=popUpShell id=popUpShell></div>");
    new Insertion.Top("popUpShell", "<div class=popUp id=popUp></div>");
    new Insertion.Top("popUp", "<div class=iface align=right id=popUpIface><h1 id=popUpTitle></h1><input type=image src=/pic/iface/close.gif width=21 height=21 onClick=popUpClose('popUp') title='Закрыть'/></div><div id=popUpContent></div>");
    new Insertion.Bottom("popUp", "<div id=popUpIface2 class=iface align=right><img src=/pic/iface/small_move.gif width=11 height=11 alt=''></div>");
    popUpJustify = true;
    new Draggable('popUp', {'handle':'popUpIface'});
    new Draggable('popUp', {'handle':'popUpIface2'});
}

function popUpClose(){
    if ($('popUpShell'))
        $('popUpShell').remove();
}

/*function doLoad(path,arr,elm){
	if (!path) return;
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
			if (req.responseJS) {
				if (!elm || elm=='none' || !$(elm)) elm = 'result';
				if (doLoadForm) {
					$(elm).value = req.responseJS.wf_main;
					doLoadForm = false;
				} else
					$(elm).innerHTML = req.responseJS.wf_main;
				if (tinyMCEmode == true)
					tinyMCE.updateContent(elm);
	      	}
	      	if (req.responseText.length > 0)
				alert(req.responseText);        
	    }
	}
	req.caching = false;
	req.open('POST', path, true);
	req.send(arr);
}

function popUp(path,arr){
	new Element.show('popUpShell');
	new Draggable('popUp',{'handle':'popUpIface'});				
	new Draggable('popUp',{'handle':'popUpIface2'});	
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS) {
				$('popUpContent').innerHTML = req.responseJS.wf_main;
        		$('popUpTitle').innerHTML = req.responseJS.iface_title;
	    	}
			if (req.responseText.length > 0)
				alert(req.responseText);
	  	}
	}
	req.caching = false;
	req.open('POST', path, true);
	req.send(arr);
}

function popUpClose(id){
	new Element.hide('popUpShell');
}*/

function checkForm (form){
	var str = '';
	for (var i=0; i<form.elements.length; i++){
		el = form.elements[i];
		str += el.type + ' ';
		switch (el.type){
			case "text":
			case "textarea":
			case "hidden":
			case "password":
			case "button":
				str += el.name + '=' + el.value;
				break;
			case "select-one":
				str += el.name + '=' + el.options[el.selectedIndex].value;
				break;				
			case "select-multiple":
				for (var j=0; j<el.length; j++){
					if (el.options[j].selected) str += el.options[j].value + '|';
				}
				break;				
		}
		str += '\n';
	}
	alert(str);
}

function formElements2Hash(formElements){
    var hash = new Object();
    for (var i=0; i<formElements.length; i++){
    	if (isArray(formElements[i])) formElements2Hash(formElements[i]);
		else {
			if (!formElements[i].name) continue;
      		hash[formElements[i].name] = formElements[i].value;
			//alert(formElements[i].name + "=" + formElements[i].value);
		}
    }
    return hash;
}

function form2hash(form){
    var hash = new Object();
    for (var i=0; i<form.elements.length; i++){
		el = form.elements[i];
		if (!el.name) continue;
		switch (el.type){
			case "text":
			case "textarea":
			case "hidden":
			case "password":
			case "button":
				hash[el.name] = el.value;
				break;
			case "checkbox":
			case "radio":
			 	if (el.checked)
					hash[el.name] = el.value;
				break;
			case "select-one":
				hash[el.name] = el.options[el.selectedIndex].value;
				break;
			case "select-multiple":
				hash[el.name] = {};
				for (var j=0; j<el.length; j++){
					if (el.options[j].selected)
						hash[el.name][j] = el.options[j].value;
				}
				break;				
		}
    }
	/*alert(Dump(hash));*/
    return hash;
}

function Dump(d,l) {
    if (l == null) l = 1;
    var s = '';
    if (typeof(d) == "object") {
        s += typeof(d) + " {\n";
        for (var k in d) {
            for (var i=0; i<l; i++) s += "  ";
            s += k+": " + Dump(d[k],l+1);
        }
        for (var i=0; i<l-1; i++) s += "  ";
        s += "}\n"
    } else {
        s += "" + d + "\n";
    }
    return s;
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function new_win(w,h,n)	{
	var n;
	if (!n) n="new_window";	
 	w1=window.open("",n,"resizable=yes, menubar=no, status=yes, scrollbars=yes, width="+w+", height="+h);
	w1.focus();
}

function Ausweis(id){
	var id;
  elm=document.getElementById(id);
  if (elm){
		if (elm.style.display == 'block') elm.style.display='none';
    else elm.style.display='block';
  }
}

function count(what,num){
 if (what.length > num) {
  	alert('Допускается не более '+num+' символов');
    return false;
  }
  return true;
}

function submit1() {
    document.passwordForm.password.focus();
    return false;
}

function submit2() {
    document.passwordForm.login.value = document.loginForm.login.value;
    /* perform any validation here */
    return true;
}

/* дерево */
var cImg = new Image();
cImg.src = '/pic/plus.gif';
var eImg = new Image();
eImg.src = '/pic/minus.gif';	
var ssel = false;
	
function toggleState(node) {
	var node;
  if(ssel) resetAll(node);
  doToggle(node);
}

function toggleGroup(str){
	var arr = str.split(',');
	for(var i = 0; i < arr.length; i++){
		toggleState(arr[i]);
	}
}
	
function doToggle(node, reset) {
  var n;
	var node;
  var n = document.getElementById(node);
	if (n){
  	/*m = document.getElementById(ode);*/
    if(!reset) reset = "inline";
    n.style.display = (n.style.display!='none'?'none':reset);
  	/*m.style.background = (n.style.display=="inline"?'url(/pic/d2/item1_abg.gif)':'url(/pic/d2/item1_bg.gif)');*/
  	document.images[node+'Img'].src = (n.style.display=="inline"?eImg.src:cImg.src);
	}
}
	
function resetAll(node) {
  var node;
  var n;
  var nIdx = getIdx(node);
  /*var d = document.getElementsByTagName("img");*/
  var n = document.getElementById(node);
	if (n){
    if(n.style.display=="none") {
    	for(var i=0; i<d.length; i++)
    		if(d[i].onclick) {
    			node = d[i].onclick.toString();
    			node = node.substr(node.lastIndexOf("(")+2);
    			node = node.substr(0, node.lastIndexOf(")")-1);
    			if(getIdx(node)==nIdx) doToggle(node, "none");
    		}
    }
	}
}
	
function getIdx(node) {
  var i = 0;
  var n = document.getElementById(node);
  if(n)
  	while(n.parentNode) {
  		n = n.parentNode;
  		i++;
  	}	
  return i;
}

function toogleMCE(ID) {
    try {
        if(tinyMCEmode==true) {
            tinyMCE.execCommand('mceRemoveControl',false,ID);
			//tinyMCE.removeMCEControl(tinyMCE.getEditorId(ID));
            tinyMCEmode = false;
        } else {
			TinyMCE.idCounter = 0;
			var inst = tinyMCE.getInstanceById(ID);
			if (inst){
				
            }
			tinyMCE.execCommand('mceAddControl',false,ID);
			//tinyMCE.addMCEControl(document.getElementById(ID), ID);
            tinyMCEmode = true;
        }
    } catch(e) {
        //error handling
		alert('Ошибка при попытке переключения режима редактора.\n tinyMCEmode='+tinyMCEmode+'\n TinyMCE.idCounter='+TinyMCE.idCounter+'\nInstance='+tinyMCE.getInstanceById(ID)+'\n'+e);
    }
}

function submitWarn(id){
	if ($(id)) $(id).style.border = '2px solid red';
}
function submitWarnCB(inst){
	if ($('submit')) $('submit').style.border = '2px solid red';
}





