var myDomain = document.domain;
var action;

$(document).ready(function () {
    url = "http://" + myDomain + "/system/modal.php";
	$('.contact').click(function (e) {
		e.preventDefault();
        action = 'contact';
        get_form(url + "?action=view_contact_form", {'width': '486px', 'top':'20%', 'margin-left':'-243px'});
	});
});

function get_form(url, style)
{
//alert(url);    
    $.get(url, function(data){
        // create a modal dialog with the data
        $(data).modal({
            onOpen: modal.open,
            onShow: modal.show,
            onClose: modal.close,
            containerCss: (style)
        });
    });
}

var modal = {
    message: null, 
    open: function (dialog) {
        dialog.overlay.fadeIn(300, function () {
			dialog.container.fadeIn(300, function () {
				dialog.data.fadeIn(300);
            });
        });
    }, 
    show: function (dialog) {
		$('#message_send').click(function (e) {
			e.preventDefault();
            var error = modal.confirm();
          
            if (error == '')
            {
                $('.wait').show(1000, function () {
                    $('#message').hide(1000, function () {
                        switch (action)
                        {
                            case 'contact':
                                res_url = "http://" + myDomain + "/system/modal.php?action=send_contact&form[body]=" + $('#body').val()+"&form[email]="+$('#email').val();
							
                                break;
                        }

                        $.getJSON(res_url, function(data){
                            if (data.error == '0')
                            {
                                $('.message-error').hide();
                                $('.td_contact_body').html(data.message);
                                $('.td_contact_body').css('height', '250px');
                            
                                $('.wait').hide();
                                $('#message').show(500);
                                
                                dialog.data.fadeOut(1000, function () {
                                    dialog.container.fadeOut(200, function () {
                                        dialog.overlay.fadeOut(100, function () {
                                            $.modal.close();
                                        });
                                    });
                                });
                            }
                            else
                            {
                                $('.wait').hide();
                                $('.message-error').html(data.message);
                                $('.message-error').show();

                                $('#message').show(500);
                            }
                        });
                    });                    
                });
            }
            else 
            {
                $('.message-error').html(error);
                $('.message-error').show(500);
            }
                
                
        });
    }, 
    close: function (dialog) {
        dialog.data.fadeOut(300, function () {
            dialog.container.fadeOut(300, function () {
                dialog.overlay.fadeOut(300, function () {
                    $.modal.close();
                });
            });
        });
    }, 
    confirm: function (dialog) {
        var num = 1;
        var mess = '';
        var str = /^[A-Za-z0-9]+[-_\.]*\w*@\w+-?\w*\.[\.\-_A-Za-z0-9]*[a-z]{2,4}$/i; 
        var str_code = /^[0-9]{6}$/;
        
        if (action == 'contact') 
        {
            if ($('#body').val() == '')
            {
                mess += num + '. Не заполнено поле "текст сообщения"<br>';
                num++;
            }
            if ($('#email').val()=='')
            {
              mess+=num+'. Не заполнено поле "ваш e-mail"<br>';
              num++;
            }
        }
        
        return mess;
    }
};

