var currentCategory = 0;

function ViewAddComment()
{
	var display = $('.newpub').css('display');
	if( display=='none' ) {
		$('.newpub').css('display','block');
	} else {
		$('.newpub').css('display','none');
	}
}
function validForm(fld) {
	switch (fld) {
		case 'title':
			if (document.getElementById(fld).value) {
				if (document.getElementById(fld+"_check_mess")) document.getElementById(fld+"_check_mess").innerHTML = "";
				changeIco(fld, true);
			} else {
				changeIco(fld, false);
				if (document.getElementById(fld+"_check_mess")) document.getElementById(fld+"_check_mess").innerHTML = "Введите название";
			}
			break;

		case 'body':
			var bodyText = $('#body').attr('value');
			if ( bodyText.length > 2 ) {
				if (false == validateBodyText(bodyText)) {
					changeIco(fld, false);
					if (document.getElementById(fld + "_check_mess"))
						document.getElementById(fld + "_check_mess").innerHTML = "Вы забыли выделить катом (<img src='/images/icon_cut.gif' alt='' />) начальный фрагмент статьи для предпросмотра в ленте статей, либо выделенный фрагмент слишком длинный.";
				} else {
					changeIco(fld, true);
					if (document.getElementById(fld + "_check_mess"))
						document.getElementById(fld + "_check_mess").innerHTML = "";
				}
			} else {
				changeIco(fld, false);
				if (document.getElementById(fld + "_check_mess"))
					document.getElementById(fld + "_check_mess").innerHTML = "Введите текст";
			}
			break;
	}
}

function validateBodyText(value)
{
	if (!value)
		return false;
	var charsCount    = 700;
	var charsCountAll = 1000;
	
	var pattern = /([\w\s]+)\[cut\]/gim;
	//var arr = pattern.exec(value);
	var arr = value.match(pattern);
	
	if (arr && arr[1]) {
		if (arr[1].length > charsCount + 5) {
			return false;
		}
	}
	if (!arr && value.length > charsCountAll) {
		return false;
	}
	
	return true;
}

function validBody( value )
{
	var fld='body';
	if( value ) {
		if (document.getElementById(fld+"_check_mess")) document.getElementById(fld+"_check_mess").innerHTML = "";
			changeIco(fld, true);
			if( !editLoad ) editLoad = true;
		} else {
			changeIco(fld, false);
			if (document.getElementById(fld+"_check_mess")) document.getElementById(fld+"_check_mess").innerHTML = "Введите текст";
		}
}


function changeIco(fld, res) {
	if (res == true) document.getElementById(fld+'_check_img').src = "/images/true.gif";
	else document.getElementById(fld+'_check_img').src = "/images/false.gif";
}

function sendReaquest(fld) {

	var var_data = "fld="+fld;
	var_data += "&value="+document.getElementById(fld).value;

	var gotError = function(err) {
		alert("System Error");
		return;
	};

	var gotInfo = function(data) {
		if (data.success == "1") {
			if (document.getElementById(fld+"_check__mess")) document.getElementById(fld+"_check__mess").innerHTML = "";
			changeIco(fld, true);
		}

		if (data.error == "1") {
			if (document.getElementById(fld+"_check__mess")) document.getElementById(fld+"_check_mess").innerHTML = data.message;
			changeIco(fld, false);
		}

		return;
	};

	changeIco(fld, true); // временно, пока не сделан ajax

	var url = base_url+"/ajax-request/check-registration/";
	var opts = {
	"data": var_data,
	"method": "POST",
	"url": url,
	"onError": gotError,
	"onData": gotInfo

	};

	HTTPReq.getJSON(opts);
}

function checkDelete( type )
{
	var str;
	if( type==1 ) {
		str = "Вы уверены, что хотите удалить статью?";
	} else {
		str = "Вы уверены, что хотите удалить комментарий?";
	}
	return confirm(str);
}

function deletePublication( id )
{
	if( checkDelete(1) ) {
		location.href = '/publication/delete/id/'+id;
	}
}

function deleteComment( id )
{
	if( checkDelete(2) ) {
		$.post(  
				'/widget/delete-comment/',
				{
					id: id
				},
				afterDeleteComment
			);
	} 
}

function afterDeleteComment( data )
{
	if ( data ) {
		$('#listComment').html( data );
	}
}

function voteUp( publicationId )
{
	$.post(
			'/widget/publication-vote-up/',
			{
				id: publicationId
			},
			afterVoteUp
	);
}

function afterVoteUp( data )
{
	var retval = $( data+' div' ).eq(0).text();
	if( retval=='0' ) {
		alert( $( data+' div' ).eq(1).text() );
	} else if( retval=='1') {
		publicationId = $( data+' div' ).eq(1).text();
		newRating = $( data+' div' ).eq(2).text();
		$('#rate_'+publicationId ).html( newRating );
	}
	
}

function voteDown( publicationId )
{
	$.post(
			'/widget/publication-vote-down/',
			{
				id: publicationId
			},
			afterVoteDown
	);
}

function afterVoteDown( data )
{
	
	var retval = $( data+' div' ).eq(0).text();
	if( retval=='0' ) {
		alert( $( data+' div' ).eq(1).text() );
	} else if( retval=='1') {
		publicationId = $( data+' div' ).eq(1).text();
		newRating = $( data+' div' ).eq(2).text();
		$('#rate_'+publicationId ).html( newRating );
	}
}

function commentVoteUp( commentId )
{
	$.post(
			'/widget/comment-vote-up/type/pcomment',
			{
				id: commentId
			},
			afterCommentVoteUp
	);
}

function afterCommentVoteUp( data )
{
	var retval = $( data+' div' ).eq(0).text();
	if( retval=='0' ) {
		alert( $( data+' div' ).eq(1).text() );
	} else if( retval=='1') {
		commentId = $( data+' div' ).eq(1).text();
		newRating = $( data+' div' ).eq(2).text();
		$('#rate_comment_'+commentId ).html( newRating );
	}
}

function commentVoteDown( commentId )
{
	$.post(
			'/widget/comment-vote-down/type/pcomment',
			{
				id: commentId
			},
			afterCommentVoteDown
	);
}

function afterCommentVoteDown( data )
{
	var retval = $( data+' div' ).eq(0).text();
	if( retval=='0' ) {
		alert( $( data+' div' ).eq(1).text() );
	} else if( retval=='1') {
		commentId = $( data+' div' ).eq(1).text();
		newRating = $( data+' div' ).eq(2).text();
		$('#rate_comment_'+commentId ).html( newRating );
	}
}
var currentCategory;
function setCurrency( categoryId, currencyId, callBack )
{
	currentCategory = categoryId;
	$('#tarifTable_' + categoryId).after('<div class="ajax-loader">&nbsp;</div>').hide().nextAll('br').remove();
				   
	$.post(
			'/widget/get-tarif-table/',
			{
				categoryid: categoryId,
				currencyid: currencyId
			},
			callBack?callBack:afterSetCurrency
	);
	
	return false;
}

function afterSetCurrency( data )
{
	if(data) {
		$('div.ajax-loader').remove();
		$('#tarifTable_' + currentCategory).after(data).remove();
	}
}

