/*
	NCCG Java Script library.  This is repository code that Ctrl-R saves and propagates to all Webs
	listed in the repository file.

	Last changed on 2007-12-09 by DGB
*/


function handleErr(msg,url,l)
{
	var txt="";
	txt="There was an error on this page.\n\n";
	txt+="Error: " + msg + "\n";
	txt+="URL: " + url + "\n";
	txt+="Line: " + l + "\n\n";
	txt+="Click OK to continue.\n\n";
	alert(txt);
	return true;
}

function get_browser_data()
{
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion >= "4.0")
	{
		return("Running IE 4 or better?");
	}
	else if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion < "4.0")
	{
		return("Running IE 3.x or less?");
	}
	else if (navigator.appName == "Netscape" && navigator.appVersion >= "4.0")
	{
		return("Running Netscape 4 or better?");
	}
	else if (navigator.appName == "Netscape" && navigator.appVersion < "4.0")
	{
		return("Running Netscape 3.x or under?");
	}
	else if (navigator.appName != "Microsoft Internet Explorer" && navigator.appName != "Netscape")
	{
		return("not running IE or Netscape!");
	}
}

function wopen( url, w, h, allowResize )
{
	/*
		Usage 1: <a href="http://www.xyz.com" target="popup" onClick="wopen( 'http://www.xyz.com', 800, 500, 1 ); return false;">
		Usage 2: <a href="xyz.htm" target="popup" onClick="wopen( 'xyz.htm', 500, 275, 0 ); return false;">
	*/
	w += 32;
	h += 96;
	if ( allowResize == 1 )
	{
		var win = window.open( url, 'popup', 'width=' + w + ', height=' + h + ', ' + 'location=yes, menubar=yes, ' + 'status=yes, toolbar=yes, scrollbars=yes, resizable=yes' );
	}
	else
	{
		var win = window.open( url, 'popup', 'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=no, resizable=no' );
	}
	win.resizeTo( w, h );
	win.focus();
}

/*
	str_repeat() is a helper function for sprintf().
*/
function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

/*
	string sprintf(string format[mixed arg1[, mixed arg2[,...]]]) works somewhat like the C-family
	function of the same name. The placeholders in the format string are marked by "%" and are 
	followed by one or more of these elements, in this order:

	1. An optional "+" sign that forces to preceed the result with a plus or minus sign on numeric values. By default, 
		only the "-" sign is used on negative numbers.
	2. An optional padding specifier that says what character to use for padding (if specified). Possible values are 
		0 or any other character precedeed by a '.'.  The default is to pad with spaces.
	3. An optional "-" sign, that causes sprintf to left-align the result of this placeholder. The default is to 
		right-align the result.
	4. An optional number that says how many characters the result should have. If the value to be returned is 
		shorter than this number, the result will be padded.
	5. An optional precision modifier, consisting of a "." (dot) followed by a number, that says how many digits should 
		be displayed for floating point numbers. When used on a string, it causes the result to be truncated.
	6. A type specifier that can be any of:
		* % - print a literal "%" character
		* b - print an integer as a binary number
		* c - print an integer as the character with that ASCII value
		* d - print an integer as a signed decimal number
		* e - print a float as scientific notation
		* u - print an integer as an unsigned decimal number
		* f - print a float as is
		* o - print an integer as an octal number
		* s - print a string as is
		* x - print an integer as a hexadecimal number (lower-case)
		* X - print an integer as a hexadecimal number (upper-case)
	7. For example, to get a two-digit string representation of a byte value, use
		sprintf("%02.2X", my_number), where:
		% - placeholder marker
		0 - pad with 0
		2 - width specifier (a minimum of 2 characters) 
		.2 - precision modifier, (a maximum of 2 characters)
		X - show an integer as an upper-cased hexadeciaml value
*/
function sprintf()
{
	var i = 0, f = arguments[i++], o = [], m, a, p;
	while (f) {
		if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
		else if (m = /^\x25{2}/.exec(f)) o.push('%');
		else if (m = /^\x25(\+)?(0|'[^$])?(-)?(\d+)?(\.\d+)?([b-fosuxX])/.exec(f)) {
			if (!(a = arguments[i++])) throw("Too few arguments.");
			if (/[^s]/.test(m[6]) && (typeof(a) != 'number'))
				throw("Expecting number but found " + typeof(a));
			switch (m[6]) {
				case 'b': a = a.toString(2); break;
				case 'c': a = String.fromCharCode(a); break;
				case 'd': a = parseInt(a); break;
				case 'e': a = m[5] ? a.toExponential(m[5].charAt(1)) : a.toExponential(); break;
				case 'f': a = m[5] ? parseFloat(a).toFixed(m[5].charAt(1)) : parseFloat(a); break;
				case 'o': a = a.toString(8); break;
				case 's': a = ((a = String(a)) && m[5] ? a.substring(0, m[5].charAt(1)) : a); break;
				case 'u': a = Math.abs(a); break;
				case 'x': a = a.toString(16); break;
				case 'X': a = a.toString(16).toUpperCase(); break;
			}
			a = (/[def]/.test(m[6]) && m[1] && a > 0 ? '+' + a : a);
			p = m[4] ? str_repeat(m[2] ? m[2] == '0' ? '0' : m[2].charAt(1) : ' ', m[5] ? m[4] - String(a).length : m[4]) : '';
			o.push(m[3] ? a + p : p + a);
		} else throw("Huh ?");
		f = f.substring(m[0].length);
	}
	return(o.join(''));
}

function update()
{
	// Define the major local variables.
	var param = document.getElementById( 'parameterId' );   // The dotted quad of the site's login page
	var pWord = document.getElementById( 'pwID' );          // The master password
	var hash  = document.getElementById( 'resultId' );      // The calculated ten-digit SHA1 hash of these two values

	// Decompose the parameter, the dotted quad IP address of the host's login page, into four discreet
	// two-digit hex octets, forcing a leading zero if necessary to get the second digit.
	var position = param.value.indexOf( '.', 0 );
	var quartet1 = param.value.substr( 0, position );
	var hexQuartet1;
	hexQuartet1 = ( quartet1 == 0 ) ? "00" : sprintf( "%02.2X", parseInt( quartet1 ) );
	position1 = param.value.indexOf( '.', position + 1 );
	var quartet2 = param.value.substr( position + 1, position1 - position - 1 );
	var hexQuartet2;
	hexQuartet2 = ( quartet2 == 0 ) ? "00" : sprintf( "%02.2X", parseInt( quartet2 ) );
	position2 = param.value.indexOf( '.', position1 + 1 );
	var quartet3 = param.value.substr( position1 + 1, position2 - position1 - 1);
	var hexQuartet3;
	hexQuartet3 = ( quartet3 == 0 ) ? "00" : sprintf( "%02.2X", parseInt( quartet3 ) );
	var quartet4 = param.value.substr( position2 + 1 );
	var hexQuartet4;
	hexQuartet4 = ( quartet4 == 0 ) ? "00" : sprintf( "%02.2X", parseInt( quartet4 ) );

	// Replace the dotted quad with an eight-position string of upper case hexadecimal digits.
	param.value = hexQuartet1 + hexQuartet2 + hexQuartet3 + hexQuartet4;

	// Calculate the ten digit SHA1 hash of this string and the master password.
	var hashapass = b64_hmac_sha1( pWord.value, param.value ) . substr( 0, 10 );
	hash.value = hashapass;
	param.value = '';
	pWord.value = '';
	hash.select();
}
