var wop;
var pops;
var container;

function init() {
    pops = new Array();
    
    if (undefined != window.opener) {
        wop = window.opener;
    } else if (undefined != window.parent){
        wop = window.parent;
    }

    loadSection(1);
}

function errorMessage(msg) {
    alert(msg);
}

function bindEvent(el, eventName, eventHandler) {
    if (el.addEventListener) {
        el.addEventListener(eventName, eventHandler, false); 
    } else if (el.attachEvent) {
        el.attachEvent('on' + eventName, eventHandler);
    }
}

function getSourceOfEvent(ev) {
    var retval = false;
    if (ev.srcElement) retval = ev.srcElement;
    else if (ev.target) retval = ev.target;
    
    try {
        if ((retval.nodeType) && (retval.nodeType==3)) retval = retval.parentNode;
    } catch(err) {
        // do nothing
    }
    return retval;
}

function openNew(url) {
    pops.push(url);
    var pid = pops.length;
    var wid = 'w' + pid;
    if (window != wop) wop.openNew(url);
    else wop.open(url,wid,'');
}

function openPopupDiv(pdivtxt) {
    var pdiv = document.createElement('div');
    pdiv.id = 'popupdiv';
    pdiv.setAttribute('class','abs popupdiv');
    pdiv.setAttribute('className','abs popupdiv');

    var pdiv1 = document.createElement('div');
    pdiv1.setAttribute('class','abs op50 popupdiv1');
    pdiv1.setAttribute('className','abs op50 popupdiv1');

    var pdiv2 = document.createElement('div');
    pdiv2.setAttribute('class','abs popupdiv2');
    pdiv2.setAttribute('className','abs popupdiv2');

    var pdiv3 = document.createElement('div');
    pdiv3.setAttribute('class','abs popupdiv3');
    pdiv3.setAttribute('className','abs popupdiv3');

    var ph1 = document.createElement('h3');
    ph1.setAttribute('class','op100');
    ph1.setAttribute('className','op100');
    ph1.innerHTML = pdivtxt;

    var pdiv4 = document.createElement('div');
    pdiv4.setAttribute('class','abs popupdiv4');
    pdiv4.setAttribute('className','abs popupdiv4');

    var cbut = document.createElement('button');
    cbut.innerHTML = 'Bezár';
    if (cbut.attachEvent) cbut.attachEvent('onclick',closePopupDiv);
    else cbut.addEventListener('click',closePopupDiv,false);

    pdiv3.appendChild(ph1);
    pdiv4.appendChild(cbut);
    pdiv.appendChild(pdiv1);
    pdiv.appendChild(pdiv2);
    pdiv.appendChild(pdiv3);
    pdiv.appendChild(pdiv4);

    pdiv.style.top = '142px';
    pdiv.style.left = '242px';

    var odiv = document.getElementById('popupdiv');
    if (odiv) odiv.parentNode.replaceChild(pdiv,odiv);
    else document.body.appendChild(pdiv);

    document.getElementById('mpage').setAttribute('class','op50');
    document.getElementById('mpage').setAttribute('className','op50');
}

function closePopupDiv() {
    document.getElementById('mpage').setAttribute('class','op100');
    document.getElementById('mpage').setAttribute('className','op100');
    var pdiv = document.getElementById('popupdiv');
    if (pdiv) pdiv.parentNode.removeChild(pdiv);
}
function loadSection(id) {
    var req = new BWS_XMLRequest();
    req.setMethod('GET');
    req.setUrl('/xml/EKOPSection');
    req.setQueryString('id=' + id);
    req.setProcessResponse(parseSection);
    req.go();
}

function loadArticle(id) {
    var req = new BWS_XMLRequest();
    req.setMethod('GET');
    req.setUrl('/xml/EKOPArticle');
    req.setQueryString('id=' + id);
    req.setProcessResponse(parseArticle);
    req.go();
}

function loadArticles() {
    var req = new BWS_XMLRequest();
    req.setMethod('GET');
    req.setUrl('/xml/EKOPNews');
    req.setQueryString();
    req.setProcessResponse(parseNews);
    req.go();
}

function linkclicked(e) {
    var rxp1 = /^[tn]link_[0-9]+$/;
    var rxp2 = /^[tn]link_/;
    var esrc = getSourceOfEvent(e).id;
    if (esrc.match(rxp1)) {
        var articleid = esrc.replace(rxp2,'');
        loadArticle(articleid);
    }
}

function parseSection(xmldata) {
    container = document.getElementById('contents');
    var ekopSection = new Array();
    ekopSection['id'] = (xmldata.getElementsByTagName('section')[0].getElementsByTagName('id')[0].firstChild) ? 
        xmldata.getElementsByTagName('section')[0].getElementsByTagName('id')[0].firstChild.nodeValue : 0 ;
    ekopSection['title'] = (xmldata.getElementsByTagName('section')[0].getElementsByTagName('title')[0].firstChild) ? 
        xmldata.getElementsByTagName('section')[0].getElementsByTagName('title')[0].firstChild.nodeValue : NULL ;
    ekopSection['body'] = (xmldata.getElementsByTagName('section')[0].getElementsByTagName('body')[0].firstChild) ? 
        xmldata.getElementsByTagName('section')[0].getElementsByTagName('body')[0].firstChild.nodeValue : NULL ;
    
    var tit = document.createElement('h1');
    tit.innerHTML = ekopSection['title'];

    var bod = document.createElement('div');
    bod.setAttribute('class','contentbody');
    bod.setAttribute('className','contentbody');
    bod.innerHTML = ekopSection['body'];

    var newcont = document.createElement('div');
    newcont.id = 'contents';

    var sep2 = document.createElement('p');
    sep2.innerHTML = '&nbsp;';
    
    newcont.appendChild(tit);
    newcont.appendChild(bod);
    newcont.appendChild(sep2);

    if (container) container.parentNode.replaceChild(newcont,container);
}

function parseNews(xmldata) {
    container = document.getElementById('contents');
    var newcont = document.createElement('div');
    newcont.id = 'contents';
    var articles = new Array();

    var dbnews = xmldata.getElementsByTagName('articles')[0].getElementsByTagName('article');
    if (dbnews) {
        for (var i=0; i<dbnews.length; i++) {
            var article = new Array();
            article['id'] = (dbnews[i].getElementsByTagName('id')[0].firstChild)
                ? dbnews[i].getElementsByTagName('id')[0].firstChild.nodeValue : 0;
            article['title'] = (dbnews[i].getElementsByTagName('title')[0].firstChild)
                ? dbnews[i].getElementsByTagName('title')[0].firstChild.nodeValue : NULL;
            article['lead'] = (dbnews[i].getElementsByTagName('lead')[0].firstChild)
                ? dbnews[i].getElementsByTagName('lead')[0].firstChild.nodeValue : NULL;
            article['validfrom'] = (dbnews[i].getElementsByTagName('validfrom')[0].firstChild)
                ? dbnews[i].getElementsByTagName('validfrom')[0].firstChild.nodeValue : NULL;
            articles.push(article);
        }

        for (var j=0; j<articles.length; j++) {
            var ncont = document.createElement('div');
            ncont.setAttribute('class','articles');
            ncont.setAttribute('className','articles');

            var tit = document.createElement('h2');
            var atit = document.createElement('a');
            atit.id = 'tlink_' + articles[j]['id'];
            bindEvent(atit,'click',linkclicked);
            atit.innerHTML = articles[j]['title'];
            tit.appendChild(atit);

            var vfr = document.createElement('div');
            vfr.setAttribute('class','tinygrey margbot10');
            vfr.setAttribute('className','tinygrey margbot10');
            vfr.innerHTML = articles[j]['validfrom'];

            var lea = document.createElement('div');
            lea.setAttribute('class','lead1');
            lea.setAttribute('className','lead1');
            lea.innerHTML = articles[j]['lead'];

            var ngo = document.createElement('a');
            ngo.id = 'nlink_' + articles[j]['id'];
            ngo.setAttribute('class','blk margbot20');
            ngo.setAttribute('className','blk margbot20');
            bindEvent(ngo,'click',linkclicked);
            ngo.innerHTML = 'Tovább »';

            var sep2 = document.createElement('p');
            sep2.innerHTML = '&nbsp;';
    
            ncont.appendChild(tit);
            ncont.appendChild(vfr);
            ncont.appendChild(lea);
            ncont.appendChild(ngo);
            ncont.appendChild(sep2);
            newcont.appendChild(ncont);
        }

        if (container) container.parentNode.replaceChild(newcont,container);
    } else {
        
    }
}

function parseArticle(xmldata) {
    container = document.getElementById('contents');
    xmlarticle = xmldata.getElementsByTagName('article')[0];

    if (xmlarticle) {
        try {
            var article = new Array();
            article['id'] = (xmlarticle.getElementsByTagName('id')[0].firstChild)
                ? xmlarticle.getElementsByTagName('id')[0].firstChild.nodeValue : 0;
            article['title'] = (xmlarticle.getElementsByTagName('title')[0].firstChild)
                ? xmlarticle.getElementsByTagName('title')[0].firstChild.nodeValue : NULL;
            article['lead'] = (xmlarticle.getElementsByTagName('lead')[0].firstChild)
                ? xmlarticle.getElementsByTagName('lead')[0].firstChild.nodeValue : NULL;
            article['body'] = (xmlarticle.getElementsByTagName('body')[0].firstChild)
                ? xmlarticle.getElementsByTagName('body')[0].firstChild.nodeValue : NULL;
            article['validfrom'] = (xmlarticle.getElementsByTagName('valid_from')[0].firstChild)
                ? xmlarticle.getElementsByTagName('valid_from')[0].firstChild.nodeValue : NULL;
            
            var tit = document.createElement('h1');
            tit.innerHTML = article['title'];
    
            var vfr = document.createElement('div');
            vfr.setAttribute('class','tinygrey');
            vfr.setAttribute('className','tinygrey');
            vfr.innerHTML = article['validfrom'];
    
            var lea = document.createElement('div');
            lea.setAttribute('class','lead2');
            lea.setAttribute('className','lead2');
            lea.innerHTML = article['lead'];
    
            var bdy = document.createElement('div');
            bdy.setAttribute('class','body');
            bdy.setAttribute('className','body');
            bdy.innerHTML = article['body'];
    
            var sep = document.createElement('p');
            sep.innerHTML = '&nbsp;';
    
            var bck = document.createElement('a');
            bck.setAttribute('class','margbot10');
            bck.setAttribute('className','margbot10');
            bck.innerHTML = '« Vissza a hírekhez';
            bindEvent(bck,'click',loadArticles);
    
            var sep2 = document.createElement('p');
            sep2.innerHTML = '&nbsp;';
    
            var newcont = document.createElement('div');
            newcont.id = 'contents';
    
            newcont.appendChild(tit);
            newcont.appendChild(vfr);
            newcont.appendChild(lea);
            newcont.appendChild(bdy);
            newcont.appendChild(sep);
            newcont.appendChild(bck);
            newcont.appendChild(sep2);
    
            if (container) container.parentNode.replaceChild(newcont,container);

        } catch(e) {
            errorMessage('Hiba: ' + e);
        }
    } else {
        errorMessage('Ismeretlen hiba.');
    }
}

function loadFeedback() {
    container = document.getElementById('contents');
    
    var newcont = document.createElement('div');
    newcont.id = 'contents';

    var tit = document.createElement('h1');
    tit.innerHTML = 'Visszajelzés a projektgazdának';

    var div0 = document.createElement('div');
    div0.setAttribute('class','margbot20');
    div0.setAttribute('className','margbot20');
    div0.innerHTML = '';

    var div1 = document.createElement('div');
    div1.setAttribute('class','bld');
    div1.setAttribute('className','bld');
    div1.innerHTML = 'Az Ön neve:';

    var div2 = document.createElement('div');
    var inp1 = document.createElement('input');
    inp1.setAttribute('type','text margbot10');
    inp1.setAttribute('class','textinput margbot10');
    inp1.setAttribute('className','textinput');
    inp1.style.width = '300px';
    inp1.id = 'fb_name';
    div2.appendChild(inp1);

    var div3 = document.createElement('div');
    div3.setAttribute('class','bld');
    div3.setAttribute('className','bld');
    div3.innerHTML = 'Az Ön e-mail címe:';

    var div4 = document.createElement('div');
    var inp2 = document.createElement('input');
    inp2.setAttribute('type','text margbot10');
    inp2.setAttribute('class','textinput margbot10');
    inp2.setAttribute('className','textinput');
    inp2.style.width = '300px';
    inp2.id = 'fb_email';
    div4.appendChild(inp2);

    var div5 = document.createElement('div');
    div5.setAttribute('class','bld');
    div5.setAttribute('className','bld');
    div5.style.width = '400px';

    var div51 = document.createElement('div');
    div51.setAttribute('class','bld');
    div51.setAttribute('className','bld');
    div51.style.float = 'left';
    div51.style.position = 'relative';
    div51.style.top = '8px';
    div51.innerHTML = 'Üzenet a projektgazdának (max 1000 karakter):';

    var div52 = document.createElement('div');
    div52.setAttribute('class','counter1');
    div52.setAttribute('className','counter1');
    div52.id = 'fb_counter';
    div52.innerHTML = '1000';

    div5.appendChild(div52);
    div5.appendChild(div51);

    var div6 = document.createElement('div');
    var txa = document.createElement('textarea');
    txa.setAttribute('class','textinput margbot20');
    txa.setAttribute('className','textinput margbot20');
    txa.style.width = '400px';
    txa.style.height = '150px';
    txa.id = 'fb_message';
    bindEvent(txa,'keyup',countletters);
    div6.appendChild(txa);

    var div7 = document.createElement('div');
    var smt = document.createElement('button');
    smt.setAttribute('class','button1');
    smt.setAttribute('className','button1');
    bindEvent(smt,'click',sendMessage);
    smt.innerHTML = 'Elküld';
    div7.appendChild(smt);

    newcont.appendChild(tit);
    newcont.appendChild(div0);
    newcont.appendChild(div1);
    newcont.appendChild(div2);
    newcont.appendChild(div3);
    newcont.appendChild(div4);
    newcont.appendChild(div5);
    newcont.appendChild(div6);
    newcont.appendChild(div7);

    if (container) container.parentNode.replaceChild(newcont,container);
}

function countletters() {
    var cnt = document.getElementById('fb_counter');
    var txt1 = document.getElementById('fb_message').value;
    var ln = (1000 - txt1.length);
    if (ln >= 0) {
        cnt.setAttribute('class','counter1');
        cnt.setAttribute('className','counter1');
    } else {
        cnt.setAttribute('class','counter2');
        cnt.setAttribute('className','counter2');
    }
    cnt.innerHTML = ln;
}

function sendMessage() {
    var errors = new Array();
    var msg = new Array();
    var emailrxp = /^[A-Za-z0-9_\-]{1,64}(\.[A-Za-z0-9_\-]{1,64})*@[A-Za-z0-9_\-]{1,64}(\.[A-Za-z0-9_\-]{1,64})*\.[a-zA-Z]{2,4}$/;

    document.getElementById('fb_name').style.borderColor = 'rgb(31,26,23)';
    document.getElementById('fb_email').style.borderColor = 'rgb(31,26,23)';
    document.getElementById('fb_message').style.borderColor = 'rgb(31,26,23)';

    msg['name'] = document.getElementById('fb_name').value;
    msg['email'] = document.getElementById('fb_email').value;
    msg['message'] = document.getElementById('fb_message').value;

    if (msg['name'].replace(/\ */,'').length < 1) {
        document.getElementById('fb_name').style.borderColor = 'rgb(200,0,0)';
        errors.push('A kitöltő nevének megadása kötelező');
    }

    if (msg['message'].replace(/\ */,'').length < 1) {
        document.getElementById('fb_message').style.borderColor = 'rgb(200,0,0)';
        errors.push('Az üzenet kitöltése kötelező');
    }

    if (msg['message'].length > 1000) {
        document.getElementById('fb_message').style.borderColor = 'rgb(200,0,0)';
        errors.push('Az üzenet legfeljebb 1000 karakter hosszú lehet');
    }

    if ((msg['email'] != '') && !(msg['email'].match(emailrxp))) {
        document.getElementById('fb_email').style.borderColor = 'rgb(200,0,0)';
        errors.push('A megadott e-mail cím nem szabályos e-mail cím');
    }

    if (errors.length > 0) {
        var errmsg = '';
        for (var i=0; i<errors.length; i++) errmsg += errors[i] + '\n';
        errorMessage(errmsg);
    } else {
        var qs = 'name=' + encodeURIComponent(msg['name'])
               + '&email=' + encodeURIComponent(msg['email'])
               + '&message=' + encodeURIComponent(msg['message']);
        var req = new BWS_XMLRequest();
        req.setMethod('POST');
        req.setContentType('application/x-www-form-urlencoded');
        req.setUrl('/xml/EKOPFeedback');
        req.setQueryString(qs);
        req.setProcessResponse(parseFeedback);
        req.go();
    }
}

function parseFeedback(xmldata) {
    // openPopupDiv('Na akkor...');
    try {
        var res = xmldata.getElementsByTagName('result')[0];
        var rescode = res.getElementsByTagName('resultcode')[0].firstChild.nodeValue;
        var resmsg = res.getElementsByTagName('resultmsg')[0].firstChild.nodeValue;

        if (parseInt(rescode) == 1) {
            setTimeout("loadSection(1);",1000);
            openPopupDiv(resmsg);
        } else {
            errorMessage(resmsg);
        }
    } catch(e) {
        errorMessage(e);
    }
}

