var tinyMCE = 1;
function strval(str) {
	// Get the string value of a variable
	//
	// version: 901.1411
	// discuss at: http://phpjs.org/functions/strval
	// +   original by: Brett Zamir
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   bugfixed by: Brett Zamir
	// %        note 1: Comment out the entire switch if you want JS-like behavior instead of PHP behavior
	// -    depends on: gettype
	// *     example 1: strval({red: 1, green: 2, blue: 3, white: 4});
	// *     returns 1: 'Array'
	var type = '';

	if (str === null) return '';

	type = gettype(str);
	switch (type) {
		case 'boolean':
		if (str === true) return '1';
		return '';
		case 'array':
		return 'Array';
		case 'object':
		return 'Object';
	}

	return str;
}

$(document).ready(
function() {
	$(".toollink").hover(function(){
		$(this).children(".tooltip").show();
	},function(){
		$(this).children(".tooltip").hide();
	});
});

document.onclick = function(e) {
	var p_node=0;
	e = e || event;
	var t = e.target || e.srcElement;

	//регистрация артиста
	if (t.id != 'slct2' && t.parentNode.id != 'slct2' && t.id != 'slct' && t.parentNode.id != 'slct') {
		if(document.getElementById('slct2') != null)
		$('#slct2').hide();
	}
	if (t.id != 's1a' && t.parentNode.id != 's1a') {
		if(document.getElementById('s1a') != null)
		$('#s1a').hide();
	}
	if (t.id != 's2a' && t.parentNode.id != 's2a') {
		if(document.getElementById('s2a') != null)
		$('#s2a').hide();
	}
	if (t.id != 's3a' && t.parentNode.id != 's3a') {
		if(document.getElementById('s3a') != null)
		$('#s3a').hide();
	}
}

function conf()
{
	if (confirm('Действительно удалить?'))
	return true;
	else
	return false;
}

function conf_ungroup()
{
	if (confirm('Отменить группировку?'))
	return true;
	else
	return false;
}

function arhive()
{
	if (confirm('Отправить предложение в архив?'))
	return true;
	else
	return false;
}

function down_menu_div_show()
{
	document.getElementById('choose_window').className='enabled';
}

function down_menu_div_hide()
{
	document.getElementById('choose_window').className='disabled';
}

function down_menu_div()
{
	if (document.getElementById('choose_window').className == 'enabled')
	document.getElementById('choose_window').className='disabled';
	else
	document.getElementById('choose_window').className='enabled';
	return false;
}

function load_article(id)
{
	$.ajax({
		type: "POST",
		url: "/ajax/load_article/",
		data: 	"article_id="+id,
		success: function(data) {
			$('div.cont_b').hide();
			$('div.cont_b').html(data);

			$('div.cont_b').prepend('<a style="font-size: 10px;" href="javascript://" onClick="window.location.reload();">в начало раздела</a><br /><br />');

			$('div.cont_b').fadeIn('slow');
		}
	});
}


function wait() {
	openWindow(200,30,'win_wait',0);
}

function wait_() {
	closeWindow('win_wait',0);
}


function validate_isValidEmail(email) { return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*\.)+[a-z]{2,4}$/i).test(email);}

function validate_isValidPhone(phone) { p = phone.replace(/\s+|(%20)+| /ig,'');  if(p.length > 13) return false; return (/\+375\d{9,9}/i).test(p)}



function no_benefit(s) {return !(/benefit|bеnefit|benеfit|bеnеfit/i).test(s)}


function trim(string) {
	if(!string) return '';
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function isInt(x) {
	var y=parseInt(x);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
}

function tCoun(field, countfield, maxlimit) {

	if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
	else
	document.getElementById(countfield).innerHTML = 'осталось: ' + (maxlimit - field.value.length);
}

function fA() {

	clearfA();


	var currentUrl = document.location;
	var id = escape(currentUrl).split("%23");

	if(id[1] != undefined) {
		var id_clear = id[1].split('_');
		cmId = id_clear[1];
		parentId = id_clear[2];

		if(parentId == undefined) {
			document.getElementById('comment_' + cmId).style.borderLeft = '1px solid #F00';
			document.getElementById('comment_' + cmId).style.paddingLeft = '3px';
			$('#cmt_'+cmId).focus();
			return false;
		}



		hideAllAnswereButtons();
		$('#answ_but_en_' + cmId).hide();
		$('#answ_but_dis_' + cmId).show();
		$('#Edit'+parentId+'-' + cmId).hide();
		if(document.getElementById('Answer'+parentId+'-' + cmId).style.display == 'none')
		$('#Answer'+parentId+'-' + cmId).show();
		else $('#Answer'+parentId+'-' + cmId).hide();
		$('#AnswerTextArea'+parentId+'-'+ cmId +'_name').focus();
		document.getElementById('comment_' + cmId).style.borderLeft = '1px solid #F00';
		document.getElementById('comment_' + cmId).style.paddingLeft = '3px';



	}
}

function clearfA() {
	$('.cmCont').css("border","0px");
}

function validate_num(vl) { return (/^[0-9\.]+$/i).test(vl);}
function validate_num_space(vl) { return (/^[0-9\.\s]+$/i).test(vl);}


function number_format(number, decimals, dec_point, thousands_sep) {
	// *     example 1: number_format(1234.56);
	// *     returns 1: '1,235'
	// *     example 2: number_format(1234.56, 2, ',', ' ');
	// *     returns 2: '1 234,56'
	// *     example 3: number_format(1234.5678, 2, '.', '');
	// *     returns 3: '1234.57'
	// *     example 4: number_format(67, 2, ',', '.');
	// *     returns 4: '67,00'
	// *     example 5: number_format(1000);
	// *     returns 5: '1,000'
	// *     example 6: number_format(67.311, 2);
	// *     returns 6: '67.31'
	// *     example 7: number_format(1000.55, 1);
	// *     returns 7: '1,000.6'
	// *     example 8: number_format(67000, 5, ',', '.');
	// *     returns 8: '67.000,00000'
	// *     example 9: number_format(0.9, 0);
	// *     returns 9: '1'
	// *    example 10: number_format('1.20', 2);
	// *    returns 10: '1.20'
	// *    example 11: number_format('1.20', 4);
	// *    returns 11: '1.2000'
	// *    example 12: number_format('1.2000', 3);
	// *    returns 12: '1.200'
	var n = !isFinite(+number) ? 0 : +number,
	prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
	sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
	dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
	s = '',
	toFixedFix = function (n, prec) {
		var k = Math.pow(10, prec);
		return '' + Math.round(n * k) / k;
	};
	// Fix for IE parseFloat(0.55).toFixed(0) = 0;
	s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	if (s[0].length > 3) {
		s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	}
	if ((s[1] || '').length < prec) {
		s[1] = s[1] || '';
		s[1] += new Array(prec - s[1].length + 1).join('0');
	}
	return s.join(dec);
}

//function isValidNumberPeriod(n) {return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*\.)+[a-z]{2,4}$/i).test(n)}
function isValidNumberPeriod(n) {return (/^([0-9\.\,\s]+\-[0-9\.\,\s]+)|([0-9\.\,\s]+)$/i).test(n)}

function formatSum(o,erDiv) {

        if (document.getElementById(erDiv)) {
            s = o.value;


            if(!trim(s)){
                    document.getElementById(erDiv).innerHTML = '';
                    return false;
            }
            if(!isValidNumberPeriod(s)) {
                    document.getElementById(erDiv).innerHTML = 'Неверный формат числа';
                    return false;
            }
            else {
                    document.getElementById(erDiv).innerHTML = '';
            }

            aS = s.split('-');
            s1 = trim(aS[0]);
            s2 = (aS[1]=='undefined') ? '' : trim(aS[1]);

            s1 = s1.replace(/\s+/g,'');
            s1 = s1.replace(/,/,'.');
            s2 = s2.replace(/\s+/g,'');
            s2 = s2.replace(/,/,'.');

            s1 = number_format(s1, 0, '.', ' ');
            s2 = number_format(s2, 0, '.', ' ');

            if(!parseInt(s1)) s1 = '';
            if(!parseInt(s2)) s2 = '';

            if(!s2)
            o.value = s1;
            else
            o.value = s1 + '-' + s2;
        }
}


//функция проверяет урл наличие новости

//function checkNewsUrl() {
//
//    var currentUrl = document.location;
//    var id = escape(currentUrl).split("%23");
//    var id_clear_article = id[1].split('article_');
//    if(id_clear_article[1] != undefined) {
//        articleId = id_clear_article[1];
//    }
//    var id_clear_news = id[1].split('news_');
//    if(id_clear[1] != undefined) {
//        articleId = id_clear[1];
//        alert(articleId);
//    }
//
//    setTimeout('checkNewsUrl()',200);
//
//}

function rat(z,r) {

	if(rat.vote!=undefined && rat.vote[z] != undefined && rat.vote[z] == r) {
		delete rat.vote[z];
		$('#r' + z + '0').html('');
		$('#r' + z + r).removeClass('rat'+ r +'on');
		$('#r' + z + r).addClass('rat'+ r);
		return false;
	}

	if(rat.vote == undefined)
	rat.vote = new Array();

	$('#r' + z + '1').removeClass('rat1on');
	$('#r' + z + '1').addClass('rat1');
	$('#r' + z + '2').removeClass('rat2on');
	$('#r' + z + '2').addClass('rat2');
	$('#r' + z + '3').removeClass('rat3on');
	$('#r' + z + '3').addClass('rat3');
	$('#r' + z + r).addClass('rat'+ r +'on');
	$('#r' + z + r).removeClass('rat'+ r);
	var w;
	if(r == 1) w = 'плохо!'
	if(r == 2) w = 'нормально'
	if(r == 3) w = 'отлично!'
	$('#r' + z + '0').html(w);
	rat.vote[z] = r;

}

function ratT(z,r,c) {

	if(c) {
		if(rat.vote == undefined)
		$('#r' + z + '0').html('');
		else {
			if(rat.vote[z] != undefined) {
				if(rat.vote[z] == 1) w = 'плохо!'
				if(rat.vote[z] == 2) w = 'нормально'
				if(rat.vote[z] == 3) w = 'отлично!'
				$('#r' + z + '0').html(w);
			}
			else {
				$('#r' + z + '0').html('');
			}
		}
	}
	else {
		var w;
		if(r == 1) w = 'плохо!'
		if(r == 2) w = 'нормально'
		if(r == 3) w = 'отлично!'
		$('#r' + z + '0').html(w);
	}
}

function encodeText(text) { var content; content = text.replace(/\+/g, "&#43"); content = content.replace(/\\/g, "&#92"); content = content.replace(/\&quot;/g, '"'); content = content.replace(/\&mdash;/g, '—'); content = content.replace(/\&laquo;/g, '«'); content = content.replace(/\&raquo;/g, '»'); return encodeURIComponent(content);}

function foc(id) {
	document.getElementById(id).focus();
}

function context_st_addView(id) {

	$.ajax({
		type: "POST",
		url: "/ajax/context_add_view/",
		data: "id="+id,
		success: function(data) {
		}
	});
}

function context_st_click(id) {

	$.ajax({
		type: "POST",
		url: "/ajax/context_click/",
		data: "id="+id,
		success: function(data) {
			//            window.open(data,'_blank');
		}
	});

}

function change_rating(bankId) {

	$.ajax({
		type: "POST",
		url: "/ajax/change_rating/",
		data: "id="+bankId,
		success: function(data) {
			$('#randRat').html(data);
		}
	});

}


function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "")
	if ((name + "=" + escape(value)).length <= 4000)
	document.cookie = curCookie
	else
	if (confirm("Cookie превышает 4KB и будет вырезан !"))
	document.cookie = curCookie
}

// name - имя считываемого cookie

function getCookie(name) {

	var prefix = name + "="
	var cookieStartIndex = document.cookie.indexOf(prefix)
	if (cookieStartIndex == -1)
	return null
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
	if (cookieEndIndex == -1)
	cookieEndIndex = document.cookie.length
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}


function remSpace(s) {
	s = s.replace(/\s+|(%20)+| /ig,'');
	return s;
}

function dontGo(id) {

	if(document.getElementById('win_dont_go').style.display!= 'none') {
		return false;
	}

	
	var timestamp = (Math.round(new Date().getTime() / 1000) + 86400);

	var coo = getCookie('dg');

	if(coo == null) {
		setCookie('dg',1,timestamp,'/');
	}
	if(coo == 1) {
		setCookie('dg',2,timestamp,'/');
	}
	if(coo == 2) {
		return false;
	}


	openWindow(850,300,'win_dont_go',1);
	$.ajax({
		type: "POST",
		url: "/ajax/dont_go/",
		data: "id="+id,
		success: function(data) {
			$('#dontGoArea').html(data);
		}
	});
}
