////////////////////////////////////////////////////////////////////
// This makes the AJAX call to get two questions from the book
////////////////////////////////////////////////////////////////////
function hw1( book_id, book_ed )
{					
	//alert("book_id=[" + book_id + "] book_ed=[" + book_ed + "]");
	
	// Call a script to get two questions from the book
	var request = new Ajax.Request(
	  "scripts/script_27.php?xaction=getq&bookid=" + book_id,
		{
			method: 'get',
			onComplete: function(request) {
				// request.responseText should be of the form 
				// "Question1|Hint1|Answer1|Question2|Hint2|Answer2"
				// We convert that to an array here.  
				// Arrays are indexed beginning with 0
				var arr=request.responseText.split("|");
								
				// Do some error checking to ensure we got back the 
				// right number of elements
				if (arr.length == 6)
				{
					// Clear the answers
					ClearAnswers();
					
					// Update the HTML
					var myBQ1 = document.getElementById("bq_question1");
					myBQ1.innerHTML = arr[0].substring(1);
				
					var myBQ2 = document.getElementById("bq_question2");
					myBQ2.innerHTML = arr[3];
				
					var myBVS = document.getElementById("book_validation_status");
					myBVS.innerHTML = 
					  "Please answer the following questions and click the " + 
					  "Validate My Book button:";
				
					// Update the hidden fields to link the questions with the 
					// answer indices
					var myBQA1 = document.getElementById("bq_ans_id1");
					myBQA1.value = arr[2];
				
					var myBQA2 = document.getElementById("bq_ans_id2");
					myBQA2.value = arr[5];
					
					// Set the hidden field that contains the questions displayed, 
					// to this book
					var myBQD = document.getElementById("bookid_validated");
					myBQD.value = book_ed;
					
					var myVS = document.getElementById("vstatus");
					myVS.value="";					
				}
				else
				{
					var myBVS = document.getElementById("book_validation_status");
					myBVS.innerHTML = 
					  " <span class=\"ErrorString\"> Unable to get questions for this" +
					  " book.  Please contact Webmaster@TouringPlans.com " + 
					  "with this message:[" + 
					  request.responseText + "]</span>";
				}

				$('book_verify').show();
				$('subscribe_button').hide();
				$('subscribe_hold').show();
				
			} // function(request)
		} // Ajax.Request
	);
		
	return true;
} // function hw1
	
	
///////////////////////////////////////////////////////////////////	
// Validates the book answers using AJAX
///////////////////////////////////////////////////////////////////
function ValidateBook()
{
	// The two answers as given by the user
	var ans1 = document.getElementById("bq_answer1");
	var ans2 = document.getElementById("bq_answer2");
	
	if (ans1.value == "" || ans2.value == "")
	{
		var myBVS = document.getElementById("book_validation_status");
		myBVS.innerHTML = 
		  "<span class=\"ErrorString\"><strong>Both questions must be answered before " +
		  "clicking the Validate My Book button.</strong></span>";
		return true;
	}
	
	// Get the question IDs in the hidden field forms.
	var myBQA1 = document.getElementById("bq_ans_id1");
	var myBQA2 = document.getElementById("bq_ans_id2");
	
	// Get the book ID being answered
	var BookQuestionsDisplayed = document.getElementById("bookid_validated");
	
	// Call a script to get two questions from the book
	var request = new Ajax.Request(
	  "scripts/script_27.php?xaction=xvalid&ans1=" + ans1.value + "&ans2=" + 
	  ans2.value + "&bqa1=" + myBQA1.value + "&bqa2=" + myBQA2.value + 
	  "&bookid=" + BookQuestionsDisplayed.value,
		{
			method: 'get',
			onComplete: function(request) {				
				
				// request.responseText should be of the form
				// "bValidationSuccess | Status Message | DiscountFactor | 
				// random string1 | random string 2"
				//													0					1					2				3				4
				// We convert that to an array here.
				// Arrays are indexed beginning with 0
				var arr=request.responseText.split("|");
				
				var bValidationSuccess = arr[0].substring(1);
				
				var StatusMessage;
				var DiscountFactor = arr[2];
				
				var random_string1 = arr[3];
				var random_string2 = arr[4];
				
				if (bValidationSuccess == "0")
					StatusMessage = "<span class=\"ErrorString\"> " +arr[1]+ "</span>";
				else
				{
					StatusMessage = "<span class=\"green\"> " + arr[1] + "</span>";
					$('book_questions').hide();
					$('newp_no_book').hide();
					$('subscribe_button').show();
					$('subscribe_hold').hide();
				}
				
				// update the HTML forms
				var myBVS = document.getElementById("book_validation_status");
				myBVS.innerHTML = StatusMessage;
				
				var myVS = document.getElementById("vstatus");
				myVS.value = "__validated";
				
				var myDF = document.getElementById("discount_factor");
				myDF.value = DiscountFactor;
				
				var myRS1 = document.getElementById("rstring1");
				myRS1.value = random_string1;
				
				var myRS2 = document.getElementById("rstring2");
				myRS2.value = random_string2;
			}
		} // request
	);
	
	return true;
}


////////////////////////////////////////////////////////////////////////////
// Clears the two answers given
// Also does any behind-the-scenes work needed to ensure the current status
// of the page is correct
////////////////////////////////////////////////////////////////////////////
function ClearAnswers()
{
	// The two answers as given by the user
	var ans1 = document.getElementById("bq_answer1");
	var ans2 = document.getElementById("bq_answer2");
	var BookQuestionsDisplayed = document.getElementById("bookid_validated");
	
	ans1.value="";
	ans2.value="";
	BookQuestionsDisplayed.value="";
	
	var myVS = document.getElementById("vstatus");
	myVS.value="";
	
	return true;
}


// when people click the "Own a 2010 Unofficial Guide?" link
function click_book_validate()
{
  if ($('validate_book_wrap'))
    $('validate_book_wrap').show();
  if ($('validate_book_link'))
    $('validate_book_link').hide();
  if ($('enter_discount_field'))
    $('enter_discount_field').hide();
  if ($('RadioGroup1'))
    $('RadioGroup1').value='bookowner';
}

// someone clicked on "i have a book" but doesn't really
function newp_no_book_link()
{
  ClearAnswers();
  if ($('subscribe_hold'))
    $('subscribe_hold').hide();
  if ($('subscribe_button'))
    $('subscribe_button').show();
  if ($('validate_book_wrap'))
    $('validate_book_wrap').hide();
  if ($('RadioGroup1'))
    $('RadioGroup1').value = 'premium';
  if ($('validate_book_link'))
    $('validate_book_link').show();
  if ($('enter_discount_field'))
    $('enter_discount_field').show();
}

// users click the "I want a basic account" link
function i_want_basic()
{
  if ($('basic_access'))
    $('basic_access').hide();
  if ($('i_want_basic_wrap'))
    $('i_want_basic_wrap').show();
  if ($('security_code'))
    $('security_code').show();
  if ($('validate_book_field'))
    $('validate_book_field').hide();
  if ($('enter_discount_field'))
    $('enter_discount_field').hide();
  if ($('RadioGroup1'))
    $('RadioGroup1').value = 'basic';  
}

// users click the "ok i really want the premium" link
function i_want_premium()
{
  if ($('basic_access'))
    $('basic_access').show();
  if ($('i_want_basic_wrap'))
    $('i_want_basic_wrap').hide();
  if ($('security_code'))
    $('security_code').hide();
  if ($('validate_book_field'))
    $('validate_book_field').show();
  if ($('enter_discount_field'))
    $('enter_discount_field').show();
  if ($('RadioGroup1'))
    $('RadioGroup1').value = 'premium';
}

// user clicks "I have a discount" link
// show the discount input field, hide the link
function i_have_discount() {
  if ($('enter_discount'))
    $('enter_discount').show();
  if ($('discount_link'))
    $('discount_link').hide();
  if ($('validate_book_field'))
    $('validate_book_field').hide();
}

// clicks "no discount" link
function newp_no_discount() {
  if ($('enter_discount'))
    $('enter_discount').hide();
  if ($('discount_link'))
    $('discount_link').show();
  if ($('validate_book_field'))
    $('validate_book_field').show();
}