/*-----------------------------------------------------------------------------------------------*/
/*
Functionality for creating/managing accounts
*/
/*-----------------------------------------------------------------------------------------------*/


/*----------------------------------------GLOBAL VARIABLES---------------------------------------*/
var xmlHttp;
/*-----------------------------------------------------------------------------------------------*/

YAHOO.namespace("ns.container");

function init()
{
	// handles submission verification
	function doSubmit()
	{
		var f = document.formReg;
		var uname = f.uname.value;
		var upass = f.upass.value;
		var fname = f.fname.value;
		var lname = f.lname.value;
		var comments = f.comments.value;
		var fSpam = false;

		// VALIDATE
		// username
		if( uname == "" )
		{
			alert( "You must enter a user name.");
			f.uname.focus();
			return false;
		}
		if( uname.indexOf( " " ) > -1 )
		{
			alert( "No spaces are allowed in the user name.");
			f.uname.focus();
			return false;
		}
		if( uname.length > 25 )
		{
			alert( "User names cannot be greater than 25 characters.");
			f.uname.focus();
			return false;
		}
		// userpass
		if( upass == "" )
		{
			alert( "You must enter a password.");
			f.upass.focus();
			return false;
		}
		if( upass.length > 15 )
		{
			alert( "Passwords cannot be greater than 15 characters.");
			f.upass.focus();
			return false;
		}
		if( upass.toUpperCase() == uname.toUpperCase() )
		{
			alert( "Password cannot be the same as the username." );
			f.upass.focus();
			return false;
		}
		// first name
		if( fname == "" )
		{
			alert( "You must enter a first name.");
			f.fname.focus();
			return false;
		}
		// last name
		if( lname == "" )
		{
			alert( "You must enter a last name.");
			f.lname.focus();
			return false;
		}
		if( fname.toUpperCase() == lname.toUpperCase() )
		{
			fSpam = true;
		}
		// email
		if( f.email.value == "" )
		{
			alert( "You must enter a valid email.");
			f.email.focus();
			return false;
		}
		// company
		if( f.company.value == "" )
		{
			alert( "You must enter a valid company/affiliation name.");
			f.company.focus();
			return false;
		}
		// street
		if( f.street.value == "" )
		{
			alert( "You must enter a valid street address.");
			f.street.focus();
			return false;
		}
		// city
		if( f.city.value == "" )
		{
			alert( "You must enter a valid city.");
			f.city.focus();
			return false;
		}
		// postal code
		if( f.zip.value == "" )
		{
			alert( "You must enter a valid postal code.");
			f.zip.focus();
			return false;
		}
		// country
		if( f.country.options[ f.country.selectedIndex ].value == "00" )
		{
			alert( "You must select a country." );
			f.country.focus();
			return false;
		}
		// reference from friend or colleague
		if( f.source.value == "Reference from Friend/Colleague" )
		{
			if( ( f.other.value == "" ) || ( f.refemail.value == "" ) )
			{
				alert( "You must enter the name and email of the referring person." );
				f.other.focus();
				return false;
			}
		}
		// comment
		if( comments == "" )
		{
			alert( "You must enter comments on your intended use.");
			f.comments.focus();
			return false;
		}
		if( ( comments.indexOf( "http:" ) > -1 ) || ( comments.indexOf( "www." ) > -1 ) )
		{
			fSpam = true;
		}

		if( fSpam == true )
		{
			numSpams++;
			xmlHttp = GetXmlHttpObject();
			if( xmlHttp != null )
			{
				var url="uc.php";
				xmlHttp.open( "GET", url, true );
				xmlHttp.send( null );
			}
			if( numSpams <= 3 )
			{
				alert( "You have been identified as a possible spammer.\nPlease, provide all important information." );
				return false;
			}
			else
			{
				alert( "You have been identified as a spammer too many times.\nPlease contact NeuroScript or try at a later time to register." );
				YAHOO.ns.container.caDlg.cancel();
				return false;
			}
		}

		// submit
		document.formReg.action = "reg.php";
		document.formReg.submit();
//		alert("The system is temporarily down.\nPlease try again later.");
	}

	function stateChanged() 
	{ 
		if( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		{
			if( xmlHttp.responseText != "" )
			{
				var msg = "The specified " + xmlHttp.responseText + " already exists. Please choose another.";
				alert( msg );
			}
			else
			{
				doSubmit();
			}
		}
	}

	function GetXmlHttpObject()
	{
		var xmlHttp = null;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		}
		catch( e )
		{
			//Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject( "Msxml2.XMLHTTP" );
			}
			catch( e )
			{
				xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
		}
		return xmlHttp;
	}

	var handleSubmit = function()
	{
		// setup ajax call to verify username --- which will call validation function
		xmlHttp = GetXmlHttpObject();
		if( xmlHttp == null )
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		var url="/includes/verifyuser.php";
		url = url + "?username=" + document.formReg.uname.value;
		url = url + "&email=" + document.formReg.email.value;
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open( "GET", url, true );
		xmlHttp.send( null );
	};
	var handleSubmitL = function()
	{
		var f = document.formLoginPop;

		// VALIDATE
		// username
		if( f.Username.value == "" )
		{
			alert( "You must enter a user name.");
			f.Username.focus();
			return false;
		}
		if( f.Username.value.indexOf( " " ) > -1 )
		{
			alert( "No spaces are allowed in the user name.");
			f.Username.focus();
			return false;
		}
		if( f.Username.value.length > 25 )
		{
			alert( "User names cannot be greater than 25 characters.");
			f.Username.focus();
			return false;
		}
		// userpass
		if( f.Password.value == "" )
		{
			alert( "You must enter a password.");
			f.Password.focus();
			return false;
		}
		ProcessWithMeter();
	};
	var handleCancel = function()
	{
		this.cancel();
	};

	// Instantiate the Create Account Dialog
	var w = "40em";
	if( navigator.appVersion.indexOf( "MSIE" ) > -1 )
	{
		w = "50em";
	}
	YAHOO.ns.container.caDlg = new YAHOO.widget.Dialog("caDlg", 
							{ width : w,
							  fixedcenter : true,
							  visible : false, 
							  modal : true,
							  constraintoviewport : true,
							  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
										  { text:"Cancel", handler:handleCancel } ]
							});

	// Key listeners
	var keyEnter = new YAHOO.util.KeyListener( document, { keys:13 },
											 { fn:handleSubmit,
											   scope:YAHOO.ns.container.caDlg,
											   correctScope:true } );
	var keyEsc = new YAHOO.util.KeyListener( document, { keys:27 },
											 { fn:handleCancel,
											   scope:YAHOO.ns.container.caDlg,
											   correctScope:true } );
	YAHOO.ns.container.caDlg.cfg.queueProperty( "keylisteners", keyEnter );
	keyEsc.enable();

	// Render the Dialog
	YAHOO.ns.container.caDlg.render();

	var doCreateAccount = function()
	{
		if( numSpams > 3 )
		{
			alert( "You have been identified as a spammer too many times.\nPlease contact NeuroScript or try at a later time to register." );
			return false;
		}
		else YAHOO.ns.container.caDlg.show();
	}

	YAHOO.util.Event.addListener("createaccount", "click", doCreateAccount, YAHOO.ns.container.caDlg, true);
	YAHOO.util.Event.addListener("reg", "click", doCreateAccount, YAHOO.ns.container.caDlg, true);
	YAHOO.util.Event.addListener("regUM", "click", doCreateAccount, YAHOO.ns.container.caDlg, true);
	YAHOO.util.Event.addListener("regLM", "click", doCreateAccount, YAHOO.ns.container.caDlg, true);


	// progress meter dialog
	function ProcessWithMeter()
	{
		if( !YAHOO.ns.container.wait )
		{
			// Initialize the temporary Panel to display while waiting for external content to load
			YAHOO.ns.container.wait = 
					new YAHOO.widget.Panel("wait",  
													{ width: "240px", 
													  fixedcenter: true, 
													  close: false, 
													  draggable: false, 
													  zindex:4,
													  modal: true,
													  visible: false
													} 
												);
			YAHOO.ns.container.wait.setHeader("Logging in, please wait...");
			YAHOO.ns.container.wait.setBody("<img src=\"gfx/rel_interstitial_loading.gif\"/>");
			YAHOO.ns.container.wait.render(document.body);
		}

		// Define the callbacks
		var callback =
		{
			success : function( o )
			{
				YAHOO.ns.container.wait.hide();
				document.formLoginPop.submit();
			},
			failure : function( o )
			{
				YAHOO.ns.container.wait.hide();
				document.formLoginPop.submit();
			}
		}

		// Show the Panel
		YAHOO.ns.container.wait.show();

		// Connect to our data source and load the data
		var conn = YAHOO.util.Connect.asyncRequest("GET", "includes/somedata.php?r=" + new Date().getTime(), callback);
	}

	// Instantiate the Login Dialog
	var w = "25em";
	if( navigator.appVersion.indexOf( "MSIE" ) > -1 )
	{
		w = "30em";
	}
	YAHOO.ns.container.loginDlg = new YAHOO.widget.Dialog("loginDlg", 
							{ width : w,
							  fixedcenter : true,
							  visible : false,
							  modal : true,
							  constraintoviewport : true,
							  buttons : [ { text:"Submit", handler:handleSubmitL, isDefault:true },
										  { text:"Cancel", handler:handleCancel } ]
							});

	// Key listeners
	var keyEnterL = new YAHOO.util.KeyListener( document, { keys:13 },
											 { fn:handleSubmitL,
											   scope:YAHOO.ns.container.loginDlg,
											   correctScope:true } );
	var keyEscL = new YAHOO.util.KeyListener( document, { keys:27 },
											 { fn:handleCancel,
											   scope:YAHOO.ns.container.loginDlg,
											   correctScope:true } );
	YAHOO.ns.container.loginDlg.cfg.queueProperty( "keylisteners", keyEnterL );
	keyEscL.enable();

	// Render the Dialog
	YAHOO.ns.container.loginDlg.render();

	YAHOO.util.Event.addListener("login", "click", YAHOO.ns.container.loginDlg.show, YAHOO.ns.container.loginDlg, true);
	YAHOO.util.Event.addListener("loginUM", "click", YAHOO.ns.container.loginDlg.show, YAHOO.ns.container.loginDlg, true);
	YAHOO.util.Event.addListener("loginBN", "click", YAHOO.ns.container.loginDlg.show, YAHOO.ns.container.loginDlg, true);
}

YAHOO.util.Event.onDOMReady( init );
