function validaSerialNumber(obj){
	if (document.getElementById("produto_sn").value.length != 0){
		if(document.getElementById("produto_sn").value.length > 5 ){
			var igual = 1;
			txt_sn = document.getElementById("produto_sn").value;
			char_sn = txt_sn.charAt(0);
			for(i=1;i<txt_sn.length;i++){
				if(txt_sn.charAt(i)==char_sn){
					igual++;
				}
			}
			if(igual==txt_sn.length){
				inputTextColoring(obj, "#FFAFB2");
				return false;
			}
			
			var seq = 1;
			for(i=1;i<txt_sn.length;i++){
				int1=parseInt(txt_sn.charAt(i-1));
				int2=parseInt(txt_sn.charAt(i))-1;
				if(int1==int2){
					seq++;
				}
			}
			if(seq==txt_sn.length){
				inputTextColoring(obj, "#FFAFB2");
				return false;
			}
			
			var dseq = 1;
			for(i=1;i<txt_sn.length;i++){
				dint1=parseInt(txt_sn.charAt(i-1))-1;
				dint2=parseInt(txt_sn.charAt(i));
				if(dint1==dint2){
					dseq++;
				}
			}
			if(dseq==txt_sn.length){
				inputTextColoring(obj, "#FFAFB2");
				return false;
			}
			
			inputTextColoring(obj, "#ffffff");
			return true;
		}else{
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
	}
	else { 
		inputTextColoring(obj, "#ffffff");
		return true;
	}
}

function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
		destination.focus()
}


function inputTextColoring(obj, color){
	obj.style.background = color;
}

function validaData(dia, mes, ano) {
	if ((ano > 1900)&&(ano < 2100)){
		switch(mes){
			case '1':
			case '01':
			case '3':
			case '03':
			case '5':
			case '05':
			case '7':
			case '07':
			case '8':
			case '08':
			case '10':
			case '12':
				if (dia <= 31){
					return true;
				}
				break;
			case '4':
			case '04':
			case '6':
			case '06':
			case '9':
			case '09':
			case '11':
				if (dia <= 30){
					return true;
				}
				break;
			case '2':
				var bissexto;

				if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)){
					bissexto = 1;
				}
				if ((bissexto == 1) && (dia <= 29)){
					return true;
				}
				if ((bissexto != 1) && (dia <= 28)){
					return true;
				}
				break;
			case '02':
				var bissexto;

				if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)){
					bissexto = 1;
				}
				if ((bissexto == 1) && (dia <= 29)){
					return true;
				}
				if ((bissexto != 1) && (dia <= 28)){
					return true;
				}
				break;
		}
	}
	
	return false;
}

function validaTextOnly(obj, tamanho){
	var regexp = new RegExp("^[A-Z\s]{1," + tamanho + "}$", "gi");
	
	if(obj.value.match(regexp)){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

//function validaDiaAtual(obj){
//	if (obj.value > dataAtual){
//	  return true
//	} else {
//	  return false
//	}	
//}

function validaDataDia(obj){
	var regexp = new RegExp("^[0-3]{0,1}[0-9]{1}$", "gi");
	
	if(obj.value.match(regexp) && obj.value > 0 && obj.value <= 31){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function validaDataMes(obj){
	var regexp = new RegExp("^[0-1]{0,1}[0-9]{1}$", "gi");
	
	if(obj.value.match(regexp) && obj.value > 0 && obj.value <= 12){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function validaDataDiaAllowNull(obj){
	var regexp = new RegExp("^[0-3]{0,1}[0-9]{1}$", "gi");
	
	if(obj.value.length == 0){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		if(obj.value.match(regexp) && obj.value > 0 && obj.value <= 31){
			inputTextColoring(obj, "#ffffff");
			return true;
		}
		else{
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
	}
}

function validaDataMesAllowNull(obj){
	var regexp = new RegExp("^[0-1]{0,1}[0-9]{1}$", "gi");
	
	if(obj.value.length == 0){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		if(obj.value.match(regexp) && obj.value > 0 && obj.value <= 12){
			inputTextColoring(obj, "#ffffff");
			return true;
		}
		else{
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
	}
}

function validaDataAno(obj){
	var regexp = new RegExp("^[1-2]{1}[09]{1}[0-9]{1}[0-9]{1}$", "gi");
	
	if(obj.value.match(regexp) && obj.value > 1900 && obj.value <= 2008){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function validaDataAnoSamsung(obj){
	var regexp = new RegExp("^[2]{1}[0]{1}[0-9]{1}[0-9]{1}$", "gi");
	
	if(obj.value.length == 0){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		if(obj.value.match(regexp) && obj.value >= 1969 && obj.value <= (new Date()).getFullYear()){
			inputTextColoring(obj, "#ffffff");
			return true;
		}
		else{
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
	}
}

function mascaraCPF(cpf){
    cpf = cpf.replace(/\D/g,"");
    cpf = cpf.replace(/(\d{3})(\d)/,"$1.$2");
    cpf = cpf.replace(/(\d{3})(\d)/,"$1.$2");
    cpf = cpf.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
    return cpf;
}


function aplicaMascaraCPF(obj){
	obj.value = mascaraCPF(obj.value);
}

function validaCPF(obj){
	var cpf = obj.value;
	cpf = cpf.replace(/[-]/gi, "");
	cpf = cpf.replace(/[.]/gi, "");
	
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	
	if (cpf.length < 11){
		inputTextColoring(obj, "#FFAFB2");
		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)){
			inputTextColoring(obj, "#FFAFB2");
			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)){
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
		
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function mascaraCNPJ(cnpj){
    cnpj = cnpj.replace(/\D/g,"");
    cnpj = cnpj.replace(/^(\d{2})(\d)/,"$1.$2");
    cnpj = cnpj.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3");
    cnpj = cnpj.replace(/\.(\d{3})(\d)/,".$1/$2");
    cnpj = cnpj.replace(/(\d{4})(\d)/,"$1-$2");
    return cnpj;
}

function aplicaMascaraCNPJ(obj){
	obj.value = mascaraCNPJ(obj.value);
}

function validaCNPJ(obj){
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	var cnpj = obj.value;
	
	cnpj = cnpj.replace(/[-]/gi, "");
	cnpj = cnpj.replace(/[.]/gi, "");
	cnpj = cnpj.replace(/[\/]/gi, "");
	
	digitos_iguais = 1;
	
	if (cnpj.length != 14){
		inputTextColoring(obj, "#FFAFB2");
		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)){
			inputTextColoring(obj, "#FFAFB2");
			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)){
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
		
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
} 

function validaEmail(obj){
	var regexp = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", "gi");
	
	if(obj.value.match(regexp) && obj.value.length <= 100){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function validaNumberOnlyAllowNull(obj, n_min, n_max){
	var regexp = new RegExp("^[0-9]{" + n_min + "," + n_max + "}$", "gi");
	
	if(obj.value.length == 0){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		if(obj.value.match(regexp)){
			inputTextColoring(obj, "#ffffff");
			return true;
		}
		else{
			inputTextColoring(obj, "#FFAFB2");
			return false;
		}
	}
}

function validaNotNull(obj, tamanho){
	//alert(obj)
	if(obj.value.length != 0 && obj.value.length <= tamanho){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}


function validaAllowNull(obj, tamanho){
	if(obj.value.length <= tamanho){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function validaNumberOnly(obj, n_min, n_max){
	var regexp = new RegExp("^[0-9]{" + n_min + "," + n_max + "}$", "gi");
	
	if(obj.value.match(regexp)){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function validaSenhaConfirmacao(obj){
		var id_client = document.getElementById("idClient");
	var aux = true;
	if(id_client)
		if(id_client.value.length > 0 && oSrc.value.length == 0)
			aux = false;
	
	if(aux){
		var objConfirm = document.getElementById("txtSenha");
		if(obj.value.length != 0 && objConfirm.value.length != 0){
			if(obj.value == objConfirm.value){
				inputTextColoring(obj, "#ffffff");
				inputTextColoring(objConfirm, "#ffffff");
				return true;
			}
			else{
				inputTextColoring(obj, "#FFAFB2");
				inputTextColoring(objConfirm, "#FFAFB2");
				return false;
			}
		} 
		else {
			inputTextColoring(obj, "#FFAFB2");
			inputTextColoring(objConfirm, "#FFAFB2");
			return false;
		}
	}else{
		return true;
	}
		
}
function validaSenha(oSrc)
{
	var id_client = document.getElementById("idClient");
	var aux = true;
	if(id_client)
		if(id_client.value.length > 0 && oSrc.value.length == 0)
			aux = false;
	if(aux){
    var regularExp = "(?=^.{8,}$)(?=.*[0-9].*)(?=.*[a-zA-Z].*)(?!(.*((0123)|(1234)|(2345)|(3456)|(4567)|(5678)|(6789)|(9876)|(8765)|(7654)|(6543)|(5432)|(4321)|(3210)).*)|(.*((abcd)|(bcde)|(cdef)|(defg)|(efgh)|(fghi)|(ghij)|(hijk)|(ijkl)|(jklm)|(klmn)|(lmno)|(mnop)|(nopq)|(opqr)|(pqrs)|(qrst)|(rstu)|(stuv)|(tuvw)|(uvwx)|(vwxy)|(wxyz)|(zyxw)|(yxwv)|(xwvu)|(wvut)|(vuts)|(utsr)|(tsrq)|(srqp)|(rqpo)|(qpon)|(ponm)|(onml)|(nmlk)|(mlkj)|(lkji)|(kjih)|(jihg)|(ihgf)|(hgfe)|(gfed)|(fedc)|(edcb)|(dcba)).*))(?!.*[ ].*)";
    var myRegxp = new RegExp(regularExp, "i")
    //var result = myRegxp.exec(oSrc.Value);
    if (oSrc.value.match(myRegxp) == null){
				alert("Senha Inválida. \n Por favor veja a política de senha.");
        return false;
		}else
        return true;
	}else{
    return true;
	}
}

function validaFormSenha(){
	var email = document.getElementById("txtEmail");
	var senha = document.getElementById("txtSenha");
	
	if(validaEmail(email) == true & validaNotNull(senha, 50) == true){
		return true;
	}
	
	return false;
}

function validaFormFisico(){

	//return false;
	
	var nome = document.getElementById("txtNome");
	var sobrenome = document.getElementById("txtSobrenome");
	var dia = document.getElementById("txtDia");
	var mes = document.getElementById("txtMes");
	var ano = document.getElementById("txtAno");
	var email = document.getElementById("txtEmail");
	var senhaAtual = document.getElementById("txtSenhaAtual");
	var senha = document.getElementById("txtSenha");
	var senha1 = document.getElementById("txtConfSenha");
	var ddd_residencial = document.getElementById("txtDddFoneRes");
	var residencial = document.getElementById("txtFoneRes");
	var ddd_celular = document.getElementById("txtDddFoneCel");
	var celular = document.getElementById("txtFoneCel");
	var ddd_comercial = document.getElementById("txtDddFoneCom");
	var comercial = document.getElementById("txtFoneCom");
	var endereco = document.getElementById("txtEndereco");
	var bairro = document.getElementById("txtBairro");
	var cep = document.getElementById("txtCep1");
	var cep_dv = document.getElementById("txtCep2");
	var cidade = document.getElementById("dprCidade")
	var estado = document.getElementById("drpEstado");
	
	if(senhaAtual){
		if(senha.value.length > 0 && senhaAtual.value.length == 0){
			alert("Preencha a senha Atual");
			return false;
		}
	}

	if(validaNotNull(nome, 50) == true && 
		validaNotNull(sobrenome, 50) == true && 
		validaDataDia(dia) == true && 
		validaDataMes(mes) == true && 
		validaDataAno(ano) == true && 
		validaSenha(senha) == true && 
		validaNumberOnly(cep, 5, 5) == true && 
		validaNumberOnly(cep_dv, 3, 3) == true && 
		validaSenhaConfirmacao(senha1) == true
		){
		if(validaData(dia.value, mes.value, ano.value) == false){
			inputTextColoring(dia, "#FFAFB2");
			inputTextColoring(mes, "#FFAFB2");
			inputTextColoring(ano, "#FFAFB2");
			return false;
		}
		
		return true;
	}
	//alert('passou');
	//document.getElementById("msgvalidacao").style.display = "block";
	return false;
}

function validaDataCompra(id){
	var dia = document.getElementById("prod-data-dia-" + id);
	var mes = document.getElementById("prod-data-mes-" + id);
	var ano = document.getElementById("prod-data-ano-" + id);
	
	if(dia.value == "" && mes.value == "" && ano.value == ""){
		return true;
	}
	else{
		if (validaDataDia(dia) == true & validaDataMes(mes) == true & validaDataAnoSamsung(ano) == true & ano.value != "" & validaData(dia.value, mes.value, ano.value) == true){
			return true;
		}
		else{
			inputTextColoring(dia, "#FFAFB2");
			inputTextColoring(mes, "#FFAFB2");
			inputTextColoring(ano, "#FFAFB2");
			return false;
		}
	}
}

function validaNotNullSelect(obj){
	if(obj.value != 0 ){
		inputTextColoring(obj, "#ffffff");
		return true;
	}
	else{
		inputTextColoring(obj, "#FFAFB2");
		return false;
	}
}

function verificaDataProdutos(data1) {
	endDateDia = (new Date()).getDate()
	endDateMes = (new Date()).getMonth() + 1
	endDateAno = (new Date()).getFullYear()
	var endDateMesNovo;
	
	if((endDateMes.toString()).length == 1){
		endDateMesNovo = "0"+endDateMes.toString();
	} else {
		endDateMesNovo = endDateMes.toString();
	}
	if((endDateDia.toString()).length == 1){
		endDateDiaNovo = "0"+endDateDia.toString();
	} else {
		endDateDiaNovo = endDateDia.toString();
	}
	var	data2 = endDateDiaNovo+"/"+endDateMesNovo+"/"+endDateAno;
	
		//alert(data1+"\n"+data2)
		if ( data1 != "" ) {
			if ( parseInt( data2.split( "/" )[2].toString() + data2.split( "/" )[1].toString() + data2.split( "/" )[0].toString() ) < parseInt( data1.split( "/" )[2].toString() + data1.split( "/" )[1].toString() + data1.split( "/" )[0].toString() ) ){
			  //alert("data maior que a atual")
			  return false
			} else {
			  //alert("data menor que a atual - ok")
			  return true;
			}
		} else {
			return true;
		}
}

function validaFormProdutos(){
	
	var dia;
	var mes;
	var ano;
	var novoMes;
	var novoDia;
	var dataProdutos;
	
	dia = document.getElementById("data_dia").value;
	mes = document.getElementById("data_mes").value;
	ano = document.getElementById("data_ano").value;
	if(dia.length < 2){
		novoDia = "0"+dia; 
	} else {
		novoDia = dia;
	}
	if(mes.length < 2){
		novoMes = "0"+mes; 
	} else {
		novoMes = mes;
	}
	dataProdutos = novoDia+"/"+novoMes+"/"+ano
	
	
	if(verificaDataProdutos(dataProdutos) == false){
		document.getElementById("data_dia").style.backgroundColor = "#FFAFB2";
		document.getElementById("data_mes").style.backgroundColor = "#FFAFB2";
		document.getElementById("data_ano").style.backgroundColor = "#FFAFB2";
		return false;
	}
	
	if(document.getElementById("data_dia").value == "" && document.getElementById("data_mes").value == "" && document.getElementById("data_ano").value == ""){
		return true;
	}
	else{
		if (validaDataDia(document.getElementById("data_dia")) == true & validaDataMes(document.getElementById("data_mes")) == true & validaDataAnoSamsung(document.getElementById("data_ano")) == true & document.getElementById("data_ano").value != "" & validaData(document.getElementById("data_dia").value, document.getElementById("data_mes").value, document.getElementById("data_ano").value) == true){
			return true;
		}
		else{
			inputTextColoring(document.getElementById("data_dia"), "#FFAFB2");
			inputTextColoring(document.getElementById("data_mes"), "#FFAFB2");
			inputTextColoring(document.getElementById("data_ano"), "#FFAFB2");
			return false;
		}
	}
	
	
	
	
	
	
	if(validaNotNullSelect(document.getElementById("produto_loja")) == false){
		return false;
	}
	
	if(validaNotNullSelect(document.getElementById("produto_tipo")) == false){
		return false;
	}
	if(validaNotNullSelect(document.getElementById("produtoModelo")) == false){
		return false;
	}
	/*if(document.getElementById("produtoModelo").value == -1){
		document.getElementById("produtoModelo").style.backgroundColor = "#FFAFB2";
		return false;
	}
	if(validaNotNull(document.getElementById("produto_sn"),50) == false){
		return false;
	}*/
	if (document.getElementById("produto_sn").value.length != 0){
		if(document.getElementById("produto_sn").value.length > 5 ){
			var igual = 1;
			txt_sn = document.getElementById("produto_sn").value;
			char_sn = txt_sn.charAt(0);
			for(i=1;i<txt_sn.length;i++){
				if(txt_sn.charAt(i)==char_sn){
					igual++;
				}
			}
			if(igual==txt_sn.length){
				document.getElementById("produto_sn").style.backgroundColor = "#FFAFB2";
				return false;
			}
	
	
			return true;
		}else{
			document.getElementById("produto_sn").style.backgroundColor = "#FFAFB2";
			return false;
		}
	}
		
	return true;
}

function validaFormExtraInfo(){
	return validaAllowNull(document.getElementById("sobre-time"), 20);
}

function validaFormJuridico(){
	var razao_social = document.getElementById("txtRazaoSocial");
	var cnpj = document.getElementById("txtCnpj");
	var email = document.getElementById("txtEmail");
	var senha = document.getElementById("txtSenha");
	var senha1 = document.getElementById("txtConfSenha");
	var ddd_residencial = document.getElementById("txtDddFoneRes");
	var residencial = document.getElementById("txtFoneRes");
	var ddd_celular = document.getElementById("txtDddFoneCel");
	var celular = document.getElementById("txtFoneCel");
	var ddd_comercial = document.getElementById("txtDddFoneCom");
	var comercial = document.getElementById("txtFoneCom");
	var endereco = document.getElementById("txtEndereco");
	var bairro = document.getElementById("txtBairro");
	var cep = document.getElementById("txtCep1");
	var cep_dv = document.getElementById("txtCep2");
	var cidade = document.getElementById("dprCidade")
	var estado = document.getElementById("drpEstado");

	
	if(validaNotNull(razao_social, 100) == true && 
		validaCNPJ(cnpj) == true && 
		validaNotNull(senha, 50) == true && 
		validaNotNull(senha1, 50) == true && 
		validaEmail(email) == true && 
		validaNumberOnly(cep, 5, 5) == true && 
		validaNumberOnly(cep_dv, 3, 3) == true && 
		validaNumberOnlyAllowNull(ddd_residencial, 2, 2) == true && 
		validaNumberOnlyAllowNull(residencial, 7, 8) == true && 
		validaNumberOnlyAllowNull(ddd_celular, 2, 2) == true && 
		validaNumberOnlyAllowNull(celular, 7, 8) == true && 
		validaNumberOnlyAllowNull(ddd_comercial, 2, 2) == true && 
		validaNumberOnlyAllowNull(comercial, 7, 8) == true && 
		validaNumberOnly(cep, 5, 5) == true && 
		validaNumberOnly(cep_dv, 3, 3) == true && 
		validaSenhaConfirmacao(senha1) == true
		){
		
		if(validaData(dia.value, mes.value, ano.value) == false){
			inputTextColoring(dia, "#FFAFB2");
			inputTextColoring(mes, "#FFAFB2");
			inputTextColoring(ano, "#FFAFB2");
			return false;
		}
		
		return true;
	}
	//document.getElementById("msgvalidacao").style.display = "block";
	return false;
}

