// JavaScript Document

	function checkPhone( type , pStr )
	{
		
		switch (type){
		
			case 1:
				var rePhone = /([2-6]{1})+(\d{7})/;
				break;
			case 2:
				var rePhone = /([7-9]{1})+(\d{7})/;
				break;
		}
		

		if (rePhone.test(pStr)) {
			return true;
		} else {
			return false;
		}
	}

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( form ){
	
		resultInputs = "";
		
		arrForm = form.getElementsByTagName('select');
		
		for( x = 0 ; x < arrForm.length ; x++  ){
			
			if( form.getElementsByTagName('select')[x].title != "" )
				resultInputs += form.getElementsByTagName('select')[x].name+";"+form.getElementsByTagName('select')[x].title+",";
						
		}
		
		arrForm = form.getElementsByTagName('input');
		
		for( x = 0 ; x < arrForm.length ; x++  ){
			
			if( form.getElementsByTagName('input')[x].title != "" )
				resultInputs += form.getElementsByTagName('input')[x].name+";"+form.getElementsByTagName('input')[x].title+",";
						
		}

		arrForm = form.getElementsByTagName('textarea');
		
		for( x = 0 ; x < arrForm.length ; x++  ){
			
			if( form.getElementsByTagName('textarea')[x].title != "" )
				resultInputs += form.getElementsByTagName('textarea')[x].name+";"+form.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,contract,typeReturn,photo,inputCheck,allowedTypes){
		
		action = "";
		
		if( inputName == "" )
			inputName = getInputs(form);	
		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.<br />Selecione 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

	if( contract >= 1 ){
	
		if( document.getElementById("aceito").checked == false ){
			
			if( contract == 1 )
				msg += '; Marque a opção: <strong>Concordo com o termo de responsabilidade</strong>';
			else
				msg += '; Marque a opção: <strong>Concordo em receber a newsletter da Tratoranel</strong>';
			control = 1;
			quantMsg++;
			
		}
		
	}


	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+' inválida';
					control = 1;
					quantMsg++;
			
			}
			
		}
		
		if( ( type[1] == "tel" ) && ( document.getElementById(id).value != "" ) ){
		
			if( document.getElementById(id).name == "mobilePhone_tel" )
				type = 2;
			else
				type = 1;
		
			if( !checkPhone( type , document.getElementById(id).value) ){
					
					msg += ';<strong>'+fieldName+'</strong> inválido';
					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 invá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 invá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 += ";" + "O campo <strong>" + fieldName + "</strong> não foi preenchido";
			
			control = 1;
			quantMsg++;
			
			//d(arrData[i].split(";")[0]).style.border='2px solid #999999'
			document.getElementById(id).focus();
		
		}
				
				
				
	}
			
		if( control == 1 ){
			
			if(quantMsg == 1 ){
				msgStart = "<strong>Foi encontrado o seguinte erro:</strong><br /><br />"; 
				msgFinal = "";
			}else{
				msgStart = "<strong>Foram encontrados os seguintes erros:</strong><br /><br />"; 
				msgFinal = "";
			}
			
			msgArr = msg.split(";");
			msg = ""
			c = 1;
			for(w = msgArr.length -1 ; w > 0 ; w--){
				msg+= c + " - " + msgArr[w] + ";<br />";
				c++;
			}
			
			msgOut = msgStart+msg+msgFinal
			
			if( typeReturn == 1 ){
				
				for( i = 0 ; i < 50 ; i++ ){
					
					msgOut = msgOut.replace("<br />","\n")
					msgOut = msgOut.replace("<strong>","")
					msgOut = msgOut.replace("</strong>","")					
				
				}
				
				alert(msgOut);
			
			}else{	
			
				document.getElementById("javascriptError").innerHTML = msgOut;
				document.getElementById("javascriptError").style.display = "block";
				if(form.name != "proposal")
					window.scroll(0,200);
				else
					window.scroll(0,733);
			
			}
			
			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;
			}
		}
		
		
	/**
	* Verify if is not  number
	*
	* @since 05/07/2006
	* @author Jonathan de Souza Pereira <jonathan_pereira@hotmail.com>
	* @access public
	*/
	
		function onlyChar(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 >= 48 && key <= 57 )
				return false;
			
			else
				return true;
		}
