function select_all(obj,name) {
	if( !name ) name = false;
	var f = obj.form;
	for( var i = 0; i < f.length; i ++ ) {
		if( f[i].type != 'checkbox' ) continue;
		if( f[i] == obj ) continue;
		if( name && !name.test( f[i].name ) ) continue;
		f[i].checked = obj.checked;
	}
}



function win( url, w, h, resize ) {
	if( !w ) w = 500;
	if( !h ) h = 400

	w = parseInt( w );
	h = parseInt( h );

	var aw = screen.availWidth;
	var ah = screen.availHeight;
	if( w > aw ) w = aw;
	if( h > ah ) h = ah;

	if( !resize || !/^[01]$/.test( resize.toString() ) ) {
		resize = 1;
	}

	var l = Math.round( ( aw - w ) / 2 );
	var t = Math.round( ( ah - h ) / 2 );

	window.open( url, "_blank", 'channelmode=0, directories=0, height='+h+'px, width='+w+'px, location=0, menubar=0, resizable='+resize+', scrollbars=1, status=0, toolbar=0, top='+t+'px, left='+l+'px' );
}

function newwin( url ) {
	return window.open( url, '_blank', 'directories,location,menubar,scrollbars,status,titlebar,title,toolbar,resizable=1' );
}

function go( url, hard ) {
	if( !hard ) hard = 0;
	if( hard ) {
		location.replace( url );
		return;
	}
	location.href = url;
}

function load_in( url, name ) {
	if( !name ) name = 'main';
	if( self.name == name ) {
		location.href = url;
		return false;
	}
	var father = window;
	for( var i = 0; i < 100; i++ ) {
		father = father.opener;
		if( father && typeof father.name == 'string' ) {
			if( father.name == name ) {
				father.location.href = url;
				return;
			}
		}
		else {
			break;
		}
	}
	window.open( url, name );
}


function create_element( type, father ) {
	if( !father ) father = document.body;
	var newElem = document.createElement(type);
	father.appendChild(newElem);
	return newElem;
}


function create_context_menu( items ) {
	ret = '';
	ret += '<table border="0" cellpadding="0" cellspacing="2" bgcolor="window"';
	ret += '	style="border: solid 1px gray;">';
	ret += '	<tr>';
	ret += '	<td>';
	ret += '	<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" bgcolor="window" onselectstart="return false;">';
	ret += '';
	for( i = 0; i < items.length; i ++ ) {
		name = items[i][0];
		action = items[i][1];
		if( /^sep[0-9]+$/.test( name ) ) {
			ret += '';
			ret += '<tr><td height="3" style="cursor: default; border-bottom: solid 1px silver; font-size: 2px;">&nbsp;</td></tr>';
			ret += '<tr><td height="3" style="cursor: default;"></td></tr>';
			continue;
		}
		ret += '';
		ret += '<tr>';
		ret += '	<td height="17" style="font: menu; background-color: window; color: windowtext;  padding: 0px 17px; cursor: default; white-space: nowrap;"';
		ret += '		onmouseover="this.style.color = \'highlighttext\'; this.style.backgroundColor = \'highlight\';"';
		ret += '		onmouseout="this.style.color = \'windowtext\'; this.style.backgroundColor = \'window\';"';
		ret += '		onclick="' + action + '">';
		ret += '		' + name + '';
		ret += '	</td>';
		ret += '</tr>';
	}
	ret += '';
	ret += '	</table>';
	ret += '	</td>';
	ret += '	</tr>';
	ret += '</table>';
	
	return ret;
}

function get_admin_menu( uid ) {
	var acm = document.getElementById( "show_adm_cm" );
	if( !acm ) {
		acm = create_element( "SCRIPT", "show_adm_cm" );
	}
	if( acm ) {
		acm.src = "/amenu.js?uid=" + uid + "&xx=" + event.x + "&yy=" + event.y ;
	}
}

var EX_INFO = false;
function get_menu( html, x, y ) {
	height	= document.body.offsetHeight ? document.body.offsetHeight : window.innerHeight;
	width	= document.body.offsetWidth ? document.body.offsetWidth : window.innerWidth;
	
	
	if( !EX_INFO ) {
		EX_INFO = create_element( 'DIV', 'admin_contextmenu' );
		EX_INFO.style.display = "none";
	}
	
	if( !x ) x = 0;
	if( !y ) y = 0;
	var xx = event ? event.x : x;
	var yy = event ? event.y : y;

	if( !EX_INFO ) {
		return;
	}
	EX_INFO.innerHTML =  html;
	EX_INFO.style.position = "absolute";
	EX_INFO.style.top = yy;
	EX_INFO.style.left = xx;
	EX_INFO.style.zIndex = "40";
	EX_INFO.style.textAlign = "center";
	EX_INFO.style.filter = "progid:DXImageTransform.Microsoft.Shadow(direction=135,color=gray,strength=3)";
	EX_INFO.style.display = "";
		
	if( !document.onkeypress ) {
		document.onkeypress = function() {
			if( event.keyCode == 27 ) {
				close_win();
			}
		}
	}
	
	if( !document.mousedown ) {
		document.onmousedown = function() {
			if( !FOCUSED ) {
				close_win();
			}
		}
	}
	
	var FOCUSED = false;
	
	EX_INFO.onmousedown = function() {
		FOCUSED = true;
	}
	
	EX_INFO.onmouseup = function() {
		FOCUSED = false;
	}
}

function close_win() {
	if( !EX_INFO ) return;
	EX_INFO.style.display = "none";
	EX_INFO.innerHTML = "";
	
	document.onkeypress = null;
	document.onmousedown = null;
}


function fullscreen( url ) {
    window.open( url, "_blank", "fullscreen=1, scrollbars=0, toolbar=0, resizable=1" );
}


function alert_r(obj, inline, callcnt) {
	if( !inline ) inline = false;
	if( !callcnt ) callcnt = 0;
	
	var type = typeof obj;
	if( type != 'array' && type != 'object' ) {
		if( inline ) {
			return obj;
		}
		alert(obj);
		return;
	}
	
	var sep = '';
	for( var i = 0; i < callcnt; i ++ ) {
		sep += "\t";
	}
	
	var r = '';
	r += type + " (" + sep + "\n";
	for( var i in obj ) {
		try {
			//r += sep + "\t" + "[" + i + "] = " + alert_r( obj[i], 1, callcnt + 1 ) + "\n";
			r += sep + "\t" + "[" + i + "] = " + obj[i] + "\n";
		}
		catch(e) {}
	}
	r += sep + ")\n";
		
	
	if( inline ) {
		return r;
	}

	alert(r);
}

function print_r(obj, inline) {
	if( !inline ) inline = false;
	var r = '<pre>' + alert_r(obj, 1 ) + '</pre>';
	if( inline ) {
		return r;
	}
	document.write( r );
}



function fill_all(f) {
	var ok = true;
	var foc = false;
	
	for( var i = 0; i < f.length; i ++ ) {
		if( f[i].type != "text" && f[i].type != "textarea" && f[i].type != "select-one" && f[i].type != "file" ) continue;
		
		if( f[i].canskip && f[i].canskip == 1 ) continue;
		
		f[i].style.backgroundColor = "";
		if( f[i].value == "" ) {
			ok = false;
			f[i].style.backgroundColor = "red";
			if( !foc ) foc = f[i];
		}
	}
	
	if( !ok ) {
		try { foc.focus(); }
		catch(e) { return true; }
		
		alert( "Заполните все поля!" );
		return false;
	}
	return true;
}

