// Autologin
// version 0.2 alpha
// 2009-02-02
// Copyright (c) 2008, Zarel
// Released under public domain
// Also licensed under BSD, MIT, GPL, LGPL
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "UMN Autologin", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          UMN Autologin
// @namespace     http://aesoft.org/
// @description   Version 0.2 alpha - Automatically logs into U of M ResNet and wireless networks
// @include       *
// ==/UserScript==

var username = '';
var password = '';
var successState = 0;

if (GM_getValue) successState = GM_getValue('umn_loginsuccess',0);
function setSuccess(n) {return (GM_setValue?GM_setValue('umn_loginsuccess',n):0); }

function encrypt() {}

function getuser(user)
{
	if (user) return user;
	if (!GM_getValue)
	{
		alert('Your UserJS engine does not support persistent data. Please set your username and password manually by modifying autologin.user.js.');
		return '';
	}
	if (user = GM_getValue('umn_username')) return user;
	if (user = prompt('UMN username?',''))
	{
		GM_setValue('umn_username',user);
		return user;
	}
}
function getpass(pass)
{
	if (pass) return pass;
	if (!GM_getValue) return '';
	if (pass = GM_getValue('umn_password')) return pass;
	if (pass = prompt('UMN password?',''))
	{
		GM_setValue('umn_password',pass);
		return pass;
	}
}
function chkSuccess(successState)
{
	if (successState == 1)
	GM_setValue('umn_username','');
	GM_setValue('umn_password','');
}

function startsWith(a,b) { return (a.substr(0,b.length))==b; }
function el(a) { return document.getElementsByName(el)[0]; }

if (document.title == 'ResNet Login - University of Minnesota')
{
	var links = document.getElementsByTagName('a');
	
	for (var i=0; i<links.length; i++)
	{
		if (links[i].innerHTML == 'Log In' &&
		startsWith(links[i].href,'https://resnet.netsec.umn.edu/2007/'))
		{
			location.href = links[i].href;
			return;
		}
	}
}

//alert((username = getuser(username))+' '+(password = getpass(password)));



if ((startsWith(window.location.href,'http://wireless.netaccess.umn.edu/cgi-bin/x5.cgi')
|| startsWith(window.location.href,'https://wireless.netaccess.umn.edu/cgi-bin/x5.cgi'))
&& document.getElementsByName('uid').length)
{
	document.getElementsByName('uid')[0].value = (username = getuser(username));
	document.getElementsByName('upass')[0].value = (password = getpass(password));
	if (successState==0 || confirm('Autologin'))
	{
		document.forms[0].submit();
		setSuccess(1);
	}
}
//alert(window.document.forms[0]);
else if (startsWith(window.location.href,'http://wireless.netaccess.umn.edu/aaa/uofm.html')
&& document.getElementsByName('weblogin').length)
{
	document.getElementsByName('username')[0].value = (username = getuser(username));
	document.getElementsByName('key')[0].value = (password = getpass(password));
	if (successState==0 || confirm('Autologin'))
	{
		document.getElementsByName('weblogin')[0].submit();
		setSuccess(1);
	}
}
else if (startsWith(window.location.href,'https://resnet.netsec.umn.edu/')
&& document.getElementsByName('username').length)
{
	document.getElementsByName('username')[0].value = (username = getuser(username));
	document.getElementsByName('password')[0].value = (password = getpass(password));
	if (successState==0 || confirm('Autologin'))
	{
		document.forms[0].submit();
		setSuccess(1);
	}
}
else if (startsWith(window.location.href,'https://resnet.netsec.umn.edu/')
&& document.getElementsByTagName('input').length
&& document.getElementsByTagName('input')[0].value == 'Finish')
{
	document.getElementsByTagName('input')[0];
	setSuccess(0);
}
else if (startsWith(window.location.href,'https://netaccess.spa.umn.edu:8001/')
&& document.getElementsByName('theform').length)
{
	document.getElementsByName('auth_user')[0].value = (username = getuser(username));
	document.getElementsByName('auth_pass')[0].value = (password = getpass(password));
	if (chkSuccess(successState) && confirm('Autologin'))
	{
		document.getElementsByName('accept')[0].click();
		setSuccess(1);
	}
}
else if (window.location.href.search('umn.edu') != -1)
	setSuccess(0);