function getq(ele)
{
    var p = ele.parentNode;
    var idx = 0;
   
    while(p.className != "question" && idx < 30)
    {
    	
		p = p.parentNode;		
		idx++;
    }
    if(idx >= 30)
    {
		return null;
	}
	else
	{
		return p;
	}    
}
function dealQitaText(FormObject,isEnabled)
{
	if(FormObject != undefined && FormObject != null ){
		if( !isEnabled){
		    FormObject.value="";
		    FormObject.disabled=false;
		}else{
		    FormObject.disabled = true;
	    }
	}
}
function setup()
{
	var agt = navigator.userAgent.toLowerCase();
	isIE = (agt.indexOf("msie") != -1 && agt.indexOf("opera") == -1);
	
	if(isIE )
	{
		try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {}
	}
	var divs=document.getElementsByTagName("div");
	for(var i=0;i<divs.length;i++)
	{
		
		if(divs[i].className.indexOf("hover")>=0)
		{
			divs[i].onmouseover=function(){this.className = this.className.replace(/hover/gi,"ruled");return false;}
			divs[i].onmouseout=function(){this.className = this.className.replace(/ruled/gi,"hover");return false;}
		    
		}
    }
	var inputs = document.getElementsByTagName("input");
	var label, img, inputID;
	for(var i = 0; i < inputs.length; i++)
	{
		switch(inputs[i].type)
		{
			case "checkbox":
				label = document.getElementById("l" + inputs[i].id);
				if(inputs[i].checked)
				{
					label.className = label.className.replace("cb_off", "cb_on");
					
				}
				if(isIE) label.style.position = "relative";
				inputs[i].onclick = function() { cbtoggle(this); }
				inputs[i].onfocus = function() { focus(this); }
				inputs[i].onblur = function() { blur(this); }
				if(isIE)	// IE! ignores images in labels.
				{
					img = label.getElementsByTagName("img")[0];
					img.onclick = function() { this.parentNode.click(); }
				}
				break;
			case "radio":		
				label = document.getElementById("l" + inputs[i].id);
				if(inputs[i].checked)
				{
					label.className = label.className.replace("rb_off", "rb_on");					
					inputs[i].history = true;
				}
				else
				{
					inputs[i].history = false;
				}
				if(isIE) label.style.position = "relative";
				inputs[i].onclick = function() { rbtoggle(this); }
				inputs[i].onfocus = function() { focus(this); }
				inputs[i].onblur = function() { blur(this); }
				
				if(isIE)	// IE! ignores images in labels.
				{					
					img = label.getElementsByTagName("img")[0];
					img.onclick = function() { this.parentNode.click(); }
				}
				break;
			case "text":
				if(inputs[i].className.indexOf("other") > -1)
				{
					creatediv(inputs[i]);
				}
				break;
		}
	}
	inputs = document.getElementsByTagName("textarea");
	for(var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.indexOf("other") > -1)
		{
			creatediv(inputs[i]);
		}
	}
	safari();
}

function focus(input)
{
	if(input)
	{
		var label = document.getElementById("l" + input.id);
		label.parentNode.className = label.parentNode.className + " selected";
	}
}
function blur(input)
{
	var label = document.getElementById("l" + input.id);
	label.parentNode.className = label.parentNode.className.replace(/\s(selected)/gi,"");
}
function cbtoggle(input)
{
	var label = document.getElementById("l" + input.id);
	label.className = "cb_" + ((input.checked) ? "on" : "off") + " selected";
	if(input.other)
	{
		var other = document.getElementById(input.other);
		if(input.className.indexOf("other") > -1 && input.checked)
		{			
			other.disabled = false;
		}else if(input.className.indexOf("other") > -1 && !input.checked)		
		{
			other.value = "";
			other.disabled = true;
			
		}
	}
	
	// 处理单多选后跟文本框的问题
	dealOptionText( input );
	
}
function rbtoggle(input)
{
	if(input.history == true)
	{
		input.checked = false;
		input.history = false;
	}
	else
	{
		input.history = true;
	}
	var group = new Array();
	var inputs = getq(input).getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++)
	{		
		if(inputs[i].name == input.name)
		{
			group.push(inputs[i]);
		}
	}	
	
	if(input.className.indexOf("col") > -1)
	{
		//inputs = input.parentNode.parentNode.parentNode.getElementsByTagName("input");		
		var objTr = input.parentNode.parentNode.parentNode.parentNode.rows;    //  行 //input.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("tr");
		var objTrLength = objTr.length;    //  行数		
		var checkedColumn = input.parentNode.parentNode.cellIndex;   //  选项所在的table的哪一列
	
		for( var k = 0; k < objTrLength; k++ )
		{		
			if(objTr[k].cells[checkedColumn] != null && objTr[k].cells[checkedColumn]!=undefined )
			{
				var input_new = objTr[k].cells[checkedColumn].getElementsByTagName("input");			
				if(input_new[0].name != input.name)
				{				
					group.push(input_new[0]);
					if(input.checked)
					{		
						input_new[0].checked = false;					
					}
				}
			}
		}		
	}
	var label;
	
	for(var i = 0; i < group.length; i++)
	{		
		label = document.getElementById("l" + group[i].id);
		label.className = "rb_" + ((group[i].checked) ? "on" : "off");
		label.parentNode.className = label.parentNode.className.replace(/\s(selected)/gi,"")
		if(group[i].id == input.id)
		{			
			label.parentNode.className = label.parentNode.className + " selected";				
		}
		else
		{
			group[i].history = false;
		}
		// 处理单多选后跟文本框的问题
	    dealOptionText( group[i] );
	}
	
	if(input.other)
	{
		var other = document.getElementById(input.other);
		if( input.className.indexOf("other") > -1 && input.checked )
		{
		    other.disabled = false;		    
		}else
		{			
			other.value = "";			
			other.disabled = true;
		}
	}
	
	
				
}
/*
 * 处理单多选后跟问题框，联动实现
 */
function dealOptionText( input ){
	
	if(document.getElementById('text_'+input.id) != 'undefind' 
				    && document.getElementById('text_'+input.id) != null )
	{
		var option_text = document.getElementById('text_'+input.id);
		
		if( input.checked )
		{			
			option_text.disabled = false;
		}
		else
		{			
			option_text.value = '';
			//option_text.readonly = true;
			option_text.disabled = true;
		}
	}
	
}
function onesubmit(element, delay)
{
	var tagname = element.tagName.toLowerCase();
	
	delay = (delay) ? delay : 1500;
	
	if(tagname == "a")
	{
		var href = element.href;
		var click = element.onclick;
		
		setTimeout(function() { element.href = "javascript:void(0);"; element.onclick = null; }, 0);
		setTimeout(function() { element.href = href; element.onclick = click; }, delay);
	}
	else if(tagname == "input")
	{
		var click = element.onclick;
		
		setTimeout(function() { element.disabled = true; element.onclick = null; }, 0);
		setTimeout(function() { element.disabled = false; element.onclick = click; }, delay);
	}
}
function safari()
{
	var ua = navigator.userAgent.toLowerCase();
    if(ua.indexOf("safari/") > -1 && ua.indexOf("version/") == -1 && ua.indexOf("chrome") == -1)
	{
		var labels = document.getElementsByTagName("label");
		for(var i = 0; i < labels.length; i++)
		{
			labels[i].onclick = function() { var input = document.getElementById(this.getAttribute("for"));  if(input) input.click(); }
		}
    }
}

function creatediv(text)
{  
	text.style.position = "relative";	
	var w = text.offsetWidth;
	var h = text.offsetHeight;
	var l = text.offsetLeft;
	var t = text.offsetTop;
		
	var div = document.createElement("div");
	div.style.position = "absolute";
	div.style.left = l + "px";
	div.style.top = t + "px";	
	div.style.width = w + "px";
	div.style.height = h + "px";
	div.style.zIndex = 100;	
	div.id = "___" + text.id;
	
	div.onclick = function(){toggleother(this.id.replace("___",""));}
	//div.onclick = function() { toggleother(this.id.replace("___other","linput")); }
	var other = getother(text);
	
	if(other)
	{		
		switch(other.type)
		{
			case "radio":
			case "checkbox":
				if(other.checked)
				{					
					text.disabled = false;				
				}
				break;
			case "select-one":
				if(other.selectedIndex == other.options.length - 1)
				{
					text.disabled = false;
				}
				break;
		}
	}
	document.body.appendChild(div);
}
function getother(text)
{
	
	var el = null;
	
	var q = getq(text);
	var inputs = q.getElementsByTagName("input");
	
	for(var i = 0; i < inputs.length; i++)
	{	
		/**  这部分jifei给注释了，如果在选择题出现其他问题时，把她放开，把下面的下面正在运行的代码删掉
		switch(inputs[i].type)
		{
			case "radio":
			case "checkbox":
				inputs[i].other = text.id;
				if(inputs[i].className.indexOf("other") > -1)
				{
					el = inputs[i];
				}
				break;
		}
		*/
		switch(inputs[i].type){
			case "radio":
			case "checkbox":
				el=inputs[i];
				el.other=text.id;
				break;
		}
		
	}
    if(el)el.isOther=true;    
	inputs = q.getElementsByTagName("select");
	if(inputs.length > 0)
	{
		
		inputs[0].other = text.id;
		inputs[0].onchange = function() { selectother(this); }
		inputs[0].onkeyup = function() { selectother(this); }
		el = inputs[0];
	}

	return el;
}
function selectother(sel)
{
	var other = document.getElementById(sel.other);
	if(sel.selectedIndex == sel.options.length - 1)
	{
		other.disabled = false;
	}
	else
	{
		other.disabled = true;
	}
}
function toggleother(id)
{
	var input = document.getElementById(id);
	var other = getother(input);
	if(other)
	{
		switch(other.type)
		{
			case "radio":
			case "checkbox":
				if(!other.checked)
					other.click();
					other.parentNode.className = other.parentNode.className.replace(/\s(selected)/gi,"")
				break;
			case "select-one":
				other.selectedIndex = other.options.length - 1;
				break;
		}
	}

	input.disabled = false;
	
	input.focus();
}
function mkLblOn(label)
{
label.style.textDecoration = 'underline';
label.style.backgroundColor = '#C3D2E8';
label.style.cursor = 'pointer';
}
function mkLblOff(label)
{
label.style.textDecoration = 'none';
label.style.backgroundColor = '';
label.style.cursor = '';
}
function mkChkToggle(_bChecked, _onDivID, _offDivID)
{
	if(_bChecked==true)
	{
		if (_offDivID != null)
			document.getElementById(_offDivID).style.display='none';
		if (_onDivID != null)
			document.getElementById(_onDivID).style.display='inline';
	}
	else
	{
		if (_onDivID != null)
			document.getElementById(_onDivID).style.display='none';
		if (_offDivID != null)
			document.getElementById(_offDivID).style.display='inline';
	}
}
function mkHovClick(_chkID, _onDivID, _offDivID)
{
	var _chkObj = document.getElementById(_chkID);
	if (_chkObj.checked)
		_chkObj.checked = false;
	else
		_chkObj.checked = true;
	mkChkToggle(_chkObj.checked, _onDivID, _offDivID);
}


var myimages=new Array();
function preloadimages(){	
	for (i=0;i<preloadimages.arguments.length;i++){
		myimages[i]=new Image();
		//if(myimages[i].complete){
		myimages[i].src=preloadimages.arguments[i];
		//}
	}
}
function preloadImg(img1,img2)
{	
	preloadimages("/servlet/CreateInputImage?c="+img1,"/servlet/CreateInputImage?c="+img2);
	
}
function submitButton(element, delay)
{
	var tagname = element.tagName.toLowerCase();
	
	delay = (delay) ? delay : 1500;
	
	if(tagname == "a")
	{
		var href = element.href;
		var click = element.onclick;
		
		setTimeout(function() { element.href = "javascript:void(0);"; element.onclick = null; }, 0);
		setTimeout(function() { element.href = href; element.onclick = click; }, delay);
	}
	else if(tagname == "input")
	{
		var click = element.onclick;
		
		setTimeout(function() { element.disabled = true; element.onclick = null; }, 0);
		setTimeout(function() { element.disabled = false; element.onclick = click; }, delay);
	}
}
//给showsv的多选矩阵单行被点击时
var oldBgDivId='';
function MatrixTRBacgr( bgDivId ){	
	for( var i = 0; i < document.getElementById(bgDivId).cells.length;i++ )
	{
		document.getElementById(bgDivId).cells[i].style.borderBottom ="dashed thin #555555";
	}	
	if( oldBgDivId!=''&& oldBgDivId != bgDivId )
	{
		for( var i = 0; i < document.getElementById(oldBgDivId).cells.length;i++ )
		{
			document.getElementById(oldBgDivId).cells[i].style.borderBottom ="";
			
		}
	}	
	oldBgDivId = bgDivId; 	
	
}
//  create by jifei 用于处理单多选后加文本框联动的功能实现
function option_text( inputObj,chkId )
{
	
	document.getElementById(chkId).checked = true;	
	var input_table = inputObj.parentNode.parentNode.parentNode.parentNode.parentNode;    //  取到该被选文本框所在的table	
	var inputs = input_table.getElementsByTagName("input");
	var label, img, inputID;
	for(var i = 0; i < inputs.length; i++)
	{
		switch(inputs[i].type)
		{
			case "checkbox":
				label = document.getElementById("l" + inputs[i].id);
				if(inputs[i].checked)
				{
					label.className = label.className.replace("cb_off", "cb_on");
					
				}else
				{
					label.className = "cb_off";
				}
				
				if(isIE) label.style.position = "relative";
				inputs[i].onclick = function() { cbtoggle(this); }
				inputs[i].onfocus = function() { focus(this); }
				inputs[i].onblur = function() { blur(this); }
				if(isIE)	// IE! ignores images in labels.
				{
					img = label.getElementsByTagName("img")[0];
					img.onclick = function() { this.parentNode.click(); }
				}
				break;
			case "radio":		
				label = document.getElementById("l" + inputs[i].id);
				if(inputs[i].checked)
				{
					label.className = label.className.replace("rb_off", "rb_on");					
					inputs[i].history = true;
				}
				else
				{
					label.className = "rb_off";				
					inputs[i].history = false;
				}
				if(isIE) label.style.position = "relative";
				inputs[i].onclick = function() { rbtoggle(this); }
				inputs[i].onfocus = function() { focus(this); }
				inputs[i].onblur = function() { blur(this); }
				if(isIE)	// IE! ignores images in labels.
				{					
					img = label.getElementsByTagName("img")[0];
					img.onclick = function() { this.parentNode.click(); }
				}
				break;
			
		}
	}
	
}

