/**
* newUserPopupButtonFactory
*
* A factory for building new user buttons
*
* @param p The HGPage the button should be associated with
**/
function newUserPopupButtonFactory( p ) { 

	var rbut = new SimpleButtonWidget( p.style_set + "_control_button", new HGNewUserPopupCommand( p ) );

	rbut.addContentNode( textToDOM( "span", "Not a R-L member? Register here!" ) );

	return rbut;

}

/**
* HGNewUserPopupCommand
*
* Generate a popup window containing all fields necessary for a new user
*
* @param p The page the user will be logging into
**/
function HGNewUserPopupCommand( p ) { 

	this.hg_page = p;

}

new HGNewUserPopupCommand();

HGNewUserPopupCommand.prototype.execute = function() { 

	//create the popup window
	var pw = new SimplePopupWindow( 
	"new_user_popup_window" ,
	"New User Window" ,
	"hg_popup" , 
	300 , 
	600 , 
	15 ,
	true , 
	false ,
	true
	); 

	//create the window title
	var win_title = textToDOM( "span", "New User Registration" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	var body_text = textToDOM( "div" , 
		"<b>Welcome <i>potential</i> new member to Recursive-Living.com! </b><br/> " + 
		"If you're looking for a page which currently provides minimal additional functionality for registered users " + 
		"then you've come to the right place!  Recursive-Living.com.... which, you already knew... because you're here... <br /><br /> " + 
		"<b><i>Anywhoo</i></b> rest assured that as the site develops many more member perks like member only content, the ability to " + 
		"post blogesque replies to articles, and a message board where you can talk about what you had for dinner last night and " + 
		"everyone else can make fun of you, will all be available.  So, go ahead and fill out the fields below and you'll be on your way to registration! " + 
		"<br /> <br />" );

	//create the logon form
	var new_user_form = new HGForm();

	var user_name_field = new HGFormElement ( 
		"text" ,
		"" , 
		"user_name" ,
		"20" ,
		"1" ,
		"20" ,
		null ,
		null ,
		"User Name" ,
		"The name which will be used to log onto recursive-living.com"
	);

	var verbose_name_field = new HGFormElement (
		"text" , 
		"" , 
		"verbose_name" ,
		"20" , 
		"1" ,
		"30" ,
		null , 
		null ,
		"Verbose Name" ,
		"The name which will be displayed to other users in your postings"
	);

	var password_field = new HGFormElement ( 
		"password" , 
		"" ,
		"password" , 
		"20" ,
		"1" , 
		"20" , 
		null ,
		null ,
		"Password" ,
		"The pasword which will be used to log onto recursive-living.com"
	);

	var email_field = new HGFormElement (
		"text" , 
		"" , 
		"email" ,
		"20" , 
		"1" ,
		"30" ,
		null , 
		null ,
		"E-Mail" ,
		"Your e-mail address. Used for update notifications and password retrieval"
	);
	
	var security_question_field = new HGFormElement (
		"text" , 
		"" , 
		"sec_question" ,
		"20" , 
		"1" ,
		"50" ,
		null , 
		null ,
		"Security Question" ,
		"A question used to retrieve a forgotten password"
	);

	var security_answer_field = new HGFormElement (
		"text" , 
		"" , 
		"sec_answer" ,
		"20" , 
		"1" ,
		"30" ,
		null , 
		null ,
		"Security Answer" ,
		"The correct answer to your security question.  This answer is case sensitive"
	);

	new_user_form.addHGFormElement( user_name_field );
	new_user_form.addHGFormElement( verbose_name_field );
	new_user_form.addHGFormElement( password_field );
	new_user_form.addHGFormElement( email_field );
	new_user_form.addHGFormElement( security_question_field );
	new_user_form.addHGFormElement( security_answer_field );

	//create a container for the body text and the form
	var pw_body_container = new SimpleWidgetFactory().build();

	pw_body_container.addContentNode( body_text );
	pw_body_container.addContentNode( new_user_form.form );

	pw_body_container.setWidgetStyle( "clear", "both" );

	//pw.body.addContentNode( body_text );
	//pw.body.addContentNode( new_user_form.form );
	pw.body.addContentNode( pw_body_container.getWidgetDOM() );

	//create the submit button
	var sbut = submitHGInsertNewUserButtonFactory( this.hg_page.style_set + "_control_button", pw, new_user_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
	pw.popup_from = this.hg_page;

}

/**
* submitHGInsertNewUserButtonFactory
**/
function submitHGInsertNewUserButtonFactory( style_set, pw, new_user_form ) { 

	var rbut = new SimpleButtonWidget( style_set , 
		new HGInsertNewUserCommand ( 
			pw, 
			new_user_form.getHGFormElement( "user_name" ) , 
			new_user_form.getHGFormElement( "verbose_name" ) ,
			new_user_form.getHGFormElement( "password" ) ,
			new_user_form.getHGFormElement( "email" ) ,
			new_user_form.getHGFormElement( "sec_question" ) ,
			new_user_form.getHGFormElement( "sec_answer" )
		) 
	);

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}




/**
* HGInsertNewUserCommand	
* 
* Generates a insert new user command which is sent to the server
*
* @param pw The popup window generating the command
* @param user_name_field 
* @param verbose_name_field
* @param password_field
* @param email_field
* @param sec_question_field
* @param sec_answer_field
**/
function HGInsertNewUserCommand( pw, user_name_field, verbose_name_field, password_field, email_field, sec_question_field, sec_answer_field ) {

	this.popup_window = pw;
	this.user_name_field = user_name_field;
	this.verbose_name_field = verbose_name_field;
	this.password_field = password_field;
	this.email_field = email_field;
	this.sec_question_field = sec_question_field;
	this.sec_answer_field = sec_answer_field;

}

new HGInsertNewUserCommand();

HGInsertNewUserCommand.prototype.execute = function() { 

	// <-- check form for validity?

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGInsertNewUserCommand" );
	nreq.addRequestElement( 'user_name', this.user_name_field.getValue() );
	nreq.addRequestElement( 'verbose_name', this.verbose_name_field.getValue() );
	nreq.addRequestElement( 'password', this.password_field.getValue() );
	nreq.addRequestElement( 'email', this.email_field.getValue() );
	nreq.addRequestElement( 'sec_question', this.sec_question_field.getValue() );
	nreq.addRequestElement( 'sec_answer', this.sec_answer_field.getValue() );

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGInsertNewUserRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "registering" );

}

function handleHGInsertNewUserRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//close popup window
		var cpw = new ClosePopupWindowCommand( document.body.windowing_environment, t );
		cpw.execute();

	}

	else { 

		t.setStatusToFailed( "unable to register user" );

	}

}
































/**
* updateUserPopupButtonFactory
*
* A factory for building update user buttons
*
* @param p The HGPage the button should be associated with
**/
function updateUserPopupButtonFactory( p ) { 

	var rbut = new SimpleButtonWidget( p.style_set + "_control_button", new HGUpdateUserPopupCommand( p ) );

	rbut.addContentNode( textToDOM( "span", "modify account information" ) );

	return rbut;

}

/**
* HGUpdateUserPopupCommand
*
* Generate a popup window containing all fields necessary for updating a user
*
* @param p The page the user is logged into
**/
function HGUpdateUserPopupCommand( p ) { 

	this.hg_page = p;

}

new HGUpdateUserPopupCommand();

HGUpdateUserPopupCommand.prototype.execute = function() { 

	//create the popup window
	var pw = new SimplePopupWindow( 
	"update_user_popup_window" ,
	"Update User Window" ,
	"hg_popup" , 
	300 , 
	600 , 
	15 ,
	true , 
	false ,
	true
	); 

	//create the window title
	var win_title = textToDOM( "span", "Update Account Information" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	var body_text = textToDOM( "div" , 
		"<br/><b>Modifying Your Account</b><br/> " + 
		"So you want to change up your account?  Just fill in the fields below which corrispond to " + 
		"the information you wish to update.  For instance, if you wish to update your Verbose Name, fill in that field.  " + 
		"If however you do NOT want to update your E-Mail, leave that field blank.  <br/><br/>" + 
		"Also, be sure to fill in the Old Password field for user validation type purposes. <br/><br/>");

	//create the form
	var update_user_form = new HGForm();

	var new_user_name_field = new HGFormElement ( 
		"text" ,
		"" , 
		"new_user_name" ,
		"20" ,
		"1" ,
		"20" ,
		null ,
		null ,
		"New User Name" ,
		"The name which will be used to log onto recursive-living.com"
	);

	var new_verbose_name_field = new HGFormElement (
		"text" , 
		"" , 
		"new_verbose_name" ,
		"20" , 
		"1" ,
		"30" ,
		null , 
		null ,
		"New Verbose Name" ,
		"The name which will be displayed to other users in your postings"
	);

	var old_password_field = new HGFormElement ( 
		"password" , 
		"" ,
		"old_password" , 
		"20" ,
		"1" , 
		"20" , 
		null ,
		null ,
		"*Old Password" ,
		"Your current recursive-living.com Password"
	);

	var new_password_field = new HGFormElement ( 
		"password" , 
		"" ,
		"new_password" , 
		"20" ,
		"1" , 
		"20" , 
		null ,
		null ,
		"New Password" ,
		"The password you wish to user to log onto recursive-living.com"
	);

	var new_email_field = new HGFormElement (
		"text" , 
		"" , 
		"new_email" ,
		"20" , 
		"1" ,
		"30" ,
		null , 
		null ,
		"New E-Mail" ,
		"Your e-mail address. Used for update notifications and password retrieval"
	);
	
	var new_security_question_field = new HGFormElement (
		"text" , 
		"" , 
		"new_sec_question" ,
		"20" , 
		"1" ,
		"50" ,
		null , 
		null ,
		"New Security Question" ,
		"A question used to retrieve a forgotten password"
	);

	var new_security_answer_field = new HGFormElement (
		"text" , 
		"" , 
		"new_sec_answer" ,
		"20" , 
		"1" ,
		"30" ,
		null , 
		null ,
		"New Security Answer" ,
		"The correct answer to your security question.  This answer is case sensitive"
	);

	update_user_form.addHGFormElement( new_user_name_field );
	update_user_form.addHGFormElement( new_verbose_name_field );
	update_user_form.addHGFormElement( old_password_field );
	update_user_form.addHGFormElement( new_password_field );
	update_user_form.addHGFormElement( new_email_field );
	update_user_form.addHGFormElement( new_security_question_field );
	update_user_form.addHGFormElement( new_security_answer_field );

	//create a container for the body text and the form
	var pw_body_container = new SimpleWidgetFactory().build();

	pw_body_container.addContentNode( body_text );
	pw_body_container.addContentNode( update_user_form.form );

	pw_body_container.setWidgetStyle( "clear", "both" );

	//pw.body.addContentNode( body_text );
	//pw.body.addContentNode( new_user_form.form );
	pw.body.addContentNode( pw_body_container.getWidgetDOM() );

	//create the submit button
	var sbut = submitHGUpdateUserButtonFactory( this.hg_page.style_set + "_control_button", pw, update_user_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
	pw.popup_from = this.hg_page;

}

/**
* submitHGUpdateUserButtonFactory
**/
function submitHGUpdateUserButtonFactory( style_set, pw, update_user_form ) { 

	var rbut = new SimpleButtonWidget( style_set , 
		new HGUpdateUserCommand ( 
			pw, 
			update_user_form.getHGFormElement( "new_user_name" ) , 
			update_user_form.getHGFormElement( "new_verbose_name" ) ,
			update_user_form.getHGFormElement( "old_password" ) ,
			update_user_form.getHGFormElement( "new_password" ) ,
			update_user_form.getHGFormElement( "new_email" ) ,
			update_user_form.getHGFormElement( "new_sec_question" ) ,
			update_user_form.getHGFormElement( "new_sec_answer" )
		) 
	);

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}




/**
* HGUpdateUserCommand	
* 
* Generates a update user command which is sent to the server
*
* @param pw The popup window generating the command
* @param new_user_name_field 
* @param new_verbose_name_field
* @param old_password_field
* @param new_password_field
* @param new_email_field
* @param new_sec_question_field
* @param new_sec_answer_field
**/
function HGUpdateUserCommand( 
				pw , 
				new_user_name_field , 
				new_verbose_name_field ,
				old_password_field , 
				new_password_field , 
				new_email_field , 
				new_sec_question_field , 
				new_sec_answer_field 
				) {

	this.popup_window = pw;
	this.new_user_name_field = new_user_name_field;
	this.new_verbose_name_field = new_verbose_name_field;
	this.new_password_field = new_password_field;
	this.old_password_field = old_password_field;
	this.new_email_field = new_email_field;
	this.new_sec_question_field = new_sec_question_field;
	this.new_sec_answer_field = new_sec_answer_field;

}

new HGUpdateUserCommand();

HGUpdateUserCommand.prototype.execute = function() { 

	// <-- check form for validity?

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUpdateUserCommand" );
	if ( this.new_user_name_field.getValue() != "" ) nreq.addRequestElement( 'new_user_name', this.new_user_name_field.getValue() );
	if ( this.new_verbose_name_field.getValue() != "" ) nreq.addRequestElement( 'new_verbose_name', this.new_verbose_name_field.getValue() );
	if ( this.old_password_field.getValue() != "" ) nreq.addRequestElement( 'old_password', this.old_password_field.getValue() );
	if ( this.new_password_field.getValue() != "" ) nreq.addRequestElement( 'new_password', this.new_password_field.getValue() );
	if ( this.new_email_field.getValue() != "" ) nreq.addRequestElement( 'new_email', this.new_email_field.getValue() );
	if ( this.new_sec_question_field.getValue() != "" ) nreq.addRequestElement( 'new_sec_question', this.new_sec_question_field.getValue() );
	if ( this.new_sec_answer_field.getValue() != "" ) nreq.addRequestElement( 'new_sec_answer', this.new_sec_answer_field.getValue() );

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGUpdateUserRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "updating account" );

}

function handleHGUpdateUserRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//close popup window
		var cpw = new ClosePopupWindowCommand( document.body.windowing_environment, t );
		cpw.execute();

	}

	else { 

		t.setStatusToFailed( "unable to update user" );

	}

}


