﻿//-----------------------------------------------------------------------------
// SignUp
//-----------------------------------------------------------------------------

function SignUpCreateAccount()
{
	HidePanel('SignUpErrorMessage');	
	$("#SignUpButton").attr("disabled", "true");

	var parameters = SignUpGetParameters();
	if (parameters == undefined)
	{
		SignUpShowErrorMessage("MissingFields", "Please fill in all of the fields");
		return;
	}
	
	if (parameters.TermsChecked == false)
	{
		SignUpShowErrorMessage("MissingTerms", "You must agree to the terms of use");
		return;
	}
	
	$.getJSON('/json', parameters, CreateValidateResponse);
	
	function CreateValidateResponse(response)
	{
		if (response.JsonResult.Result == 'Success')
		{
			HidePanel('fields');
			ShowPanel('working', '380px');
			parameters.Command = 'Group.Create';				
			$.getJSON('/json', parameters, CreateResponse);
		}
		else
		{
			SignUpShowErrorMessage(response.JsonResult.ErrorCode, response.JsonResult.ErrorDescription);
		}
	}
	
	function CreateResponse(response)
	{
		if (response.JsonResult.Result == 'Success')
		{
			window.location = response.RedirectUrl;
		}
		else
		{
			HidePanel('working');
			SignUpShowErrorMessage(response.JsonResult.ErrorCode, response.JsonResult.ErrorDescription);
		}
	}
}

//-----------------------------------------------------------------------------

function SignUpGetParameters(parameters)
{
	var parameters = 
	{
		Command: 'Group.CreateValidate',
		
		Email: $("#email").val(),
		Password: $("#password").val(),
		NameFirst: $("#name-first").val(),
		NameLast: $("#name-last").val(),
		Phone1: $("#phone-home").val(),
		Company: $("#company").val(),
		Country: $("select[id$='country']").val(),
		TimeZone: $("select[id$='timezone']").val(),
		TermsChecked: $("#terms:checked").is('[checked]'),
		TrainingRequested: $("#training:checked").is('[checked]'),
		
		LeadSource: $("input[name='SignupLeadSource']").val(),
		LeadRefererFirst: $("input[name='SignupLeadRefererFirst']").val(),
		LeadRefererLast: $("input[name='SignupLeadRefererLast']").val()
	};
	
	// Get rid of defaults if they are still there.
	if (parameters.Email == 'Email will be verified')
		parameters.Email = '';	
	if (parameters.Phone1 == 'Required for phone support')
		parameters.Phone1 = '';
	
	if ((parameters.Email.trim() == '') || (parameters.Password.trim() == '')
		|| (parameters.NameFirst.trim() == '') || (parameters.NameLast.trim() == '')
		|| (parameters.Phone1.trim() == '') || (parameters.Company.trim() == '')
		|| (parameters.Country.trim() == '')|| (parameters.TimeZone.trim() == ''))
		return null;
	else
		return parameters;
}

//-----------------------------------------------------------------------------

function SignUpShowErrorMessage(errorCode, errorMessage)
{
	var html = GetErrorHtml(errorCode, errorMessage);
	$("#SignUpErrorMessage").html(html);
	ShowPanel('fields');
	ShowPanel('SignUpErrorMessage', '', true);
	$("#SignUpButton").attr("disabled", "")
}

//-----------------------------------------------------------------------------
