function roundNumber(number,x) {
// rounds number to x decimal, default to 2
x = (!x ? 2 : x);
return Math.round(number*Math.pow(10,x))/Math.pow(10,x);
}

function calc(){
    var rows = document.getElementById('zakact').getElementsByTagName('tr');
    var checkbox;
    var summa=0;
	var numfol=0;
    // cobzor_ cupomi_
    for ( var i = 0; i < rows.length; i++ ) {
        psumma = 0;
        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' && checkbox.checked == true && checkbox.id>0) {
          sum_post = document.getElementById('summ_'+checkbox.id).value*1;
          psumma = sum_post;

          obzor = document.getElementById('obzor_'+checkbox.id);
          if ( obzor && obzor.type == 'checkbox' && obzor.checked == true) {            obzor_cena = document.getElementById('cobzor_'+checkbox.id).value*1;
            psumma = (psumma+obzor_cena)*1;
          }

          upomi = document.getElementById('upomi_'+checkbox.id);
          if ( upomi && upomi.type == 'checkbox' && upomi.checked == true) {
            upomi_cena = document.getElementById('cupomi_'+checkbox.id).value*1;
            psumma = (psumma+upomi_cena)*1;
          }

          numfol += document.getElementById('folow_'+checkbox.id).value*1;
          psumma = roundNumber(psumma,3);
          document.getElementById('psumm_'+checkbox.id).innerHTML = psumma + ' у.е.';
          summa = summa+psumma;
        }
    }
    summa=Math.round(summa*100,2)/100;
    document.getElementById('fullstoim').value = summa;
    document.getElementById('stoim').innerHTML = summa + ' у.е.';
    document.getElementById('folow').innerHTML = numfol;
    return true;
}


function addBookmark(url, title)
{
  if (!url) url = location.href;
  if (!title) title = document.title;

  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
  {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }
  else return false;

  return true;
}

function Up(name,section){
    var obj=document.getElementById("cnt_"+section+"_"+name);
    obj.value=parseInt(obj.value)+1;
    return false;
}

function Dn(name,section){
    var obj=document.getElementById("cnt_"+section+"_"+name);
    obj.value=parseInt(obj.value)>1?parseInt(obj.value)-1:1;
    return false;
}

function setCheckboxColumn(theCheckbox,theTable){
    if (document.getElementById(theCheckbox)) {
        document.getElementById(theCheckbox).checked = (document.getElementById(theCheckbox).checked ? false : true);
        document.getElementById(theTable).style.border = (document.getElementById(theCheckbox).checked ? "1px solid #cc2200" : "1px solid #e5e5e5");
        if (document.getElementById(theCheckbox + 'r')) {
            document.getElementById(theCheckbox + 'r').checked = document.getElementById(theCheckbox).checked;
            document.getElementById(theTable + 'r').style.backgroundColor = "silver";
        }
    } else {
        if (document.getElementById(theCheckbox + 'r')) {
            document.getElementById(theCheckbox + 'r').checked = (document.getElementById(theCheckbox +'r').checked ? false : true);
            if (document.getElementById(theCheckbox)) {
                document.getElementById(theCheckbox).checked = document.getElementById(theCheckbox + 'r').checked;
            }
        }
    }
}

function markAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('p');
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {
        checkbox = rows[i].getElementsByTagName( 'input' )[0];
        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
            }
        }
    }
    return true;
}

function unMarkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('p');
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {
        checkbox = rows[i].getElementsByTagName( 'input' )[0];
        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
        }
    }
    return true;
}

function markAllRow( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {
        checkbox = rows[i].getElementsByTagName( 'input' )[0];
        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
            }
        }
    }
    return true;
}

function unMarkAllRow( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {
        checkbox = rows[i].getElementsByTagName( 'input' )[0];
        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
        }
    }
    return true;
}

function show_used_len( obj, len_max, to_write)
{
var len_current=parseInt( document.getElementById( obj ).value.length );
var obj_to_write=document.getElementById( to_write );
obj_to_write.innerHTML=len_current;
}

function $(obj) {
  	if (typeof obj == 'object')
  	return obj;
	if (document.getElementById)
		return (document.getElementById(obj));
	else if (document.all)
		return document.all(obj);

    return null;
}

String.prototype.str_replace = function(srch, rpl)
{
	var ar = this.split(srch);
	return ar.join(rpl);
}


function getParent(el) {

	return ((el.parentElement) ? el.parentElement : ((el.parentNode) ? el.parentNode : null));

}



function getElementPosition(elem)
{

    var w = elem.offsetWidth;
    var h = elem.offsetHeight;

    var l = 0;
    var t = 0;

    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}



function createTextAreaWidget(el, min, max)
{
	var el = $(el);
	var counter = $('counter' + el.id);
	if (!counter)
	{
		var parent = getParent(el);
		var counter = document.createElement('div');
		counter.setAttribute('id', 'counter' + el.id);
		counter.className = 'counter';
		parent.appendChild(counter);
		parent.style.position = 'relative';
		counter.style.position = 'absolute';

		counter.style.left = getElementPosition(el).width + 2 +  'px';
		counter.style.top = 0;
		counter.style.height = getElementPosition(el).height + 'px';
	}

	lens = el.value.str_replace(String.fromCharCode(13), '').length;
	len = max-lens;
	if (len <= 0) {
		el.value = el.value.substr(0, max);
		len = 0;
	}

	el.onkeyup = function () {createTextAreaWidget(el, min, max);}
	el.onchange = function () {createTextAreaWidget(el, min, max);}
	createStat(counter, min, max, len);

}

function createStat(el, min, max, current)
{
	var className = (current < 10) ? 'tend' : ((current >= max) ? 'norm' : 'norm');
	var cur = (current >= max) ? current + '' : current;
	document.getElementById('chars').className = className;
	document.getElementById('chars').innerHTML = current;
}

 function hish(el){
 var link = document.getElementById(el);
 var text = document.getElementById(el+'_text');
 if(text.style.display=='none'){
   text.style.display='block';
   link.innerHTML='<a class=\"hide\" onclick=\"hish(\''+el+'\');\">Скрыть</a>';
 }else{
   text.style.display='none';
   link.innerHTML='<a class=\"hide\" onclick=\"hish(\''+el+'\');\">Показать</a>';
 }
 }

 function chb(el){
 var box = document.getElementById(el);
 var text = document.getElementById(el+'_text');
 if(box.checked==true){
   text.style.display='block';
 }else{
   text.style.display='none';
 }
}