AdminNewsletterNewslettersGridJsParams = "width=800,height=600";
AdminNewsletterNewslettersGridInterval = null;

function AdminNewsletterNewslettersGridModify( uid ) {
	window.open( 'newsletter_edit.php?mode=modify&uid='+uid , '_blank' , AdminNewsletterNewslettersGridJsParams );
}

function AdminNewsletterNewslettersGridAction( n , action , params ) {
	if ($("#"+FNTHSuperGrid_ToolbarId( n , action )).hasClass( "dsb" )) return;
	switch (action) {
	case "new":
		window.open( 'newsletter_edit.php?mode=new&newsletter_group_id='+params["newsletter_group_id"] , '_blank' , AdminNewsletterNewslettersGridJsParams );break;
	case "modify":
		AdminNewsletterNewslettersGridModify( $("#"+n).data( "selecteds" )[0] , AdminNewsletterNewslettersGridJsParams );
		break;
	case "delete":
		if (!window.confirm( aJSLangDefs.newsletters_Confirm_delete_newsletter )) return;
		BGAction( n , action );
		break;
	case "filter":
		$("#frm_AdminNewsletterNewslettersGrid").append( "<input type=\"hidden\" name=\"task\" value=\"filter\" />" );
		document.frm_AdminNewsletterNewslettersGrid.submit();
		break;
	case "activate":
		if (!window.confirm( aJSLangDefs.newsletters_Confirm_activate_newsletter )) return;
		BGAction( n , action , params );
		break;
	case "send":
		if (!window.confirm( aJSLangDefs.newsletters_Confirm_send_newsletter )) return;
		BGAction( n , action , params );
		break;
	case "deactivate":
		if (!window.confirm( aJSLangDefs.newsletters_Confirm_deactivate_newsletter )) return;
		BGAction( n , action , params );
		break;
	case "test_email":
		AdminNewsletterTestEmail( params["newsletter_newsletter_id"] );
		break;
	case "clone_email":
		AdminNewsletterCloneEmail( params["newsletter_newsletter_id"] , params["newsletter_group_id"] );
	}
}

function AdminNewsletterNewslettersGridSelectionChanged( tblid , chk , id ) {
	BGSelectionChanged( tblid , chk , id );
	BGSelectionChangedDefaultAMD( tblid , chk, id );
}

function AdminNewsletterNewslettersGridRefreshRowsCallback( datas ) {
	var i,tdid;

	if (typeof datas.clearscript !="undefined") {
		eval( datas.clearscript );
	}
	for (i in datas.ids) {
		var job_id = datas.ids[i];
		var item = datas.datas[job_id];

		var prfx = "#"+datas.id + "_row"+job_id;

		var row = $(prfx);

		var onl_title = $(prfx+"_nl_title");
		var ostatus = $(prfx+"_status");
		var oadmin = $(prfx+"_admin");

		var changed = true;
		if (onl_title.html()!=item.nl_title) changed = true;
		if (ostatus.html()!=item.status) changed = true;
		if (oadmin.html()!=item.admin) changed = true;

		if (changed) {
			if (typeof item.clearscript!="undefined") eval( item.clearscript );
			onl_title.html( item.nl_title );
			eval( item.nl_title_js );
			ostatus.html( item.status );
			eval( item.status_js );
			oadmin.html( item.admin );
			eval( item.admin_js );
		}
	}
}

function AdminNewsletterNewslettersGridRefreshRows( tblid , ids ) {
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					inajax : 1,
					ajaxevent : "refresh_grid_rows",
					id : tblid,
					uids : ids
				}
			),
			success: function(ret) {
				AdminNewsletterNewslettersGridRefreshRowsCallback( eval( ret ) );
			}
		}
	);
}

function AdminNewsletterNewslettersGridTimer( tblid ) {
	var o = $("#"+tblid );
	var datas = o.data( "griddata" );
	AdminNewsletterNewslettersGridRefreshRows( tblid , datas.ids );
}

function InitAdminNewsletterNewslettersGrid( tblid , datas ) {
	var o = $("#"+tblid );

	// set tech timestamps
	var i;
	for (i in datas.ids) {
		var rid = datas.ids[i];
	}
	o.data( "griddata" , datas );
	AdminNewsletterNewslettersGridInterval = setInterval( "AdminNewsletterNewslettersGridTimer('"+tblid+"');" , 1000 );

	var opts = $(document.frm_clone_email.newsletter_group_id);
	var s = "";
	for (var i in datas.newsletter_groups) {
		s+="<option value=\""+i+"\">"+datas.newsletter_groups[i]+"</option>";
	}
	opts.html( s );
}

function AdminNewsletterTestEmail( newsletter_newsletter_id ) {
	var btns = {};
	btns[aJSLangDefs["newsletters_Cancel"]] =function () { AdminNewsletterTestEmailCancel(); }
	btns[aJSLangDefs["newsletters_Send"]] =function () { AdminNewsletterTestEmailSend(); }

	document.frm_test_email.newsletter_newsletter_id.value = newsletter_newsletter_id;
	$("#testemaildialog").dialog( {
			title: aJSLangDefs.newsletters_Test_email,
			resizable: false,
			height: 150,
			width: 300,
			modal: true,
			buttons: btns
		}
	);
}

function AdminNewsletterTestEmailCancel() {
	$("#testemaildialog").dialog( "close" );
	$("#testemaildialog").dialog( "destroy" );
}

function AdminNewsletterTestEmailSend() {
	$("#testemaildialog").dialog( "close" );
	$("#testemaildialog").dialog( "destroy" );
	document.frm_test_email.submit();
}

function AdminNewsletterCloneEmailCancel() {
	$("#cloneemaildialog").dialog( "close" );
	$("#cloneemaildialog").dialog( "destroy" );
}

function AdminNewsletterCloneEmailClone() {
	$("#cloneemaildialog").dialog( "close" );
	$("#cloneemaildialog").dialog( "destroy" );
	document.frm_clone_email.submit();
}


function AdminNewsletterCloneEmail( newsletter_newsletter_id , newsletter_group_id ) {
	var btns = {};
	document.frm_clone_email.newsletter_newsletter_id.value = newsletter_newsletter_id;
	var sel = $(document.frm_clone_email.newsletter_group_id);
	sel.val( newsletter_group_id );
	btns[aJSLangDefs["newsletters_Cancel"]] =function () { AdminNewsletterCloneEmailCancel(); }
	btns[aJSLangDefs["newsletters_Clone"]] =function () { AdminNewsletterCloneEmailClone(); }
	$("#cloneemaildialog").dialog( {
		title: aJSLangDefs.newsletters_Clone_newsletter ,
		resizable: false,
		height: 250,
		width: 400,
		modal: true,
		buttons: btns
		}
	);
}
