function amf2005_valid_date(l,dd,mm,yy)
{
    z='err';
	
    if (l == 6 || l == 8)
    {
        xx=yy;
        if (dd >= 1 && dd <= 31)
        {
            if (mm == 2 || mm == 4 || mm == 6 || mm == 9 || mm == 11)
            {
                if (dd <= 30)
                {
                    if (mm == 2)
                    {
                        if (dd <= 28)
                        {
                            z='ok';
                        }
                        else
                        {
                            if (dd == 29)
                            {
                                bb=xx%4;    // 4 by 4
                                if (bb == 0)
                                {
                                    cc=xx%100;    // 100 by 100
                                    if (cc == 0)
                                    {
                                        qq=xx%400;    // 400 by 400
                                        if (qq == 0)
                                        {
                                            z='ok';
                                        };
                                    }
                                    else
                                    {
                                        z='ok';
                                    };    
                                };
                            };
                        };
                    }
                    else
                    {
                        z='ok';
                    };
                };
            }
            else
            {
                if (mm >= 1 && mm <= 12) { z='ok'; };
            };
        };
    };
    ii=200;        // inner parameter
    if (xx < 1600 || xx > 2400 || xx < 2005 - ii || xx > 2005 + ii) { z='err'; };
    return z;
};

function amf2005_consist_date(dat)
{
    n='0123456789';
    d=dat.value;
    l=d.length;
    s='';
    for (i=0; i<l; i++)
    {
        c=d.charAt(i);
        if (n.indexOf(c) >= 0)
        {
            s+=c;
        };
    };
    l=s.length;
    xx='0000';
    r=s;
    if (l > 8)
    {
        r=s.substr(0,8);
        s=r;
        l=8;
    };
    if (l == 6)
    {
        dd=s.substr(0,2);
        mm=s.substr(2,2);
        yy=s.substr(4,2);
        if (yy < 50)
        {
            xx='20'+yy;
        }
        else
        {
            xx='19'+yy;
        };
        ww=dd+'/'+mm+'/'+yy;
    };
    if (l == 8)
    {
        dd=s.substr(0,2);
        mm=s.substr(2,2);
        yy=s.substr(4,4);
        xx=yy;
        ww=dd+'/'+mm+'/'+yy;
    };
    if (l == 6 || l == 8)
    {
        z=amf2005_valid_date(l,dd,mm,xx);
        if (z == 'ok') { r=ww; };
    };
	
    dat.value=r;
    return 'ok';
};

function amf2005_update_date(dat)
{
    n='0123456789';
    d=dat.value;
    l=d.length;
    s='';
    for (i=0; i<l; i++)
    {
        c=d.charAt(i);
        if (n.indexOf(c) >= 0)
        {
            s+=c;
			
        };
    };
    l=s.length;
    if (l == 6)
    {
		
        dd=s.substr(0,2);
        mm=s.substr(2,2);
        yy=s.substr(4,2);
        if (yy < 50)
        {
            yy='20'+yy;
        }
        else
        {
            yy='19'+yy;
        };
        ww=dd+'/'+mm+'/'+yy;
		
    };
    if (l == 8)
    {	
        dd=s.substr(0,2);
        mm=s.substr(2,2);
        yy=s.substr(4,4);
        ww=dd+'/'+mm+'/'+yy;
    };
    rr='';
	
    if (l == 6 || l == 8)
    {	
        z=amf2005_valid_date(l,dd,mm,yy);
        if (z == 'ok')
        { 
            rr=ww;
			
        };
    };
	dat.value=rr;
    return 'ok';
};



function amf2005_BecameCurrency(cur,len)
{
   n='__0123456789';
   d=cur.value;
   l=d.length;
   r='';
   if (l > 0)
   {
    z=d.substr(0,l-1);
    s='';
    a=2;
    for (i=0; i < l; i++)
    {
        c=d.charAt(i);
        if (n.indexOf(c) > a)
        {
            a=1;
            s+=c;
        };
    };
    l=s.length;
    t=len-1;
    if (l > t)
    {
        l=t;
        s=s.substr(0,t);
    };
    if (l > 2)
    {
        r=s.substr(0,l-2)+','+s.substr(l-2,2);
    }
    else
    {
        if (l == 2)
        {
            r='0,'+s;
        }
        else
        {
            if (l == 1)
            {
                r='0,0'+s;
            };
        };
    };
    if (r == '')
    {
        r='0,00';
    }
    else
    {
        l=r.length;
        if (l > 6)
        {
            j=l%3;
            w=r.substr(0,j);
            wa=r.substr(j,l-j-6);
            wb=r.substr(l-6,6);
            if (j > 0)
            {
                w+='.';
            };
            k=(l-j)/3-2;
            for (i=0; i < k; i++)
            {
                w+=wa.substr(i*3,3)+'.';
            };
            r=w+wb;
        };
    };
   };
   if (r.length <= len)
   {
    cur.value=r;
   }
   else
   {
    cur.value=z;
   };
   return 'ok';
};




function show(id,id2){

	if (document.getElementById(id2).checked == false){
		document.getElementById(id).style.visibility="hidden";
	}
	if (document.getElementById(id2).checked == true){
		document.getElementById(id).style.visibility="visible";
	
	}
	
}


 function preenchelista(pag,div,pag2)
 {
   /*----------------------------------------------------------------------------------------------*/
     // criacao do objeto XMLHTTP do arquivo ajax.js
     var oHTTPRequest = createXMLHTTP();
     oHTTPRequest.open("post",pag, true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
   // para solicitacoes utilizando o metodo post deve ser acrescentado
   // este cabecalho HTTP
     oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   // a funcao abaixo e executada sempre que o estado do objeto muda (onreadystatechange)
     oHTTPRequest.onreadystatechange=function(){
     // o valor 4 significa que o objeto ja completou a solicitacao
      if (oHTTPRequest.readyState==4)
	  		{// abaixo o texto gerado no arquivo executa.asp e colocado no div
	  
        		document.getElementById(div).innerHTML = oHTTPRequest.responseText;
				
		 	}
	  }
       oHTTPRequest.send("pag=" + pag2);
	   
  /*---------------------------------------------------------------*/
 }

function erase(id){
	
	document.getElementById(id).value="";
	
}

function hideAjax (id){
	document.getElementById(id).style.visibility="hidden";
}

function showAjax (id){
	document.getElementById(id).style.visibility="visible";	
}

function retornar(id,idSelect) {
	//alert(Tipo);	
   // opener.document.frm1.txtBusca.value = Tipo;

  document.getElementById(id).value =  document.getElementById(idSelect).value;
    //close();
}

function somar(){
	var valor1 = document.getElementById('custoImpressao');
	
	if (valor1.value == ""){
		valor1 = 0;
	}else {
		valor1 = valor1.value.replace(".","");
		valor1 = valor1.replace(".","");
		valor1 = valor1.replace(",",".");
	}
	
	var valor2 = document.getElementById('custoEditoracao');
	if (valor2.value == ""){
		valor2 = 0;
	}else {
		valor2 = valor2.value.replace(".","");
		valor2 = valor2.replace(".","");
		valor2 = valor2.replace(",",".");
	}
	
	var valor3 = document.getElementById('custoRedTrad');
	if (valor3.value == ""){
		valor3 = 0;
	}else {
		valor3 = valor3.value.replace(".","");
		valor3 = valor3.replace(".","");
		valor3 = valor3.replace(",",".");
	}
	
	var total = eval(parseFloat(valor1) + parseFloat(valor2) + parseFloat(valor3));	
	total = Math.round(total*100)/100;
	total = String(total);
	total = total.replace(".",",");
	var srt = total;
	if (srt.search(",") == -1){
	
		total = total +",00";  
	
	}	
	
	document.getElementById('custoTotal').value = total;
	
}



function calcula(operacao){
    var parcela1 = document.calc.parcela1.value;
    var parcela2 = document.calc.parcela2.value;
    var result = eval(parcela1 + operacao + parcela2);
    document.calc.resultado.value = result;
}
