/*************
Global variables
**************/

var userAnswers = new Array();
var totalUserInfoQuestions = 16;
var questionChoice = 3;

var prevPage;
var currPage;
var nextPage;

var counter=0;
var expiredays = null;
var cookieName = "cookieAnswers";


/*******************************************************************************************/
// Force user to check the agreement before proceeding otherwise return to page 1.
/*******************************************************************************************/
function checkAgreement() {
	
	var val="";

	for(i=0; i< document.form.agreement.length; i++){
		if(document.form.agreement[i].checked){
			val = document.form.agreement[i].value; 
		}
	}
	
	if(val == "accept") {
		//location.href="page3.php";		
		document.form.submit();
	} else if(val == "decline") {
		//location.href="instructions.php?referral=<?php=$referralID?>";		
		location.href= document.form.url.value;		
	} else {
		alert("Please make a seletion");
	}
}


/*******************************************************************************************/
// move to previous page and pop out the the the user's answers
/*******************************************************************************************/
function movePrevPage(prevPage, records, prevQuestions) {

	var pageNum = prevPage.split("_");
	var prevPage = parseInt(pageNum[1]) - 1;
	var allAnswer = getSelectedAnswers(prevQuestions);
	var url = "processPage.php?referral=" + referralID + "&page=" + prevPage + "&row="+ records + "&prevQuestions=" + prevQuestions + "&data=" +  allAnswer;
	// move to previous page;
	window.location = url;
}

/*******************************************************************************************/
// move to next page and store the user's answers
/*******************************************************************************************/
function moveNextPage(currPage, records, prevQuestions) {
	
	var pageNum = currPage.split("_");
	var nextPage = parseInt(pageNum[1]) + 1;
	var allAnswer = getSelectedAnswers(prevQuestions);
	var url = "processPage.php?referral=" + referralID + "&page=" + nextPage + "&row="+ records + "&prevQuestions=" + prevQuestions + "&data=" +  allAnswer;

//	alert(currPage + " -- " + records + " -- " + prevQuestions)
	if(pageNum[1] != "last") {
		// move to next page;
		window.location = url;
	} else {
		// on last page return all store data to check for valid before submiting to server.
		return string;
	}
}
/*******************************************************************************************/
// glab the user's radio button seletion 
/*******************************************************************************************/
function getSelectedAnswers(prevQuestions) {

	var numQuestions = document.getElementById('questions').rows.length;
	var	pageData = "";
	var pageInfo = "";
	var string = "";

	pg = pageNum.split("_")
    nextPage = parseInt(pg[1]) + 1;

	pageInfo = pg[1] + "_" + (numQuestions) + "_" + prevQuestions;
 
	// start at index one since heading text is zero - check if any radio button have been selected.
	for(var i=1; i<=numQuestions; i++) {

		switch(i) {
			case 1: 
				var whichQuestion = document.form.one;
				break;
			case 2:
				var whichQuestion = document.form.two;
				break;
			case 3:
				var whichQuestion = document.form.three;
				break;
			case 4:
				var whichQuestion = document.form.four;
				break;
			case 5:
				var whichQuestion = document.form.five;
				break;
		}
		
		for(var j=0; j<questionChoice; j++) {			// store the page number and the selected radio button in a string
			if(whichQuestion[j].checked) {
				if(i == (numQuestions-1) )	{
					var answers = pageNum + "_" + i + "_" + whichQuestion[j].value + "_" + whichQuestion[j].id + ":";
				} else {	
					var answers = pageNum + "_" + i + "_" + whichQuestion[j].value + "_" + whichQuestion[j].id + ":";
				}

				if(i == 1) {
					string = answers;
				} else {
					string +=  answers; 
				}

			}	
		}
	}
	
	if(string == "") {
		string = pageInfo + ":";			// nothing is selected
	} else {
		string = pageInfo + ":" + string;	// some questions answered
	}

	return string;
}

/*******************************************************************************************/
// grab last page information and selection and pass to server side script to validate and save all data .
/*******************************************************************************************/
function submitValidationData(currPage) {

	var pageNum = currPage.split("_");
	var page = parseInt(pageNum[1]);

	var allAnswer = getSelectedAnswers("");
	var url = "saveData.php?referral=" + referralID + "&data=" +  allAnswer + "&page=" + page;

//	alert("page: " + pageNum + "-ans: " + allAnswer);
	window.location = url;
}

/*******************************************************************************************/
// this function is called for testing purposes to generate report . Not needed.
/*******************************************************************************************/
function generateReport() {

	var url = "generateReportReview.php";

	window.location = url;
}
/*******************************************************************************************/

/*******************************************************************************************/
// this function is called for testing purposes to generate report . Not needed.
/*******************************************************************************************/
function getDefinitions() {

	//var url = "includes/definitions.php";
	if(referral == "en-gb") {
		window.open("includes/DMSA_Definitions_new_UK.pdf");
	} else {
		window.open("includes/DMSA_Definitions_new.pdf");
	}

//	window.open("includes/DMSA_Definitions_new.pdf");
//	window.location = url;
}
/*******************************************************************************************/
function getQuestions() {
	
	if(language == "en-gb") {
		window.open("includes/DMSA_Questions_gb.pdf");
	} else {
		window.open("includes/DMSA_Questions.pdf");
	}
}
