function checkblanks(start, end, type) {
	//the purpose of this function is to check all form inputs and check for illegal responses (blanks or 0s)

	//set missing counter to 0
	var missing = 0;
	var i = 0;
	
	//document.write(type);
	//if a radio form, then use this checking mechanism:
	if (type == 2) {
	
		//check for blank responses
		for(i = start; i <= end; i++) {
			var name = "v" + i;				// create button name
			var buttonrow = document.getElementsByName(name);	//get button controls with matching names
	
			for(var q = 0; q < buttonrow.length; q++) {		///loop through row of buttons
				var test = buttonrow[q].checked;
				if (test == "1") {				//if the button is checked
					if (buttonrow[q].value == "") {				//and it's value is blank, then display error message
					missing++;
					}
				}
			}				
		}
		//document.write(end);
	} else {
	// if a text form, then use this checking mechanism

		//check for blank responses
		for(i = start; i <= end; i++) {
			var name = "v" + i;
			var box = document.getElementsByName(name);
			
			//document.write(box[0].value);
			
			if (box[0].value == "") {
				missing++;
			}
		}
	}
	var good = false;
	//if missing if > 0, then display alert message
	if (missing > 0) {
		alert("I'm sorry, you've forgotten to answer " + missing + " question(s)");

	} else {
		formsubmit('others');
		
	}
}

function formsubmit(form) {
	var frm = document.getElementsByName(form);
	frm[0].submit();
	//document.others.submit();
}

function leave(login) {
	response = confirm("Are you sure you want to QUIT this study? Click OK to quit the study for good or CANCEL to return your current page.");
	//if response is true, then go to quit.php
	if (response) {
		//if logged in user, then attach type=user to quit url
		if (login) {
			parent.location = "quit.php?type=user";
		//otherwise, attach rater to quit url
		} else {
			parent.location = "quit.php?type=rater";
		}
	}
}

function logout() {
	var answer = confirm("If you log out in the middle of a task, you will lose all data for that task. Click OK to logout now or click CANCEL to stay logged in.");
	//if response is true, then logout
	if (answer) {
		parent.location="logoff.php";
	}
}

function msg() {
	var why = alert("Providing your high-school will allow you to retrieve your unique ID and password in case you forget it.");
}


function blocker(num, subname, op) {
	//create click counter
	if (typeof blocker.count == 'undefined') {
		blocker.count = 1;
	}
	
	//increment or decrease block counter
	if (op == "add") {
		blocker.count++;
	} else if(op == "subtract") {
		blocker.count--;
	}
	
	//if the user has click the appropriate number of times, then enable the submit button
	if ((blocker.count) == num) {
		//enable the element
		document.surverform.goforth.disabled=false;
	} else {
		document.surverform.goforth.disabled=true;
	}
	var target = "block" + blocker.count;
	window.location.hash=target;
}
		
function dissubmit(butt,action) {
	if (action == "disable") {
		butt.disabled=true;
	} else if (action == "enable") {
		butt.disabled=false;
	}
	
}		 

function validate(prefix, start, end, type, min, max) {

	//disable submit button
	dissubmit(document.surverform.goforth, 'disable');
	
	//the purpose of this function is to check all form inputs and check for illegal responses (blanks or 0s)

	//set missing counter to 0
	var missing = 0;
	var invalid = 0;
	var i = 0;
	var answer = "";
	var what = new Array();
	
	//document.write(type);
	//if a radio form, then use this checking mechanism:
	if (type == 2) {
	
		//check for blank responses
		for(i = start; i <= end; i++) {
			var name = prefix + i;				// create button name
			var buttonrow = document.getElementsByName(name);	//get button controls with matching names
	
			for(var q = 0; q < buttonrow.length; q++) {		///loop through row of buttons
				var test = buttonrow[q].checked;
				if (test == "1") {				//if the button is checked
					if (buttonrow[q].value == "") {				//and it's value is blank, then display error message
					missing++;
if(min!="") {
					what[missing]= i - min;
} else {
what[missing] = i;
}
					}
				}
			}
		}
	} else {
	// if a text form, then use this checking mechanism

		//check for blank responses
		for(i = start; i <= end; i++) {
			var name = prefix + i;
			var box = document.getElementsByName(name);
			
			//document.write(box[0].value);
			
			if (box[0].value == "") {
				missing++;
				what[missing]= i;
			//check to see that the box contains entries within desired range
			} else if (box[0].value < min || box[0].value > max) {
				invalid++;
			}

		}
	}
	var good = false;
	//if missing if > 0, then display alert message
	if (missing > 0) {
		var list = "";
		for(i=1; i<=what.length-1; i++) {
			
			list = list + what[i] + ", ";
		}
		
		answer=confirm("You've forgotten to answer " + missing + " question(s):\n\r" + list + "\n\r Click OK to continue or CANCEL to go back.");
		if(answer) {
		} else {
			dissubmit(document.surverform.goforth, 'enable');
		}
		
	} else if (invalid > 0) {
		
		answer = confirm("I'm sorry, " + invalid + " of your answers are not within an acceptable range. Click OK to continue without fixing your answers. Click CANCEL to go back.");
		if(answer) {
		} else {
			dissubmit(document.surverform.goforth, 'enable');
		}
		
	} else {
		answer = 1;
		
	}
	
	if (answer) {
		formsubmit('surverform');
	}
}

function ym(type) {
	if(type==2) {
	
	var g = document.surverform.v8.value;
	var h = document.surverform.v9.value;
	var i = document.surverform.v10.value;
	var j = document.surverform.v11.value;
	
	} else if(type==1) {
	//check years and months
	var g = document.surverform.v22.value;
	var h = document.surverform.v23.value;
	var i = document.surverform.v24.value;
	var j = document.surverform.v25.value;
	
	}
	if (g==""|g<0|g>100|h==""|h<0|h>12|i==""|i<0|i>100|j==""|j<0|j>12) { // if years is blank, or not between 0 and 100
		var response = alert("I'm sorry, please check your years and months. They appear to be incorrect. If zero years or months, please type 0 instead of leaving the box blank.");
		
	} else {

		validate('v',1,21,2,'','');
	}

}

