// --------------------
// Общие функции
// --------------------
function findobj(n, d) { 
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findobj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

// --------------------
// Вставка после поиска в попап-окне
// --------------------

function paste(into,value,text) {
	var field = window.opener ? findobj(into,window.opener.document) : findobj(into);
	var field_name = window.opener ? findobj(into+"_name",window.opener.document) : findobj(into+"_name");
	if(field) field.value = value;
	if(field_name) field_name.value = text;
	if(window.opener) self.close();
}

// --------------------
// Открытие окна
// --------------------

function popup(w,h,t,l) {
	if(!url) return false;
	w = parseInt(w);
	h = parseInt(h);
	if(!w) w=600;
	if(!h) h=400;
	if(!t) t=screen.height/2.5-h/2;
	if(!l) l=screen.width/2.5-w/2;
	href = url.href ? url.href : 
	window.open('',img,'resizable=yes,scrollbars=no,width='+w+',height='+h+',top='+t+',left='+l);
}


function openchat() {
        window.open("/ch/","chatwin","resizable=yes,scrollbars=yes,width=640,height=460,top=40,left=20");
}

function win(target,w,h) {
	var l=(window.screen.width-w)/2;
	if(l<0) {
		l=0;
		w=window.screen.width;
	}
	var t=(window.screen.height-h)/2;
	if(t<0) {
		t=0;
		h=window.screen.height;
	}
	if(window.opener) { l+=20; t+=20; }
	window.open('',target,'resizable=yes,scrollbars=yes,width='+w+',height='+h+',top='+t+',left='+l);
}

function mess(id) {
	win(id,'500','400');
}

function mhist() {
	win('mhist','500','400');
}


function showimage(img,w,h) {
	w = parseInt(w); 
	h = parseInt(h);
	//		if(!w || w>700) w=660;
	//		if(!h || h>550) h=515;
	w+=4;h+=52;
	t=screen.height/2.5-h/2;
	l=screen.width/2.5-w/2;
	window.open('',img,'resizable=yes,scrollbars=no,width='+w+',height='+h+',top='+t+',left='+l);
}

// --------------------
// Открыть/закрыть секцию
// --------------------

function hideshow(id) { 
	obj=findobj("b"+id);
	img=findobj("im"+id);
	if(obj.style) obj=obj.style;
    if(obj.display=="none" || !obj.display) {
		obj.display = "block";
		img.src = "/_design/img/minus.gif";
    } else {
		obj.display = "none";
		img.src = "/_design/img/plus.gif";
    }
}
with(document) {
	var bV = parseInt(navigator.appVersion);
	//document.write("-"+bV);
	var hide = ((bV >= 4) 
	// && (navigator.appName.indexOf('Netscape') == -1)
	&& (navigator.userAgent.indexOf('Opera') == -1)) ? true: false;
	//document.write("-"+navigator.appName);
	write("<STYLE TYPE='text/css'>");
	if(hide) {
		write(".parent {text-decoration: none; position: relative;}");
		write(".child {text-decoration: none; display: none;}");
	}
	write("</STYLE>");
}

function showobj(obj,show) {
	//alert(obj);
	obj = findobj(obj);
	if(obj.style) obj=obj.style;
	if(show) {
		obj.display = "block";
  	} else {
		obj.display = "none";
  	}
}

function boxcollaps(id) { 
	obj=findobj(id);
	img=findobj("box_im"+id);
	if(obj.style) obj=obj.style;
  if(obj.display=="none" || !obj.display) {
		obj.display = "block";
		img.src = "/_design/img/box_hide.gif";
  } else {
		obj.display = "none";
		img.src = "/_design/img/box_show.gif";
  }
}

function changedef(input) {
	input.value = input.value.substring(input.value.indexOf('*')+1,input.value.length);
}

// --------------------
// Работа с датами в панели назначений
// --------------------

var asscurrec = null;

function setassdate(form,id) {
	var tmp,idx1,idx2,tmp1;
	if(!id) {
		tmp1=eval('document.'+form+'.elements["assfrom[year]"]');
		tmp1.value = '';
		tmp1.disabled = true;
		tmp1=eval('document.'+form+'.elements["assfrom[month]"]');
		tmp1.value = '';
		tmp1.disabled = true;
		tmp1=eval('document.'+form+'.elements["assfrom[day]"]');
		tmp1.value = '';
		tmp1.disabled = true;
		tmp1=eval('document.'+form+'.elements["assto[year]"]');
		tmp1.value = '';
		tmp1.disabled = true;
		tmp1=eval('document.'+form+'.elements["assto[month]"]');
		tmp1.value = '';
		tmp1.disabled = true;
		tmp1=eval('document.'+form+'.elements["assto[day]"]');
		tmp1.value = '';
		tmp1.disabled = true;
		return;
	}
	
	tmp=eval('document.'+form+'.elements["datefrom['+id+']"].value');
	idx1 = tmp.indexOf('-');
	tmp1=eval('document.'+form+'.elements["assfrom[year]"]');
	tmp1.value=tmp.substring(0,idx1);
	tmp1.disabled = false;
	idx2 = tmp.indexOf('-',idx1+1);
	tmp1=eval('document.'+form+'.elements["assfrom[month]"]');
	tmp1.value=tmp.substring(idx1+1,idx2);
	tmp1.disabled = false;
	tmp1=eval('document.'+form+'.elements["assfrom[day]"]');
	tmp1.value=tmp.substring(idx2+1,tmp.length);
	tmp1.disabled = false;

	tmp=eval('document.'+form+'.elements["dateto['+id+']"].value');
	idx1 = tmp.indexOf('-');
	tmp1=eval('document.'+form+'.elements["assto[year]"]');
	tmp1.value=tmp.substring(0,idx1);
	tmp1.disabled = false;
	idx2 = tmp.indexOf('-',idx1+1);
	tmp1=eval('document.'+form+'.elements["assto[month]"]');
	tmp1.value=tmp.substring(idx1+1,idx2);
	tmp1.disabled = false;
	tmp1=eval('document.'+form+'.elements["assto[day]"]');
	tmp1.value=tmp.substring(idx2+1,tmp.length);
	tmp1.disabled = false;

	asscurrec = id;
}

function changecurrec(form) {
	if(!asscurrec) return;
	var tmp;
	tmp=eval('document.'+form+'.elements["assigntable[]"]');
	tmp=tmp.options[tmp.selectedIndex];
	var yf = eval('document.'+form+'.elements["assfrom[year]"].value');
	yf = yf > 1000 ? yf.substring(2,4) : yf;
	var yt = eval('document.'+form+'.elements["assto[year]"].value');
	yt = yt > 1000 ? yt.substring(2,4) : yt;
	tmp.text = tmp.text.indexOf('/') < 0 ? tmp.text+' ' : tmp.text.substring(0,tmp.text.indexOf('/'));
	tmp.text = tmp.text
		+ "/"
		+ eval('document.'+form+'.elements["assfrom[day]"].value')
		+ "."
		+ eval('document.'+form+'.elements["assfrom[month]"].value')
		+ "."
		+ yf
		+ "-"
		+ eval('document.'+form+'.elements["assto[day]"].value')
		+ "."
		+ eval('document.'+form+'.elements["assto[month]"].value')
		+ "."
		+ yt
		+ "/";
	tmp.style.color = '#0000FF';
	
	tmp=eval('document.'+form+'.elements["datefrom['+asscurrec+']"]');
	tmp.value =
		eval('document.'+form+'.elements["assfrom[year]"].value')
		+ '-' + eval('document.'+form+'.elements["assfrom[month]"].value')
		+ '-' + eval('document.'+form+'.elements["assfrom[day]"].value');
	tmp=eval('document.'+form+'.elements["dateto['+asscurrec+']"]');
	tmp.value =
		eval('document.'+form+'.elements["assto[year]"].value')
		+ '-' + eval('document.'+form+'.elements["assto[month]"].value')
		+ '-' + eval('document.'+form+'.elements["assto[day]"].value');	
	
	tmp=eval('document.'+form+'.elements["datechanged['+asscurrec+']"]');
	tmp.value = 1;
}


// --------------------
// Элемент дата/время
// -------------------

		var val="";
		var caretPos="";
		var lock=false;

		function dbox_fcs(input) {
			val=input.value;
			input.select();
		}
		
		function dbox_kdown(input) {
			if(lock) return false;
			lock=true;
			//IE support
			if (document.selection && document.selection.createRange)  {
				caretPos = document.selection.createRange().duplicate();
			}
			val=input.value;	
		}
		
function check(str,chars) {
  var ok = true;
  for (var i = 0; i < str.length; i++) {
    var chr = str.charAt(i);
    var found = false;
    for (var j = 0; j < chars.length; j++) {
      if(chr == chars.charAt(j)) {
				found = true;
				break;
			}
    }
    if (!found) { ok = false; break; }
  }
  return ok;
}

		function dbox_clear(form,name) {
			var arr=new Array();
			arr[0]=eval('document.'+form+'.day'+name);
			arr[1]=eval('document.'+form+'.month'+name);
			arr[2]=eval('document.'+form+'.year'+name);
			arr[3]=eval('document.'+form+'.hour'+name);
			arr[4]=eval('document.'+form+'.min'+name);
			arr[5]=eval('document.'+form+'.sec'+name);
			for(var i=0;i<6;i++) if(arr[i]) arr[i].value='';
		}


		// 10 - day, 20 - month, 30 - year, 40 - hour, 50 - min, 60 - sec
		function dbox_kup(input,form,name,type) {
			lock=false;
			var t,next,prev;
			switch(type) {
				case 10 : next='month';prev='';break;
				case 20 : next='year';prev='day';break;
				case 30 : next='hour';prev='month';break;
				case 40 : next='min';prev='year';break;
				case 50 : next='sec';prev='hour';break;
				case 60 : next='';prev='min';break;			
				default : next='';prev='';
			}
			if(next) next=eval(form+'.'+next+name);
			if(prev) prev=eval(form+'.'+prev+name);
			
			if (document.selection && document.selection.createRange)  {
				var is_bound = document.selection.createRange().duplicate().offsetLeft == caretPos.offsetLeft;
			}	
			
			if(is_bound && event.keyCode==39 && next) {
				next.select();
				next.focus();
			}
			if((is_bound && event.keyCode==37 || val=='' && event.keyCode==8) && prev) {
				prev.select();
				prev.focus();
			}

			if(input.value==val) return;
			
			if(!check(input.value,'0123456789')) {
				caretPos.text='';
				return;
			}

			var value=input.value;
			if(input.value=="") val="";	
			if(type==10 && (value>3 || input.value.length==2)) {
				if(value>31) input.value=31;
				if(next) { next.select();next.focus(); }
			}	else
			if(type==20 && (value>1 || input.value.length==2)) {
				if(value>12) input.value=12;
				if(next) { next.select();next.focus(); }
			}	else
			if(type==30 && (value!=19 && value!=20 && input.value.length==2 || input.value.length==4)) {
				if(input.value.length!=4) input.value=value<60 ? "20"+input.value : "19"+input.value;
				if(next) { next.select();next.focus(); }
			}
		}

		function ndbox_clear(form,name) {
			var arr=new Array();
			arr[0]=eval('document.'+form+'.elements["'+name+'['+'day'+']"]');
			arr[1]=eval('document.'+form+'.elements["'+name+'['+'month'+']"]');
			arr[2]=eval('document.'+form+'.elements["'+name+'['+'year'+']"]');
			arr[3]=eval('document.'+form+'.elements["'+name+'['+'hour'+']"]');
			arr[4]=eval('document.'+form+'.elements["'+name+'['+'min'+']"]');
			arr[5]=eval('document.'+form+'.elements["'+name+'['+'sec'+']"]');
			for(var i=0;i<6;i++) if(arr[i]) arr[i].value='';
			changecurrec(form);
		}


		function ndbox_kup(input,form,name,type) {
			lock=false;
			var t,next,prev;
			switch(type) {
				case 'day' : next='month';prev='';break;
				case 'month' : next='year';prev='day';break;
				case 'year' : next='hour';prev='month';break;
				case 'hour' : next='min';prev='year';break;
				case 'min' : next='sec';prev='hour';break;
				case 'sec' : next='';prev='min';break;
				default : next='';prev='';
			}

			if(next) next=eval('document.'+form+'.elements["'+name+'['+next+']"]');
			if(prev) prev=eval('document.'+form+'.elements["'+name+'['+prev+']"]');
			
			if (document.selection && document.selection.createRange)  {
				var is_bound = document.selection.createRange().duplicate().offsetLeft == caretPos.offsetLeft;
			
				if(is_bound && event.keyCode==39 && next) {
					next.select();
					next.focus();
				}
				if((is_bound && event.keyCode==37 || val=='' && event.keyCode==8) && prev) {
					prev.select();
					prev.focus();
				}
			}
			if(input.value==val) return;
			
			if(!check(input.value,'0123456789')) {
				if(caretPos) caretPos.text='';
				else input.value=val;
				return;
			}

			var value=input.value;
			if(input.value=="") val="";	
			if(type=='day' && (value>3 || input.value.length==2)) {
				if(value>31) input.value=31;
				if(next) { next.select();next.focus(); }
			}	else
			if(type=='month' && (value>1 || input.value.length==2)) {
				if(value>12) input.value=12;
				if(next) { next.select();next.focus(); }
			}	else
			if(type=='year' && (value!=19 && value!=20 && input.value.length==2 || input.value.length==4)) {
				if(input.value.length!=4) input.value=value<60 ? "20"+input.value : "19"+input.value;
				if(next) { next.select();next.focus(); }
			}
		}
		


function markasread(img) {
		document[img].src='/_design/img/oldmessage.gif';
}
		
		
function dateSelect(from, to, varname, v) {
	document.write('<SELECT NAME='+varname+' class=small>');
	for (i=from; i<=to; i++) {
		var ii = i >= 10 ? i : '0'+i;
		var selected = ii == v ? 'selected' : '';
		document.write('<option value="'+ii+'" ' + selected + '>'+ii+'</option>');
	}
	document.write('</SELECT>');
}

function domDetect(obj) {
	return (typeof(window.opera) == 'undefined' && typeof(obj.getAttribute) != 'undefined');
}

function cellColor(theCell,color) {
	if(domDetect(theCell)) {
	 	theCell.setAttribute('bgcolor', color, 0);
	} else {
	 	theCell.style.backgroundColor = color;
	}
}

function tclick(obj) {
	if(obj.children.tags('A')[0]) obj.children.tags('A')[0].click();
}

var marked_row = Array();

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

//    alert(currentColor);

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

//    alert(newColor);
    
    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
};

