function abrirPopUp(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function mostrar(id){
	za = MM_findObj(id);
	za.style.display='block';
}

function esconder(id){
	za = MM_findObj(id);
	za.style.display='none'
}

function mostrar_esconder(id){
	za = MM_findObj(id);
	if(za.style.display == 'none'){
		za.style.display='block';
	}else{
		za.style.display='none'
	}
}


//Checa Hora
function valida_Hora(v){
	if(v.length!=5||v.substr( 0, 2  ) > 23||v.substr( 0, 2  ) < 0||v.substr( 3, 2  ) > 59||v.substr( 3, 2  ) < 0){
		return false;
	}else{
		return true;
	}
}

//Formata data
function FormataData(Campo,teclapres) 
{
	var tecla = teclapres.keyCode;
	vr = document.form[Campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 )
	{
		if ( tam > 2 && tam < 5 )
			document.form[Campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}

//Formata Hora
function FormataHora(Campo,teclapres) 
{
	var tecla = teclapres.keyCode;
	vr = document.form[Campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ":", "" );
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 )
	{
		if ( tam > 2)
			document.form[Campo].value = vr.substr( 0, 2 ) + ':' + vr.substr(  tam - 2, tam );
	}
}

//Muda de campo após atingir o tamanho máximo
function DFchangeField(f){
	if(f.value.length==f.maxLength){
  	for(var i=0;i<f.form.length;i++){
    	if(f.form[i]==f&&f.form[i+1]){f.form[i+1].focus();break}
		}
	}
}

function somente_numero(){
	if (window.event.keyCode < 48 || window.event.keyCode > 57) {
			window.event.keyCode=0;
			return false;
	}
	return true		
}

function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;

}


function MM_validateForm() { //v4.0
  var i,p,q,nm,nMsg,test,num,tipo,data,d,m,Y,min,max,h,campoOk,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { 
	  test=args[i+2]; 
	  nMsg=args[i+1]; 
	  val=MM_findObj(args[i]);
	  campoOk=0;
    if (val) { 
		nm=val.name;
		tipo = "";
		if(nm == undefined){
			tipo = val[0].type;
		}
		
		if(test.indexOf('isHora')!=-1){
			if(trim(val.value) == "" || valida_Hora(trim(val.value)) == false){
				errors+='- '+nMsg+': Hora inválida.\n';
			}
		}else if(test.indexOf('isCEP')!=-1){
			if(trim(val.value) == "" || valida_CEP(trim(val.value)) == false){
				errors+='- '+nMsg+': CEP inválido.\n';
			}
		}else if(test.indexOf('isCPF')!=-1){
			if(trim(val.value) == "" || DFcheckCpf(trim(val.value)) == false){
				errors+='- '+nMsg+': CPF inválido.\n';
			}
		}else if(test.indexOf('isCNPJ')!=-1){
			if(trim(val.value) == "" || valida_CNPJ(trim(val.value)) == false){
				errors+='- '+nMsg+': CNPJ inválido.\n';
			}
		}else if(nm == "concordo"){
			if(val.checked == false){
				errors+='- '+nMsg+': você precisa concordar com os Termos.\n';
			}
		}else if(tipo == "radio" || tipo == "checkbox"){
			for(h=0;h<val.length;h++){
				if(val[h].checked == true){
					campoOk=1;
				}
			}
			if(campoOk == 0) errors+='- '+nMsg+': você precisa indicar uma opção.\n';
		}else if(val.type == "select-one" || val.type == "select"){
				if(val.selectedIndex < 1)  errors+='- '+nMsg+': você precisa selecionar um.\n';
		}else if ((val=val.value)!="") {
			if (test.indexOf('isEmail')!=-1) { 
				p=val.indexOf('\@');
				if (p<1 || val.indexOf(',') > 1 || p==(val.length-1)) errors+='- '+nMsg+' precisa ser um e-mail.\n';
			} else if (test!='R') {
				if (isNaN(val)) errors+='- '+nMsg+' deve conter apenas números.\n';
				if (test.indexOf('inRange') != -1) {
					p=test.indexOf(':');
					min=test.substring(8,p); max=test.substring(p+1);
					if ((val*1)<(min*1) || (max*1)<(val*1)) errors+='- '+nMsg+' deve conter um número entre '+min+' e '+max+'.\n';
				} 
			} 
		} else if (test.charAt(0) == 'R') errors += '- '+nMsg+' é requerido / is required.\n'; }
  } if (errors) alert('O envio gerou o(s) seguinte(s) avisos / The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
