﻿/*
Javascript control showing a footer
*/

var WebContactForm = Class.create();

WebContactForm.prototype =
{
    // Constructor
    initialize : function()
    {
        // Declare variable holding the id of the entity's html node
        this._nodeId = 'WebContactForm';
        
        // Declare variable holding the html node which serves as content container
        this._htmlContainer;
        
        this._introText;
        
        // Declare variables holding the input fields
        this._salutationInputMale;
        this._salutationInputFemale;
        this._firstNameInput;
        this._lastNameInput;
        this._mailInput;
        this._messageInput;
    },
    
    Build : function()
    {
        // Create a window holding the contact form
        this._htmlContainer = new WebWindow(Lingo.GetLingoItem('CONTACT_FORM'));
        
        this._htmlContainer.Width = 500;
        this._htmlContainer.zIndex = 200;
        this._htmlContainer.CenterScreen = true;
        this._htmlContainer.MakeDraggable = true;
        this._htmlContainer.Class = 'WebContactForm';
        
        // Build label for the salutation
        var salutationLabel = Builder.node('label')
        salutationLabel.appendChild(document.createTextNode('Anrede:'));
        
        // Build radio buttons for the salutation
        this._salutationInputMale = Builder.node('input', { type: 'radio', name: 'input_salutation' }); //var salutationMaleInput = '<input type="radio" name="input_salutation" style="border=0" />' 
        var salutationMaleLabel = Builder.node('label')
        salutationMaleLabel.appendChild(document.createTextNode('Herr'));
        this._salutationInputFemale = Builder.node('input', { type: 'radio', name: 'input_salutation', style: 'margin-left:5px' }); //var salutationFemaleInput = '<input type="radio" name="input_salutation" style="margin-left:5px;border=0" />' 
        var salutationFemaleLabel = Builder.node('label')
        salutationFemaleLabel.appendChild(document.createTextNode('Frau'));
        
        // Build label for the first name
        var firstnameLabel = Builder.node('label');
        firstnameLabel.appendChild(document.createTextNode('Vorname, '));
        
        // Build label for the last name
        var lastnameLabel = Builder.node('label');
        lastnameLabel.appendChild(document.createTextNode('Nachname:'));
        
        // Build text input field for the first name
        this._firstNameInput = Builder.node('input', { type: 'text', name: 'input_firstname' });
        
        // Build text input field for the last name
        this._lastNameInput = Builder.node('input', { type: 'text', name: 'input_lastname', style: '*margin:2px 4px 0 0' });
        
        // Build label for the mail
        var mailLabel = Builder.node('label');
        mailLabel.appendChild(document.createTextNode('E-Mail:'));
        
        // Build text input field for the mail
        this._mailInput = Builder.node('input', { type: 'text', name: 'input_mail' });
        
        // Build label for the message
        var messageLabel = Builder.node('label');
        messageLabel.appendChild(document.createTextNode('Ihre Nachricht:'));
        
        // Build text input field for the mail
        this._messageInput = Builder.node('textarea');
        
        // Build background holder containers for each input field except the radio buttons
        var firstnameBackground = Builder.node('div');
        firstnameBackground.className = 'input_bg_short';
        var lastnameBackground = Builder.node('div', {style: 'margin-left:3px'});
        lastnameBackground.className = 'input_bg_short';
        var mailBackground = Builder.node('div');
        mailBackground.className = 'input_bg_long';
        var messageBackground = Builder.node('div');
        messageBackground.className = 'input_bg_textarea';
        
        // Add the input fields to the background holders
        firstnameBackground.appendChild(this._firstNameInput);
        lastnameBackground.appendChild(this._lastNameInput);
        mailBackground.appendChild(this._mailInput);
        messageBackground.appendChild(this._messageInput);        
        
        // Build containers for labels and input fields
        var labelContainer = Builder.node('div', { style: 'float:left;width:30%;padding-top:4px;line-height:15px;' });
        var inputContainer = Builder.node('div', { style: 'float:left;width:65%;padding-left:10px;border-left:1px solid #A0C9DB' });
        
        labelContainer.appendChild(salutationLabel);
        labelContainer.appendChild(Builder.node('br'));
        labelContainer.appendChild(Builder.node('br'));
        labelContainer.appendChild(firstnameLabel);
        labelContainer.appendChild(lastnameLabel);
        labelContainer.appendChild(Builder.node('br'));
        labelContainer.appendChild(Builder.node('br'));
        labelContainer.appendChild(mailLabel);
        labelContainer.appendChild(Builder.node('br'));
        labelContainer.appendChild(Builder.node('br'));
        labelContainer.appendChild(messageLabel);
        
        inputContainer.appendChild(this._salutationInputMale); //inputContainer.innerHTML += salutationMaleInput;
        inputContainer.appendChild(salutationMaleLabel);
        inputContainer.appendChild(this._salutationInputFemale); //inputContainer.innerHTML += salutationFemaleInput;
        inputContainer.appendChild(salutationFemaleLabel);
        inputContainer.appendChild(Builder.node('br'));
        inputContainer.appendChild(Builder.node('br'));
        inputContainer.appendChild(firstnameBackground);
        inputContainer.appendChild(lastnameBackground);
        inputContainer.appendChild(Builder.node('br'));
        inputContainer.appendChild(Builder.node('br'));
        inputContainer.appendChild(mailBackground);
        inputContainer.appendChild(Builder.node('br'));
        inputContainer.appendChild(messageBackground);
        
        var introTextHolder = Builder.node('div');
        this._introText = new WebText('5b6c543d-1e77-42de-be13-4c45f9916906', introTextHolder);
        this._introText.Show();
        
        var buttonHolder = Builder.node('div', { style: 'clear:left;height:20px;padding:10px 0 0 0;text-align:center;background-color:#FFFFFF' });
        var button = new WebButton('Senden');
        button.OnClick = this.Send.bind(this);
        var buttonNode = button.GetHtml();
        $(buttonNode).setStyle({ position:'absolute',right:'160px' });
        buttonHolder.appendChild(buttonNode);
        
        this._htmlContainer.appendChild(introTextHolder);
        this._htmlContainer.appendChild(Builder.node('br'));
        this._htmlContainer.appendChild(labelContainer);
        this._htmlContainer.appendChild(inputContainer);
        this._htmlContainer.appendChild(buttonHolder);
    },
    
    Show : function()
    {        
        if((this._htmlContainer != null) && (this._htmlContainer.parentNode != null))
        {
            this._htmlContainer.parentNode.removeChild(this._htmlContainer);
        }
        
        // Create modal dialogue
        this._overlay = new WebOverlay();
        
        // Get the window's scroll offset
        var scrollOffset = Helpers.GetWindowScrollOffset();
        // Calculate the value for the margin-top property
        var topMargin = -50 + scrollOffset.top;
        
        // Build container for contact form
        var contactFormWrapper = Builder.node('div', { id : 'ContactFormWrapper', style : 'background-color:#FFFFFF;top:50%;left:50%;width:50px;height:50px;margin:' + topMargin + 'px 0 0 -25px;position:absolute;z-index:150;background-image:url(Template/INVENIT1/Image/loadingImageWhite.gif);background-repeat:no-repeat;background-position:center center;display:none' })
        // Append large image to document body
        document.body.appendChild(contactFormWrapper);
        
        // Show overlay and contactFormWrapper
        this._overlay.Show(0.6);
        Effect.Appear(contactFormWrapper, { duration: 0.8 });
        
        // Call Build function to create the contact form
        this.Build();
        this._htmlContainer.OnClose = this.Hide.bind(this);
        
        var morphFunc = function()
        {
            // Get the window's scroll offset
            var scrollOffset = Helpers.GetWindowScrollOffset();
            // Calculate the value for the margin-top property
            var topMargin = ((396 / 2) * (-1)) + scrollOffset.top;
            // Create new effect
            new Effect.Morph(contactFormWrapper, { duration:0.8, style: 'width:500px;height:396px;margin-top:' + topMargin + 'px;margin-left:-' + (500 / 2) + 'px;' });        
        }
        
        var displayFunc = function()
        {
            var cForm = this._htmlContainer.GetHtml();
            cForm.style.display = 'none';
            document.body.appendChild(cForm);
            new Effect.Appear(cForm);
        }
        
        var removeWrapperFunc = function()
        {
            if(contactFormWrapper != null)
            {
                contactFormWrapper.parentNode.removeChild(contactFormWrapper);
            }
        }
        
        window.setTimeout(morphFunc.bind(this), 2000)
        window.setTimeout(displayFunc.bind(this), 2900);
        window.setTimeout(removeWrapperFunc.bind(this), 3900);
        MainMenu.Disable();
    },
    
    Hide : function()
    {    
        this._overlay.Hide();
        MainMenu.Enable();
    },
    
    Send : function()
    {
        var valueSalutationMale = $(this._salutationInputMale).getValue();
        var valueSalutationFemale = $(this._salutationInputFemale).getValue();
        var valueFirstName = $(this._firstNameInput).getValue();
        var valueLastName = $(this._lastNameInput).getValue();
        var valueMail = $(this._mailInput).getValue();
        var valueMessage = $(this._messageInput).getValue();
        
        var alertMessages = new Array();
        
        if((valueSalutationMale == null) && (valueSalutationFemale == null))
        {
            alertMessages.push('MISSING_FIELD_SALUTATION');
        }
        else if(valueSalutationMale == 'on')
        {
            var valueSalutation = 'male';
        }
        else if(valueSalutationFemale == 'on')
        {
            var valueSalutation = 'female';
        }
        
        if((valueLastName == null) ||(valueLastName.blank()))
        {
            alertMessages.push('MISSING_FIELD_LASTNAME');
        }
        
        var mailRegX = /^\b[A-Za-z0-9._%-äöüßÄÖÜ]+@[A-Za-z0-9.-äöüßÄÖÜ]+\.[A-Za-z]{2,4}\b$/;
        if((valueMail == null) ||(valueMail.blank()) || (!mailRegX.test(valueMail)))
        {
            alertMessages.push('MISSING_FIELD_MAILADDRESS');
        }
        
        if((valueMessage == null) ||(valueMessage.blank()))
        {
            alertMessages.push('MISSING_FIELD_MESSAGE');
        }
        
        if(alertMessages.length > 0)
        {
            var msgText = '';
            
            for(var i = 0; i < alertMessages.length; i++)
            {
                msgText += Lingo.GetLingoItem(alertMessages[i]) + '<br>';
            }
            
            var msgBox = new WebMessage(Lingo.GetLingoItem('MISSING_FIELDS'), msgText);
            msgBox.EnableAlertImage = true;
            msgBox.Show();
            
            return;
        }
        
        // Create input parameters for the webservice method
        var params=$H( {piHostHeader : Site.HostHeader, piSalutation : valueSalutation, piFirstName : valueFirstName, piLastName : valueLastName, piMail : valueMail, piMessage : valueMessage} ).toQueryString();
        // Create new Ajax request
        new Ajax.Request("ISDataService.asmx/SendMail", { method : "post", parameters : params, requestHeaders : ['Pragma', 'no-cache', 'Cache-Control', 'no-store, no-cache, max-age=0, must-revalidate'], onSuccess : this.OnSendResponse.bind(this), onFailure: this.OnSendFail.bind(this) });
        
        this._htmlContainer.Close();
    },
    
    OnSendFail : function()
    {
        Logs.WriteError('ERROR_SENDING_MAIL', 'Send', null, 'CONTACTFORM', null, true);
    },
    
    // Receives the success message
    OnSendResponse : function(transport)
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_RECEIVING_DATA', 'OnSendResponse', arguments, 'CONTACTFORM', null)
            // Set local variable holding the xml data
            this._entity = new EntityObject(new XmlDocument(transport.responseXML));
            
            // Write error log if the last interaction did not succeed
            if(this._entity.Success == false)
            {
                // Write Error to the Log
                Logs.WriteError(this._entity.SuccessMessage, 'OnDataRequestResponse', arguments, 'CONTACTFORM', null, true)
                
                return;
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnSendResponse', arguments, 'CONTACTFORM', null, e, false)
        }
    }
}