
$(function() {

//	$('#pathoflife_form').submit(function() {
//		var queryString = '';
//		$('#pathoflife_form input').each(function() {
//												queryString+='&'+$(this).attr('name')+'='+$(this).val();
//											});
//		if (queryString!='')
//			queryString = queryString.substr(1);
//			
//		$.ajax({
//			type: 'POST',
//			url: $('#pathoflife_form').attr('action'),
//			data: queryString,
//			success: function(html) {
//				result
//			}
//		});
//	});
	$('#receive_quote_holder').html('<a href="javascript:void(0);" id="receive_quote"><img src="'+themeURL+'/images/receive.gif" width="358" height="30" alt="To receive the quote of the day, click here" title="" /></a>');
	$('#receive_quote').click(openReceiveQuote);

	$('#pathoflife_form').submit(showPathOfLife);

	$('#stress_test_finished').click(function() {
											  var abandon = false;
											  var total=0;
											  for (var a=1; a<26; a++)
											  {
												  	if (!isNaN(parseInt($('.q'+a+':checked').val())))
													  total+=parseInt($('.q'+a+':checked').val());
													 else
													 {
													 	alert('Please put an answer in for every question.');
														abandon = true;
														break;
													 }
											  }
											  if (!abandon)
											  {  
												  $('#four_or_less').removeClass('score_selected');
												  $('#five_to_thirteen').removeClass('score_selected');
												  $('#fourteen_or_more').removeClass('score_selected');
												  
												  if (total <= 4)
													$('#four_or_less').addClass('score_selected');
												  else if (total >= 14)
													$('#fourteen_or_more').addClass('score_selected');
												  else
													$('#five_to_thirteen').addClass('score_selected');
													
												$('#your_score').html('Your Score is '+total);
												$('#stress_result').css('left', ($(window).width() / 2) - ($('#stress_result').width() / 2));
												$('#stress_result').css('top', 200);
											  }
											});
	$('#close_stress_pop_up').click(function() { $('#stress_result').css('left', -3000); });
	$('#close_path_pop_up').click(function() { $('#path_of_life_graphic').css('left', -3000); });
	$('#close_sign_up_pop_up').click(function() { $('#sign_up').css('left', -3000); } );
	
	$('.book_course').click(show_terms)
	$('#close_booking_pop_up_box').click(function() { $('#terms_and_conditions').css('left', -3000); });
	$('#purchase_form').submit(submit_purchase);
});

function submit_purchase()
{
	var allow_continue = true;
	var dataString = '';
	
	$('#purchase_form input').each(function() { 
		if ($(this).val()=='')
		{
			if (allow_continue)
			{
				alert('Please fill in all of the information, before submitting your application.');
				allow_continue = false;
			}
		}
	});

	if (allow_continue)
	{
		$('#purchase_form input').each(function() { 
			dataString+=$(this).attr('name')+'='+$(this).attr('value')+'&'; 
			//alert(dataString);
		});
	
		dataString = dataString.substr(0, dataString.length -1);
		
		$.ajax({
			type: 'POST',
			url: themeURL+'/php/email_purchase_details.php',
			data: dataString,
			success: function(html) {
				$('#purchase_form').unbind();
				$('#purchase_form').submit();
			}			   
		});
		return false;
	}
	else
	{
		return false;	
	}
}

function show_terms()
{
	var purchase_info = $(this).attr('rel');
	var split_purchase_info = purchase_info.split('|');
	
	if (split_purchase_info.length==2)
	{
		$.ajax({
			type: 'POST',
			url: themeURL+'/php/get_purchase_form.php',
			data: 'item_name='+split_purchase_info[0]+'&item_price='+split_purchase_info[1],
			success: function(html) {
				$('#terms_and_conditions_form').html(html);
				$('#terms_and_conditions').css('left', ($(window).width() / 2) - ($('#terms_and_conditions').width() / 2));
			}
		});	
	}
	else
	{
		alert('We\'re sorry, something has gone wrong with booking this course. Please try again.');	
	}
}

function openReceiveQuote()
{
	//var signUpHTML = '<form action="'+themeURL+'/php/sendreceiverequest.php" method="post" id="sign_up_form">'+
	//'<a href="#" onclick="$(\'#sign_up\').html(\'\');$(\'#sign_up\').removeClass(\'sign_up\');" style="float:right; color:black; text-decoration:none; margin-top: -100px; margin-right: 50px;">X</a>'+
	//'<div class="sign_up_form_line"><label for="name">Name*</label><input type="text" name="name" id="name" value="" /></div>'+
	//'<div class="sign_up_form_line"><label for="email">E-mail*</label><input type="text" name="email" id="email" value="" /></div>'+
	//'<div class="sign_up_form_splitter"></div>'+
	//'<div class="sign_up_form_line"><label for="address1">Address Line 1</label><input type="text" name="address1" id="address1" value="" /></div>'+
	//'<div class="sign_up_form_line"><label for="address2">Address Line 2</label><input type="text" name="address2" id="address2" value="" /></div>'+
	//'<div class="sign_up_form_line"><label for="city">City</label><input type="text" name="city" id="city" value="" /></div>'+
	//'<div class="sign_up_form_line"><label for="postcode">Postcode</label><input type="text" name="postcode" id="postcode" value="" /></div>'+
	//'<input type="image" src="'+themeURL+'/images/sign_up_button.gif" id="sign_up_submit" /></form>';
	
	//$('#sign_up').addClass('sign_up');
	//$('#sign_up').html(signUpHTML);	
	$('#sign_up').css('left', ($(window).width() / 2) - ($('#sign_up').width() / 2));
	$('#sign_up').css('top', 200);
	$('#sign_up_submit').click(sendReceiveQuote);
}
function sendReceiveQuote()
{
	if ($('#name').val()=='' || $('#email').val()=='')
	{
		alert('Please fill in your name and e-mail address...');	
	}
	else
	{
		var dataString = '';
		
		$('#sign_up_form input').each(function() { 
			dataString+=$(this).attr('name')+'='+$(this).attr('value')+'&'; 
		});
	
		dataString = dataString.substr(0, dataString.length -1);
		
		$.ajax({
			type: "POST",
			url: $('#sign_up_form').attr('action'),
			data: dataString
		});

		$('#sign_up').css('left', -3000);
		//$('#sign_up').html('');
		//$('#sign_up').removeClass('sign_up');
	}
	return false;
}

function showPathOfLife()
{
	var dataString = '';
	if ($('.q1:checked').val())
		dataString += 'q1='+$('.q1:checked').val()+'&';
	if ($('.q2:checked').val())
		dataString += 'q2='+$('.q2:checked').val()+'&';
	if ($('.q3:checked').val())
		dataString += 'q3='+$('.q3:checked').val()+'&';
	if ($('.q4:checked').val())
		dataString += 'q4='+$('.q4:checked').val()+'&';
	if ($('.q5:checked').val())
		dataString += 'q5='+$('.q5:checked').val()+'&';
	if ($('.q6:checked').val())
		dataString += 'q6='+$('.q6:checked').val()+'&';
	if ($('.q7:checked').val())
		dataString += 'q7='+$('.q7:checked').val()+'&';
	if ($('.q8:checked').val())
		dataString += 'q8='+$('.q8:checked').val()+'&';
	if ($('.q9:checked').val())
		dataString += 'q9='+$('.q9:checked').val()+'&';
	if ($('.q10:checked').val())
		dataString += 'q10='+$('.q10:checked').val()+'&';
	if ($('.q11:checked').val())
		dataString += 'q11='+$('.q11:checked').val()+'&';
	if ($('.q12:checked').val())
		dataString += 'q12='+$('.q12:checked').val()+'&';
	if ($('.q13:checked').val())
		dataString += 'q13='+$('.q13:checked').val()+'&';
	
	
	dataString = dataString.substr(0, dataString.length -1);
	
	//$('#path_of_life_graphic').html('<a href="#" onclick="$(\'#path_of_life_graphic\').html(\'\'); $(\'#path_of_life_graphic\').css(\'left\', -3000);" style="float:right; color:black; text-decoration:none;">X</a><img src="'+themeURL+'/php/calculate_pol.php?'+dataString+'" width="918" height="548"><p>Now you can see each area of your life. This will give you a good indication of which areas of your life need to be improved and your goal would be to get every section of your life as close to a nine or ten.  The Mindpower Course for Business or for Life will help you to achieve this.</p>');
	$('#path_of_life_img').attr('src', themeURL+'/php/calculate_pol.php?'+dataString);
	$('#path_of_life_graphic').css('left', ($(window).width() / 2) - ($('#path_of_life_graphic').width() / 2));
	$('#path_of_life_graphic').css('top', 150);
	
	return false;
}