function ajaxInit() {
    var req;
    try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(ex) {
            try {
                req = new XMLHttpRequest();
            } catch(exc) {
                alert("Esse browser n?o tem recursos para uso do Ajax");
                req = null;
            }
        }
    }
    return req;
}

allChecks = 0;
maxCheck = 0;

function selectAll(){
	allChecks=(allChecks==1)?AllChecks=0:AllChecks=1;
	for(x=0;x<maxCheck;x++){
		allChecks = allChecks;
		document.getElementById('myRadio'+x).checked = allChecks;
	}
}

function validFormMusicLog(){
	if(String(document.getElementById('titulo').value).length < 1 ){
		document.getElementById('titulo').focus();
		alert("Digite o titulo de seu Post!");
		return false;
	}

	if(String(document.getElementById('texto').value).length < 1 ){
		document.getElementById('texto').focus();
		alert("Digite o texto de seu Post!");
		return false;
	}

	if(String(document.getElementById('musica').value).toLowerCase().indexOf('.mp3') == -1 ){
		document.getElementById('musica').focus();
		alert("Arquivo não reconhecido!\nApenas arquivos MP3 são permitidos para o Musiclog.\nVerifique seu arquivo e tente novamente!");
		return false;
	}
	showPerfilPic();
	return true;
}

function showPerfilPic(){
	document.getElementById('picPlace').style.display = 'block';
	document.getElementById('picDone').style.top = (document.body.clientHeight/2) - (document.getElementById('picDone').offsetHeight/2) + document.body.scrollTop;
	document.getElementById('picDone').style.left = (document.body.clientWidth/2) - (document.getElementById('picDone').offsetWidth/2);
}

function closePerfilPic(){
	document.getElementById('picPlace').style.display = 'none';
}

function envia(pag){
    document.getElementById('formuPage').action= pag
    document.getElementById('formuPage').submit()
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function envia_hide(uid,mensagem,site,num){
    document.getElementById('to_uid_hide').value = uid;
    document.getElementById('id_mensagem_hide').value = mensagem;
    enviaRecado(site,num);
}

function enviaRecado(site,num) {
    uid = document.getElementById('to_uid_hide').value;
    mensagem = URLEncode(document.getElementById('id_mensagem_hide').value);
    alert(mensagem);
    //ajaxHTML('mensagem_'+num, site+'/send_mensagem_ondemand.php?refer=gray_skin&to_uid='+uid+'&mensagem='+mensagem);
}

function enviaRecadoDirect(site,num) {
    mensagem = URLEncode(document.getElementById('mensagem_'+num).value);
    uid = document.getElementById('to_uid_'+num).value;
    ajaxHTML('divmensagem_'+num, site+'/send_mensagem_ondemand.php?refer=gray_skin&to_uid='+uid+'&mensagem='+mensagem);
}

function enviaRecadoDirectProfessor(site,num) {
    mensagem = URLEncode(document.getElementById('mensagem_prof').value);
    uid = document.getElementById('to_uid_prof').value;
    uids = document.getElementById('uids').value;
    ajaxHTML('divmensagem_prof', site+'/send_mensagem.php?refer=gray_skin&to_uid='+uid+'&mensagem='+mensagem+'&uids='+uids);
}

function blockCopy(e)
	{
		var code;
                if(!e)e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
			if (e && e.ctrlKey && (code == 67 || code == 86)) {
				//
				if(window.event)e.returnValue=false;
				else e.preventDefault();
			}
	}

function validFormAlteraDados(){


	if(document.getElementById('email').value.length < 3 || String(document.getElementById('email').value).indexOf('.')==-1 || String(document.getElementById('email').value).indexOf('@')==-1 ){
		alert("Preencha seu e-mail corretamente!");
		document.getElementById('email').focus();
		return false;
	}

	if(document.getElementById('confirmemail').value.length < 3 || document.getElementById('confirmemail').value != document.getElementById('email').value){
		alert("Digite a confirmação do e-mail corretamente!");
		document.getElementById('confirmemail').focus();
		return false;
	}


	if(document.getElementById('nome').value.length < 2){
		alert("Digite seu nome!");
		document.getElementById('nome').focus();
		return false;
	}

	if(document.getElementById('sobrenome').value.length < 2){
		alert("Digite seu sobrenome!");
		document.getElementById('sobrenome').focus();
		return false;
	}

	if(document.getElementById('apelido').value.length < 2){
		alert("Digite seu apelido!");
		document.getElementById('apelido').focus();
		return false;
	}


	if(document.getElementById('estadoId').selectedIndex == 0 && document.getElementById('estadoExt').value.length < 2){
		alert("Selecione seu estado!");
		return false;
	}

	if(document.getElementById('cidade').selectedIndex == 0 && document.getElementById('cidadeExt').value.length < 2){
		alert("Selecione sua cidade!");
		return false;
	}

	if(document.getElementById('dia_nasc').selectedIndex == 0 ||  document.getElementById('mes_nasc').selectedIndex == 0 || document.getElementById('ano_nasc').selectedIndex == 0){
		alert("Selecione a data de seu nascimento!");
		document.getElementById('dia_nasc').focus();
		return false;
	}

	 return true;
}

function criaMusicLog()
{
	endereco = document.getElementById('endereco').value;

	ajaxHTML('conteudo', 'http://www.djlab.com.br/painel/criar_musiclog/'+endereco+'/');
}

function Bloqueia(caracter) {
	var tecla;
	if(window.event) { // Internet Explorer
		tecla = event.keyCode;
	}
	else { // Firefox
		tecla = caracter.which;
	}

	if((tecla == 32) || (tecla == 224) || (tecla == 225) || (tecla == 226) || (tecla == 227) || (tecla == 231) || (tecla == 241) || (tecla == 192) || (tecla == 193) || (tecla == 195) || (tecla == 199) || (tecla == 209) || (tecla == 33) || (tecla == 34) || (tecla == 35) || (tecla == 36) || (tecla == 37) || (tecla == 38) || (tecla == 39) || (tecla == 40) || (tecla == 41) || (tecla == 42) || (tecla == 58) || (tecla == 59) || (tecla == 60) || (tecla == 61) || (tecla == 62) || (tecla == 63) || (tecla == 92) || (tecla == 94)) {
		return false;
	}
	else { return true; }
}

function validContatoForm(){

	if(document.getElementById('nome').value.length < 1){
		alert("Informe seu nome para enviar!");
		document.getElementById('nome').focus();
		return false;
	}

	if(document.getElementById('email').value.length < 1 || String(document.getElementById('email').value).indexOf('.') == -1 ||  String(document.getElementById('email').value).indexOf('@') == -1 ){
		alert("Informe seu email para enviar!");
		document.getElementById('email').focus();
		return false;
	}

	if(document.getElementById('assunto').value.length < 1){
		alert("Informe o assunto para enviar!");
		document.getElementById('assunto').focus();
		return false;
	}

	if(document.getElementById('mensagem').value.length < 1){
		alert("Informe a mensagem para enviar!");
		document.getElementById('mensagem').focus();
		return false;
	}

	ajaxHTML('conteudo', "http://www.djlab.com.br/funcoes/helpdesk/send_contato.php?nome="+document.getElementById('nome').value+"&email="+document.getElementById('email').value+"&mensagem="+document.getElementById('nome').value);

}

function avaliaPlayer(id){
	var form = document.avaliacao;
	var csinc = document.getElementById('com_sincronizacao').text;
	var cconf = document.getElementById('com_conflitos').text;
	var ccoer = document.getElementById('com_coerencia').text;
	var ccont = document.getElementById('com_controles').text;
	var cqual = document.getElementById('com_qualidade').text;
	var radio_sinc;
	var radio_conf;
	var radio_coer;
	var radio_cont;
	var radio_qual;
	var Radio = null;

	Radio= form.radio_sincronizacao;
    for(var i=0;i<Radio.length;i++) {
        if(Radio[i].checked) {
            radio_sinc = Radio[i].value;
        }
    }

	Radio= form.radio_conflitos;
    for(i=0;i<Radio.length;i++) {
        if(Radio[i].checked) {
            radio_conf = Radio[i].value;
        }
    }

	Radio= form.radio_coerencia;
    for(i=0;i<Radio.length;i++) {
        if(Radio[i].checked) {
            radio_coer = Radio[i].value;
        }
    }

	Radio= form.radio_controles;
    for(i=0;i<Radio.length;i++) {
        if(Radio[i].checked) {
            radio_cont = Radio[i].value;
        }
    }

	Radio= form.radio_qual;
    for(i=0;i<Radio.length;i++) {
        if(Radio[i].checked) {
            radio_qual = Radio[i].value;
        }
    }

	var query = "?nsinc="+radio_sinc+"&nconf="+radio_conf+"&ncoer="+radio_coer+"&ncont="+radio_cont+"&nqual="+radio_qual;
	query = query+"&csinc="+csinc+"&cconf="+cconf+"&ccoer="+ccoer+"&ccont="+ccont+"&cqual="+cqual+"&id="+id;
	ajaxHTML('avaliar_'+id,'http://www.djlab.com.br/funcoes/avaliacao/calc_nota.php'+query);
}

/**
 *
 * @access public
 * @return void
 **/
function indicaForm(site){

	var continua = true;

	if(document.getElementById('nome').value.length < 3){
		alert("Digite o nome do seu indicado!");
		document.getElementById('nome').focus();
		continua = false;
	}

	if(document.getElementById('email').value.length < 5){
		alert("Digite o email do seu indicado!");
		document.getElementById('email').focus();
		continua = false;
	}

	if(document.getElementById('mensagem').value.length < 5){
		alert("Digite uma mensagem para seu indicado!");
		document.getElementById('mensagem').focus();
		continua = false;
	}

	if(document.getElementById('concordo').checked != true){
		alert("Concorde com os termos para indicar!");
		document.getElementById('concordo').focus();
		continua = false;
	}

	if(continua == true)
	{
		var query = "?nome="+document.getElementById('nome').value+"&email="+document.getElementById('email').value+"&mensagem="+document.getElementById('mensagem').value;
		ajaxHTML('conteudo', site+'/funcoes/indicacoes/indicar_cad.php'+query);
	}

}

function checkMail(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	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;
	        }
}

/**
 *
 * @access public
 * @return void
 **/
function validAlteraDadosForm(site){

	email = document.getElementById('email').value;
	confirmaemail = document.getElementById('confirmemail').value;
	nome = document.getElementById('nome').value;
	sobrenome = document.getElementById('sobrenome').value;
	apelido = document.getElementById('apelido').value;
	pais = document.getElementById('pais').value;
	estado = document.getElementById('estadoId').value;
	cidade = document.getElementById('cidadeId').value;
        conheceu = document.getElementById('conheceu').value;
        estadoExt = document.getElementById('estadoExt').value;
        cidadeExt = document.getElementById('cidadeExt').value;

	if(checkMail(email) == false) {
		alert('E-mail inválido');
		return false;
	}

	if(checkMail(confirmaemail) == false) {
		alert('E-mail de confirmação inválido');
		return false;
	}

	if(email != confirmaemail) {
		alert('Os e-mails não conferem');
		return false;
	}

//	var profissionais = "";
//	var campos = document.getElementsByTagName("input");
//	var aux;
//	for (i=0; i < campos.length; i++)
//	{
//		if(campos[i].getAttribute("type") == "checkbox")
//		{
//			if(campos[i].checked == true)
//				profissionais = profissionais + "&p["+ campos[i].value +"]=1";
//			else
//				profissionais = profissionais + "&p["+ campos[i].value +"]=0";
//		}
//	}

	if(document.getElementById('receberNews').checked==true)
		recebeNews = 1;
	else
		recebeNews = 0;

	if(document.getElementById('receberParc').checked==true)
		recebeParc = 1;
	else
		recebeParc = 0;

	qs = 'email='+email+'&nome='+nome+'&sobrenome='+sobrenome+'&apelido='+apelido+'&pais='+pais+'&estado='+estado+'&cidade='+cidade+'&recebeNews='+recebeNews+'&recebeParc='+recebeParc+'&conheceu='+conheceu+'&cidadeExt='+cidadeExt+'&estadoExt='+estadoExt;
	ajaxHTML('conteudo', site+'/funcoes/meus_dados/alterar_dados.php?'+qs);

}

function logaCadastro(){

	login = document.getElementById('login_login').value;
	senha = encode64(document.getElementById('login_senha').value);

	qs = './cadastro_ok_login.php?login_login='+login+'&login_senha='+senha;

	ajaxHTML('links', qs);

}

function addField(who){
	document.getElementById('changedFields').value += "@" +who;
}

allChecks = 0;
maxCheck = 0;

function selectAll(){
    allChecks=(allChecks==1)?AllChecks=0:AllChecks=1;
    for(x=0;x<maxCheck;x++){
        allChecks = allChecks;
        document.getElementById('myRadio'+x).checked = allChecks;
    }
}

function incraseMaxCheck(){
    maxCheck++;
}

function validFormSenha(site_url){

        var continua = true;

        if(document.getElementById('senha').value.length < 5){
                alert("Escolha uma senha com mais de 5 caracteres!");
                document.getElementById('senha').focus();
                continua = false;
        }

        if(document.getElementById('confirmsenha').value != document.getElementById('senha').value){
                alert("Digite a confirmação de senha corretamente!");
                document.getElementById('confirmsenha').focus();
                continua = false;
        }

         if(continua == true) {
             ajaxHTML('conteudo', site_url+'/alterar_senha.php?senha='+document.getElementById('senha').value);
         }
}

var checkall=0;
function AllCheckBox()
{
    $(".cinput").each(function()
    {
        if(checkall == 0) this.checked = true;
        else this.checked = false;
    });
    checkall = checkall ? 0 : 1;
}

function soUf(who){
        if(who==1){
                document.getElementById('slcEstado').style.display='block';
                document.getElementById('slcCidade').style.display='block';
                document.getElementById('txEstado').style.display='none';
                document.getElementById('txCidade').style.display='none';
        }else{
                document.getElementById('slcEstado').style.display='none';
                document.getElementById('slcCidade').style.display='none';
                document.getElementById('txEstado').style.display='block';
                document.getElementById('txCidade').style.display='block';
        }

}

function soCity(who){
        for(x=0;x<=27;x++){
                document.getElementById('uf'+x).style.display='none';
        }
        document.getElementById('uf'+who).style.display='block';
}

function getEstate(who, site){

var xhttp = ajaxInit();
var urlPesquisa = site+'/includes/inc_ufs_new.php';
var params = 'ufId='+who+'&rand='+Math.random(1);

        var myAjax = new Ajax.Updater('contentUfs',
                               urlPesquisa,
                                    {
                                            method: 'get',
                                            parameters: params,
                                            onLoading: ufLoading,
                                            onFailure: ufFailure
                                    }
                              )

}
function ufLoading(){
        $('contentUfs').innerHTML = "<p><strong class='txAr12Bl'>Carregando...</strong></p>"
}
function ufFailure(){
        $('contentUfs').innerHTML = "<p><strong>Desculpe falha na busca, contate o administrador.</strong></p>"
}

function checkAceita() {
    if(document.getElementById("aceita").checked ==  true)
        return true
    else {
        alert("Você precisa aceitar os termos para fazer o pagamento.");
        return false;
    }
}

function validaBonusForm(site) {

    disponivel = document.getElementById('disponivel').value;
    credito = document.getElementById('credito').value;
    id = document.getElementById('id').value;

    if(credito > disponivel) {
        alert("Você não possui tudo isso de crédito!");
        return false;
    } else {
        ajaxHTML('conteudo', site+'/indique_apply_credit.php?id_aula='+id+'&credito='+credito);
    }


}