function ShowLoginDiv() {
    if (!document.getElementById) return true;
    var notlogged = document.getElementById("NotLoggedInDiv");
    var logbox = document.getElementById("LoginBox");
    if (!notlogged || !logbox) return true;
    notlogged.style.display = 'none';
    logbox.style.display = 'block';
    return false;
}

function FirstContact() {
   // put your hand in the cookie jar
   var cookie_ls = document.cookie;
   // if you don't come up with a cookie, this is their first visit
   if (cookie_ls.length == 0) {
      // bake a cookie, put it in the cookie jar for next time and mark it with a freshness date
      document.cookie = window.location.href + "; path=/; expires=Mon, 31-Dec-2030 00:00:01 GMT;";
   }
} 


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2]; }
}

function goforit() {
  //Function does nothing
  }
  
//Store a cookie with the supplied name/value (path and expires are optional).
// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
   document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
   if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
    WM_acceptsCookies = true;
   }// If it succeeds, set variable
} else { // there was already a cookie
 WM_acceptsCookies = true;
}


function WM_setCookie (name, value, hours, path, domain, secure) {
   if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
    var not_NN2 = (navigator && navigator.appName
                && (navigator.appName == 'Netscape')
                && navigator.appVersion
                && (parseInt(navigator.appVersion) == 2))?false:true;

    if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
        if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
         var numHours = hours;
        } else if (typeof(hours) == 'number') { // calculate Date from number of hours
         var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
        }
    }
    document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
   }
} // WM_setCookie


function WM_readCookie(name) {
   if(document.cookie == '') { // there's no cookie, so go no further
    return false;
   } else { // there is a cookie
    var firstChar, lastChar;
    var theBigCookie = document.cookie;
    firstChar = theBigCookie.indexOf(name);     // find the start of 'name'
    var NN2Hack = firstChar + name.length;
    if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
        firstChar += name.length + 1; // skip 'name' and '='
        lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
        if(lastChar == -1) lastChar = theBigCookie.length;
        return unescape(theBigCookie.substring(firstChar, lastChar));
    } else { // If there was no cookie of that name, return false.
        return false;
    }
   }    
} // WM_readCookie

function WM_killCookie(name, path, domain) {
 var theValue = WM_readCookie(name); // We need the value to kill the cookie
 if(theValue) {
     document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
 }
} // WM_killCookie

// 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 - 1;

	// 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;
}
