(function () {
	this.Namespace = {};
})(this);

(function (global, namespace, undefinded) {
	namespace.submitForm = function (formId) {
		$.ajax({
			type: 'post',
            url: $('#'+ formId).attr('action'),
			dataType: 'json',
			data: namespace.getFormParameters(formId),

			success: function () {

                var top = $('#colorbox').position().top,
                	confirmBox = (formId === 'sendPdfForm') ?
                		$('#brochure-confirm-box') :
                		$('#submission-confirm-box');
                
                top = (formId === 'sendPdfForm') ? top : 150;

                $('#cboxClose').click();

				$(confirmBox).css('top', top).fadeIn();

				setTimeout(function () {
					$(confirmBox).fadeOut('slow');
					$('#' + formId)[0].reset();
				}, 5000)
			},
			error: function () {
				alert('Error');
			}
		});
	}
	
	namespace.getFormParameters = function (formId) {
		var d = {};
		$('#' + formId).find('input[type!=submit]').each(function (i, el) {
			d[$(el).attr('name')] = $(el).val();
		});
		$('#' + formId).find('textarea').each(function (i, el) {
			d[$(el).attr('name')] = $(el).val();
		});
		return d;
	}
	
	$(function () {
		if (!!$('.form-with-confirmation').length > 0 ) $('.form-with-confirmation')[0].reset();
		DOL._Form.create('form-events-demo').init({
			'onSubmit': function (formId) {
				namespace.submitForm(formId);
			}
		}).addValidators([
			['first_name', DOL.Validator.isNotBlank, 'Required'],
			['last_name', DOL.Validator.isNotBlank, 'Required'],
			['title', DOL.Validator.isNotBlank, 'Required'],
			['company', DOL.Validator.isNotBlank, 'Required'],
			['email', DOL.Validator.isNotBlank, 'Required'],
			['email', DOL.Validator.isValidEmailFormat, 'Invalid'],
			['phone', DOL.Validator.isNotBlank, 'Required'],
			['phone', DOL.Validator.isValidPhonenumber, 'Invalid']
		]);
		
		DOL._Form.create('form-events-webcasts').init({
			'onSubmit': function (formId) {
				namespace.submitForm(formId);
			}
		}).addValidators([
			['first_name', DOL.Validator.isNotBlank, 'Required'],
			['last_name', DOL.Validator.isNotBlank, 'Required'],
			['title', DOL.Validator.isNotBlank, 'Required'],
			['company', DOL.Validator.isNotBlank, 'Required'],
			['email', DOL.Validator.isNotBlank, 'Required'],
			['email', DOL.Validator.isValidEmailFormat, 'Invalid'],
			['phone', DOL.Validator.isNotBlank, 'Required'],
			['phone', DOL.Validator.isValidPhonenumber, 'Invalid']
		]);
		
		DOL._Form.create('form-contact').init({
			'onSubmit': function (formId) {
				namespace.submitForm(formId);
			}
		}).addValidators([
			['first_name', DOL.Validator.isNotBlank, 'Required'],
			['last_name', DOL.Validator.isNotBlank, 'Required'],
			['email', DOL.Validator.isNotBlank, 'Required'],
			['email', DOL.Validator.isValidEmailFormat, 'Invalid'],
			['phone', DOL.Validator.isNotBlank, 'Required'],
			['phone', DOL.Validator.isValidPhonenumber, 'Invalid'],
			['comments', DOL.Validator.isNotBlank, 'Required']
		]);

            
        DOL._Form.create('sendPdfForm').init({
            'onSubmit': function (formId) {
                namespace.submitForm(formId);
            }
            }).addValidators([
            ['full_name', DOL.Validator.isNotBlank, 'Please enter your name.'],
            ['company', DOL.Validator.isNotBlank, 'Please enter a company.'],
            ['email_addr', DOL.Validator.isNotBlank, 'Please enter an email.'],
            ['email_addr', DOL.Validator.isValidEmailFormat, 'Please enter a valid email.'],
        ]);

	});
})(this, this.Namespace);
