
var xmlHttp
var json


function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp = new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}



function CheckHttpObj()
{
    if (xmlHttp == null)
    {
      alert ('Vas browser ne podrzava AJAX!');
      return;
    }
}




function getVoteScore(id)
{     
    xmlHttp = GetXmlHttpObject();
    CheckHttpObj();
    
    crossProcedureDataPass = id;
    
    var url = "ajax/getVoteScore.aspx";
    var params= "id=" + id;
    params=params + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = getVoteScoreStateChanged;
    xmlHttp.open("POST", url, true);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.send(params);
}

function getVoteScoreStateChanged() 
{
    if (xmlHttp.readyState == 4)
    {        
        // ako je status 200 znaci da je sve ok sto se tice servera
        if(xmlHttp.status == 200)
		{
//            hideLoadingWindow();
        
        
            txt = xmlHttp.responseText;

            document.getElementById("ratingScore").innerHTML = txt;
                        
            startCount();
            comments(crossProcedureDataPass);
        }
    }
    else
    {
        // prikazi neku poruku da se nesto desava na serveru
//        showLoadingWindow('Loading');
    }
}




var crossProcedureDataPass;
function vote(voteId, id, pg)
{     
    xmlHttp = GetXmlHttpObject();
    CheckHttpObj();
    
    pageTracker._trackEvent('Voting', '" + pg + "' + voteId);
    
    crossProcedureDataPass = id;
    
    var url = "ajax/vote.aspx";
    var params= "voteId=" + voteId;
    params=params + "&id=" + id;
    params=params + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = voteStateChanged;
    xmlHttp.open("POST", url, true);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.send(params);
}

function voteStateChanged() 
{
    if (xmlHttp.readyState == 4)
    {        
        // ako je status 200 znaci da je sve ok sto se tice servera
        if(xmlHttp.status == 200)
		{
            hideLoadingWindow();        
        
            document.getElementById("votingMessage").innerHTML = "Hvala sto ste glasali!";
            getVoteScore(crossProcedureDataPass); 
        }
    }
    else
    {
        // prikazi neku poruku da se nesto desava na serveru
        showLoadingWindow('Submitting');
    }
}





function comments(id)
{     
    xmlHttp = GetXmlHttpObject();
    CheckHttpObj();
    
    crossProcedureDataPass = id;
    
    var url = "ajax/comments.aspx";
    var params= "id=" + id;
    params=params + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = commentsStateChanged;
    xmlHttp.open("POST", url, true);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.send(params);
}

function commentsStateChanged() 
{
    if (xmlHttp.readyState == 4)
    {        
        // ako je status 200 znaci da je sve ok sto se tice servera
        if(xmlHttp.status == 200)
		{
//            hideLoadingWindow();
            document.getElementById("ajaxComments").innerHTML = "";
        
            txt = xmlHttp.responseText;
        
            document.getElementById("ajaxComments").innerHTML = txt;
        }
    }
    else
    {
        // prikazi neku poruku da se nesto desava na serveru
//        showLoadingWindow('Loading');
            document.getElementById("ajaxComments").innerHTML = "<span style='text-align: center;'><img src='../../images/loader.gif' alt='Loader' title='Loader' /></span>";
    }
}




function deleteComment(id)
{
    if (confirm("Da li ste sigurni?") == true)
    {
        xmlHttp = GetXmlHttpObject();
        CheckHttpObj();
        
        var url = "ajax/delete.aspx";
        var params= "id=" + id;
        params=params + "&sid=" + Math.random();
        xmlHttp.onreadystatechange = deleteCommentChanged;
        xmlHttp.open("POST", url, true);
        
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        
        xmlHttp.send(params);
    }
}

function deleteCommentChanged() 
{
    if (xmlHttp.readyState == 4)
    {        
        // ako je status 200 znaci da je sve ok sto se tice servera
        if(xmlHttp.status == 200)
		{
            hideLoadingWindow();
            
            if (xmlHttp.responseText != "")
                alert(xmlHttp.responseText);
        
            comments(crossProcedureDataPass); 
        }
    }
    else
    {
        // prikazi neku poruku da se nesto desava na serveru
        showLoadingWindow('Deleting');
    }
}





function commentAdd(id)
{
    var uname = document.getElementById("txtUsername").value;
    var website = document.getElementById("txtWebsite").value;
    var description = document.getElementById("txtDescription").value;
    
    if (uname != "" && website != "" && description != "")
    {
        xmlHttp = GetXmlHttpObject();
        CheckHttpObj();
        
        crossProcedureDataPass = id;
        
        var url = "ajax/commentAdd.aspx";
        var params= "username=" + uname;
        params=params + "&id=" + id;
        params=params + "&website=" + website;
        params=params + "&description=" + description;
    //    params=params + "&ip=" + id;
        params=params + "&sid=" + Math.random();
        xmlHttp.onreadystatechange = commentAddStateChanged;
        xmlHttp.open("POST", url, true);
        
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        
        xmlHttp.send(params);
    }
    else
        alert("Sva tri polja su obavezna!");
}

function commentAddStateChanged() 
{
    if (xmlHttp.readyState == 4)
    {        
        // ako je status 200 znaci da je sve ok sto se tice servera
        if(xmlHttp.status == 200)
		{
            hideLoadingWindow();
            
            document.getElementById("txtUsername").value = "";
            document.getElementById("txtWebsite").value = "http://";
            document.getElementById("txtDescription").value = "";
            
            comments(crossProcedureDataPass); 
        }
    }
    else
    {
        // prikazi neku poruku da se nesto desava na serveru
        showLoadingWindow('Adding');
    }
}