/**
* logonButtonFactory
*
* A factory for building log on buttons
*
* @param p The HGPage the button should log the user onto
**/
function logonPopupButtonFactory( p ) { 

	var rbut = new SimpleButtonWidget( p.style_set + "_control_button", new HGLogonPopupCommand( p ) );

	rbut.addContentNode( textToDOM( "span", "Logon" ) );

	return rbut;

}

/**
* HGLogonPopupCommand
*
* Generate a popup window containing a field for user name and a field for password
*
* @param p The page the user will be logging into
**/
function HGLogonPopupCommand( p ) { 

	this.hg_page = p;

}

new HGLogonPopupCommand();

HGLogonPopupCommand.prototype.execute = function() { 

	//create the popup window
	var pw = new SimplePopupWindow( 
	"logon_popup" ,
	"Logon Window" ,
	"hg_popup" , 
	200 , 
	400 , 
	15 ,
	true , 
	false ,
	false
	); 

	//create the window title
	var win_title = textToDOM( "span", "Logon information" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the logon form
	var logon_form = new HGForm();

	var user_name_field = new HGFormElement ( 
		"text" ,
		"" , 
		"user_name" ,
		"20" ,
		"1" ,
		"500" ,
		null ,
		null ,
		"User Name" ,
		"The user name you selected during registration"
	);

	var password_field = new HGFormElement ( 
		"password" , 
		"" ,
		"password" , 
		"20" ,
		"1" , 
		"20" , 
		null ,
		null ,
		"Password" ,
		"The password you selected during registration"
	);

	logon_form.addHGFormElement( user_name_field );
	logon_form.addHGFormElement( password_field );

	pw.body.addContentNode( logon_form.form );

	var forgot_password_button = forgotPasswordPopupButtonFactory( this.hg_page, pw );
	pw.body.addContentNode( forgot_password_button.getWidgetDOM() );

	//create the submit button
	var sbut = submitHGLogonButtonFactory( this.hg_page.style_set, pw, logon_form );

	//add the submit button to the control bar of the popup window
	pw.addControlButton( sbut );

	//add the popup window to the windowing engine
	document.body.windowing_environment.addPopupWindow( pw );

	//set the popup from parameter so that we can reload the page upon submition
	pw.popup_from = this.hg_page;

}

/**
* submitHGLogonButtonFactory
**/
function submitHGLogonButtonFactory( style_set, pw, logon_form ) { 

	var rbut = new SimpleButtonWidget( style_set + "_control_button", new HGLogonCommand ( pw, logon_form.getHGFormElement( "user_name" ) , logon_form.getHGFormElement( "password" ) ) );

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

/**
* HGLogonCommand	
* 
* Generates a logon command which is sent to the server
*
* @param pw The popup window generating the command
* @param user_name_field The field where the user name will be entered
* @param password_field The field where the password will be entered
**/
function HGLogonCommand( pw, user_name_field, password_field ) {

	this.popup_window = pw;
	this.user_name_field = user_name_field;
	this.password_field = password_field;

}

new HGLogonCommand();

HGLogonCommand.prototype.execute = function() { 

	// <-- check user name and password for validity?

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGLogonCommand" );
	nreq.addRequestElement( 'user_name', this.user_name_field.getValue() );
	nreq.addRequestElement( 'password', this.password_field.getValue() );

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGLogonRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "logging on" );

}

function handleHGLogonRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//refresh the page
		var hgm = x.getElementsByTagName( 'HGMessage' )[0];
		var user_id = hgm.getElementsByTagName( 'user_id' )[0].childNodes[0].nodeValue;
		var access_level = hgm.getElementsByTagName( 'access_level' )[0].childNodes[0].nodeValue;

		t.popup_from.resetUserInfo( user_id, access_level );

		//close popup window
		var cpw = new ClosePopupWindowCommand( document.body.windowing_environment, t );
		cpw.execute();

	}

	else { 

		t.setStatusToFailed( "logon unsuccessful.  Please check user name and password" );

	}

}

/**
* logoffButtonFactory
*
* A factory for building log on buttons
*
* @param p The HGPage the user is logging off of
**/
function logoffButtonFactory( p ) { 

	var rbut = new SimpleButtonWidget( p.style_set + "_control_button", new HGLogoffCommand( p ) );

	rbut.addContentNode( textToDOM( "span", "Logoff" ) );

	return rbut;

}

/**
* HGLogoffCommand	
* 
* Generates a logoff command which is sent to the server
*
* @param p The HGPage being logged off of
**/
function HGLogoffCommand( p ) {

	this.hg_page = p;

}

new HGLogoffCommand();

HGLogoffCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGLogoffCommand" );

	var af = AJAXRequestFunctionFactory( this.hg_page, handleHGLogoffRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGLogoffRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		t.resetUserInfo( -1, 20 );

	}

	else { 

		alert( "Logoff unsuccessful.  Please try again in a few seconds" );

	}

}


















/**
* forgotPasswordPopupButtonFactory
*
* A factory for building forgot my password buttons
*
* @param p The HGPage the button should log the user onto
* @param from_win The window this request is coming from
**/
function forgotPasswordPopupButtonFactory( p, from_win ) { 

	var rbut = new SimpleButtonWidget( p.style_set + "_control_button", new HGForgotPasswordPopupCommand( p, from_win ) );

	rbut.addContentNode( textToDOM( "span", "Oops, I forgot my user name and/or password" ) );

	return rbut;

}

/**
* HGForgotPasswordPopupCommand
*
* Generate a popup window containing a field for email address
*
* @param p The page the user will be logging into
* @param from_win The window this request is coming from
**/
function HGForgotPasswordPopupCommand( p, from_win ) { 

	this.hg_page = p;
	this.from_win = from_win;

}

new HGForgotPasswordPopupCommand();

HGForgotPasswordPopupCommand.prototype.execute = function() { 

	//create the popup window
	var pw = new SimplePopupWindow( 
	"forgot_password_popup" ,
	"Forgot Password Window" ,
	"hg_popup" , 
	200 , 
	400 , 
	15 ,
	true , 
	false ,
	false
	); 

	//create the window title
	var win_title = textToDOM( "span", "So you forgot your logon info..." );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the logon form
	var logon_form = new HGForm();

	var email_field = new HGFormElement ( 
		"text" ,
		"" , 
		"email" ,
		"20" ,
		"1" ,
		"50" ,
		null ,
		null ,
		"EMail Address" ,
		"The e-mail address you provided upon registration."
	);

	logon_form.addHGFormElement( email_field );

	pw.body.addContentNode( logon_form.form );

	//create the email submition button
	var email_submit = forgotPasswordEmailSubmitButtonFactory( this.hg_page, pw, email_field );

	pw.body.addContentNode( email_submit.getWidgetDOM() );

	//create the submit button
	//var sbut = submitHGLogonButtonFactory( this.hg_page.style_set, pw, logon_form );

	//add the submit button to the control bar of the popup window
	//pw.addControlButton( sbut );

	//add the popup window to the windowing engine
	document.body.windowing_environment.addPopupWindow( pw );

	//set the popup from parameter so that we can reload the page upon submition
	pw.popup_from = this.hg_page;

	//remove the window which created this window
	(new ClosePopupWindowCommand( document.body.windowing_environment, this.from_win )).execute();

}


/**
* forgotPasswordEmailSubmitButtonFactory
*
* Creates the submit button for the e-mail portion of the forgot password window
*
* @param hg_page 
* @param pw the Popup window the submition is coming from
* @param email_field
**/
function forgotPasswordEmailSubmitButtonFactory ( hg_page, pw, email_field ) { 

	var rbut = new SimpleButtonWidget( hg_page.style_set + "_control_button", new HGForgotPasswordSubmitEmailCommand( hg_page, pw, email_field ) );

	rbut.addContentNode( textToDOM( "span", "submit e-mail" ) );

	return rbut;

}

/**
* HGForgotPasswordSubmitEmailCommand
*
* Checks the server for a user with an e-mail matching the e-mail passed in
*
* @param hg_page
* @param pw
* @param e-mail field
**/
function HGForgotPasswordSubmitEmailCommand( hg_page, pw, email_field ) { 

	this.hg_page = hg_page;
	this.pw = pw;
	this.email_field = email_field;

}

new HGForgotPasswordSubmitEmailCommand();

HGForgotPasswordSubmitEmailCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUserByEmailPCommand" );
	nreq.addRequestElement( 'email', this.email_field.getValue() );

	var af = AJAXRequestFunctionFactory( this.pw, handleHGForgotPasswordSubmitEmailRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.pw.setStatusToWorking( "checking e-mail address" );

}

function handleHGForgotPasswordSubmitEmailRequest( x, t ) { 

	var xuser = x.getElementsByTagName( 'HGUser' )[0];
	var xquestion = xuser.getElementsByTagName( 'sec_question' );

	if ( xquestion[0] ) { 
		var user_id = xuser.getElementsByTagName( 'user_id' )[0].childNodes[0].nodeValue;

		var q_text = textToDOM( 'span', "Question: " + xquestion[0].childNodes[0].nodeValue );
		t.body.addContentNode( q_text );

		//add a new form for the answer

		var answer_form = new HGForm();

		var answer_field = new HGFormElement ( 
			"text" ,
			"" , 
			"sec_answer" ,
			"25" ,
			"1" ,
			"25" ,
			null ,
			null ,
			"Answer" ,
			"The answer you provided to your security question (case sensitive)"
		);

		answer_form.addHGFormElement( answer_field );

		t.body.addContentNode( answer_form.form );

		//create the submit button
		var sbut = submitHGVerifySecurityButtonFactory( t, user_id, answer_field );

		//add the submit button to the control bar of the popup window
		t.addControlButton( sbut );
		
	}

	else {
		alert ( "No users are registered under the specified e-mail" );
		t.setStatusToFailed( "No users are registered under the specified e-mail" );
	}
}

/**
* submitHGVerifySecurityButtonFactory
*
* Create a button to submit a VerifySecurity command request
*
* @param pw
* @param user_id
* @param answer_field
**/
function submitHGVerifySecurityButtonFactory( pw, user_id, answer_field ) { 

	var rbut = new SimpleButtonWidget( pw.popup_from.style_set + "_control_button", new HGUserVerifySecurityCommand( pw, user_id, answer_field ) );

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

function HGUserVerifySecurityCommand( pw, user_id, answer_field ) { 

	this.pw = pw;
	this.user_id = user_id;
	this.answer_field = answer_field;

}

new HGUserVerifySecurityCommand();

HGUserVerifySecurityCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUserVerifySecurityCommand" );
	nreq.addRequestElement( 'user_id', this.user_id );
	nreq.addRequestElement( 'sec_answer', this.answer_field.getValue() );

	var af = AJAXRequestFunctionFactory( this.pw, handleHGVerifySecurityRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.pw.setStatusToWorking( "verifying security answer" );

}

function handleHGVerifySecurityRequest( x, t ) { 

	var success = HGPopupMessage( x );

	//close the popup window no matter what happens
	(new ClosePopupWindowCommand( document.body.windowing_environment, t )).execute();

}
