//the javascript to support:
// - display the complete review when the content is too long
// - AJAX voting call
// - AJAX reporting abuse call
// - show/hiding reporting popover

//display the complete review.
function readMore(reviewDisplay, divDecider){
    if (reviewDisplay == 1){
        document.getElementById("readmore."+divDecider).style.display = "block";
        document.getElementById("seeless."+divDecider).style.display = "none";
    }
    else {
        document.getElementById("readmore."+divDecider).style.display = "none";
        document.getElementById("seeless."+divDecider).style.display = "block";
    }
}

//hide report button
function hideReport(divDecider){
    document.getElementById("reportButton."+divDecider).style.display = "none";
}

//report abuse AJAX call
function reportButtons(e, url) {

    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    httpRequest.onreadystatechange=function() {
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200)  {
                var reqText= httpRequest.responseText;
                var votepattern = /reportsuccess/;
                if (votepattern.test(reqText)) {
                    newDiv= document.createElement("div");
                    newDiv.className = "reportConfirm";
                    text = document.createTextNode("Thank you. Your report has been sent to Merchant.");
                    newDiv.appendChild(text);
                    while (e.className != "popparent" && e != e.parentNode) {
                        e = e.parentNode;
                    }
                    e.appendChild(newDiv);
                }
                else {
                    newDiv= document.createElement("div");
                    newDiv.className = "reportConfirm";
                    text = document.createTextNode("Your report is not complete. Please try again later.");
                    newDiv.appendChild(text);
                    while (e.className != "popparent" && e != e.parentNode) {
                        e = e.parentNode;
                    }
                    e.appendChild(newDiv);
                }
            }
        } else {
            //do something else
        }
    }
    
    httpRequest.open("POST", url, true );
    httpRequest.send("");
}

//voting AJAX call
function yesNoButtons(e, url) {

    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var parent;
    while (e.className != "yesNoButtonsContainer" ) e  = e.parentNode;
    parent = e.parentNode;

    var hasVotedOnThisItem = false;
    for( var childNodeIndex = 0; parent.childNodes[childNodeIndex]; childNodeIndex++ ) {
      var child = parent.childNodes[childNodeIndex];
      if (child.className == "voteConfirm") {
	hasVotedOnThisItem = true;
      }
    }

    httpRequest.onreadystatechange=function() {
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200 && !hasVotedOnThisItem )  {
                var reqText= httpRequest.responseText;
                var votepattern = /votesuccess/;
                if (votepattern.test(reqText)) {
                    newDiv= document.createElement("div");
                    newDiv.className = "voteConfirm";
                    text = document.createTextNode("Thank you for the valuable feedback you provided to our other readers and reviewers. Your vote will be counted and will appear on the product page within 24 hours.");
                    newDiv.appendChild(text);
                    parent.appendChild(newDiv);
                }
                else {
                    newDiv= document.createElement("div");
                    newDiv.className = "voteConfirm";
                    text = document.createTextNode("Your vote is not complete. Please try again later.");
                    newDiv.appendChild(text);
                    parent.appendChild(newDiv);
                }
            }
        } else {
            //do something else
        }
    } 

    httpRequest.open("POST", url, true );
    httpRequest.send("");
}

//display popover
function showFlyOver(e)
{
    var reportLink= e;
                                                                                                                                                             
    // Walk up until we find popparent
    while (e.className != "popparent" && e != e.parentNode) {
        e = e.parentNode;
    }
                                                                                                                                                             
    // Walk the children until we find popover
    e = e.firstChild;
    while (e.className != "popover") {
        e = e.nextSibling;
    }
    e.style.display = "block";
    e.reportLink = reportLink;
}

//hide popover                                                                                                                                   
function hideFlyOver(e)
{
    while (e.className != "popover") e = e.parentNode;
    e.style.display = "none";
}
