
// Only launched if the DOM is ready
$(document).ready(function() {

	function loadJSON() {
		$.ajax({
			type: "POST",
			url: "http://fmsummit2009.web.shphosting.com/components/com_myfmbutler/posttofm.php?" + params_participant + "order_id=" + order_id,
			data: {name : params_participant} ,
			dataType: "json",
			success: function(json){
				jQuery(this).attr('finished', 'ok');
			}
		});

	}

    // Validate an email address
    function validateEmail(email) {
        var at = email.lastIndexOf("@");
        if (at < 1 || (at + 1) === email.length) {
            return false;
        }
        if (/(\.{2,})/.test(email)) {
            return false;
        }
        var local = email.substring(0, at);
        var domain = email.substring(at + 1);
        if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255) {
            return false;
        }
        if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain)) {
            return false;
        }
        if (!/^"(.+)"$/.test(local)) {
            // It's a dot-string address...check for valid characters
            if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
                return false;
        }
        if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1) {
            return false;	
        }
        return true;
    }

    // Function to show the modal dialog
    function showParticipantsModal(e) {

        // Prevent the default event
        e.preventDefault();

        // Empty the errors
        $('#modalErrors').html('');

        // Show the modal dialog
        $('#addParticipantDetails').modal({
           overlay: 65,
           overlayId: 'contactModalOverlay',
           containerId: 'contactModalContainer',
           persist: true
        });

    }

    // When someone clicks on the add to cart button
    $('a.add-to-cart').click(function(e) {
    	
        // Remember the url
        $.targetUrl = $(this).attr('href');


		
        // Show the modal dialog box
        showParticipantsModal(e);
        
        
        // Don't follow the link right now
        return false;
        
    });
    
    // Remove an order item
    $('a.removeItem').click(function() {
        return confirm('Bent u zeker dat u dit item wil verwijderen?');
    });
    
    // Code for changing participant
    $('a.changeParticipant').click(function(e) {
    
        // Fill in the details
        $('#participant_name').val($(this).attr('participant_name'));
        $('#participant_email').val($(this).attr('participant_email'));
        $('#participant_company').val($(this).attr('participant_company'));

        // Remember the url
        $.targetUrl = $(this).attr('href');
		
		
        // Show the modal dialog box
        showParticipantsModal(e);
        
        return false;
    
    });
    
    // When the details of the participant are entered
    $('#participant_ok').click(function() {

        // Get the list of errors
        var errors = '';

        // Get the data
        var participant_name = $('#participant_name').val();
        var participant_email = $('#participant_email').val();
        var participant_email_confirm = $('#participant_email_confirm').val();
        var participant_company = $('#participant_company').val();

        // Check for errors
        if (jQuery.trim(participant_name) == '') {
            errors += 'Naam van de deelnemer is verplicht.<br/>';
        }
        if (jQuery.trim(participant_email) == '') {
            errors += 'Email van de deelnemer is verplicht.<br/>';
        } else {
            if (validateEmail(participant_email) == false) {
                errors += 'Email van de deelnemer is geen correct email adres.<br/>';
            }
        }
        if (jQuery.trim(participant_email_confirm) == '') {
            errors += 'Bevestiging van de email van de deelnemer is verplicht.<br/>';
        } else {
            if (validateEmail(participant_email_confirm) == false) {
                errors += 'Bevestiging van de email van de deelnemer is geen correct email adres.<br/>';
            }
        }
        if (jQuery.trim(participant_company) == '') {
            errors += 'Bedrijf van de deelnemer is verplicht.<br/>';
        }
        if (jQuery.trim(participant_email) != '' && jQuery.trim(participant_email_confirm) != '') {
            if (participant_email != participant_email_confirm) {
                errors += 'De bevestiging van de email addressen is niet correct.<br/>';
            }
        }              
        
        // Set the errors
        $('#modalErrors').html(errors);

        // Close the modal dialog if no errors
        if (errors == '') {
        
            // Update the URL
            var targetUrl = $.targetUrl;
            targetUrl += '&participant_name=' + escape(participant_name);
            targetUrl += '&participant_email=' + escape(participant_email);
            targetUrl += '&participant_company=' + escape(participant_company);

            // Go to the url
            window.location.href = targetUrl;
            
        }
        
    });
    
    // When the details of the participant are cancelled
    $('#participant_cancel').click(function() {
        $.modal.close();
    });

		
})