/*
 * jQuery Form
 * http://kustomwebdesigns.com.au
 *
 * Copyright (c) 2009 Blake Kus
 *
 * Date: 2009-03-18 13:58:32 +1000 (Wed, 18 Mar 2009)
 * Revision: 1
 */
var fadeinouttime = 500;
var formdata;
$(document).ready(function(){
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
	if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
		$.browser.firefox = true;
	}
	if($.browser.chrome){
        $.browser.safari = false;
    }
	$("button").livequery(function(){ 
		$.each($("button"), function(i, e) {
			$(e).addClass("sprite onhover");
			$.each($(e).children('div'), function(j, f) {
				$(f).addClass("sprite inline-block");
			});
		});
	});
	$(".group-checkbox").livequery('click', function(event) {
		alert($(this).attr('id'));
	});
	$("#button-cancel").livequery('click', function(event) {
		if(confirm('Are you sure you want to cancel?')){
			location.href = "";
		}
	});
	$(".cellHoverClick").livequery('click', function(event) {
		thisid = $(this).attr('id');
		if(thisid.substr(0,4) == "row-"){
			$("#answer"+thisid.substr(3)).attr('checked','checked');
		}else{
			parentid = thisid.substr(thisid.indexOf("-")+1,thisid.lastIndexOf("-")-thisid.indexOf("-")-1);
			childid = thisid.substr(thisid.lastIndexOf("-")+1);
			$("#answer-"+parentid+"-"+childid).attr('checked','checked');
		}
	});
	$(".cellHoverClick").livequery(function(){ 
		$(this).hover(
			function(){ 
				thisid = $(this).attr('id');
				if(thisid.substr(0,4) == "row-"){
					$("#"+thisid).addClass("hover-scale");
				}else{
					parentid = thisid.substr(thisid.indexOf("-")+1,thisid.lastIndexOf("-")-thisid.indexOf("-")-1);
					childid = thisid.substr(thisid.lastIndexOf("-")+1);
					$("#rowa-"+parentid+"-"+childid).addClass("hover-scale");
					$("#rowb-"+parentid+"-"+childid).addClass("hover-scale");
				}
			}, 
			function(){ 
				thisid = $(this).attr('id');
				if(thisid.substr(0,4) == "row-"){
					$("#"+thisid).removeClass("hover-scale");
				}else{
					parentid = thisid.substr(thisid.indexOf("-")+1,thisid.lastIndexOf("-")-thisid.indexOf("-")-1);
					childid = thisid.substr(thisid.lastIndexOf("-")+1);
					$("#rowa-"+parentid+"-"+childid).removeClass("hover-scale");
					$("#rowb-"+parentid+"-"+childid).removeClass("hover-scale");
				}
			}
		);
	});
});
/*function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isUndefined(a) {
    return (typeof a == 'undefined');
}*/
function form_show_results(){
	$("#button_form_next").click();
}
function tickAll(t){
	$.each($('.group-'+$(t).attr('id')), function(i, e) {
		$(e).attr('checked','checked');
	});
}
function toPostVariable(s){
	var v = s.toString().toLowerCase();
	v = v.replace(/ /g,'_');
	v = v.replace(/\W/g,'');
	return v;
}
function regex(str,exp){
	var re = new RegExp(exp,"i");
	//str = str.toLowerCase();
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}
function restoreHTMLcodes(s){
	var v = s.toString();
	v = v.replace(/&#47;/g,'/');
	return v;
}
function jsonsafe(s){
	var v = s.toString();
	v = v.replace(/'/g,'&#39;');
	return v;
}
function formValidate(){
	var errors = [];
	$.each($.find('.regexp'), function(i, e) {
		if($(e).val().length > 0){
			if(!regex($(e).prev().val(),$(e).val())){
				nextid = $(e).parent().parent().parent().parent().parent().attr('id');
				errors.push("Please check question '"+$('#'+nextid).find("p:first").html().split("<span")[0]+"', "+$('#'+nextid).find(".form-question-help").html()+".");
			}
		}
	});
	for(a in formdata) {
		switch(a){
			case "questionnaire":
				if(isArray(formdata[a]["items"]) || isObject(formdata[a]["items"])){
					for(i in formdata[a]["items"]){
						if(formdata[a]["items"][i]["required"] == true){
							switch(formdata[a]["items"][i]["type"]){
								case "multiplechoice":
									if(isUndefined($("[name="+formdata[a]["items"][i]["name"]+"]:checked").val())){
										errors.push("'"+formdata[a]["items"][i]["question"]+"' is mandatory!");
									}
								break;
								case "checkbox":
									for(c in formdata[a]["items"][i]["choice"]){
										if(isUndefined($("[name=json-"+formdata[a]["items"][i]["choice"][c]["name"]+"]:checked").val())){
											errors.push("'"+formdata[a]["items"][i]["choice"][c]["choice"]+"' is mandatory!");
										}
									}
								break;
								case "text":
									if($("[name="+formdata[a]["items"][i]["name"]+"]").val() == ""){
										errors.push("'"+formdata[a]["items"][i]["question"]+"' is mandatory!");
									}
								break;
								case "paragraph":
									if($("[name="+formdata[a]["items"][i]["name"]+"]").val() == ""){
										errors.push("'"+formdata[a]["items"][i]["question"]+"' is mandatory!");
									}
								break;
								case "scale":
									if(isUndefined($("[name="+formdata[a]["items"][i]["name"]+"]:checked").val())){
										errors.push("'"+formdata[a]["items"][i]["question"]+"' is mandatory!");
									}
								break;
								case "scalegroup":
									for(c in formdata[a]["items"][i]["choice"]){
										if(isUndefined($("[name=json-"+formdata[a]["items"][i]["choice"][c]["name"]+"]:checked").val())){
											errors.push("'"+formdata[a]["items"][i]["choice"][c]["choice"]+"' is mandatory!");
										}
									}
								break;
							}
						}
					}
				}
			break;
		}
	}
	if(errors.length == 0){
		return true;
	}else{
		var msg = "";
		for(error in errors){
			msg += " - "+restoreHTMLcodes(errors[error])+"\n";
		}
		alert(msg);
		return false;
	}
}
function createForm(o){
	if(isArray(o) || isObject(o)){
		for(a in o) {
			switch(a){
				case "questionnaire":
					//$("#title").html(o[a]["title"]);
					//$("#description").html(o[a]["description"]);
					//$("#sortable").parent().prepend("<input type=\"text\" name=\"totalquestions\" value=\""+o[a]["totalquestions"]+"\" />");
					$("#progressbar").reportprogress(Math.round((o[a]["questionsanswered"]/o[a]["questionstotal"])*100));
					if(isArray(o[a]["items"]) || isObject(o[a]["items"])){
						addField(o[a]["items"]);
					}
				break;
			}
		}
	}
}
function addField(o,options){
	var options = (options == null) ? {} : options;
	if(isArray(o) || isObject(o)){
		//options.questioncount = 0;
		for(i in o) {
			var nextid = 1;
			var choiceAnswer = "";
			$.each($("#sortable").children(), function(i, e) {
				if(e.id >= nextid) nextid = parseInt(e.id)+1;
		    });
			var strQuestion = o[i]["question"];
			options.question = strQuestion;
			if(isUndefined(o[i]["answer"]) || o[i]["answer"] == ""){
				var strAnswer = "";
			}else{
				var strAnswer = o[i]["answer"];
			}
			options.answer = strAnswer;
			if(isUndefined(o[i]["help"])){
				var strHelp = "";
			}else{
				var strHelp = o[i]["help"];
			}
			if(isUndefined(o[i]["name"]) || o[i]["name"] == ""){
				var strName = toPostVariable(strQuestion);
			}else{
				var strName = o[i]["name"];
			}
			options.name = strName;
			if(isUndefined(o[i]["regexp"]) || o[i]["regexp"] == ""){
				var strRegExp = "";
			}else{
				var strRegExp = o[i]["regexp"];
			}
			if(o[i]["type"] == "multiplechoice" || o[i]["type"] == "exam" || o[i]["type"] == "checkbox" || o[i]["type"] == "text" || o[i]["type"] == "paragraph" || o[i]["type"] == "scale" || o[i]["type"] == "scalegroup"){
				//options.questioncount++;
				var lastQuestion = strName;
			}
			if(o[i]["type"] == "logic"){
				$('#sortable').find("li:last").append('<input type="hidden" name="logic" value="'+strName+'" />');
			}else{
				$('#sortable').append($("<li></li>").attr('id',nextid).attr('class','onhover').html("<table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr><td><p class=\"form-question-title\">"+strQuestion+"<span class=\"form-question-required\" id=\"required-"+nextid+"\"></span></p></td></tr></table>"));
				if(o[i]["required"] == true){
					//$("#required-"+nextid).html('<input type="hidden" name="required[]" value="'+nextid+'" /> * Required');
					$("#required-"+nextid).html('<input type="hidden" name="required[]" value="'+strName+'" /> * Required');
				}
				if(strHelp.length > 0){
					$('#'+nextid).find("td:first").append($("<p class=\"form-question-help\"></p>").html(strHelp));
				}
				switch(o[i]["type"]){
					case "multiplechoice":
						$('#'+nextid).find("tr:last").after("<tr><td><table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" class=\"choices\" width=\"100%\"><tbody></tbody></table></td></tr>");
						if(isArray(o[i]["choice"]) || isObject(o[i]["choice"])){
							for(c in o[i]["choice"]){
								addChoice(nextid,{"type": o[i]["type"], "name": o[i]["choice"][c]["name"], "choice": o[i]["choice"][c]["choice"]},options);
							}
						}
					break;
					case "exam":
						$('#'+nextid).find("tr:last").after("<tr><td><table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" class=\"choices\" width=\"100%\"><tbody></tbody></table></td></tr>");
						if(isArray(o[i]["choice"]) || isObject(o[i]["choice"])){
							for(c in o[i]["choice"]){
								addChoice(nextid,{"type": o[i]["type"], "name": o[i]["choice"][c]["name"], "choice": o[i]["choice"][c]["choice"], "score": o[i]["choice"][c]["score"]},options);
							}
						}
					break;
					case "checkbox":
						options.group = strName;
						$('#'+nextid).find("tr:last").after("<tr><td><table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" class=\"choices\" width=\"100%\"><tbody><tr><td align=\"right\"><label>Select All </label><input onclick=\"javascript:tickAll(this);\" type=\"radio\" name=\"\" id=\""+strName+"-yes\" value=\"yes\" /><label for=\""+strName+"-yes\"> Yes</label> <input onclick=\"javascript:tickAll(this);\" type=\"radio\" name=\"\" id=\""+strName+"-no\" value=\"no\" /><label for=\""+strName+"-no\"> No</label></td></tr></tbody></table></td></tr>");
						if(isArray(o[i]["choice"]) || isObject(o[i]["choice"])){
							for(c in o[i]["choice"]){
								if(isUndefined(o[i]["choice"][c]["answer"]) || o[i]["choice"][c]["answer"] == ""){
									choiceAnswer = "";
								}else{
									choiceAnswer = o[i]["choice"][c]["answer"];
								}
								addChoice(nextid,{"type": o[i]["type"], "name": o[i]["choice"][c]["name"], "choice": o[i]["choice"][c]["choice"], "answer": choiceAnswer},options);
							}
						}
						$('#'+nextid).find(".choices tr:odd").addClass("form-answer-alternate");
					break;
					case "text":
						//$('#'+nextid).find("tr:last").after("<tr><td><input type=\"text\" name=\""+nextid+"\" size=\"20\" /></td></tr>");
						if(strRegExp == ""){
							$('#'+nextid).find("tr:last").after("<tr><td><input type=\"text\" name=\""+strName+"\" size=\"20\" value=\""+strAnswer+"\" /></td></tr>");
						}else{
							$('#'+nextid).find("tr:last").after("<tr><td><input type=\"text\" name=\""+strName+"\" size=\"20\" value=\""+strAnswer+"\" /><input type=\"hidden\" class=\"regexp\" value=\""+strRegExp+"\" /></td></tr>");
						}
					break;
					case "paragraph":
						//$('#'+nextid).find("tr:last").after("<tr><td><textarea name=\""+nextid+"\" rows=\"3\" cols=\"40\"></textarea></td></tr>");
						$('#'+nextid).find("tr:last").after("<tr><td><textarea name=\""+strName+"\" rows=\"3\" cols=\"40\">"+strAnswer+"</textarea></td></tr>");
					break;
					case "scale":
						$('#'+nextid).find("tr:last").after("<tr><td><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"choices\" width=\"100%\"><tbody></tbody></table></td></tr>");
						if(isArray(o[i]["choice"]) || isObject(o[i]["choice"])){
							first = (o[i]["choice"][0]["choice"].length>0)?true:false;
							middle = false;
							last = (o[i]["choice"][o[i]["choice"].length-1]["choice"].length>0)?true:false;
							for(x=1;x<o[i]["choice"].length-1;x++){
								if(o[i]["choice"][x]["choice"].length>0){
									middle = true;
								}
							}
							$('#'+nextid).find(".choices").find("tbody").html("<tr></tr><tr></tr>");
							options.count = o[i]["choice"].length;
							options.firstlast = false;
							options.showlables = false;
							if(first == true && middle == false && last == true){
								options.firstlast = true;
							}else if(first == false && middle == false && last == false){
								options.showlables = false;
							}else{
								options.showlables = true;
								//$('#'+nextid).find(".choices").attr("width","400");
							}
							for(c in o[i]["choice"]){
								addChoice(nextid,{"type": o[i]["type"], "name": o[i]["choice"][c]["name"], "choice": o[i]["choice"][c]["choice"]},options);
							}
							if(first == true && middle == false && last == true){
								$('#'+nextid).find(".choices").find("tbody").find("tr:first").prepend($("<td></td>").html("&nbsp;"));
								$('#'+nextid).find(".choices").find("tbody").find("tr:last").prepend($("<td align=\"right\"></td>").html(o[i]["choice"][0]["choice"]));
								$('#'+nextid).find(".choices").find("tbody").find("tr:first").append($("<td></td>").html("&nbsp;"));
								$('#'+nextid).find(".choices").find("tbody").find("tr:last").append($("<td align=\"left\"></td>").html(o[i]["choice"][o[i]["choice"].length-1]["choice"]));
							}
						}
					break;
					case "scalegroup":
						options.group = strName;
						$('#'+nextid).find("tr:last").after("<tr><td><table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" class=\"choices\" width=\"100%\"><tbody><tr></tr></tbody></table></td></tr>");
						$('#'+nextid).find(".choices").find("tbody").find("tr:first").append($("<td width=\"50%\"></td>").html("&nbsp;"));
						if(isArray(o[i]["option"]) || isObject(o[i]["option"])){
							options.count = o[i]["option"].length;
							options.options = [];
							x=0;
							for(c in o[i]["option"]){
								x++;
								options.options[x] = o[i]["option"][c]["name"];
								if(isUndefined(o[i]["option"][c]["option"]) || o[i]["option"][c]["option"] == ""){
									choiceAnswer = "&nbsp;";
								}else{
									choiceAnswer = o[i]["option"][c]["option"];
								}
								$('#'+nextid).find(".choices").find("tbody").find("tr:first").append($("<td width=\""+Math.round(((1/options.count)*100)/2)+"%\" align=\"center\"></td>").html(choiceAnswer));
							}
						}
						if(isArray(o[i]["choice"]) || isObject(o[i]["choice"])){
							for(c in o[i]["choice"]){
								if(isUndefined(o[i]["choice"][c]["answer"]) || o[i]["choice"][c]["answer"] == ""){
									choiceAnswer = "";
								}else{
									choiceAnswer = o[i]["choice"][c]["answer"];
								}
								addChoice(nextid,{"type": o[i]["type"], "name": o[i]["choice"][c]["name"], "choice": o[i]["choice"][c]["choice"], "answer": choiceAnswer},options);
							}
						}
						$('#'+nextid).find(".choices tr:odd").addClass("form-answer-alternate");
					break;
				}
			}
		}
		$("#sortable").parent().prepend("<input type=\"hidden\" name=\"lastquestion\" value=\""+lastQuestion+"\" />");
	}
}
function addChoice(parentid,data,options){
	var options = (options == null) ? {} : options;
	var nextid = 1;
	$.each($('#'+parentid).find(".choices").find("tbody").find("tr"), function(i, e) {
		if($(e).attr("id").lastIndexOf("-") > -1){
			if($(e).attr("id").substr($(e).attr("id").lastIndexOf("-")+1) >= nextid) nextid = parseInt($(e).attr("id").substr($(e).attr("id").lastIndexOf("-")+1))+1;
		}
	});
	switch(data["type"]){
		case "multiplechoice":
			//$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"\" name=\"answer["+parentid+"]["+nextid+"]\" value=\""+data["choice"]+"\" /> <label for=\"answer-"+parentid+"-"+nextid+"\">"+data["choice"]+"</label></td>"));
			$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"\" name=\""+options.name+"\" value=\""+data["name"]+"\" /> <label for=\"answer-"+parentid+"-"+nextid+"\">"+data["choice"]+"</label></td>"));
			if(options.answer != ""){
				if(options.answer == data["name"]){
					$('#answer-'+parentid+'-'+nextid).attr('checked','checked');
				}
			}
		break;
		case "exam":
			var score = isNaN(data["score"])?0:data["score"];
			//$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"\" name=\"answer["+parentid+"]["+nextid+"]\" value=\""+data["choice"]+"\" /> <label for=\"answer-"+parentid+"-"+nextid+"\">"+data["choice"]+"</label></td>"));
			$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"\" name=\""+options.name+"\" value=\""+data["name"]+"\" /> <label for=\"answer-"+parentid+"-"+nextid+"\">"+data["choice"]+"</label></td>"));
			if(options.answer != ""){
				if(options.answer == data["name"]){
					$('#answer-'+parentid+'-'+nextid).attr('checked','checked');
				}
			}
		break;
		case "checkbox":
			//$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><input type=\"checkbox\" id=\"answer-"+parentid+"-"+nextid+"\" name=\"answer["+parentid+"]["+nextid+"]\" value=\""+data["choice"]+"\" /> <label for=\"answer-"+parentid+"-"+nextid+"\">"+data["choice"]+"</label></td>"));	// name=\""+toPostVariable(options.question)+"[]\"	// <table cellspacing=\"0\" padding=\"0\" border=\"1\" width=\"400\"><tr><td width=\"80%\">"+data["choice"]+"</td><td width=\"20%\"><input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"-y\" name=\""+toPostVariable(data["choice"])+"\" value=\"Yes\" /> <label for=\"answer-"+parentid+"-"+nextid+"-y\">Yes</label> <input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"-n\" name=\""+toPostVariable(data["choice"])+"\" value=\"No\" /> <label for=\"answer-"+parentid+"-"+nextid+"-n\">No</label></td></tr></table>
			//$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><table cellspacing=\"0\" padding=\"0\" border=\"1\" width=\"400\"><tr><td width=\"80%\">"+data["choice"]+"</td><td width=\"20%\"><input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"-y\" name=\""+toPostVariable(data["choice"])+"\" value=\"Yes\" /> <label for=\"answer-"+parentid+"-"+nextid+"-y\">Yes</label> <input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"-n\" name=\""+toPostVariable(data["choice"])+"\" value=\"No\" /> <label for=\"answer-"+parentid+"-"+nextid+"-n\">No</label></td></tr></table></td>"));
			$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td><dl style=\"width: 100%;\"><dt style=\"width: 75%;float: left;\">"+data["choice"]+"</dt><dd style=\"width: 25%;float:right;text-align:right;\"><input type=\"radio\" class=\"group-"+options.group+"-yes\" id=\"answer-"+parentid+"-"+nextid+"-y\" name=\"json-"+data["name"]+"\" value='"+$.toJSON({"parent":options.name,"name":data["name"],"value":"yes"})+"' /> <label for=\"answer-"+parentid+"-"+nextid+"-y\">Yes</label> <input type=\"radio\" class=\"group-"+options.group+"-no\" id=\"answer-"+parentid+"-"+nextid+"-n\" name=\"json-"+data["name"]+"\" value='"+$.toJSON({"parent":options.name,"name":data["name"],"value":"no"})+"' /> <label for=\"answer-"+parentid+"-"+nextid+"-n\">No</label></dd></dl></td>"));
			//$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td>"+data["choice"]+" <input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"-y\" name=\""+toPostVariable(data["choice"])+"\" value=\"Yes\" /> <label for=\"answer-"+parentid+"-"+nextid+"-y\">Yes</label> <input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"-n\" name=\""+toPostVariable(data["choice"])+"\" value=\"No\" /> <label for=\"answer-"+parentid+"-"+nextid+"-n\">No</label></td>"));
			if(data["answer"] != ""){
				if(data["answer"] == "yes"){
					$('#answer-'+parentid+'-'+nextid+'-y').attr('checked','checked');
				}else if(data["answer"] == "no"){
					$('#answer-'+parentid+'-'+nextid+'-n').attr('checked','checked');
				}
			}
		break;
		case "scale":
			$.each($('#'+parentid).find(".choices").find("tbody").find("tr:first").find("td"), function(i, e) {
				if($(e).attr("id").lastIndexOf("-") > -1){
					if($(e).attr("id").substr($(e).attr("id").lastIndexOf("-")+1) >= nextid) nextid = parseInt($(e).attr("id").substr($(e).attr("id").lastIndexOf("-")+1))+1;
				}
			});
			label = nextid;
			width = "";
			if(options.showlables == true){
				if(data["choice"].length > 0){
					label = data["choice"];
					width = " width=\""+Math.round((1/options.count)*100)+"%\"";
				}else{
					label = "";
				}
			}
			$('#'+parentid).find(".choices").find("tbody").find("tr:first").append($("<td class=\"cellHoverClick\" align=\"center\""+width+"></td>").attr("id","rowa-"+parentid+"-"+nextid).html("<label for=\"answer-"+parentid+"-"+nextid+"\">"+label+"</label>"));
			//$('#'+parentid).find(".choices").find("tbody").find("tr:last").append($("<td align=\"center\"></td>").attr("id","row-"+parentid+"-"+nextid).html("<input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"\" name=\"answer["+parentid+"]["+nextid+"]\" value=\""+Math.round((nextid/options.count)*100)+"\" />"));
			$('#'+parentid).find(".choices").find("tbody").find("tr:last").append($("<td class=\"cellHoverClick\" align=\"center\""+width+"></td>").attr("id","rowb-"+parentid+"-"+nextid).html("<input type=\"radio\" id=\"answer-"+parentid+"-"+nextid+"\" name=\""+options.name+"\" value=\""+data["name"]+"\" />"));
			if(options.answer != ""){
				if(options.answer == data["name"]){
					$('#answer-'+parentid+'-'+nextid).attr('checked','checked');
				}
			}
		break;
		case "scalegroup":
			$.each($('#'+parentid).find(".choices").find("tbody").find("tr:first").find("td"), function(i, e) {
				if($(e).attr("id").lastIndexOf("-") > -1){
					if($(e).attr("id").substr($(e).attr("id").lastIndexOf("-")+1) >= nextid) nextid = parseInt($(e).attr("id").substr($(e).attr("id").lastIndexOf("-")+1));
				}
			});
			$('#'+parentid).find(".choices").find("tbody").append($("<tr></tr>").attr("id","row-"+parentid+"-"+nextid).html("<td>"+data["choice"]+"</td>"));
			for(x=1;x<=options.count;x++){
				//label = Math.round((x/options.count)*100);
				label = options.options[x];
				$('#'+parentid).find(".choices").find("tbody").find("tr:last").append($("<td class=\"cellHoverClick\" align=\"center\"></td>").attr("id","row-"+parentid+"-"+nextid+"-"+x).html("<input type=\"radio\" class=\"group-"+options.group+"-"+x+"\" id=\"answer-"+parentid+"-"+nextid+"-"+x+"\" name=\"json-"+data["name"]+"\" value='"+$.toJSON({"parent":options.name,"name":data["name"],"value":label})+"' />"));
				if(options.answer != ""){
					if(options.answer == label){
						$('#answer-'+parentid+'-'+nextid+'-'+x).attr('checked','checked');
					}
				}
			}
		break;
	}
}
/*function print_r(a,l){
	var l = (l == null) ? 0 : l;
	var m = "Array => (\n";
	s = Array((l+1)*4).join(" ");
	for(i in a) {
		if(isArray(a[i]) || isObject(a[i])){
			m += s+i+" => "+print_r(a[i],l+1)+"\n";
		}else{
			m += s+i+" => "+a[i]+"\n";
		}
	}
	s = Array(l*4).join(" ");
	m += s+")\n";
	return m;
}*/
function removeId(id){
	$("#"+id).remove();
}
/*function hide(i){
	$("#"+i).css({display: 'none'});
}*/