var currForm = null;

$(function(){
    $('form').submit(function(){
        currForm = $(this).attr('id');
    });

    $('#c_edit').live('click', function(){
        clearConfirmPage();
        showForm();
    });
    
    $('#c_print').live('click', function(){
        window.print();
    });

    $('#c_cancel').live('click', function(){
        location.href = "/join.html";
    });

    $('#c_confirm').live('click', function(){
        $.get('/join_send.html?total_complete=1', function(data) {
           if (1 == data) {
               clearConfirmPage();
               hideForm();
               $('#confirm_page').html('<center><h2>SUCCESSFULLY COMPLETED</h2></center>');
               window.setTimeout("document.location.href='/'", 7000);
           } else {
               alert(data);
           }
        });
    });

    $('.fl-t').live('click', function(event){
        $('body').prepend('<div id="overlay"></div>')
        $('#overlay').css(
            {
                'margin' : '0'
                , 'background-color' : 'white'
                , 'position' : 'fixed'
                , 'width' : $('body').width()
                , 'height' : $('body').height()
                , 'z-index' : '1000'
            }
        ).fadeTo(1, 0.5);

        $('body').prepend('<div id="flash_tour"></div>');
        $('#flash_tour').css(
            {
                'margin' : '0'
                , 'position' : 'fixed'
                , 'width' : '1100px'
                , 'height' : '588px'
                , 'top' : '50%'
                , 'left' : '50%'
                , 'margin' : '-294px 0 0 -550px'
                , 'z-index' : '1001'
            }
        ).html($('#flashContent').html());
        event.preventDefault();
    });

   $('#overlay').live('click', function(){
       $(this).fadeOut(1500, function(){$(this).remove()});
       $('#flash_tour').slideUp(800, function(){$(this).remove()});
   });

   $('#cft').live('click', function(){
        $('#overlay').click();
        return false;
   });

});

function sendContactStart ()
{
    beforeResponse();
    $('#f_preloader').show();
}

function sendContactResponse (response)
{
    $('#f_preloader').hide();
    if (1 == response) {
        $('#m_form').hide();
        $('#m_msg').html('<center><h1>REQUEST SUCCESSFULLY SENT, WE WILL CALL YOU BACK SHORTLY.</h1></center>!');
        window.setTimeout("$('#m_form').show(); $('#m_msg').html('');", 5000);
    } else {
        alert(response);
    }
}

function sendJoinStart ()
{
    
}

function sendJoinResponse (response)
{
    if (1 == response) {
        hideForm();
        getConfirmPage();
    } else {
        alert(response);
    }
}

function hideForm ()
{
    $('.note').hide('slow');
    $('.form').hide('slow');
}

function showForm ()
{
    $('.note').show('slow');
    $('.form').show('slow');
}

function getConfirmPage ()
{
    $.get('/confirm.html', function(data) {
        $('#confirm_page').html(data);
    });
}

function clearConfirmPage ()
{
    $('#confirm_page').html('');
}

function beforeResponse ()
{
    $(".error").removeClass('error');
}

function afterResponse (response)
{
    data = jQuery.parseJSON(response);

    if (data.errors) {
        $.each(data.errors, function(index, value) {
            $('#' + currForm).find("input[name='" + index + "']").parent('div').addClass('error');
        });
    } else if(data.success) {
        alert('Success');
    }
}

function captchaReload() {
	$('.captchaImage').attr('src', 'captcha.html?rand='+Math.random());
	$('#indicator').html('<img src="images/load_indicator1.gif" alt="" width="16" height="16" />');
}

