
function clicked(variable) {
	if (document.webquest.elements[variable].length > 1) {
		var eraseCheck = 0
		for(i=0;i<document.webquest.elements[variable].length;i++) {
			if(navigator.appName == "Netscape") {
				if (document.webquest.elements[variable][i].checked) { eraseCheck = eraseCheck + 1 }
				if (eraseCheck == 2) { 
					alert("You can only select one box for this answer.")
					for(nx=0;nx<document.webquest.elements[variable].length;nx++)	{
						document.webquest.elements[variable][nx].checked=false
				}
				if (eraseCheck ==2) { eraseCheck=0}
				}
			}
			else {
				if (document.webquest.elements[variable][i].checked) document.webquest.elements[variable][i].click()
			}
		}
	}
	var tempValue = removeQuotes(getValue(variable))
	varValues[getIndexOf(variable)] = tempValue
/*	varVal = getValue(variable)
	xxx = getIndexOf(variable)
*/	
	checkConditions()

}

function refreshValues() {
	aNumber = document.webquest.elements.length-1
	for(var i=0;i<=aNumber;++i) {
		varValues[i] = getValue(varNames[i])
	}
}

function refreshNatValues() {
	aNumber = document.webquest.elements.length-1
	for(var i=0;i<=aNumber;++i) {
		varNatValues[i] = getValue(varNatNames[i])
	}
}


function upDate(vName,vValue) {
	var oTemp = document.webquest.elements[vName]
	if(oTemp.type == undefined) {
		for(var i=0;i<oTemp.length;++i) {
			if (oTemp[i].value == vValue) {
				oTemp[i].checked = "true"
			}
		}
	
	}
	if(oTemp.type == 'checkbox') {
		oTemp.checked = "true"
	}
	if(oTemp.type == 'text') {
		oTemp.value = vValue
	}
	if(oTemp.type == 'textarea') {
		oTemp.value = vValue
	}
	if(oTemp.type == 'select-one') {
		for(var i=0;i<oTemp.length;++i) {
			if (oTemp[i].value == vValue) {
				oTemp[i].selected = "true"
			}
		}
	}
	
}

function unHide(variable) {
	divref = document.getElementById(variable)
	divref.style.display = "block"
}

function hide(variable) {
	divref = document.getElementById(variable)
	divref.style.display = "none"
}

function testCondition(condition) {
	split = condition.split(" ")
	if (split[1] == "<") {
		if (parseFloat(varValues[getIndexOf(split[0])]) < parseFloat(split[2])) { return true }
	}
	if (split[1] == ">") {
		if (parseFloat(varValues[getIndexOf(split[0])]) > parseFloat(split[2])) { return true }
	}
	if (split[1] == "=") {
		if ("" + varValues[getIndexOf(split[0])] == split[2]) { return true }
		if (split[2] == 'undefined' && varValues[getIndexOf(split[0])] == '') { return true }
	}
	if (split[1] == "!") {
	if ("" + varValues[getIndexOf(split[0])] != split[2] && varValues[getIndexOf(split[0])] != "") { return true }	
	}
	return false
}

function getValue(variable) {
	answers = document.getElementsByName(variable)
	for(i=0;i<answers.length;i++) {
		if (answers[i].type == "checkbox") {
			if (answers[i].checked == true) {
				return answers[i].value
				break
			}
		}
		if (answers[i].type == "select-one") {
			return answers[i].value
			break
		}
		if (answers[i].type == "text") {
			return removeQuotes(answers[i].value)
			break
		}
		if (answers[i].type == "textarea") {
			return removeQuotes(answers[i].value)
			break
		}

	}	
	return "undefined"
}

function getIndexOf(variable) {
	for(indexOf=0;indexOf<varNames.length;++indexOf) {
		if (varNames[indexOf] == variable) {
			return indexOf
			break
		}
	}
}

function onBlur(variable) {
	setFocus(varNames[getIndexOf(variable) + 1])
}

function checkRequired(variable) {
	requiredIndex = getIndexOf(variable)
	for(ix=0;ix!=requiredIndex ;++ix) {
		if (isRequired(varNames[ix]) && ((getValue(varNames[ix]) == "undefined") || (getValue(varNames[ix]) == ""))) {
			alert("You must answer the question (" + varDesc[ix] + ") to continue")
			setFocus(varNames[ix])
			return false
		}
	}
	return true
}

function checkRequiredComplete(variable) {
	requiredIndex = getIndexOf(variable) + 1
	for(ix=0;ix!=requiredIndex ;++ix) {
		if (isRequired(varNames[ix]) && ((getValue(varNames[ix]) == "undefined") || (getValue(varNames[ix]) == ""))) {
			alert("You must answer the question (" + varDesc[ix] + ") to continue")
			setFocus(varNames[ix])
			return false
		}
	}
	return true
}


function isRequired(variable) {
	for (ii=0;ii<varRequired.length;++ii) {
		if(varRequired[ii] == variable) { return true }
	}
	return false
}

function setFocus(variable) {
	sf = document.getElementsByName(variable)
	sf[sf.length - 1].focus() 
}

function onFocus(variable) {
   if(errorChk==1 || errorChk=='1') {
   				if(varNames[(getIndexOf(variable)-1)]=='copayvstdk' || varNames[(getIndexOf(variable)-2)]=='copayvstdk') {
   					if(getValue('copayvstdk')==1) {
   					upDate('copayvst','')
   					varValues[getIndexOf('copayvst')] = ''
   					}
   				}
  				if(varNames[(getIndexOf(variable)-1)]=='copaymeddk' || varNames[(getIndexOf(variable)-2)]=='copaymeddk') {
   					if(getValue('copaymeddk')==1) {
   					upDate('copaymed','')
   					varValues[getIndexOf('copaymed')] = ''
   					}
   				}
  				if(varNames[(getIndexOf(variable)-1)]=='copaytotdk' || varNames[(getIndexOf(variable)-2)]=='copaytotdk') {
   					if(getValue('copaytotdk')==1) {
   					upDate('copaytot','')
   					varValues[getIndexOf('copaytot')] = ''
   					}
   				}

	if (checkMinMax(variable)) {
		if (checkRequired(variable)) {
			if (checkErrors(variable)) {
				if(varNames[(getIndexOf(variable) - 1)]=='e_mail') {
					validateEmail('e_mail',0,errorChk)
				}
				if(varNames[(getIndexOf(variable) - 1)]=='e_mail_chk') {
					checkEmail(getValue('e_mail_chk'))
				}
				if(varNames[(getIndexOf(variable) - 1)]=='e_mail2') {
					validateEmail('e_mail2',0,errorChk)
				}
				return true
			}
		}
	}
	return false
    }
}

function tryToSubmit() {
var variable =	varNames[varNames.length - 1]
	if (checkMinMaxComplete(variable)) {
		if (checkRequiredComplete(variable)) {
			if (completeErrorsCheck(variable)) {
					return true
			}
		}
	}
	return false
}


function checkMinMax(variable) {
	requiredIndex = getIndexOf(variable)
	for(ix=0;ix!=requiredIndex ;++ix) {
		if (varMin[ix] != '' && varMax[ix] != '') {
			if(isNaN(varValues[ix]) && varValues[ix] != "" && varValues[ix] != undefined && document.webquest.elements[varNames[ix]].type == "text") {
				alert("You must enter a number between " + varMin[ix] + "-" + varMax[ix] + " for question (" + varDesc[ix] + ") to continue")
				setFocus(varNames[ix])
				return false
			}
			if (parseFloat(varValues[ix]) > parseFloat(varMax[ix]) || parseFloat(varValues[ix]) < parseFloat(varMin[ix])) {
				alert("You must enter a number between " + varMin[ix] + "-" + varMax[ix] + " for question (" + varDesc[ix] + ") to continue")
				setFocus(varNames[ix])
				return false
			}
			if ((parseFloat(varValues[ix]) > parseFloat(0) && parseFloat(varValues[ix]) < parseFloat(1)) || (checkDecimal(varValues[ix]))) {
				alert("You must enter a whole number with no decimals for question (" + varDesc[ix] + ") to continue")
				setFocus(varNames[ix])
				return false
			}

		}
	}
	return true
}

function checkMinMaxComplete(variable) {
	requiredIndex = getIndexOf(variable) + 1
	for(ix=0;ix!=requiredIndex ;++ix) {
		if (varMin[ix] != '' && varMax[ix] != '') {
			if(isNaN(varValues[ix]) && varValues[ix] != "" && varValues[ix] != undefined && document.webquest.elements[varNames[ix]].type == "text") {
				alert("You must enter a number between " + varMin[ix] + "-" + varMax[ix] + " for question (" + varDesc[ix] + ") to continue")
				setFocus(varNames[ix])
				return false
			}
			if (parseFloat(varValues[ix]) > parseFloat(varMax[ix]) || parseFloat(varValues[ix]) < parseFloat(varMin[ix])) {
				alert("You must enter a number between " + varMin[ix] + "-" + varMax[ix] + " for question (" + varDesc[ix] + ") to continue")
				setFocus(varNames[ix])
				return false
			}
			if ((parseFloat(varValues[ix]) > parseFloat(0) && parseFloat(varValues[ix]) < parseFloat(1)) || (checkDecimal(varValues[ix]))) {
				alert("You must enter a whole number with no decimals for question (" + varDesc[ix] + ") to continue")
				setFocus(varNames[ix])
				return false
			}
			
		}
	}
	return true
}

function checkErrors(variable) {
	requiredIndex = getIndexOf(variable)
	for(var ix=0;ix!=requiredIndex ;++ix) {
		for (var iix=0;iix<errName.length;++iix) {
			if(errName[iix] == varNames[ix]) { 
				if(evalCond(errConditions[iix])) {
					alert(errMessage[iix])
					setFocus(errFocus[iix])
					return false
				}
			}
		}
	}
	return true
}

function completeErrorsCheck(variable) {
	requiredIndex = getIndexOf(variable) + 1
	for(var ix=0;ix!=requiredIndex ;++ix) {
		for (var iix=0;iix<errName.length;++iix) {
			if(errName[iix] == varNames[ix]) { 
				if(evalCond(errConditions[iix])) {
					alert(errMessage[iix])
					setFocus(errFocus[iix])
					return false
				}
			}
		}
	}
	return true
}

function evalCond(conditions) {
	err = 0
	
	try {
		if(inLoop == 1) {
			splitConditions = conditions.split("&")
			for(xi=0;xi<splitConditions.length;++xi) {
				if (testCondition(splitConditions[xi])) { err += 1 }
			}
			if (err == splitConditions.length) { return true }
		else { return false }
		}
	}
	catch(exception) { inLoop = 0}
	if (inLoop == 0) {
		splitConditionsUp = conditions.split("|")
		if(splitConditionsUp.length > 1) {
			inLoop = 1
			for(xxi=0;xxi<splitConditionsUp.length;++xxi) {
				if (evalCond(splitConditionsUp[xxi])) {
					inLoop = 0
					return true 
				}
			}
			inLoop = 0
			return false
		}
	}
	
	splitConditions = conditions.split("&")
	for(xi=0;xi<splitConditions.length;++xi) {
		if (testCondition(splitConditions[xi])) { err += 1 }
	}
	if (err == splitConditions.length) { return true }
	else { return false }
}

function removeQuotes(s) {
	var temp = ""
	try {
		for(var i = 0;i<s.length;++i) {
			var c=s.charAt(i)
			if(c!="'" && c!='"' && c!='&' && c!='#' && c!='%') temp += c
		}
		return temp
	} catch(exception) { }
}

function removeBlanks(s) {
	var temp = ""
	try {
		for(var i = 0;i<s.length;++i) {
			var c=s.charAt(i)
			if(c!=" ") temp += c
		}
		return temp
	} catch(exception) { }
}

function addCookie(variable,vValue) {
	document.cookie=variable + '=' + vValue + ';expires="Thursday, 01-Jul-16 12:00:00 GMT"'
}

function getCookie(variable) {
	var tempString = document.cookie
	var tString = tempString.split(";")
	for(var i=0;i<tString.length;++i) {
		var tS = tString[i].split("=")
		if (removeBlanks(tS[0]) == variable) { return tS[1] }
	}
	return false
}


function toggleErrorChk() {
	if (errorChk ==1) {
		alert("Error checking has been disabled")
		errorChk = 0
		addCookie("errorChk",0)}
	else {
		alert("Error checking has been enabled")
		errorChk = 1
		addCookie("errorChk",1)}
}

function chkErrorChk() {
	if (errorChk ==0) {
		unHide("pageNavigator")
	}
}

function setBgColor (id, color) {
  if (document.getElementById)
    document.getElementById(id).style.backgroundColor = color;
  else if (document.layers)
    document[id].bgColor = color == 'transparent' ? null : color;
  else if (document.all)
    document.all[id].style.backgroundColor = color; 
  if(color=='#EBEFF5' || color=='#EFD6AD') { document.getElementById(id).style.cursor = 'hand'}
  if(color=='white' || color=='') { document.getElementById(id).style.cursor = ''}
}

function checkDecimal(s) {
	var temp = ""
	try {
		for(var i = 0;i<s.length;++i) {
			var c=s.charAt(i)
			if(c==".") return true
		}
		return false
	} catch(exception) { }
}

function validateEmail(emailVar,man,db) {
	addr=getValue(emailVar)
	if (addr == '') {
	   return true;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
	      if (db) alert('email address contains invalid characters');
	      setFocus(emailVar)
	      return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
	      if (db) alert("email address contains non ascii characters.");
	      setFocus(emailVar)
	      return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('email address must contain an @');
	   setFocus(emailVar)
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('email address must not start with @');
	   setFocus(emailVar)
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('email address must contain only one @');
	   setFocus(emailVar)
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('email address must contain a period in the domain name');
	   setFocus(emailVar)
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   setFocus(emailVar)
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   setFocus(emailVar)
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   setFocus(emailVar)
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	suffix = suffix.toLowerCase();
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address.  Examples of valid domains are ".com .net .org .edu" etc');
	   setFocus(emailVar)
	   return false;
	}
	return true;
}

function checkEmail(sEmailCheck)
{
	sEmail=getValue('e_mail');
	sEmail=sEmail.toLowerCase();
	sEmailCheck=sEmailCheck.toLowerCase();
	if (sEmailCheck.indexOf(sEmail,0) == -1)
	{
		alert('The Email address you just typed does not match.  Please make sure you retyped your email address correctly.');
		setFocus('e_mail_chk');
		return false;
	}
	return true;	
}

