// Funções JavaScript
// Plugins: JQuery
// Created by: Raphael Silva
// Date: 04/03/2009

// Função usada para limpar um elemento
// element = elemento a ser limpo
// value = valor do elemento
// original_value = valor que devera aparecer no elemento caso o user tire o foco do elemento. 
// só limpa se value = original_value
function clean(element, value, original_value)
{
  //alert(element+value);
  if (value == original_value)
  {
    var aux = document.getElementById(element);
    aux.value = '';
  }
}

// Função usada para inserir um valor no elemnto caso ele esteja vazio
// element = Elemento a ser preenchido
// original_value = Valor no qual o elemento deve ser preenchido
// só preenche se o element.value == '';
function fill(element, original_value)
{
  var aux = document.getElementById(element);
  if (aux.value == '')
  {
    aux.value = original_value;
  }
}

// Função de validação de formulário
// valida se campos estão vazios
// valida campo de e-mail
// trocar essa função futuramente
function validate_form(form, ajax)
{
	var length_form = form.elements.length;
    var text = '';
    for (i=0; i<length_form; i++)
	{
		if (form.elements[i].value == '')
		{   
			text = text + '\n' + 'O campo ' + form.elements[i].name+' não pode ser vazio!';
		}
		
		if (form.elements[i].name == form.elements[i].value)
		{
			text = text + '\n' + 'O campo ' + form.elements[i].name+' não pode ser vazio!';
		}
		
		if (form.elements[i].name == 'e-mail' || form.elements[i].name == 'E-mail')
		{
            if (form.elements[i].value.indexOf ('@',0) == -1 || form.elements[i].value.indexOf ('.',0) == -1 || form.elements[i].value.indexOf (';') > -1)
			{
			  text = text + '\n' + 'Este ' +form.elements[i].name+' não é valido!';
			}
			  
		}
	}
	if (text != '')
    {
	  alert(text);
	  return false;
	}
	else
	{
	  if (ajax == 'sim')
	  {
		if (form.name == 'form_produtos')  
		{
			send_form_result(form);
			return false;
		}
		else
		{
	      send_form_alert(form);
		  return false;
		}
	  }
	  else
	  {
	    return true;
	  }
	}
}

function send_form_alert(form)
{
  var params = $(form.elements).serialize();

  $.ajax({
    type: 'POST',
    url: form.action,
    data: params,
    beforeSend: function(){
	},
    success: function(txt){
	  alert(txt);
    },
    error: function(txt){
      alert(txt);
    }
  })
  return false;
}

function send_form_result(form)
{
  var params = $(form.elements).serialize();

  $.ajax({
    type: 'POST',
    url: form.action,
    data: params,
    beforeSend: function(){
	  $("#carregando").show();
	},
    success: function(txt){
	  $("#result").empty();
	  $("#result").append(txt);
	  $("#carregando").hide();	
	  $("#result").show();
    },
    error: function(txt){
      alert(txt);
    }
  })
  return false;
}
function tabs(var1, var2, var3, var4, var5, var6)
{
	$("#"+var1).hide();
	$("#"+var2).hide();
	$("#"+var3).hide();
	$("#"+var4).show();
	$("#"+var5).show();
	$("#"+var6).show();
}
function verificaForm(obj)
{
    radiochk = false;

    for ( n = 0; n < obj.opt_id.length; n++ )
    {
        el = obj.opt_id[n];
        radiochk = radiochk || el.checked;
    }

    if ( !radiochk ) {
        alert("Nenhuma opção da enquete foi escolhida!");
        return false;
    }
    
    return true;
}

function vota()
{
    if(verificaForm(document.form_enquete))
    {
        var nwin = window.open('', 'winEnquete', 'location=no, width=435, height=330, scrollbars=yes');
        document.form_enquete.submit();
        nwin.focus();
    }
}
  
function verResultado(id)
{
    var nwin = window.open('enquete/enquete_resultado.php?acao=ver_resultado&enq_id='+id, 'winEnquete', 'location=no, width=435, height=330, scrollbars=yes');
    nwin.focus();
}



