// JavaScript Document

function valida_cpf(cpf)
      {
      var numeros, digitos, soma, i, resultado, digitos_iguais;
      digitos_iguais = 1;
      if (cpf.length < 11)
            return false;
      for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      }

function valida_cnpj(cnpj)
      {
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      } 

function doDate(pStr)
{

	var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	
	if (reDate.test(pStr)) {
		return true;
	} else if (pStr != null && pStr != "") {
		return false
	}
} // doDate

function checkMail(emailad){
	
    var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
    var check=/@[\w\-]+\./;
    var checkend=/\.[a-zA-Z]{2,3}$/;
    if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
        return false;
    }
    else {
        return true;
    }
	
}
	/**
	* Validate Mail
	* @Event OnSubmit
	* @since 27/03/2007
	* @author Michael Rodrigues Mafort <michaelmafort@gmail.com.com>
	* @access public
	*/

function validateMail( email ) {
	email_str = email.split("@");
	invalidChar = "!#$%¨&*()+=-;:>,<]}~^[{´`\\/|";
	check_num = 0;
	msg_txt = "";

	if( email_str.length == 2 ){
		user = email_str[0];
		domain = email_str[1];
		
	if( ( domain.indexOf(".") == -1 ) || ( domain.indexOf(".") == ( domain.length ) ) ){
		check_num = 1;
		msg_txt = "E-mail com formato inválido.";
	}
	for( i = 0 ; i < invalidChar.length; i++ ){
		if( email.indexOf( invalidChar.charAt( i ) ) != -1 ){
			check_num = 1;
			msg_txt = "E-mail com caracteres inválidos.";
		}
	}
	}else{
		check_num = 1;
		msg_txt = "Este e-mail não é válido.";
	}
	
	if( check_num == 1 ){
		return msg_txt;
	}else{
		return "success";
	}
}

	function getInputs(){
	
		arrForm = document.getElementsByTagName('input');
		
		resultInputs = "";
	
		for( x = 0 ; x < arrForm.length ; x++  ){
			
			if( document.getElementsByTagName('input')[x].title != "" )
				resultInputs += document.getElementsByTagName('input')[x].name+";"+document.getElementsByTagName('input')[x].title+",";
						
		}
		
		arrForm = document.getElementsByTagName('textarea');
		
		for( x = 0 ; x < arrForm.length ; x++  ){
			
			if( document.getElementsByTagName('textarea')[x].title != "" )
				resultInputs += document.getElementsByTagName('textarea')[x].name+";"+document.getElementsByTagName('textarea')[x].title+",";
						
		}
		
		resultInputs = resultInputs.substr(0,(resultInputs.length-1))
		
		return resultInputs;
		
	} 

	/**
	* Required forms
	* @Event OnSubmit
	* @since 11/09/2006
	* @author Michael Rodrigues Mafort <michaelmafort@gmail.com.com>
	* @access public
	* @sample required('input1;Nome,input2;Idade',this,1 or 0,'photo','jpg')
	*/


	function required(inputName,form,photo,inputCheck,allowedTypes){
		
		action = "";
		
		if( inputName == "" )
			inputName = getInputs();	
		else
			inputName = inputName;

 		arrData = inputName.split(",");
		
		d = document.getElementById;
		msg = ""
		quantMsg = 0;
		control = 0;
		
	//Função para verificar se o arquivo pode ser carregado;

	if(photo == 1){
	
		alloweds = allowedTypes.split(",");
		check = document.getElementById(inputCheck).value.substr( document.getElementById(inputCheck).value.length-3, 3 );
		breakPoint = 1;
		
			for( i = 0; i < alloweds.length; i++ ){
			
				if( check.toUpperCase() == alloweds[i].toUpperCase() ){
					
					breakPoint = 0;
				
				}else if( check == "" ){
				
					breakPoint = 2;
					
				}
				
			}
			
			if( breakPoint == 1 ){
				
				alert("Extensão \""+check+"\" não permitido para upload.\nSelecione apenas arquivos com extensão: "+allowedTypes);
				return false;
				
			}else if( breakPoint == 2){
			
				alert("Não foi carregado nenhum arquivo.");
				return false;
			
			}else{
				
				action = 1;
			
		}

	}
	
//Fim da função

	for( i = arrData.length-1; i > -1 ; i-- ){
		elem_obj = new Array();
		elem_obj = arrData[i].split(";");
		id = elem_obj[0];
		fieldName = elem_obj[1];
		
		type = document.getElementById(id).name.split("_");
		
		if( ( type[1] == "dat" ) && ( document.getElementById(id).value != "" ) ){
		
			if( !doDate(document.getElementById(id).value) ){
					
					msg += ';'+fieldName+' válida';
					control = 1;
					quantMsg++;
			
			}
			
		}
	
		if( ( (document.getElementById(id).name == "mail_str") || (document.getElementById(id).name == "mail_aux") ) && document.getElementById(id).value != "" ){
		
			if( ( checkMail(document.getElementById(id).value)==false ) && (document.getElementById(id).value != "")){
				msg += ';E-mail válido';
				control = 1;
				quantMsg++;
			}
		}
		
		if( ( (document.getElementById(id).name == "cpf_str") || (document.getElementById(id).name == "cpf_aux") ) && document.getElementById(id).value != "" ){
		
			if( document.getElementById(id).value != "" ){
				
				if ( ( valida_cpf(document.getElementById(id).value) == true ) || ( valida_cnpj(document.getElementById(id).value) == true ) ){
					msg += "";
				}else{
					msg += ';Cpf/Cnpj válido';
					control = 1;
					quantMsg++;					
				}			
			}
		}
		
		if( document.getElementById(id).name == "password_str" && document.getElementById(id).value != "" ){
		
			if( document.getElementById(id).value.length < 6 ){
			
				msg +=';A senha deve conter no mínimo 6 caracteres';
				control = 1;
				quantMsg++;
			
			}else{
			
				if(document.getElementById(id).value != document.getElementById("confirmPassword_aux").value ){
					msg += ';Senha e confimação da senha devem ser idênticos';
					control = 1;
					quantMsg++;
				}
				
			}
		
		}
				
		if( document.getElementById(id).value == "" ){
			
			msg += ";" + fieldName;
			
			control = 1;
			quantMsg++;
			
			//d(arrData[i].split(";")[0]).style.border='2px solid #999999'
			document.getElementById(id).focus();
		
		}
				
				
				
	}
		
		if( control == 1 ){
			
			if(quantMsg == 1 ){
				msgStart = "Necessário:\n\n"; 
				msgFinal = "";
			}else{
				msgStart = "Necessários:\n\n"; 
				msgFinal = "";
			}
			
			msgArr = msg.split(";");
			msg = ""
			
			for(w = msgArr.length -1 ; w > 0 ; w--){
				msg+= "- " + msgArr[w] + "\n";
			}
			
			msgOut = msgStart+msg+msgFinal
			alert(msgOut);
			return false;
		
		}else{
		
			action += 1;
		
		}
		
		return true;

	}

	
	/**
	* Limit TextArea
	* @since 23/03/2007
	* @author Michael Rodrigues Mafort <michaelmafort@gmail.com.com>
	* @access public
	* @sample checkchars(textarea , maxNum )
	*/
	
	
	function checkchars(input , maxCar) { 
		if (input.value.length > maxCar) { 
			alert("O máximo de caracteres é "+maxCar+"."); 
			input.value = input.value.substring(0,maxCar);
			return false; 
		} 
			else return true; 
	} 


	/**
	* Verify if is number
	*
	* @since 05/07/2006
	* @author Jonathan de Souza Pereira <jonathan_pereira@hotmail.com>
	* @access public
	*/
	
		function num(e) {
		
			if(window.event) {
			// for IE, e.keyCode or window.event.keyCode can be used
			key = e.keyCode;
			}
			else if(e.which) {
			// netscape
			key = e.which;
			}
			if (key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8)) || (key==45);
			{
			return true;
			}
		}
		
	/**
	* Verify if is number and accept point
	*
	* @since 05/07/2006
	* @author Jonathan de Souza Pereira <jonathan_pereira@hotmail.com>
	* @access public
	*/
	
		function numAndPoint(e) {
				
			if(window.event) {
			// for IE, e.keyCode or window.event.keyCode can be used
			key = e.keyCode;
			}
			else if(e.which) {
			// netscape
			key = e.which;
			}
			if (key!=46 || key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8) || (key==46)) || (key==45);
			{
			return true;
			}
		}
