// dom ready
function domReady() {
	installMenu();
	//$('.datePicker').datepicker();

	// how to highlight days of the present month
	//highlightDays([2,5,7]);
	
	installBoxListeners();
	installNewsletter();
	installContact();
	installCongressRegistration();
}


function _(c) {
	switch(window._LANG) {
		case 2:
			window._lang_str = ['System failure. Please try again.'];
			break;
		case 1:
			window._lang_str = ['Falha no sistema. Por favor tente novamente.'];
			break;
	}
	
	return window._lang_str[c];
}


// newsletter
function installCongressRegistration() {
	$("#submit_congress_registration").bind("click", function(event) {
		event.preventDefault();
		
		try {
			$.ajax({
			   type: $('#congress_registration').attr('method'),
			   url: $('#congress_registration').attr('action'),
			   cache: false,
			   data: $('#congress_registration').serialize(),
			   success: function(r) {
			     if (typeof(r.msg) != 'string') {
			    	 alert(_(0));
			    	 return;
			     }
			     alert(r.msg);
			   },
			   error: function() {
				   alert(_(0));
			   }
			});
		} catch (e) {
		     alert(_(0));
		}
	});
}

//newsletter
function installNewsletter() {
	$("#newsletter_form").bind("submit", function(event) {
		event.preventDefault();
		
		try {
			$.ajax({
			   type: $(this).attr('method'),
			   url: $(this).attr('action'),
			   cache: false,
			   data: $(this).serialize(),
			   success: function(r) {
			     if (typeof(r.msg) != 'string') {
			    	 alert(_(0));
			    	 return;
			     }
			     alert(r.msg);
			   },
			   error: function() {
				   alert(_(0));
			   }
			});
		} catch (e) {
		     alert(_(0));
		}
	});
}


//contact form
function installContact() {
	$("#contact_form").bind("submit", function(event) {
		event.preventDefault();
		
		try {
			$.ajax({
			   type: $(this).attr('method'),
			   url: window.location.href,
			   cache: false,
			   data: $(this).serialize(),
			   success: function(r) {
			     if (typeof(r.msg) != 'string') {
			    	 alert(_(0));
			    	 return;
			     }
			     alert(r.msg);
			   },
			   error: function() {
				   alert(_(0));
			   }
			});
		} catch (e) {
		     alert(_(0));
		}
	});
}


// calendario
function goMonth(id)
{
	$('.ui-datepicker').hide();
	$('#month-' + id).show();
}


// menu
function installMenu()
{
	$('.menu span').mouseover(function()
	{
		$(this).find('div').fadeIn();
	});
	
	$('.menu span').mouseleave(function()
	{
		$(this).find('div').fadeOut();
	});
}

// box listeners
function installBoxListeners()
{
	$('.nl_name').focus(function()
	{
		if($('.nl_name').val() == 'Nome')
			$('.nl_name').val('');
	});

	$('.nl_name').blur(function()
	{
		if($('.nl_name').val() == '')
			$('.nl_name').val('Nome');
	});
	
	$('.nl_email').focus(function()
	{
		if($('.nl_email').val() == 'Email')
			$('.nl_email').val('');
	});

	$('.nl_email').blur(function()
	{
		if($('.nl_email').val() == '')
			$('.nl_email').val('Email');
	});
	
	$('.searchTerm').focus(function()
	{
		$('.searchTerm').css('color','#5A5B5E');
		if($('.searchTerm').val() == 'Pesquisa')
		{
			$('.searchTerm').val('');
		}
	});

	$('.searchTerm').blur(function()
	{
		if($('.searchTerm').val() == '')
		{
			$('.searchTerm').val('Pesquisa');
			$('.searchTerm').css('color','#9a9a9a');
		}
	});
}



// lightbox
function showlightbox(lightbox)
{
	$('.' + lightbox).show();
	$('.lightbox').fadeIn();
}

function hidelightbox()
{
	$('.lightbox').fadeOut(1000, function()
	{
		$('.lb').hide();	
	});
}

function hideLoginShowPwd()
{
	$('.login').fadeOut();
	$('.retrievepwd').fadeIn();
}

function hideLoginShowRegistry()
{
	$('.login').fadeOut();
	$('.register').fadeIn();
}


//Submit Registation pré-Inscricao HP
function submitRegistation(f){
      
    f = $(f).serialize();
    
    if($('v_erro') != null)
        $('v_erro').hide();
    
    
    //startLoading();
    
    $.ajax({
            url: '/pre-registo',
            data: 'ajax=AJAX&'+f,
            type: 'POST',
            dataType: 'json',
            success: function(transport) {
                            
                            json = transport;
                            if(json.status == 1) {
                            
                                $('.text').val('');
                                $('#r_nl').attr('checked', false);
                                $('#r_n2').attr('checked', false);
                                $('#r_n3').attr('checked', false);
                                
                                $('#v_erro').html(json.msg);
                                $('#v_erro').show();
                                 
                            } else { 
                                 $('#v_erro').html(json.msg);
                                 $('#v_erro').show();
                            }
            }
        });
    return false;
}

//Submit Registation pré-Inscricao HP CAMP I
function submitRegistationCamp(f){
      
    f = $(f).serialize();
    
    if($('v_erro_c') != null)
        $('v_erro_c').hide();
    
    
    //startLoading();
    
    $.ajax({
            url: '/pre-registo-camp',
            data: 'ajax=AJAX&'+f,
            type: 'POST',
            dataType: 'json',
            success: function(transport) {
                            
                            json = transport;
                            if(json.status == 1) {
                            
                                $('.text').val('');
                                
                                $('#v_erro_c').html(json.msg);
                                $('#v_erro_c').show();
                                 
                            } else { 
                                 $('#v_erro_c').html(json.msg);
                                 $('#v_erro_c').show();
                            }
            }
        });
    return false;
}

