function quotemessage(nickname) {
	var quote = ''
	if (window.getSelection) {
		quote = window.getSelection().toString()
	}
	else if (document.selection) {
		quote = document.selection.createRange().text
	}
	else if (document.getSelection) {
		quote = document.getSelection()
	}
	else {
		return false
	}

	if (quote.length > 0) { 
		document.getElementById('id_text').value += "\r\n[quote" + ( (nickname && nickname != "") ? "=" + nickname : "" ) + "]" + quote + "[/quote]\r\n";
		window.location.href = "#reply";
	} else { 
	 	alert('Для использования функции цитирования следует выделить мышкой цитируемый текст');
	}
}

function showHelper(helpid) {
	helper = document.getElementById(helpid);
	if (helper.style.display == 'none') {
		helper.style.display = '';
	} else {
		helper.style.display = 'none';
	}
}

function Redraw(butid) {
	button = document.getElementById(butid);
	if (button.parentNode.parentNode.className == "help-cont-passive") {	
		button.parentNode.parentNode.className = 'help-cont-active';
		button.parentNode.className = 'help but-active';
	} else {	
		button.parentNode.parentNode.className = 'help-cont-passive';
		button.parentNode.className = 'help but-passive';
	}
}

function fisherYates(arr) {
	var arr = arr.slice(0, arr.length)
	var i = arr.length;
	if (i == 0) return false;
	while (--i) {
		var j = Math.floor(Math.random() * ( i + 1 ));
		var tempi = arr[i];
		var tempj = arr[j];
		arr[i] = tempj;
		arr[j] = tempi;
	}
	return arr;
}

$('#ad').ready(function() {
	$.getJSON('/ad/ad.json', function(data) {
		$.each(fisherYates(data).slice(0, 3), function (key, line) {
			$('.content-bottom #ad').append('<div class="text-right fleft w25"><a href="' + line[1] + '">' + line[0] + '</a>'+'<p>' + line[2] + '</p></div>');
			$('.inner-left #ad').append('<div class="text-right"><a href="' + line[1] + '">' + line[0] + '</a>'+'<p>' + line[2] + '</p></div>');
		});
	});
});

$(document).ready(function(){
	$("#id_mail_form").submit(function(){
		var email = $("#id_user_field").val();	
		var hostname = email.split('@')[1];
		if (hostname) {
			var action = "http://mail." + hostname + "/";
			$(this).attr('action', action);
		}
	})
	checkPartnerLink();
})


function checkPartnerLink() {
    var re = /^#p_(\w+)$/;
    var result = re.exec(location.hash);
    if (result) {
        var partnerIdentifier = result[1];
        var url = "https://panel.netangels.ru/register/mark_client/" + partnerIdentifier + "/?callback=?"
        $.getJSON(url, function(data){
            location.hash = "";
        });
    }
};

