/*
* Função desenvolvida para facilitar a criação do SQL
* baseado nos campos de um form.
* Como Usar
* 	1º - Cria um Button
*   2º - Criar uma div com o id "textSQL"
*   3º - Criar um button com o action onclick="createSQL('<nome do formulario>');"
*/
function createSQL(formulario) {

	var sqlText = "";
	var nText   = 0;
	
	sqlText = "CREATE TABLE `nome_da_tabela` (<br>";
	
	var numElem = eval(document.getElementById(formulario).length);
	var Obj     = document.getElementById(formulario);
	
	for(var i = 0; i < numElem; i++) {
		if((Obj.elements[i].type == "text") || (Obj.elements[i].type == "hidden") || (Obj.elements[i].type == "select-one")) {
			if(i < numElem) {
				sqlText += "`"+Obj.elements[i].id+"` VARCHAR(10) NOT NULL,<br>";
			} else {
				sqlText += "`"+Obj.elements[i].id+"` VARCHAR(10) NOT NULL";
			}
		}
	}
	sqlText += ") ENGINE = MYISAM;";
	// Enviando pra Div
	document.getElementById("textSQL").innerHTML = sqlText;
	
}
/*
* checkForm(form);
* Função pra verificar os campos de um form basta atribuir a propriedade title="<nome do campo>" ao campo do form
*/
function checkForm(form) {
	var numElement = eval(document.getElementById(form).length);
	var i;
	var Obj = document.getElementById(form);
	var erro = "";
	
	for (i = 0; i < numElement; i++) {
		// VALOR DO SELECT
		var index = Obj.elements[i].selectedIndex;
		
		// CAMPO DE TEXTO
		if ((Obj.elements[i].type == "text") && (Obj.elements[i].title != "") && (Obj.elements[i].value == "")) {
			erro += "- O Campo "+Obj.elements[i].title+".\n";
			Obj.elements[i].focus();
			break;
		}
		
		// CAMPO DE SELECT
		if ((Obj.elements[i].type == "select-one") && (Obj.elements[i].title != "") && (Obj.elements[i].options[index].index == 0)) {
			erro += "- O Campo "+Obj.elements[i].title+" deve ser selecionado um item.\n";
			Obj.elements[i].focus();
			break;
		}
		
		// CAMPO PASSWORD
		if ((Obj.elements[i].type == "password") && (Obj.elements[i].title != "") && (Obj.elements[i].value == "")) {
			erro += "- O Campo "+Obj.elements[i].title+".\n";
			Obj.elements[i].focus();
			break;
		}
		
	}
	
	if (erro != "") {
		alert("Por Favor verifique o Campo:\n\n"+erro);
		return false;
	} else {
		return true;
	}
}
/*
* messageAlert(mensagem,location,janela);
* Função exibir uma mensagem personalizada e Redirecionar
*/
function messageAlert(mensagem,location,janela, extra) {
	
	if(extra != "") {
		extra = ", "+extra;
	}
	
	if ((mensagem != "") && (janela != "")) {
	
		if(confirm(''+mensagem+'')) {
			window.open(''+location+'','Janela','fullscreen=no, scrollbars=yes'+extra);
		}
		
	} else if(mensagem != "") {
		
		if(confirm(''+mensagem+'')) {
			window.location.href = ''+location+'';
		}
		
	} else {
			if(janela == "") {
				window.location.href = ''+location+'';
			} else {
				window.open(''+location+'','Janela','fullscreen=no, scrollbars=yes'+extra);
			}
	}
	
}
/*
* checkPassword(campo1, campo2);
* Função para verificar se um campo é identido do outro
*/
function checkPassword(ID1, ID2) {
	var valor1 = document.getElementById(ID1).value;
	var valor2 = document.getElementById(ID2).value;
	
	if (valor1 != valor2) {
		alert("A senha digitada não confere!!");
		document.getElementById(ID2).focus;
		return false;
	} else {
		return true;
	}
	
}

/*
* onlyNumber(e)
* Função para habilitar apenas numeros no campo text
* onkeypress="return onlyNumber(event);"
*/
function onlyNumber(e){
    var tecla = (window.event) ? event.keyCode: e.which;
    if((tecla > 47 && tecla < 58) || (tecla == 44) || (tecla == 46)) {
			return true;
    } else{
			if (tecla != 8) {
				return false;
			}	else {
				return true;
			}
		}
}

/*
* checkEmail(email)
* Função para verificar se o e-mail é valido
* EX.: checkEmail(this.value);
*/
function checkEmail(email) {
  var CharValido = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  var erro 		 = "";
  var valido     = true;
  var i;
  
  for (i=0; i < email.length; i++) {
    var letras = email.charAt(i).toLowerCase();
    if (CharValido.indexOf(letras) != -1)
      continue;
      valido = false;
      break;
  }
  
  if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
   	erro += "Não foi encontrado o caracter @ e-mail.\n";
  } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
    erro += "Não foi encontrado o nome do servidor no e-mail.\n";
  } else if (email.indexOf("@") == email.length) {
	erro += ("@2");
  } else if (!valido) {
    erro += "Existe caracteres inválidos no e-mail.\n";
  }
  
  if (erro != "") {
    alert("Por Favor verifique os seguintes erro no campo e-mail:\n\n"+erro);
  } else {
    return true
  }
  
}

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))
                 alert("CPF Inválido");
            return true;
            }
      else
              alert("CPF Inválido");
      }

