
/*

BBBop Main JavaScript File

jQuery fuckin rocks!

*/


//rewards form

function togglePhoneError(formId, message)
{
	if(message == '')
	{
		$('#'+ formId +' .phones').css('background-color', '#ffffff');
		$('#formErrors').css('display', 'none');
	}
	else
	{
		$('#'+ formId +' .phones').css('background-color', '#ff0000');
		$('#formErrors').html(message);
		$('#formErrors').css('display', 'block');
	}
}


function toggleEmailError(formId, message)
{
	if(message == '')
	{
		$('#'+ formId +' .emails').css('background', '#ffffff');
		$('#formErrors').css('display', 'none');
	}
	else
	{
		$('#'+ formId +' .emails').css('background', '#ff0000');
		$('#formErrors').html(message);
		$('#formErrors').css('display', 'block');
	}
}


function rewardsComplete(data)
{
//console.group('Rewards Complete');

	//possible todo: remove UI stuff indicateing processing
	
	//check there was no collision
	if(data.length == 0)
	{
		//notify user of phone number collision
		togglePhoneError('rewardsForm', 'Another Rewards Account is using this number, please provide another.');
		return false;
	}
	
	//var dataParts = data.split('|||');
//console.debug('dataParts: '+ dataParts);
	//$(dataParts[0]).appendTo("body");
	$('#content').html(data);
	
//console.groupEnd();
}


function processForm(formId)
{
//console.group('Process Form: '+ formId);

	//todo: add UI stuff to indicate processing
	var phone = email = false;
	var phones = $('#'+ formId +' .phones');
	var emails = $('#'+ formId +' .emails');

//console.debug(phones);
//console.debug(emails);
	
	//phone verification
	var x = 0;
	var phone = phone1 = '';
	while(x <= 2)
	{
		if($(phones[x]).val() == $(phones[x + 3]).val())
		{
			phone += $(phones[x]).val();
		}
		else
		{
			//notify phone numbers do not match
			togglePhoneError(formId, 'Rewards numbers entered do not match, please verify and correct the entries.');
			return false;
		}
		
		togglePhoneError(formId, '');
		x++;
	}
	
	if(phone.length != 10)
	{
		//notify user of illegal phone number
		togglePhoneError(formId, 'Rewards number entered is not 10 digits.  Please provide a 10 digit number.');
		return false;
	}
	
	//email verification
	if($(emails[0]).val() != $(emails[1]).val())
	{
		//notify emails do not match
		toggleEmailError(formId, 'Emails entered do not match, please verify and correct the entries.');
		return false;
	}
	
	toggleEmailError(formId, '');
	email = $(emails[0]).val();
	
	//process checkboxes
	var checkBoxQueryString = '';
	var checkBoxes = $('#'+ formId +' [type=checkbox]');
//console.debug(checkBoxes);
	
	x = 0;
	while(x <= 1)
	{
		checkBoxQueryString += '&'+ $(checkBoxes[x]).attr('name') +'='+ ($(checkBoxes[x]).attr('checked') ? '1' : '0');
		x++;
	}
	
	var normalData = $('#'+ formId +' .normalData').serialize();

	var queryString = normalData +'&email='+ email +'&phone='+ phone + checkBoxQueryString; 
//console.debug('queryString: '+ queryString);
//console.groupEnd();	
	$.ajax({
  		 type: 'POST',
  		 url: 'processRewardsForm.php',
  		 data: queryString,
   		 success: rewardsComplete
 	});
}


//rewards confirmation
function emailUpdated(data)
{
//console.group('Update Email Callback...');
	//update DOM elements
	data = data.replace(' ', '');
	$('input[name="email"]').val(data);
	$('#email').html(data).show(data);
	
	//undo UI
	cancelUpdate();
	
//console.groupEnd();
}


function correctEmail(acctId, acctEmail, resPath)
{	
	$('#email').hide();
	$('#input').show();
}


function updateEmail()
{
//console.group('Update Email AJAX...');
	var queryString = 'ajax=emailUpdate&id='+ $('input[name="accountId"]').val() +'&email='+ $('input[name="email"]').val();
	
	$.ajax({
  		 type: 'POST',
  		 url: 'UIinputContoller.php',
  		 data: queryString,
   		 success: emailUpdated
 	});
//console.groupEnd();
}


function cancelUpdate()
{
	$('#input').hide();
	$('#email').show();
}
