﻿var ContactUsPopup = Class.extend
({
	container: 'contactUsPopup',

	init: function(params) {
		this.sendUrl = params.sendUrl;

		var popup = this;

		$('.sendMessage a').click(function() {
			popup.send();
		});
	},

	clearInvalid: function(selector) {
		if ($(selector, this.getContainer()).siblings('.erorMsg').length > 0) {
			$(this.getContainer()).height($(this.getContainer()).height() - 15);
			$(selector, this.getContainer()).siblings('.erorMsg').remove();
		}

		$(selector, this.getContainer()).parent().removeClass('error');
	},

	markInvalid: function(selector, error) {
		$(this.getContainer()).height($(this.getContainer()).height() + 15);
		$(selector, this.getContainer()).parent().addClass('error');
		$('<span class="erorMsg">' + error + '</span>').insertAfter($(selector, this.getContainer()));
	},

	validateEmail: function(email) {
		var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		if (email != '' && emailReg.test(email) == false) {
			return false;
		}

		return true;
	},

	show: function() {
		this.clearFields();

		var popup = this;

		this.dialog = new PixDialog('#' + this.container, { title: 'AdoramaPix API Request Form', autoOpen: false, dialogClass: 'JOBPopup', width: 460, height: 507 });

		this.dialog.onHiding = function() { popup.dialog.destroy(); popup.dialog = null; };

		this.dialog.show();
	},

	clearFields: function() {
		$('#name', this.getContainer()).val('');
		$('#phone', this.getContainer()).val('');
		$('#company', this.getContainer()).val('');
		$('#email', this.getContainer()).val('');
		$('#message', this.getContainer()).val('');

		this.clearErrors();
	},

	collect: function() {
		var result = {};

		result.Name = $('#name', this.getContainer()).val();
		result.Email = $('#email', this.getContainer()).val();
		result.Phone = $('#phone', this.getContainer()).val();
		result.Company = $('#company', this.getContainer()).val();
		result.Message = $('#message', this.getContainer()).val();
		result.SendUrl = this.sendUrl;

		return result;
	},

	send: function() {
		if (!this.validate()) {
			return false;
		}

		var model = this.collect();
		var popup = this;

		$.post
		(
			this.sendUrl,
			model,
			function(result) {
				if (result.Succeeded == true) {
					if (popup.dialog != null) {
						popup.dialog.hide();
					}
				}
				else {
					alert('Error occured during registration.');
				}
			},
			'json'
		);
	},

	validate: function() {
		var result = true;

		this.clearErrors();

		var name = $('#name', this.getContainer()).val();
		//var phone = $('#phone', this.getContainer()).val();
		var company = $('#company', this.getContainer()).val();
		var email = $('#email', this.getContainer()).val();
		var message = $('#message', this.getContainer()).val();

		// Validate required fields
		if (name == '') {
			this.markInvalid('#name', 'Name is required');
			result = false;
		}

		/*if (phone == '') {
			this.markInvalid('#phone', 'Phone is required');
			result = false;
		}*/

		if (company == '') {
			this.markInvalid('#company', 'Company is required');
			result = false;
		}

		if (message == '') {
			this.markInvalid('#message', 'Message is required');
			result = false;
		}

		if (email == '') {
			this.markInvalid('#email', 'Email is required');
			result = false;
		}

		if (this.validateEmail(email) == false) {
			this.markInvalid('#email', 'Incorrect email');
			result = false;
		}

		return result;
	},

	clearErrors: function() {
		this.clearInvalid('#name');
		this.clearInvalid('#phone');
		this.clearInvalid('#company');
		this.clearInvalid('#email');
		this.clearInvalid('#message');
	},

	getContainer: function() {
		return $('#' + this.container).get(0);
	}
});