function submitLogin(f) {

  $$('input.registar').first().style.visibility = 'hidden';

  f = $(f);

  var post = new Hash(f.serialize(true));
  post.set('ajax', 'true');

  new Ajax.Request('/login/', {
    method: 'post',
    encoding: 'UTF-8',
    parameters: post,
    onSuccess: function(transport) {

                 var response = transport.responseJSON;
                 if (typeof (response.result) != 'undefined') {

                   if (response.result == true) {
                     var redirect = response.redirect;
                     
                     if (typeof (redirect) == 'string' && redirect.length > 0) {
                       window.location = redirect;
                     }
                     else
                       window.location = "/";
                     return;
                   } else {
                     $$('input.registar').first().style.visibility = 'visible';
                     showErrors(response.errors);
                     return;
                   }
                 }

                 $$('input.registar').first().style.visibility = 'visible';
                 showErrors();
                 return;
               },
    onFailure: function(transport) {

                 $$('input.registar').first().style.visibility = 'visible';
                 showErrors();
                 return;
               }
  });

  return false;
}

function getParam(p) {
  if (location.search.length <= 0)
    return;

  var tmp = location.search.substring(1);
  tmp = tmp.split("&");

  for (var i = 0; i < tmp.length; i ++) {
    //alert(tmp);
    var param = tmp[i].split("=");
    if (param[0] == p) {
      if (param.length > 1)
        return param[1];
      else
        return "";
    }
  }  

  return;
}

function showErrors(e) {

  // Remove previous errors
  try {
    $$('div#errors').first().remove();
  } catch (error) {
  }

  // Add errors
  if (typeof (e) == 'undefined') {
    var e = new Array('Erro no sistema. Por favor tenta novamente.');
  }

  var html = '<div id="errors">' + 
             '<span>Verifica os seguintes campos:</span>';

  for (var i = 0; i < e.length; i ++) {
    if (i > 0)
      html += ', ';
    html += e[i];
  }

  html += '</div>';

  $('header').insert({after: html}); 
  window.scrollTo(0,0);
}

function submitRegister(f) {

  $$('input.registar').first().style.visibility = 'hidden';

  f = $(f);

  var post = new Hash(f.serialize(true));
  if($('usr_tos').checked == true) post.set('usr_tos', 'true');
  if($('usr_bla').checked == true) post.set('usr_bla', 'true');
  post.set('ajax', 'true');

  new Ajax.Request('/registo/', {
    method: 'post',
    encoding: 'UTF-8',
    parameters: post,
    onSuccess: function(transport) {

                 var response = transport.responseJSON;
                 if (typeof (response.result) != 'undefined') {

                   if (response.result == true) {
                     var redirect = getParam('redirect');
                     if (typeof (redirect) == 'string' && redirect.length > 0)
                       window.location = redirect;
                     else
                       window.location = "/thankyou/";
                     return;
                   } else {
                     $$('input.registar').first().style.visibility = 'visible';
                     showErrors(response.errors);
                     return;
                   }
                 }

                 $$('input.registar').first().style.visibility = 'visible';
                 showErrors();
                 return;
               },

    onFailure: function(transport) {

                 $$('input.registar').first().style.visibility = 'visible';
                 showErrors();
                 return;
               }
  });

  return false;
}

function limit_text(field, limit, counterDesired) {
    if (counterDesired == null)
        counterDesired = false;
    var length = $F(field).length;
    if (length > limit)
        $(field).value = $(field).value.substring(0, limit);
    if (counterDesired) {
        if ($($(field).id + '_counter')) {
            $($(field).id + '_counter').update($F(field).length + " / " + limit);
        } else {
            var counterText = new Element('span', {'id': $(field).id + '_counter'});
            counterText.update($F(field).length + " / " + limit);
            $(field).insert({'after': counterText});
        }
    }
}

function rateImg(rating,chaaid){

    url = '/desafio/?rating='+rating+'&chaid='+chaaid+'';
    new Ajax.Request(url,{
            method: 'get',
            onComplete: function(transport) {
                
                            json = transport.responseJSON;
                            
                            if(json.status == 1) {
                                  rating = rating * 10;
                                  $('current_rating_'+chaaid).style.width = rating+'px';
                                  $('ratelinks_'+chaaid).style.display = 'none';
                            }else{
                            }
            },
            
            onFailure: function(transport) {
                
            }   
        });
    return false;
}  

function toggleWiiners(active, active_week)
{
	//totalReports = $('winner_active_week_'+active).childElements().length;
	
    $('winner_active_week_ini').style.display = 'none';
     
	for(i = 1; i <= active_week; i++){
            
		if(i != active)
		{
            $('semana_'+i).removeClassName('actine_week');

            $('winner_active_week_'+i).style.display = 'none';;
            
			//new Effect.Fade('winner_active_week_'+i, { duration: 1.0 }); 
		}
		else
		{
            $('week_opened').update('<strong>Semana '+i+'</strong>');
             $('semana_'+i).addClassName('actine_week');
			new Effect.Appear('winner_active_week_'+i, { duration: 1.0 });
            
            
		}
	}
}




