/**
 *	PineTree CMS *tm
 *	@author	Kirill I Sosnin kirill@sosnin.spb.ru
 *
 *	License: GPL
 *
 */
dom = (document.getElementById) ? (true) : (false);
ie = (document.all) ? (true) : (false);
ie4 = ie && !dom;
nn4 =(document.layers) ? (true) : (false);

$(document).ready(function(){
	if($(".date_input").length)
	{
		$(".date_input").date_input();
	}

    var regexImei=/^[0-9]{15}$/;

    var errorImei = "Неверный формат IMEI. IMEI должен состоять из 15 цифр";

    $.getDocHeight = function(){
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
	    );
	};

	$("#imei")
             .focus(function () {
                    if($(this).val()=='IMEI') $(this).val("");
             })
             .blur(function() {
                    if($(this).val()=='') $(this).val("IMEI");
             });

	$("#imei_add").click(function(e){
		if($('#imei').val()!="" && $('#imei').val().length==15 && regexImei.test($('#imei').val()) && $('#imei').val()!="IMEI")
		{
			$.post("/blacklist/add.php", { imei:$('#imei').val(),rand:Math.random() } ,function(data) {
				if(data=='1') {
					// ok
					jMsg('Спасибо! IMEI '+$('#imei').val()+' успешно добавлен', 'Спасибо', function(r) {
						if (r == true) {
							window.location='/add_stolen_phone/?imei='+$('#imei').val();
						}
						});

				}
				else
				{
					jAlert(data,'Ошибка');
				}
			});
		}
		else
			jAlert(errorImei,'Ошибка');

		return false;
	});

	$("#check").click(function(e){
		var overlay = $("#overlay");
		var realH = $.getDocHeight();
		var w = document.body.scrollWidth + 'px';
		var h = realH + 'px';
		overlay.css('width', w);
		overlay.css('height', h);
		overlay.css('display','block');
		//overlay.css('top', h-$.getDocHeight());

		e.preventDefault();

		if($('#imei').val()!="" && $('#imei').val().length==15 && regexImei.test($('#imei').val()))
		{
 			$("#check_result").removeClass().addClass('process').text('Проверяется IMEI '+$('#imei').val()+'...').fadeIn(1000);
 			$("#check_box").show();
			//$("#check_result").html('Проверяется..');
	        $.post("/blacklist/check.php", { imei:$('#imei').val(),rand:Math.random() } ,function(data) {

				//$("#check").html('').removeClass();


				if(data!='') {
					//$("#check_result").fadeTo(200,0,function(){						//$(this).html(data).removeClass().addClass('none').fadeTo(900,1);
						//$("#check_box").css('display','none');
						$("#check_box").hide();
						jMsg(data, 'Сообщение');

					//});
				}
				else
				{
					//$("#check_result").fadeTo(200,0,function(){
					//	$(this).html('IMEI '+$('#imei').val()+' <b>отсутствует</b> в Черном Списке').removeClass().addClass('success').fadeTo(900,1);
						$("#check_box").hide();
					jMsg('IMEI '+$('#imei').val()+' <b>отсутствует</b> в Черном Списке', 'Сообщение', function(r) {
					});
					//});
				}
			});
		}
		else
		{
				jAlert(errorImei,'Ошибка');


					/*$("#check_result").fadeTo(200,0.1,function(){
						$(this).html(errorImei).removeClass().addClass('error').fadeTo(900,1);
					});*/
		}


		return false;
	});

	$("#close_check_btn").click(function(e){
		$("#overlay").css('display','none');
		$("#check_box").css('display','none');
	});

	$(".login_link").click(function(e){

		$("#login_box").css('display','block');
		e.preventDefault();

	});

	$("#close_btn").click(function(e){
		$("#overlay").css('display','none');
		$("#login_box").css('display','none');
		e.preventDefault();
	});
});



function popup(file, query)
{
window.open(file+'?'+query,'popup', 'width=640,height=640,screenX=210,screenY=200,top=100,left=100,copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=yes');
}

function popupCat(file)
{
	window.open('/video/'+file+'/'+file+'.html','popup', 'width=350,height=320,screenX=210,screenY=200,top=100,left=100,copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=yes');
	return false;
}


function showItem(id)
{
window.open('/includes/misc/popup_item.php?id='+id,'popup', 'width=400,height=400,screenX=210,screenY=200,top=100,left=100,copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=yes');
}

function cat_show_hide(root, form_id, level) {
	var root_form_id;

	root_form_id = "cat"+root;

	if(dom) { root_div = document.getElementById(root_form_id); div = document.getElementById(form_id);
		ul_root_div = document.getElementById("li_"+form_id); a_root_div = document.getElementById("a_"+form_id);}
	else if(ie4) {
		root_div = document.all[root_form_id];
		div = document.all[form_id];
		ul_root_div = document.all["li_"+form_id];
		a_root_div = document.all["a_"+form_id];
	}
	else if(nn4) {
		root_div = document.layers[root_form_id];
		div = document.layers[form_id];
		ul_root_div = document.layers["li_"+form_id];
		a_root_div = document.layers["a_"+form_id];
	}
	else
		return false;

	if(div)
	{
		if(div.style.display == "block")
		{

			div.style.display = "none";
			if(ul_root_div)
			{
				//ul_root_div.style.backgroundImage = "url(/images/triang.gif)";
				a_root_div.style.color = "#FA7012";
				//a_root_div.style.fontWeight = "normal";
				//a_root_div.style.textDecoration = "underline";
			}
		}
		else
		{
			div.style.display = "block";
			if(ul_root_div)
			{
				//window.scrollTo(0,600);
				//ul_root_div.style.backgroundImage = "url(/images/triang_a.gif)";
				a_root_div.style.color = "#4BADFD";
				//a_root_div.style.fontWeight = "bold";
				//a_root_div.style.textDecoration = "none";
				hide_others(root, form_id, level);
			}
		}
	}
//else
//	alert('К данной странице нельзя добавить отзыв');
	return false;
}

function hide_others(cur_id, form_id, level) {
	var _form_id;
	var div;
	var ul_root_div;
	var a_root_div;
	if(level==0)
	{
		 for (var i=1; i<50; i++)
		 {
			_form_id = "cat"+ i;
			div = document.getElementById(_form_id);
			if(!div)
			{
				_form_id = "items"+ i;
				div = document.getElementById(_form_id);
			}

				if(div && _form_id!=form_id && _form_id!="cat1")
				{
					div.style.display = "none";
					ul_root_div = document.getElementById("li_cat"+i);
					if(ul_root_div)
					//ul_root_div.style.backgroundImage = "url(/images/triang.gif)";

					a_root_div = document.getElementById("a_cat"+i);
					if(a_root_div)
					{
						a_root_div.style.color = "#FA7012";
						a_root_div.style.fontWeight = "normal";
						//a_root_div.style.textDecoration = "underline";
					}
				}
		}
	}
	return false;
}



function show_hide(form_id) {
if(document.getElementById(form_id))
{
	if(document.getElementById(form_id).style.display == "block")
	{
		document.getElementById(form_id).style.display = "none";
	}
	else
	{
		document.getElementById(form_id).style.display = "block";
		document.getElementById(form_id).style.background = "url(/images/logo.gif)";
	}
}
//else
//	alert('К данной странице нельзя добавить отзыв');
}


function show_picture(gid, id, f, c, w, h, color, title)
{ var size="width="+w+",height="+h; picture = window.open("/includes/misc/popup_img.php?gid="+gid+"&id="+id+'&f='+f+"&c="+c+"&col="+color+"&tit="+title, "picture", "dependent=yes,status=yes,directories=no,menubar=no,scrollbars=yes,resizable=yes,"+size);
}
function show_cat_picture(gid, id, f, c, w, h, color, title)
{ var size="width="+w+",height="+h; picture = window.open("/includes/misc/popup_cat_img.php?gid="+gid+"&id="+id+'&f='+f+"&c="+c+"&col="+color+"&tit="+title, "picture", "dependent=yes,status=yes,directories=no,menubar=no,scrollbars=yes,resizable=yes,"+size);
}


function checkEmail(mail)
{
	var mailstr= new String(mail);
	var indexOfDog = mailstr.indexOf("@");
	var indexOfPeriod = mailstr.indexOf(".");
	var indexOfSpace = mailstr.indexOf(" ");
    var regex5=/[^a-zA-Z_0-9]/;
    var regex7=/[а-я]/;
    var regex8=/[А-Я]/;
    var error_message = "";

	if(indexOfDog==-1){
		error_message+="\nEmail не полон, пропущена @";
		error=1;
	}

	if(indexOfDog==0){
		error_message+="\nEmail не полон, перед @ ничего нет";
		error=1;
	}

	if(indexOfSpace!=-1){
		error_message+="\nEmail не должен содержать пробелов";
		error=1;
	}

	if(indexOfPeriod==-1){
		error_message+="\nEmail не полон, пропущена точка";
		error=1;
	}

	if(regex7.test(mail)||regex8.test(mail)||regex5.test(mail))
	{
		error_message+="\nEmail может содержать только цифры и английские буквы";
		error=1;
	}

	return error_message;
}