

function gecko() {

    return document.getBoxObjectFor != undefined;
}


function webkit(){
    return   ! navigator.taintEnabled;
}


function css( e, p, v ) {

    if( arguments.length == 3 )
        return e.style[ camelCase(p) ] = v;
        
    if( e.currentStyle )
        return e.currentStyle[ camelCase(p) ];
    var computed = document.defaultView.getComputedStyle( e, null );
    return   computed &&   computed.getPropertyValue([ p ]);

    function camelCase( s ) {

        return s.replace(/-\D/g, function(match){
            return match.charAt(1).toUpperCase();
        });
    }
}


function offsetXY( n, rel ) {

    var x=0, y=0, c=n;
    while( c && (c != rel) ) {
        x += c.offsetLeft;
        y += c.offsetTop;
        if( gecko() ||   webkit() ) {
            x += parseInt( css(c, 'border-left-width') );
            y += parseInt( css(c, 'border-top-width') );
        }
        c = c.offsetParent;
    }
    if( gecko() ||   webkit() ) {
        x -= parseInt( css(n, 'border-left-width') );
        y -= parseInt( css(n, 'border-top-width') );
    }
    return {x: x, y: y};
}


function getXmlHttp(){

    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {}
    }
    if( !xmlhttp &&   typeof XMLHttpRequest != 'undefined' ) {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}


function onClick() {

    var xmlhttp = getXmlHttp()
    xmlhttp.open('POST', 'mail.php', true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange = function() {
        if( xmlhttp.readyState == 4 ) {
            var text = document.getElementById('msg'),
                span = text.parentNode;
                div =   $('div', span).length   ? $('div', span)[0]
                                                : document.createElement('div');
            if( xmlhttp.status == 200 ) {
                if( xmlhttp.responseText == 'ok' ) {
                    div.innerHTML = 'You\'re successfully joined our mailing list.';
                    div.className = 'ok';
                } else {
                    div.innerHTML = 'There were errors during sending message.';
                    div.className = 'error';
                }
            } else {
                div.innerHTML = 'There were errors during sending message.';
                div.className = 'error';
            }
            var right = $('body').width() - (offsetXY(span).x + span.offsetWidth),
                top = $(span).offset().top + span.offsetHeight;
            div.style.right = right+'px';
            div.style.top = top+'px';
            div.style.display = 'block';
            span.appendChild( div );
            setTimeout(function() {
                $(div).fadeOut(1000);
            }, 1000);
        }
    };
    var text = document.getElementById('msg'),
        body = 'msg='+encodeURIComponent( text.value );
    xmlhttp.send( body );
}
