function changeStatusOfNewClient(status) {
	if (status=='4') {
		alert("МОЛЯ, ЗАДЪЛЖИТЕЛНО СЕ СВЪРЖЕТЕ С ТОЗИ КЛИЕНТ И МУ ПРЕДАЙТЕ, ЧЕ НЯМА ТЕХНИЧЕСКА ВЪЗМОЖНОСТ ЗА МОМЕНТА!"); 
	}
	if (status=='5'||status=='6') {
		$('protocol_table').style.display='block';
	} else {
		$('protocol_table').style.display='none';
	}
}

function check_Radio(data1,data2)
{
  var kabel_no=0;
  var kabel_no1=0;
  var form_r1=document.protocol.elements[data1];
  var form_r2=document.protocol.elements[data2];
  for (i=0;i<form_r1.length;i++)
  {
     if (form_r1[i].checked==true)
     {
       kabel_no++;
     }
  }
  for (i=0;i<form_r2.length;i++)
  {
     if (form_r2[i].checked==true)
     {
       kabel_no1++;
     }
  }
  if (kabel_no1=="0")
    alert ("Не сте избрали тип окабеляване");
  if (kabel_no=="0")
    alert ("Не сте избрали тип кабел");
}

function sum_Kabel(data1,data2,data3,data4)
{
  var pr;
  var guilty;
  if (document.protocol.guilty)
  {
    if (document.protocol.guilty.options[1].selected)
     guilty=1;
    else
     guilty=0; 
  }
  else 
    guilty=1; 
  var str=Number(document.protocol.elements[data1].value);
  var end=Number(document.protocol.elements[data2].value);
  document.getElementById(data3).innerHTML=Math.abs(end-str);
  var form_r1=document.protocol.elements['time'];  
  if (document.protocol.elements[data4][0].checked==true)
  {
    if (end-str>40)
    {
     var m=(end-str)-40;
     if (guilty) 
       document.protocol.elements['cabel_paid_by_client'].value=m;
     
     if (document.protocol.elements['type1'].checked)
       document.protocol.elements['cabel_paid_by_client'].value=m;
     for (i=0;i<form_r1.length;i++)
     {
       if (form_r1[i].checked==true)
       {
          pr=(i+1)*3;
       }
     }
     if (!pr)
     {
       pr=parseInt(document.protocol.elements['time_otder'].value);
       pr=parseInt(pr/15)+1;
       pr=pr*3; 
       pr=Math.floor(pr);
     }
     if (document.protocol.elements['type1'].checked)
      pr=0;
     /////////////////////////   
     if (guilty) 
       document.protocol.elements['price'].value=pr+m;  
    }
  }
  if (document.protocol.elements[data4][1].checked==true)
  {
    if (end-str>70)
    {
     var m=(end-str)-70;
     if (guilty)
       document.protocol.elements['cabel_paid_by_client'].value=m;
     for (i=0;i<form_r1.length;i++)
     {
       if (form_r1[i].checked==true)
       {
         pr=(i+1)*3; 
       }
     }
     if (!pr)
     {
       pr=parseInt(document.protocol.elements['time_otder'].value);
       pr=parseInt(pr/15)+1;
       pr=pr*3; 
       pr=Math.floor(pr);
       //alert(pr); 
     }
     if (guilty)
       document.protocol.elements['price'].value=pr+m;  
    }  
  }
    
}

function hide_protocol_table() {
	if (document.getElementsByName("guilty")[0].options[1].selected) {
		document.getElementById("protocol_table").style.display='block';
	} else if (document.getElementsByName("guilty")[0].options[2].selected) {
		document.getElementById("protocol_table").style.display='block';
		document.getElementById("price").value='';
	} else {
		document.getElementById("protocol_table").style.display='none';
	}
}

function hide_extra_textarea() {
	if (document.getElementById("reason").value=='33') {
		document.getElementById("extra_textarea").style.display='block';
		document.getElementById("extra_textarea").text='';
	} else {
		document.getElementById("extra_textarea").style.display='none';
		document.getElementById("extra_textarea").text='';
	}
}

function calculatePriceInProtocol() {
	var times		=document.getElementsByName('time');
	var time_by_hand=document.getElementById('time_by_hand');
	var price;
	var guilty;
	
	if (document.getElementsByName('guilty').length>0) {
		if (document.getElementsByName('guilty')[0].options[1].selected) {
			guilty=1;
		} else {
			guilty=0;
		}
	} else {
		guilty=0;
	}

	if (guilty==1) {
		for (i=0; i<times.length; i++) {
			if (time_by_hand.value==''||parseInt(time_by_hand.value)==0) {
				if (times[i].checked==true) {
					price=(times[i].value/15)*3;
				}
			} else {
				times[i].checked=false;
				price=Math.round((parseInt(time_by_hand.value)/15)*3);
			}
		}
		if (!price&&document.getElementById('price').value=='') {
			price=0;
		}
			
		if (document.getElementById('type1').checked) {
			price=0;
		} else if (document.getElementById('payment3').checked) {
			price=0;
		}
		
		document.getElementById('price').value=price;
	}
}




var mounted	=new Array();	
mounted[1]	=2;
mounted[2]	=2;

function addNewMaterial(type) {
	
	if (mounted[type]<=20) {
		var trMaterial	=new Element('tr', {
			'id'		: 'trMaterial_'+type+'_'+mounted[type]
		}).inject($('tableMaterial_'+type));
		
		var tdAjurCode	=new Element('td', {
			'id'		: 'tdAjurCode_'+type+'_'+mounted[type],
			'class'		: 'w75px middle'
		}).inject(trMaterial);
		
		var	ajurCode	=new Element('input', {
			'id'		: 'ajurCode_'+type+'_'+mounted[type],
			'name'		: 'ajurCode_'+type+'_'+mounted[type],
			'type'		: 'text',
			'value'		: '',
			'size'		: '6',
			'onblur'	: 'getMaterialInfo('+type+', '+mounted[type]+');'
		}).inject(tdAjurCode);
		
		var tdMaterial	=new Element('td', {
			'id'		: 'tdMaterial_'+type+'_'+mounted[type],
			'class'		: 'w300px middle'
		}).inject(trMaterial);
		
		var tdQuantity	=new Element('td', {
			'id'		: 'tdQuantity_'+type+'_'+mounted[type],
			'class'		: 'w175px middle right padR5'
		}).inject(trMaterial);
		
		var	quantity	=new Element('input', {
			'id'		: 'quantity_'+type+'_'+mounted[type],
			'name'		: 'quantity_'+type+'_'+mounted[type],
			'type'		: 'text',
			'value'		: '',
			'class'		: 'none',
			'size'		: '6'
		}).inject(tdQuantity);
		
		var	cabelStart	=new Element('input', {
			'id'		: 'cabelStart_'+type+'_'+mounted[type],
			'name'		: 'cabelStart_'+type+'_'+mounted[type],
			'type'		: 'text',
			'value'		: 'НАЧАЛО',
			'class'		: 'none',
			'size'		: '6',
			'onfocus'	: 'this.select();'
		}).inject(tdQuantity);
		
		var	cabelEnd	=new Element('input', {
			'id'		: 'cabelEnd_'+type+'_'+mounted[type],
			'name'		: 'cabelEnd_'+type+'_'+mounted[type],
			'type'		: 'text',
			'value'		: 'КРАЙ',
			'class'		: 'none',
			'size'		: '6',
			'onfocus'	: 'this.select();'
		}).inject(tdQuantity);
		
		var tdMeasure	=new Element('td', {
			'id'		: 'tdMeasure_'+type+'_'+mounted[type],
			'class'		: 'w50px middle'
		}).inject(trMaterial);
		
		var tdAddNew	=new Element('td', {
			'id'		: 'tdAddNew_'+type+'_'+mounted[type],
			'class'		: 'middle right'
		}).inject(trMaterial);
		
	//	$('tableMaterial_'+type).inject(trMaterial);
		$('ajurCode_'+type+'_'+mounted[type]).focus();
		
		mounted[type]++;
	}
}

function checkProtocolValues() {
	var n=$('number').value;
	var a=$('address').value;
	var c=$('client_no').value;
	var d=$('date').value;
	var t=$('time').value;
	
	var ok=true;
	
	if (trim(n).length<5) {
		ok=false;
		alert('Не си попълнил правилно полето: НОМЕР НА ПРОТОКОЛА');
	}
	if ($('fld_mto').selectedIndex == 0) {
		ok=false;
		alert('Не си избрал техник - материално отговорно лице');
	}
	if (trim(a).length<20) {
		ok=false;
		alert('Не си попълнил правилно полето: АДРЕС');
	}
	if (trim(c).length<7) {
		ok=false;
		alert('Не си попълнил правилно полето: ПРОЕКТ №');
	}
	if (trim(d).length!=10) {
		ok=false;
		alert('Не си попълнил правилно полето: ДАТА');
	}
	if (trim(t).length<4||trim(t).length>5) {
		ok=false;
		alert('Не си попълнил правилно полето: ИЗРАЗХОДВАНО ВРЕМЕ');
	}
	
	var mto = $('fld_mto');
	if (mto.value.length < 15) {
		ok=false;
		alert('Невалиден МТО код!\nМоля изберете техник от менюто.');
		mto.focus();
	}
	
	var type_task=$('type_proto').value;
	if (type_task=='type9'){
		ok=false
		alert('Моля изберете дейност!');
	}
	
	return ok;
}

