
function    external_links ()
{
    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName ("a");

    for (var i = 0; i<anchors.length; i++)
    {
        var anchor = anchors[i];

        if (anchor.getAttribute ("href") && (anchor.getAttribute ("rel") == "external"))
        {
            anchor.target = "_blank";
            anchor.title = "új ablakban nyílik meg";
        }
    }
}

function    nospam_emails ()
{
    var replaces = 0;

    while (document.body.innerHTML.indexOf ('##' + 'kukac##') != -1 && replaces < 1000)
    {
        document.body.innerHTML = document.body.innerHTML.replace ('##' + 'kukac##', String.fromCharCode (64));
        replaces++;
    }
}

window.onload = function  ()
{
    external_links ();
    nospam_emails ();
}

//---------------------------------------------------------
// popups

function create_popup (target, page, _do, params, width, height)
{
    var top = (screen.height / 2) - (height / 2);
    var left = (screen.width / 2) - (width / 2);

    var mywin = window.open
    (
        'popup.php?page=' + page + '&do=' + _do + '&' + params,
        '_' + target,
        'directories=no, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, width=' + width +', height=' + height + ', left=' + left + ', top=' +top
    );

    //why this doesn't work for me, on FF???
    //meant to change to the window, if target already open...
    mywin.focus();
}


//---------------------------------------------------------
// ajax: K.I.S.S style :D

function create_request_object ()
{
    var ro;

    if (window.XMLHttpRequest)
    {
        ro = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        ro = new ActiveXObject('Microsoft.XMLHTTP');
    }

    return ro;
}

function send_req (mode, div, page, _do, params, sendparams)
{
    var http = create_request_object ();

    http.open (mode, 'ajax.php?page=' + page + '&do=' + _do + '&' + params, true);

    http.onreadystatechange = function () { handle_response (http, div); };

    if (mode == 'POST')
    {
        http.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader ("Content-length", sendparams.length);
        http.setRequestHeader ("Connection", "close");

        http.send (sendparams);
    }
    else
    {
        http.send (null);
    }
}

function handle_response (http, div)
{
    if (http.readyState == 4 && http.status == 200)
    {
        element = document.getElementById(div);
        
        if (element)
        {
            element.innerHTML = http.responseText;
        }

        delete http;
    }
}

function send_form (div, page, _do, params, formid)
{
    form_object = document.getElementById (formid);

    var paramstr = '';

    for (var i = 0; i < form_object.elements.length; i++)
    {

        if (form_object.elements[i].type == 'radio' && !form_object.elements[i].checked)
            continue;

        if (paramstr)
            paramstr += '&';

        paramstr += form_object.elements[i].name + '=' + encodeURI(escape(form_object.elements[i].value));
    }

    send_req ('POST', div, page, _do, params, paramstr);
};

