// JavaScript Document
function bmf(bid, pid){
    var d=document,root,node,cnode;
	if (d.getElementById) {
	    root=d.getElementById(bid);
		if (null==root||typeof root!='object') {
		    return false;
		}
		cnode = root.firstChild;
		if(1 != cnode.nodeType) {
		    cnode = cnode.nextSibling;
		}
		node=cnode.nodeName;
		while ('LI'!=node){
		    root=root.firstChild;
			node=root.firstChild.nodeName;
		}
		if('LI'!=node){
		    return false;
		}
	    return root;
	}
	return false;
}
function bmi(bid, pid){
    var d=document,i=0,j=0,root,node,cnode;
	root=bmf(bid, pid);
	if (null==root||typeof root!='object') {
		return false;
	}	
	for(i=0;i<root.childNodes.length;i++){
		node=root.childNodes[i];
		if('LI'== node.nodeName){
			cnode=node.firstChild.nodeName;
			while ('A'!=cnode){
				node=node.firstChild;
				cnode=node.firstChild.nodeName;
			}
			if('A'==cnode){
				node.firstChild.num = j++;
				node.firstChild.onclick=function(){bm(bid, pid, this.num);}
			}
		}
	}
	return true;
}
function bm(bid, pid, num){
    var d=document,i=0,j=0,root,node,cnode,classname;
	root=bmf(bid, pid);
	if (null==root||typeof root!='object') {
		return false;
	}
     
	for(i=0;i<root.childNodes.length;i++){
		node=root.childNodes[i];
		if('LI'== node.nodeName){
			classname = (num == j++) ? 'active' : '';
			cnode=node.firstChild.nodeName;
			while ('A'!=cnode){
				node=node.firstChild;
				cnode=node.firstChild.nodeName;
			}
			if('A'==cnode){
				node.firstChild.className = classname;
				node.firstChild.blur();
			}
		}
	}
    bmp(root, pid, num);
	return true;
}
function bmp(root, pid, num){
    var d=document,i,node;
	root=(typeof root=='object')?root.parentNode:null;
	if (null==root||typeof root!='object') {
		return false;
	}
	for(i=0;i<root.childNodes.length;i++){
		node=root.childNodes[i];
		if('DIV'== node.nodeName&&-1!=node.id.indexOf(pid)){ 
			if (num == node.id.substr(pid.length) - 1) {
				if(node.style){
					node.style.display='block';
				}
			} else {
				if(node.style){
					node.style.display='none';
				}
			}
		}
	}
	return true;
}
/*
11 = 5000
12 = 5000
13 = 5000

21 = 200
22 = 2500
23 = 100000

31 = 600
32 = 300
33 = 15000

41 = 600
42 = 15000
*/
function chfv(id, n1, n2){
	var d=document, limits = new Array();
	n1=n1||0;
	n2=n2||0;
	limits[1] = {1: 5000, 2: 5000, 3: 5000};
	limits[2] = {1: 200, 2: 2500, 3: 100000};
	limits[3] = {1: 600, 2: 300, 3: 15000};
	limits[4] = {1: 600, 2: 15000};
	id.value=id.value.replace(/[^\d\.]/gi,'');
	if(''==id.value||0==id.value){
		id.value=(-1!=id.id.indexOf('calcntfld'))?'1':'';
	} else if(n1 > 0 && n2 > 0){
		if (id.value > limits[n1][n2]){
			id.value=limits[n1][n2];
		}
		if (id.value==limits[n1][n2]){
			id.className = id.className + ' red';
		} else {
		    id.className = id.className.replace(/ red/gi,'');
		}
	}
	return true;
}
var count = new Array();
function bmpr(num, col){
	var d = document, c, i, o, e, e1, e2, limit = 11;
	o = d.getElementById('calrow' + num);
	c = (count[num]) ? count[num] + 1 : 2;
	count[num] = (c < limit) ? c : count[num];

	if (null != o && typeof o == 'object' && c < limit) {
		e = d.createElement('tr');
		e.className  = 'addcalfld';
		
		for (i=1; i<4; i++) {
			e1 = d.createElement('td');
			if (col>=i) {
				e2           = d.createElement('input');
				e2.type      = 'text';
				e2.id        = 'calfld' + num + count[num] + i;
				e2.value     = '';
				e2.maxLength = (d.getElementById('calfld' + num + '1' + i)) ? d.getElementById('calfld' + num + '1' + i).maxLength : '6';
				e2.num = i;
				e2.onkeyup = function(){chfv(this, num, this.num);}
				e1.appendChild(e2);
			}
			e.appendChild(e1);
		}
		o.appendChild(e);
				
		e        = d.createElement('tr');
		e.className  = 'resrow addresrow';
		
		e1       = d.createElement('td');
		e1.appendChild(d.createTextNode(calc_res_row_name));
		e.appendChild(e1);
		
		e1       = d.createElement('td');
		e2       = d.createElement('span');
		e2.id    = 'calres' + num + count[num];
		e2.appendChild(d.createTextNode('0.00'));
		e1.appendChild(e2);
		e.appendChild(e1);
		
		
		e1       = d.createElement('td');
		e2       = d.createElement('input');
		e2.type  = 'text';
		e2.id    = 'calcntfld' + num + count[num] + '4';
		e2.value = '1';
		e2.maxLength = '3';
		e2.onkeyup = function(){chfv(this);}
		e1.appendChild(e2);
		e1.appendChild(d.createTextNode(' ' + calc_res_row_unit));
		
		e.appendChild(e1);
		
		o.appendChild(e);
	}

	return true;
}
function calculate(num){
	var d=document,i, m, k, r, t, o, p, p1, p2, p3, s = 0, c = 0;
	count[num]=(count[num]) ? count[num] : 1;

	t = d.getElementById('caltotal' + num);
	o = d.getElementById('calcount' + num);
	
	for (i=1; i<=count[num]; i++) {
		p  = d.getElementById('calfld' + num + i + '1');
		p1 = p ? parseInt(p.value) : null;
		p  = d.getElementById('calfld' + num + i + '2');
		p2 = p ? parseInt(p.value) : null;
		p  = d.getElementById('calfld' + num + i + '3');
		p3 = p ? parseInt(p.value) : null;
		k  = d.getElementById('calcntfld' + num + i + '4');
		r  = d.getElementById('calres' + num + i);
		
		if (isNaN(p1) || p1 <= 0 || isNaN(p2) || p2 <= 0 || (4 != num && (isNaN(p3) || p3 <= 0))) {
			r.removeChild(r.firstChild);
			r.appendChild(d.createTextNode('0.00'));
			r.className = '';
			continue;
		}
		switch(num){
			case 1: {
				m = (p1 / 2000 * p1 / 2000 * 3.14 - p2 / 2000 * p2 / 2000 * 3.14) * p3 / 1000 * 2.7 * 1000;
			}
			break;
			case 2: {
				m = p1 / 1000 * p2 / 1000 * p3 / 1000 * 2.7 * 1000;
			}
			break;
			case 3: {
				m = (p1 / 2000 * p1 / 2000 * 3.14 - (p1 - 2 * p2) / 2000 * (p1 - 2 * p2) / 2000 * 3.14) * p3 / 1000 * 2.7 * 1000;
			}
			break;
			case 4: {
				m = p1 / 2000 * p1 / 2000 * 3.14 * p2 / 1000 * 2.7 * 1000;
			}
			break;
		}
		
		m = Math.round(m);
		m = parseInt(k.value) * m;
		
		if (null == k || typeof k != 'object' || isNaN(parseInt(k.value)) || parseInt(k.value) <= 0 || m <= 0) {
			k.value = 1;
		}

		c = (m) ? c + parseInt(k.value) : c;
		s = s + m;

		if (null != r && typeof r == 'object' && m > 0) {
			r.removeChild(r.firstChild);
			r.appendChild(d.createTextNode(m));
			r.className = 'bold';
		} else {
			r.removeChild(r.firstChild);
			r.appendChild(d.createTextNode('0.00'));
			r.className = '';
		}
	}
	
	if (null != t && typeof t == 'object' && s > 0) {
	    t.removeChild(t.firstChild);
	    t.appendChild(d.createTextNode(s));
		t.className = 'bold';
	} else {
	    t.removeChild(t.firstChild);
	    t.appendChild(d.createTextNode('0.00'));
		t.className = '';
	}
	if (null != o && typeof o == 'object' && c > 0) {
	    o.removeChild(o.firstChild);
	    o.appendChild(d.createTextNode(c));
		o.className = 'bold';
	} else {
	    o.removeChild(o.firstChild);
	    o.appendChild(d.createTextNode('0'));
		o.className = '';
	}
			
    return true;
}