// JavaScript Document

/* Notes: url to captcha image hardcoded
					multiple instances with captcha - must refresh image - or will be wrong.
*/

var login_form_count = 0; // for multiple instances

// uses common functions:
// jslink


function LoginForm(main)
{
	var id = 'login_form'+login_form_count++;
	var registration_id = id+'registration';
	var login_id 				= id+'login';
	var forgot_id 				= id+'forgot';	
	var chooser = document.createElement('DIV');
			chooser.className = 'boxtitle center';

	var choose_login = document.createElement('A');
			choose_login.appendChild(document.createTextNode('Login'));
			choose_login.href = 'javascript:void(0)';
			choose_login.onclick = function() {
				show_login();
			}
	var choose_registration = document.createElement('A');
			choose_registration.appendChild(document.createTextNode('Register'));
			choose_registration.href = 'javascript:void(0)';			
			choose_registration.onclick = function() {
				show_registration();
			}

	var set_choice_styles = function(choice)
	{
		choose_registration.className = (choice == 'register') ? 'login_selected' : 'login_unselected';
		choose_login.className 				= (choice == 'login') ? 'login_selected' : 'login_unselected';
	}
	
	var container = document.createElement('DIV');
	
	chooser.appendChild(choose_login);
	chooser.appendChild(document.createTextNode(' / '));
	chooser.appendChild(choose_registration);
		
	main.appendChild(chooser);
	main.appendChild(container);
	
	var show_forgot = function()
	{
		set_choice_styles('forgot');
		if (xGetElementById(login_id)) { xGetElementById(login_id).style.display = 'none';	}
		if (xGetElementById(registration_id)) { xGetElementById(registration_id).style.display = 'none';	}
		if (xGetElementById(forgot_id)) { xGetElementById(forgot_id).style.display = ''; get_captcha({move_to:forgot_captcha_container}); return false; }
		var forgot 	 = document.createElement('FORM');
				forgot.onsubmit = function() {
					submit_form(forgot);
					return false;
				}
				forgot.id = forgot_id;
				forgot.line_break = line_break;
				forgot.add_text 	= add_text;	
		
		var findby = new_input();
		var action = new_input('hidden');
				action.value = 'forgot';
		var word 					= new_input();

		var default_note = document.createElement('SPAN');
				default_note.id = forgot_id+'_default_note';
				
		forgot_captcha_container.onclick = function() {
			word.focus();
		}
		get_captcha({move_to:forgot_captcha_container});

		
		var submit_button = new_input('SUBMIT');
				submit_button.value = 'Send Account Information';
				submit_button.className = 'create_account';
		var submit_para = document.createElement('P');
				submit_para.className = 'center';
				submit_para.appendChild(submit_button);
				
		findby.name 				= 'findby';
		word.name						= 'word';
		action.name					= 'action';

		findby.original_note 	= 'Enter either you username or the email address on file for your account.';
		findby.note 					= findby.original_note;
		word.note 						=	'If you can\'t read the word click on the image to refresh.';
		
		findby.className			= 'fillwindow';
		word.className 				= 'fillwindow';
			
		forgot.appendChild(action);
		
		forgot.line_break();
		forgot.add_text('username or email address:');
		forgot.line_break();
		forgot.appendChild(findby);

		forgot.line_break();		
		forgot.appendChild(forgot_captcha_container);
		forgot.line_break();				
		forgot.add_text('word above:');
		forgot.line_break();
		forgot.appendChild(word);

		forgot.appendChild(submit_para);
		
		forgot.appendChild(default_note);
		
		container.appendChild(forgot);
		
		xAddEventListener(findby,'focus',show_note,false);							// show note on focus
		xAddEventListener(word,'focus',show_note,false);									// show note on focus				
	}								

	var show_login = function()
	{
		set_choice_styles('login');
		if (xGetElementById(registration_id)) { xGetElementById(registration_id).style.display = 'none';	}
		if (xGetElementById(forgot_id)) { xGetElementById(forgot_id).style.display = 'none';	}
		if (xGetElementById(login_id)) { xGetElementById(login_id).style.display = '';  return false; }
		var login 	 = document.createElement('FORM');
				login.id = login_id;
				login.onsubmit = function() {
					$('demBinfo1').value = screen.width + ' x ' + screen.height;
					submit_form(this);
					return false;
				}				
				login.add_text 	= add_text;
				login.line_break = line_break;
		var username = new_input();
		var password = new_input('PASSWORD');
		var submit_button = new_input('SUBMIT');
				submit_button.value = 'Login';
		var submit_para = document.createElement('P');
				submit_para.className = 'center';
				submit_para.appendChild(submit_button);
		var remember_me = new_input('CHECKBOX');
				remember_me.checked = false;				
		var action = new_input('hidden');
				action.value = 'login';
		var default_note = document.createElement('SPAN');
				default_note.id = login_id+'_default_note';
		var forgot = commonjs.jslink('forgot username or password?');
				forgot.className = 'small_text';
				forgot.onclick = function() {
					show_forgot();
				}

			var binfo = new_input('HIDDEN');
					binfo.id = 'demBinfo1';
					binfo.name = 'binfo';
		
		username.name 				= 'username';
		password.name 				= 'password';
		remember_me.name 			= 'remember_me';		
		action.name						= 'action';

		username.className = 'fillwindow';
		password.className = 'fillwindow';		
		
		submit_para.appendChild(document.createElement('BR'));
		submit_para.appendChild(forgot);
		
		login.appendChild(action);
		login.appendChild(binfo);
		
		login.line_break();
		login.add_text('username:');
		login.line_break();		
		login.appendChild(username);
		
		login.line_break();
		login.add_text('password:');
		login.line_break();
		login.appendChild(password);
		
		login.line_break();
		login.appendChild(remember_me);
		login.add_text(' remember me');
		
		login.appendChild(default_note);		
		
		login.appendChild(submit_para);
		
		container.appendChild(login);
		
	}

	var new_input = function(type)
	{
		if (!type) type = 'text';
		var input = document.createElement('INPUT');
				input.type = type;
		return input;
	}

	var line_break = function() {
		this.appendChild(document.createElement('BR'));
	}

	var add_text = function(text) {
		if (!text) text = ' ';
		this.appendChild(document.createTextNode(text));
	}

	var captcha_refresh = function()
	{
		var src 		= jsconfig.captcha;
				src = src+"?"+Math.round(Math.random()*100000);	
		return src;
	}

	var captcha = document.createElement('IMG');
			captcha.src = captcha_refresh();
			captcha.className = 'captcha';
			captcha.onclick = function() {
				captcha.src = captcha_refresh();
			}				

	var register_captcha_container = document.createElement('DIV');
	var forgot_captcha_container = document.createElement('DIV');

	var get_captcha = function(parameters)
	{
		if (parameters.refresh_captcha) captcha.src = captcha_refresh();
		if (parameters.move_to) parameters.move_to.appendChild(captcha);
	}
				
	var show_registration = function()
	{
		set_choice_styles('register');		
		if (xGetElementById(login_id)) { xGetElementById(login_id).style.display = 'none';	}
		if (xGetElementById(forgot_id)) { xGetElementById(forgot_id).style.display = 'none';	}		
		if (xGetElementById(registration_id)) { xGetElementById(registration_id).style.display = ''; get_captcha({move_to:register_captcha_container}); return false; }
		var registration 	 = document.createElement('FORM');
				registration.onsubmit = function() {
					submit_form(registration);
					return false;
				}				
				registration.id = registration_id;
				registration.line_break = line_break;
				registration.add_text 	= add_text;	
		var username = new_input();
		var password = new_input('password');
		var action = new_input('hidden');
				action.value = 'register';
		var confirm_password = new_input('password');
		var email = new_input();
		var referral_code = new_input();
		var word 					= new_input();
		
				register_captcha_container.onclick = function() {
					word.focus();
				}

				get_captcha({move_to:register_captcha_container});
		
		var submit_button = new_input('SUBMIT');
				submit_button.value = 'Create My Account!';
				submit_button.className = 'create_account';
		var submit_para = document.createElement('P');
				submit_para.className = 'center';
				submit_para.appendChild(submit_button);
				
		username.name 				= 'username';
		username.id						= id+'username';
		password.name 				= 'password';
		password.id						= id+'password';		
		confirm_password.name = 'confirm_password';
		confirm_password.id		= id+'confirm_password';				
		email.name 						= 'email';
		referral_code.name 		= 'referral_code';
		word.name 						= 'word';
		action.name						= 'action';

		username.original_note 	= 'Your username must be 3-16 character and must start with a letter.';
		username.note 					= username.original_note;
		password.note 					= 'Your password must be at least 6 characters long.';
		confirm_password.note 	= 'Please enter your password again to ensure that you typed it correctly.';				
		email.original_note			= 'a valid email address is the only way to find forgotten passwords.';	
		email.note 							= email.original_note;
		word.note 							=	'If you can\'t read the word click on the image to refresh.';
		referral_code.note			=	'If you have a refferal code enter it in here.';		
		
		username.className 					= 'fillwindow';
		password.className 					= 'fillwindow';
		confirm_password.className 	= 'fillwindow';
		email.className 						= 'fillwindow';		
		word.className 							= 'fillwindow';
		referral_code.className 		= 'fillwindow';		
		
		confirm_password.disabled = true;
		
		registration.appendChild(action);
		
		registration.line_break();
		registration.add_text('username:');
		registration.line_break();
		registration.appendChild(username);

		registration.line_break();
		registration.add_text('password:');
		registration.line_break();
		registration.appendChild(password);

		registration.line_break();
		registration.add_text('confirm password:');
		registration.line_break();
		registration.appendChild(confirm_password);

		registration.line_break();
		registration.add_text('email:');
		registration.line_break();
		registration.appendChild(email);

		registration.line_break();		
		registration.appendChild(register_captcha_container);
		registration.line_break();				
		registration.add_text('word above:');
		registration.line_break();
		registration.appendChild(word);

		registration.line_break();
		registration.add_text('referral code (optional):');
		registration.line_break();
		registration.appendChild(referral_code);

		registration.appendChild(submit_para);
		
		container.appendChild(registration);		
		
		xAddEventListener(username,'change',check_username,false);				// check username on change
		xAddEventListener(password,'keyup',check_password,false);					// check password on keyup
		xAddEventListener(email,'change',check_email,false);								// check email on blur
		xAddEventListener(confirm_password,'keyup',check_password,false);	// check confirm password on keyup		
		xAddEventListener(email,'focus',show_note,false);									// show note on focus
		xAddEventListener(word,'focus',show_note,false);									// show note on focus		
		xAddEventListener(password,'focus',show_note,false);							// show note on focus
		xAddEventListener(confirm_password,'focus',show_note,false);			// show note on focus		
		xAddEventListener(username,'focus',show_note,false);							// show note on focus
		xAddEventListener(referral_code,'focus',show_note,false);					// show note on focus
		
	}

	var show_note = function(evt,element)
	{
 		if (!element)
		{
			var e = new xEvent(evt);
			var element = e.target;
		}
		if(xGetElementById(id+'_note')) var note = xGetElementById(id+'_note');
		else {
				var	note = document.createElement('DIV');
				note.id = id+'_note';
				note.className = 'form_note';
		}
		note.innerHTML = element.note;
		element.parentNode.insertBefore(note,element.nextSibling);
	}

	var check_password = function(evt)
	{
 		var e 						= new xEvent(evt);		
		var element 			= e.target;
		var password 			= xGetElementById(id+'password');
		var confirm_pass 	= xGetElementById(id+'confirm_password');				

		if (element == password) {
			confirm_pass.className = 'fillwindow';
			if (password.value.length >= 6) {
				password.className = 'fillwindow green';
				confirm_pass.disabled = false;
				if (password.value == confirm_pass.value) confirm_pass.className = 'fillwindow green';
				else confirm_pass.className = 'fillwindow red';
			}
			else {
				if (password.value.length > 0) password.className = 'fillwindow red';
				confirm_pass.disabled  = true;
			}
		}
		else {
			if (password.value.length >= 6 && confirm_pass.value == password.value ) confirm_pass.className = 'fillwindow green';
			else confirm_pass.className = 'fillwindow red';			
		}
	}

	var check_email = function(evt)
	{
 		var e 						= new xEvent(evt);
		var email 				= e.target;
		if (email.value.length == 0) { email.className = 'fillwindow'; return false; }
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email.value)) { email.className = 'fillwindow red'; return false; }
	
		var email_addr = urlencode(email.value);
		xmlhttp.open("GET", jsconfig.site.root+"library/account_actions.php?action=checkemail&email="+email_addr,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var response = JSON.parse(xmlhttp.responseText);
				if (response == false) { alert(xmlhttp.responseText); }
				else
				{
					if (response.email == true) {						
						email.className = 'fillwindow green';
						email.note = email.original_note;
					}
					else {
						email.className = 'fillwindow red';
						email.note = 'This email address is invalid or already in use.';
						email.focus();
						email.select();
					}
				}
			}
		}
		xmlhttp.send(null)					
	}

	var check_username = function()
	{
		username = urlencode(xGetElementById(id+'username').value);
		xmlhttp.open("GET", jsconfig.site.root+"library/account_actions.php?action=checkusername&username="+username,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var response = JSON.parse(xmlhttp.responseText);
				if (response == false) { alert(xmlhttp.responseText); }
				else
				{
					var input = xGetElementById(id+'username');
					if (response.username == true) {						
						input.className = 'fillwindow green';
						input.note = input.original_note;
					}
					else {
						input.className = 'fillwindow red';
						input.note = 'This username is unavailable';
						input.focus();
						input.select();
					}
				}
			}
		}
		xmlhttp.send(null)
	}

	var submit_form = function(theform)
	{
		var poststr = Form.serialize(theform);
		Form.disable(theform);
		xmlhttp.open("POST", jsconfig.site.root+"library/account_actions.php",true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(poststr);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status == 200) {
					var response = JSON.parse(xmlhttp.responseText);
					if (response == false) { 
							alert(xmlhttp.responseText);
							Form.enable(theform);
							return false;
					}
					else if (response.action == 'login') handle_login_response(response,theform);
					else if (response.action == 'register') handle_register_response(response,theform);
					else if (response.action == 'forgot') handle_forgot_response(response,theform);					
					else Form.enable(theform);
				}
			}
		}
	}

	// set these if we want to be able to access these function outside of object
	// ----------------------------------------------
	this.show_login = function() {
		show_login();
	}
	this.show_registration = function() {
		show_registration();
	}
	// ----------------------------------------------	
	
	var handle_login_response = function(response,theform)
	{
		if (response.valid == true) window.location.replace(jsconfig.site.root+'redirect.php?url='+window.location);
		else { 
			var element = xGetElementById(login_id+'_default_note');
					element.note = response.message ? response.message : 'Password or username is invalid.';
			show_note(null,element);
			Form.enable(theform);
		}
	}

	var handle_forgot_response = function(response,theform)
	{
		if (response.error)
		{
			var element = xGetElementById(forgot_id+'_default_note');
					element.note = response.error;
			show_note(null,element);
			captcha.src = captcha_refresh();
			Form.enable(theform);			
		}
		else 
		{
			show_login();
			var element = xGetElementById(login_id+'_default_note');
					element.note = response.msg;
			show_note(null,element);
			theform.findby.value = '';
			theform.word.value = '';
			captcha.src = captcha_refresh();
			Form.enable(theform);
		}
	}


	var handle_register_response = function(response,theform)
	{
		if (response.valid == true) window.location.replace(jsconfig.site.root+'redirect.php?url='+window.location);
		else {
			var errorStr = 'Please correct the following errors:';
			var count = 1;
		  if (response.errors) {
				if (!response.errors.username) errorStr += '\n'+(count++)+'. Email address is invalid';
				if (!response.errors.password) errorStr += '\n'+(count++)+'. Your password is invalid';	
				if (!response.errors.email)    errorStr += '\n'+(count++)+'. Please enter a valid email address';
				if (!response.errors.captcha)  errorStr += '\n'+(count++)+'. The word you entered was incorrect';								
			}
			alert(errorStr); 
			Form.enable(theform); 
		}
	}

	show_login();
	
}