// js/lilo.js

// dependencies: common.js, tw-sack.js

function login()
{
	if ( false == login_GATEKEEPER() ) return;
	login_SETUP_AJAX();
	ajax.onCompletion = login_AJAX_COMPLETE;
	ajax.runAJAX();
}

function login_GATEKEEPER()
{
	if ( $.trim($('#txtLoginEmail').val()) == '' )
	{
		$('#txtLoginEmail').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtLoginEmail').css('background-color','').focus();
			},
			1000);
		return false;
	}
	if ( $.trim($('#txtLoginPassword').val()) == '' )
	{
		$('#txtLoginPassword').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtLoginPassword').css('background-color','').focus();
			},
			1000);
		return false;
	}
	$('#butLogin').attr('disabled','disabled');
	return true;
}

function login_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("attempt_login", true);
	ajax.setVar("txtEmail", $('#txtLoginEmail').val());
	ajax.setVar("txtPassword", $('#txtLoginPassword').val());
	ajax.requestFile = "index.php";
}

function login_AJAX_COMPLETE()
{
	if ( ajax.response.substring(0,7) == 'success' )
	{
		// passing any argument at all to this function will prevent the page from reloading
		// this is a slight compromise to help with testing
		if ( arguments.length == 0 )
		{
			redirect('index.php');
		}
		return true;
	}
	else
	{
		safe_alert('The email/password combination you submitted was not found in our database, which means you either have not registered or misspelled something.  Please correct the problem and try again.  Contact PeachForms.com for further assistance.');
		$('#butLogin').attr('disabled','');
		return false;
	}
}

function update_name_email()
{
	if ( false == update_name_email_GATEKEEPER() ) return;
	update_name_email_SETUP_AJAX();
	ajax.onCompletion = update_name_email_AJAX_COMPLETE;
	ajax.runAJAX();
}

function update_name_email_GATEKEEPER()
{
	if ( $.trim($('#txtName').val()) == '' )
	{
		$('#txtName').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtLoginEmail').css('background-color','').focus();
			},
			1000);
		return false;
	}
	if ( ($.trim($('#txtEmail').val()) == '') || !check_email('#txtEmail') )
	{
		$('#txtEmail').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtEmail').css('background-color','').focus();
			},
			1000);
		return false;
	}
	if ( $.trim($('#txtPwd1').val()) == '' )
	{
		$('#txtPwd1').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtPwd1').css('background-color','').focus();
			},
			1000);
		return false;
	}
	return true;
}

function update_name_email_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("name", $('#txtName').val());
	ajax.setVar("email", $('#txtEmail').val());
	ajax.setVar("password", $('#txtPwd1').val());
	ajax.setVar("submission", 'home');
	ajax.requestFile = "backdoor.php";
}

function update_name_email_AJAX_COMPLETE()
{
	if ( ajax.response.substring(0,7) == 'success' )
	{
		var working = ajax.response.split('|');
		working.push('');
		working.push('');
		var cookie_value = working[1] + '|' + working[2];
		$.cookie('peachformsdotcom_remembered', cookie_value, {expires:365, path:'/'});
		
		// passing any argument at all to this function will prevent the page from reloading
		// this is a slight compromise to help with testing
		if ( arguments.length == 0 )
		{
			redirect('index.php');
		}
		return true;
	}
	else
	{
		safe_alert(ajax.response);
		return false;
	}
}

function update_password()
{
	if ( false == update_password_GATEKEEPER() ) return;
	update_password_SETUP_AJAX();
	ajax.onCompletion = update_password_AJAX_COMPLETE;
	ajax.runAJAX();
}

function update_password_GATEKEEPER()
{
	if ( ($.trim($('#txtPwd2').val()) == '') || !check_password('txtPwd2') )
	{
		$('#txtPwd2').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtPwd2').css('background-color','').focus();
			},
			1000);
		return false;
	}
	if ( ($.trim($('#txtPwd3').val()) == '') || !check_password('txtPwd3') )
	{
		$('#txtPwd3').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtPwd3').css('background-color','').focus();
			},
			1000);
		return false;
	}
	if ( $('#txtPwd3').val() != $('#txtPwd4').val() )
	{
		safe_alert('The values for New Password must match.');
		$('#txtPwd3').css('background-color','#FF8888');
		$('#txtPwd4').css('background-color','#FF8888');
		setTimeout(
			function(){
				$('#txtPwd4').css('background-color','');
				$('#txtPwd3').css('background-color','').focus().select();
			},
			1000);
		return false;
	}
}

function update_password_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("old_password", $('#txtPwd2').val());
	ajax.setVar("new_password", $('#txtPwd3').val());
	ajax.setVar("submission", 'home');
	ajax.requestFile = "backdoor.php";
}

function update_password_AJAX_COMPLETE()
{
	if ( ajax.response.substring(0,7) == 'success' )
	{
		var working = ajax.response.split('|');
		working.push('');
		working.push('');
		var cookie_value = working[1] + '|' + working[2];
		$.cookie('peachformsdotcom_remembered', cookie_value, {expires:365, path:'/'});
		
		// passing any argument at all to this function will prevent the page from reloading
		// this is a slight compromise to help with testing
		if ( arguments.length == 0 )
		{
			redirect('index.php');
		}
		return true;
	}
	else
	{
		safe_alert(ajax.response);
		return false;
	}
}

function register()
{
	if ( false == register_GATEKEEPER() ) return;
	register_SETUP_AJAX();
	ajax.onCompletion = register_AJAX_COMPLETE;
	ajax.runAJAX();
}

function register_GATEKEEPER()
{
	if ( !check_email('#txtRegisterEmail') )
	{
		safe_alert('The email address you submitted appears to be invalid.  Please double-check and contact PeachForms.com if further assistance is needed.');
		$('#txtRegisterEmail').focus();
		return false;
	}
	if ( !check_password('#txtRegisterPassword') )
	{
		$('#txtRegisterPassword').focus();
		return false;
	}
	var check = $.trim($('#txtRegisterName').val());
	check = strip_pipe(check);
	$('#txtRegisterName').val(check);
	$('#butRegister').attr('disabled','disabled');
	return true;
}

function register_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar('email', $('#txtRegisterEmail').val());
	ajax.setVar('password', $('#txtRegisterPassword').val());
	ajax.setVar('name', $('#txtRegisterName').val());
	ajax.setVar("submission", 'home');
	ajax.requestFile = "backdoor.php?register";
}

function register_AJAX_COMPLETE()
{
	if ( ajax.response != 'success' )
	{
		safe_alert(ajax.response);
	}
	else
	{
		safe_alert('Registration complete; thank you for using PeachForms.com!');
		redirect('index.php');
	}
}

function forgot_password()
{
	if ( false == forgot_password_GATEKEEPER() ) return;
	forgot_password_SETUP_AJAX();
	ajax.onCompletion = forgot_password_AJAX_COMPLETE;
	ajax.runAJAX();
}

function forgot_password_GATEKEEPER()
{
	if ( !check_email('#txtLoginEmail') )
	{
		safe_alert('Please provide your registered email address.');
		$('#txtLoginEmail').focus();
		return false;
	}
	return safe_confirm('Reset password?');
}

function forgot_password_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("txtLoginEmail", $('#txtLoginEmail').val());
	ajax.setVar("submission", 'home');
	ajax.requestFile = "backdoor.php?forgotpassword";
}

function forgot_password_AJAX_COMPLETE()
{
	if ( ajax.response != '' )
	{
		safe_alert(ajax.response);
	}
}

function check_email(_id)
{
	var trimmed = $.trim($(_id).val());
	$(_id).val(trimmed);
	var s = $(_id).val();
	return echeck(s);
}

function echeck(str)
{
	// courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	var valid = true;
	if (str.indexOf(at)==-1) valid = false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) valid = false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) valid = false;
	if (str.indexOf(at,(lat+1))!=-1) valid = false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) valid = false;
	if (str.indexOf(dot,(lat+2))==-1) valid = false;
	if (str.indexOf(" ")!=-1) valid = false;
	
	return valid;
}

function check_password(_id)
{
	if ( _id.substring(0,1) != '#' ) _id = '#' + _id;
	var trimmed = $.trim($(_id).val());
	$(_id).val(trimmed);
	var trimmed = $.trim($(_id).val());
	var s = $(_id).val();
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if ( (s.length < 6) || illegalChars.test(s) || !((s.search(/(A-Za-z)+/)) && (s.search(/(0-9)+/))) )
	{
		safe_alert("The password must be at least 6 characters long and must contain at least one letter and at least one number.");
		return false;
	}
	return true;
}

// end of file
