$(document).ready(function() {
  
});

//Добавить в Избранное 
function add_favorite(a) { 
  title=document.title; 
  url=document.location; 
  try { 
    // Internet Explorer 
    window.external.AddFavorite(url, title); 
  } 
  catch (e) { 
    try { 
      // Mozilla 
      window.sidebar.addPanel(title, url, ""); 
    } 
    catch (e) { 
      // Opera 
      if (typeof(opera)=="object") { 
        a.rel="sidebar"; 
        a.title=title; 
        a.url=url; 
        return true; 
      } 
      else { 
        // Unknown 
        alert('Нажмите Ctrl-D чтобы добавить страницу в закладки'); 
      } 
    } 
  } 
  return false; 
}


function showRegister() {
	
	if ( ! $('body #fade').length ) $("body").append('<div id="fade"></div>');
		
	if ( ! $('body #registerForm').length ) $("body").append('<div id="registerForm"></div>');
	
	body_height = $(window).height();
	
	var fade = $("body #fade");
	fade.css("height", body_height + 'px');
	fade.fadeTo(0, 0.7);
	fade.show();
	
	$("#registerForm").show();
                               
    window.scrollTo(0, 0); 
    
    var registerForm = $("#registerForm");
	registerForm.css('width', 500);
	registerForm.css('height', 500);
	
	var ajax = new dle_ajax();
	ajax.requestFile = dle_root + "engine/ajax/reg/regform.php";
	ajax.method = 'POST';
	ajax.element = 'registerForm';
	ajax.execute = true;
	ajax.onCompletion = whenCompletedSave;
	ajax.sendAJAX('');
	return false;
}

function regСlose() {
	$("body #fade").hide();
	$("body #registerForm").hide();
}

function createTempId() {
	/* DLE bug-fix */
	if (!document.getElementById('rnd4')) {
		var div = document.createElement('div');
		div.setAttribute('id', 'rnd4');
		div.setAttribute('style','position: absolute;top:0px;width:1px;display:none');
		document.getElementById('login-panel').appendChild(div);
	}
	return 'rnd4';
}
function regSubmit() {
	
	var name = document.forms.registration.name.value;
	var password1 = document.forms.registration.password1.value;
	var password2 = document.forms.registration.password2.value;
	var email = document.forms.registration.email.value;
	var sec_code = document.forms.registration.sec_code.value;
	
	var ajax = new dle_ajax();
	ajax.setVar("name", ajax.encodeVAR( name ) );
	ajax.setVar("password1", ajax.encodeVAR( password1 ) );
	ajax.setVar("password2", ajax.encodeVAR( password2 ) );
	ajax.setVar("email", ajax.encodeVAR( email ) );
	ajax.setVar("sec_code", sec_code);
	ajax.requestFile = dle_root + "engine/ajax/reg/regsubmit.php";
	ajax.method = 'POST';
	ajax.element = createTempId();
	ajax.execute = true;
	ajax.onCompletion = whenCompletedSave;
	ajax.sendAJAX('');
	return false;
	
}

function my_check_reg() {
	if(document.forms.registration.name.value == '') {
		alert('Имя пользователя не может быть пустым!');return false;
	}
	if(document.forms.registration.password1.value.length < 6) {
		alert('Длина пароля должна быть не менее 6 символов!');return false;
	}
	if(document.forms.registration.password1.value != document.forms.registration.password2.value) {
		alert('Оба введенных пароля должны быть идентичны!');return false;
	}
	if(document.forms.registration.email.value == '') {
		alert('Введен неверный E-Mail адрес!');return false;
	}
	if (!(/[-!#$%&\'*+\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\./0-9=?A-Z^_`a-z{|}~]+/.test(document.forms.registration.email.value))) {
		alert('Введен неверный E-Mail адрес!');return false;
	}
	regSubmit();
	
	return false;
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}

function reloadPage(){
	window.location.reload();
}

function processResult() {
	var strBack = '<div align="center"><input type="button" value="Вернуться назад" /></div>';
	
	if (ajax_status == 'OK' ) {
		$('#r-in').html( ajax_conent );
		window.setInterval("reloadPage()", 700);
	} else {
		alert( ajax_conent );
	}
}

function change_city_state() {
	var obj = document.getElementById('city_id');
	if ( ! obj ) return;
	//alert(obj.options.length);
	/*if ( obj.options.length == 1 || obj.options.length == 0 ) {
		obj.disabled = true;
	} else {
		obj.disabled = false;
	}*/
}
function getCities( obj ) {
	var ajax = new dle_ajax();
	ajax.setVar("region_id", obj.value);
	ajax.requestFile = dle_root + "engine/ajax/get_region.php";
	ajax.method = 'POST';
	/*ajax.onDone = change_city_state;*/
	ajax.element = 'city_id';
	ajax.sendAJAX('');
	return false;	
}

function delImage( img_id ) {
	var ajax = new dle_ajax();
	ajax.setVar("id", img_id);
	ajax.requestFile = dle_root + "engine/ajax/doska_delete_image.php";
	ajax.method = 'POST';
	ajax.element = 'images-table';
	ajax.sendAJAX('');
	return false;	
}

function delImageDog( img_id ) {
	var ajax = new dle_ajax();
	ajax.setVar("id", img_id);
	ajax.requestFile = dle_root + "engine/ajax/profile_dog_delete_img.php";
	ajax.method = 'POST';
	ajax.element = 'images-table';
	ajax.sendAJAX('');
	return false;	
}

function delBlog( blog_id ) {
	var agree = confirm(dle_del_agree);
	if (agree) {
		document.location = dle_root + 'blogs/delete/' + blog_id + "/" + dle_login_hash; 
	}
}

function blogRate( rate, id ) {
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "go_rate=" + rate;
	ajax.setVar("news_id", id);
	ajax.setVar("skin", dle_skin);
	ajax.setVar("mode", "short");
	ajax.requestFile = dle_root + "engine/ajax/blogs_rating.php";
	ajax.method = 'GET';
	ajax.element = 'ratig-layer-' + id;
	ajax.sendAJAX(varsString);
};
