// -------------------------------------------------------------------
// processCredentialsResponse()
//  This function gets verifies the response received from the ajax
//  routine that verifies the user credentials
// -------------------------------------------------------------------
function processCredentialsResponse(data){

	response = data;

	if(response.status == "VALID"){
		document.customerLogin.submit();
	}
	else if(response.status == "ACCOUNT_LOCKED"){
		var outPut = 'You have made too many failed attempts. Your password has been reset. ';
		outPut += '<a href="forgotPassword.php">Please click here to request your password by e-mail.';
		outPut += '</a>';
		
		
		document.getElementById('alert').innerHTML =  outPut;
	}
	else{
		document.getElementById('alert').innerHTML = "Your e-mail address and password " +
			"are incorrect, please try again.";
	}
}

// -------------------------------------------------------------------
// verifyUserCredentials()
//  This function gets the username and password and verifies if the
//  user credentials are correct.
// -------------------------------------------------------------------
function verifyUserCredentials(){
	// Builds paramater string
	var parameters = "username=" + document.getElementById('username').value 
		+ "&password=" + document.getElementById('password').value
		+ "&callback=?";
	
	var domain = document.getElementById('domainName').value;
	
	$.getJSON(
		"http://" + domain + "/web/js/httpRequests/verifyCredentials.php",
		parameters,
		function(data){
			processCredentialsResponse(data);
		});
}

// -------------------------------------------------------------------
// resetCustomerLoginForm()
//  This function clears the login alert message and resets the 
//  the customer login form.
// -------------------------------------------------------------------
function resetCustomerLoginForm(){
	document.getElementById('alert').innerHTML = "";
	
	document.customerLogin.reset();
}


// -------------------------------------------------------------------
// resetForgotPasswordForm()
//  This function clears the login alert message and resets the 
//  the customer login form.
// -------------------------------------------------------------------
function resetForgotPasswordForm(){
	document.getElementById('alert').innerHTML = "";
	
	document.forgotPassword.reset();
}

// -------------------------------------------------------------------
// verifyEmailAddress()
//  This function gets the email address and verifies if it exists
//  in the database.
// -------------------------------------------------------------------
function verifyEmailAddress(){
	// Builds paramater string
	var parameters = "email=" + document.getElementById('email').value;

	$.getJSON(
		"web/js/httpRequests/verifyEmailAddress.php",
		parameters,
		function(data){
			processEmailAddressResponse(data);
		});
}

// -------------------------------------------------------------------
// processEmailAddressResponse(data)
//  This function handles the response from the email verification
//  ajax routine.
// -------------------------------------------------------------------
function processEmailAddressResponse(data){
	
	response = data;

	if(response.status == "VALID"){
		document.forgotPassword.submit();
	}
	else{
		document.getElementById('alert').innerHTML = "Your e-mail address is not " +
		"registered to any user, please try again.";
	}
	
}