
$(document).ready(function() {

   $("#nav input").hover(
      function() {$(this).attr("src","img/next_on.gif"); }, // over
      function() {$(this).attr("src","img/next.gif");}      // out
   );
   
   $("#start input").hover(
      function() {$(this).attr("src","img/start_on.gif");}, // over
      function() {$(this).attr("src","img/start.gif");}      // out
   )
   
   $("td.clickable").click( function() {
      $(this).parent('tr').children('td').removeClass('RadioH');
      $(this).children('input').attr('checked',true);
      $(this).addClass('RadioH');
   });
   
   $("#MaxDiff td.clickableMD").click( function() {
      $(this).parent('tr.MDRow').children('td.MDRadio').removeClass('MDRadioH');
      ($(this).hasClass('max')) ? $('td.max').removeClass('MDRadioH'): $('td.min').removeClass('MDRadioH');
      $(this).children('input').attr('checked',true);
      $(this).addClass('MDRadioH');
   });
   
   $(".dataCHANGE").animate({
       backgroundColor: "#FEFF5F"
   },1000);
   
         $(".points").change(function() {
            var tot = 0;
            $(".points").each(function(i) { 
               tot += (jQuery.trim($(this).val()) == "" || isNaN(jQuery.trim($(this).val()))) ? 0 : parseInt($(this).val()); });
            $("#total").val(tot);
         });
         $("#container").mousemove(function() { $(".points").change() ;});
         
    $(".ClearText").click( function() {
        $(".ClearRadios").val("");
    });
    
    $(".ClearRadios").keypress(function(){$(".ClearText").attr("checked","");});
    $(".ClearRadios").click(function(){$(".ClearText").attr("checked","");});
   
});   // ready



// Clears all checkboxes on the page, except for the calling element. Useful for "None of the above" type answers.
//	Usage: 	<input type="checkbox" id="checkbox42" name="foo42" value="1" onclick="ClearAll(checkbox42, this.form);">
//				Clears all checkboxes on the form, except for id="checkbox42"
function ClearAll(me, form){
	if( me.checked == true )
		for(i = 0; i < form.elements.length; i++)
			if(form.elements[i].type == "checkbox" && form.elements[i].name != me.name)
				form.elements[i].checked = false;
}

// Clears one checkbox.  Useful for clearing out the "None..." answer when selecting something else.
// Usage: 	<input type="checkbox" id="checkbox1" name="foo1" value="1" onclick="ClearOne(checkbox42);">
//				Clears id="checkbox42"
function ClearOne(control){
	control.checked = false;    // the 'None' control
}

// Clears one checkbox.  Useful for clearing out the "None..." answer when selecting something else.
// Usage: 	<input type="checkbox" id="checkbox1" name="foo1" value="1" onclick="ClearOne(checkbox42);">
//				Clears id="checkbox42"
function CheckOne(control){
	control.checked = true;    // the 'None' control
}

// Clears a range of checkboxes.  Useful when you need to clear a few checkboxes, but not all of them!
// Usage: 	<input type="checkbox" id="checkbox1" name="foo1" value="1" onclick="ClearRange(2, 10);">
//				Clears id="checkbox2", id="checkbox3" ... id="checkbox9", id="checkbox10"
function ClearRange(start, end){
	var foo;
	for(i = start; i <= end; i++) {
		foo = 'checkbox' + i;
		document.getElementById(foo).checked = false;
	}
}

// Clears a range of radios.  Useful when you need to clear a few radios, but not all of them!
// Usage: 	<input type="radio" id="radio" name="foo1" value="1" onclick="ClearRange(2, 10);">
//				Clears id="radio1", id="radio2" ... id="radio9", id="radio10"
function ClearRangeRadio(start, end){
	var foo;
	for(i = start; i <= end; i++) {
		foo = 'radio' + i;
		document.getElementById(foo).checked = false;
	}
}

// Clears a range of text boxes.  Useful when you need to clear a few textboxes, but not all of them!
// Usage: 	<input type="radio" id="radio" name="foo1" value="1" onclick="ClearRange(2, 10);">
//				Clears id="radio1", id="radio2" ... id="radio9", id="radio10"
function ClearRangeText(start, end){
	var foo;
	for(i = start; i <= end; i++) {
		foo = 'text' + i;
		document.getElementById(foo).checked = false;
	}
}

// Clears the contents of one textbox.  When clearing out a series of radios/checks, clear out the textbox
// attached to the "other"" response as well.
// Usage: 	<input type="checkbox" id="checkbox1" name="foo1" value="1" onclick="ClearAll(checkbox42, this.form); ClearText('text2');">
//				Clears all checkboxes on the page, and the textbox with id="text2"

function ClearText(control){
	document.getElementById(control).value = '';    // the 'None' control
}

// ADDCHECKS: how many checkboxes are selected
// Usage: 	<input type="checkbox" id="check1" name="features1" value="checkbox" onclick="addchecks(this.form)">
function addchecks(form){
	var temp;	
	temp = 0;

	for(i = 0; i < form.elements.length; i++) 
		if(form.elements[i].type == "checkbox" && form.elements[i].checked == true) 
			temp ++;

	form.total.value = temp;
}

//Sets the select index to the ith item in the select box. 
//To set the select box to the first item in the list use: onClick="ClearSelect('selectboxid',0);"

function ClearSelect(control, i) {
	document.getElementById(control).selectedIndex = i; 
}


function nxtSwap(imgName) {
	document.getElementById(imgName).src='images/nexth.gif'
}

function nxtRevert(imgName) {
	document.getElementById(imgName).src='img/next.gif'
}

//Toggles the visibility of an html element with a given ID. Use in a onclick event.
//Usage:		onclick="ToggleDisplay('idofelement',2);"
//				Onoroff = 0, turns off display, OnOrOff = 1, turns on display, OnOrOff = 2, flips the display to off if on or on if off.

function ToggleDisplay(id,onoroff) {
	if (onoroff == 0) {
		document.getElementById(id).style.display = 'none';
	}
	if (onoroff == 1) {
		document.getElementById(id).style.display = '';
	}
	if (onoroff == 2) {
		if (document.getElementById(id).style.display == '') {
			document.getElementById(id).style.display = 'none';
		}
		else {
			document.getElementById(id).style.display = '';
		}
	}
}

//Toggles the visibility of a range of html elements with a given IDbase and range. Use in a onclick event.
//Usage:		onclick="ToggleDisplay('idbase',3);"
//				Toggles the visibility of html elements with ids of idbase1, idbase2, idbase3
//				Onoroff = 0, turns off display, OnOrOff = 1, turns on display, OnOrOff = 2, flips the display to off if on or on if off.

function ToggleDisplayRange(idbase, range, onoroff) {
	if (onoroff == 0) {
		for (i=1;i<range+1;i++) {
			document.getElementById(idbase + i).style.display = 'none';
		}
	}
	if (onoroff == 1) {
		for (i=1;i<range+1;i++) {
			document.getElementById(idbase + i).style.display = '';
		}
	}
	
	if (onoroff == 2) {
		for (i=1;i<range+1;i++) {
			if (document.getElementById(idbase + i).style.display == '') {
				document.getElementById(idbase + i).style.display = 'none';
			}
			else {
				document.getElementById(idbase + i).style.display = '';
			}
		}
	}
}

//Shows toggled items on page load if control is not selected. This is required to show toggled items on refresh or on inline error.
//This also hides toggled items on page load. This is required to hide toggled items on initial page load. This is necessary if javascript is disabled.
//Usage:		<body onLoad="ShowToggledElements('radio1','monthly',4);">
//				Checks to see if radio1 is selected and then makes sure that elements with ids monthly1 through monthly4 are visible, or invisisble depending on the state of control

function ShowToggledElementsRange(control,toggledidbase,range) {
	if (document.getElementById(control).checked) {
		for(i=1; i<range+1;i++) {
			document.getElementById(toggledidbase + i).style.display = '';
		}
	}
	else {
		for(i=1; i<range+1;i++) {
			document.getElementById(toggledidbase + i).style.display = '';
		}
	}
	
}

//Shows toggled items on page load if control is not selected. This is required to show toggled items on refresh or on inline error.
//This also hides toggled items on page load. This is required to hide toggled items on initial page load. This is necessary if javascript is disabled.
//Usage:		<body onLoad="ShowToggledElements('radio1','monthly');">
//				Checks to see if radio1 is selected and then makes sure that element with id monthly are visible, or invisisble depending on the state of control

function ShowToggledElements(control,toggledid) {
	if (document.getElementById(control).checked) {
		document.getElementById(toggledid).style.display = ''
	}
	else {
		document.getElementById(toggledid).style.display = 'none'
	}
}



//Shows toggled items on page load if control is selected, but does not hide if not selected.
//Developed by W on 2/25/09 
function ShowToggledElementsNoHide(control,toggledid) {
	if (document.getElementById(control).checked) {
		document.getElementById(toggledid).style.display = ''
	}
}

//Functions for highlighting TDs. Requires Csshover2.htc.

// Function for Radio Buttons
function ToggleTDHighlight(qNum,row, me) {

   var i
   for (i = 0; document.getElementById('td'+qNum+'_'+row+'_'+i); i++)
   	document.getElementById('td'+qNum+'_'+row+'_'+i).style.backgroundColor = "";
      
   document.getElementById('td'+qNum+'_'+row+'_'+me).style.background = "#C9D5DF";
   document.getElementById('radio'+qNum+'_'+row+'_'+me).checked = true;
}

// Function for Radio Buttons
function ToggleTDHighlightMD(qNum,row, me) {

   var i
   for (i = 1; document.getElementById('td'+qNum+'_'+row+'_'+i); i++)
   	document.getElementById('td'+qNum+'_'+row+'_'+i).style.backgroundColor = "";
      
   document.getElementById('td'+qNum+'_'+row+'_'+me).style.background = "#C9D5DF";
   document.getElementById('radio'+qNum+'_'+row+'_'+me).checked = true;
}

//for xbc: page with one column of buttons; row is the variable number and me is always 1
function ToggleTDHighlightXBC(qNum,row, me) {

   var i
   for (i = 1; document.getElementById('td'+qNum+'_'+i+'_'+me); i++)
   	document.getElementById('td'+qNum+'_'+i+'_'+me).style.backgroundColor = "";
      
   document.getElementById('td'+qNum+'_'+row+'_'+me).style.backgroundColor = "#C9D5DF";
   document.getElementById('radio'+qNum+'_'+row+'_'+me).checked = true;
}

// Function for CheckBoxes
function ToggleTDHighlightCheckBoxes(qNum,row, me) {     
	if (document.getElementById('td'+qNum+'_'+row+'_'+me).style.backgroundColor == "") {
		document.getElementById('td'+qNum+'_'+row+'_'+me).style.backgroundColor = "#C9D5DF";
   	document.getElementById('checkbox'+qNum+'_'+row+'_'+me).checked = true;
	}
	else 
	{
		document.getElementById('td'+qNum+'_'+row+'_'+me).style.backgroundColor = "";
   	document.getElementById('checkbox'+qNum+'_'+row+'_'+me).checked = false;
	}
}

//onload highlight the right boxes
function ReHighlightTDs(numQs) {
	var i, j, k
	i = 1;
	j = 1;
	k = 1;
	q = 1;	// for the question number!
	for(q=1; q<=numQs; q++) {	// loop through the number of questions..., provided in the body tag of the page
		if (document.getElementById('td'+q+'_'+i+'_'+j)) { 
			for(k = q; document.getElementById('td'+k+'_'+i+'_'+j); k++) {
				for(i = 1; document.getElementById('td'+k+'_'+i+'_'+j); i++) {
					for(j = 1; document.getElementById('td'+k+'_'+i+'_'+j); j++) {
						if (document.getElementById('radio'+k+'_'+i+'_'+j)) {
							if (document.getElementById('radio'+k+'_'+i+'_'+j).checked == true)
								document.getElementById('td'+k+'_'+i+'_'+j).style.backgroundColor="#C9D5DF";
						} //if
						else {
							if (document.getElementById('checkbox'+k+'_'+i+'_'+j).checked == true)
								document.getElementById('td'+k+'_'+i+'_'+j).style.backgroundColor="#C9D5DF";
						} //else
					}//for j
					j = 1
				} //for i
				i = 1
			} //for k
		} //if
	} //for q
} // function

//Highlights an answer in each of the x5b rows
//automatically with the onload event
//This works only on webdev
//Chooses which column to fill randomly
function FillX5B(numRows,columnmax) {
	var i
	if (String(window.location).indexOf('webdev') > 0) {
		for(i=1;i<=numRows;i++) {
			var randomnumber=Math.floor(Math.random()*columnmax+1)
			document.getElementById('radio1_'+i+'_'+randomnumber).checked = true;
		}
	}
}