
function numbers(object)
{
	if (document.all) var c = event.keyCode;
    else var c = event.charCode;
	    
    if((c < 58 && c > 47) || c == 45 || c == 46 || c == 44) return true;
    else return false;                   
}

function limit(object,maxlen,donr)
{
	if(object.value.length < maxlen)
	{
		if(donr)
		{
			if(numbers(object)) return true;
			else return false;
		}
		else return true;
	}
	else return false;
}
function showalert(msg)
{
	document.forms[0].timewarning.value = "FALSE";
	alert(msg);	
}

//Toggles RadioButton
var lastChecked;
function rb_toggle(rb)
{		
	if(lastChecked == rb.id)
	{		
		rb.checked = false;
		lastChecked = "";		
	}
	else
	{
		rb.checked = true;
		lastChecked = rb.id;
	}
}

var lastradio
function img_toggleradio(obj_id)
{		
	var	obj = document.getElementById(obj_id);
	var elements = document.getElementsByName(obj.name);
	
	for (var i = 0; i<elements.length; i++)
	{
		elements[i].src = "../gfx/nochkradio.gif";
	}	
	if(obj.src.indexOf("gfx/chkradio.gif") != -1)
	{
		obj.src = "../gfx/nochkradio.gif";
	}
	else
	{
		obj.src = "../gfx/chkradio.gif";			
	}
}
function img_togglecheckbox(obj_id)
{	
	var	obj = document.getElementById(obj_id);
	if(obj.src.indexOf("gfx/chkcheckbox.gif") != -1)
	{
		obj.src = "../gfx/nochkcheckbox.gif";
	}
	else
	{
		obj.src = "../gfx/chkcheckbox.gif";
	}
}


function ranked(hrowid,soi_id,answer)
{	
	var tmp = document.forms[0].ansSet.value;
	
	// Lägg till object i ansset	
	if(tmp.indexOf(hrowid)== -1)
	{		
		append('singelmatrix',soi_id,answer);
		return true;
	}
	// Ta bort object i ansset	
	else
	{		
		var x = -1, y = -1, tmp = "";
			
		tmp = document.forms[0].ansSet.value;
		x = tmp.indexOf(soi_id + "<|sm|>");
			
		if(x!=-1)
		{
			y = tmp.indexOf("{|}",x);
			if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
		}
		document.forms[0].ansSet.value = tmp;
				
		if (lastChecked != "")
			return false;
		else
			return true;
	}
}

function setpage(go,nxt)
{	
	document.forms[0].go.value = go;
	document.forms[0].nxt.value = nxt;	
}

function checkreq(word)
{
	var req = document.forms[0].ansReq.value;
	var tmp = document.forms[0].ansSet.value;
	var regular = document.forms[0].ansRegular.value;
	
	regular = regular.split("<|>");	
	// check regular expression
	if (regular[0] != "")
	{
		for (i=0; i<regular.length; i++)
		{
			if (regular[i] != "")
			{
				var alt = regular[i].split("<:>");
				var tmpid = alt[0].split("<,>")
				var tmpreg = document.getElementById(tmpid[0])
				var regex = eval("/^"+alt[1]+"$/");
				//if (!tmpreg.value.match(alt[1]))
				if (!regex.test(tmpreg.value))
				{				
					alert(tmpid[1] +'. Invalid format');
					return false;					
				}
			}
		}
	}
	req = req.split("<|>");
	
	if(req[0] != "")
	{
		for(i=0;i<req.length;i++)
		{		
			if(req[i] != "")
			{
				var alt = req[i].split("<:>");
				if(alt[1].indexOf("<#>") != -1)
				{
					var amt = alt[1].split("<#>");
					var extraalt = false;
					
					//Extra alternativ i multimatrix undantages från obligatoriskt
					if(amt[2] != null)
					{
						if((tmp.indexOf(alt[0] + "<|") != -1) && (tmp.indexOf(amt[2]+"{|}") != -1))
						{
							var tmp_ans = tmp.split(alt[0] + "<|");
							for(k = 0; k<tmp_ans.length; k++)
							{
								var mm_ans = tmp_ans[k].substring(0, tmp_ans[k].indexOf("{|}"))
								if((mm_ans.substring(0, 3) == "mm|")
									&& (mm_ans.indexOf(amt[2]) != -1))
									extraalt = true;
							}
						}
					}
					
					if(!extraalt)
					{
						if(amt[1] != "0-0")
						{
							var minmax = amt[1].split("-");
							var x=0;
							
							x = tmp.split(alt[0]+"<|").length - 1;

							if(minmax[0] != 0)
							{
								if(minmax[0]>x)
								{
									alert(amt[0] +" Min "+ minmax[0]);
									return false;
								}
							}
							if(minmax[1] != 0)
							{
								if(minmax[1]<x)
								{
									alert(amt[0] +" Max "+ minmax[1]);
									return false;
								}
							}
						}
						else
						{
							if(tmp.indexOf(alt[0]+"<|") == -1)
							{
								alert(amt[0] +" "+ word);
								return false;
							}
						}
					}
				}				
				else
				{
					if((tmp.indexOf(alt[0]+"<|") == -1))
					{
						alert(alt[1] +" "+ word);
						return false;
					}
				}
			}
		}
	}
}

function fillup(viewResult)
{		
	var tmp="",obj;
	tmp = document.forms[0].ansSet.value;
	obj = tmp.split("{|}");
	var lastAnsweredQuestion = '';
	
	if(obj[0]!="")
	{
		for(i=0;i<obj.length;i++)
		{
			if(obj[i]!= "")
			{
				//onechoice
				if(obj[i].indexOf("<|o|>") != -1)
				{
					var alt = obj[i].split("<|o|>");
															 
					if(eval("document.forms[0].FTA_"+alt[0]) && viewResult != true)
					{
						eval("document.forms[0].FTA_"+alt[0]).className = 'mediuminput';
						eval("document.forms[0].FTA_"+alt[0]).disabled = false;
					}
					
					if(alt[1].indexOf("|*|") != -1)
					{
						var fta = alt[1].split("|*|");
						alt[1] = fta[0];
						if(eval("document.forms[0].FTC_"+alt[0]))
							eval("document.forms[0].FTC_"+alt[0]).value = fta[1];
						if (eval("document.forms[0].FTA_"+alt[0]))
							eval("document.forms[0].FTA_"+alt[0]).value = fta[1];
						
					}					
					if(document.getElementById("NX_"+ alt[1]) != null)
					{
						if (document.getElementById("NX_"+ alt[1]).checked == null)
						{
							img_toggleradio("NX_"+ alt[1]);
							document.getElementById("NX_"+ alt[1]).src = "../gfx/chkradio.gif";
						}
						else
							document.getElementById("NX_"+ alt[1]).checked = true;
						lastAnsweredQuestion = obj[i];						
					}
					
				}
				//multichoice
				else if(obj[i].indexOf("<|m|>") != -1)
				{
					var alt = obj[i].split("<|m|>");
					if(alt[1].indexOf("|*|") != -1)
					{
						var fta = alt[1].split("|*|");
						alt[1] = fta[0];
						if(eval("document.forms[0].FTA_"+alt[1]))
						{
							if(viewResult != true)
							{
								eval("document.forms[0].FTA_"+alt[1]).className = 'mediuminput';
								eval("document.forms[0].FTA_"+alt[1]).disabled = false;
							}
							eval("document.forms[0].FTA_"+alt[1]).value = fta[1];
							lastAnsweredQuestion = obj[i];						
						}
						if(eval("document.forms[0].FTC_"+alt[1]))
						{
							if(viewResult != true)
							{
								eval("document.forms[0].FTC_"+alt[1]).className = 'mediuminput';
								eval("document.forms[0].FTC_"+alt[1]).disabled = false;
							}
							eval("document.forms[0].FTC_"+alt[1]).value = fta[1];
							lastAnsweredQuestion = obj[i];						
						}
					}
					else if(eval("document.forms[0].FTA_"+alt[1]))
					{
						eval("document.forms[0].FTA_"+alt[1]).className = 'mediuminput';
						eval("document.forms[0].FTA_"+alt[1]).disabled = false;
					}
					if(document.getElementById("NX_"+ alt[1]) != null)
					{ 
						if (document.getElementById("NX_"+ alt[1]).checked == null)					
							document.getElementById("NX_"+ alt[1]).src = "../gfx/chkcheckbox.gif";
						else
							document.getElementById("NX_"+ alt[1]).checked = true;
						lastAnsweredQuestion = obj[i];						
					}
				}
				//dropdown
				else if(obj[i].indexOf("<|d|>") != -1)
				{
					var alt = obj[i].split("<|d|>");
					if(document.getElementById("NX_"+ alt[0]) != null)
					{
						ref = document.getElementById("NX_"+ alt[0]);
						for(r=0; r< ref.length; r++)
						{
							if(ref.options[r].value.indexOf(alt[1]) != -1) ref.options[r].selected = true;
						}
						lastAnsweredQuestion = obj[i];						
					} 					
				}
				//singlematrix
				else if(obj[i].indexOf("<|sm|>") != -1)
				{										
					var alt = obj[i].split("<|sm|>");
					var subalt = alt[1].split("|");					
										
					if((alt[1].indexOf("|*|") != -1)&&(document.getElementById("NX_"+ alt[0]) != null) )
					{
						var fta = alt[1].split("|*|");
						alt[1] = fta[0];
						eval("document.forms[0].NX_"+alt[0]).value = fta[1];
						lastAnsweredQuestion = obj[i];						
					}				
					else if(document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]) != null)
					{
						if(document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]).checked == null)
							document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]).src = "../gfx/chkradio.gif";
						else
						document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]).checked = true;
						lastAnsweredQuestion = obj[i];						
					}
				}
				//multimatrix
				else if(obj[i].indexOf("<|mm|>") != -1)
				{
					var alt = obj[i].split("<|mm|>");
					var subalt = alt[1].split("|");
					if((alt[1].indexOf("|*|") != -1) && (document.getElementById("NX_"+ alt[0]) != null) )
					{
						var fta = alt[1].split("|*|");
						alt[1] = fta[0];
						eval("document.forms[0].NX_"+alt[0]).value = fta[1];
						lastAnsweredQuestion = obj[i];						
					}
					else if(document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]) != null)
					{
						if (document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]).checked == null)
							document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]).src = "../gfx/chkcheckbox.gif";
						else
							document.getElementById("NX_"+ alt[0]+"|"+ subalt[1]).checked = true;
						lastAnsweredQuestion = obj[i];						
					}


				}	
				//slider			
				else if(obj[i].indexOf("<|s|>") != -1)
				{
					var alt = obj[i].split("<|s|>");
					var subalt = alt[1].split("|");
					
					if(document.getElementById("NX_"+ alt[0]) != null)
					{
						document.getElementById("NX_"+ alt[0]).value = subalt[1];
						lastAnsweredQuestion = obj[i];						
					}
					if(document.all)
					{
						if(document.getElementById("KNOB_"+ alt[0]) != null) document.getElementById("KNOB_"+ alt[0]).style.pixelLeft = subalt[0];
					}
				}
				//drag and drop
				else if(obj[i].indexOf("<|dd|>") != -1)
				{
					var alt = obj[i].split("<|dd|>");
					var subalt = alt[1].split("|");
					if(document.all)
					{
						if(document.getElementById("nx_"+ alt[0]) != null)
						{
							document.getElementById("nx_"+ alt[0]).style.pixelTop = subalt[3];
							document.getElementById("nx_"+ alt[0]).style.pixelLeft = subalt[4];
							lastAnsweredQuestion = obj[i];						
						}
					}
				}				
				//hotspot
				else if(obj[i].indexOf("<|h|>") != -1)
				{
					var alt = obj[i].split("<|h|>");
					var subalt = alt[1].split("|");
					if(document.all)
					{
						if(document.getElementById("NX_"+ alt[0]) != null)
						{
							document.getElementById("NX_"+ alt[0]).style.pixelTop = subalt[0]-9;
							document.getElementById("NX_"+ alt[0]).style.pixelLeft = subalt[1]-9;
							document.getElementById("NX_"+ alt[0]).style.visibility = 'visible';
							lastAnsweredQuestion = obj[i];						
						}
					}
				}
				//freetext
				else if(obj[i].indexOf("<|f|>") != -1)
				{
					var alt = obj[i].split("<|f|>");
					if(document.getElementById("NX_"+ alt[0]) != null)
					{
						document.getElementById("NX_"+ alt[0]).value = alt[1];
						lastAnsweredQuestion = obj[i];						
					}
				}
				//combiner
				else if(obj[i].indexOf("<|co|>") != -1)
				{
		
					var alt = obj[i].split("<|co|>");
					var subalt = alt[1].split("|");															
					if (document.all)
					{
						if (document.getElementById("NX_" +alt[0]) != null)
						{							
							if (subalt.length == 4)
							{																
								document.getElementById("nx_" +alt[0]).style.pixelTop = subalt[3];
								document.getElementById("nx_" +alt[0]).style.pixelLeft = subalt[2].replace('_','-');							
							}
							// Används för att visa ett visst resultat
							else
							{
								
								var x = 0;
								var y = 0;								
								var element = document.getElementById("nx_" +subalt[1]);
								while(element.offsetParent)
								{
									x += element.offsetLeft;
									y += element.offsetTop; 
									element = element.offsetParent;
									
								}								
								var objElement = document.getElementById("nx_" +subalt[1]);												
								var movElement = document.getElementById("nx_" +alt[0]);								
								
								document.getElementById("nx_" +alt[0]).style.position = "absolute";
								document.getElementById("nx_" +alt[0]).style.posTop =  y +((objElement.height/2) -(movElement.height/2));
								document.getElementById("nx_" +alt[0]).style.posLeft = x +((objElement.width/2) -(movElement.width/2));
								
							}
							lastAnsweredQuestion = obj[i];						
						}
					}
				}
				//gap
				else if(obj[i].indexOf("<|g|>") != -1)
				{
					var alt = obj[i].split("<|g|>");
					if(document.all)
					{
						if(document.getElementById("NX_"+ alt[0]) != null)
						{
							document.getElementById("NX_"+ alt[0]).value = alt[1];
							lastAnsweredQuestion = obj[i];						
						}
					}
				}
			}
		}
	}
	//document.forms[0].hidLastAnsweredQuestion.value = lastAnsweredQuestion;
	if (typeof moveCorrect == "function") moveCorrect();
}


function fixfta(type,soi_id,soii_id,answer)
{
	var x = -1, y = -1, tmp = "";
	tmp = document.forms[0].ansSet.value;
	switch(type)
	{
		case "ofta":
		
			x = tmp.indexOf(soi_id + "<|o|>" + soii_id);
			if(x!=-1)
			{
				y = tmp.indexOf("{|}",x);
				if(y!=-1)
				{	
					tmp = tmp.replace(tmp.substring(x,y+3),soi_id + "<|o|>" + soii_id +"|*|"+ answer +"{|}");
					document.forms[0].ansSet.value = tmp;
				}
			}
			break;
			
		case "oftc":
		
			x = tmp.indexOf(soi_id + "<|o|>" + soii_id);
			if(x!=-1)
			{
				y = tmp.indexOf("{|}",x);
				if(y!=-1)
				{
					tmp = tmp.replace(tmp.substring(x,y+3), "");
				}
			}
			tmp = tmp + soi_id + "<|o|>" + soii_id +"|*|"+ answer +"{|}";
			document.forms[0].ansSet.value = tmp;
			break;
		
		case "mfta":
				
			x = tmp.indexOf(soi_id + "<|m|>" + soii_id);
			if(x!=-1)
			{
				y = tmp.indexOf("{|}",x);
				if(y!=-1)
				{
					tmp = tmp.replace(tmp.substring(x,y+3),soi_id + "<|m|>" + soii_id +"|*|"+ answer +"{|}");
					document.forms[0].ansSet.value = tmp;
				}
			}
			break;
			
		case "mftc":
		
			x = tmp.indexOf(soi_id + "<|m|>" + soii_id);
			if(x!=-1)
			{
				y = tmp.indexOf("{|}",x);
				if(y!=-1)
				{
					tmp = tmp.replace(tmp.substring(x,y+3), "");
				}
			}
			tmp = tmp + soi_id + "<|m|>" + soii_id +"|*|"+ answer +"{|}";
			document.forms[0].ansSet.value = tmp;
			break;
		
		//Meken added Freetext in MultiMatrix and SingleMatrix
		case "mmFreeText":
			var z=0, v=0;
			x = tmp.indexOf(soi_id + "<|mm|>" + answer);
			z = tmp.indexOf(soi_id + "<|mm|>" + soii_id);
			if(x!=-1 || z!=-1)
			{
				y = tmp.indexOf("{|}", x);
				v = tmp.indexOf("{|}", z);
				if(y!=-1)
				{
					tmp = tmp.replace(tmp.substring(x,y+3), soi_id + "<|mm|>" + soii_id +"|*|" + answer + "{|}");
					document.forms[0].ansSet.value = tmp;
				}
			}
			break;
			
		case "smFreeText":
			var z=0, v=0;
			x = tmp.indexOf(soi_id + "<|sm|>" + answer);
			z = tmp.indexOf(soi_id + "<|sm|>" + soii_id);
			if(x!=-1 || z!=-1)
			{
				y = tmp.indexOf("{|}", x);
				v = tmp.indexOf("{|}", z);
				if(y!=-1)
				{
					tmp = tmp.replace(tmp.substring(x,y+3), soi_id + "<|sm|>" + soii_id +"|*|" + answer + "{|}");
					document.forms[0].ansSet.value = tmp;
				}
			}
			break;
	}
}
// variabel som används vid pekskärm(__Dopostback)
// 1 = next
// 2 = save
var postback = false;

function dopostback()
{		
	var curr = document.forms[0].curr.value;	
	var amt = document.forms[0].amt.value;
	
	if (curr==amt)
		__doPostBack('lbSave',null);	
	else
		__doPostBack('lbNext',null);	
}

function uncheckMM(soii_id, soii_id2)
{
	var tmp = document.forms[0].ansSet.value;
	ar_tmp = tmp.split("{|}");
	for(i = 0; i<ar_tmp.length; i++)
	{
		if((ar_tmp[i].indexOf(soii_id + "<|mm|>") != -1) && (ar_tmp[i].indexOf(soii_id2) == -1))
		{
			var startindex = ar_tmp[i].indexOf("|", 45) + 1;
			var soii2 = ar_tmp[i].substring(startindex);
			var tmp_id = "NX_"+ soii_id +"|"+soii2;
			document.getElementById(tmp_id).checked = false;
			tmp = tmp.replace(ar_tmp[i] + "{|}", "");
		}
	}
	document.forms[0].ansSet.value = tmp;
}

function uncheckExtraAlt(soii_id, soii_id2, soi_id)
{
	var tmp_id = "NX_" + soii_id + "|" + soii_id2;
	if(document.getElementById(tmp_id) != null)
		document.getElementById(tmp_id).checked = false;
		
	var tmp = document.forms[0].ansSet.value;
		ar_tmp = tmp.split("{|}");
	var tmp_ans = soii_id + "<|mm|>" + soi_id + "|" + soii_id2 + "{|}";
	tmp = tmp.replace(tmp_ans , "");
	document.forms[0].ansSet.value = tmp;	
}

function append(type,soi_id,answer,fta)
{
	switch(type)
	{
		case "onechoice":
			
			var x = -1, y = -1, tmp = "", z = -1;
			
			tmp = document.forms[0].ansSet.value;			
			
			var tmpAr = new Array();
			tmpAr = tmp.split("{|}");			
			for (var i = 0; i< tmpAr.length ; i++)
			{
				if ( ((tmpAr[i].indexOf("|*|") == -1) && (tmpAr[i].length >10)) 
				 ||	 ((tmpAr[i].indexOf("|*|") != -1) && (eval("document.forms[0].FTA_"+soi_id))&&(tmpAr[i].length >10)))
				{
					x = tmpAr[i].indexOf(soi_id + "<|o|>");
					if ( x != -1)
					{
						tmp = tmp.replace(tmpAr[i] + "{|}","");
						break;
					}
				}
			}						
				/*
				y = tmpAr[z].indexOf("{|}",x);
				
				if((y!=-1) && (tmpAr[z].substring(x,y+3).indexOf("|*|") == -1))
					tmp = tmp.replace(tmpAr[z].substring(x,y+3),"");*/
			
			if(eval("document.forms[0].FTA_"+soi_id))
			{
				var css;
				css = eval("document.forms[0].FTA_"+soi_id).className;
				css = css.replace("_disabled","");
				eval("document.forms[0].FTA_"+soi_id).value = "";
				if(fta)
				{
					eval("document.forms[0].FTA_"+soi_id).className = css;
					eval("document.forms[0].FTA_"+soi_id).disabled = false;
				}
				else
				{
					//eval("document.forms[0].FTA_"+soi_id).className = 'mediuminput_disabled';
					if(eval("document.forms[0].FTA_"+soi_id).disabled == false)
					{
						eval("document.forms[0].FTA_"+soi_id).className = eval('css+"_disabled"');
						eval("document.forms[0].FTA_"+soi_id).disabled  = true;
					}
				}
			}
						
			
			if (eval("document.forms[0].NX_" + answer).checked == null)
			{
				if (eval("document.forms[0].NX_" + answer).src = "../gfx/chkradio.gif")
					tmp += soi_id + "<|o|>" + answer +"{|}";			
				document.forms[0].ansSet.value = tmp;
			}
			else
			{
				if (eval("document.forms[0].NX_" + answer).checked)
					tmp += soi_id + "<|o|>" + answer +"{|}";			
				document.forms[0].ansSet.value = tmp;
			}
			// postback
			
			if (postback)
			{
				// save
				setTimeout('dopostback()', 500);
			}			
			break;
		
		case "multichoice":
			
			var x = -1, y = -1, tmp = "";
			
			ref = eval("document.forms[0].NX_"+ answer);
			if(eval("document.forms[0].FTA_"+ answer))
			{
				var css;
				css = eval("document.forms[0].FTA_"+answer).className;
				css = css.replace("_disabled","");
				eval("document.forms[0].FTA_"+ answer).value = "";
				if(ref.checked)
				{
					eval("document.forms[0].FTA_"+ answer).className = css;
					eval("document.forms[0].FTA_"+ answer).disabled = false;
				}
				else
				{
					if(eval("document.forms[0].FTA_"+ answer).disabled == false)
					{
						eval("document.forms[0].FTA_"+ answer).className = eval('css+"_disabled"');
						eval("document.forms[0].FTA_"+ answer).disabled  = true;
					}
				}
			}
			
			if (ref.checked == null)
			{				
				if (ref.src.indexOf("gfx/chkcheckbox.gif") != -1)
				{
					document.forms[0].ansSet.value = document.forms[0].ansSet.value + soi_id + "<|m|>" + answer +"{|}";
				}
				else
				{
					tmp = document.forms[0].ansSet.value;
					x = tmp.indexOf(soi_id + "<|m|>"+ answer);
					if(x!=-1)
					{
						y = tmp.indexOf("{|}",x);
						if(y!=-1)
						{	
							tmp = tmp.replace(tmp.substring(x,y+3),"");
							document.forms[0].ansSet.value = tmp;
						}
					}
				}
			}
			else
			{	
				if(ref.checked)
				{
					document.forms[0].ansSet.value = document.forms[0].ansSet.value + soi_id + "<|m|>" + answer +"{|}";
				}
				else
				{
					tmp = document.forms[0].ansSet.value;
					x = tmp.indexOf(soi_id + "<|m|>"+ answer);
					if(x!=-1)
					{
						y = tmp.indexOf("{|}",x);
						if(y!=-1)
						{	
							tmp = tmp.replace(tmp.substring(x,y+3),"");
							document.forms[0].ansSet.value = tmp;
						}
					}
				}
			}
			break;
	
		case "dropdown":
			
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|d|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			
			if(answer!= "")
			{
				if(answer.indexOf("_|_") != -1)
				{
					ans = answer.split("_|_");
					tmp += soi_id + "<|d|>" + ans[0] +"{|}";
					eval(ans[1]);
				}
				else
				{
					tmp += soi_id + "<|d|>" + answer +"{|}";
				}
			}
			document.forms[0].ansSet.value = tmp;
			window.focus();
			break;
			
		case "combiner":
			
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|co|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			
			if(answer!= "")
			{
				if(answer.indexOf("_|_") != -1)
				{
					ans = answer.split("_|_");
					tmp += soi_id + "<|co|>" + ans[0] +"{|}";
					eval(ans[1]);
				}
				else
				{
					tmp += soi_id + "<|co|>" + answer +"{|}";
				}
			}
			document.forms[0].ansSet.value = tmp;
			window.focus();
			break;		
		
		case "singelmatrix":
			
			var x = -1, y = -1, tmp = "";			
			ans = answer.split('|');
			ref = document.getElementById("NX_"+soi_id+"|"+ans[1]);
			tmp = document.forms[0].ansSet.value;
			
			var tmpAr = new Array();
			tmpAr = tmp.split("{|}");
			for (var i = 0; i< tmpAr.length ; i++)
			{
				if ((tmpAr[i].indexOf("|*|") == -1) && (tmpAr[i].length >10))
				{
					if (tmpAr[i].indexOf(soi_id + "<|sm|>" + answer) != -1)
					{
						tmp = tmp.replace(tmpAr[i] + "{|}","");
						break;
					}
				}
			}
			
			if(!fta)
			{
				var ans_Array = new Array();
				ans_Array = answer.split("|");
								
				if (document.getElementById("NX_"+ soi_id + "|" + ans_Array[1]).checked == null)
				{
					if(document.getElementById("NX_"+ soi_id + "|" + ans_Array[1]).src.indexOf("gfx/chkradio.gif") != -1)
					{					
						x = tmp.indexOf(soi_id + "<|sm|>" + ans[0]);
						if(x != -1)
						{  
							y = tmp.indexOf("{|}",x);
							if(y!=-1)
								tmp = tmp.replace(tmp.substring(x,y+3),"");
						}
						tmp += soi_id + "<|sm|>" + answer +"{|}" ;
					}
				}
				else
				{
					if(document.getElementById("NX_"+ soi_id + "|" + ans_Array[1]).checked)
					{
						x = tmp.indexOf(soi_id + "<|sm|>" + ans[0]);
						if(x!=-1)
						{
							y = tmp.indexOf("{|}",x);
							if(y!=-1)
								tmp = tmp.replace(tmp.substring(x,y+3),"");
						}
						tmp += soi_id + "<|sm|>" + answer +"{|}" ;
					}
				}
			}
			else
			{
				x = tmp.indexOf(soi_id + "<|sm|>");
				if(x != -1)
				{
					y = tmp.indexOf("{|}", x);
					if(y!= -1)
						tmp = tmp.replace(tmp.substring(x,y+3), "");
				}
				if (answer!="")
					tmp += soi_id + "<|sm|>" + answer + "{|}" ;
			}
			document.forms[0].ansSet.value = tmp;
			

			break;
			
		case "multimatrix":
			
			var x = -1, y = -1, tmp = "";

			ans = answer.split('|');
			ref = document.getElementById("NX_"+soi_id+"|"+ans[1]);
			tmp = document.forms[0].ansSet.value;

			if (!fta)
				if (ref.checked == null)
				{
					if (ref.src.indexOf("gfx/nochkcheckbox.gif") != -1)
					{
						x = tmp.indexOf(soi_id + "<|mm|>" + answer);
						if(x!=-1){
							y = tmp.indexOf("{|}",x);
							if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
						}
					}
					else
					{
						tmp += soi_id + "<|mm|>" + answer +"{|}" ;
					}
					
				}
				else
				{
					if(!ref.checked)
					{
						x = tmp.indexOf(soi_id + "<|mm|>" + answer);
						if(x!=-1){
							y = tmp.indexOf("{|}",x);
							if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
						}
					}
					else
					{
						tmp += soi_id + "<|mm|>" + answer +"{|}" ;
					}
				}
			else
				{
					x = tmp.indexOf(soi_id + "<|mm|>");
					if(x!=-1)
					{
						y = tmp.indexOf("{|}",x);
						if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
					}
					if (answer!="")
						tmp += soi_id + "<|mm|>" + answer +"{|}" ;
				}

			document.forms[0].ansSet.value = tmp;
			
			break;
			
		case "dragdrop":
			
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|dd|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			
			if(answer!= "")
			{
				tmp += soi_id + "<|dd|>" + answer +"{|}";
			}
			document.forms[0].ansSet.value = tmp;
			
			break;
			
		case "freetext":
			
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|f|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			
			if(answer!= "")
			{
				tmp += soi_id + "<|f|>" + answer +"{|}";
			}
			document.forms[0].ansSet.value = tmp;
			
			break;
			
		case "gap":
			
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|g|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			
			if(answer!= "")
			{
				tmp += soi_id + "<|g|>" + answer +"{|}";
			}
			document.forms[0].ansSet.value = tmp;
			
			break;
			
		case "slider":
		
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|s|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			if(answer!= "")
			{
				tmp += soi_id + "<|s|>" + answer +"{|}";
			}
			document.forms[0].ansSet.value = tmp;
		
			break;
		
		case "hotspot":
		
			if (!document.all) return;
			var x = -1, y = -1, tmp = "";
			
			tmp = document.forms[0].ansSet.value;
			x = tmp.indexOf(soi_id + "<|h|>");
			
			if(x!=-1){
				y = tmp.indexOf("{|}",x);
				if(y!=-1) tmp = tmp.replace(tmp.substring(x,y+3),"");
			}
			
			ref = document.getElementById("NX_"+soi_id);
			tdref = document.getElementById("td_"+soi_id);
			answer = (window.event.clientY-getRealTop(tdref)+document.body.scrollTop) +"|"+ (window.event.clientX-getRealLeft(tdref)+document.body.scrollLeft);
			if(answer!= "")
			{
				tmp += soi_id + "<|h|>" + answer +"{|}";
				ref.style.visibility = "visible";
				ref.style.pixelTop = window.event.clientY-getRealTop(tdref)+document.body.scrollTop-9;
				ref.style.pixelLeft = window.event.clientX-getRealLeft(tdref)+document.body.scrollLeft-9;
			}
			document.forms[0].ansSet.value = tmp;
		
			break;
	}	
}

function getRealLeft(el) {
	xPos = el.offsetLeft;
	tempEl = el.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
return xPos;
}

function getRealTop(el) {
	yPos = el.offsetTop;
	tempEl = el.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
return yPos;
}

document.write("<style>")
document.write(".drag {position: relative; cursor: pointer}");
document.write(".moveme	{position: relative; cursor: move;}");
document.write(".movemeobj {position: absolute; cursor: move;}");
document.write(".handleobj {position: absolute; cursor: se-resize;}");
document.write("</style>");

//Slider code !

var dragMe=false,kObj,xPos,yPos,oldX,oldY,direction,o_id,o_width,o_heigh,p_id,p_width,p_height,
				o_start,o_stop,o_step,temp1,temp2, aSlider;				
var e;
function moveSlider(force)
{			
	if (e.button==1 && dragMe || force)
	{	
		
			
		// Firefox working
		oldX = parseInt(kObj.style.left.replace("px",""));		
		
		kObj.style.left = temp2 + e.clientX - xPos;
		if (parseInt(kObj.style.left.replace("px","")) > parseInt(oldX)) direction="lf"; else direction="rg";
		if (parseInt(kObj.style.left.replace("px","")) < -150 && direction=="rg") {kObj.style.left=-150; direction="lf";}
		if (parseInt(kObj.style.left.replace("px","")) > 150 && direction=="lf") {kObj.style.left=150; direction="rg";}
								
		// Firefox working
		if(o_start<o_stop)
		{
			var scale = 300/(o_stop-o_start);
			var curr = (parseInt(kObj.style.left.replace("px",""))+150);
			curr = o_start + Math.round(curr/scale);
			var i = o_start;
		}
		else
		{
			var scale = 300/(o_start-o_stop);
			var curr = (parseInt(kObj.style.left.replace("px",""))-150);
			curr = o_stop - Math.round(curr/scale);
			var i = o_stop;
		}
		
		
		while(true)
		{
			i+=o_step;
			if(i>curr) break;	
		}
		if(0-(i-o_step-curr) < i-curr) i -= o_step;
		eval("document.forms[0].NX_"+ o_id).value = i;
				
		// Kontrollera om vi ska visa bilder till slidern		
		if (aSlider != null)
		{					
			if (aSlider.length > 0)
			{
				var index;
				index = i-o_start;
				
				var tmp = aSlider[(i-o_start)/o_step];
				// pipa lite
				var split = tmp.split(',');
				
				// Sätt src, bredd och höjd till bilden
				eval("document.forms[0].img_" +o_id ).src = split[0]; 			
				eval("document.forms[0].img_" +o_id).width = split[1];
				eval("document.forms[0].img_"+ o_id).height = split[2];						
			}		
		}
		
		
		return false;
	}
}

function stopSlider()
{
	//alert('stop');
	dragMe=false;
	document.onmousemove = null;
	moveSlider(true);
	// firefox
	append("slider",o_id, parseInt(kObj.style.left.replace("px","")) +"|"+ eval("document.forms[0].NX_"+ o_id).value);
}

function dragSliderImage(e,id,start,stop,step, arImage)
{
	this.e = e;
	if (!document.all) return;
	o_id = id; o_start = start; o_stop = stop, o_step = step;
	dragMe = true;
	
	if (e.target)
		kObj = e.target;
	else
		kObj = e.srcElement;
		
	temp2 = parseInt(kObj.style.left.replace("px",""));
	xPos = e.clientX;			
	aSlider = arImage;
	document.onmousemove = moveSlider;
}
function dragSlider(e,id,start,stop,step)
{			
	this.e = e;
	//if (!document.all) return;
	o_id = id; o_start = start; o_stop = stop, o_step = step;
	dragMe = true;		
	
	// firefox
	if (e.target)
		kObj = e.target;
	// explorer
	else
		kObj = e.srcElement;	
		
	//temp2 = kObj.style.pixelLeft;
	// Firefox
	temp2 = parseInt(kObj.style.left.replace("px",""));
	xPos = e.clientX;
	
	document.onmousemove = moveSlider;//(true, e);
}

//drgdrop items
function dragDragDropItem(id,pid,pwidth,pheight,owidth,oheight)
{
	if(!document.all) return;
	o_id = id;
	o_width = owidth;
	o_height = oheight;
	p_id = pid;
	p_width = pwidth;
	p_height = pheight;
	dragMe = true;
	kObj = event.srcElement;
	temp1 = kObj.style.pixelTop;
	temp2 = kObj.style.pixelLeft
	yPos = event.clientY;
	xPos = event.clientX;
		
	document.onmousemove = moveDragDropItem;
}
// Nemo testar lite dra och släpp för Combinern
var cId, cWidht, cHeight, allPId, allPwidth, allPheigth, cObj, cXpos, cYPos, cPixelTop, cPixelLeft, surveyobjectid;
			
function moveChild(cId, cWidth, cHeight, allPId, allPwidth, allPheigth, surveyobjectid)
{	
if(!document.all) return;
	// set dragme to true
	dragMe = true;

	// set som parameters
	this.cId = cId;
	this.cWidht = cWidth;
	this.cHeight = cHeight;
	this.allPId = allPId;
	this.allPwidth = allPwidth;
	this.allPheigth = allPheigth;
	this.surveyobjectid = surveyobjectid;	
	
	// set drag obj
	cObj = event.srcElement;
	
	// y and x for the child
	cXpos = event.clientX;
	cYpos = event.clientY;
		
	cPixelTop = cObj.style.pixelTop;
	cPixelLeft = cObj.style.pixelLeft;	
	// when the mouse is moving		
	document.onmousemove = moveChildItem;		
}
function moveChildItem()
{	
	// if left button preesed and dragMe
	if (event.button == 1 && dragMe)
	{
		cObj.style.pixelLeft = cPixelLeft +event.clientX - cXpos;
		cObj.style.pixelTop = cPixelTop + event.clientY - cYpos;
		
		return false;
	}
}
function moveChildPos(cId)
{
	var tmp = document.forms[0].ansSet.value;	
	if (tmp.indexOf(cId) != -1)
	{				
		var possition = tmp.split(",");
		var x = possition[1];
		var y = possition[2];						
		
		var obj = document.getElementById("nx_" +cId);
		event.srcElement.style.pixelTop = y;
		event.srcElement.style.pixelLeft = x;				
	}
}
function stopChild()
{
	dragMe = false;
	document.onmousemove = null;
	var allobjsID = allPId.split(",");
	var allobjWidth = allPwidth.split(",");
	var allobjHeigth = allPheigth.split(",");
	var findPicture = false;
	
	for(i=0; i<allobjsID.length-1; i++)
	{		
		ref = document.getElementById("nx_"+cId);	
		mainref = document.getElementById("nx_" +allobjsID[i]);
		var mainWidth = allobjWidth[i];
		var mainHeigth = allobjHeigth[i];
		
		var cx = parseFloat(getRealLeft(ref)) +(parseFloat(cWidht)/2);
		var cy = parseFloat(getRealTop(ref)) +(parseFloat(cHeight)/2);				
		var px1 = parseFloat(getRealLeft(mainref));
		var px2 = parseFloat(getRealLeft(mainref)) + parseFloat(mainWidth);
		var py1 = parseFloat(getRealTop(mainref));
		var py2 = parseFloat(getRealTop(mainref)) + parseFloat(mainHeigth);
		
		// find a picture to stand with		
		if ((cx > px1 && cx < px2) && (cy > py1 && cy < py2) )
		{
			append("combiner", cId, surveyobjectid +"|" +allobjsID[i] +"|" +ref.style.pixelLeft +"|" +ref.style.pixelTop);
			findPicture = true;
			break;			
		}	
		
	}	
	// send picture back	
	if (!findPicture)
	{	
		ref.style.pixelTop = 0;
		ref.style.pixelLeft = 0;
		append("combiner", cId, "");	
	}			
}
function moveDragDropItem()
{
	if (event.button==1 && dragMe)
	{
		oldX = kObj.style.pixelLeft;
		kObj.style.pixelLeft = temp2 + event.clientX - xPos;
		kObj.style.pixelTop = temp1 + event.clientY - yPos;		
				
		return false;
	}
}
function stopDragDropItem()
{
	dragMe=false;
	document.onmousemove = null;
	ref = document.getElementById("nx_"+o_id);
	mainref = document.getElementById("dd_"+p_id);	
	if(getRealTop(ref) - getRealTop(mainref)< 0 || getRealTop(ref) - getRealTop(mainref) > (p_height-o_height) || getRealLeft(ref) - getRealLeft(mainref)< 0 || getRealLeft(ref) - getRealLeft(mainref) > (p_width-o_width))
	{
		ref.style.pixelTop = 0;
		ref.style.pixelLeft = 0;
		append("dragdrop", o_id,"");
	}
	else
	{
		append("dragdrop", o_id, p_id +"|"+ (getRealTop(ref) - getRealTop(mainref)) +"|"+ (getRealLeft(ref) - getRealLeft(mainref)) +"|"+ ref.style.pixelTop +"|"+ ref.style.pixelLeft);
	}
}










