User:Func/wpfunc/blockiphelp.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//	<pre><nowiki>

function BlockIpHelp()
{
	var form = document.getElementById( 'blockip' );
	
	if ( ! form || form.tagName != 'FORM' || ! form.wpBlockAddress ) return;

	var span = document.createElement( 'SPAN' );

	form.span = span; // avoid globals

		//	to the right of the IP or username field
		//
	form.wpBlockAddress.parentNode.insertBefore( span, form.wpBlockAddress.nextSibling );

	function ipMessage( message, selectedIndex, color )
	{	var span = document.createElement( 'SPAN' );
			span.appendChild( document.createTextNode( ' ' + ( message || '' ) ) );
			span.style.color = color || '#000000';
			span.style.fontWeight = 'bold';
			span.selectedIndex = selectedIndex || 0;
		return span;
	}
	var expiry = {
		     'other' : 0,
		'15 minutes' : 1,
		    '1 hour' : 2,
		   '3 hours' : 3,
		  '24 hours' : 4,
		  '48 hours' : 5,
		    '1 week' : 6,
		   '1 month' : 7,
		'indefinite' : 8
	};
	span.ipNone    = ipMessage();
	span.ipNot     = ipMessage( 'Not an IP.'     , expiry[ '24 hours'   ], '#009900' ); // green
	span.ipUnknown = ipMessage( 'Unknown IP.'    , expiry[ '1 hour'     ], '#999900' ); // yellow
	span.ipAOL     = ipMessage( 'AOL IP address.', expiry[ '15 minutes' ], '#990000' ); // red

	span.appendChild( span.ipNot ); // place holder

	span.showMessage = function( span )
	{	this.replaceChild( span, this.firstChild );
	}

	form.wpBlockAddress.onchange = function()
	{
		var ip = IpDottedToLong( this.value );

		     if ( ip === null ) this.form.span.showMessage( this.form.span.ipNone    );
		else if ( isNaN( ip ) ) this.form.span.showMessage( this.form.span.ipNot     );
		else if ( IsAOL( ip ) ) this.form.span.showMessage( this.form.span.ipAOL     ); // what else, EarthLink?
		else                    this.form.span.showMessage( this.form.span.ipUnknown );
		
		this.form.wpBlockExpiry.selectedIndex = this.form.span.selectedIndex;
	}
	form.wpBlockAddress.onchange();
}
if ( window.addEventListener ) window.addEventListener( 'load', BlockIpHelp, false );
else if ( window.attachEvent ) window.attachEvent(    'onload', BlockIpHelp );

function IpDottedToLong( ip ) //-> number, Number.NaN for username, or null
{
	if ( ! ip ) return null;

	if ( (  ip.constructor != String          ) ||
	     ( (ip = ip.split( '.' )).length != 4 )  ) return Number.NaN;
		//
	for ( var i = 0; i < 4; i++ )
		if ( isNaN( ip[ i ] = Number( ip[ i ] ) ) ||
		            ip[ i ] <   0                 ||
		            ip[ i ] > 255                  ) return Number.NaN;

		//	I "think" JavaScript numbers can handle this....
		//
	return	ip[ 0 ] * 256 * 256 * 256 +	// JS lacks an exponent op, I refuse to use Math.pow()
			ip[ 1 ] * 256 * 256 +
			ip[ 2 ] * 256 +
			ip[ 3 ];
}

function IsAOL( ip )
{
	     if ( ip <  1074528256 ) return false; //  64. 12.  0.  0 ..
	else if ( ip <= 1074593791 ) return true;  //        .255.255

	else if ( ip <  2511208448 ) return false; // 149.174.  0.  0 ..
	else if ( ip <= 2511273983 ) return true;  //        .255.255

	else if ( ip <  2560819200 ) return false; // 152.163.  0.  0 ..
	else if ( ip <= 2560884735 ) return true;  //        .255.255

	else if ( ip <  2894069760 ) return false; // 172.128.  0.  0 ..
	else if ( ip <= 2899574783 ) return true;  //    .211.255.255

	else if ( ip <  3277651968 ) return false; // 195. 93.  0.  0 ..
	else if ( ip <= 3277684735 ) return true;  //        .127.255

	else if ( ip <  3327197184 ) return false; // 198. 81.  0.  0 ..
	else if ( ip <= 3327205375 ) return true;  //        . 31.255

	else if ( ip <  3393404928 ) return false; // 202. 67. 64.  0 ..
	else if ( ip <= 3393421311 ) return true;  //        .127.255

	else if ( ip <  3451650048 ) return false; // 205.188.  0.  0 ..
	else if ( ip <= 3451715583 ) return true;  //        .255.255

	else if ( ip <  3486007296 ) return false; // 207.200. 64.  0 ..
	else if ( ip <= 3486023679 ) return true;  //        .127.255

	else return false;
}

//	</nowiki></pre>