function verifica(theForm) {
  if (theForm.nome.value=="") {
    alert("Informe seu nome!");
    theForm.nome.focus();
    return (false);
  }

  if (theForm.email.value=="") {
    alert("Informe seu e-mail.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.mensagem.value=="") {
    alert("Descreva tua mensagem.");
    theForm.mensagem.focus();
    return (false);
  }
}

function ExcluirLinha(linha){
    var elem = document.getElementById(linha);
    elem.style.display = 'none';
}

function validarInteger(evt) { 
	if(evt.keyCode < 48 || evt.keyCode > 57){
		evt.keyCode = "";
	}
}

function validarReal(evt,campo) { 
	if((evt.keyCode < 48 || evt.keyCode > 57) && evt.keyCode != 44){
		evt.keyCode = "";
	}
	if(evt.keyCode == 44){
		if(campo.value.indexOf(",") >= 0){
			evt.keyCode = "";
		}
	}
}

function somaTotal(quantidade, unitario, total, total2, geral, geral_f) { 
	if(quantidade.value == ''){
		quantidade.value = '0';
	}
	if(unitario.value == ''){
		unitario.value = '0';
	}
	var v_unitario = '';
	for(var i=0; i < unitario.value.length; i++){
		if(i == unitario.value.indexOf(",")){
			v_unitario += '.';
		}else{
			v_unitario += unitario.value.charAt(i);
		}
	}
	total2.value = eval(quantidade.value)*eval(v_unitario);
	total.value = total2.value;  
	calculaTotal(geral)
	formataTotal(total);
	formataTotal(geral);
}

function formataTotal(valor) { 
	var v_unitario = '';
	for(var i=0; i < valor.value.length; i++){
		if(i == valor.value.indexOf(".")){
			v_unitario += ',';
		}else{
			v_unitario += valor.value.charAt(i);
		}
	}
	if(v_unitario.indexOf(",") >= 0){
		var array_valor = v_unitario.split(",");
		var temp = eval(array_valor[1].charAt(2));
		if(temp >= 5){
			array_valor[1] = eval(array_valor[1].substring(0,2))+1;
		}else{
			array_valor[1] = eval(array_valor[1].substring(0,2));
		}
		if(array_valor[1] < 10){
			v_unitario = array_valor[0]+","+array_valor[1]+'0';
		}else{
			v_unitario = array_valor[0]+","+array_valor[1];
		}
	}else{
		v_unitario += ",00"; 
	}
	valor.value = v_unitario;
}

function calculaTotal(valor){ 
	lin = eval('document.form1.linhas.value');
	var total = 0;
	var temp = 0;
	for(var i = 1; i <= lin; i++){
		temp  =  eval('document.form1.valor_total_f_'+i+'.value');
		total = eval(total)+eval(temp);
	}
	valor.value = total;
}

function InsertLinha(table){
	document.form1.linhas.value++;
	lin = eval('document.form1.linhas.value');
	table.insertRow(lin);
	table.rows[lin].id='Linha_'+lin;
    table.rows[lin].insertCell(0);
	table.rows[lin].bgColor='#FFFFFF';
    table.rows[lin].cells[0].align='center';	
	table.rows[lin].cells[0].innerHTML='<input name="unidade_'+lin+'" type="text" class="p_campo_pp">';
    table.rows[lin].insertCell(1);
   table.rows[lin].cells[1].align='center';   
	table.rows[lin].cells[1].innerHTML='<input name="quantidade_'+lin+'" type="text" class="p_campo_pp" onKeyPress="validarInteger(event)" onBlur="somaTotal(document.form1.quantidade_'+lin+',document.form1.valor_unitario_'+lin+',document.form1.valor_total_'+lin+',document.form1.valor_total_f_'+lin+',document.form1.total,document.form1.total_f)">';
    table.rows[lin].insertCell(2);
    table.rows[lin].cells[2].align='center';
	table.rows[lin].cells[2].innerHTML='<input name="codigo_'+lin+'" type="text" class="p_campo_p">';
    table.rows[lin].insertCell(3);
    table.rows[lin].cells[3].align='center';
	table.rows[lin].cells[3].innerHTML='<input name="oc_'+lin+'" type="text" class="p_campo_p">';
    table.rows[lin].insertCell(4);
    table.rows[lin].cells[4].align='center';
	table.rows[lin].cells[4].innerHTML='<textarea name="especificacao_'+lin+'" type="text" class="p_campo_text">';
    table.rows[lin].insertCell(5);
    table.rows[lin].cells[5].align='center';
	table.rows[lin].cells[5].innerHTML='<input name="marca_'+lin+'" type="text" class="p_campo_p">';
    table.rows[lin].insertCell(6);
    table.rows[lin].cells[6].align='center';
	table.rows[lin].cells[6].innerHTML='<input name="valor_unitario_'+lin+'" type="text" class="p_campo_pp" onKeyPress="validarReal(event,this)"onBlur="somaTotal(document.form1.quantidade_'+lin+',document.form1.valor_unitario_'+lin+',document.form1.valor_total_'+lin+',document.form1.valor_total_f_'+lin+',document.form1.total,document.form1.total_f)">';
    table.rows[lin].insertCell(7);
    table.rows[lin].cells[7].align='center';
	table.rows[lin].cells[7].innerHTML='<input name="valor_total_'+lin+'" type="text" class="p_campo_pp" readonly="true"><input name="valor_total_f_'+lin+'" type="hidden" value="0">';
    table.rows[lin].insertCell(8);
    table.rows[lin].cells[8].align='center';
	table.rows[lin].cells[8].innerHTML='<a href="javascript:;" onClick="ExcluirLinha(\'Linha_'+lin+'\');document.form1.excluir_'+lin+'.value=\'S\'"><strong>X</strong></a><input type="hidden" name="excluir_'+lin+'" value="N">';
}