var clickedTag;
//--------------------------------------------------
function __SelectOrganization(_o)
{
	//var _appPath = _o._appPath;
	var _clientID = _o._clientID;
	var _args = new Function();
	if (window.showModalDialog('/Framework/OrgTree/default.htm', _args, 'dialogWidth:300px; dialogHeight:350px; center:yes; edge:raised; scroll:no; status:no;'))
	{
		document.getElementById(_clientID+'hidCmd').value		= "ChangeOrg";
		document.getElementById(_clientID+'hidID').value		= _args.OrgID;
		document.getElementById(_clientID+'hidParam1').value	= _args.OrgName;
		document.getElementById(_clientID+'hidParam2').value	= _args.OrgShortName;
		document.getElementById(_clientID+'lbCmd').click();
	}
}
//--------------------------------------------------
function __Survey_Preview()
{
	var ww = window.screen.availWidth - 100;
	var hh = window.screen.availHeight - 100;
	window.open("/Survey/TakeSurvey.htm", "_blank", "height="+hh+"px,width="+ww+"px,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}
//--------------------------------------------------
function __Survey_GetXmlMenu(pan)
{
	var a = pan.children;
	for(var j=0; j<a.length; j++)
	{
		if (a[j].tagName.toLowerCase() == "xml")
			return a[j];
	}
	return null;
}
//--------------------------------------------------
function __Survey_OnClickSurveyIcon(o)
{
	event.returnValue = false;
	event.cancelBubble = true;

	var clientID= Survey_GetClientID(o);
	var tbl		= __Survey_GetItemTable(o);
	var td		= o;while(td.tagName!="TD"){td=td.parentElement;}
	var xRoot	= __Survey_GetXmlMenu(document.getElementById(clientID+"_panXmls")).XMLDocument.documentElement.cloneNode(true);
	var x		= xRoot.selectSingleNode("//ContextMenus/Caller[@ref='Survey']");

	clickedTag = o;
	
	try
	{
		x.selectSingleNode("Item[@guid='EditInfo']").setAttribute("disabled",	tbl.c_perm_EditInfo=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='Design']").setAttribute("disabled",		tbl.c_perm_Design=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='Upload']").setAttribute("disabled",		tbl.c_perm_Upload=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='Delete']").setAttribute("disabled",		tbl.c_perm_Delete=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='Export']").setAttribute("disabled",		tbl.c_perm_Export=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='BackupXml']").setAttribute("disabled",	tbl.c_perm_Export=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='EnterSurvey']").setAttribute("disabled",tbl.c_perm_EnterSurvey=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='SamplePlans']").setAttribute("disabled",tbl.c_perm_SamplePlans=="y"?"n":"y");
		x.selectSingleNode("Item[@guid='ViewReports']").setAttribute("disabled",tbl.c_perm_ViewReports=="y"?"n":"y");
		var xI = x.selectSingleNode("Item[@guid='Publish']");
		var ok = tbl.c_IsPublished=="y";
		xI.setAttribute("disabled",	tbl.c_perm_Publish=="y"?"n":"y");
		xI.setAttribute("guid",	ok?"ResetPublish":"Publish");
		xI.setAttribute("text",	ok?"Reset Publish State":"Publish");
		xI.setAttribute("icon",	"/images/23x22/" + (ok?"UndoEvents.gif":"Events.gif"));
		x.selectSingleNode("Item[@guid='SetPerm']").setAttribute("disabled",tbl.c_perm_SetPerm=="y"?"n":"y");

		//...set references to Popup objects
		try{ td._Popup.hide();}catch(e){} 
		td._Popup = window.createPopup();
		td._RootPopup = td._Popup;
		td._path = '';
		
		eval("Menu_ShowContextMenu(0, 19, xRoot, x, td, '__Survey_ContextMenuSelected');");
	}
	catch(e)
	{
alert(e.message);
	}
}
//--------------------------------------------------
function __Survey_ContextMenuSelected(cmd)
{
	var o			= clickedTag;
	var clientID	= Survey_GetClientID(o);
	var tbl			= __Survey_GetItemTable(o);
	var tr			= tbl.rows[0];
	var hidID		= document.getElementById(clientID+"_hidID");
	var hidCmd		= document.getElementById(clientID+"_hidCommand");
	var hidValue	= document.getElementById(clientID+"_hidValue");
	var lb			= document.getElementById(clientID+"_lbCommand");
	var doCmd		= false;
	

	hidCmd.value	= cmd;
	hidID.value		= tbl.c_ItemID;

	if (cmd=="EditInfo" || cmd=="SamplePlans" || cmd=="EnterSurvey" || cmd=="ViewReports")
	{
		doCmd = true;
	}
	else if (cmd=="Design"){		__Survey_CallEditor(o);		}
	else if (cmd=="Publish"){		__Survey_AskFor(o,'Publish');	}
	else if (cmd=="ResetPublish"){	__Survey_AskFor(o,'ResetPublish');	}
	else if (cmd=="Upload"){		__Survey_CallUploader(o);		}
	else if (cmd=="Delete"){		__Survey_AskFor(o,'Delete');	}
	else if (cmd=="Export")
	{
		window.open( "/Panels/SurveyStructure/SurveyXmlStructure.aspx?surveyID=" + tbl.c_ItemID );
	}
	else if (cmd=="BackupXml")
	{
	window.open( "/Panels/SurveyStructure/Backup.aspx?surveyID=" + tbl.c_ItemID );
	}
	else if (cmd=="DownloadDD")
	{
		var fn = "/LinkedProjects/Tools/Survey/DownloadDataDictionary/Sheet1.aspx";
		var url = fn + "?ItemID="+tbl.c_ItemID;
		var args = new Function();
//alert(url);
		tr.runtimeStyle.backgroundColor = "yellow";
		window.open(url);
		alert("A new window with Data Dictionary in Excel format should appear shortly. You may Open Excel file in the browser or Save it in local file and then open with Excel.");
		//if (window.showModalDialog(url, args, 'dialogWidth:780px; dialogHeight:600px; center:yes; edge:raised; scroll:no; status:no;'))
//		{
			//hidValue.value = args.xmlPerms;
			//doCmd = true;
//alert(hidValue.value);
//		}
		tr.runtimeStyle.backgroundColor = "";
	}
	else if (cmd=="SetPerm"){		__Survey_SetPerm(o);		}

	if (doCmd)
		try{lb.click();}catch(e){alert(e.message);}
}
//-------------------------------------------------------------
function __Survey_AskFor(o,cmd)
{
	var clientID	= Survey_GetClientID(o);
	var tbl=__Survey_GetItemTable(o);//o;while(tbl.c_token!="ItemTable")tbl=tbl.parentElement;
	var tr = tbl.rows[0];
	var ss="";
	if(cmd=="Delete" || o.c_askFor=="Delete")
	{
		ss = 'Do you want to delete selected survey?';
		cmd = "Delete";
	}
	else if(cmd=="Publish" || o.c_askFor=="Publish")
	{
		ss = 'Notice that, when published, survey becomes accessible for consumers and after first response can not be redesigned any more.\n\n Do you want to PUBLISH selected survey?';
		cmd = "Publish";
	}
	else if(cmd=="ResetPublish")
	{
		ss = 'Do you want to RESET published survey? \n\nNotice that next time you publish survey all data will be lost.';
	}
	
	if(ss=="")
	{
		alert("Unknown command");
		return;
	}
	tr.runtimeStyle.backgroundColor = "yellow";
	if (confirm(ss))
	{
		document.getElementById(clientID+"_hidCommand").value	= cmd;
		document.getElementById(clientID+"_hidID").value		= tbl.c_ItemID;
		try{document.getElementById(clientID+"_lbCommand").click();}catch(e){alert(e.message);}
	}
	tr.runtimeStyle.backgroundColor = "";
}
//-------------------------------------------------------------
function __Survey_CallEditor(o)
{
	var tbl=__Survey_GetItemTable(o);//o;while(tbl.c_token!="ItemTable")tbl=tbl.parentElement;
	var clientID	= Survey_GetClientID(o);
//	var args = new Function();
//	args.SurveyID = tbl.c_ItemID;
	
	var bh = Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close;
	var args = new Function();
	args.clientID = clientID;
	args.response = null;
	args.callback = "__Survey_HasBeenChanged";
	args.SurveyID = tbl.c_ItemID;

	var url = "/Panels/SurveyEditor/default.aspx"; //htm";
	g_global.OpenWindow(url,args,1100,600,"Survey Editor", bh);
	
	/*
	if (window.showModalDialog("/Panels/SurveyEditor/default.htm", args, 'dialogWidth:70em; dialogHeight:55em; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;'))
	{
		document.getElementById(clientID+"_hidCommand").value	= "Refresh";
		document.getElementById(clientID+"_lbCommand").click();
	}
	*/
}
function __Survey_HasBeenChanged(args)
{
	g_global.ClickCommand(args.clientID,"Refresh");
}
//-------------------------------------------------------------
function __Survey_CallUploader(o)
{
	var tbl=__Survey_GetItemTable(o);//o;while(tbl.c_token!="ItemTable")tbl=tbl.parentElement;
	var clientID = Survey_GetClientID(o);
	var args = new Function();
	args.SurveyID = tbl.c_ItemID;
	if (window.showModalDialog("/Survey/UploadData/default.htm", args, 'dialogWidth:70em; dialogHeight:55em; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;'))
	{
		document.getElementById(clientID+"_hidCommand").value	= "Refresh";
		document.getElementById(clientID+"_lbCommand").click();
	}
}
//-------------------------------------------------------------
function __Survey_CallAudience(o)
{
	var tbl=__Survey_GetItemTable(o);//o;while(tbl.c_token!="ItemTable")tbl=tbl.parentElement;
	var clientID	= Survey_GetClientID(o);
	var args = new Function();
	args.SurveyID = tbl.c_ItemID;
	if (window.showModalDialog("/Panel/Audience/default.htm", args, 'dialogWidth:650px; dialogHeight:450px; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;'))
	{
		document.getElementById(clientID+"_hidCommand").value	= "Refresh";
		document.getElementById(clientID+"_lbCommand").click();
	}
}
//-------------------------------------------------------------
function Survey_GetPanel(o,tokenName)
{
	var p=o;while(p.c_token!=tokenName)p=p.parentElement;
	return p;
}
//-------------------------------------------------------------
function Survey_GetClientID(o,tokenName)
{
	var token="ListSurveys";
	if (tokenName != null)token = tokenName;
	var p=o;while(p.c_token!=token)p=p.parentElement;
	return p.getAttribute("c_clientID");
}
//-------------------------------------------------------------
function Survey_SelectGroup(o)
{
	var clientID	= Survey_GetClientID(o,"ChooseWay");
	var url			= "/Membership/SelectGroup/default.htm";
	var args = new Function();
	args.Mode = "OnlyGroup";
	args.SelectionMode = "single";
	if (window.showModalDialog(url, args, 'dialogWidth:400px; dialogHeight:500px; center:yes; edge:raised; scroll:no; status:no;'))
	{
		document.getElementById(clientID+'_hidCommand').value	= "ChangeGroup";
		document.getElementById(clientID+'_hidID').value		= args.GroupIDs;
		document.getElementById(clientID+'_lbCommand').click();
	}

/*
	var pan		= Survey_GetPanel(o,"ChooseWay");
	var clientID= pan.c_clientID;
	var url		= "/Panels/SelectPrincipal/default.htm";
	var args = new Function();
	args.Mode = "OnlyGroup";
	args.SelectionMode = "single";
	if (window.showModalDialog(url, args, 'dialogWidth:700px; dialogHeight:500px; center:yes; edge:raised; scroll:no; status:no;'))
	{
		var groupID = args.xRoot.selectSingleNode("I").getAttribute("id");
		document.getElementById(clientID+"_hidCommand").value = "ChangeGroup";
		document.getElementById(clientID+"_hidID").value = groupID;
		document.getElementById(clientID+"_lbCommand").click();
	}
	*/
}
//-------------------------------------------------------------
function Survey_UploadDataFile(o)
{
	var clientID= Survey_GetClientID(o,"NewEdit");
	var url		= "/Survey/Generator/default.htm";
	var hid		= document.getElementById(clientID+"_hidGenerateXml");
	var args	= new Function();
	args.xRootXml = hid.value;
	if (window.showModalDialog(url, args, 'dialogWidth:750px; dialogHeight:550px; center:yes; edge:raised; scroll:no; status:no;'))
	{
		document.getElementById(clientID+"_hidGenerateXml").value = args.xRoot.xml;
		var lbl = document.getElementById(clientID+"_lUploadedFileInfo");
		//lbl.style.color = "#003399";
		lbl.innerHTML = "Uploaded File Name:" + 
			"<BR><b>" + args.xRoot.getAttribute("OriginalFileName") + "</b>" + 
			"<BR>File Type:&nbsp;<b>" + args.xRoot.getAttribute("FileType") + "</b>" + 
			"<BR>Number of Columns:&nbsp;<b>" + args.xRoot.selectNodes("C").length + "</b>";
		var txt = document.getElementById(clientID+"_txtDataFileUploaded");
		txt.value = "y";
		txt.fireEvent("onchange");
	}
}
//-------------------------------------------------------------
function __Survey_GetItemTable(o)
{
	var tbl=o;while(tbl.c_token!="ItemTable")tbl=tbl.parentElement;
	return tbl;
}
//-------------------------------------------------------------
function __Survey_SetPerm(o)
{
	var clientID= Survey_GetClientID(o);
	var tbl=__Survey_GetItemTable(o);
	var args = new Function();
	args.FullItemID = tbl.c_ItemID;
	args.ModuleToken = "Survey";
	if (window.showModalDialog("/Panels/SetPermissions/default.htm", args, 'dialogWidth:70em; dialogHeight:55em; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;'))
	{
//alert(args.xmlSecurity);
		document.getElementById(clientID+"_hidCommand").value	= "SetPerm";
		document.getElementById(clientID+"_hidValue").value	= args.xmlSecurity;
		document.getElementById(clientID+"_lbCommand").click();
	}
}
//-------------------------------------------------------------
function __Survey_AllowedSurveys(o,clientID)
{
	var args = new Function();
	args.selectedItemIDs = document.getElementById(clientID+'_hidAllowedSurveys').value;
	args.mode = "all";
	if (window.showModalDialog("/Panels/SelectSurveys/default.htm", args, "dialogWidth:770px; dialogHeight:600px; center:yes; edge:raised; resizable:yes; scroll:yes; status:yes;"))
	{
		//document.getElementById(clientID+'_hidCommand').value	= "AllowedSurveys";
		document.getElementById(clientID+'_hidAllowedSurveys').value	= args.selectedItemIDs;
		//document.getElementById(clientID+'_lbCommand').click();
		return true;
	}
	return false;
}
//-------------------------------------------------------------
function __SurveyProps_ClickMsgCB(o)
{
	var tr=o;while(tr.tagName!="TR")tr=tr.parentNode;
	tr.nextSibling.style.display = o.checked ? "block" : "none";
}
