function $(el) {return document.getElementById(el)}

function countNL(text) {
	var count = 0

	var temp = text.split('\n')
	for (var i=0;i<temp.length;i++) {
		if (temp[i].length > 29) {
			count += Math.round(temp[i].length/29+0.35)
//			count += Math.round(temp[i].length/29+0.45)
		}
		else if (temp[i].length > 4)
			count += 1
	}
	return count
}
function calcPrice(obj) {
	if (!$('content1')) return;
	var price = 0
	var lines = parseInt($('mLength').innerHTML)
	var linePrice = 0
	if ($('content1').checked)
		linePrice = 50
	if ($('content2').checked)
		linePrice = 55
	if ($('content3').checked)
		linePrice = 55
	if ($('content4').checked)
		linePrice = 65
	if ($('content5').checked)
		linePrice = 65
	if ($('content6').checked)
		linePrice = 80
	price = lines * linePrice
	
	$('price').innerHTML = price
}

function changeLength(obj) {
		//if (obj.value.length > 174) {
		//	alert('Длина объявления превысила допустимую')
		//	obj.value = obj.value.substr(0,174)
		
		var length = 0
		var words = obj.value.split(/\n/).join(' ').split(' ')
		for (var i=0;i<words.length;i++) {
			if (words[i].length >= 3) length += 1
		}
		$('mLength').innerHTML = length
		calcPrice()
}

function wrapArea(el) {
	if (el.value.length-countNL(el.value)/29>0.8)
	//alert(el.value.length-countNL(el.value)/29)
	if (((el.value.length-countNL(el.value))/29 - parseInt($('mLength').innerHTML)) >= 1) {
		el.value += "\n"
		$('mLength').innerHTML = countNL(el.value)
	}
}


function ShowImageWindow(picname, width, height, title) {
	width += 30
	height += 30    
	var params='toolbar=no,height='+height+',width='+width+',location=no,scrollbars=no,status=no,menubar=no,resizable=yes';
	picwin=window.open("","_blank",params);
	picwin.document.open();
	picwin.document.write('<html><title>Ва-Банкъ</title>');
	picwin.document.write('<body>');
	picwin.document.write('<table><tr><td align="center" valign="middle"><img src="'+picname+'" border="0"/>');
	picwin.document.write('</td></tr></table></body></html>');
	picwin.document.close();
}

function showModule(obj) {
	obj.className = "active"
	for (var i=1;i<=3;i++) {
		if ($(obj.id + '_' + i))
			$(obj.id + '_' + i).style.display = "block"
	}
}

function hideModule(obj) {
	obj.className = ""
	for (var i=1;i<=3;i++) {
		if ($(obj.id + '_' + i))
			$(obj.id + '_' + i).style.display = "none"
	}
}

function toggleCB(el) {
	var obj = $(el)
	if (el=="ramka")
		var obj2 = $("blackBack")
	else if (el=="blackBack")
		var obj2 = $("ramka")
	if (obj.checked)
		obj2.disabled="true"
	else obj2.disabled=""
}

function chooseOp(num,to) {
	var cur = $('op'+num)
	var newOp = $('op'+(num+to))
	cur.style.display = "none" 
	newOp.style.display = "block"
}
