// Perseus JavaScript Functions
// For assistance, please e-mail techsupport@perseus.com
// (c) 2003 Perseus Development Corp.

//Version 3.01 10/13/00
//Version 5.00 03/06/02 BP
//Version 6.00 07/29/03

function RandomizeChoices(QuestionName, SelectedValue) {
	//Randomizes the contents of an option list.
	//Don't move the first item which is the placeholder.
	//(e.g., "", or "(Click to choose)")

	var y;
	var XText, XValue;
	var YText, YValue;

	var ChoiceList = document.PdcSurvey.elements[GetFormIndex(QuestionName)];
	var OptionCount = ChoiceList.options.length - 1;
	var SelectedIndex = SelectedValue;

	// Loop thru the options.
	for (var x=1;x<OptionCount;x++){
	   	// Swap the current option with a randomly select different option
		y = Math.floor(Math.random() * OptionCount) + 1;
		XText = ChoiceList.options[x].text;
		XValue = ChoiceList.options[x].value;
		YText = ChoiceList.options[y].text;
		YValue = ChoiceList.options[y].value;
		ChoiceList.options[x] = new Option(YText, YValue);
		ChoiceList.options[y] = new Option(XText, XValue);

        // Preserve the selection.
        if (SelectedValue == XValue) {
			SelectedIndex = y;
		} else if (SelectedValue == YValue) {
			SelectedIndex = x;
		}
	}
	// Reset the selection.
	ChoiceList.selectedIndex = SelectedIndex;
	return;
}

// Autojump based on a list selection.
function SelectJump(sel, JumpString) {
   var SelArr = JumpString.split(";");
   var ValArr = new Array();

   for (var i=0;i<SelArr.length;i++){
     if (SelArr[i].length > 0) {
       ValArr = SelArr[i].split("|");
         if((ValArr.length == 2) && (parseInt(sel.options[sel.selectedIndex].value) == parseInt(ValArr[0]))){
            window.location = ValArr[1]
            break;
         }
      }
   }
}

//Return true if the supplied value is an integer.
function IsInteger(Value) {
	//Test if parseInt returns a number. See the doc about NaN for why the code is written this way.
	if ((Value == "") || (isNaN(parseInt(Value))))
		{return false;}
	else
		{return true;}
}

//Return true if the supplied value is a number.
function IsNumber(Value) {
	//Test if parseFloat returns a number. See the doc about NaN for why the code is written this way.
	if ((Value == "") || (isNaN(parseFloat(Value))))
		{return false;}
	else
		{return true;}
}

//Return the index of the specified form element.
function GetFormIndex(ElementName) {
	var i;
	for(i=0;i<document.PdcSurvey.elements.length;i++) {
		if (document.PdcSurvey.elements[i].name == ElementName) {
			return i;
		}
	}
	return -1;
}

//Return the index of the selected option from the array.
function PdcSelectedOption(optionArray) {
	for(var x=0;x<optionArray.length;x++) {
		//The option could a radio button which is check or a selected list item. If either of those
		//cases is true return the index.
		if (((optionArray[x].type == "radio") && optionArray[x].checked) || optionArray[x].selected)
			return x;
	}
	return -1;
}

//Return true if all topics in the table have unique choice selections.
function PdcValidateRankings() {
	var index;
	var choiceValues = new Array();
	
	for (var x=0;x<arguments.length;x++){
		//Get the selected index
		index = PdcSelectedOption(document.PdcSurvey[arguments[x]]);

		//If there is a selected index test if it has already been set.
		if (index != -1) {
			if (choiceValues[index] == 1) {
				return false;
			} else {
				choiceValues[index] = 1;
			}
		}
	}
	return true;
}

//Store a cookie with the supplied name/value (path and expires are optional).
function SetCookie(Name, Value, Path, Expires) {
	var CookieString;

	CookieString = Name + "=" + escape(Value)

	if (Path) CookieString += "; path=" + Path;
	if (Expires) CookieString += "; expires=" + Expires.toGMTString();

	document.cookie = CookieString
}

//Answer the stored value for the named cookie.
function GetCookieValue(Name) {
	var Cookies;
	var NameValuePair;
	var CookieString = document.cookie;

	//Get an array of cookie pairs by splitting on ;
	Cookies = CookieString.split("; ");

	for (var i=0;i<Cookies.length;i++){
		NameValuePair = Cookies[i].split("=");
		if (NameValuePair[0] == Name) {
			return unescape(NameValuePair[1]);
		}
	}
}

//Save the current page.
function SaveCurrentPage(PageValue) {
	var d = new Date();
	var cookieValue;

	if (PageValue == undefined)  {
		cookieValue = document.PdcSurvey.PdcCurrentPage.value; }
	else {
		cookieValue = PageValue; }

	//Set the cookie to expire in one month.
	d.setMonth(d.getMonth() + 1);
	SetCookie(CurrentPageCookieName(), cookieValue, "/", d);
}

//Check if there was a previous current page.
function CheckCurrentPage() {
	var CurrentPage = GetCookieValue(CurrentPageCookieName());

	//Verify that current page is non-null and not an empty string.
	//Verify that there is a nextpage field on this page.
	//Defect 925. Was comparing to null which is not cross-browser
	//If the user submitted the survey dont change next page (the user will then go to the normal next page).
	if ((CurrentPage) && (CurrentPage != "") && (document.PdcSurvey.PdcNextPage) && (CurrentPage != 'submitted'))
		document.PdcSurvey.PdcNextPage.value = CurrentPage;
}

//Answer the name of the cookie to save the current page with.
function CurrentPageCookieName() {
	var ProjectName;

	ProjectName = document.PdcSurvey.PdcProjectID.value;
	if (ProjectName == "")
		return "PdcCurrentPage";
	else
		return "Pdc" + ProjectName;
}

function PdcGetElement(elementName) {
	// Check if the browser supports GetElementById, otherwise loop thru all of the form elements.
	if (document.getElementById) {
		return document.getElementById(elementName);
	} else {
		for(var i=0;i<document.PdcSurvey.elements.length;i++) {
			if (document.PdcSurvey.elements[i].id == elementName)
				return document.PdcSurvey.elements[i];
		}
	}
	return null;
}

function PdcShuffleArray(A) {
	for (var X=0;(X<A.length);X++){
   		// swap the current option with a randomly selected different option
   		Y = Math.floor(Math.random() * A.length);
   		XText = A[X];
   	     	YText = A[Y];
   	     	A[X] = YText;
   	     	A[Y] = XText;
   	}
   	return A.join(" ");
}

function PdcRemoveOption(QuestionName, ChoiceValue) {
	//Remove the specified Choice from a selection list

	var ChoiceList = document.PdcSurvey.elements[GetFormIndex(QuestionName)];
	var OptionCount = ChoiceList.options.length;

	// Loop thru the options.
	for (var x=1;x<OptionCount;x++){
	   	if (ChoiceList.options[x].value == ChoiceValue){
	   		ChoiceList.options[x] = null;
	   		break;
	   	}
	}
}

function PdcSelectedCount() {
	//Answer the number of selected buttons
	var Selected=0;

	for (var x=0;x<arguments.length;x++){
		if (PdcGetElement(arguments[x]).checked) {Selected += 1;}
	}           
	return Selected;
}


function PdcClearCheckboxes() {
	for (var x=0;x<arguments.length;x++){
		PdcGetElement(arguments[x]).checked = false;
	}
}

function PdcZeroNotAnswered() {
	for (var x=0;x<arguments.length;x++){
		if (PdcGetElement(arguments[x]).value == '') {
			PdcGetElement(arguments[x]).value = '0';
		}
	}
}


function PdcJumpToQuestion(QuestionId, Message) {
	//Jump to the question and notify the user.
	window.location = QuestionId;
	alert(Message);
}

function PdcAnyButtonSelected() {
	//Takes an array of buttons and returns true if any of them are selected.
	for (var x=0;x<arguments.length;x++){
		if (PdcGetElement(arguments[x]).checked) {return true;}
	}
	return false;
}

function PdcAnyAnswered() {
	//Takes an array of text fields and answers true if any have values.
	for (var x=0;x<arguments.length;x++){
		if (PdcGetElement(arguments[x]).value != '') {return true;}
	}
	return false;
}

function PdcAnyNotAnswered() {
	//Takes an array of text fields and answers true if any have blank values.
	for (var x=0;x<arguments.length;x++){
		if (PdcGetElement(arguments[x]).value == '') {return true;}
	}
	return false;
}

function PdcGetUrlParameter(parameterName) {
	//Return the value for the specfied named parameter (passed in the URL).

	//Get the query string minus the ?
	var queryString = location.search.substring(1);

	//Split the args by & and the look at each named pair.
	var args = queryString.split("&");
	for (var x=0;x<args.length;x++) {
		var pair = args[x].split("=");
		if ((pair.length == 2) && (pair[0] == parameterName)) {
			return unescape(pair[1]);
		}
	}
}

function PdcFieldTotal() {
	//Takes an array of text fields and answers the total.
	var Total = 0;
    var TextValue;
   
	//Loop thru the arguments, for each text field get the value and if numeric add to the total.
	for (var x=0;x<arguments.length;x++){
		TextValue = PdcGetElement(arguments[x]).value
		if (IsNumber(TextValue)) Total += parseFloat(TextValue)
	}
	return Total;
}

function PdcSelectedButtons() {
	//Answer a serial string of selected buttons
	var Selected='';
	for (var x=0;x<arguments.length;x++){
		if (PdcGetElement(arguments[x]).checked) {
			Selected += arguments[x] + '|';
		}
	}
	if (Selected != '') {Selected = Selected.substring(0, Selected.length-1)}
	return Selected;
}