﻿var pageID;
function changeTheme(ddl){
	async("MySurvey_EditorFull.aspx","ChangeTheme","{'themeID':'"+$(ddl).val()+"','pageID':'"+pageID+"'}",function(r){
		if(r.d.error){}else{$("#CSSThemeLink").attr("href",r.d.url);$("#ThemeEditButton").css("display",(r.d.edit)?"":"none");}
	});
}
function renumberSections(){
	if(sectionNumbering>0){
		$("span[@SPos_Fixed=1]").each(function(i){$(this).text(""+(i+1));});
		$("span[@SPos_Var=1]").each(function(i){$(this).text(""+(i+1));});
	}
}
function renumberQuestions(){
	var x;var pos=startingQPos;
	if(questionNumbering>0){$("span[@QPos]").each(function(i){
			var t=$(this).attr("CurrSection");
			if(x!=t&&questionNumbering==1){x=t;pos=1;}
			$(this).text(pos+". ");
			pos++;
		});
	}
}
function sectionUpdated(sID){
	async("MySurvey_EditorFull.aspx","SectionText","{sID:'"+sID+"'}",function(r){
		if(r.d.error){}else{
			var d=$("div.pTitle[@sectionID="+sID+"]");
			d.html("<h2>"+r.d.head+"</h2><br class=\"clear\" />");
			if(d.next().is("div.pDesc")){
				if($.trim(r.d.sub)==""){d.next().remove();}
				else{d.next().html(r.d.sub);}
			}else if($.trim(r.d.sub)!=""){d.after("<div class=\"pDesc\">"+r.d.sub+"</div>");}
			var t="#"+r.d.headdecoded;
			if(t.length>29){t=t.substr(0,29)+"...";}
			$("#ddlPageList option:eq("+r.d.pos+")").text(t);
		}				
	});
}
function sectionAdded(sID,divID){
	async("MySurvey_EditorFull.aspx","GetSection","{sID:'"+sID+"',pv:"+singlePageView+"}",function(r){
		if(r.d.length>0){
			$("#qBody_"+divID).after(r.d);
			TB_init();renumberSections();renumberQuestions();
		}
	});
}
function sectionDeleted(sID,dSID){
	$(document.getElementById("qBody_"+sID)).fadeOut("normal",function(){
		$(this).next().remove();
		$(this).remove();
	});
	if(dSID!=""){// questions were moved
		$(document.getElementById("qBody_"+dSID)).fadeOut("normal",function(){
			var i=$(this).prev();
			$(this).next().remove();
			$(this).remove();
			async("MySurvey_EditorFull.aspx","GetSection","{sID:'"+dSID+"',pv:"+singlePageView+"}",function(r){
				if(r.d.length>0){
					i.after(r.d);
					TB_init();
				}
			});
		});
	}
	renumberSections();renumberQuestions();
}
function sectionSplit(nSID,oSID)
{
	async("MySurvey_EditorFull.aspx","SplitSections","{nSID:'"+nSID+"',oSID:'"+oSID+"',pv:"+singlePageView+"}",function(r){
		if(r.d.error){}else{
			if(r.d.redirect)location.href=r.d.redirect;
			else{
				var p=$(document.getElementById("qBody_"+r.d.oSID));
				var i=p.prev();
				var f=true;
				p.fadeOut("normal",function(){
					if(f){f=false;
						$(this).next().remove();
						$(this).remove();
						i.after(r.d.html);
						TB_init();renumberSections();renumberQuestions();
					}
				});
			}
		}
	});
}
function copyPage(btn,qs){
	var d="{sID:'"+readCookie($(btn).attr("EncSID") + "_PageCopyMode")+"',url:'"+qs+"',pv:'"+singlePageView+"'}";
	async("MySurvey_EditorFull.aspx","CopySection",d,function(r){
		if(r.d.error){cancelPageCopy($(btn).attr("EncSID"));if(r.d.refresh){refresh();}}
		else{
			switch(r.d.resultCode){
				case 1:cancelPageCopy($(btn).attr("EncSID"));alert("The example survey may not be modified.  Please join now for free to create your own surveys!");return;
				case 2:if(typeof(r.d.redirect)=='undefined'){alert("A basic account is limited to ten questions per survey.\n\nUpgrade to a professional subscription to allow unlimited questions per survey.");}break;
			}
			if(singlePageView){cancelPageCopy($(btn).attr("EncSID"));if(r.d.redirect){location.href=r.d.redirect;}}
			else{
				if(r.d.divID){
					$(document.getElementById("qBody_"+r.d.divID)).after(r.d.html);
					setup();
					$("a[@btnType=AddPageBtn]").unbind();
					cancelPageCopy($(btn).attr("EncSID"));renumberSections();renumberQuestions();
				}
			}
		}
	});
}
function cancelPageCopy(encSID){
	$("a[@btnType=AddPageBtn]").unbind("click").attr("class","TextBtn thickbox").html("Add Page Here");
	$("a[@btnType=CancelCopyPageBtn]").css("display","none");
	eraseCookie(encSID+"_PageCopyMode");eraseCookie(encSID+"_PageMoveMode");TB_init(); 
}
function restoreQuestion(btn,qs){
	qID=readCookie($(btn).attr("EncSID")+"_RestoreMode");
	async("MySurvey_EditorFull.aspx","RestoreQuestion","{qID:'"+qID+"',url:'"+qs+"',pv:"+singlePageView+"}",function(r){
		if(r.d.error){}else{
			$(btn).parent("div").before(r.d.html);
			setup();
			$("a[@btnType=AddQuestionBtn]").unbind();
			if(r.d.split){$(btn).parent("div").find("a[@btnType=SplitPageBtn]").show();}
			else{$(btn).find("a[@btnType=SplitPageBtn]").hide();
				$(btn).find("a[@btnType=AddPageBtn]").hide();
			}
			cancelQuestionCopy($(btn).attr("EncSID"));
			renumberQuestions();
		}
	});
}
function questionDeleted(qID,first){
	var f=true;
	$(document.getElementById("qBody_"+qID)).fadeOut("normal",function(){
		if(f){f=false;setup();
			var d=$(this).parents("div.ed_QstHdr");
			if(first){d.next().find("a[@btnType=SplitPageBtn]").hide();
				d.next().find("a[@btnType=AddPageBtn]").hide();
			}
			d.prev().remove();
			d.remove();
			renumberQuestions();
		}
	});
}
function copyQuestion(btn,q){
	var qID=readCookie($(btn).attr("EncSID")+"_CopyMode");
	async("MySurvey_EditorFull.aspx","CopyQuestion","{qID:'"+qID+"',url:'"+q+"',pv:"+singlePageView+"}",function(r){
		if(r.d.error){cancelQuestionCopy($(btn).attr("EncSID"));if(r.d.refresh)refresh();}
		else{
			switch(r.d.resultCode)
			{
				case 1:cancelQuestionCopy($(btn).attr("EncSID"));
					alert("The example survey may not be modified.  Please join now for free to create your own surveys!");
					break;
				case 2:cancelQuestionCopy($(btn).attr("EncSID"));
					alert("A basic account is limited to ten questions per survey.\n\nUpgrade to a professional subscription to allow unlimited questions per survey.");
					break;
				case 0:
					$(document.getElementById("qBody_"+r.d.divID)).before(r.d.html);
					setup();
					$("a[@btnType=AddQuestionBtn]").unbind();
					var q=$(document.getElementById("qBody_"+r.d.qID)).parents("div.ed_QstHdr");
					if(r.d.split){q.next().find("a[@btnType=SplitPageBtn]").show();}
					else{q.next().find("a[@btnType=SplitPageBtn]").hide();
						q.next().find("a[@btnType=AddPageBtn]").hide();						
					}
					cancelQuestionCopy($(btn).attr("EncSID"));
					renumberQuestions();				
			}
		}
	});
}
function moveQuestion(btn, qs){
	async("MySurvey_EditorFull.aspx","MoveQuestion","{qID:'"+readCookie($(btn).attr("EncSID") + "_MoveMode")+"',url:'"+qs+"',pv:"+singlePageView+"}",function(r){
		if(r.d.error){cancelQuestionCopy($(btn).attr("EncSID"));if(r.d.refresh)refresh();}
		else if(!r.d.moved){cancelQuestionCopy($(btn).attr("EncSID"));
			switch(r.d.resultCode){
				case 1:alert("The example survey may not be modified.  Please join now for free to create your own surveys!");break;
				case 2:alert("A basic account is limited to ten questions per survey.\n\nUpgrade to a professional subscription to allow unlimited questions per survey.");break;
			}
		}else{
			var q=$(document.getElementById("qBody_"+r.d.qID)).parents(".ed_QstHdr");
			var b;
			if(q.length>0){
				b=q.next();
				if(r.d.split.source){b.find("a[@btnType=SplitPageBtn]").show();}
				else{b.find("a[@btnType=SplitPageBtn]").hide();b.find("a[@btnType=AddPageBtn]").hide();}
				q.prev().remove();q.remove();
			}
			var i=$(document.getElementById("qBody_"+r.d.divID));
			i.before(r.d.html);
			setup();
			$("a[@btnType=AddQuestionBtn]").unbind();
			b=$(document.getElementById("qBody_"+r.d.qID)).parents(".ed_QstHdr").next();
			if(r.d.split.destination){b.find("a[@btnType=SplitPageBtn]").show();}
			else{b.find("a[@btnType=SplitPageBtn]").hide();b.find("a[@btnType=AddPageBtn]").hide();}
			cancelQuestionCopy($(btn).attr("EncSID"));
			renumberQuestions();		
		}
	});
}
function movePage(btn,qs){
	async("MySurvey_EditorFull.aspx","MoveSection","{sID:'"+(readCookie($(btn).attr("EncSID") + "_PageMoveMode"))+"',url:'"+qs+"',pv:"+singlePageView+"}",function(r){
		cancelPageCopy($(btn).attr("EncSID"));
		if(r.d.error){if(r.d.refresh)refresh();}
		else{
			if(r.d.moved){
				if(r.d.redirect){location.href=r.d.redirect;}
				else{
				var s=$(document.getElementById("qBody_"+r.d.sID));
					s.next().remove();s.remove();
					$(document.getElementById("qBody_"+r.d.divID)).after(r.d.html);
					$("a[@btnType=AddPageBtn]").unbind();
					renumberSections();
					renumberQuestions();
				}
			}else{
				switch(r.d.resultCode){
					case 1:alert("The example survey may not be modified.  Please join now for free to create your own surveys!");break;
					case 2:alert("A basic account is limited to ten questions per survey.\n\nUpgrade to a professional subscription to allow unlimited questions per survey.");break;
				}
			}
		}
	});
}
function questionUpdated(qID,divID,isNew){
	async("MySurvey_EditorFull.aspx","UpdateQuestion","{qID:'"+qID+"',pv:"+singlePageView+",isNew:"+isNew+"}",function(r){
		if(r.d.error){}else{
			if(isNew){$(document.getElementById("qBody_"+divID)).before(r.d.html);}
			else{var i=$(document.getElementById("qBody_"+r.d.qID)).parents(".ed_QstHdr");
				i.prev().after(r.d.html);
				i.remove();
			}
			setup();
			$("a[@btnType=AddQuestionBtn]").unbind();			
			var b=$(document.getElementById("qBody_"+r.d.qID)).parents(".ed_QstHdr").next();
			if(r.d.split){b.find("a[@btnType=SplitPageBtn]").show();}
			else{b.find("a[@btnType=SplitPageBtn]").hide();
				b.find("a[@btnType=AddPageBtn]").hide();
			}
			TB_init();
			renumberQuestions();
		}
	});
}
function initQuestionCopy(sid,qid){
	if($("a[@btnType=AddPageBtn]").attr("class").indexOf("TextBtnOrange")>=0)cancelPageCopy(sid);
	$("a[@btnType=AddQuestionBtn]").attr("class","TextBtnOrange").html("Paste Question Here").unbind("click").click(function(){copyQuestion(this,this.href);return false;});
	$("a[@btnType=CancelCopyBtn]").css("display","inline").html("Cancel Copy");
	createCookie(sid+"_CopyMode",qid);
}
function initPageCopy(encSID,encSectionID){
	if($("a[@btnType=AddQuestionBtn]").attr("class").indexOf("TextBtnOrange")>=0)cancelQuestionCopy(encSID);
	$("a[@btnType=AddPageBtn]").attr("class","TextBtnOrange").html("Paste Page Here").unbind("click").click(function(){copyPage(this,this.href);return false;});
	$("a[@btnType=CancelCopyPageBtn]").css("display","inline").html("Cancel Copy");
	createCookie(encSID+"_PageCopyMode",encSectionID);
}
function reInitQuestionCopy(){$("a[@btnType=AddQuestionBtn]").unbind("click").click(function(){copyQuestion(this,this.href);return false;});}
function reInitPageCopy(){$("a[@btnType=AddPageBtn]").unbind("click").click(function(){copyPage(this,this.href);return false;});}
function reInitQuestionMove(){$("a[@btnType=AddQuestionBtn]").unbind("click").click(function(){moveQuestion(this,this.href);return false;});}
function reInitPageMove(){$("a[@btnType=AddPageBtn]").unbind("click").click(function(){movePage(this,this.href);return false;});}
function reInitQuestionRestore(encSID,encQID){$("a[@btnType=AddQuestionBtn]").unbind("click").click(function(){restoreQuestion(this,this.href);return false;});}
function initQuestionMove(encSID,encQID){
	$("a[@btnType=AddQuestionBtn]").attr("class","TextBtnOrange").html("Move Question Here").unbind("click").click(function(){moveQuestion(this,this.href);return false;});
	$("a[@btnType=CancelCopyBtn]").css("display","inline").html("Cancel Move");
	createCookie(encSID+"_MoveMode",encQID);
}
function initPageMove(encSID,encSectionID){
	if($("a[@btnType=AddQuestionBtn]").attr("class").indexOf("TextBtnOrange")>=0)cancelQuestionCopy(encSID);
	$("a[@btnType=AddPageBtn]").attr("class","TextBtnOrange").html("Move Page Here").unbind("click").click(function(){movePage(this,this.href);return false;});
	$("a[@btnType=CancelCopyPageBtn]").css("display","inline").html("Cancel Move");
	createCookie(encSID+"_PageMoveMode",encSectionID);
}
function initQuestionRestore(encSID,encQID,redirectURL){
	createCookie(encSID+"_RestoreMode",encQID);
	window.location.href="MySurvey_EditorPage.aspx?sm="+redirectURL;
}
function cancelQuestionCopy(encSID){
	$("a[@btnType=AddQuestionBtn]").unbind("click").attr("class","TextBtn thickbox").html("Add Question Here");
	$("a[@btnType=CancelCopyBtn]").css("display","none");
	eraseCookie(encSID+"_CopyMode");
	eraseCookie(encSID+"_MoveMode");
	eraseCookie(encSID+"_RestoreMode");
	TB_init(); 
}
function qSkipLogic(qID,cnt){
	var n=$("a[@LogicID=QLogic_" + qID + "]");
	if(cnt>0){n.removeClass().addClass("TextBtnOrange thickbox").text("Edit Logic ("+cnt+")");}
	else{n.removeClass().addClass("TextBtn thickbox").text("Add Logic");}
}
function sSkipLogic(sID,cnt){
	var n=$("a[@LogicID=SLogic_" + sID + "]");
	if(cnt>0){n.removeClass().addClass("TextBtnOrange thickbox").text("Edit Logic");}
	else{n.removeClass().addClass("TextBtn thickbox").text("Add Logic");}
}
function refresh(){
	var url=location.href;
	if(location.href.indexOf("?")>-1){url+="&";}else{url+="?";}
	url+="d="+Math.random();
	location.href=url;
}