function addLoading(div) {
	if (div) {
		$('#' + div).html("<img src='_images/loader.gif' border='0'>");
	}
}
function delLoading(div) {
	if (div) {
		$('#' + div).html("");
		$('#' + div).hide();
	}
}
function showHide(div) {
	if ($("#" + div).css("display") == "none" || $("#" + div).css("display") == "") {
		$("#" + div).slideDown('slow');
	} else {
		$("#" + div).slideUp('slow');
	}
}
function txtBox(box,tipo) {
	if (box) {
		if (tipo == 'in') {
			if (box.value == box.title) {
				box.value = '';
			}
		} else {
			if (box.value == '') {
				box.value = box.title;
			}
		}
	}
}

function emailCheck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if (str.indexOf(" ")!=-1){
		return false
	}	
	return true					
}

function doPlay(video, img, autoplay) {
	var so = new SWFObject('mediaplayer.swf','mpl','320','240','7');
	so.addParam('allowfullscreen','true');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','transparent');
	so.addParam('stretching','none');
	so.addVariable('autostart',autoplay);
	so.addVariable('file','_files/videos/' + video);
	so.addVariable('image','_files/videos/' + img);
	so.write('player');
}
function setChecked(id){
    var item = document.getElementById(id);
    return item.checked ? item.checked = false : item.checked = true;
}

function numberFormat(number){
    number = new String(number).replace(',','.');
    number = number.split('.');
    
    if(number.length == 1){
        number[1] ='00';
    }else if(number[1].length == 1){
        number[1] = number[1]+'0';
    }
    
    return number[0]+'.'+number[1];
}
function validaCompraProdutos() {
    var f = document.form_produtos;
    var erro = false;
    var lists = false;

    lists = false;
	$("input.check1").each(function() {
		var id = $(this).val();
		var prd = document.getElementById('produto'+id);
		if (prd.checked) {
			lists = true;
		}
	});
	
    if(!lists){
        alert("Selecione ao menos um produto!");
        return false;
    }

    if(isEmpty(f.nome.value)){
        alert("Informe o seu nome!");
        return false;
    }
    /*
    if(!validarCpf(f.cpf.value)){
        alert("CPF inválido!");
        return false;
    }
    */
    if(isEmpty(f.cidade.value)){
        alert("Informe a cidade!");
        return false;
    }

    if(isEmpty(f.bairro.value)){
        alert("Informe o bairro!");
        return false;
    }

    if(isEmpty(f.logradouro.value)){
        alert("Informe o logradouro!");
        return false;
    }
    if(isEmpty(f.numero.value)){
        alert("Informe o Numero!");
        return false;
    }

    if((f.cep.value.replace('-','').length) != 8 ){
        alert("Preencha corretamente o CEP!");
        return false;
    }

    if(!emailCheck(f.email.value)){
        alert("Informe corretamente o e-mail!");
        return false;
    }

    return true;

}

function validaCompraCursos() {
    var f = document.form_cursos;
    var erro = false;
    var lists = false;

    for(i=0; i< f.tipo.length; i++){
      if(f.tipo[i].checked){
        lists = true;
      }
    }
    if(!lists){
        alert("Selecione o tipo!");
        return false;
    }

    lists = false;
    for(i=0; i < f.totalCursos.value; i++){
        if(document.getElementById('curso'+i).checked){
            lists = true;
        }
    }
    if(!lists){
        alert("Selecione ao menos um curso!");
        return false;
    }

    if(isEmpty(f.nome.value)){
        alert("Informe o seu nome!");
        return false;
    }
    /*
    if(!validarCpf(f.cpf.value)){
        alert("CPF inválido!");
        return false;
    }
    */
    if(isEmpty(f.cidade.value)){
        alert("Informe a cidade!");
        return false;
    }

    if(isEmpty(f.bairro.value)){
        alert("Informe o bairro!");
        return false;
    }

    if(isEmpty(f.logradouro.value)){
        alert("Informe o logradouro!");
        return false;
    }
    if(isEmpty(f.numero.value)){
        alert("Informe o Numero!");
        return false;
    }

    if((f.cep.value.replace('-','').length) != 8 ){
        alert("Preencha corretamente o CEP!");
        return false;
    }

    if(!emailCheck(f.email.value)){
        alert("Informe corretamente o e-mail!");
        return false;
    }

    return true;

}


function isEmpty(str) {
   x = str.trim();

   if (x.length==0) {
       return true;
   } else {
       return false;
   }
}
function maskIt(w,e,m,r,a){
        // Cancela se o evento for Backspace
  if (!e) var e = window.event
  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;

  // Variáveis da função
  var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
  var mask = (!r) ? m : m.reverse();    var pre  = (a ) ? a.pre : "";
  var pos  = (a ) ? a.pos : "";
  var ret  = "";

  if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;

  // Loop na máscara para aplicar os caracteres
  for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
          if(mask.charAt(x)!='#'){
            ret += mask.charAt(x); x++;
    } else{
            ret += txt.charAt(y); y++; x++;
    }
  }

  // Retorno da função
  ret = (!r) ? ret : ret.reverse()
  w.value = pre+ret+pos;
}


String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

