
var urlLegal = 'abcdefghijklmnopqrstuvwxyz0123456789-';

//
//								newUser
//
//	Toggles visibility of new user fields when the 'new user' checkbox is clicked
//
function newUser()
{
	var rb = document.getElementsByName( "rbnewuser" );
	var disp = "none";
	if( rb[0].checked )
	{
		// IE and Mozilla use different versions, probably Mozilla's is W3C approved
		disp = document.all ? "block" : "table-row";
	}

	document.getElementById( "idconfirmrow" ).style.display = disp;
	document.getElementById( "idemailrow" ).style.display = disp;
}

//
//								onSubmit
//
//	Does basic client side validation and then submits the new forum data for processing.
//
function onSubmit()
{
	// get the field
	var TAB = "\t";
	var username = document.getElementById( "idusername" ).value;
	var rb = document.getElementsByName( "rbnewuser" )[0].checked;
	var password = document.getElementById( "idpassword" ).value;
	var confirm = document.getElementById( "idconfpassword" ).value;
	var email = document.getElementById( "idemail" ).value;
	var forum = document.getElementById( "idforum" ).value;
	var urlprefix = document.getElementById( "idurlprefix" ).value;

	return validateForm( username, rb, password, confirm, email, forum, urlprefix );
}

//
//						validateForm
//
function validateForm( name, newUser, pass, confPass, email, forum, urlprefix )
{
	// check the username
	if( name.length < 2 )
	{
		alert( "Your username must be at least two characters long." );
		return false;
	}
	if( name.length > 31 )
	{
		alert( "Your username must be 31 characters or less in length." );
		return false;
	}
	var legal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-+=@.!#";

	// check username for legal chars
	for( i = 0; i < name.length; i++ )
	{
		var s = name.substr( i, 1 );
		if( legal.indexOf( s ) == -1 )
		{
			alert( "Illegal character in username: " + name.charAt(i) + "\nYour username can contain only letters, numbers and the characters _-+=@.!#" );
			return false;
		}
	}

	// make sure they provide a password
	if( pass.length == 0 )
	{
		alert( "You must provide a password." );
		return false;
	}

	// check password for legal chars
	for( i = 0; i < pass.length; i++ )
	{
		var s = pass.substr( i, 1 );
		if( legal.indexOf( s ) == -1 )
		{
			alert( "Illegal character in password: " + pass.charAt(i) + "\nYour password can contain letters, numbers, and the characters _-+=@.!#" );
			return false;
		}
	}

	// make sure passwords match
	if( newUser )
	{
		if( pass != confPass )
		{
			alert( "Password confirmation does not match original.  Try again." );
			return false;
		}

		// make sure email addr is provided, so ultra simply sanity checks on it
		if( email.length == 0 )
		{
			alert( "You must provide an email address so we can email you the information about your new discussion board." );
			return false;
		}
		if( email.length < 6 
		 || email.indexOf("@") == -1 
		 || email.indexOf(".")  == -1 
		 || email.indexOf(",") != -1
		 || email.indexOf(";") != -1
		)
		{
			alert( "That does not appear to be a valid email address. You must provide an email address so we can email you the information about your new discussion board." );
			return false;
		}
	}

	// quick check of forum name
	if( forum.length == 0 )
	{
		alert( "You must provide a name for your new discussion board.  You will be able to change the name later if you desire." );
		return false;
	}

	// revalidate the URL prefix
	if( urlprefix.length == 0 )
	{
		alert( "You must specify a uniqe URL prefix for your discussion board."  );
		return false;
	}
	for( i = 0; i < urlprefix.length; i++ )
	{
		var c = urlprefix.charAt( i );
		if( urlLegal.indexOf( c ) == -1 )
		{
			alert( "The URL prefix must contain only letters, numbers, and/or the hyphen ('-') character." );
			return false;
		}
	}

	return true;
}

//
//						terms
//
//	Opens the Terms and Conditions window
//
function terms()
{
	window.open( "http://www.minuteboard.com/m/b/terms.html", 
		"_blank", 
		"width=500,height=400,resizable=1,scrollbars=1,menubar=0,toobar=0" );
}

//
//						onUrlKeypress
//
function onUrlKeypress( e )
{

	if( !e )
	{
		var e = window.event;
	}
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	// allow default processing of backspace, return, delete, and tab	
	if( code == 8 || code == 13 || code == 46 || code == 9 )
	{
		return;
	}

	var c = String.fromCharCode( code );
	if( urlLegal.indexOf( c ) == -1 )
	{
		pre = document.getElementById( 'idurlprefix' );
		pre.style.color = 'red';
	}
	return;
}

//
//						onUrlKeyUp
//
function onUrlKeyUp()
{
	var pre = document.getElementById( 'idurlprefix' ).value;
	var i;
	var url = '';
	for( i = 0; i < pre.length; i++ )
	{
		var c = pre.charAt(i).toLowerCase();
		if( urlLegal.indexOf( c ) != -1 )
		{
			url += c;
		}
	}

	// always update the displayed full URL
	document.getElementById( 'idurl' ).innerHTML = 'http://' + url + '.minuteboard.com';

	// see if we need to fix the input box
	if( url != pre )
	{
		var obj = document.getElementById( 'idurlprefix' );
		obj.value = url;
		obj.style.color = 'black';
	}
}

//
//						getXMLHTTP
//
function getXMLHTTP()
{
	var xmlhttp = null;

	// if we're in Mozilla, just new the puppy
	if( window.XMLHttpRequest )
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
		}
		return xmlhttp;
	}
	
	// try several flavors in IE
	try
	{
		xmlhttp = new ActiveXObject( "MSXML2.XMLHTTP.3.0" );
		if( xmlhttp != null )
			return xmlhttp;
	}
	catch(e)
	{
	}
	
	try
	{
		xmlhttp = new ActiveXObject( "MSXML2.XMLHTTP" );
		if( xmlhttp != null )
			return xmlhttp;
	}
	catch(e)
	{
	}
	
	try
	{
		xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
		if( xmlhttp != null )
			return xmlhttp;
	}
	catch(e)
	{
	}

	return xmlhttp;	
}
