// JavaScript Document
var MS=navigator.appVersion.indexOf("MSIE")
window.isIE4 =(MS>0) && ((parseInt(navigator.appVersion.substring(MS+5,MS+6)) >= 4) && (navigator.appVersion.indexOf("MSIE"))>0)

function BlockTecla(block) {
var enter = 13 
var ctrl=window.event.ctrlKey;
var tecla=window.event.keyCode; 
//if (ctrl && tecla==67) {alert("CTRL+C"); event.keyCode=0; event.returnValue=false;}
//if (ctrl && tecla==86) {alert("CTRL+V"); event.keyCode=0; event.returnValue=false;}
// COPIE E COLE A LINHA ABAIXO PARA ADICIONAR BLOQUEIO A MAIS CARACTERES //
if (tecla==block) {event.keyCode=0; event.returnValue=false;}
}

function checkExpand() {
    if (""!=event.srcElement.id) {
      var ch = event.srcElement.id + "Child"
      var el = document.all[ch] 
      if (null!=el) el.style.display = "none" == el.style.display ? "" : "none"
      //event.returnValue=false
    }
  }
  
function Imprimir(){
	if (window.print) {
		window.print();
	}
	else {
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGTH=0 CLASSID="CLSID:8856F961-340A-11D0-A96B00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	WebBrowser1.ExecWB(6, 2);
	WebBrowser1.outerHTML = " ";
	}
} 

function EscreveJS(src) { 
document.write(src); 
} 

function imgPopUp (src){
PopUp('/img/img.asp?src='+src, '', 1, 1, 0, 0)
}
function PopUp(urlLoc, winName, w, h, showStatus, isViewer) {
		l = (screen.availWidth - w)/2;
		t = (screen.availHeight - h)/2;
		features  = "toolbar=no";      // yes|no 
		features += ",location=no";    // yes|no 
		features += ",directories=no"; // yes|no 
		features += ",status=" + (showStatus?"yes":"no");  // yes|no 
		features += ",menubar=no";     // yes|no 
		features += ",scrollbars=" + (isViewer?"yes":"no");   // auto|yes|no 
		features += ",resizable=" + (isViewer?"yes":"no");   // yes|no 
		features += ",dependent";      // close the parent, close the popup, omit if you want otherwise 
		features += ",height=" + h;
		features += ",width=" + w;
		features += ",left=" + l;
		features += ",top=" + t;
		winName = winName.replace(/[^a-z]/gi,"_");
		return window.open(urlLoc,winName,features);
} 
function DialogBox(sURL,vArguments,w,h,scr,res){ // scr e res = true | false
return window.showModalDialog(sURL, vArguments,"dialogWidth:"+w+"px;dialogHeight:"+h+"px;help:no;status:no;scroll:"+(scr?"yes":"no")+";resizable:"+(res?"yes":"no")+";")
}
function mOvr(src,clrOver) {
 if (!src.contains(event.fromElement)) {
  src.style.cursor = 'hand';
  src.bgColor = clrOver;
 }
}
function mOut(src,clrIn) {
 if (!src.contains(event.toElement)) {
  src.style.cursor = 'default';
  src.bgColor = clrIn;
 }
}
function mClk(src) {
 if(event.srcElement.tagName=='TD'){
  src.children.tags('A')[0].click();
 }
}

var errors=''

function FormataCpf(campo,tammax,teclapres) {
 var tecla = teclapres.keyCode;
 vr = event.srcElement.value;
 vr = vr.replace( "/", "" );
 vr = vr.replace( "/", "" );
 vr = vr.replace( ",", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( "-", "" );
 vr = vr.replace( "-", "" );
 vr = vr.replace( "-", "" );
 vr = vr.replace( "-", "" );
 vr = vr.replace( "-", "" );
 event.srcElement.value = vr ;
}

function ValidaCGC(pcgc) {//aaaaaaaaaaaaaaaaaaaa
       // verifica o tamanho
 if (pcgc.length != 14) {sim=false}
 else {sim=true}
if (sim){  // verifica se e numero   bbbbbbbbbbbbbbbbbbbbbbb
  for (i=0;((i<=(pcgc.length-1))&& sim); i++)  { //1111111111
   val = pcgc.charAt(i)
       // alert (val)
   if ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") && (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
   } //1111111111111
   if (sim) { // se for numero continua
    m2 = 2
    soma1 = 0
    soma2 = 0
    for (i=11;i>=0;i--) {//222222222222222
		 val = eval(pcgc.charAt(i))
		   // alert ("Valor do Val: "+val)
		 m1 = m2
		  if (m2<9) { m2 = m2+1}
		  else {m2 = 2}
		  soma1 = soma1 + (val * m1)
		  soma2 = soma2 + (val * m2)
			}  // fim do for de soma 22222222222222222222222222
  soma1 = soma1 % 11
  if (soma1 < 2) {  d1 = 0}
  else { d1 = 11 - soma1 }
  soma2 = (soma2 + (2 * d1)) % 11
  if (soma2 < 2) { d2 = 0}
   else { d2 = 11- soma2}
  if ((d1==pcgc.charAt(12)) && (d2==pcgc.charAt(13))) { sim = true } 
  else {sim = false}
   }  //bbbbbbbbbbbbbbbbbbbbb
}
  if (sim) { 
  return false
  } else  { return true }
 }  //aaaaaaaaaaaaaaaaaaaaaaaaaa

function ValidaCPF(pcpf) {  //#####################################################
 if (pcpf.length != 11 || pcpf==11111111111 || pcpf==22222222222 || pcpf==33333333333 || pcpf==44444444444 || pcpf==55555555555 || pcpf==66666666666 || pcpf==77777777777 || pcpf==88888888888 || pcpf==99999999999 || pcpf==00000000000 ) 
 {sim=false}
 else 
 {sim=true}

if (pcpf.length > 0) {
	  if (sim )  // valida o primeiro digito
	  {
	  for (i=0;((i<=(pcpf.length-1))&& sim); i++)  {
	   val = pcpf.charAt(i)
	   if((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") && (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
	   }
	
	   if (sim)  {
		soma = 0
		for (i=0;i<=8;i++)    {
		 val = eval(pcpf.charAt(i))
		 soma = soma + (val*(i+1))
		}
	
		resto = soma % 11
		if (resto>9) dig = resto -10
		else  dig = resto
		if (dig != eval(pcpf.charAt(9))) { sim=false }
	   else   // valida o segundo digito
		{
	
		 soma = 0
		for (i=0;i<=7;i++)     {
		 val = eval(pcpf.charAt(i+1))
		  soma = soma + (val*(i+1))
		}
	
		 soma = soma + (dig * 9)
		resto = soma % 11
		 if (resto>9) dig = resto -10
		 else  dig = resto
	   if (dig != eval(pcpf.charAt(10))) { sim = false }
		else sim = true
	   }
	   }
	  }
}
  if (sim) { 
  return false
  } else  { return true }
 }//-----------------------------------------------------------------

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.title; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) 
	  { if (val.search(emailRegEx) == -1) errors+='- '+nm+' não é um e-mail válido.\n';} 
		else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' só pode conter números.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' tem que conter números entre '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' é requerido(a).\n'; }
	if (test.indexOf('isCPF')!=-1) { if (ValidaCGC(val) && ValidaCPF(val)) errors += '- CPF ou CNPJ inválido.\n';}
  }
//  ValidaCPFCGC(Form.NR_CPFCGC.value);
  if (errors) alert('Ocorreram os seguintes erros:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function CheckLen(target) {
   	StrLen = target.value.length; 
  if (target.name == "DSOBS") {
    if (StrLen == 1 && document.Form.DSOBS.value.substring(0,1) == " ")
	{ 
        document.Form.DSOBS.value = ""
        StrLen = 0
    }
    if (document.Form.callback.value != "") {
       StrLen = StrLen + document.Form.callback.value.length;
      } 
       if (StrLen > 90) {
        document.Form.DSOBS.value = document.Form.DSOBS.value.substring(0,document.Form.DSOBS.value.length-1)
        CharsLeft = 0  
       }
       else
       {
        CharsLeft = 90 - StrLen
       }
  }
  else {
    if (StrLen == 1 && document.Form.callback.value.substring(0,1) == " ")
    { 
        document.Form.callback.value = ""
        StrLen = 0
    }
    if (document.Form.callback.value != "") {
       StrLen = StrLen + document.Form.DSOBS.value.length;
      }
    else
    {
        StrLen = StrLen + document.Form.DSOBS.value.length;
    }
    if (StrLen > 90) {
        document.Form.callback.value = document.Form.callback.value.substring(0,document.Form.callback.value.length-1)
        CharsLeft = 0  
    }
    else
    {
        CharsLeft = 90 - StrLen
    }
  }
    document.Form.Anzahl.value = CharsLeft
}
//-------------------------------------------------------------------------------------
//<form name="form1" action="teste.php"> 
     
//    <input type="Text" name="teste1" size="17" maxlength="17" onkeydown="FormataCampo(this,event,'##/##:##-## ##:##')"> 
     
//</form> 

function FormataCampo(Campo,teclapres,mascara){ 
    //pegando o tamanho do texto da caixa de texto com delay de -1 no event 
    //ou seja o caractere que foi digitado não será contado. 
    strtext = Campo.value 
    tamtext = strtext.length 
    //pegando o tamanho da mascara 
    tammask = mascara.length 
    //criando um array para guardar cada caractere da máscara 
    arrmask = new Array(tammask)     
    //jogando os caracteres para o vetor 
    for (var i = 0 ; i < tammask; i++){ 
        arrmask[i] = mascara.slice(i,i+1) 
    }  
    //alert (teclapres.keyCode) 
    //começando o trabalho sujo 
    if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))){ 
        if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){ 
            Organiza_Casa(Campo,arrmask[tamtext],teclapres.keyCode,strtext)         
        } 
        else{ 
            Detona_Event(Campo,strtext) 
        } 
    } 
    else{//Aqui funcionaria a mascara para números mas eu ainda não implementei 
        if ((arrmask[tamtext] == "A"))    { 
            charupper = event.valueOf() 
            //charupper = charupper.toUpperCase() 
            Detona_Event(Campo,strtext) 
            masktext = strtext + charupper  
            Campo.value = masktext 
        } 
    } 
} 
function Organiza_Casa(Campo,arrpos,teclapres_key,strtext){ 
    if (((arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres_key == 8)){ 
        separador = arrpos 
        masktext = strtext + separador 
        Campo.value = masktext 
    } 
} 
function Detona_Event(Campo,strtext){ 
    event.returnValue = false;
	
    if (strtext != "") { 
        Campo.value = strtext 
    } 
} 
function Tamanhodata(Daata){ 
    Dteste = daata.value
    if (Dteste.lenght < 10) alert('Data no formato inválido') ; 
} 

//------------------------------------------------
 
           
          
