var xmlhttp=false;

if (window.ActiveXObject)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
  try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function loadFragmentInToElement(fragment_url, element_id) {
    var element = document.getElementById(element_id);
    element.innerHTML = 'Loading ...';
    xmlhttp.open("GET", fragment_url);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      element.innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.send(null);
}

function xmlMarkPost(post_id, img_id, tag_id)
{
    var img = document.getElementById(img_id);
    if(img.src.indexOf('standard') != -1)
    {
        var newsrc = '/images/makepremium.gif';
        var mark = 's';
    }
    else
    {
        var newsrc = '/images/makestandard.gif';
        var mark = 'p';
    }
    var tag = document.getElementById(tag_id);
    xmlhttp.open("POST", "/xmlmarkpost.php", true);
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var result = xmlhttp.responseText;
            if(result == 'success')
            {
                img.src = newsrc;
                tag.style.display = (newsrc == '/images/makestandard.gif') ? 'block' : 'none';
            }
            else
            {
                alert(result);
            }
        }
    }
    xmlhttp.send("post_id="+post_id+'&mark='+mark);
}

function xmlMarkTopic(topic_id, mark)
{
    if(mark == 's')
    {
        var newsrc = '/images/makepremium.gif';
    }
    else
    {
        var newsrc = '/images/makestandard.gif';
    }

    xmlhttp.open("POST", "/xmlmarktopic.php", true);
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var result = xmlhttp.responseText;
            if(result == 'success')
            {
                var images = document.getElementsByTagName('img');

                for(x=0;x<images.length;x++)
                {
                    if(images[x].id.indexOf('_img') != -1)
                    {
                        images[x].src = newsrc;
                        var img_id = images[x].id.split('_');
                        var post_id = img_id[0];
                        document.getElementById('premium_tag_'+post_id).style.display = (newsrc == '/images/makestandard.gif') ? 'block' : 'none';
                    }
                }
            }
            else
            {
                alert(result);
            }
        }
    }
    xmlhttp.send("topic_id="+topic_id+'&mark='+mark);
}

function xmlRatePost(post_id, user_id, rating)
{
    if(user_id > 0)
    {
    xmlhttp.open("POST", "./xmlratepost.php", true);
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status == 200)
        {
        var result = xmlhttp.responseText;
        var rating_grade = new Array();
        rating_grade[0]='Offtopic/Superfluous';
        rating_grade[1]='Not very helpful/informative';
        rating_grade[2]='Somewhat helpful/informative';
        rating_grade[3]='Overall helpful/informative';
        rating_grade[4]='Very helpful/informative';
        rating_grade[5]='Solved my problem!';
        if(result.indexOf('success') != -1)
        {
            var calc_rating_arr = result.split(':');
            var calc_rating = calc_rating_arr[1];
            var elem = document.getElementById('rating_'+post_id);
            if(elem.parentNode.style.display == 'none')
            {
            elem.style.width = calc_rating+'px';
            document.getElementById('no_rating_'+post_id).style.display = 'none';
            elem.parentNode.style.display = 'block';
            elem.parentNode.onmouseover=function(){return overlib('<div style=\'background-color:#CCCCFF;text-align:center;padding:2px;border:solid 1px #333399;\'>Overall rating: '+calc_rating_arr[2]+'<br />'+rating_grade[calc_rating_arr[2]]+'</d'+'iv>',LEFT,FULLHTML);}
            }
            else
                    {
            elem.style.width = calc_rating+'px';
                        elem.parentNode.onmouseover=function(){return overlib('<div style=\'background-color:#CCCCFF;text-align:center;padding:2px;border:solid 1px #333399;\'>Overall rating: '+calc_rating_arr[2]+'<br />'+rating_grade[calc_rating_arr[2]]+'</d'+'iv>',LEFT,FULLHTML);}
                    }
        }
        }
    }
    xmlhttp.send('post_id='+post_id+'&user_id='+user_id+'&rating='+rating);
    }
}

function topicResolved(topic_id, m)
{
    if(topic_id)
    {
    xmlhttp.open("POST", "./xmltopicresolved.php");
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        alert(xmlhttp.responseText);
        }
    }
    xmlhttp.send('topic_id='+topic_id+'&m='+m);
    }
}

function switchTopicResolved(topic_id)
{
    var mark_elem = document.getElementById(topic_id+'_mark');
    var mark = mark_elem.value;
    //var sid_elem = document.getElementById(topic_id+'_sid');
    //var sid = sid_elem.value;
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == 'sid') {
        var sid = pair[1];
        }
    } 

    xmlhttp.open("POST", "./xmltopicresolved.php?sid="+sid);
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var result = xmlhttp.responseText;
            if(result == 'success')
            {
                var resdiv = document.getElementById(topic_id+'_resolved');
                var opendiv = document.getElementById(topic_id+'_open');
                var workarounddiv = document.getElementById(topic_id+'_workaround');
                if(mark == 'r')
                {
                    resdiv.style.display = 'block';
                    opendiv.style.display = 'none';
                    workarounddiv.style.display = 'none';
                }
                else if(mark == 'o')
                {
                    resdiv.style.display = 'none';
                    opendiv.style.display = 'block';
                    workarounddiv.style.display = 'none';
                }
                else if(mark == 'w')
                {
                    resdiv.style.display = 'none';
                    opendiv.style.display = 'none';
                    workarounddiv.style.display = 'block';
                }
            }
            else
            {
                alert('failure: '+result+' - '+xmlstr);
            }
        }
    }
    xmlstr = 'topic_id='+topic_id+'&mark='+mark;
    xmlhttp.send(xmlstr);
}
