// 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;
	}
}


function ClearHighlight(idstart, start, end){
	var foo;
	for(i = start; i <= end; i++) {
		foo = idstart + i;
		document.getElementById('td'+foo).style.background = '';
		//alert(document.getElementById(foo).checked);
		document.getElementById('checkbox'+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='images/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'
	}
}


//Functions for highlighting TDs. Requires Csshover2.htc.

// Function for Radio Buttons
function ToggleTDHighlight(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;
		}
	}
}

$(function(){

  // Slider
  $('.slider').slider({
    value: 5,
    min: 1,
    max: 9,
    step: 1,
    change: function(event, ui) {
      $(this).find("input").val(ui.value);
      $(this).closest(".slider-row").ClearTypeFadeIn({ speed: "fast"});
      $(this).ClearTypeFadeIn({ speed: "fast"});
    }
  });

  $('.sliderReverse').slider({ 
    value: 5,
    min: 1,
    max: 9,
    step: 1,
    change: function(event, ui) {
      $(this).find("input").val(10 - ui.value);
      $(this).closest(".slider-row").ClearTypeFadeIn({ speed: "fast"});
      $(this).ClearTypeFadeIn({ speed: "fast"});
    }
  });

  $('.slider, .sliderReverse').each(function() { 
    if($(this).find("input").val() != ""){
      $(this).slider("option", "value", $(this).find("input").val()); 
    } else {
      //$(this).closest(".slider-row").ClearTypeFadeTo({ speed: "slow", opacity: 0.40 });
      $(this).ClearTypeFadeTo({ speed: "slow", opacity: 0.40 });
    }
  });

});

(function($){
  $.fn.ClearTypeFadeTo = function(options) {
    if (options){
      $(this)
        .each(function() {
          if (!jQuery.support.opacity ) {
            $(this).attr('originalBackgroundColor', $(this).css('background-color'));
            $(this).css({ 'background-color': (options.bgColor ? options.bgColor : '#fff') });
          }
        });
      $(this)
        .fadeTo(options.speed, options.opacity, function() {
          if (!jQuery.support.opacity ) {
            if (options.opacity == 0 || options.opacity == 1) {
              $(this).css({ 'background-color': $(this).attr('originalBackgroundColor') }).removeAttr('originalBackgroundColor');
              $(this).get(0).style.removeAttribute('filter');
            }
          }
          if (options.callback != undefined) options.callback();
        });
    }
  };

  $.fn.ClearTypeFadeIn = function(options) {
    if (options){
      $(this)
        .css({ opacity: 0 })
        .ClearTypeFadeTo({ speed: options.speed, opacity: 1, callback: options.callback });
    }
  };

  $.fn.ClearTypeFadeOut = function(options) {
    if (options){
      $(this)
        .css({ opacity: 1 })
        .ClearTypeFadeTo({ speed: options.speed, opacity: 0, callback: options.callback });
    }
  };
})(jQuery);
