<!-- Begin

//**********************************************************************************************************************************************
//	JsFunctions.js
//
//		This file contains all the static javascript functions used.  Make each function general so it can be called by any script!  
//		Try to keep this file alphabetized by function name.  Add to table of contents as needed.
//
//		Call this script with: 
//			<SCRIPT LANGUAGE="JAVASCRIPT" SRC="../CommonIncludes/JsFunctions.js"></SCRIPT>
//
//		Table Of Contents: 
//		------------------------------------------------------
//		browserRedirect(redirectPage)
//		changeOneChangeAnother(field1, field2, value1, indexToSetTo)
//		changeOneChangeAnotherInverse(field1, field2, value1, indexToSetTo)
//		checkCityAndState(city, state)
//		checkAll(checked, checkboxIDName)
//		checkEmail(emailField)
//		checkField(fieldToCheck, nameToDisplay)
//		checkRequired(which)
//		chooseCategory(categoryList, newCategoryBox, type)
//		deleteAnything(phrase)
//		deleteSomething(whichWords)
//		getDimensions(picture)
//		jumpToUrl(urlValue)
//		moveListElements(list, index, to)
//		preloadImage(imageString)
//		printWindow()
//		scaleEnforce(whichAG, whichQT, NumQT)
//		selectAllList(list)
//		showHide(elementID)
//		textCounter(field, maxCharacters)
//		validateDate(field)
//		validateNumber(field)
//		validatePosInteger(field, zeroOkay)
//		windowChecked(mypage, w, h, scroll, checked)
//		windowCorner(mypage, w, h, scroll)
//		windowCornerFull(mypage, w, h, scroll)
//		windowMiddle(mypage, w, h, scroll)
//
//**********************************************************************************************************************************************


//********************************************************************************************************
// browserRedirect
//		This function redirects to another page if the browser is old.  redirectPage should be a 
//		well-formed URL.  Old is defined as Netscape pre v.6 and IE pre v.5
//********************************************************************************************************
function browserRedirect(redirectPage) {
	var bName = navigator.appName;
	var bVersion = parseInt(navigator.appVersion)
	var bAgent = navigator.userAgent.toLowerCase(); 
	
	//If (the version of netscape is not 6 and not 7 and not Mozilla/Gecko) OR (the version of IE is not 5 and not 6) THEN redirect
	if (((bName == "Netscape") && (!(bAgent.indexOf("6.")!=-1) && !(bAgent.indexOf("7.")!=-1) && !(bAgent.indexOf("gecko")!=-1))) || ((bName == "Microsoft Internet Explorer") && (!(bAgent.indexOf("msie 5.")!=-1) && !(bAgent.indexOf("msie 6.")!=-1)))) {
		window.location = redirectPage;
		//alert(bName + "<br>" + bVersion + "<br>" + bAgent);
	}
	//else keep loading the current page 
}

//********************************************************************************************************
// changeOneChangeAnother
//		This function allows you to check that if pulldown menu field 1 is set to value 1.  If so, then
//		pulldown menu field 2 will be set to indexToSetTo.
//********************************************************************************************************
function changeOneChangeAnother(field1, field2, value1, indexToSetTo){
	if (field1.options[field1.selectedIndex].value==value1) {
		field2.options[indexToSetTo].selected = true;
	}	
}

//********************************************************************************************************
// changeOneChangeAnotherInverse
//		This function allows you to check that if pulldown menu field 1 is set to ANYTHING BUT value 1.
//		If so, then pulldown menu field 2 will be set to indexToSetTo.
//********************************************************************************************************
function changeOneChangeAnotherInverse(field1, field2, value1, indexToSetTo){
	if (field1.options[field1.selectedIndex].value!=value1) {
		field2.options[indexToSetTo].selected = true;
	}	
}

//********************************************************************************************************
// checkAll
//		This function checks all the checkboxes in your form when 'true' is passed in, or unchecks them
//		all when false is passed in.  All checkboxes must have their ID property set to checkboxIDName.
//		The form must have the name property as 'list'.
//********************************************************************************************************
function checkAll(checked, checkboxIDName) {
	len = document.list.elements.length;
	var i=0;
	for(i=0; i<len; i++) {
		if (document.list.elements[i].id==checkboxIDName) {
			document.list.elements[i].checked=checked;
		}
	}
}

//********************************************************************************************************
// checkCityAndState
//		This function makes sure that if the user entered a city name, they also chose a state name.
//		Type should be either "State" or "Province"
//********************************************************************************************************
function checkCityAndState(city, state, type) {
	if (city.value!="") {
		if (state.value!="")
			return true;
		else {
			alert("You must enter a " + type + " if you choose a city!");
			return false;			
		}
	}
	else
		return true;
}

//********************************************************************************************************
// checkEmail
//		This function makes sure the user entered a valid email address.
//********************************************************************************************************
function checkEmail(emailField) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailField.value)){
		return (true);
	}
	if (emailField.value!="") {
		alert("Invalid E-mail Address! Please re-enter.");
		emailField.value = "";
		emailField.focus();
	}
	else {
		alert("Please enter an email address.");
		emailField.focus();
	}
	return (false)
}

//********************************************************************************************************
// checkField
//		This function makes sure the user entered info into a field (without the field starting with 
//		the word 'Required'.
//********************************************************************************************************
function checkField(fieldToCheck, nameToDisplay) {
	if (fieldToCheck.value!="") {
		return (true);
	}
	else {
		alert("Please fill out the " + nameToDisplay + " field before continuing.");
		fieldToCheck.focus();
	}
	return (false)
}

//********************************************************************************************************
// checkRequired
//		This function checks that all fields in a form whose first 8 letters are the word 'required'
//		have a value.  If not, it alerts an error and returns false.  
//		To make the output alert message look good, add this to the tag with the required field:
//			ID="Field Name To Be Displayed"
//********************************************************************************************************
function checkRequired(formName) {
	var pass=true;
	if (document.images) {
		for (i=0;i<formName.length;i++) {
			var tempobj=formName.elements[i];
			if (tempobj.name.substring(0,8)=="required") {
				if (((tempobj.type=="text"||tempobj.type=="textarea"||tempobj.type=="password")&&
					tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
					tempobj.selectedIndex==0)) {
					pass=false;
					break;
         		}
      		}
   		}
	}
	if (!pass) {
		if (tempobj.id=='')
			alert("Please make sure the "+tempobj.name.substring(8,30)+" field was properly completed.");
		else
			alert("Please make sure the "+tempobj.id+" field was properly completed.");
		return false;
	}
	else
		return true;
}

//********************************************************************************************************
// chooseCategory
//		This function makes sure if they choose 'New Category' from the pull down, then
//		they also enter a new category name.  The type determines which msg will be displayed.
//********************************************************************************************************
function chooseCategory(categoryList, newCategoryBox, type) {
	if ((categoryList.value=="0") && ((newCategoryBox.value=='') || (newCategoryBox.value==' '))){
		if (type=1)
			alert("If you choose New Category, you must enter a New Category Name.");
		else if (type=2)
			alert("If you choose a New Category for the Project Attribute, you must enter a New Category Name.");
		return false;
	}
}

//********************************************************************************************************
// deleteAnything
//		This function checks that a user is sure they want to delete before we let them, and lets you
//		pass in the question to be asked first.
//********************************************************************************************************
function deleteAnything(phrase) {
	if (confirm(phrase))
		return true;
	else
		return false;
}

//********************************************************************************************************
// deleteSomething
//		This function checks that a user is sure they want to delete before we let them.
//********************************************************************************************************
function deleteSomething(whichWords) {
	if (whichWords==1)
		var agree=confirm("Are you sure you want to delete all Respondents listed?");
	else if (whichWords==2)
		var agree=confirm("Are you sure you want to delete all Subjects listed?");
	else if (whichWords==3)
		var agree=confirm("Are you sure you want to cancel all the selected surveys?");
	else if (whichWords==4)
		var agree=confirm("Are you sure you want to cancel this survey?");
	else if (whichWords==5)
		var agree=confirm("Are you sure you want to delete this resume?");
	else if (whichWords==6)
		var agree=confirm("Are you sure you want to delete this item?");
	else if (whichWords==7)
		var agree=confirm("Are you sure you want to delete this contact?");
					
	if (agree)
		return true;
	else
		return false;
}

//********************************************************************************************************
// getDimensions
//		This function takes in an images's url and returns its height and width.  The H & W are displayed
//		in a form called Dimform in textboxes called width and height respectively.
//********************************************************************************************************
function getDimensions(picture) {
	var uploadedImage = new Image(); 
	uploadedImage.src = picture;
	width = uploadedImage.width;
	height = uploadedImage.height;
	document.Dimform.width.value = width;
	document.Dimform.height.value = height;
}

//********************************************************************************************************
// jumpToUrl
//		This function changes the current window to the URL passed in.
//		Call with: onChange="jumpToUrl(this.options[this.selectedIndex].value)"  
//		in the SELECT tag.
//********************************************************************************************************
function jumpToUrl(urlValue) {
	window.location.href = urlValue;
}

//********************************************************************************************************
// moveListElements
//		This function is used by the ranking question type in Surveys.  Its purpose is to reorder the
//		elements in a drop-down list.  
//
//		list - the drop-down element of the form
//		index - the item in the drop-down list to be reordered
//		to - the number of positions away relative to the current index position to move the item to (+ or -)
//********************************************************************************************************
function moveListElements(list, index, to) {
	var total = list.options.length-1;
	if (index == -1) return false;
	if (to == +1 && index == total) return false;
	if (to == -1 && index == 0) return false;
	var items = new Array;
	var values = new Array;
	for (i = total; i >= 0; i--) {
		items[i] = list.options[i].text;
		values[i] = list.options[i].value;
	}
	for (i = total; i >= 0; i--) {
		if (index == i) {
			list.options[i + to] = new Option(items[i],values[i], 0, 1);
			list.options[i] = new Option(items[i + to], values[i + to]);
			i--;
		}
		else {
			list.options[i] = new Option(items[i], values[i]);
		}
	}	
	list.focus();
	list.options[index + to].selected = true;
}

//********************************************************************************************************
// preloadImage
//		This function preloads to a page any image passed in.
//********************************************************************************************************
function preloadImage(imageString) {
	var preload = new Image();
  	preload.src = imageString;
}

//********************************************************************************************************
// printWindow
//		This function brings up the print dialog box in order to print the current window.
//********************************************************************************************************
function printWindow() {
	browserVersion = parseInt(navigator.appVersion);
	if (browserVersion>=4) window.print();
}

//********************************************************************************************************
// scaleEnforce
//		This function makes sure that if the answer group for a question is 'scale' then the question
//		type must be 'RBH'.  Pass in the answer group field to check, the question type field to check,
//		and the number of question types in the QT pulldown menu.
//********************************************************************************************************
function scaleEnforce(whichAG, whichQT, NumQT) {
	if ((whichAG.value == 'Scale') && (whichQT.value != 'RBH')) { 
		alert("The Scale answer group can be used with the Multiple Choice question type only.");
		for (k=1; k<=(NumQT-1); k++) {
			if (whichQT.options[k].value == 'RBH') {
				whichQT.options[k].selected = true;
			}
		}
	}
}

//********************************************************************************************************
// selectAllList
//		This function selects all elements in a list
//********************************************************************************************************
function selectAllList(list) {
	for (i=0; i<=list.options.length-1; i++) {
		list.options[i].selected = true;
	}
}	

//********************************************************************************************************
// showHide
//		This function shows or hides a tag with ID=elementID.  Use an 'a' tag to show or hide it with:
//		onclick="showHide('elementID');return false;"
//		To start the tag's content off as hidden add this to the tag:
//		style="display: none;"
//********************************************************************************************************
function showHide(elementID){  
var objEl = document.getElementById(elementID)
   if (objEl.style.display == "none"){
        objEl.style.display = "block";
    }else{
        objEl.style.display = "none";
   }
}

//********************************************************************************************************
// textCounter
//		This function stops a person from putting more than maxCharacters in a text field
//********************************************************************************************************
function textCounter(field, maxCharacters) {
	if (field.value.length > maxCharacters) { 	// if too long...trim it
		field.value = field.value.substring(0, maxCharacters);
		alert("You may only have " + maxCharacters + " characters in this field.");
	}	
}

//********************************************************************************************************
// validateDate
//		This function checks that a valid date was entered.
//********************************************************************************************************
function validateDate(field) {
	var datefield = field;
	var today = new Date();
	if (chkdate(field) == false) {
		field.select();
		alert("That date is invalid.  Please try again.");
		field.value = (today.getMonth()+1) + "/" + today.getDay() + "/" + today.getFullYear();
		field.focus();
		return false;
	}
	else {
		return true;
   	}
}
function chkdate(field) {
	var strDatestyle = "US"; //United States date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = field;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
   		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   	}
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}

	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
   			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
   		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function doDateCheck(from, to) {
	if (Date.parse(from.value) <= Date.parse(to.value)) {
		alert("The dates are valid.");
	}
	else {
		if (from.value == "" || to.value == "") 
			alert("Both dates must be entered.");
		else 
			alert("To date must occur after the from date.");
   	}
}

//********************************************************************************************************
// validateNumber
//		This function checks that a number was entered (negative and decimal okay).
//********************************************************************************************************
function validateNumber(field) {
	var valid = "0123456789.-"
	var ok = "yes";
	var numPeriods = 0;
	var numDashes = 0;
	var temp;
	if (field.value.length==0) ok = "no";
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
		if (numPeriods<1 && temp==".") numPeriods += 1;
		else if (temp==".") ok = "no";
		if (numDashes<1 && temp=="-" && i==0) numDashes += 1;
		else if (temp=="-") ok = "no";
	}
	if (ok == "no") {
		alert("Only numbers are accepted as answers to this question.");
		field.value = "";
		field.focus();
		field.select();
	}
}

//********************************************************************************************************
// validatePosInteger
//		This function checks that an integer was entered greater than zero.  If ZeroOkay="yes" then
//		entering zero will not cause a warning.
//********************************************************************************************************
function validatePosInteger(field, zeroOkay) {
	var valid = "0123456789"
	var ok = "yes";
	var numPeriods = 0;
	var numDashes = 0;
	var temp;
	if (field.value.length==0) ok = "no";
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
		if (numPeriods<1 && temp==".") numPeriods += 1;
		else if (temp==".") ok = "no";
		if (numDashes<1 && temp=="-" && i==0) numDashes += 1;
		else if (temp=="-") ok = "no";
		if (ok=="yes" && zeroOkay=="no") {
			if (field.value < 1) ok = "no";
		}
	}
	if (ok == "no") {
		alert("Please enter a positive integer number.");
		field.value = "";
		field.focus();
		field.select();
		return false;
	}
}

//********************************************************************************************************
// windowChecked
//		This function brings up a new window only when a checkbox is checked.
//		Attributes: variable width, variable height, variable scroll (yes/no), resizable, top lefthand corner of screen
//					toolbar, menubar, locationbar
//********************************************************************************************************
function windowChecked(mypage, w, h, scroll, checked) {
	if (checked.checked) {
		var winl = 0;
		var wint = 0;
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable,menubar,status,toolbar,location,directories'
		win = window.open(mypage, 'popupnav', winprops);
	  	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}
}

//********************************************************************************************************
// windowCorner
//		This function brings up a new window in the top lefthand corner of the screen without the browser
//		topbars showing.
//
//		Attributes: variable width, variable height, variable scroll (yes/no), resizable, top lefthand corner of screen
//					no toolbar, no menubar, no locationbar
//********************************************************************************************************
function windowCorner(mypage, w, h, scroll) {
	var winl = 0;
	var wint = 0;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, 'popupnav', winprops);
  	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//********************************************************************************************************
// windowCornerFull
//		This function brings up a new window in the top lefthand corner of the screen with the browser
//		topbars showing.
//
//		Attributes: variable width, variable height, variable scroll (yes/no), resizable, top lefthand corner of screen
//					toolbar, menubar, locationbar
//********************************************************************************************************
function windowCornerFull(mypage, w, h, scroll) {
	var winl = 0;
	var wint = 0;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=yes,menubar=yes,location=yes,resizable'
	win = window.open(mypage, 'popupnav', winprops);
  	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//********************************************************************************************************
// windowMiddle
//		This function brings up a new window centered in the middle of the screen without the browser
//		topbars showing.
//
//		Attributes: variable width, variable height, variable scroll (yes/no), resizable, middle middle of screen
//					no toolbar, no menubar, no locationbar
//********************************************************************************************************
function windowMiddle(mypage, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, 'popupnav', winprops);
  	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
//  End -->