﻿function GetModulesOnPage()
{
	$('#SaveModulesPositionButton').html('<center>Загрузка...<br/><img src=\'/images/indicators/snake.gif\' border=0/></center>');
	var ModulesOnCenter = $('#WLS_modules_list_sort').sortable('toArray');
	var ModulesOnLeft = $('#WLS_modules_list_sort_left').sortable('toArray');
	var ModulesOnRight = $('#WLS_modules_list_sort_right').sortable('toArray');
	if (ModulesOnCenter != "")
	{
		$.ajax({
			url: "/System/Actions/Page/SetPageModulesSort.asp?z=center&m="+ModulesOnCenter,
			dataType: "html",
			async: false,
			complete:function(request){}
		});
	}
	if (ModulesOnLeft != "")
	{
		$.ajax({
			url: "/System/Actions/Page/SetPageModulesSort.asp?z=left&m="+ModulesOnLeft,
			dataType: "html",
			async: false,
			complete:function(request){}
		});
	}
	if (ModulesOnRight != "")
	{
		$.ajax({
			url: "/System/Actions/Page/SetPageModulesSort.asp?z=right&m="+ModulesOnRight,
			dataType: "html",
			async: false,
			complete:function(request){}
		});
	}
	$('#SaveModulesPositionButton').html('<a href="#" onclick="GetModulesOnPage();">Сохранить</a>');
}

function CheckParameterType(o)
{
	var ctrl = o;
	if (ctrl.options[ctrl.selectedIndex].value == "Link")
	{
		document.getElementById("LinkParameterExtra1").style.display = "block";
	}
	else
	{
		document.getElementById("LinkParameterExtra1").style.display = "none";
	}
}

function GetModalWindow(url, width, height)
{
	var result = window.showModalDialog(url, null, "dialogWidth:" + width + "px; dialogHeight:" + height + "px; scroll:no; center:yes; help:no; status:yes;" );
	return result;
}

function GetRecordIcon(obj, Id, RecordId)
{
	var url = "/helper/records/RecordIconUploader.asp?id=" + Id + "&r=" + RecordId;
	var res = GetModalWindow(url,700,500);
	if (res != null)
	{
		document.getElementById(Id).style.display = "none";
		document.getElementById(Id + "_link").style.display = "block";
		document.getElementById(Id + "_link").href = document.getElementById(Id+"_link").href + res;
	}
}

function GetRecordFile(obj, Id, RecordId)
{
	var url = "/helper/records/RecordFileUploader.asp?id=" + Id + "&r=" + RecordId;
	var res = GetModalWindow(url,700,500);
	if (res != null)
	{
		document.getElementById(Id).style.display = "none";
		document.getElementById(Id + "_link").style.display = "block";
		document.getElementById(Id + "_link").href = document.getElementById(Id+"_link").href + res;
	}
}

function GetFieldsForNode(obj)
{
	var url = "/helper/nodes/GetFiledsSelect.asp?n=" + obj.options[obj.selectedIndex].value;
	var str = WLJS_use_ajax(url);
	document.getElementById("NodeFieldsLayer").innerHTML = str;
}

function GetNodeTable(LayerID, NodeId, PageNum, PageSize, SortNum)
{
	var url = "/helper/nodes/GetNodeTable.asp?id=" + NodeId + "&sort=" + SortNum + "&p=" + PageNum + "&ps=" + PageSize;
	var str = WLJS_use_ajax(url);
//	alert(url);
	document.getElementById(LayerID).innerHTML = str;
}

function GetDialogPageNewModule(PageId)
{
	$('#WLS_dialog_layer').html('<center>Загрузка...<br/><img src=\'/images/indicators/snake.gif\' border=0/></center>');
	$('#WLS_dialog_layer').dialog({ 
		modal: true, 
		overlay: { opacity: 0.5, background: 'black'},
		title: "Добавление нового модуля на страницу",
		width: 430,
		height: 550,
		resizable: false,
		draggable: false,
		position: "center",
		buttons: {  
			"Отправить": function() {
				if (confirm("Добавить выбранный модуль на страницу?")!=false)
				{
					var index = $("#AddModuleOnPageFormSelectedZone").get(0).selectedIndex;
					if($("#AddModuleOnPageFormSelectedModule").get(0).value!="")
					{
						$.ajax({
							url: "/System/Helper/Module/ModulePageAdd.asp?pid="+PageId+"&mid="+$("#AddModuleOnPageFormSelectedModule").get(0).value+"&z="+$("#AddModuleOnPageFormSelectedZone").get(0)[index].value,
							dataType: "html",
							async: false
						});
					}
					$(this).dialog("close");
					document.location.reload();
				}
		  },  
		  "Закрыть": function() {
			$(this).dialog("close");
		  }  
		}  
	});
	$.ajax({
		url: "/Modules/ModulesList.asp?pid="+PageId,
		dataType: "html",
		async: true,
		complete:function(request){
			$('#WLS_dialog_layer').html(request.responseText);
		}
	});
}

function DialogPageNewModuleClickItem(ItemId)
{
	$("#AddModuleOnPageFormSelectedModule").get(0).value = ItemId;
	$("#ModulesOnPageItemsTable td").each(function(index)
	{
		var $td = $(this);
		if ($td.attr("id") == ItemId)
		{
			try
			{
				($td).removeClass("BO_admin_params_value_name_alt", 500);
				($td).addClass("BO_admin_params_value_name", 500);
			}
			catch(exception)
			{
				
			}
		}
		else if($td.attr("id")!="")
		{
			try
			{
				($td).removeClass("BO_admin_params_value_name", 500);
				($td).addClass("BO_admin_params_value_name_alt", 500);
			}
			catch(exception)
			{
				
			}
		}
	});
}

/*
Диалоговое окно параметров модуля
*/
function GetDialogModuleSettings(Url, Title)
{
	$('#WLS_dialog_layer').html('<center>Загрузка...<br/><img src=\'/images/indicators/snake.gif\' border=0/></center>');
	$('#WLS_dialog_layer').dialog({ 
		modal: true, 
		overlay: { opacity: 0.5, background: 'black'},
		title: Title,
		width: 650,
		height: 550,
		resizable: true,
		draggable: true,
		position: "center",
		buttons:
		{  
			"Сохранить": function()
			{
				$('#ModuleConfigForm').ajaxSubmit(
				{
					target:'#ModuleConfigAlert'
				});
			},  
		  "Закрыть": function() {
			$(this).dialog("close");
		  }  
		}  
	});
	$.ajax({
		url: Url,
		dataType: "html",
		async: true,
		complete:function(request){
			$('#WLS_dialog_layer').html(request.responseText);
		}
	});
}

function GetDialogLogon()
{
	$('#WLS_dialog_layer').html('<center>Загрузка...<br/><img src=\'/images/indicators/snake.gif\' border=0/></center>');
	$.ajax({
		url: "/System/Helper/UserLogon/IsUserActive.asp",
		dataType: "html",
		async: true,
		complete:function(request){
			if (request.responseText == "1")
			{
				$('#WLS_dialog_layer').dialog({ 
					modal: true, 
					overlay: { opacity: 0.5, background: 'black'},
					title: "Авторизация",
					width: 350,
					height: 300,
					resizable: false,
					draggable: false,
					position: "center",
					buttons:
					{
						"Выйти": function()
						{
							$.ajax(
							{
								url: "/System/Helper/UserLogon/EraseUser.asp",
								dataType: "html",
								async: false
							});
							//$(this).dialog("close");
						},
						"Закрыть": function()
						{
							$(this).dialog("close");
						}
					}  
				});
				$.ajax({
					url: "/Modules/UserLogon/UserInfoForm.asp",
					dataType: "html",
					async: true,
					complete:function(request){
						$('#WLS_dialog_layer').html(request.responseText);
					}
				});
			}
			else
			{
				$('#WLS_dialog_layer').dialog({ 
					modal: true, 
					overlay: { opacity: 0.5, background: 'black'},
					title: "Авторизация",
					width: 350,
					height: 300,
					resizable: false,
					draggable: false,
					position: "center",
					buttons:
					{
						"Войти": function()
						{
							$('#WLSUserLogonForm').ajaxSubmit({target: "#LogonFormAlert"});
							//$(this).dialog("close");
						},
						"Закрыть": function()
						{
							$(this).dialog("close");
						}
					} 
				});
				$.ajax({
					url: "/Modules/UserLogon/LogonForm.asp",
					dataType: "html",
					async: true,
					complete:function(request){
						$('#WLS_dialog_layer').html(request.responseText);
					}
				});
			}
		}
	});
}

function GetSimpleDialog(u, t, w, h)
{
	$('#WLS_dialog_layer').html('<center>Загрузка...<br/><img src=\'/images/indicators/snake.gif\' border=0/></center>');
	$('#WLS_dialog_layer').dialog({ 
		modal: true, 
		overlay: { opacity: 0.5, background: 'black'},
		title: t,
		width: w,
		height: h,
		resizable: false,
		draggable: false,
		position: "center",
		close: function(ev, ui) { $(this).remove(); },
		buttons: {  
		  "Закрыть": function() {
			$(this).dialog("close");
			}  
		}  
	});
	$.ajax({
		url: u,
		dataType: "html",
		async: true,
		complete:function(request){
			$('#WLS_dialog_layer').html(request.responseText);
		}
	});
}

function DeleteModuleFromPage(mid)
{
	if(confirm('Удалить модуль со страницы?'))
	{
		$.ajax({
			url: "/System/Helper/Module/ModulePageDelete.asp?mid="+mid,
			dataType: "html",
			async: false,
			complete:function(request){
			alert("Модуль удален со страницы.");
			document.location.reload();
		}
		});
	}
}
