function doCheckboxOpenResponseChange(inval)
{
	if (document.getElementById){
		var arr = inval.name.split("_")
		if((arr.length = 4) && (inval.type == "text")) {
			obj = document.getElementById("question_" + arr[2]);
			if(obj.length > 0) {
				for(i = 0; i < obj.length; i++) {
					curobj = obj[i]
					if(curobj && (curobj.type == "radio" || curobj.type == "checkbox") && curobj.value == arr[3]) {
						if(inval.value == "") curobj.checked = false
						else curobj.checked = true
					}		
				}
			}
		}
	}
}

function doCheckboxChange(que, ans)
{
	if (document.getElementById){
		//var robj = document.getElementById("question_" + que + "_" + ans);
		var obj = document.getElementById("open_response_" + que + "_" + ans);
		if(obj) {
			if(isChecked(que, ans)) {

			//obj.disabled = false
				obj.focus()
			}
			//else { obj.disabled = true; }
		}
	}
}

function doRadioChange(que, ans)
{
	if (document.getElementById){
		var robj = document.getElementById("question_" + que + "_" + ans);
		var obj = document.getElementById("open_response_" + que + "_" + ans);
		if(obj) {
			if(isChecked(que, ans)) {
				//obj.disabled = false
				obj.focus()
			}
			//else { obj.disabled = true; }
		}
	}
}

function isChecked(que, ans)
{
	ret = false
		nobj = document.getElementsByTagName("input") 
		if(nobj.length > 0) {

			for(i = 0; i < nobj.length; i++) {
				curobj = nobj[i]
				if(curobj && curobj.name == "question_" + que && (curobj.type == "radio" || curobj.type == "checkbox") && curobj.value == ans && curobj.checked) 
					ret = true
			}
		}
	return ret
}	

function doChecked(obj,que,ans)
{
	if(obj.value != "") {
		nobj = document.getElementsByTagName("input") 
		if(nobj.length > 0) {
			for(i = 0; i < nobj.length; i++) {
				curobj = nobj[i]
				if(curobj && curobj.name == "question_" + que && (curobj.type == "radio" || curobj.type == "checkbox") && curobj.value == ans) 
					curobj.checked=true;
			}
		}
		
	}
}


function limitTextSize(num, obj)
{
	if(obj.value.length > num-1)
	{ 
		obj.value = obj.value.substring(0, num-2)
		alert("You can not type more than "+ num +" characters in this field");
	}
}

var key = 0
var bkey = 0
var infocus = 0
document.onkeydown = doKeyDown;
function doKeyDown(e)
{
	if (!e) var e = window.event;
	bkey = key
	key = e.keyCode
}




//// related to calculations:
names = new Array()
namesh = new Array()
functions = new Array()
function calculate(obj)
{
	var t = ''
	var val = 0
	var i;
	for(i = 0; i < names.length; i++) 
	{
		newobj = document.getElementById(names[i])		// element to change
		fname = getFunctionName(functions[i])   		//function name
		quesv = getFunctionValues(functions[i]) 		// array of que values
		if(fname == "sum") val = doSum(newobj.name, quesv)	// make calculation
		if(newobj && val) newobj.value = val 		// init value
		newobjh = document.getElementById(names[i].replace('question','questionh'))
		if(newobjh && val > 0) newobjh.value = val 		// init hidden value
	}
}
function getFunctionName(fnm)
{
	arr = fnm.split('(')
	if(arr.length > 1) return arr[0]
	else return ""
}
function getFunctionValues(fnm)
{
	arr = fnm.split('(')
	if(arr.length > 1) {
		arr2 = arr[1].split(')')
		if(arr2.length > 1) {
			result = arr2[0].split(',')
		}
	} else {
		result = new Array()
	}
	return result;
}
function doSum(name, quesv)
{
	re = /^(\+|-){0,1}[\d.]+$/;
	arr = name.split('_')
	var su = 0
	if(arr.length > 3) {
	   for(i = 0; i < quesv.length; i++) {
		nm = arr[0] + "_" + arr[1] + "_" + quesv[i] //+ (eval(quesv[i])+1) + "_" + arr[3]
		sobj = document.getElementById(nm)

		if(sobj && re.exec(sobj.value)) {
			su = su +  eval(sobj.value)
			}
	   }
	}
	return su
}
//// END related to calculations

