// JavaScript Document

addLoadEvent( function(){
	
	$('#nav ul li a').each(function() {
		var w = $(this).width();
		if (w < 180) {
			$(this).css('display','block');
			$(this).width(180);
		} else {
			$(this).css('display','block');
			$(this).width(w+1);
		}
	});
	
	if ($('.ui-state-default').length < 2) {
		$('.ui-tabs-nav').hide();
	}

	$(".foco").focus(function(){
		eskemaFocos(this);			  
	});
	$(".foco").blur(function(){
		eskemaBlur(this);			  
	});
	
  $("#nav ul > li a").click(function(){
    //alert($(this).html());
    /*
    $(this).parent().children("ul").toggle('300');
    
    if ( $(this).parent().children("ul").length > 0 ){
      if ( !$(this).parents('li').children('a').hasClass('selectDrop') ){
        $(this).parents('li').children('a').addClass('selectDrop');
      }else{
        $(this).parents('li').children('a').removeClass('selectDrop');
      }
    }
    */
  })
  
  $("#nav ul > li a").each(function(){
    if ( $(this).attr('href') == url ){
      $(this).parents('ul').show();
      $(this).parent().find('ul').show();
      //$(this).parents('li').parents('li').children('a').addClass('selectDrop');
      //$(this).addClass('selectDrop');
    }
  })
  
  //_# Contato action button
  $("#enviarForm").click(function(){
    if ( $("#nome").val() == '' ){
      alert('Favor preencher o campo Nome!');
      $("#nome").focus();
    }else if ( !checkMail($("#email").val()) ){
      alert('Favor preencher o campo E-mail corretamente!');
      $("#email").focus();
    }else if ( $("#idEstado").val() == '0' ){
      alert('Favor selecionar um Estado!');
      $("#email").focus();
    }else if ( $("#idCidade").val() == '0' ){
      alert('Favor selecionar uma Cidade!');
      $("#email").focus();
    }else if ( $("#comentario").val() == '' ){
      alert('Favor preencher o campo Comentário!');
      $("#comentario").focus();
    }else{
      $("#contatoForm").submit();
    }
  })
		
});

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

function eskemaFocos(obj){
	trimed = Trim(obj.value);
	if (trimed == Trim(obj.defaultValue)){
		obj.value = "";
	}
}

function eskemaBlur(obj){
	trimed = Trim(obj.value);
	if (trimed == ''){
		obj.value = obj.defaultValue;
	}
}

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;
	}
}

function validaContato() {
	if ( $("#nome").val() == '' ){
		alert('Favor preencher o campo Nome corretamente!');
		$("#nome").focus();
		return false;
	} else if ( !checkMail( $("#email").val() ) ){
		alert('Favor preencher o campo E-mail corretamente!');
		$("#email").focus();
		return false;
	} else if ( $("#empresa").val() == '' ){
		alert('Favor preencher o campo Empresa!');
		$("#empresa").focus();
		return false;
	} else if ( $("#mensagem").val() == '' ){
		alert('Favor preencher o campo Mensagem!');
		$("#mensagem").focus();
		return false;
	}else{
		return true;
	}
	return false;
}

  function ajax_estado(idEstado,idCidade,strCidade){
    $.post("/ajax/common.php?funcao=ajax_estado", { idEstado: idEstado, idCidade: idCidade }, function(retorno){
      $("#c_cidade").html(retorno);
      if (cidadeAjax != ''){
        $('#idCidade').val(cidadeAjax);
        if (strCidade != ''){
          idCidade = $("#idCidade option:econtains('"+strCidade+"')").val();
          $('#idCidade').val(idCidade);
        }
      }
    });
  }

