//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;
}



//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);
}


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 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 PdcJumpToQuestion(Message) {
	//Jump to the question and notify the user.
	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 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;
}