function openpopup( url, w, h, wn )
{
	l = (screen.width/2)  - (w/2);
	t = (screen.height/2) - (h/2);

	win1 = window.open( url, wn, 'left='+l+', top='+t+', width='+w+', height='+h+', status=no, toolbar=no, menubar=no, resizable=yes, scrollbars=yes' );
}

function redirectself( url )
{
	self.location = url;
}

function redirectblank( url, wn )
{
	w = screen.width;
	h = screen.height;

	win1 = window.open( url, wn, 'width='+w+', height='+h+'status=yes, toolbar=yes, location=yes, menubar=yes, resizable=yes, scrollbars=yes' );
}

function js_select_all(id)
{
	w_el = document.getElementById(id);
	for( i = 0; i < w_el.length; i++ )
	{
		w_el[i].selected = 1;
	}
}

function js_deselect_all(id)
{
	w_el = document.getElementById(id);
	for( i = 0; i < w_el.length; i++ )
	{
		w_el[i].selected = 0;
	}
}

function js_select_switch( f )
{
	checked = true;
	for (i = 0; i < f.length; i++)
	{
		if( f.elements[i].checked == 0 )
		{
			checked = false;
		}
	}

	if( checked == false )
	{
		for (i = 0; i < f.length; i++)
		{
			f.elements[i].checked = 1;
		}
	}

	if( checked == true )
	{
		for (i = 0; i < f.length; i++)
		{
			f.elements[i].checked = 0;
		}
	}
}

// TEXT CHECK FUNCTIONS
function CheckEmailField( text )
{
	return (text.search( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/ ) == -1) ? true : false;
}

function CheckNameField( text )
{
	return (text.search( "^[a-zA-Z_ 0-9\-]*$" ) == -1) ? true : false;
}

function CheckNumberField( text )
{
	return (text.search( "^[0-9]+$" ) == -1) ? true : false;
}

function MyFindEmail( text )
{
	return (text.search( /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+/ ) == -1) ? false : true;
}

function MyFindUrl( text )
{
	return (text.search( /((https?:\/\/)|(www\.))\w+([\.-]?\w+)*(\.\w{2,4})+/i ) == -1) ? false : true;
}

// MULTI_LANG FUNCTIONS
function ChangeLang()
{
	var lang_str = document.getElementById( 'lang' ).value;
	redirectself( '/' + lang_str + '/?reload=1' );
	return false;
}

// TOOLTIP FUNCTIONS
function ShowTooltip( id )
{
	if( mousex - 30 < 0 )
	{
		xx = 5;
	}
	else
	{
		xx = mousex - 30;
	}
	document.getElementById(id).style.left = xx;
	document.getElementById(id).style.top = mousey + 15;
	document.getElementById(id).style.visibility = 'visible';
}

function HideTooltip( id, timeout )
{
	document.getElementById(id).style.visibility = 'hidden';
}

// JS PROMT EXPLAIN
function ExplainAction( url, question )
{
	descr = prompt( question, "" );
	if( descr !== null )
	{
		location.href = url + '?s_reason=' + descr;
	}
}

function ConfirmExplainAction( url, question, confirm_quest, needconfirm )
{
	confirmed = needconfirm ? confirm( confirm_quest ) : true;
	if( confirmed )
	{
		descr = prompt( question, "" );
		if( descr !== null )
		{
			location.href = url + '?s_reason=' + descr;
		}
	}
}

function ExplainFormAction( url, question )
{
	descr = prompt( question, "" );
	if( descr !== null )
	{
		document.forms[0].action = url + '?s_reason=' + descr;
		document.forms[0].submit();
	}
}

function ExplainBan( url, question1, question2 )
{
	descr = prompt( question1, "" );
	days = prompt( question2, 0 );

	if( descr !== null && days !== null )
	{
		location.href = url + '?s_reason=' + descr + '&i_days=' + days;
	}
}

function ExplainFormBan( url, question1, question2 )
{
	descr = prompt( question1, "" );
	days = prompt( question2, 0 );

	if( descr !== null && days !== null )
	{
		document.forms[0].action = url + '?s_reason=' + descr + '&i_days=' + days;
		document.forms[0].submit();
	}
}

function MsgInform( text )
{
	alert( text );
}

// FORM HANDLE FUNCTIONS
function ConfirmFormAction( f, ac, msg, needconfirm )
{
	var is_sel = 0;
	for (i = 0; i < f.length; i++)
	{
		if( f.elements[i].checked == 1 )
		{
			is_sel = 1;
		}
	}

	if( is_sel == 0 )
	{
		//alert( 'Please, select the data to work with' );
	}
	else
	{
		confirmed = needconfirm ? confirm( msg ) : true;
		if( confirmed )
		{
			f.action = ac;
			f.submit();
		}
	}
}

function ConfirmTextAction( loc, msg, needconfirm )
{
	confirmed = needconfirm ? confirm( msg ) : true;
	if( confirmed )
	{
		self.location = loc;
	}
}

function ImitateFormSubmit( ac, form_name )
{
	document.getElementById(form_name).action = ac;
	document.getElementById(form_name).submit();
}

function AddFavourites( url, id, wait_text )
{	var req = new JsHttpRequest();

	req.onreadystatechange = function()
	{
		if( req.readyState == 4 )
		{
			if( req.responseText )
			{
	            ShowText( 'ajax_process', req.responseText );
			}
		}
	}

	ShowText( 'ajax_process', wait_text );

	req.caching = false;
	u = url + '?id=' + id;
 	req.loader = 'script';
 	req.open('GET',u,true);
 	req.send({});
}

function RateImage( url, id, wait_text )
{    var req = new JsHttpRequest();

	req.onreadystatechange = function()
	{
		if( req.readyState == 4 )
		{
			if( req.responseText )
			{
	            ShowText( 'ajax_process', req.responseText );
			}
		}
	}

	ShowText( 'ajax_process', wait_text );

	var rate_el = document.getElementById('rate_content');
	var rate_value = rate_el[rate_el.selectedIndex].value;

	req.caching = false;
	u = url + '?id=' + id + '&rate=' + rate_value;
 	req.loader = 'script';
 	req.open('GET',u,true);
 	req.send({});}

function ShowText( id, text )
{	document.getElementById(id).innerHTML = text;
	document.getElementById(id).style.display = '';}

function ChooseUpload( chooseID, videoID, galleryID )
{	var choose = document.getElementById( chooseID );
	if( choose.options[choose.selectedIndex].value == 'g' )
	{		document.getElementById( videoID ).style.display = 'none';
		document.getElementById( galleryID ).style.display = '';	}
	else
	{        document.getElementById( videoID ).style.display = '';
		document.getElementById( galleryID ).style.display = 'none';	}}

function ShoutCheck()
{	var text_field = document.forms.shoutform.user_text.value;
	var name_field = document.forms.shoutform.sender_name.value;

	if( text_field == '' )
	{
		alert( 'May it is a good idea to add some text ? ;)' );
		return false;
	}

	if( name_field == '' )
	{
		alert( 'Please, introduce yourself' );
		return false;
	}}