/*************************************************************************************************
 *  external.js                                                                                  *
 *  Copyright 2009 Skill Technology, LLC                                                         *
 *                                                                                               *
 *  Author:  Matt Haak                                                                           *
 *  Website: http://www.skilltechnology.com                                                      *
 *************************************************************************************************/

function submitForBestsByCourse(mode) {
	var parameters = {};
	parameters.mode = mode;
	parameters.action = GET_BESTS_BY_COURSE_ACTION;
	HTTP.get(parameters);
	return false;
}

/*  NOTE
 *  actionResponseHandler[GET_BESTS_BY_COURSE_ACTION] has been moved to common.js.
 */

function submitSignUpForm(sign_up_form) {
	var action_error_msg = "";

	var sign_up_name = document.getElementById('sign_up_name').value;
	var sign_up_passwd = document.getElementById('sign_up_passwd').value;
	var sign_up_passwd_conf = document.getElementById('sign_up_passwd_conf').value;
	var sign_up_email = document.getElementById('sign_up_email').value;
	var terms_accepted = document.getElementById('accept_terms').checked;

	if (	sign_up_name == "" ||
			sign_up_email == "" ||
			sign_up_passwd == "" ||
			sign_up_passwd_conf == "" ) {

		action_error_msg += ('<li>You must complete all fields</li>');

	} else {
	
		if (!isValidName(sign_up_name))
			action_error_msg += ('<li>Nicknames can contain only letters, numbers and underscores and must be 3 to 26 '
				+ 'characters long</li>');
	
		if (sign_up_passwd != sign_up_passwd_conf)
			action_error_msg += ('<li>The password and confirmation do not match</li>');
		else if (!sign_up_passwd.match(/^.{7,26}$/))
			action_error_msg += ('<li>Passwords must be 7 to 26 characters long</li>');


		if (!sign_up_email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i))
			action_error_msg += ('<li>The email address you\'ve provided is invalid</li>'); // You must
				// provide a valid email address to which you have access in order to complete
				// this sign up. --> *</li>
		
		if (!terms_accepted)
			action_error_msg += ('<li>You must accept the Terms of Service</li>');

	}
	
	if (action_error_msg == "") {
		var parameters = {};
		parameters.action = SIGN_UP_ACTION;
		// parameters.sign_up_fname = sign_up_fname;
		// parameters.sign_up_lname = sign_up_lname;
		parameters.sign_up_name = sign_up_name;
		parameters.sign_up_passwd = sign_up_passwd;
		parameters.sign_up_passwd_conf = sign_up_passwd_conf;
		parameters.sign_up_email = sign_up_email;
		parameters.terms_accepted = (terms_accepted ? 'Y' : 'N');
		parameters.browser_metrics = getBrowserMetrics();
		HTTP.get(parameters);
	} else {
		action_error_msg = ('<UL>' + action_error_msg + '</UL>');
		actionError(SIGN_UP_ACTION, action_error_msg);
	}

	return false;
}

actionResponseHandler[SIGN_UP_ACTION] = function(response) {
	clearActionError(SIGN_UP_ACTION);
	if (validateResponse(response)) {
		top.location = getHttpsBaseUrl() + 'a/?signup#' + response.payload.session.id;
	}
}

actionResponseHandler[UPDATE_SESSION_ACTION] = function(response) {
	clearActionError(UPDATE_SESSION_ACTION);
	if (validateResponse(response)) {
		// top.location = getHttpsBaseUrl() + 'a/#' + response.payload.session.id;
		top.location = getHttpsBaseUrl() + 'a/';
	} else {
		var login_btn_elmt = document.getElementById('login_btn');
		if (login_btn_elmt)
			login_btn_elmt.className = 'login_btn';
	}
}

function hideForgotPWForm() {
	var homeForgotPW_elmt = document.getElementById('homeForgotPW');
	document.getElementById('forgot_pw_placer').removeChild(homeForgotPW_elmt);
}

function showForgotPWForm() {
	var elmnt = document.getElementById('homeForgotPW');
	if (elmnt) {
		elmnt.style.display = "block";
	} else {
		elmnt = document.createElement("div");
		elmnt.setAttribute("align", "center");
		elmnt.setAttribute("id", "homeForgotPW");
		elmnt.innerHTML = '<form id="forgotPW_form" onSubmit="return submitForgotPWForm();">' +
			'<table border="0" cellspacing="16" cellpadding="0"><tr>' +
			'<td colspan="2"><p id="forgotPW_notice">Submit either one of the following in order to have an ' +
			'email containing your nickname and a new password sent to you.</p></td></tr>' +
			'<tr><td><p class="forgotPW_label">Email:</p></td>' +
			'<td><p class="forgotPW_input"><input type="text" id="forgetPW_email" autocomplete="off"/></p></td></tr>' +
			'<tr><td><p class="forgotPW_label">Nickname:</p></td>' +
			'<td><p class="forgotPW_input"><input type="text" id="forgetPW_nickname" autocomplete="off"/></p></td></tr>' +
			'<tr><td colspan="2"><p id="forgotPW_buttons">' +
			'<img src="a/imgs/progress.gif" height="30" width="30"' +
			' id="forgotPW_progress" style="float:right;position:relative;top:5px;display:none">' +
			'<img src="images/forgotPW/cancel.gif" width="90" height="40"' +
			' style="float:none; cursor:pointer;" onClick="hideForgotPWForm()">' +
			'<input type="image" src="images/forgotPW/submit.gif" width="90" height="40" style="float:none;">' +
			'</p></td></tr></table></form>';

		document.getElementById('forgot_pw_placer').appendChild(elmnt);
	}
}

function submitForgotPWForm() {
	var forgotPW_form = document.forms['forgotPW_form'];
	var parameters = {};
	parameters.action = FORGOT_PW_ACTION;
	parameters.email = forgotPW_form.elements['forgetPW_email'].value;
	parameters.nickname = forgotPW_form.elements['forgetPW_nickname'].value;
	if (parameters.email || parameters.nickname)
		HTTP.get(parameters);
	document.getElementById('forgotPW_progress').style.display = "block";
	return false;
}

var FORGOT_PW_MSG = [];
FORGOT_PW_MSG[0] = 'No player with the nickname or email you supplied has been found. You can ' +
	'contact accounts' + '@' + 'skilltechnology' + '.' + 'com if you know for sure that you have already signed up.';
FORGOT_PW_MSG[1] = 'No player with the nickname or email you supplied has been found. However, ' +
	'your email address is listed as being signed up for beta testing. You should reveive an email ' +
	'containing the invitation code needed to sign up for a player account once more people are ' +
	'invited to the beta test.';
FORGOT_PW_MSG[2] = 'No player with the email address you supplied has been found. However, ' +
	'you have been invited to join the beta test. An email containing an invitation code is being ' +
	're-sent to <EMAIL> and you can use that code to sign up as a new player.';
FORGOT_PW_MSG[3] = 'An email containing your player nickname and a new password has been sent to the email ' +
	'address associated with your account.';


actionResponseHandler[FORGOT_PW_ACTION] = function(response) {
	document.getElementById('forgotPW_progress').style.display = "none";
	if (validateResponse(response)) {
		var elmnt = document.getElementById('homeForgotPW');
		elmnt.innerHTML = '<table border="0" cellspacing="18" cellpadding="0"><tr>' +
			'<td><p id="forgotPW_notice">' +
			FORGOT_PW_MSG[response.payload.msg_idx].replace(/<EMAIL>/g, response.payload.email) +
			'</p></td></tr><tr><td><p id="forgotPW_buttons">' +
			'<img src="images/forgotPW/okay.gif" width="90" height="40"' +
			' style="float:none; cursor:pointer;" onClick="hideForgotPWForm()">' +
			'</p></td></tr></table>';
	}
}

if (window.slp)
	slp(3);