globSelIdx = 0;
globScrl = 0;
toggleNewMessGB = 0;
toggleAddInfoReg = 0;
pTime = 0;
pTimeSearch = 0;
globId = 0;
globOrgMenu = '';
globAnsw = 0;
var Letters=new Array('%C0','%C1','%C2','%C3','%C4','%C5','%C6','%C7','%C8','%C9','%CA','%CB','%CC','%CD','%CE','%CF','%D0','%D1','%D2','%D3','%D4','%D5','%D6','%D7','%D8','%D9','%DA','%DB','%DC','%DD','%DE','%DF','%E0','%E1','%E2','%E3','%E4','%E5','%E6','%E7','%E8','%E9','%EA','%EB','%EC','%ED','%EE','%EF','%F0','%F1','%F2','%F3','%F4','%F5','%F6','%F7','%F8','%F9','%FA','%FB','%FC','%FD','%FE','%FF','%A8','%B8');

function EscapeToWin(AStr){
var Result='';
for(var i=0;i<AStr.length;i++)
if(AStr.charAt(i)>='А' && AStr.charAt(i)<='я')
Result+=Letters[AStr.charCodeAt(i)-0x0410];
else if(AStr.charAt(i)=='Ё')
Result+=Letters[64];
else if(AStr.charAt(i)=='ё')
Result+=Letters[65];
else if(AStr.charAt(i)=='=')
Result+='%3D';
else if(AStr.charAt(i)=='&')
Result+='%26';
else
Result+=AStr.charAt(i);
return Result;
}//EscapeToWin

function scrollBack()
{
	window.scrollBy(0, globScrl);
}
function getWidth()
{
	var width = document.body.offsetWidth;
	if (width < 800)
		width = 800;
	return width;
}
function eraseQuote()
{
	$("#quoteMessGB").attr("value", 0);
	var str = "Вы никого не цитируете";
	$("#showQuote").text("");
	$("#showQuote").text(str);
	setTimeout("$('#showQuote').slideUp()", 1000);
//	clearTimeout(pTime);
}
function setWidthLastImageOnTop()
{
	var w = 608 + getWidth()-800;
	$("#topTable").css("width", getWidth());
	$("#lastImgOnTop").css("width", w);
}
function HideAllSubMenues(out)
{
	$("div.topSubMenues").each(function()
			{
				if($(this).attr("id") != out)
					$(this).hide();
			}
		)

}
function getCaretPos(obj)
{
  obj.focus();
  
  if(obj.selectionStart) return obj.selectionStart;//Gecko
  else if (document.selection)//IE
  {
    var sel = document.selection.createRange();
    var clone = sel.duplicate();
    sel.collapse(true);
    clone.moveToElementText(obj);
    clone.setEndPoint('EndToEnd', sel);
    return clone.text.length;
  }
  
  return 0;
}
function mousePageXY(e) 
{
	var x = 0, y = 0;
	var coord=new Array(); 
  if (!e) e = window.event; 
  if (e.pageX || e.pageY) 
  { x = e.pageX; 
    y = e.pageY; 
  } 
  else if (e.clientX || e.clientY) 
  { x = e.clientX + (document.documentElement.scrollLeft || 
document.body.scrollLeft) - document.documentElement.clientLeft; 
    y = e.clientY + (document.documentElement.scrollTop || 
document.body.scrollTop) - document.documentElement.clientTop; 
  } 
  coord.x=x;coord.y=y; 
  alert(coord);
  return coord; 
} 
function getAbsolutePos(el)
{
   var r = { x: el.offsetLeft, y: el.offsetTop };
   if (el.offsetParent)
   {
       var tmp = getAbsolutePos(el.offsetParent);
       r.x += tmp.x;
       r.y += tmp.y;
   }
   return r;
}
function printElement(element, index, ar)
{
    alert('Element ' + index + ' = ' + element);
}

function onSucc(data)
{
	alert(data);
	eval(data);
//	data.forEach(printElement);
}
function onSuccLoc(data)
{
//	alert(data);
//	data.forEach(printElement);
	$("#showLocDescrip > p").text(data);
	$("#showLocDescrip").show('slow');
	$("#loadImageDiv").hide();
}
function onSuccMarq(data)
{
	$("#marqText").attr('value',data);
}
function onSuccInfoOrg(data)
{
	$("#showInfoOrg").html(data);
}
function onSuccRubrics(data)
{
	$("#srubrica2").html(data);
}
function onSuccRubrics2(data)
{
	$("#childChCatalog").html(data);
}
function onSuccRubricsWithSel(data)
{
	$("#srubrica2").html(data);
	$("form select[name='srid'] option[value="+globSelIdx+"]").attr('selected','selected');
}
function onSuccEditOrg(data)
{
//	var ar = eval(data);
//	alert(data);
	$("form[name=editOrgForm]").html(data);
}
function onSuccVoteAdd(data)
{
//	alert(data);
	$("#archiveVotes").html(data);
}
function onSuccAnswVoteAdd(data)
{
	alert(data);
}
$(function(){
	globAnsw = 0;
	$("input[name=answ]").change(function()
		{
			globAnsw = $(this).val();
		}
	);
	$("#sendAnsw2Vote").click(function()
		{
			var idvote = $("form[name=answ2Vote] > input[name=id]").val();
			var ans = globAnsw;
			if (ans == 0)
			{
				alert('Вы не выбрали вариант ответа');
				return;
			}
			else
			{
				$.post('/voteanswadd',
					{
						id: idvote,
						num_answ: ans
					}, onSuccAnswVoteAdd);
			}
		}
	);
	$("#saveVoteQuestion").click(function()
		{
			$.post('/voteadd',
					{
						question: $("textarea[name=question]").val(),
						answ1: $("input[name=answ1]").val(),
						answ2: $("input[name=answ2]").val(),
						answ3: $("input[name=answ3]").val(),
						answ4: $("input[name=answ4]").val(),
						answ5: $("input[name=answ5]").val()
					}, onSuccVoteAdd);
		}
	);
	$("select[name=col_vars_answ]").change(function()
		{
			var i = $(this).val();
			var j = 1;
			for (j = 1; j <= i; j ++)
			{
				$("input[name=answ"+j+"]").show();
			}
			j = parseInt(i)+1;
			for (j; j <= 5; j ++)
			{
				$("input[name=answ"+j+"]").attr('value','');
				$("input[name=answ"+j+"]").hide();
			}
		}
	);
	$("#showEditOrgForm").click(function()
		{
			var idorg = $(this).attr('idorg');
			$("#idEditClient").attr("value", idorg);
			$.post('/basegeteditorg',
					{
						id: idorg
					}, onSuccEditOrg);
			$("#editOrgForm").show();
		}
	);
	$("#checkParent").change(function()
		{
			if ($(this).attr("checked"))
				$("#selParentAddPanel").attr("disabled","disabled");
			else
				$("#selParentAddPanel").removeAttr("disabled");
		}
	);
	$("font.moderRubric, font.moderSubRubric, font.moderOrg").hover(function()
		{
			$(this).css('cursor','pointer');
		}
	)
	$("font.moderRubric, font.moderSubRubric").click(function()
		{
			var subdiv = $(this).attr('subdiv');
			$("#"+subdiv).toggle();
		}
	)
	$("#showInfoOrg").hover(function()
		{
			$('td.showorgmenu').each(function()
				{
					if ($(this).attr('oper') == globOrgMenu)
					{
						$(this).removeClass('showorgmenu');
						$(this).addClass('showorgmenuligth');
					}
				}
			);
		}
	);
	$("td.showorgmenu").hover(function()
		{
			$('td.showorgmenuligth').each(function()
				{
					$(this).removeClass('showorgmenuligth');
					$(this).addClass('showorgmenu');
				}
			);
			$(this).removeClass('showorgmenu');
			$(this).addClass('showorgmenuligth');
			globOrgMenu = $(this).attr('oper');
			var id = $(this).attr('id');
			$.post('/baseinfoorg',
						{
							action: globOrgMenu,
							pid: id
						}, onSuccInfoOrg);
		},
		function()
		{
			globOrgMenu = $(this).attr('oper');
//			$(this).removeClass('showorgmenuligth');
//			$(this).addClass('showorgmenu');
		}
	);
	$("a.chldiv").click(function()
		{
			var subdiv = $(this).attr("childiv");
			$("div.hiddenchild").each(function()
				{
					if ($(this).attr('id') != subdiv)
						$(this).hide('fast');
				}
			);
			globScrl = document.body.scrollTop;
			$("#"+subdiv).toggle('fast', scrollBack);
			if ($(this).attr('oper') == 'show')
			{
				$(this).attr('oper','hide');
				$('img',this).attr('src','/_img/minus_white.png');
			}
			else
			{
				$(this).attr('oper','show');
				$('img',this).attr('src','/_img/plus_white.png');
			}
		}
	);
	$("#editMarquee").click(function()
		{
			var t = $("#marqText").attr('value');
			//t = EscapeToWin(t);
			//alert(t);

			$.post('/updatemarquee',
					{
						txt: t
					}, onSuccMarq);
		}
	);
	$("#parentChCatalog").change(function()
		{
			var id = $(this).val();
			$.post('/getrubrics',
					{
						pid: id
					}, onSuccRubrics2);
		}
	);
	$("#frubrica2").change(function()
		{
			var id = $(this).val();
			$.post('/getrubrics',
					{
						pid: id
					}, onSuccRubrics);
		}
	);
	$("a.openCloseWnd").click(function()
		{
			var wndid = $(this).attr("wndid");
			if ($(this).attr("oper") == 'hide')
			{
				$("#"+wndid).hide();
				switch (wndid)
				{
					case 'regInSystem':
						$(this).attr("oper","show");
						break;
				}
			}
			else if ($(this).attr("oper") == 'show')
			{
				$("#"+wndid).show();
				switch (wndid)
				{
					case 'regInSystem':
						$(this).attr("oper","hide");
						break;
					case 'editOrgForm':
					var idorg = $(this).attr('idorg');
//					$("#idEditClient").attr("value", idorg);
					$.post('/basegeteditorg',
							{
								id: idorg
							}, onSuccEditOrg);
						break;
					case 'editOnlyCatalog':
						$("form[name=editOnlyCat] > input[name=id]").attr("value",$(this).attr("idorg"));
						break;
				}
			}
		}
	);
});
