var dtCh= "/";
var minYear=1900;
var maxYear=2100;

// Percorre um campo radio button para ver o valor dele.
function percorre_radio(camporadio) {
 var tamanhocampo = camporadio.length
 for (contador = 0; contador < tamanhocampo; contador++) {
  if (camporadio[contador].checked) {
   var campomarcado = camporadio[contador].value;
   break;
  } 
 }
 return campomarcado;
}

function formataValor(campo,tammax,teclapres) 
{
      var tecla = teclapres.keyCode;
	  
	  if (tecla == 9) {
	  	event.returnValue = true;
		return true;
	  }
      
// if( tecla <  48 || tecla > 57 ) event.returnValue=false;
      
      vr = campo.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( ".", "" );
      tam = vr.length;

      if (tam <  tammax && tecla != 8){ tam = vr.length + 1 ; }

      if (tecla == 8 ){ tam = tam - 1 ; }
           
      if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla
<= 105 )
           with(campo)
           {
           if ( tam <= 2 ){ 
      value = vr ; }
      if ( (tam > 2) && (tam <= 5) ){
      value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
      if ( (tam >= 6) && (tam <= 8) ){
      value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ','
+ vr.substr( tam - 2, tam ) ; }
      if ( (tam >= 9) && (tam <= 11) ){
      value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.'
+ vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
      if ( (tam >= 12) && (tam <= 14) ){
      value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) +
'.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' +
vr.substr( tam - 2, tam ) ; }
      if ( (tam >= 15) && (tam <= 17) ){
      value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) +
'.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' +
vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
      if ( (tam >= 18) && (tam <= 20) ){
      value = vr.substr( 0, tam - 17 ) + '.' + vr.substr( tam - 17, 3 ) +
'.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' +
vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr(
tam - 2, tam ) ;}
   } 
      else
           event.returnValue=false;
      
}

// Função que verifica ao menos um check marcado
// Por Tiago Bonamigo
// Rafael acrescentou um break para que a rotina retorne true somente uma vez.

function isRadio(obj){
	if(!isNaN(obj.length)){
		for(i=0; i<obj.length; i++){
			if(obj[i].checked){
				return true;
				break;
			}
		}
	} else {
		if(obj.checked){return true}
	}
	return false;
}

function isCheck(obj){
	if(!isNaN(obj.length)){
		for(i=0; i<obj.length; i++){
			if(obj[i].checked){
				return true;
				break;
			}
		}
	} else {
		if(obj.checked){return true}
	}
	return false;
}

function isCheckAll(obj){
	if(!isNaN(obj.length)){
		for(i=0; i<obj.length; i++){
			if(!obj[i].checked){
				return false;
				break;
			}
		}
	} else {
		if(!obj.checked){
		return false;}
	}
	return true;
}

 // Validação de hora Client-Side
 function verificaHora(Hora)
   {
   var hm = -1;
   var hora = Array(2);
   var ch = Hora.charAt(0); 
   for(i=0; i < Hora.length && (( ch >= '0' && ch <= '9' ) || ( ch == ':' && i != 0 ) ); ){
    hora[++hm] = '';
    if(ch!=':' && i != 0) return false;
    if(i != 0 ) ch = Hora.charAt(++i);
    if(ch=='0') ch = Hora.charAt(++i);
    while( ch >= '0' && ch <= '9' ){
     hora[hm] += ch;
     ch = Hora.charAt(++i);
    } 
   }
   if(ch!='') return false;
   if(hora[0] == '' || isNaN(hora[0]) || parseInt(hora[0]) < 0 || parseInt(hora[0]) > 23) return false;
   if(hora[1] == '' || isNaN(hora[1]) || parseInt(hora[1]) < 0 || parseInt(hora[1]) > 59) return false;
   return true;
  }

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function isEmpty(param) {
	if (param.replace(/ /g,'').length < 1) return (true);
	return (false);
}

  function isEmail (email){ // Rotina para validação de email
    var arroba = false;
    var ponto = false;
    for (var i=0; i < email.length; i++) {
	if (email.substring(i-1,i) == "@") {
	  arroba = true;
	}
	if (email.substring(i-1,i) == ".") {
	  ponto = true;
	}
    }
    if ((arroba == false) || (ponto == false)){
	return false;}
    else {
	return true;
    }
  }

function alertErro(erro) {
	if (erro.length > 0)	 {
		alert('Ocorreram os seguintes erros:\n\n' + erro + '\nPor favor, corrija-os e tente novamente.')
		return(false);
	}
	return (true);
}

function moveSelect(p1,p2) {
	for (i=0;i<p2.options.length;i++) {
		if(p2.options[i].value == '') {
			p2.remove(i);
		}
	}
	for (i=0;i<p1.options.length;i++) {
		if(p1.options[i].selected == true) {
			var oOption = document.createElement("OPTION");
			p2.add(oOption);
			oOption.innerText = p1.options[i].innerText;
			oOption.value = p1.options[i].value;
			p1.remove(i);
			i--;
		}
	}
	if (p1.options.length > 0) orderSelect(p1,0,p1.options.length-1);
	if (p2.options.length > 0) orderSelect(p2,0,p2.options.length-1);
}

function orderSelect(p1,loBound,hiBound) {

	pivot = p1.options[parseInt((loBound + hiBound) / 2)].innerText;
	pivot2 = p1.options[parseInt((loBound + hiBound) / 2)].value;
	p1.options[parseInt((loBound + hiBound) / 2)].innerText = p1.options[loBound].innerText;
	p1.options[parseInt((loBound + hiBound) / 2)].value = p1.options[loBound].value;
	p1.options[loBound].innerText = pivot;
	p1.options[loBound].value = pivot2;
	loSwap = loBound + 1;
	hiSwap = hiBound;

	do {
		while (loSwap <= hiSwap && p1.options[loSwap].innerText <= pivot)
			loSwap++;

		while (p1.options[hiSwap].innerText > pivot)
			hiSwap--;

		if (loSwap < hiSwap)
		{
			temp = p1.options[loSwap].innerText;
			temp2 = p1.options[loSwap].innerText;
			p1.options[loSwap].innerText = p1.options[hiSwap].innerText;
			p1.options[loSwap].value = p1.options[hiSwap].value;
			p1.options[hiSwap].innerText = temp;
			p1.options[hiSwap].value = temp2;
		}
	} while (loSwap < hiSwap);

	p1.options[loBound].innerText = p1.options[hiSwap].innerText;
	p1.options[loBound].value = p1.options[hiSwap].value;
	p1.options[hiSwap].innerText = pivot;
	p1.options[hiSwap].value = pivot2;

	if (loBound < hiSwap - 1)
		orderSelect(p1, loBound, hiSwap - 1);

	if (hiSwap + 1 < hiBound)
		orderSelect(p1, hiSwap + 1, hiBound);
}

function selectAll(p1) {
	for (i=0;i<p1.options.length;i++) {
		p1.options[i].selected = true;
	}
	return true;
}