  /* 
  1.4 Added support for "exclusiveGroup"-class
  1.3 Fixed custom input problem
  1.2 Changed to handle new deeper table structure in simple questions.
  */
  

var CSSInputs = {  
  rowHoverClassName : "rowHover",
  cellHoverClassName : "cellHover",
  selectedRowClassName : "selectedRow",
  selectedCellClassName : "selectedCell",
  fakeClassName : "fakeInput",
  activeFakeBaseClassName : "active",
  inactiveFakeBaseClassName : "inactive",
  disabledFakeBaseClassName : "disabled",
  fakeRowClassName : "fakeInputRow",
  exclusiveClassName : "exclusive",
  fakeConnectionName : "fakeconnection",
  customTypeInput: "customTypeInput",
  
  cssInputsDebug : true,
  debugPreString : "imagecbxsxsl.js: ",
  debugPostString : "-error: ",
  
  inputs : null,
  testImage : null,
  
  checkForBackgroundImage : function() {
    CSSInputs.inputs = Utilities.getElementsByClassAndTagName(CSSInputs.fakeClassName, "A");
    var imageLoadError = true;
    if(CSSInputs.inputs.length > 0) {
      var cssBackgroundValue = Utilities.getActualStyleValue(CSSInputs.inputs[0], "background-image", "backgroundImage");
      if(cssBackgroundValue) {
        var reg = /[^url\(\\"].+[^\\"\)]/;
        var imageUrl = reg.exec(cssBackgroundValue);
        if(imageUrl.length > 0) {
          CSSInputs.testImage = new Image();
          CSSInputs.testImage.onload = CSSInputs.setupCSSinputs;
          CSSInputs.testImage.src = imageUrl[0];      
          if(typeof(noImages) == 'function') CSSInputs.testImage.onerror = noImages;
          document.body.appendChild(CSSInputs.testImage);
          CSSInputs.testImage.style.display = "none";
        }
      }
    }
  },

  setupCSSinputs : function() {
    for(var i=0; i<CSSInputs.inputs.length; i++) {
    	    var parentCell = Utilities.getKin(CSSInputs.inputs[i], "parent", "TD");
          try {   
      			if(CSSInputs.inputs[i].checked) Utilities.addClassName(parentCell.parentNode, CSSInputs.selectedRowClassName);
      			else CSSInputs.checkFakeConnection(CSSInputs.inputs[i]);
    			} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "initial check" + CSSInputs.debugPostString + err);}
    			//parentCell.style.cursor = "pointer"; //Uncomment to give the cell a pointer-cursor
    			if (Utilities.hasClassName(CSSInputs.inputs[i], new Array(CSSInputs.activeFakeBaseClassName + "Radio", CSSInputs.inactiveFakeBaseClassName + "Radio", CSSInputs.exclusiveClassName))) {
    			  CSSInputs.inputs[i].exclusive = true;
    				parentCell.onclick = function (e) {
    				  try {
    				  var targ = Utilities.getEventTarget(e);
    				  if(targ.type != "text" && targ.tagName != "TEXTAREA" && targ.tagName != "LABEL" && (targ.tagName != "A" || Utilities.hasClassName(targ, CSSInputs.fakeClassName))) {
      					var input = Utilities.getElementsByTagAndType("INPUT", new Array("radio", "checkbox"), this);
      					if(input.length == 1) {
      						input = input[0];
      						CSSInputs.dispatchClick(input);
      					}
    					}
    					} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "parentCell.onclick exclusive true" + CSSInputs.debugPostString + err);}
    				};
    			}
    			else {
    			  CSSInputs.inputs[i].exclusive = false;
    				parentCell.onclick = function (e) {
    				  try {
      				  var targ = Utilities.getEventTarget(e);
      				  	if(targ.type != "text" && targ.tagName != "TEXTAREA" && targ.tagName != "LABEL" && (targ.tagName != "A" || Utilities.hasClassName(targ, CSSInputs.fakeClassName))) {
        					var input = Utilities.getElementsByTagAndType("INPUT", "checkbox", this);
        					if(input.length == 1) {
        						input = input[0];
        						CSSInputs.dispatchClick(input);
        					}
        					else {
                    var fakeInputs = Utilities.getElementsByClassAndTagName(CSSInputs.fakeClassName, "A", this);
                    if(fakeInputs.length > 0 && fakeInputs[0].getAttribute(CSSInputs.fakeConnectionName)) {
                      var fakeInput = fakeInputs[0];
                      CSSInputs.fakeHideAndReveal(fakeInput);
                      CSSInputs.updateFakeInput(fakeInput, "checkbox", fakeInput.checked, false);
                    }
                  }
      					}
    					} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "parentCell.onclick exclusive false" + CSSInputs.debugPostString + err);}
    				};
    			}
    			Utilities.addClassName(parentCell.parentNode, CSSInputs.fakeRowClassName); 
    			parentCell.parentNode.onmouseover = function (e) {
    				Utilities.addClassName(this, CSSInputs.rowHoverClassName); 		 
    			};
    			parentCell.parentNode.onmouseout = function (e) {
    				Utilities.removeClassName(this, CSSInputs.rowHoverClassName); 	 		 
    			};
    			parentCell.onmouseover = function (e) {
    				Utilities.addClassName(this, CSSInputs.cellHoverClassName); 		 
    			};
    			parentCell.onmouseout = function (e) {
    				Utilities.removeClassName(this, CSSInputs.cellHoverClassName); 	 		 
    			}; 
    }
  },
  
  updateFakeInput : function(fakeInput, type, selected, disabled) {
    try {
  	if(fakeInput.exclusive) type = "radio";
  	if(Utilities.hasClassName(fakeInput, CSSInputs.customTypeInput)) type = type + "Custom";
  	if(disabled) {
  		Utilities.addClassName(fakeInput, CSSInputs.disabledFakeBaseClassName + Utilities.capitalize(type));
  	}
  	if(selected) {
  		Utilities.removeClassName(fakeInput, CSSInputs.inactiveFakeBaseClassName + Utilities.capitalize(type));
  		Utilities.addClassName(fakeInput, CSSInputs.activeFakeBaseClassName + Utilities.capitalize(type));
  	}
  	else {
  	  Utilities.removeClassName(fakeInput, CSSInputs.activeFakeBaseClassName + Utilities.capitalize(type));
  		Utilities.addClassName(fakeInput, CSSInputs.inactiveFakeBaseClassName + Utilities.capitalize(type));
    } 
  	} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "updateFakeInput" + CSSInputs.debugPostString + err);}
  },
  
  fakeHideAndReveal : function(fakeInput) {
    if(fakeInput) {
    try {
        var name = fakeInput.getAttribute(CSSInputs.fakeConnectionName);
        if(name) {
          var el = document.getElementsByName(name);
          if(el && el.length > 0) {
            el = el[0];
            if(el.style.display != "none") {
              el.style.display = "none";
              fakeInput.checked = false;
            }
            else {
              el.style.display = "";
              fakeInput.checked = true;
            }
          }
        }
      } catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "fakeHideAndReveal" + CSSInputs.debugPostString + err);}
    }
  },
  
  checkFakeConnection : function(fakeInput) {
    if(fakeInput) {
    try {
      var name = fakeInput.getAttribute(CSSInputs.fakeConnectionName);
      if(name) {
        var el = document.getElementsByName(name);
        if(el && el.length > 0) {
          el = el[0];
          if(!el.value || el.value == "") {
            el.style.display = "none";
            fakeInput.checked = false;
          }
          else {
            el.style.display = "";
            fakeInput.checked = true;
          }
          CSSInputs.updateFakeInput(fakeInput, "checkbox", fakeInput.checked, false);
        }
      }
    } catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "checkFakeConnection" + CSSInputs.debugPostString + err);}
    }
  },
  
  /* Dispatch a click event into the document tree */
  dispatchClick : function(elm) {
    try {
  	//return;
  	var evt = null; 
  	if(document.createEvent) { 
  		evt = document.createEvent('MouseEvents'); 
  		if(elm && elm.dispatchEvent && evt && evt.initMouseEvent) { 
  			evt.initMouseEvent( 
  			'click', 
  			true,     // Click events bubble 
  			true,     // and they can be cancelled 
  			document.defaultView,  // Use the default view 
  			1,        // Just a single click 
  			0,        // Don't bother with co-ordinates 
  			0, 
  			0, 
  			0, 
  			false,    // Don't apply any key modifiers 
  			false, 
  			false, 
  			false, 
  			0,        // 0 - left, 1 - middle, 2 - right 
  			null);    // Click events don't have any targets other than 
  			            // the recipient of the click 
  			elm.dispatchEvent(evt); 
  		} 
  	} else if( document.createEventObject ) {
  	  if(!elm.exclusive || !elm.checked) elm.checked = !elm.checked;
  	  //else elm.checked = !elm.checked;
  		elm.fireEvent('onclick');
  	}
  	} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "dispatchClick" + CSSInputs.debugPostString + err);}
  }
};
if(addOnloadEvent) addOnloadEvent(CSSInputs.checkForBackgroundImage);
else CSSInputs.checkForBackgroundImage();

LqSetValueLink.prototype.onclick = function () {
  if(!(this.answer.htmlElement.type == "radio" || this.answer.singleselect) || !this.isSelected)
  	this.answer.setSelected(!this.isSelected);
	//this.answer.htmlElement.checked = !this.answer.htmlElement.checked;
	//this.answer.onclick();
	return false;
}

LqSetValueLink.prototype.setSelected = function (value) {
  try {
	this.isSelected = value;
	var realInput = this.answer.htmlElement;
	var fakeInput = this.htmlElement;
	fakeInput.exclusive = this.answer.singleselect != null ? true : false;
  CSSInputs.updateFakeInput(fakeInput, realInput.type, this.isSelected, realInput.disabled);
	} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "LqSetValueLink.prototype.setSelected" + CSSInputs.debugPostString + err);}
};

LqAnswer.prototype.onclick = function () {
  try {
  	var el = this.htmlElement;
  	//if(el.type == "radio" && !el.checked) return true; //Fix so that CSS-radiobuttons cannot be deselected
  	this.updateSetValueLink();
    if(this.updateConnection != "undefined" || this.updateConnection != null) this.updateConnection();
  	if(jQuery) $(this.htmlElement).change();
	if(!el.checked) return true;
  	this.verifySingleSelect();
	} catch(err) {if(CSSInputs.debug) alert(CSSInputs.debugPreString + "LqAnswer.prototype.onclick" + CSSInputs.debugPostString + err);}
	return true;
};

LqAnswer.prototype.getGroup = function () {
		var group = [];
		var classActivator = "exclusiveGroups";
		if(jQuery) {
			this.exclusiveGroups = this.exclusiveGroups || $(this.htmlElement).parents(".tableFormat1:first").hasClass(classActivator);
			if(this.exclusiveGroups) {
				var altGroup = $(this.htmlElement).hasClass("alternateDesign");
				$.each(this.question.answers,function() {
					if($(this.htmlElement).hasClass("alternateDesign") === altGroup) {
						group.push(this);
					}
				});
			}
		}
		if(!group.length) group = this.question.answers;
		if(!jQuery) {
			var args = Utilities.getFakeClassArguments(this.htmlElement, "group");
			var range = (args && args.length > 1) ? args[1].split("-") : null;
			if(range && range.length > 1) {
			var from = range[0];
			var to = range[1];
			if(!isNaN(from) && !isNaN(to) && from <= to && to <= group.length && from >= 1) {
				group = group.slice(from-1,to);
			}
		}
	  }
	  return group;
};
LqAnswer.prototype.verifySingleSelect = function () {
	var group = this.getGroup();
	for (var i=0; i<group.length; i++) {
		if (group[i] != this) {
			if (group[i].select!=null) {
				if (group[i].isSelected() && 
					!(this.singleselect == null && group[i].singleselect == null || 
					this.singleselect != "" && this.singleselect == group[i].singleselect ))
						group[i].setSelected(false);

				if (!group[i].isSelected()) {
					for (var j=0; j<this.question.answers.length; j++)
						if (this.question.answers[j].answerconnect == this.question.answers[i]) {
							 this.question.answers[j].htmlElement.value = "";
							 this.question.answers[j].answerconnect.updateConnection();
						}
				}
			}
			else switch (group[i].htmlElement.type) {
				case "radio":
				case "checkbox":
					if (group[i].htmlElement.checked && 
						!(this.singleselect == null && group[i].singleselect == null || 
						this.singleselect != "" && this.singleselect == group[i].singleselect )) {
							   group[i].htmlElement.checked = false;
							   group[i].updateConnection();
							}

					if (!group[i].htmlElement.checked) {
						for (var j=0; j<this.question.answers.length; j++)
							if (this.question.answers[j].answerconnect == this.question.answers[i])
								this.question.answers[j].htmlElement.value = "";
					}
					break;
				case "text":
					if (!group[i].answerconnect && group[i].htmlElement.value != "" && 
						!(this.singleselect == null && group[i].singleselect == null || 
						this.singleselect != "" && this.singleselect == group[i].singleselect ))
							group[i].htmlElement.value = ""; 
					break;
			}
			group[i].updateSetValueLink();
		}
				else if(this.connectedto.length > 0) {
            this.updateConnection();
        }
	}
};
function addSubmitHover() {
var submitHover = "submitHover";
try {
  var inputs = Utilities.getElementsByTagAndType("INPUT", "submit");
  for(var i=0; i<inputs.length; i++) {
    var parent = inputs[i];
		parent.onmouseover = function (e) {
			Utilities.addClassName(this, submitHover);
		};
		parent.onmouseout = function (e) {
			Utilities.removeClassName(this, submitHover); 	 		 
		};     
  }
  } catch(e) { alert("removeemptycaptionrow.js-error: addSubmitHover: " + e); }
}
//if(jQuery) $('input:submit').hover(function() {$(this).toggleClass(submitHover)});
//else if(addOnloadEvent) addOnloadEvent(addSubmitHover);
//else addSubmitHover();
