jQuery(document).ready(function($) {
	
	var currentTab = 0;
	var validated = new Array();
    var baseUrl = 'http://www.toplettre.fr/';
	openTab(1);

    $('#postMessage').hide();
	
	$('form input[type=button]').click(function(){

		var empty = false;
		var error = false;
	
		$('#tab_'+currentTab+' form input').not('.exclude').each(function(){
			
			var val =  $(this).val();
		
			if( val.length < 1 )
			{
				$(this).removeClass('veld').addClass('error');
				empty = true;
			}else{
				if( $(this).hasClass('error') )
				{
					$(this).removeClass('error').addClass('veld');
				}
			}
		});
		
		if( empty )
		{
			showError('Tous les champs doivent &ecirc;tre remplis !');
			error = true;
		}
		
		if( !error )
		{
			
			var email = $('#tab_'+currentTab+' form input[name=emailadres]').val();
			
			if( email )
			{
				if( !isValidEmailAddress( email ) )
				{
					showError('Adresse e-mail non valable!');
					$('input[name=emailadres]').removeClass('veld').addClass('error');
					error = true;					
				}
			}
			
			if( currentTab == 3 )
			{
				if( validated[1] == undefined )
				{
					alert('Vous n\u2018avez pas enti\350rement compl\351t\351  l\u2018\351tape  n\2601. Retournez \340 cette \351tape et cliquez sur envoyer');
					openTab( 1 );
					error = true;
				}else{
					if( validated[2] == undefined )
					{
						alert('Vous n\u2018avez pas enti\350rement compl\351t\351 l\u2018\351tape n\2602.  Retournez \340 cette \351tape et cliquez sur envoyer');
						openTab( 2 );
						error = true;
					}
				}
				
				if( !error )
				{				
				
					$('#tab_4').html('<div style="padding: 25px;">Chargement des informations de paiement en cours ...</div>');
				
					$.ajax({
						type: "POST",
						url:  baseUrl + "brief/recieve_sollicitatie",
						data: $('body form').serialize(),
						success: function( msg ){
							$('#tab_4').html(msg);
						},
						error: function(xhr, status, error) {
							alert('Une erreur sÕest produite lors de lÕenvoi de vos donn\351es');
							$.ajax({
								type: "POST",
								url:  baseUrl + "error/recieve_error",
								data: "error=" + xhr.responseText
							});
						}

					});
 
				}
		
			}
			
			if( !error )
			{
				validated[currentTab] = true;
				currentTab = parseInt(currentTab) + 1;
				openTab( currentTab );
			}
			
		}
		
	});
	
	$('form input').focus(function(){
		if( $(this).hasClass('error') )
		{
			$(this).removeClass('error').addClass('veld');
		}
	});
	
	$('#menu li').click(function(){
		$(this).parent().children().removeClass('actief');
		$(this).addClass('actief');
		
		id = $(this).attr('id');
		openTab( id.substr(8) );
		
	});
	
    $(".demoText").focus(function(){
        $(this).removeClass('demoText');
    });
	
	$('input[name=functie]').keyup(function(){
		$('.replaceFunctie').html( $(this).val() );
	});
	
	$('input[name=bedrijf]').keyup(function(){
		$('.replaceBedrijf').html( $(this).val() );
	});
	
	$(".selectie span").click(function(){
		var tog = $(this).parent().find("input[type=checkbox]:checked").attr("checked");
		$(this).parent().find("input[type=checkbox]").attr("checked",!tog);
	});

	$(".openLetter a").live('click',function(){
		$(this).parent().parent().find('.brief').removeClass("brief");
        $(this).fadeOut();
		return false;
	});

    $(".addCoupon").live('click',function(){
        $(this)
            .parent()
                .slideUp('normal', function(){
                    $(this).next().slideDown();
                });

        return false;

    });

    $(".addCouponClose").live('click',function(){
        $(this)
            .parent()
                .slideUp('normal', function(){
                    $(this).prev().slideDown();
                });

        return false;

    });

    $(".addCouponSend").live('click',function(){
        var coupon = $(this);

        $.ajax({
            type: "POST",
            url: baseUrl + "brief/appendCoupon",
            data: 'coupon=' + $(this).parent().find('input').val(),
            success: function( msg ){
                if( msg == 'OK' )
                {
                    coupon
                        .parent()
                            .slideUp('normal',function(){
                                $(this)
                                    .html('<b>Coupon is succesvol geactiveerd</b><br/><br/><input type="button" value="Download nu je sollicitiatiebrief" href="'+baseUrl+'brief/get_sollicitatie" class="button_groot" id="load"/>')
                                    .slideDown('normal');
                            })
                }else{
                    alert('De coupon kon niet geactiveerd worden omdat: <br/>' + msg);
                }
            },
            error: function(xhr, status, error) {
                alert('Er is een fout opgetreden met het verzenden van uw gegevens');
                $.ajax({
                    type: "POST",
                    url:  baseUrl + "error/recieve_error",
                    data: "error=" + xhr.responseText
                });
            }
        });

        return false;

    });

	function openTab( container )
	{

		$('#menu li.actief').removeClass('actief');
        $('#menu li span').show();
		$('#menu li#opentab_' + container).addClass('actief').find('span').hide();

		$('#postMessage').hide();
        $('body .down').removeClass('down');

        if( currentTab )
        {
            $('.tabContainer:visible').stop(true,true).slideUp("slow", function () {
                $('#tab_' + container).slideDown("slow");
            });
        }else{
            $('#tab_' + container).slideDown("slow");
        }
		
		if( container == 4 )
		{
			$(".formbox").addClass("white");
		}else{
			$(".formbox").removeClass("white");
		}

		currentTab = container;
	}
	
	function showError( input )
	{
		$('div#postMessage span').html( input ).parent().show();
        $('.tabContainer').addClass('down');
	}
	
	function resetError()
	{
		$('div#postMessage').hide();
        $('body .down').removeClass('down');
	}
	
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	} 
	
}) 
