
function wopen( url, target, w, h, res ) {
	if( !target ) target = '_blank';
	if( !w ) w = 500;
	if( !h ) h = 400

	w = parseInt( w, 10 );
	h = parseInt( h, 10 );
	

	var aw = screen.availWidth;
	var ah = screen.availHeight;
	if( w > aw ) w = aw;
	if( h > ah ) h = ah;

	if( res )r = 0; else r = 1;

	var left = Math.round( ( aw - w ) / 2 );
	var top = Math.round( ( ah - h ) / 2 );

	var wd = window.open( url, target, 'channelmode= 0, directories=0, height='+h+'px, width='+w+'px, location=0, menubar=0, resizable='+r+', scrollbars=1, status=0, toolbar=0, top='+top+'px, left='+left+'px' );
	
	return wd; 
}

//**************************************************

function go( url, opt, w, h, s, r ){
	if( !url )return false;
	if( opt == 1 ){
		if( !w )w = 700;
		if( !h )h = 400;
		if( !s )s = 1; else s = 0;
		if( !r )r = 1; else r = 0;
		l = ( screen.width - w )/2;
		t = ( screen.height - h )/2;
		
		window.open( url, "_blank", "toolbar=0,scrollbars="+s+",resizable="+r+",left="+l+",top="+t+",width="+w+",height="+h );
	}
	else if( opt == 2 ) {
		window.open( url, "_blank" );
	}
	else if( opt == 3 ){
		var wd = window.open( url, '_blank', 'fullscreen=1, scrollbars=1, toolbar=0, resizable=1' );
	}
	else {
		window.location.href = url;
	}
}

//**************************************************

function main_location( url, name ) {
	if( !name ) name = 'main';
	if( self.name == name ) {
		location.href = url;
		return false;
	}
	father = window;
	for( 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, id, father, add ) {
	if( !father ) father = document.body;
	if( !add ) add = '';
	var newElem = document.createElement(type);
	newElem.id = id;
	if( add != "" ) {
		add = "\
		with( newElem ) {\
			" + add + "\
		}";
		eval( add );
	}
	father.appendChild(newElem);
	return newElem;
}

//**************************************************

function destroy_element( id ) {
	if( /object/.test(typeof id) ) el = id;
	else el = document.getElementById( id );
	if( el ) {
		el.outerHTML = "";
	}
}

//**************************************************

function rand(from,to) {
	return from + Math.round( ( to - from ) * Math.random() );
}

//**************************************************

function alert_r(obj, ret, separator) {
	if( !ret ) ret = false;
	if( !separator ) separator = "\n";
	var r = "";
	for( var i in obj ) {
		r += i + ": " + obj[i] + separator;
	}
	if( ret ) {
		return r;
	}
	alert( r );
}

//**************************************************

function winfull( url, target ) {
	if ( !url ) { return false; }
	var addu = '';
	if ( !target ) { target = '_blank'; }
    var wd = window.open( url+addu, target, 'fullscreen=1, scrollbars=0, toolbar=0, resizable=1' );
}


//*****************************************************

function date( format, time ) {
	if( !time ) time = new Date();
	var ret = format;
	with( time ) {
		ret = ret.replace( /Y/g, getFullYear() );
		ret = ret.replace( /m/g, withnull( getMonth() + 1) );
		ret = ret.replace( /d/g, withnull( getDate() ) );
		ret = ret.replace( /H/g, withnull( getHours() ) );
		ret = ret.replace( /i/g, withnull( getMinutes() ) );
		ret = ret.replace( /s/g, withnull( getSeconds() ) );
		ret = ret.replace( /j/g, getDate() );
		ret = ret.replace( /G/g, getHours() );
	//	ret = ret.replace( /w/g, getDay() );
		ret = ret.replace( /y/g, getFullYear().toString().replace( /^[0-9]{2}([0-9]{2})$/, '$1' ) );
	}
	return ret;
}

//*****************************************************
function trim( txt ) {
	return txt.replace( /^\s+/, '' ).replace( /\s+$/, '' );
}


//*****************************************************

function sel_all( obj, name ) {
	if( !name ) name = false;
	var f = obj.form;
	for( var i = 0; i < f.length; i ++ ) {
		if( f[i] == obj ) continue;
		if( !/checkbox/i.test( f[i].type ) ) continue;
		if( name && f[i].name.replace( /\[[^\[\]]*\]/g, "" ) != name ) continue;
		f[i].checked = obj.checked;
	}
}


function bad_email(email) {
	return email.length < 7 || !/^[~\._a-z0-9-]+@([\._a-z0-9-]{2,}\.[a-z]{2,4})$/i.test( email );
}


function _light(el, str, color) {
	if( typeof el == 'string' ) {
		el = document.getElementById( el );
	}
	if( !el ) return;
	if( !color ) color = 'yellow';
	
	var tmp = el.innerHTML;
	var reg = new RegExp( '(' + preg_quote( str ) + ')', 'gi' );
	
	tmp = tmp.replace( reg, '<b style="background-color: '+color+';">$1</b>' );
	tmp = tmp.replace( /(<a[^<>]+href="[^"]*)<[^<>]+>([^"]*)<[^<>]+>([^"]*")/gi, "$1$2$3" );
	el.innerHTML = tmp;
}

function preg_quote( reg ) {
	return reg.replace( /([\.\\\+\*\?\[\^\]\$\(\)\{\}\|])/g, "\\$1");
}


function msie6_png(obj) {
	if( !window.clientInformation ) return;
	var v = window.clientInformation.appVersion.match( /MSIE\s+([0-9]+)/i );
	if( !v && !v[1] ) return;
	if( v[1] > 6 ) return;
	if( !/\.png/i.test( obj.src ) ) return;

	with( obj ) {
		style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + src + ",sizingMethod=crop)";
		src = "/img/p.gif";
	}
}
