//Funes para validao de campos de formulrios
//Escrito por Elcio Ferreira 
//elcio.com.br

/*********************************************
------------detalhes importantes---------------
 - nome do campo não pode ser igual ao nome da função...
**********************************************/

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

function so_numeros(v){
    return v.replace(/[^01234567890.]/g,"")
}

function formata_telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que no  dgito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parnteses em volta dos dois primeiros dgitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hfen entre o quarto e o quinto dgitos
    return v
}

function formato_data(v){
    v=v.replace(/\D/g,"")                 	//Remove tudo o que no  dgito
    v=v.replace(/^(\d\d)(\d)/g,"$1/$2") 	//formata como data DD/MM/AAAA
 	v=v.replace(/(\D\d\d)(\d)/g,"$1/$2")
    return v
}

function formata_cpf(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que no  dgito
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dgitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dgitos
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hfen entre o terceiro e o quarto dgitos
    return v
}

function cep(v){
    v=v.replace(/D/g,"")                //Remove tudo o que no  dgito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse  to fcil que no merece explicaes
    return v
}

function cnpj(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que no  dgito
    v=v.replace(/^(\d{2})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro dgitos
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dgitos
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           //Coloca uma barra entre o oitavo e o nono dgitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")              //Coloca um hfen depois do bloco de quatro dgitos
    return v
}

function romanos(v){
    v=v.toUpperCase()             //Maisculas
    v=v.replace(/[^IVXLCDM]/g,"") //Remove tudo o que no for I, V, X, L, C, D ou M
    //Essa  complicada! Copiei daqui: http://www.diveintopython.org/refactoring/refactoring.html
    while(v.replace(/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/,"")!="")
        v=v.replace(/.$/,"")
    return v
}

function site(v){
    v=v.replace(/^http:\/\/?/,"")
    dominio=v
    caminho=""
    if(v.indexOf("/")>-1)
        dominio=v.split("/")[0]
        caminho=v.replace(/[^\/]*/,"")
    dominio=dominio.replace(/[^\w\.\+-:@]/g,"")
    caminho=caminho.replace(/[^\w\d\+-@:\?&=%\(\)\.]/g,"")
    caminho=caminho.replace(/([\?&])=/,"$1")
    if(caminho!="")dominio=dominio.replace(/\.+$/,"")
    v="http://"+dominio+caminho
    return v
}

function moeda(v){
    return v
}

function validar(campos)
{
	tinyMCE.triggerSave(); //atualiza os dados que estão sendo editados pelo TinyMCE
	campos_v=campos.split(",");
	for(x=0;x<=campos_v.length;x++){

		if(document.getElementById(campos_v[x]).type=='checkbox'){
			if(document.getElementById(campos_v[x]).checked==''){
				alert('Campo em branco!');
				document.getElementById(campos_v[x]).focus();
				return false;
				break;
			}
		}
		else{
			if(document.getElementById(campos_v[x]).value==''){
				alert('Campo em branco!');
				document.getElementById(campos_v[x]).focus();
				return false;
				break;
			}	
		}


	}
	return true;
}

function editar(campo)
{
	campo.style.border='solid 1px orange';
	campo.style.backgroundColor='#ffffff';
}

function visu(campo)
{
	campo.style.border='none';
	campo.style.backgroundColor='transparent';
	nome_coluna=campo.name;
	complemento_somar_coluna=nome_coluna.replace(/\w{1,}_(?=\w{1,}_form$)/gi,"");
	complemento_total_m2=nome_coluna.replace(/_\w{1,}_form$/gi,"");
	//alert(complemento_total_m2);
	calcula_total_m2(complemento_total_m2);
	calcula_total_preco(complemento_total_m2);
}

function calcula_total_preco(complemento)
{
	//alert(complemento);
	v1=parseFloat(verifica_NAN(document.getElementById(complemento+'_totalm2_form').value));
	v2=parseFloat(verifica_NAN(document.getElementById(complemento+'_valorm2_form').value));
	var vtotal = new Number(v1*v2);
	//alert(vtotal);
	if(vtotal!='' && vtotal!=0){
	document.getElementById(complemento+'_totalreal_form').value=vtotal.toFixed(2);
	}
	else{
		document.getElementById(complemento+'_totalreal_form').value='';
	}
	
	//soma os resultados dos m2
	v3=parseFloat(verifica_NAN(document.getElementById('Sala_totalreal_form').value));
	v4=parseFloat(verifica_NAN(document.getElementById('Corredor1_totalreal_form').value));
	v5=parseFloat(verifica_NAN(document.getElementById('Corredor2_totalreal_form').value));
	v6=parseFloat(verifica_NAN(document.getElementById('Quarto01_totalreal_form').value));	
	v7=parseFloat(verifica_NAN(document.getElementById('Quarto02_totalreal_form').value));
	v8=parseFloat(verifica_NAN(document.getElementById('Quarto03_totalreal_form').value));
	v9=parseFloat(verifica_NAN(document.getElementById('Quarto04_totalreal_form').value));
	v10=parseFloat(verifica_NAN(document.getElementById('Escada_totalreal_form').value));	
	v11=parseFloat(verifica_NAN(document.getElementById('Varanda_totalreal_form').value));
	v12=parseFloat(verifica_NAN(document.getElementById('Rodape_totalreal_form').value));
	v13=parseFloat(verifica_NAN(document.getElementById('Decks1_totalreal_form').value));
	
	var vtotalm2= new Number(v3+v4+v5+v6+v7+v8+v9+v10+v11+v12+v13);
	
	if(vtotalm2!='' && vtotalm2!=0){
		document.getElementById('Total_totalreal_form').value=vtotalm2.toFixed(2);
	}
	else{
		document.getElementById('Total_totalreal_form').value='';
	}
	
	//soma os resultados da segunda parte do formulario
		//soma os resultados dos m2
	v3=parseFloat(verifica_NAN(document.getElementById('Assoalhos_totalreal_form').value));
	v4=parseFloat(verifica_NAN(document.getElementById('Palitos_totalreal_form').value));
	v5=parseFloat(verifica_NAN(document.getElementById('Paineis_totalreal_form').value));
	v6=parseFloat(verifica_NAN(document.getElementById('Brises_totalreal_form').value));	
	v7=parseFloat(verifica_NAN(document.getElementById('Tacos_totalreal_form').value));
	v8=parseFloat(verifica_NAN(document.getElementById('Demolicao_totalreal_form').value));
	v9=parseFloat(verifica_NAN(document.getElementById('Jateado_totalreal_form').value));
	v10=parseFloat(verifica_NAN(document.getElementById('Escada2_totalreal_form').value));	
	v11=parseFloat(verifica_NAN(document.getElementById('Decks2_totalreal_form').value));
	v12=parseFloat(verifica_NAN(document.getElementById('Rodape2_totalreal_form').value));
	v13=parseFloat(verifica_NAN(document.getElementById('Revestsauna1_totalreal_form').value));
	
	var vtotalm2= new Number(v3+v4+v5+v6+v7+v8+v9+v10+v11+v12+v13);
	
	if(vtotalm2!='' && vtotalm2!=0){
		document.getElementById('Total2_totalreal_form').value=vtotalm2.toFixed(2);
	}
	else{
		document.getElementById('Total2_totalreal_form').value='';
	}

	return;
}

function calcula_total_m2(complemento)
{
	//alert(complemento);
	v1=parseFloat(verifica_NAN(document.getElementById(complemento+'_comp_form').value));
	v2=parseFloat(verifica_NAN(document.getElementById(complemento+'_larg_form').value));
	var vtotal = new Number(v1*v2);
	//alert(vtotal);
	if(vtotal!='' && vtotal!=0){
	document.getElementById(complemento+'_totalm2_form').value=vtotal.toFixed(2);
	}
	else{
		document.getElementById(complemento+'_totalm2_form').value='';
	}
	
	//soma os resultados dos m2
	v3=parseFloat(verifica_NAN(document.getElementById('Sala_totalm2_form').value));
	v4=parseFloat(verifica_NAN(document.getElementById('Corredor1_totalm2_form').value));
	v5=parseFloat(verifica_NAN(document.getElementById('Corredor2_totalm2_form').value));
	v6=parseFloat(verifica_NAN(document.getElementById('Quarto01_totalm2_form').value));	
	v7=parseFloat(verifica_NAN(document.getElementById('Quarto02_totalm2_form').value));
	v8=parseFloat(verifica_NAN(document.getElementById('Quarto03_totalm2_form').value));
	v9=parseFloat(verifica_NAN(document.getElementById('Quarto04_totalm2_form').value));
	v10=parseFloat(verifica_NAN(document.getElementById('Escada_totalm2_form').value));	
	v11=parseFloat(verifica_NAN(document.getElementById('Varanda_totalm2_form').value));
	v12=parseFloat(verifica_NAN(document.getElementById('Rodape_totalm2_form').value));
	v13=parseFloat(verifica_NAN(document.getElementById('Decks1_totalm2_form').value));
	
	var vtotalm2= new Number(v3+v4+v5+v6+v7+v8+v9+v10+v11+v12+v13);
	
	if(vtotalm2!='' && vtotalm2!=0)
	{
		document.getElementById('Total_totalm2_form').value=vtotalm2.toFixed(2);
	}
	else
	{
		document.getElementById('Total_totalm2_form').value='';
	}
	
	//soma os resultados da segunda parte do formulario
		//soma os resultados dos m2
	v3=parseFloat(verifica_NAN(document.getElementById('Assoalhos_totalm2_form').value));
	v4=parseFloat(verifica_NAN(document.getElementById('Palitos_totalm2_form').value));
	v5=parseFloat(verifica_NAN(document.getElementById('Paineis_totalm2_form').value));
	v6=parseFloat(verifica_NAN(document.getElementById('Brises_totalm2_form').value));	
	v7=parseFloat(verifica_NAN(document.getElementById('Tacos_totalm2_form').value));
	v8=parseFloat(verifica_NAN(document.getElementById('Demolicao_totalm2_form').value));
	v9=parseFloat(verifica_NAN(document.getElementById('Jateado_totalm2_form').value));
	v10=parseFloat(verifica_NAN(document.getElementById('Escada2_totalm2_form').value));	
	v11=parseFloat(verifica_NAN(document.getElementById('Decks2_totalm2_form').value));
	v12=parseFloat(verifica_NAN(document.getElementById('Rodape2_totalm2_form').value));
	v13=parseFloat(verifica_NAN(document.getElementById('Revestsauna1_totalm2_form').value));
	
	var vtotalm2= new Number(v3+v4+v5+v6+v7+v8+v9+v10+v11+v12+v13);
	
	if(vtotalm2!='' && vtotalm2!=0){
		document.getElementById('Total2_totalm2_form').value=vtotalm2.toFixed(2);
	}
	else{
		document.getElementById('Total2_totalm2_form').value='';
	}
	return;
}


function soma_coluna_comprimento1(complemento)
{
	v1=parseInt(verifica_NAN(document.getElementById('Sala_'+complemento).value));
	v2=parseInt(verifica_NAN(document.getElementById('Corredor1_'+complemento).value));
	v3=parseInt(verifica_NAN(document.getElementById('Corredor2_'+complemento).value));
	v4=parseInt(verifica_NAN(document.getElementById('Quarto_01_'+complemento).value));
	v5=parseInt(verifica_NAN(document.getElementById('Quarto_02_'+complemento).value));
	v6=parseInt(verifica_NAN(document.getElementById('Quarto_03_'+complemento).value));
	v7=parseInt(verifica_NAN(document.getElementById('Quarto_04_'+complemento).value));
	v8=parseInt(verifica_NAN(document.getElementById('Escada_'+complemento).value));
	v9=parseInt(verifica_NAN(document.getElementById('Varanda_'+complemento).value));
	v10=parseInt(verifica_NAN(document.getElementById('Rodape_'+complemento).value));
	v11=parseInt(verifica_NAN(document.getElementById('Decks1_'+complemento).value));
	vtotal=v1+v2+v3+v4+v5+v6+v7+v8+v9+v10+v11;

	document.getElementById('Total_'+complemento).value=vtotal;
	return;
}

function verifica_NAN(valor)
{
	if(valor=='')
	{
		valor=0;
		}
	return valor;
	}
