﻿var $j = jQuery.noConflict();

function CreateXMLHttpRequest()
{
    http_request = false;
    if (window.XMLHttpRequest) 
    { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) 
        {
            // set type accordingly to anticipated content type
            // http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
        }
    } 
    else if (window.ActiveXObject) 
    { // IE
         try 
         {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } 
         catch (e) 
         {
            try 
            {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) 
            {
               alert("not good at all..");
            }
         }
     }
     if (!http_request) 
     {
        alert('Your Browser Do Not Support AJAX Request !!!');
        return false;
     }
     else
     {
        return http_request;
     }  
}

function voteonstep(stepID, isLiked, steptype)
{     
    var method = 'VoteOnStep';
    data = "stepID="+stepID+"&isLiked="+isLiked+"&steptype=" + steptype;    
            
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;
                document.getElementById('divStepDiffrence' + stepID).innerHTML = result; 
            
                document.getElementById('divStepDisAgree' + stepID).innerHTML =  "<img src='../../images/article/disagree-disabled.jpg' width='16' height='16' alt='' />";            
                document.getElementById('divStepAgree' + stepID).innerHTML =  "<img src='../../images/article/agree-disabled.jpg' width='16' height='16' alt='' />";
            
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);
}

function postvote(articleid, votevalue)
{ 
  
    var ipaddress = GetIPAddress();
    
    var method = 'PostVote';
    data = "articleID="+articleid+"&memberID="+0+"&voteValue="+votevalue+"&IPAddress="+ipaddress;
            
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;
                var votes = result.split(","); 
                var forvotes = votes[0]
                var againstvotes = votes[1];          
                           
                document.getElementById('divLikeIt').innerHTML =  "Like it";
                document.getElementById('divDisLikeIt').innerHTML =  "Dislike it";
                document.getElementById('divLikeItCount').innerHTML =  forvotes;
                document.getElementById('divDisLikeItCount').innerHTML =  againstvotes;
                document.getElementById('LikeDislike').style.backgroundImage = "url(../../images/article/likedislike-disabled.jpg)";
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);
}

function showreplies(commentid)
{ 
    var method = 'GetComments';
    data = "commentID="+commentid;
            
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;
                result = result.replace(/\.\.\//g, "../../");
                
                document.getElementById("divreply" + commentid).innerHTML =  result;                     
                document.getElementById('divcomment' + commentid).style.display =  'none';
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);
}

function reply(articleid, commentid, newlevel)
{ 
    var method = 'GetReplyToSection';
    data = "articleID=" + articleid + "&commentID="+commentid+"&newLevel="+newlevel;
            
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;
                document.getElementById("divreply" + commentid).innerHTML =  result;      
                document.getElementById('divcomment' + commentid).style.display =  'none';
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);
}

function hidereply(commentid)
{
    if(document.getElementById('divreply' + commentid) != null)
    {
        document.getElementById('divreply' + commentid).innerHTML = "";
        document.getElementById('divcomment' + commentid).style.display =  'block';
    }    
}

function postComment(btnName)
{  
    try
    {
        if(btnName == "btnSubmitComments")
        {
            document.getElementById('commenterror').innerHTML  = "";
            document.getElementById('loaderimage').style.display =  'block';
                       
            var commentID = 0;  
            var articleID = document.getElementById(btnName).getAttribute("aid");    
            var level = 1;    
                       
            var name = document.getElementById('inputName').value;
            var email = document.getElementById("inputEmail").value;
            var website = document.getElementById("inputWebsite").value;
            var text = document.getElementById("inputText").value;
            var replyto =  commentID;
            var memberID = 1;  
            var ipaddress = GetIPAddress();  
            var response = dataIsValid(name, email, text);
            
            if(response == true)
            {
                
                var responseContainer = 'divcomment' + commentID;
                response = postCommentInitialize(articleID, name, email, website, text, memberID, replyto, level, 'divcomment'+commentID, commentID, ipaddress);                           
            }
            else
            {                   
                document.getElementById('commenterror').innerHTML = "<div class='loading-image'>" + response + "</div>";
                document.getElementById('commenterror').style.display =  'block'; 
                document.getElementById('loaderimage').style.display =  'none';
            }
        }
        else
        {
            if(document.getElementById('responseSpan') != null)
            {
               document.getElementById('responseSpan').innerHTML  = "";
            }
            
            
            var commentID = document.getElementById(btnName).getAttribute("comid");  
            var articleID = document.getElementById(btnName).getAttribute("aid");    
            var level = document.getElementById(btnName).getAttribute("level");    
             
            
            var name = document.getElementById('txtName' + commentID).value;
            var email = document.getElementById("txtEmail" + commentID).value;
            var website = document.getElementById("txtWebsite" + commentID).value;
            var text = document.getElementById("commentText" + commentID).value;
            var replyto =  commentID;
            var memberID = 1;  
            var ipaddress = GetIPAddress();  
            var response = dataIsValid(name, email, text);
            
            if(response == true)
            {
                var responseContainer = 'divcomment' + commentID;
                response = postCommentInitialize(articleID, name, email, website, text, memberID, replyto, level, 'divcomment'+commentID, commentID, ipaddress);                           
            }
            else
            {                   
                document.getElementById('responseSpan').innerHTML = "<div class = 'errorBox'>" + response + "</div>"; 
            }
        }
    }
    catch(err)
    {
        alert(err);
    }
}



function dataIsValid(name, email, commentsText)
{
    var isValid = true;
    var msg = "";
    if(name == "Name" || name == "")
    {
         msg = "Name is Required";
         isValid = false;        
    }       
    else if(email == "Email" || email == "")
    {
         msg = "Email is Required";
         isValid = false;
    }
       
    else if(commentsText == "Comments" || commentsText == "")
    {
        msg = "Comments are Required";
         isValid = false;
    }  
    
    if(isValid)
    {
        return true;
    }   
    else
    {
        return msg;
    } 
}


function postCommentInitialize(articleID, name, email, website, text, memberID, replyto, level, responseContainer, commentID, ipaddress)
{  
    var returnmsg = ""; 
    var method = 'RepliedToComment';
    if(level == 1)
    {
        method = 'RepliedToArticle';
    }    
    text = text.replace(/&/g, ' and ');
    text = text.replace(/'/g, ' ');
    data = "articleID="+articleID+"&replyTo=" + replyto + "&name="+name+"&website="+website+"&email="+email+"&message="+text+"&level="+level+"&commentID=" + commentID + "&IPAddress=" + ipaddress + "&memberID=" + memberID;
            
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {
        if (http_request.readyState == 4) 
        {            
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;
                result = result.replace(/\.\.\//g, "../../");
                if(replyto == "0")
                {
                    var commentsData = document.getElementById('commentsList').innerHTML;
                    document.getElementById('commentsList').innerHTML = commentsData + result;
                    SetCommentsCountBox(articleID);
                    document.getElementById('commenterror').innerHTML = "<div class='loading-image'>Comments Posted</div>";
                    document.getElementById('commenterror').style.display =  'block'; 
                    document.getElementById('loaderimage').style.display =  'none';
                }
                else
                {
                    var pre = "<div class='replynreplyto'><a href='javascript:hidereply(\"" + commentID + "\");'>Hide</a></div>";
                    var po = "<div class = 'clear'></div><div class = 'errorBox'>Comments Posted</div><div class = 'clear'></div>";
                    document.getElementById("divreply" + commentID).innerHTML = pre + result + po; 
                    document.getElementById('divcomment' + commentID).style.display =  'none';
                    SetCommentsCountBox(articleID);
                }
                
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);     
}

function SetCommentsCountBox(articleID)
{
    var method = 'GetCommentsCount';
    data = "articleID=" + articleID;      
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {        
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;                
                document.getElementById("CommentCountText").innerHTML =  "<a href='#viewcomments'>" + result + " comments</a>";                
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);
}

function RateArticle(btnName)
{
    var articleID = document.getElementById(btnName).getAttribute("aid");
    var ipaddress = GetIPAddress();
    var ratingValue = 5;    
    var method = 'RateArticle';
    data = "articleID=" + articleID + "&IPAddress=" + ipaddress + "&ratingValue=" + ratingValue;
     
     http_request = CreateXMLHttpRequest();          
     url = "/webservice/elistmania.php?method=" + method;
     http_request.onreadystatechange = function()
     {        
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200) 
            {
                var result = http_request.responseText;
                var votes = result.split(","); 
                var isAlreadySubmitted = votes[0];
                var rating = votes[1];
                var numVotes = votes[2];
                if(isAlreadySubmitted == 0)
                {
                    var ratingWidth= rating * 20;
                    document.getElementById("currentrating").innerHTML = "<li class = 'current-rating' style = 'width:" + ratingWidth + "%'/>";
                    
                    document.getElementById("spanVoteCount").innerHTML = "(out of " + numVotes + " votes)";
                    document.getElementById("spanRatingCount").innerHTML = Math.round(10 * rating)/10 + "/5.0";
                  
                     
                    document.getElementById("LiteralRatingSubmissionStatus").innerHTML = "<div style='font-size:small; color:#FE0000; padding-left:20px; padding-bottom:5px;'>Your vote has been successfully submitted.</div>";
                } 
                else                
                {
                    document.getElementById("LiteralRatingSubmissionStatus").innerHTML = "<div style='font-size:small; color:#FE0000; padding-left:20px; padding-bottom:5px;'>Your vote has already been submitted.</div>";
                }              
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
     };
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.setRequestHeader("Content-length", data.length);
	 http_request.setRequestHeader("Connection", "close");
	 http_request.send(data);
}