﻿
function DemoRequestForm() {
    this.Container;
    this.NotificationPanel;
    this.Fields;
    this.LoadingIndicator;
    this.Init = function() {
        
        var demoRequestForm = this;
        $(document).keydown(function(e) {
            var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
            if (key == 13) {
                demoRequestForm.ValidateForm();
            }
        });
        
        this.LoadingIndicator = new LoadingModal();
        this.LoadingIndicator.Container = $("#divModalContainer");
        this.LoadingIndicator.Overlay = $("#divModalOverlay");
        
        delete this.Init;
    }
    this.ValidateForm = function() {

        var emailRegex = new RegExp("^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$");
        var phoneRegex = new RegExp("^(([0-9]{1})*[- .(]*([0-9a-zA-Z]{3})*[- .)]*[0-9a-zA-Z]{3}[- .]*[0-9a-zA-Z]{4})+$");
        var validationMessages = new Array();

        var valid = true;
        for (var i in this.Fields) {
            if (this.Fields[i].val() == "" && !this.Fields[i].hasClass("Notes")) {
                valid = false;
                this.Fields[i].addClass("Invalid");
                if (!(validationMessages.length > 0)) {
                    validationMessages.push("All fields are required");
                }
            }
            else
                this.Fields[i].removeClass("Invalid");
        }

        if (!emailRegex.test(this.Fields.txtEmail.val())) {
            valid = false;
            validationMessages.push("A valid email address is required");
            this.Fields.txtEmail.addClass("Invalid");
        }
        else
            this.Fields.txtEmail.removeClass("Invalid");

        if (!phoneRegex.test(this.Fields.txtPhone.val())) {
            valid = false;
            validationMessages.push("A valid phone number is required");
            this.Fields.txtPhone.addClass("Invalid");
        }
        else
            this.Fields.txtPhone.removeClass("Invalid");

        if (valid) {
            this.SubmitForm();
        }
        else {

            var errorDisplay = $(document.createElement("div"))
                                    .append($(document.createElement("p"))
                                        .append(document.createTextNode("Sorry we couldn't submit your request because.."))
                                    );

            var errorList = $(document.createElement("ul"));
            for (var m in validationMessages) {
                errorList
                    .append($(document.createElement("li"))
                        .append(document.createTextNode(validationMessages[m]))
                    );
            }

            errorDisplay.append(errorList);

            this.NotificationPanel
                .html("")
                .addClass("NotificationError")
                .html(errorDisplay)
                .fadeIn(500);
        }
    }
    this.SubmitForm = function() {
        this.LoadingIndicator.ShowModal(150, 80);
        var demoRequestForm = this;
        var demoRequestData = new DemoRequestData();
        demoRequestData.FirstName = this.Fields.txtFirstName.val();
        demoRequestData.LastName = this.Fields.txtLastName.val();
        demoRequestData.Title = this.Fields.txtTitle.val();
        demoRequestData.Company = this.Fields.txtCompany.val();
        demoRequestData.Email = this.Fields.txtEmail.val();
        demoRequestData.Phone = this.Fields.txtPhone.val();
        demoRequestData.Notes = this.Fields.txtNotes.val();
        NewHire.Web.WebServices.DemoRequest.SubmitRequest(demoRequestData, demoRequestForm.SubmitForm_Success, demoRequestForm.SubmitForm_Error, demoRequestForm);
    }
    this.SubmitForm_Success = function(response, demoRequestForm) {
        demoRequestForm.LoadingIndicator.HideModal();
        demoRequestForm.NotificationPanel
            .removeClass("NotificationError")
            .addClass("NotificationSuccess")
            .html(document.createTextNode("Your Demo Request has been submitted, a NewHire representative will reach out to your shortly."))
            .fadeIn(500);
        pageTracker._trackPageview("/Demo/RequestSubmitted");
        
        var capterra_vkey = "5d8f9a675861ade6a22279c881eccb62";
        var capterra_vid = "2070567";
        var capterra_prefix = (("https:" == document.location.protocol) ? "https://ct.capterra.com" : "http://ct.capterra.com");
        $(document).append(unescape("<script src='" + capterra_prefix + "/capterra_tracker.js?vid=" + capterra_vid + "&vkey=" + capterra_vkey + "' type='text/javascript'%3E%3C/script%3E"));
    
    }
    this.SubmitForm_Error = function(error, demoRequestForm) {
        demoRequestForm.LoadingIndicator.HideModal();
        demoRequestForm.NotificationPanel
            .addClass("NotificationError")
            .html(document.createTextNode("There was an error with your submission, please try again."))
            .fadeIn(500);
    }
}

function LoadingModal() {

    this.Container;
    this.Overlay;
    this.Visible = false;

    this.ShowModal = function(width, height) {

        var modalWindow = this;
        this.Container.css(
            {
                "display": "block",
                "width": width,
                "height": height,
                "top": this.GetPositionTop(height),
                "left": this.GetPositionLeft(width)
            }
        );
        this.Overlay.css(
            {
                "display": "block",
                "height": $(document).height()
            }
        );
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            $(document).find("select").css("visibility", "hidden");
        }
        this.Visible = true;
    };

    this.HideModal = function() {

        this.Container.css("display", "none");
        this.Overlay.css("display", "none");

        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            $(document).find("select").css("visibility", "visible");
        }

        this.Visible = false;
    };
    
    this.GetPositionTop = function(height) {
        return (($(window).height() - height) / 2);
    };
    
    this.GetPositionLeft = function(width) {
        return (($(window).width() - width) / 2);
    };
}

function DemoRequestFormFields() {
    this.txtFirstName;
    this.txtLastName;
    this.txtTitle;
    this.txtCompany;
    this.txtEmail;
    this.txtPhone;
    this.txtNotes;
}

function DemoRequestData() {
    this.FirstName;
    this.LastName;
    this.Title;
    this.Company;
    this.Email;
    this.Phone;
    this.Notes;
}