/*** PAGE FUNCTIONS ***/
auth.signup.invite = new Array();
auth.signup.invite.push (function()
{
	params = NEW_PAGE;
	var size = params.length;

	if( size > 2 && params[2] == 'invite')
	{
		//deal with the invite friends here
	}
});

auth.signup.betaSignup = new Array();
auth.signup.betaSignup.push (function()
{
	params = NEW_PAGE;
	var size = params.length;

	if( size > 4 && params[2] == 'betaSignup')
	{
		$('#betaKey').val(params[3]);
		//$('#signupEmail').focus();
		$('#signupEmail').val(html_decode(params[4]));
		//$('#signupFirstName').focus();
	}
});

auth.signup.redirect = new Array();
auth.signup.redirect.push (function()
{
	params = NEW_PAGE;
	var size = params.length;

	if( size > 3 && params[2] == 'redirect')
	{
		$('#redirect-overide').val(params.slice(3).join('/'));
	}
});

/*** SIGNUP ***/

function registerToBeta()
{
	if ($('#registerForm').valid())
	{
		var data = 'email=' + $('#register-email').val();
		
		$.ajax(
		{
			type: 'POST',
			url: 'ajax/business/registerToBeta.php',
			data: data,
			success: function(result)
			{
				$('#register-email').val('');
				$('#register-email').blur();
				result = handleValidConfirm( result, $('#register-errors'), $('#register-confirm') );
			}
		});
	}
}

function signup()
{
	
	if ($('#signupForm').valid())
	{
		showPageLoading();
		
		var data = 'firstName=' + $('#signupFirstName').val();
		data += '&lastName=' + $('#signupLastName').val();
		data += '&email=' + $('#signupEmail').val();
		data += '&location=' + $('#signupLocation').val();
		data += '&password=' + $('#signupPassword').val();
		data += '&type=' + $('#signupType').val();
		data += '&betaKey=' + $('#betaKey').val();
		
		$.ajax(
		{
			type: 'POST',
			url: 'ajax/controller/signup.php',
			data: data,
			success: function(result)
			{
				//there is no $('#signup-confirm') box, since if its sucessfull, we simply redirect,
				//so showing a confirm-box for 2 sec seems silly
				result = handleValidConfirm( result, $('#signup-errors'), $('#signup-confirm') );
				
				if (result.code == 'ok')
				{
					if ($('#redirect-overide').val() == '')
						processLink( result.redirect );
					else
					{
						processLink ($('#redirect-overide').val());
					}
				}
				else
				{
					hidePageLoading();
				}
			}
		});
	}
}

function signupFacebook()
{
	showPageLoading();
	$.ajax(
	{
		type: 'POST',
		url: 'ajax/controller/signupFacebook.php',
		success: function(result)
		{
		
			//there is no $('#social-signup-confirm') box, since if its sucessfull, we simply redirect/alter the page.
			result = handleValidConfirm( result, $('#social-signup-errors'), $('#social-signup-confirm') );
			if (result.code == 'ok')
			{
				processLink(result.redirect);
				
			}
			else 
			{
				if (result.code == 'info')
				{
					$('#signupSocial').hide();
					$('#vertical-line').hide();
					$('#signupType').val(result.type);
					$('#purpleText').text(result.text);

					//password is always given a random string juste to insure that the validation will work
					$('#divPassword').hide();
					//autocomplete everything
					$('#signupPassword').val(result.password);	
					
					$('#signupFirstName').focus();
					$('#signupFirstName').val(result.firstName);
					
					$('#signupLastName').focus();
					$('#signupLastName').val(result.lastName);
					
					$('#signupEmail').focus();
					$('#signupEmail').val(result.email);
					
					$('#signupLocation').focus();
					$('#signupLocation').val(result.location);
					
					
			
					if (!isEmpty(result.firstName) && !isEmpty(result.lastName))
					{
						$('#divName').hide();
					}
					
					if (!isEmpty(result.email) )
					{
						$('#divEmail').hide();
					}
					
					if (!isEmpty(result.location) )
					{
						$('#divLocation').hide();
					}
				}
				hidePageLoading();
			}	
		}
	});
}

function signupTwitter()
{
	showPageLoading();
	$.ajax(
	{
		type: 'POST',
		url: 'ajax/controller/signupTwitter.php',
		success: function(result)
		{
			//there is no $('#social-signup-confirm') box, since if its sucessfull, we simply redirect/alter the page.
			result = handleValidConfirm( result, $('#social-signup-errors'), $('#social-signup-confirm') );
			if (result.code == 'ok')
			{
				processLink(result.redirect);
			}
			else 
			{

				if (result.code == 'info')
				{
			
					
					$('#signupSocial').hide();
					$('#vertical-line').hide();
					$('#signupType').val(result.type);
					$('#purpleText').text(result.text);

					//password is always given a random string juste to insure that the validation will work
					$('#divPassword').hide();
					//autocomplete everything
					$('#signupPassword').val(result.password);
										
					$('#signupEmail').focus();
					$('#signupEmail').val(result.email);
					
					$('#signupLocation').focus();					
					$('#signupLocation').val(result.location);

					$('#signupLastName').focus ();
					$('#signupLastName').val(result.lastName);
					
					$('#signupFirstName').focus();
					$('#signupFirstName').val(result.firstName);
					
					
					if (!isEmpty(result.email) )
					{
						$('#divEmail').hide();
					}
					
					if (!isEmpty(result.location) )
					{
						$('#divLocation').hide();
					}
				}
				hidePageLoading();
			}	
		}
	});
}


/*** LOGIN ***/
function login()
{
	if ($('#loginForm').valid())
	{
		showPageLoading();
		var data = 'email=' + $('#email').val();
		data += '&password=' + $('#pass').val();
		
		$.ajax(
		{
			type: 'POST',
			url: 'ajax/controller/login.php',
			data: data,
			success: function(result)
			{
				result = json_decode( result );
				if (result.code == 'ok')
				{
					if ($('#redirect-overide').val() == '')
						processLink( result.redirect );
					else
					{
						processLink ($('#redirect-overide').val());
					}
					
				}
				else if(result.code == 'error')
				{
					hidePageLoading();
					$('#login-errors').show();
					$('#login-errors').html(result.content);
				}
			}
		});
	}
}





function loginFacebook()
{
	showPageLoading();
	$.ajax(
	{
		type: 'POST',
		url: 'ajax/controller/loginFacebook.php',
		success: function(result)
		{
			result = handleValidConfirm( result, $('#login-errors'), $('#login-confirm') );
			
			if( result.code == 'ok' )
			{
				processLink( result.redirect );
			}
			else 
			{
				hidePageLoading();
			}
		}
	});
}

function loginTwitter()
{
	showPageLoading();
	$.ajax(
	{
		type: 'POST',
		url: 'ajax/controller/loginTwitter.php',
		success: function(result)
		{
			result = handleValidConfirm( result, $('#login-errors'), $('#login-confirm') );
			
			if( result.code == 'ok' )
			{
				processLink( result.redirect );
				
			}
			else
			{
				hidePageLoading();
			}
		}
	});
}

/*** OTHERS ***/

function recoverPassword()
{
	if ($('#recoverForm').valid())
	{
		$('#recover-loading').show();
		var data = 'email=' + $('#recoverEmail').val();
		
		$.ajax(
		{
			type: 'POST',
			url: 'ajax/controller/resetPassword.php',
			data: data,
			success: function(result)
			{
				$('#recover-loading').hide();
				result = handleValidConfirm( result, $('#recover-errors'), $('#recover-confirm') );
			}
		});
	}
}