﻿var contactWin = null, contactSmallWin = null;
var contact_name, contact_email, contact_phone, contact_price, contact_location, contact_destination, contact_peopleCount, contact_howYouKnow, contact_somethingElse;
var contact_name_small, contact_email_small, contact_phone_small, contact_interests_small, contact_howYouKnow_small, contact_somethingElse_small;

function showContactForm()
{
    if (contactWin == null)
    {
        contactWin = new Ext.Window({
            width: 455,
            height: 676,
            modal: true,
            contentEl: "ContactForm",
            frame: false,
            closable: false,
            resizable: false,
            shadow: false
        });
        contactWin.show();

        contact_name = new Ext.form.TextField({
            width: 277,
            applyTo: 'name',
            fieldClass: 'tour-assembly-comboBox',
            regex: /[A-Za-zА-Яа-я -]/,
            allowBlank: false,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_email = new Ext.form.TextField({
            width: 277,
            applyTo: 'email',
            fieldClass: 'tour-assembly-comboBox',
            regex: /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z]{2,4}$/,
            allowBlank: false,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_phone = new Ext.form.TextField({
            width: 277,
            applyTo: 'phone',
            fieldClass: 'tour-assembly-comboBox',
            enableKeyEvents: true,
            regex: /^\d{1}-\d{3}-\d{7}$/,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });
        contact_phone.on('keydown', function (e) { if (e.getValue().length == 1 || e.getValue().length == 5) e.setValue(e.getValue() + '-'); });

        contact_price = new Ext.form.ComboBox({
            width: 205,
            listWidth: 205,
            editable: false,
            transform: 'readyToPay',
            fieldClass: 'tour-assembly-comboBox',
            style: { lineHeight: '25px', height: '28px', color: 'Black', border: 'none' },
            invalidClass: '_invalidField',
            triggerAction: 'all',
            triggerConfig: { tag: "img", src: 'http://www.vtrip.me/img/new/select-arrow-noborder.gif', cls: "x-form-trigger " + this.triggerClass, style: { margin: '3px 0'} }
        });

        contact_location = new Ext.form.TextField({
            width: 277,
            applyTo: 'location',
            fieldClass: 'tour-assembly-comboBox',
            regex: /[A-Za-zА-Яа-я -]/,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_destination = new Ext.form.TextField({
            width: 277,
            applyTo: 'destination',
            fieldClass: 'tour-assembly-comboBox',
            regex: /[A-Za-zА-Яа-я -]/,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_peopleCount = new Ext.form.ComboBox({
            width: 205,
            listWidth: 205,
            editable: false,
            transform: 'peopleCount',
            fieldClass: 'tour-assembly-comboBox',
            style: { lineHeight: '25px', height: '28px', color: 'Black', border: 'none' },
            invalidClass: '_invalidField',
            triggerAction: 'all',
            triggerConfig: { tag: "img", src: 'http://www.vtrip.me/img/new/select-arrow-noborder.gif', cls: "x-form-trigger " + this.triggerClass, style: { margin: '3px 0'} }
        });

        contact_howYouKnow = new Ext.form.ComboBox({
            width: 275,
            listWidth: 275,
            editable: true,
            transform: 'howYouKnow',
            fieldClass: 'tour-assembly-comboBox',
            style: { lineHeight: '25px', height: '28px', color: 'Black', border: 'none' },
            invalidClass: '_invalidField',
            triggerAction: 'all',
            //            value: '',
            emptyText: 'Выберите из списка или напишите',
            triggerConfig: { tag: "img", src: 'http://www.vtrip.me/img/new/select-arrow-noborder.gif', cls: "x-form-trigger " + this.triggerClass, style: { margin: '3px 0'} }
        });

        contact_somethingElse = new Ext.form.TextField({
            width: 277,
            height: 83,
            applyTo: 'somethingElse',
            fieldClass: 'tour-assembly-comboBox',
            style: { color: 'Black', fontFamily: 'Verdana' },
            emptyText: 'Чем Вы обычно занимаетесь, что любите? Что хотите получить от путешествия?',
            invalidClass: '_invalidField'
        });
    }
    else
    {
        contactWin.show();
    }
}

function showContactSmallForm()
{
    if (contactSmallWin == null)
    {
        contactSmallWin = new Ext.Window({
            width: 517,
            height: 613,
            modal: true,
            contentEl: "ContactSmallForm",
            frame: false,
            closable: false,
            resizable: false,
            shadow: false
        });
        contactSmallWin.show();

        contact_name_small = new Ext.form.TextField({
            width: 339,
            applyTo: 'name_small',
            fieldClass: 'tour-assembly-comboBox',
            regex: /[A-Za-zА-Яа-я -]/,
            allowBlank: false,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_email_small = new Ext.form.TextField({
            width: 339,
            applyTo: 'email_small',
            fieldClass: 'tour-assembly-comboBox',
            regex: /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z]{2,4}$/,
            allowBlank: false,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_phone_small = new Ext.form.TextField({
            width: 339,
            applyTo: 'phone_small',
            fieldClass: 'tour-assembly-comboBox',
            enableKeyEvents: true,
            regex: /^\d{1}-\d{3}-\d{7}$/,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });
        contact_phone_small.on('keydown', function (e) { if (e.getValue().length == 1 || e.getValue().length == 5) e.setValue(e.getValue() + '-'); });

        contact_interests_small = new Ext.form.TextField({
            width: 339,
            applyTo: 'interests_small',
            fieldClass: 'tour-assembly-comboBox',
            regex: /[A-Za-zА-Яа-я -]/,
            style: { lineHeight: '25px', height: '28px', color: 'Black' },
            invalidClass: '_invalidField'
        });

        contact_howYouKnow_small = new Ext.form.ComboBox({
            width: 339,
            listWidth: 339,
            editable: true,
            transform: 'howYouKnow_small',
            fieldClass: 'tour-assembly-comboBox',
            style: { lineHeight: '25px', height: '28px', color: 'Black', border: 'none' },
            invalidClass: '_invalidField',
            triggerAction: 'all',
            //            value: '',
            emptyText: 'Выберите из списка или напишите',
            triggerConfig: { tag: "img", src: 'http://www.vtrip.me/img/new/select-arrow-noborder.gif', cls: "x-form-trigger " + this.triggerClass, style: { margin: '3px 0'} }
        });

        contact_somethingElse_small = new Ext.form.TextField({
            width: 339,
            height: 83,
            applyTo: 'somethingElse_small',
            fieldClass: 'tour-assembly-comboBox',
            style: { color: 'Black', fontFamily: 'Verdana' },
            emptyText: 'например, вам бы хотелось что бы мы позвонили или вы сами собираетесь заглянуть к нам? Дайте нам знать об удобном для вас времени',
            invalidClass: '_invalidField'
        });
    }
    else
    {
        contactSmallWin.show();
    }
}

function sendMail(type)
{
    var isFormValid = true;
    var message = '';

    if (type == 'full')
    {
        if (!contact_name.validate())
        {
            isFormValid = false;
        }

        if (!contact_email.validate())
        {
            isFormValid = false;
        }

        if (!contact_phone.validate())
        {
            isFormValid = false;
        }

        if (!contact_location.validate())
        {
            isFormValid = false;
        }

        if (!contact_destination.validate())
        {
            isFormValid = false;
        }

        if (!isFormValid)
        {
            return;
        }

        message = 'Имя: ' + contact_name.getValue() + '<br />E-mail ' + contact_email.getValue() + '<br />';
        if (contact_phone.getValue() != '')
        {
            message += 'Контактный номер: ' + contact_phone.getValue() + '<br />';
        }
        message += 'Готов потратить: ' + contact_price.getValue() + '<br />Место нахождения: ' + contact_location.getValue() + '<br />Место назначения: ' + contact_destination.getValue() + '<br />Количество человек: ' + contact_peopleCount.getValue() + '<br />';
        message += 'Как о нас узнали: ' + contact_howYouKnow.getValue() + '<br />Что-нибудь еще: ' + contact_somethingElse.getValue();
        if ($('ctl00_bodyContent__ctrl_0_AgreeCheckBox').checked)
        {
            message += '<br />Да, я хочу получать новости';
        }
        contactWin.hide();
    }
    else
    {
        if (!contact_name_small.validate())
        {
            isFormValid = false;
        }

        if (!contact_email_small.validate())
        {
            isFormValid = false;
        }

        if (!contact_phone_small.validate())
        {
            isFormValid = false;
        }

        if (!contact_interests_small.validate())
        {
            isFormValid = false;
        }

        if (!isFormValid)
        {
            return;
        }

        message = 'Имя: ' + contact_name_small.getValue() + '<br />E-mail ' + contact_email_small.getValue() + '<br />';
        if (contact_phone_small.getValue() != '')
        {
            message += 'Контактный номер: ' + contact_phone_small.getValue() + '<br />';
        }
        message += 'Область интересов: ' + contact_interests_small.getValue() + '<br />Как о нас узнали: ' + contact_howYouKnow_small.getValue() + '<br />Что-нибудь еще: ' + contact_somethingElse_small.getValue();
        if ($('ctl00_bodyContent__ctrl_0_AgreeCheckBox_small').checked)
        {
            message += '<br />Да, я хочу получать новости';
        }
        contactSmallWin.hide();
    }

    try
    {
        xmlRequest = new XMLHttpRequest();
    }
    catch (ex)
    {
        xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = document.location.href.substring(0, document.location.href.lastIndexOf('/')) + "/services/SendMail.ashx";
    xmlRequest.open("POST", url, true);
    //    xmlRequest.onreadystatechange = login_applyUpdate;
    xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlRequest.send("&action=sendmail&tour_id=" + escape($('_tourID').value) + "&tour_title=" + escape($('TourTitle').innerHTML) + "&message=" + escape(message) + "&email=" + escape(contact_email_small.getValue()) + "&subscribe=" + ($('ctl00_bodyContent__ctrl_0_AgreeCheckBox_small').checked ? '1' : '0'));
}
