var browserType='MSIE';
var g_gridColor="#3D7BE6";
var g_hasImageButtonsbehavior = false;
var g_selections = new Array();
var g_inputs = new Array();
var g_isGrid3D = false;
var g_isGrid = false;
var g_grid3DRadioButtons = new Array();
var g_gridRadioButtons = new Array();

var g_questionType;
var g_questionTypes = {Open:"open", Single:"single", Multi:"multi", Grid:"grid", Grid3D:"grid3D"};

var tables = document.getElementsByTagName("table");

if(navigator.userAgent.indexOf("MSIE") < 0 ){ //NOT IE;
 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 browserType='Firefox';
}

var i;
for(i=0; i<tables.length; i++){

  if(browserType=='Firefox') {
  	initGrid3D(tables[i]);
  	initGrid(tables[i]);
  }
  else {
	  if( tables[i].p_questionType == "grid3D" ) {
		initGrid3D(tables[i]);
	  }
	  if( tables[i].p_questionType == "grid" ) {
		initGrid(tables[i]);
	  }
  }
}

function initGrid3D(element)
{


	if (element.tagName == "TABLE") {
		//pub_windowStart(element);
		
		//g_hasImageButtonsbehavior = pub_hasBehavior(element, "imageButtons");
		g_questionType = element.p_questionType;
		g_isGrid3D = g_questionType==g_questionTypes.Grid3D;
		
		if(!g_hasImageButtonsbehavior || g_isGrid3D || browserType=='Firefox') {
			//g_gridColor	= pub_getElementProperty(element.p_gridColor, pub_getInverseColor(pub_getBgColor(element)));
			if( typeof(element.p_grid3DRadioButtons)=="string" && element.p_grid3DRadioButtons.indexOf("[")>-1 ) {
				var temp;
				eval("temp = " + element.p_grid3DRadioButtons);
				for(var i=0; i<temp.length; i++) {
					g_grid3DRadioButtons[temp[i]] = true;
				}
			}
		
			var inputs = element.getElementsByTagName('INPUT');
			for(var i=0;i<inputs.length;i++) {
				var input = inputs[i];
				if(input.type != "submit" && input.name != "__loop" ) {  // RL ADDED NOV 6 2008 - TAKE NO ACTION ON SUBMIT BUTTON
					var questionName = pub_removePrecode(input.name); 
					if(!g_inputs[input.id] && input.parentNode.childNodes.length==1) {

						g_inputs[input.id] = input;
						//alert("g " + g_isGrid3D + " qn " + questionName);
						if(iwfStyle(input, 'visibility') == "hidden" || iwfStyle(input, 'display') == "none") {
						  input.disabled = true;
						}
						if (!input.disabled ){


						  if( input.type=="radio" && ( browserType=='Firefox')) {

							  if(input.parentNode.addEventListener)
								input.parentNode.addEventListener("click",pe_doRadioButton ,false);
							  else
								input.parentNode.attachEvent("onclick", pe_doRadioButton);

							  if(input.checked)
								pe_doRadioButton(input, true)
						   }
						   if( input.type == "checkbox" && ( (g_isGrid3D && g_grid3DRadioButtons[questionName]!=null) || browserType=='Firefox' )) {
						    if(input.parentNode.addEventListener)
								input.parentNode.addEventListener("click",pe_doCheckbox ,false);
							else
								input.parentNode.attachEvent("onclick", pe_doCheckbox);
						   }
						}
					}
                		}
			}

		}		
	}

}

function initGrid(element)
{

	if (element.tagName == "TABLE") {
		var inputs = element.getElementsByTagName('INPUT');
		for(var i=0;i<inputs.length;i++) {
			var input = inputs[i];
			if(input.type != "submit" && input.name != "__loop") {  // RL ADDED NOV 6 2008 - TAKE NO ACTION ON SUBMIT BUTTON
				var questionName = pub_removePrecode(input.name);  
				if(iwfStyle(input, 'visibility') == "hidden" || iwfStyle(input, 'display') == "none") {
				  input.disabled = true;
				}
			}
		}
	}		
}

function pe_doCheckbox(e)
{
  var button;
  
  if(e.target)
  	button=e.target;
  else
  	button = e.srcElement;

  if(button.tagName=="TD") {
  	button = button.firstChild;
  	if(button.tagName=="INPUT" && button.type != "check")
  		button.checked = !button.checked;
  }
  
  

}

function pub_removePrecode(inputName) {
	if( typeof(inputName)=="string" && inputName.indexOf("_") > 0 )
		return inputName.substring( 0, inputName.lastIndexOf("_") );
	else
		return inputName;
}

function pe_doRadioButton(button, load)
{
	try	{
	
		if(!load) {
			if(button.target)
				button=button.target;
			else
				button = window.event.srcElement;
		}
		

		if(button.tagName=="TD") {
			button = button.firstChild;
		}
		
		if(button.tagName!="INPUT" && button.type != "radio")
			return;

		if(!load) {
			button.checked = true;
		}
		
		var name = button.name;
		if( g_selections[name] )
			iwfStyle(g_selections[name],'backgroundColor', '');
			//g_selections[name].style.backgroundColor = '';

			var parentCell = button.parentNode;
			if(parentCell) {
				g_selections[name] = parentCell;
				iwfStyle(parentCell,'backgroundColor', g_gridColor);
				//parentCell.style['backgroundColor'] = g_gridColor;
		}
	} catch (e){ window.status = e.description }
}

function pub_getElementProperty(value, fallbackValue) {
	if (typeof(value)!="undefined" && value!=null && value!="undefined" && value!="null" && value!="")
		return value;
	return fallbackValue;
}

function pub_getBgColor( obj )
{
	var bgColor = "silver";
	found = false;
	
	while(!found) {
		if( (obj.visibility)!="undefined" ) {
			if( obj.currentStyle.backgroundColor && obj.currentStyle.backgroundColor!="" ) {
				bgColor = obj.style.backgroundColor;
				found = true;
			}
			else if (obj.bgColor && obj.bgColor!="") {
				bgColor = obj.bgColor;
				found = true;
			}
		}
		if(!found)
			obj = obj.parentNode;
	}
	return bgColor;
}

function pub_getInverseColor(color)
{
	var hex = "0123456789ABCDEF";
	var tcolor = parseInt(color.substring(1),16);
	var another = ~tcolor & 0xffffff; // inverse color
	var invstr = "";

	while(another>0) {
		invstr = hex.charAt(another & 0xf) + invstr;
		another>>>=4;
	}
	while(invstr.length < 6)
		invstr = "0" + invstr;
	
	invstr = "#" + invstr;

	return invstr;
}
function iwfStyle(id, styleName, newStyle)
{ 
	//var el = document.getElementById(id); 
	var el;
	el = id;
	if (!el) { return false; } 
		var ret = ''; 
	if (typeof(newStyle) != 'undefined'){   // assigning new style   
		if (el.runtimeStyle){

			ret = el.runtimeStyle[styleName] = newStyle;
		} 
		else {

			ret = el.style[styleName] = newStyle;
		} 
	} 
	else if (el.currentStyle) {   // reading IE style
		ret = el.currentStyle[styleName];
	} 
	else {   // reading DOM style   
		try {
			var cs = document.defaultView.getComputedStyle(el,null);
			ret = cs.getPropertyValue(styleName);   
		}
		catch(e) {   } 
	}
	return ret;

}

