﻿function Perten() {}

Perten.prototype.initQuestionnaire = function (options) {
    options = options || {};
    this.formContainer = options.formContainer || $("#contact-form");
    this.url = this.formContainer.data("url");
    this.pageId = this.formContainer.data("pageId");
    this.messageField = options.messageField || $("#contact-form-question");
    this.nameField = options.nameField || $("#contact-form-name");
    this.emailField = options.emailField || $("#contact-form-email");
    this.companyField = options.companyField || $("#contact-form-company");
    this.positionField = options.positionField || $("#contact-form-position");
    this.countryField = options.countryField || $("#contact-form-country");
    this.postButton = options.postButton || $("#contact-form-button");
    this.successOverlay = $("#contact-form-success");
    this.formBox = $("#form-box");
    this.errorOverlay = $("#contact-form-error");
    this.errorMessage = $(".error-message", this.errorOverlay);

    var currentInstance = this;

    $("a.close", this.successOverlay).click(function (e) {
        e.preventDefault();
        currentInstance.messageField.val('');
        currentInstance.emailField.val('');
        currentInstance.successOverlay.fadeOut("fast");
    });

    $("a.close", this.errorOverlay).click(function (e) {
        e.preventDefault();
        currentInstance.errorOverlay.fadeOut("fast");
    });

    this.postButton.click(function (e) {
        e.preventDefault();
        var data = currentInstance.getQuestionnaireData();
        var validationMessages = currentInstance.validate(data);
        if (validationMessages.length > 0) {
            currentInstance.onErrorPostedQuestionnaire(validationMessages.join("\n"));
            return false;
        }
        //alert("data=" + data.message);
        //return false;
        currentInstance.postQuestionnaire(data);
    });
};

Perten.prototype.validate = function (data) {
	var messages = [];
	if (data.name.length <= 3) {
	    messages.push("Name is mandatory. <br/>");
	}
	if (data.email.length <= 3) {
	    messages.push("E-mail is mandatory. <br/>");
	}
	if (data.country.length <= 3) {
	    messages.push("Country is mandatory. <br/>");
	}
	if (data.message.length <= 0) {
		messages.push("Question is mandatory. ");
	}

	return messages;
}

Perten.prototype.postQuestionnaire = function (data) {
    var currentInstance = this;
    $.ajax({
        url: currentInstance.url,
        type: "GET",
        cache: false,
        data: data,
        success: function (data, textStatus, jqXHR) {
            currentInstance.onSuccessPostedQuestionnaire(data, textStatus, jqXHR);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            currentInstance.log(jqXHR.responseText);
            currentInstance.onErrorPostedQuestionnaire("Form could not be sent. Please try again later.");
        }
    });
};

Perten.prototype.getQuestionnaireData = function () {
    return {
		message : this.messageField.val(),
		name: this.nameField.val(),
		email: this.emailField.val(),
		pageId: this.pageId,
		company: this.companyField.val(),
		position: this.positionField.val(),
		country: this.countryField.val()
}
};

Perten.prototype.onSuccessPostedQuestionnaire = function (data, textStatus, jqXHR) {
	this.successOverlay.show();
};

Perten.prototype.onErrorPostedQuestionnaire = function (message) {
	this.log(message);
	this.errorMessage.html(message);
	this.errorOverlay.show();	
};

Perten.prototype.log = function (msg) {
	if(window && window.console)
		window.console.log(msg);
};


