// New JS Library for NewLeaf CMS by IT-Security
// Add JQuery support
// Old content
	var old_content = Array();
// Load content editor
	function load_editor (id, type)
		{
			 $.ajax({
   				type: "GET",
   				url: "/index.php?mod=ajax&submod=content",
      				data: "id="+id+"&action=view&type="+type,
      				success: function(output){
         			if (!old_content[id] || old_content[id] == '')
         				{
         					old_content['content_'+id] = $("#content_"+id).attr('innerHTML');
         				}
				$("#content_"+id).attr('innerHTML', output);
				var div = document.getElementById("content_"+id);
   				var x = div.getElementsByTagName("script"); 
   				for(var i=0;i<x.length;i++)
   					{
		       				eval(x[i].text);
   					}
      				}
    			});
		}
// Save content
	function save_content (id, type, title, body)
		{
			tinyMCE.activeEditor.setProgressState(1);
			 $.ajax({
   				type: "POST",
   				url: "/index.php?mod=ajax&submod=content",
      				data: "id="+id+"&action=save_content&type_story="+type+"&title="+title+"&story="+encodeURIComponent(body),
      				success: function(output){
         				$("#content_"+id).attr('innerHTML', output);	
      				}
    			});
		}
// Cancel edit
	function cancel_edit (content_id)
		{
			if (old_content[content_id] != "")
				{
					$('#'+content_id).attr('innerHTML', old_content[content_id]);
				}
			return false;
		}
// URL redir confirm
	function confirm_redir (msg, url)
		{
			var agree = confirm(msg);
			if (agree)
				{
					parent.location = url;
				}
		}
// Select/Deselect
	function selectIt (id) {
		deselectIt();
		if (document.selection) {
			var range = document.body.createTextRange();
			range.moveToElementText(document.getElementById(id));
			range.select();
		} else if (window.getSelection) {
			var range = document.createRange();
			range.selectNode(document.getElementById(id));
			window.getSelection().addRange(range);
		}
	}
	function deselectIt () {
		if (document.selection)
			document.selection.empty();
		else if (window.getSelection)
			window.getSelection().removeAllRanges();
	}
// Hide element
	function hide (id) {
		var new_id = '#'+id;
		return $(new_id).css('display', 'none');
	}
// Show element
	function show (id) {
		var new_id = '#'+id;
		return $(new_id).css('display', 'block');
	}
// Switch language
	function switch_language (lang) {
		if (lang == 'en')
			{
				hide('ru_area1');
				hide('ru_area2');
				show('en_area1');
				show('en_area2');
			}
		else
			{
				show('ru_area1');
				show('ru_area2');
				hide('en_area1');
				hide('en_area2');
			}
	}
// Switch settings
	function switch_settings (setting)
		{
			if (setting == 'meta')
				{
					show('meta');
					hide('service');
					hide('news');
					hide('users');
					hide('mainpage');
				}
			else if (setting == 'mainpage')
				{
					show('mainpage');
					hide('service');
					hide('meta');
					hide('news');
					hide('users');
				}
			else if (setting == 'news')
				{
					show('news');
					hide('service');
					hide('meta');
					hide('users');
					hide('mainpage');
				}
			else if (setting == 'users')
				{
					show('users');
					hide('service');
					hide('meta');
					hide('news');
					hide('mainpage');
				}
			else if (setting == 'service')
				{
					show('service');
					hide('meta');
					hide('news');
					hide('users');
					hide('mainpage');
				}
		}
// Check user
	function check_login (login)
		{
			if (login == '')
			{
					$("#result_log").attr('innerHTML', '<span style="color: red;">Не указан логин</span>');
      }
			else
			{
			 		$.ajax({
   						type: "POST",
   						url: "/index.php?mod=ajax&submod=register",
		      				data: "action=is_register&uname="+login,
      						success: function(output){
							if (output == 0)
								{
		         						$("#result_log").attr('innerHTML', '<span style="color: green;">ok</span>');
								}
							else
								{
		         						$("#result_log").attr('innerHTML', '<span style="color: red;">Логин занят</span>');
								}
						}
    					});
				}
		}
//check email
  function isValidEmail (email, strict)
  {
    if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
    return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
  }
  
	function check_email (email)
		{
			if (email == '')
			{
					$("#result_email").attr('innerHTML', '<span style="color: red;">Не указан E-mail</span>');
      }
			else
			{
			 		if(isValidEmail(email)) $("#result_email").attr('innerHTML', '<span style="color: green;">ok</span>');
			 		else $("#result_email").attr('innerHTML', '<span style="color: red;">E-mail указан не верно</span>');
			}
		}
//chack pass
	function check_pass (pass,repass,num)
		{
      if(num == 1){
        if (pass == '')
        {
          $("#result_pass1").attr('innerHTML', '<span style="color: red;">Пароль не может быть пустым</span>');
        }else if (pass.length<3)
        {
          $("#result_pass1").attr('innerHTML', '<span style="color: red;">Пароль не может быть короче 3х символов</span>');
        }else{
          $("#result_pass1").attr('innerHTML', '<span style="color: green;">ok</span>');
        }
      }else if(num == 2){
        if (repass == '')
        {
          $("#result_pass2").attr('innerHTML', '<span style="color: red;">Повторите пароль!</span>');
        }else if (pass!=repass)
        {
          $("#result_pass2").attr('innerHTML', '<span style="color: red;">Пароли должны совпадать.</span>');
        }else{
          $("#result_pass2").attr('innerHTML', '<span style="color: green;">ok</span>');
        }
      }      
		}
//register
  function send_reg(login,passw,passw2,mail,captcha)
  {
   
   $.ajax({
   						type: "POST",
   						url: "/index.php?mod=ajax&submod=register",
		      				data: "action=register&login="+login+"&passw="+passw+"&passw2="+passw2+"&mail="+mail+"&captcha="+captcha,
      						success: function(output){
							//if (output == 0)
							//	{
		         	//					$("#regdiv").attr('innerHTML', '');
								//}
							//else
								//{
		         						$("#regdiv").attr('innerHTML', output + "<hr>" + $('#regdiv').attr('innerHTML'));
								//}
						}
    					}); 
  
  }
// Update captcha
	function update_captcha()
		{
			$("#sec_img").attr("innerHTML", "<img src=\"/modules/antibot.php?"+Math.random()+"\" heigth=\"60\" width=\"120\"><br><small><a href=\"#\" onclick=\"update_captcha();return false;\">обновить</a></small>");
		}
		
 // для создания вкладок;		
		function switchCell(n)  {
     
  for(i=0;i<7;i++)  
  {
  	if(n!=i){
	$('#navcell'+i).attr("className","tab-off");
  	$('#tb'+i).hide();
  }}
  $('#navcell'+n).attr("className","tab-on");
 // $('#tb'+n).show('fade');
  $('#tb'+n).fadeIn("slow");
  
 
//  tb_content.height=screen.availHeight;
  }
	// Новый скин
		function new_skin ()
			{
				var skin_name = $("#new_skin").attr('value');
				// Создаём
		 		$.ajax({
						type: "GET",
						url: "/index.php?mod=ajax&submod=templates",
	      				data: "action=new&skin_name="+skin_name,
  						success: function(output){
						if (output == 'false|exist')
							{
	         					alert('Такой скин уже существует!');
							}
						else if (output == 'false|none')
							{
								alert('Название не указано или содержит некорректные символы');
							}
						else
							{
	         					alert('Скин успешно создан!');
	         					$("#new_skin_layout").hide('slow');
	         					window.location.reload();
							}
		 			}
		 		});
			}
	// Новый шаблон
		function new_template (skin_name)
			{
				var template = $("#new_template").attr('value');
				// Создаём
		 		$.ajax({
						type: "GET",
						url: "/index.php?mod=ajax&submod=templates",
	      				data: "action=new_template&skin_name="+skin_name+"&template_name="+template,
  						success: function(output){
						if (output == 'false|exist')
							{
	         					alert('Такой шаблон уже существует!');
							}
						else if (output == 'false|none')
							{
								alert('Название не указано или содержит некорректные символы');
							}
						else
							{
	         					alert('Шаблон успешно создан!');
	         					$("#new_template_layout").hide('slow');
	         					window.location.reload();
							}
		 			}
		 		});
		 		
			}
// Переименовываем
		function rename_skin ()
			{
				var old_name = $("#old_name").attr('value');
				var skin_name = $("#rename_skin").attr('value');
				// Создаём
		 		$.ajax({
						type: "GET",
						url: "/index.php?mod=ajax&submod=templates",
	      				data: "action=rename&skin_name="+old_name+"&new_skin_name="+skin_name,
  						success: function(output){
						if (output == 'false|exist')
							{
	         					alert('Такой скин уже существует!');
							}
						else if (output == 'false|none')
						{
							alert('Название не указано или содержит некорректные символы');
						}
						else if (output == 'false|noexist')
						{
							alert('Такого скина не существует');
						}
						else
							{
	         					alert('Скин успешно переименован!');
	         					$("#rename_skin_layout").hide('slow');
	         					window.location.reload();
							}
		 			}
		 		});
			}
// Удаляем скин
		function del_skin (skin)
			{
				var agree = confirm("Вы уверены, что хотите удалить этот скин?");
				if (agree)
					{
						$.ajax({
								type: "GET",
									url: "/index.php?mod=ajax&submod=templates",
									data: "action=delete&skin_name="+skin,
									success: function(output){
							if (output == 'false|noexist')
								{
	         						alert('Такого скина не существует!');
								}
							else if (output == 'false|none')
								{
									alert('Название не указано или содержит некорректные символы');
								}
							else if (output == 'false|main_skin')
								{
									alert('Этот шаблон нельзя удалять');
								}
							else
									{
									alert('Скин успешно удалён!');
	         						window.location.reload();
								}
							}
						});
					}
			}
	function send_vote()
    {
       $.ajax({
          type: "GET",
          url: "/index.php?mod=ajax&submod=vote",
          data: "val="+radio_val,
          success: function(output)
            {
              $("#vote_content").attr('innerHTML', output);
            }
      });
    }
// Открыть календарь
			function open_calendar(el)
      {
        var x = 0;
        var y = 0;

        if(!window.Event){
          x=event.screenX;
          y=event.screenY;
        }
        
        if( (x+230) > window.screen.width) x = window.screen.width-250;
        if( (y+180) > window.screen.height) y = window.screen.height-220;

        window.open('/include/libs/calendar.php?el='+el,'Calendar','width=280,height=200,noresize,top='+y+',left='+x);
      }