function doSub() {
    $().ready(function(){
        $("#subForm").validate({
            submitHandler: function() {
                formSubmit(document.getElementById('subForm'));
            },
            rules : {
                eml: {email: true }
            },
            messages : {
                eml: "Некорректный email"
            }
        });
    });
}
function formSubmit(){
    openAjaxPopup('/subscribe.php?action=subscribe&email='+document.getElementById('eml').value, 300);
    return false;
}
$.setupJMPopups({
    screenLockerBackground: "#e6e6e6",
    screenLockerOpacity: "0.7"
});

function openAjaxPopup(urlc, width) {
    $.openPopupLayer({
        name: "mySecondPopup",
        width: width,
        url: urlc
    });
}
function openStaticPopup() {
    $.openPopupLayer({
        name: "myStaticPopup",
        width: 250,
        target: "myHiddenDiv"
    });
}

function doSend(){
    document.getElementById('error').innerHTML = '';
    var body_mes = document.getElementById('body_mes').value.replace(/(^\s*)|(\s*$)/g, "") ;
    if ( body_mes == '' || body_mes.length<15 ){
        openStaticPopup();
        return false;
    }
    if ( body_mes.length>1024 ){
        document.getElementById('error').innerHTML = 'Слишком большое сообщение!';
        return false;
    }
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if ( req.responseJS.error != ''  ){
                document.getElementById('error').innerHTML = req.responseJS.error;
            }
            else{
                document.getElementById('result').innerHTML = req.responseJS.result;
            }
        }
    }
    req.open('post', '/send.php', false);
    req.send( { q: document.getElementById('f') } );
}
function doCont() {
    $().ready(function(){
        $("#contact").validate({
            submitHandler: function() {
                contSubmit(document.getElementById('contact').value);
            },
            errorElement:  'span' ,
            messages : { 
                name: "Укажите Имя", 
                email: "Укажите Email", 
                subject: "Укажите тему сообщения", 
                message: "Укажите текст сообщения" 
            } 
        }); 
    });
}
function contSubmit(){
    document.getElementById('error').innerHTML = '';
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if ( req.responseJS.error != ''  ){
                document.getElementById('error').innerHTML =  req.responseJS.error;
            }
            else{
                document.getElementById('result').innerHTML = req.responseJS.result;
            }
        }
    }
    req.open('post', '/contact.php', false);
    req.send( { q: document.getElementById('contact') } );

}
