/**/
function validDate(data){
	var datapartes = /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{4})$/;
	var dividedata = data.match(datapartes);

	if(dividedata==null){
		return false;
	}
	else{
		var dia = dividedata[1];
		var mes = dividedata[3];
		var ano = dividedata[4];

		if((dia<1 || dia>31 || mes <1 || mes>12 || ano<1900 || ano > 2100)||
		((mes==4 || mes==6 || mes==9 || mes==11)&& dia >30) ||
		(mes==2 && dia>29) || (mes==2 && dia==29 && !ano_bi(ano))){
			return false;
		}

	}
	return true;
}

function validCpf (CPF) {
    if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
    return true;
}

function validCnpj(cnpj) {
	var erro = true; 
	var aux_cnpj = "";	
	var cnpj1=0,cnpj2=0;
	for(j=0;j<cnpj.length;j++)
		if(cnpj.substr(j,1)>="0" && cnpj.substr(j,1)<="9")
			aux_cnpj += cnpj.substr(j,1);
	if(aux_cnpj.length!=14 || aux_cnpj == "00000000000000")
		erro = false;
	else {
		cnpj1 = aux_cnpj.substr(0,12);
		cnpj2 = aux_cnpj.substr(aux_cnpj.length-2,2);
		fator = "543298765432";
		controle = "";
		for(j=0;j<2;j++) {
			soma = 0;
			for(i=0;i<12;i++) 
				soma += cnpj1.substr(i,1) * fator.substr(i,1);
			if(j==1) soma += digito * 2;
			digito = (soma * 10) % 11;
			if(digito==10) digito = 0;
			controle += digito;
			fator = "654329876543";
		} 
		if(controle!=cnpj2)
			erro = false;
	} 
	return erro;
}

function SelectAll(box) {
	// Seleciona todos os elementos de um campo múltiplo
    var opt, a, i;
    i = 0;
    if (box.type == 'select-multiple'){
		while (opt = box[i++]){
			opt.selected = true;
		}
	}
}

function validAno(ano){
	var valida_ano = /^[0-9][0-9][0-9][0-9]$/;
	if (valida_ano.test(ano)) {
		return true;
	}
	else{
		return false;
	}
}

function validSemestre(semestre){
	var valida_semestre = /^[0-9][0-9][0-9][0-9][/][1-2]$/;
	if(valida_semestre.test(semestre)){
		return true;
	}
	else{
		return false;
	}
}


function retiraAcentosBusca(valor){
	valorLength = valor.length;
	var caracteres_inv = "ÁÉÍÓÚÀÈÌÒÙÂÊÎÔÛÄËÏÖÜÃÕÇ";
	var caracteres_val = "AEIOUAEIOUAEIOUAEIOUAOC";
	var retorno  = "";

	for(i=0; i<caracteres_inv.length; i++)
	   {
	   for(j=0; j<valorLength; j++)
		  {
		  retorno = valor.replace(caracteres_inv.substr(i,1),caracteres_val.substr(i,1));
		  valor = retorno;
		  }
	   }
	return valor;
}

function comboBusca(in_sFormName, in_sInputName, in_sSelectName){
	sSearchString = document.forms[in_sFormName].elements[in_sInputName].value.toUpperCase();
	sSearchString = retiraAcentosBusca(sSearchString);
	iSearchTextLength = sSearchString.length;

	for (j=0; j < document.forms[in_sFormName].elements[in_sSelectName].options.length; j++){
		sOptionText = document.forms[in_sFormName].elements[in_sSelectName].options[j].text;
		sOptionComp = sOptionText.substr(0, iSearchTextLength).toUpperCase();

		if(sSearchString == sOptionComp){
			document.forms[in_sFormName].elements[in_sSelectName].selectedIndex = j;
			break;
		}
	}
}


function validFile(sPath, aFileTypes){
    var ext = sPath.substring(sPath.lastIndexOf(".")+1, sPath.length).toLowerCase();
    var valid = false
    for (var i=0; i<aFileTypes.length; i++){
        if (aFileTypes[i] == ext) {
            valid = true;
        }
    }
    return valid;
}

function validFileReplaceInput(oInputFileOld, aFileTypes){    
    var sPath = oInputFileOld.value;
    if(sPath != '' && aFileTypes instanceof Array){           
        var valid = validFile(sPath, aFileTypes);
        if(valid == false){  
            // Tem que recriar o elemento pois não da pra setar 
            // o value vazio em alguns navegadores
            var oInputFileNew = document.createElement("input");
            oInputFileNew.setAttribute("type", oInputFileOld.getAttribute("type"));
            oInputFileNew.setAttribute("size", oInputFileOld.getAttribute("size"));
            oInputFileNew.setAttribute("name", oInputFileOld.getAttribute("name"));
            oInputFileNew.setAttribute("id", oInputFileOld.getAttribute("id"));                
            oInputFileNew.onchange = function() {
                // Readiciona o comportamento no novo elemento
                validFileReplaceInput(this, aFileTypes); 
            }
            oInputFileOld.parentNode.replaceChild(oInputFileNew, oInputFileOld);   
            var sFileTypes = '';
            for(i=0; i<aFileTypes.length; i++){
                sFileTypes += aFileTypes[i]+' ';
            }                
            alert('Arquivo inválido! \nSomente arquivos '+sFileTypes+'são permitidas.');                
        }
    }    
}

function validaEmail(mail){
    var er = new RegExp(/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/);
    
	if(typeof(mail) == "string"){
        if(er.test(mail)){ 
			return true; 
		}
    }
	else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
			return true;
        }
    }
	else{
        return false;
    }
}

function textCounter(form, campo, campo_contador) {
	
	var qtd_caracteres = campo.value.length;
	var max_caracteres = campo.getAttribute ? parseInt(campo.getAttribute("maxlength")) : "";
	var contador = document.getElementById(campo_contador);

	if (campo.getAttribute){

		if (qtd_caracteres > max_caracteres){
			campo.value = campo.value.substring(0, max_caracteres);
		}
		
		if (contador){
			contador.value = max_caracteres - campo.value.length;
		}
		
		if (qtd_caracteres != campo.value.length){
			alert('A quantidade máxima é de '+max_caracteres+' caracteres');
			return false;
		}
	}
}

function Move_Limit(formulario, fbox, tbox, ncombo) {

	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;

	fbox = formulario.elements[fbox];
	tbox = formulario.elements[tbox];

		

	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}		

	var fLength = 0;
	var tLength = arrTbox.length;
	
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		}
		else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
	   }
	}
	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;
	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	for(c = 0; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
	}
}