/*
if (parent.frames.location.href.indexOf("i-survey.dk") != -1) {
	//parent.frames.location.href = document.location;
	alert("The Survey system is running inside a frame from another website !\n\nThat will not work as it should, so pleasebreak out of current frameset !");
}
*/

// What browser are the user running ?
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_opera = (agt.indexOf("opera") != -1);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) || (agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_xp = (agt.indexOf('nt 5.1') != -1);
var is_mac = (agt.indexOf("mac") != -1);
var is_safari = (agt.indexOf("safari") != -1);
var is_firefox = (agt.indexOf("firefox") != -1);
var is_ie6 = (agt.indexOf("msie 6") != -1);
var is_ie7 = (agt.indexOf("msie 6") != -1);
var is_ie8 = (agt.indexOf("msie 8") != -1);
var is_ie = (agt.indexOf("msie") != -1);

if (is_firefox) {
	is_nav = false; is_nav2 = false; is_nav3 = false; is_nav4 = false; is_nav4up = false;
}

if (document.location.href.indexOf("i-survey.dk")) {
	window.onerror = null;
}

function layerWrite(id, text, layerWidth, layerHeight, pic) {
	
	if (is_ie) {
		var scrollWidth = top.document.body.clientWidth / 2;
		var scrollHeight = top.document.body.clientHeight / 2;
	} else {
		var scrollWidth = parent.pageXOffset + (window.innerWidth / 2);
		var scrollHeight = parent.pageYOffset + (window.innerHeight / 2);
	}
	
	var layerTop = (scrollHeight - (layerHeight / 2));
	var layerLeft = (scrollWidth - (layerWidth / 2));
	
	document.getElementById(id).style.left = layerLeft +'px';
	document.getElementById(id).style.top = layerTop +'px';
	document.getElementById(id).style.width = layerWidth +'px';
	document.getElementById(id).style.height = layerHeight +'px';
	document.getElementById(id).innerHTML = text;
	document.getElementById(id).style.visibility = 'visible';

	if (pic) {
		setTimeout("document.getElementById('innerPic').src = 'pics/"+ pic +"';", 100);
	}
	
	return true;
	
}

var dragApproved = false;

function dragDrop(e) {

	if (is_ie) {
		var e = e || window.event;
		mouseXX = e.pageX || e.clientX;
		mouseYY = e.pageY || e.clientY;
	} else {
		mouseXX = e.clientX;
		mouseYY = e.clientY;		
	}
	
	if (dragApproved == true) {
		document.getElementById('wait').style.left = currentX + mouseXX - mouseX + 'px';
		document.getElementById('wait').style.top = currentY + mouseYY - mouseY + 'px';
		return false;
	}
 
}
 
function initializeDrag(e) {
	
	var e = e || window.event;
	mouseX = e.pageX || e.clientX;
	mouseY = e.pageY || e.clientY;

	currentX = parseInt(document.getElementById('wait').style.left) + document.body.scrollLeft + document.documentElement.scrollLeft;
	currentY = parseInt(document.getElementById('wait').style.top) + document.body.scrollTop + document.documentElement.scrollTop;
	dragApproved = true;
	document.getElementById('tbar').style.cursor = 'move';
	document.getElementById('tbar').style.background = '#DDDDDD';
	document.onmousemove = dragDrop;
	
}

function stopDrag() {
	try {
		document.onmousemove = null;
		document.onmouseup = null;
		document.getElementById('tbar').style.cursor = 'pointer';
		document.getElementById('tbar').style.background = '#C0C0C0';
	}
	catch(nothing) {}
}

function detectKeyCode(e) {
	var keyCode = (window.event) ? window.event.keyCode : e.which;
	if (keyCode == 27) { stopDrag(); }
}

function scrollInfoLayers() {
	try {
		if (document.getElementById('wait').style.position == 'absolute' && document.getElementById('wait').style.visibility == 'visible') {
			centerLayer('wait');
		}
	}
	catch(nothing) {}
}

document.onkeyup = detectKeyCode;
if (is_ie) {
	window.onscroll = scrollInfoLayers;
}

function infoLayer(id, title, picture, text, layerWidth, layerHeight, typ, padding, okBtn) {
	var textout;
	if (is_ie) document.getElementById(id).style.position = 'absolute';
	document.getElementById(id).style.visibility = 'visible';
	textout = "<html><body><table border=0 width=100% cellpadding=0 cellspacing=0><tr><td><table border=0 width=100% cellpadding=1 cellspacing=0 bgcolor=#000000><tr><td><table width=100% border=0 cellpadding=2 cellspacing=0 bgcolor=#eeeeee>";
	if (typ == 1) {
		textout += "<tr id=tbar><td valign=top style=\'padding: "+ padding +"px; font-family: tahoma, sans-serif; font-size: 14px; font-weight: bold;\'><img id=innerPic src=pics/spacer.gif width=32 height=33 hspace=5 align=absmiddle>" + text +"<span id=innerWait></span></td></tr>";
	} else if (typ == 2) {
		if (picture != '') {
			var cols = "colspan=2";
		} else {
			var cols = "";
		}
		textout += "<tr bgcolor=c0c0c0 id=tbar><td width="+ (layerWidth - 15) +" style=\"border-bottom: 1px solid #000000;\" onmouseover=\"this.style.cursor='move';\" height=10 "+ cols +" onmousedown=\"initializeDrag(event);\" onmouseup=\"stopDrag(); document.getElementById('tbar').style.background='#C0C0C0';\"><span style=\"font-family: tahoma; font-weight: bold; color: #000000;\">"+ (title ? title : "&nbsp;") +"</span></td><td width=15 align=right style=\"text-align:right; border-bottom: 1px solid #000000;\"><img src=pics/popup_close_onclick.gif border=1 width=11 height=11 onmouseover=\"this.style.cursor=\'pointer\';\" onclick=\"hideLayer(\'"+ id +"\');\"></td></tr><tr>";
		if (picture != '') {
			textout += "<td width=40 valign=top align=right><img id=innerPic src=pics/spacer.gif width=32 height=33 vspace=10></td>";
			var cols = "";
		} else {
			var cols = " colspan=2";
		}
		textout += "<td valign=top"+ cols +" style=\'padding: "+ padding +"px; font-family: tahoma; font-size: 11px; font-weight: bold;\'>"+ text +"</td></tr>";
	}
	if (!okBtn) okBtn = 0;
	if (typ == 2 && okBtn != '0') {
		textout += "<tr><td height=30 colspan=3 valign=bottom align=right><img onmouseover=\"this.style.cursor='pointer';\" onclick=\"hideLayer(\'"+ id +"\');\" src=\"pics/"+ okBtn +"\"></td></tr>";
	}

	textout += "</table></td></tr></table></td><td width=4 onmousedown=\"initializeDrag(event);\" onmouseup=\"stopDrag(); document.getElementById('tbar').style.background='#C0C0C0';\" background=pics/shadowgrid_r.gif><img src=pics/spacer.gif width=4 height=1></td></tr><tr><td onmousedown=\"initializeDrag(event);\" onmouseup=\"stopDrag(); document.getElementById('tbar').style.background='#C0C0C0';\" height=4 colspan=2 background=pics/shadowgrid_b.gif></td></tr></table></body></html>";

	layerWrite(id, textout, layerWidth, layerHeight, picture);
	if (typ == 1) {
		setInterval('waitingDots(4);', 500);
	}
	centerLayer(id);
}

function waitingDots(num, pic) {
	if (document.getElementById('innerWait').innerHTML.length == num) {
		document.getElementById('innerWait').innerHTML = "";
	}
	document.getElementById('innerWait').innerHTML += ".";
	return true;
}

function hideLayer(id) {
	if (!id) return;
	if (document.getElementById(id).style.visibility == "visible") {
		document.getElementById(id).style.visibility = "hidden";
		nd();
	}
}

function centerLayer(id) {
	
	layerHeight = parseInt(document.getElementById(id).style.height);
	viewFrameHeight = window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
	scrollTop = window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||  document.body.scrollTop;
	position = (viewFrameHeight / 2) - (layerHeight / 2);
	// Fix for IE where position is absolute, and then the layer position needs to have the scrollamount added...
	if (document.getElementById(id).style.position == 'absolute') position += scrollTop;
	document.getElementById(id).style.top = position + 'px';
	
}

function NewWindow(mypage, myname, w, h, scroll) {

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	winprops = 'height='+ (screen.height-20) +',width='+w+',top='+wint+',left='+winl+',status=0,menubar=0,scrollbars='+scroll+',resizable=yes'
	//win = window.showModelessDialog(mypage, myname, winprops)
	win = window.open(mypage, myname, winprops)

	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function makeArray() {

old = new Array();
oldText = new Array();
var tags = document.form.tags;

	for (i=0; i < tags.length; i++) {
		old[i] = tags.options[i].value;
		oldText[i] = tags.options[i].text;
	}

}

function searchTextarea() {

var tags = document.form.tags;
var text = document.form.fileEdit;

	for (i=0; i < tags.length; i++) {
		if (text.value.indexOf(old[i]) >= 0) { //is it in the textarea ?
			//removing option.....
			tags.options[i] = new Option('',tags.options[i].value);

		} else {
			//setting the option back.....
			tags.options[i] = new Option(oldText[i],old[i]);
		}
	}
}

function resizeThisWindow() {
	resizeThisWindow2();
	/*
	var height;
	var width;
	if (is_nav) { // Netscape !
		width = window.innerWidth;
		height = window.innerHeight;
		if (width > screen.availWidth) {
			width = screen.availWidth-50;
		}
		if (height > screen.availHeight) {
			height = screen.availHeight-50;
		}
	} else { // IE and others !
		var winHeight = document.body.scrollHeight;
		var winWidth = document.body.scrollWidth;
		if ((winHeight) < (screen.height-50)) {
			height = winHeight+80;
		} else {
			height = screen.height-100;
		}
		if ((winWidth) < (screen.width-50)) {
			width = winWidth+60-27;
		} else {
			width = screen.width-100-27;
		}
	}
	if (!is_nav) {
		top.window.resizeTo(width, height);
		var placeY;
		var placeX;
		placeY = (screen.height - height) / 2;
		placeX = (screen.width - width) / 2;
		top.window.moveTo(placeX, placeY);
	}
	*/
}

function resizeThisWindow2() {
	//if (!is_nav && !is_mac) top.window.resizeTo('100', document.body.scrollHeight);
	var pheight;
	var pwidth;
	if (is_nav6up) {
		var winHeight = window.outerHeight;
		var winWidth = window.outerWidth;
	} else {
		var winHeight = document.body.scrollHeight;
		var winWidth = document.body.scrollWidth;
	}
	pheight = winHeight+32;
	if (is_xp) pheight = pheight+35;
	pwidth = winWidth+29;
	if (!is_xp)	pwidth = pwidth-1;
	//if (!is_nav && !is_mac) {
		if (pheight > screen.availHeight) {
			pheight = screen.availHeight-50;
		}
		if (pwidth > screen.availWidth) {
			pwidth = screen.availWidth-50;
		}
			//document.write(pwidth);
			//alert(pheight);

		top.window.resizeTo(pwidth, pheight);
		var placeY;
		var placeX;
		placeY = (screen.height - pheight) / 2;
		placeX = (screen.width - pwidth) / 2;
		top.window.moveTo(placeX, placeY);
	//}
}

function removecheck() {

	if (document.form.endTest != null) {
		document.form.endTest.checked = false;
	}

}

function color () {

BlinkObj = document.getElementById("blink")

acolor = BlinkObj.style.color;
	if (acolor == '#eeeeee') { BlinkObj.style.color = '#000000'; acolor = ''; };
	if (acolor == '#000000') { BlinkObj.style.color = '#eeeeee'; acolor = ''; };
}
//setInterval("color()",400);

var expDays = 1;

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
   }
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


function reCalculate(fromField, toField, fromYear) {

	year = fromYear.value;
	if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0)) { // If the year is a leap year, make 29 days in febuary, otherwise 28
		var feb = 29;
	} else {
		var feb = 28;
	}
	var months = Array("", "31", feb, "31", "30", "31", "30", "31", "31", "30", "31", "30", "31");
	var oldValue = toField.options.selectedIndex;
	if (fromField.value < 10) {
		var monthSelected = fromField.value.substring(1,2);
	} else {
		var monthSelected = fromField.value;
	}
	remakeOption(toField, months[monthSelected], oldValue);
}

function remakeOption(toField, topNo, oldValue) {

	var i=0;
	while (i < toField.options.length) {
		toField.options[i]=null;
	}
	var k=1;
	while (k <= topNo) {
  	var no = new Option();
  	if (k < 10) {
  		no.value = '0'+k;
  	} else {
  		no.value = k;
  	}
  	no.text = k;
  	toField.options[toField.options.length] = no;
  	k++;
  }
  if (oldValue >= toField.options.length) {
  	toField.selectedIndex = toField.options.length-1;
  } else {
  	toField.selectedIndex = oldValue;
  }
}

// New funtions for making dropdown layer/menus

var $cookie_expire = "Monday, 02-Sep-2002 00:00:00 GMT";
var $cookie_domain = ".i-survey.dk";
function validateDateFields(minute, hour, day, month, year, error) {

	var makeDate = nowYear +""+ nowMonth +""+ nowDay +""+ nowHour +""+ nowMinute;
	var selectDate = year.value +""+ month.value +""+ day.value +""+ hour.value +""+ minute.value;
	//alert('dag fra db: '+ makeDate);
	//alert('dag fra form: '+ selectDate);
	if (makeDate > selectDate) {
		alert(error);
		return false;
	}
	return true;
}

function changeQuestionType(qType, stype) {

	if (qType == 5) {
		document.getElementById("matrice1").style.visibility='visible';
		document.getElementById("matrice1").style.display='';
		document.getElementById("matrice2").style.visibility='visible';
		document.getElementById("matrice2").style.display='';
		document.form.subQuestions.focus();
		//resizeThisWindow();
	} else {
		if (stype == 1) {
			document.getElementById("matrice1").style.visibility='hidden';
			document.getElementById("matrice1").style.display='none';
			document.getElementById("matrice2").style.visibility='hidden';
			document.getElementById("matrice2").style.display='none';
		}
	}
	if (qType == 3 || qType == 5 || qType == 6) { // Hide the row with noOfAnswers
		document.getElementById("rowNoOfAnswers").style.visibility='hidden';
		document.getElementById("rowNoOfAnswers").style.display='none';
	} else if (qType != "") {
		document.getElementById("rowNoOfAnswers").style.visibility='visible';
		document.getElementById("rowNoOfAnswers").style.display='';
		document.form.noOfAnswers.focus();
	}

}

// ###### START PLUGIN DETECTER ######## //

//These functions detects the following plugins:
//Flash
//Windows Media Player
//Java
//Shockwave
//RealPlayer
//QuickTime
//Acrobat Reader
//SVG Viewer

//Below line already declared on line 9
//var agt = navigator.userAgent.toLowerCase();
var ie  = (agt.indexOf("msie") != -1);
var ns  = (navigator.appName.indexOf("Netscape") != -1);
var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
var mac = (agt.indexOf("mac")!=-1);

/*
if (ie && win) {	pluginlist = detectIE("Adobe.SVGCtl","SVG Viewer") + detectIE("SWCtl.SWCtl.1","Shockwave Director") + detectIE("ShockwaveFlash.ShockwaveFlash.1","Shockwave Flash") + detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer") + detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") + detectIE("MediaPlayer.MediaPlayer.1","Windows Media Player") + detectIE("PDF.PdfCtrl.5","Acrobat Reader"); }
if (ns || !win) {
		var nse = "";
		for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
		pluginlist = detectNS("image/svg-xml","SVG Viewer") + detectNS("application/x-director","Shockwave Director") + detectNS("application/x-shockwave-flash","Shockwave Flash") + detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + detectNS("video/quicktime","QuickTime") + detectNS("application/x-mplayer2","Windows Media Player") + detectNS("application/pdf","Acrobat Reader");
}
*/

function detectIE(ClassID,name) { result = false; document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n'); if (result) return name+','; else return ''; }
function detectNS(ClassID,name) { n = ""; if (nse.indexOf(ClassID) != -1) if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; return n; }

//pluginlist += navigator.javaEnabled() ? "Java," : "";
//if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);

//SAMPLE USAGE- detect "Flash"
//if (pluginlist.indexOf("Flash")!=-1)
//document.write("You have flash installed")

// ###### END PLUGIN DETECTER ######## //

function addText(newCode, thefield) {
	if (thefield.createTextRange && thefield.caretPos) {
		var caretPos = thefield.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? newCode + ' ' : newCode;
	} else {
		thefield.value+=newCode +' ';
	}
	thefield.focus();
}

function storeCaret(textEl) {
	if (document.selection) {
		textEl.caretPos = document.selection.createRange().duplicate();
	} else if (textEl.selectionStart || textEl.selectionStart == '0') {   // Firefox support
     textEl.caretPos = textEl.selectionStart;
  }
}

function insertAtCaret(textIn, textEl) {
	try {
		if (document.selection && textEl.caretPos) {
			caretPos = textEl.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? textIn + ' ' : textIn;
			return true;
		} else if (textEl.selectionStart) {
     	textEl.focus();
			var start = textEl.selectionStart;
			textEl.value = textEl.value.substr(0, start) + textIn + textEl.value.substr(textEl.selectionEnd, textEl.value.length);
			return true;
		}
	}
	catch(e) {
		textEl.value += textIn;
	}
}


function changeJumpSelects(qNum) {
	topSelected = document.getElementById("jump"+ qNum +"0")
	subSelected = document.getElementById("jump"+ qNum +"1")
	var i=1;
	if (topSelected.selectedIndex != "") {
		while(subSelected){
			subSelected.selectedIndex = topSelected.selectedIndex;
			subSelected.disabled = true;
			i++;
			subSelected = document.getElementById("jump"+ qNum + i);
		}
	} else {
		while(subSelected){
			subSelected.selectedIndex = topSelected.selectedIndex;
			//if (!locked)
				subSelected.disabled = false;
			i++;
			subSelected = document.getElementById("jump"+ qNum + i);
		}
	}
}

function boldText(errorMsg) {
  if (document.selection && document.selection.createRange) {
    if (document.selection.createRange().text == '') {
      alert(errorMsg);
      return;
    }
    txt = document.selection.createRange().text;
    document.selection.createRange().text = '<b>' + txt + '</b>';
   } else {
    //alert("Nonworking...");
    return;
  }
}

function italicText(errorMsg) {
  if (document.selection && document.selection.createRange) {
    if (document.selection.createRange().text == '') {
      alert(errorMsg);
      return;
    }
    txt = document.selection.createRange().text;
    document.selection.createRange().text = '<i>' + txt + '</i>';
   } else {
    //alert("Nonworking...");
    return;
  }
}

function underlineText(errorMsg) {
  if (document.selection && document.selection.createRange) {
    if (document.selection.createRange().text == '') {
      alert(errorMsg);
      return;
    }
    txt = document.selection.createRange().text;
    document.selection.createRange().text = '<u>' + txt + '</u>';
   } else {
    //alert("Nonworking...");
    return;
  }
}

function trim(strText) {
    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	//var re = /^[^\s\n]+\@[a-zA-Z0-9][a-zA-Z0-9\-\.]*\.[a-zA-Z]{2,4}$/;
	return (!r1.test(str) && r2.test(str));
}

function copyClipboard(obj) {
	if (is_firefox) {
		alert('Funktionen er desværre ikke tilgængelig i FireFox!\n\nSorry, this function is not available in FireFox!');
		return;
	}
	obj = findObj(obj);
	if (obj) {
		window.clipboardData.setData('Text', obj.value);
	}
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function getRefToDivMod( divID, oDoc ) {
	if(!oDoc ) { oDoc = document; }
	if( document.layers ) {
	if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
	for( var x = 0, y;!y && x < oDoc.layers.length; x++ ) {
	y = getRefToDivNest(divID,oDoc.layers[x].document); }
	return y; } }
	if( document.getElementById ) { return oDoc.getElementById(divID); }
	if( document.all ) { return oDoc.all[divID]; }
	return document[divID];
}

function resizeWinTo( idOfDiv ) {
  var oH = getRefToDivMod( idOfDiv ); if( !oH ) { return false; }
  var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
  var oH = oH.clip ? oH.clip.height : oH.offsetHeight; if( !oH ) { return false; }
  var x = window; x.resizeTo( oW + 200, oH + 200 );
  var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
  if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }
  else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }
  else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }
  if( window.opera && !document.childNodes ) { myW += 16; }
  x.resizeTo( oW + ( ( oW + 200 ) - myW ), oH + ( (oH + 200 ) - myH ) );
}


/* function for code retrieval */


function dualLinebreak(dataIn) {
	var string = dataIn;
	string = string.replace(/\\\\n/g, '\\\\\\\\n');
	return string;
}


function singleLinebreak(dataIn) {
	var string = dataIn;
	string = string.replace(/\\\\\\\\n/g, '\\\\n');
	return string;
}

function urlEncode (dataIn) {
	string = dataIn.replace(/ /g, '%20');
	return escape(string);
}

function surveyPreview() {
	open(protocol +'://'+ siteUrl +"/isurvey.php?s="+ surveyHash +"&preview="+ previewHash +"&ref="+ urlEncode(document.location.href),'Isurvey','width=600,height=600,resizable=yes,scrollbars=yes');
}

function surveyDemo() {
	open(protocol +'://'+ siteUrl +"/isurvey.php?s="+ surveyHash +"&preview="+ previewHash +"&demo=yes&ref="+ urlEncode(document.location.href),'Isurvey','width=600,height=600,resizable=yes,scrollbars=yes');
}

function previewTag() {
	open(protocol +'://'+ siteUrl +"/admin/preview.php",'Isurvey','width=300,height=300,resizable=yes,scrollbars=yes');
}

function makeLink() {
	text3 = document.getElementById('text3');
	tmpClass = document.getElementById('tmpClass');
	tmpLook1 = document.getElementById('tmpLook1');
	tmpLook2 = document.getElementById('tmpLook2');
	var nameField = document.tmp.tmpName.value;
	nameField = nameField.replace(/ /g, '%20');
	nameField = nameField.replace(/\"/g, '\'');
	nameField = nameField.replace(/'/g, "´");
	nameField = nameField.replace(/&/g, "%26");
	text3.value = '<script type="text/javascript" src="'+ protocol +'://'+ siteUrl +'/makelink.php?s='+ surveyHash +'&name='+ nameField +'&class='+ tmpClass.value +'&ref=\"+ document.location.href +\""></script';
	if (!nameField) {
		text3.value = text3.value.replace('&name=', '');
	}
	if (!tmpClass.value) {
		text3.value = text3.value.replace('&class=', '');
	}
	if (tmpLook1.checked == true) {
		text3.value = text3.value.replace('&look=', '');
	} else if (tmpLook2.checked == true) {
		text3.value = text3.value.replace('&ref=', '&look=button&ref=');
	}
	text3.value += '>';
}


function makeConfirm() {
	txt = document.getElementById('txt');
	text1 = document.getElementById('text1');
	tr0 = document.getElementById('tr0');
	trc0 = document.getElementById('tr0');
	if (txt.value != '') var nameField = encode(txt.value); else var nameField = '';
	if (tr0.checked == true || trc0.checked == true) {
		text1.value = '<script type="text/javascript" src="'+ protocol +'://'+ siteUrl +'/start.php?s='+ surveyHash +'&confirm='+ nameField +'&ref=\" + document.location.href +\""></script';
	} else {
		text1.value = '<script type="text/javascript" src="'+ protocol +'://'+ siteUrl +'/exit.php?s='+ surveyHash +'&confirm='+ nameField +'&exit=true&ref=\" + document.location.href +\""></script';
		text1.value = dualLinebreak(text1.value);
	}
	if (!nameField) {
		text1.value = text1.value.replace('&confirm=', '');
	}
	text1.value += '>';
}


function encode(field) {
	if(field == ""){
		return "";
	} else {
	var text = field;
	enctext = transform(text);
	}
	if (enctext)
		return enctext;
	else
		return "";
}

function transform(s) {
	var hex = '';
	var i;
	for (i=0; i<s.length; i++) {
	hex += "%"+hexfromdec( s.charCodeAt(i) )

    }
	return hex
}

function hexfromdec(num) {
if (num > 65535) { return ("err!") }
	first = Math.round(num/4096 - .5);
	temp1 = num - first * 4096;
	second = Math.round(temp1/256 -.5);
	temp2 = temp1 - second * 256;
	third = Math.round(temp2/16 - .5);
	fourth = temp2 - third * 16;
	return (""+getletter(third)+getletter(fourth));
}

function getletter(num) {
	if (num < 10) {
		return num;
	}
	else {
	    if (num == 10) { return "A" }
	    if (num == 11) { return "B" }
	    if (num == 12) { return "C" }
	    if (num == 13) { return "D" }
	    if (num == 14) { return "E" }
	    if (num == 15) { return "F" }
	}
}