/**************************************************

Filename:		login.js
Date Modified:	November 14, 2011
Modified By:	Chuck Bloodgood

Dependencies:	login.php 

Description:	javascript file for dealing with
				login.php

**************************************************/

// Create namespace and specify some meta-information
Login = {};
Login.NAME = "Login";
Login.VERSION = 1.0;
 
/*************************************************

Facebook login

**************************************************/ 

// Initialize
$(function() {
	// Facebook submit
	$('#facebookButton').bind('click', function() {	
	  	FB.login(
	  		function(response) {
	  			if (response.authResponse) {
	  				Login.facebookLogin();
	  			}
	  		}, 
       		{
       			scope: 'email, user_birthday'
      		}
   		);
   		
   		// Disable
   		return false;
	});
});

Login.facebookLogin = function() {
	// Call ajax
	$.ajax({
		url: './ajax/loginAjax.php',
		type: 'post',
		data: {
			action: 'facebookLogin'
		},
		dataType: 'json',
		success: function(response) {
			
			if (response.error) {
				//$('#facebookResults').html(response.error);
				$('#responseMessage').html(response.error);
			}
			else if (response.location) {
				window.location = response.location;
			}
		}
	});
}

/*************************************************

Forgot password

**************************************************/ 
Login.forgotPW = function($this)
{	   
	var email = $('#loginEmail').val();   
	var password = $('#loginPW').val();  
	
	// Call ajax
	$.ajax({
		url: './ajax/loginAjax.php',
		type: 'post',
		data: {
			action: 'forgotEmail', 
			email: email,
			password: password 
		},
		dataType: 'html',
		success: function($response) {
			$rsp = JSON.parse($response);
			// Show feedback
	   		var element = $('#responseMessage');
	   		var opts = {
	   			msg: $rsp[0],
	   			delay: 2000	
	   		};
			Global.ajaxFeedback(element, opts);
			setListeners();
		}
	}); 
}

Login.setNewPW = function($this)
{	   
	var password = $('#NewLoginPW').val(); 
	var confirm = urlParams["z"];
	var form = $this.closest("form"); 
	$('#passwordError').empty();  
	//call the login checker in accountCreation.js
	var isOK = AccountCreation.validatePassword(form); 
    if(isOK){  
  		// Call ajax
		$.ajax({
			url: './ajax/loginAjax.php',
			type: 'post',
			data: {
				action: 'setNewPW', 
				password: password ,
				confirm: confirm  
			},
			dataType: 'html',
			success: function($response) {
				$rsp = JSON.parse($response);
				// Show feedback
	   			var element = $('#setNewPW').html($rsp[1]);
				setListeners();
			}
		});  
	}		
}
 
/*************************************************

Set Listeners

**************************************************/ 
function setListeners(){
	$('.doForgotEmail').unbind('click');
	$('.doForgotEmail').click(function() { 
		Login.forgotPW($(this)); 
		$('.responseMessage').empty()
		// Disable the form
		return false;
	});
	$('.doSetNewPW').unbind('click');
	$('.doSetNewPW').click(function() { 
		Login.setNewPW($(this)); 
		$('.setNewPW').empty()
		// Disable the form
		return false;
	});
}  	
$(document).ready(function() { 
  setListeners();
});


/*************************************************

To Get Querystring

**************************************************/ 
var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

