///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	CoPilot Creative Front End CMS AJAX Call Functions 
//	Copy Right of Propel Studios, LLC.
//	Developed by Thomas Eisenbeis
//	
//	
//	File Name = ajax_framework.js
// 	This will hold all neccessary functions for calls to actions within site that will allow updates without refresh
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



/* -------------------------- */
/* PROCESS REGISTRATION       */
/* -------------------------- */

function processReg()
{
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	$('#submit').html('Processing your request...<br><img src="/images/loader.gif" width="220" height="19" border="0" />').fadeIn('fast');
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var attend = $('#attending').val();
	var email = $('#email').val();
	var phone = $('#phone').val();
	var fname = $('#fname').val();
	var lname = $('#lname').val();
	
	
	var bname = $('#bname').val();
	var address = $('#address').val();
	var city = $('#city').val();
	var state = $('#state').val();
	var zip = $('#zip').val();
	var action = $('#action').val();
	var totalAmt = $('#totalAmt').val();
	
	if ($('#r_guest').length > 0)
	{
		var guest = $('#r_guest').val();
		attend = 2;
	}
	else {
		
		var guest = '';
		
	}
	
	if ($('#r_guest1').length > 0) {
		var guest1 = $('#r_guest1').val();
		attend = 3;
	}
		else { var guest1 = "";}
		
		if ($('#r_guest2').length > 0) {
			var guest2 = $('#r_guest2').val();
			attend = 4;
		}
			else { var guest2 = "";}
	

	
	if ($('#cnum').length > 0)
	{
	
		var ctype = $('#ctype').val();
		var cnum = $('#cnum').val();
		var expire = $('#expire').val();
		var cvc = $('#cvc').val();
	
		var cardUrl = '&ctype='+ctype+'&cnum='+cnum+'&expire='+expire+'&cvc='+cvc;
	
	
	}
	else if ($('#promo').length > 0)
	{
		
		var promo = $('#promo').val();
		var cardUrl = '&promo='+promo;
		
	} 
	else
	{
		
		var cardUrl = '';
		
	}
	
	/*alert('r_fname='+fname+'&r_lname='+lname+'&r_email='+email+'&r_address='+address+'&r_city='+city+'&r_state='+state+'&r_zip='+zip+'&r_attending='+attend+'&r_phone='+phone+'&r_guest='+guest+'&r_guest1='+guest1+'&r_guest2='+guest2+'&r_bname='+bname+''+cardUrl+'&action='+action+'&r_total='+totalAmt);*/
	
	// same button code for all responses
	var button = '<input type="image" src="/images/submit-lrg-btn.png" id="submit" name="submit" class="swapImage {src:\'/images/submit-lrg-btn-RO.png\'}" tabindex="22" />';
	
	$.ajax({
		type: "POST",
		url: "/includes/processPage.php",
		data: 'r_fname='+fname+'&r_lname='+lname+'&r_email='+email+'&r_address='+address+'&r_city='+city+'&r_state='+state+'&r_zip='+zip+'&r_attending='+attend+'&r_phone='+phone+'&r_guest='+guest+'&r_guest1='+guest1+'&r_guest2='+guest2+'&r_bname='+bname+''+cardUrl+'&action='+action+'&r_total='+totalAmt,
		success: function(data){
			var response = data.split('|',3);
			//alert(data);
			if (response[0] == 1)
			{
				
				// APPROVED
				window.location = 'success?transType='+response[1]+'&id='+response[2];
			
			}
			else if (response[0] == 2) 
			{
				
				// DECLINED
				$('#submit').html('<input type="image" src="/images/submit.png" id="submit" name="submit" class="domroll /images/submitRO.png" />').fadeIn('fast');
				$('#message').addClass('error').html('The transaction failed due to card being DECLINED').focus();
			
			}
			else if (response[0] == 3)
			{
				
				// ERROR
				$('#submit').html('<input type="image" src="/images/submit.png" id="submit" name="submit" class="domroll /images/submitRO.png" />').fadeIn('fast');
				$('#message').addClass('error').html('The transaction failed due to card being DECLINED').focus();
				
			}
			else if (response[0] == 4)
			{
				
				// VERIFICATION
				$('#submit').html('<input type="image" src="/images/submit.png" id="submit" name="submit" class="domroll /images/submitRO.png" />').fadeIn('fast');
				$('#message').addClass('error').html('Please verify your card information.').focus();
				
			}
			else if (response[0] == 0)
			{
				// PROMO CODE NOT MATCH$
				$('#submit').html(button).fadeIn('fast');
				inlineMsg('promo','Promo code invalid.',2);
							
			}
			else if (response[0] == 9)
			{
				
				// ERROR WITH SYSTEM
				$('#submit').html('<input type="image" src="/images/submit.png" id="submit" name="submit" class="domroll /images/submitRO.png" />').fadeIn('fast');
				$('#message').addClass('error').html('We already have a record for that email address. Thank you for registering.').focus();
				
			}
			else if (response[0] == 888)
			{
				
				// ERROR WITH SYSTEM
				$('#submit').html('<input type="image" src="/images/submit.png" id="submit" name="submit" class="domroll /images/submitRO.png" />').fadeIn('fast');
				$('#message').addClass('error').html('Sorry, we do not have you on our list for that promotion code. Please recheck the code and email you entered.').focus();
				
			}
			else if (response[0] == 111)
			{
				
				// ERROR WITH SYSTEM
				$('#submit').html(button).fadeIn('fast');
				$('#message').addClass('error').html('Please use the email address your evite was sent to complete registration. If you are still having trouble please contact the Independent directly at 577-4545').focus();	
			}
			else if (response[0] != 1 || response[0] != 2 || response[0] != 3 || response[0] != 4 || response[0] != 0 || response[0] != 888 || response[0] != 9 || response[0] != 111)
			{
				
				// ERROR WITH SYSTEM
				$('#submit').html('<input type="image" src="/images/submit.png" id="submit" name="submit" class="domroll /images/submitRO.png" />').fadeIn('fast');
				$('#message').addClass('error').html('There was an error processing your transaction. Please verify your information.').focus();
				
			}

	   },
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$('#message').removeClass().addClass('error').text('There was an error. Please try again.').fadeIn(800);
			$('#ccContainer').html('<div class="span-9 append-bottom prepend-top"><label>promo code</label><div><input type="text" id="promo" name="promo" class="span-8" value="" /></div></div><div class="span-8"><div id="submit"><input type="image" src="images/submit.png" id="submit" name="submit" class="domroll images/submitRO.png" /></div></div><input type="hidden" id="action" name="action" value="promo" /><input type="hidden" id="totalAmt" name="totalAmt" value="25" />').fadeIn('slow');
			setTimeout(function() {
				$('#message').fadeOut(800)
			}, 3000);
		}
	 });
	
}

/* -------------------------- */
/* PROCESS SIGNUP             */
/* -------------------------- */
/* Function processes mailing list signups */

function processStatus() {
	
	var email = $('#email').val();
	var fname = $('#fname').val();
	var lname = $('#lname').val();
	$('#send').fadeOut('fast');
	$('#message').html('Processing your request...<br><img src="/images/loader.gif" width="220" height="19" border="0" />').fadeIn('slow');
	
	$.ajax({
		type: "POST",
		url: "/includes/processPage.php",
		data: 'r_fname='+fname+'&r_lname='+lname+'&r_email='+email+'&action=noAttend',
		success: function(data){
			if (data == 1)
			{
			
				$('#message').html('<div class="highlight">We will see you next time. Thank you.</div>');
			
			}
			else if (data == 9) 
			{
				
				$('#message').addClass('error').html('We already have a record for that email address.<br />Thank you for registering.').focus();
			
			}

	   },
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$('#message').removeClass().addClass('error').text('There was an error. Please try again.').fadeIn(800);
			setTimeout(function() {
				$('#message').fadeOut(800)
			}, 3000);
		}
	 });
	
}
