﻿// JScript File
var originalContents = new Object;
var hiddenComments = new Object;
var path = appRoot;

function submitComment(comment, userID, location, item, commentBox)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      alert("Your browser does not support AJAX comments.");
      
    //do it
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4)
        {
            document.getElementById(commentBox).innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET",""+appRoot+"gcr/ajax/submitComment.aspx?comment="+comment+"&account="+userID+"&location="+location+"&item="+item+"",true);
    xmlhttp.send(null);
}

function expandTextbox(tbID, action)
{
    if(action)
    {
        document.getElementById('ajcb'+tbID).style.height="35px";
        document.getElementById('inputButtons'+tbID).style.display='block';
    }
    else
    {
        try {
            document.getElementById('ajcb'+tbID).style.height="20px";
            document.getElementById('inputButtons'+tbID).style.display='none';
            }
        catch(e) { }
    }
}


function parseXML(xmlDoc, divID, location, item, hideable, writeable, userID)
{

    
    
    //write
    
    var comments = new Array();
    var names = new Array();
    var times = new Array();
    for(i=0; i<xmlDoc.getElementsByTagName("comment").length; i++)
    {
        comments[i]=xmlDoc.getElementsByTagName("comment")[i].firstChild.data;
        names[i]=xmlDoc.getElementsByTagName("comment")[i].attributes[0].value;
        times[i]=xmlDoc.getElementsByTagName("comment")[i].attributes[1].value;
    }
    var location = xmlDoc.getElementsByTagName("gcrItemComments")[0].attributes[0].value;
    var item = xmlDoc.getElementsByTagName("gcrItemComments")[0].attributes[1].value;
    
    var commentsIH="<div style=\"height: 10px; background-image: url('"+path+"gcr/images/effects/commentArrow.png'); background-repeat: no-repeat; margin-left: 20px;\"></div>";
    if(hideable)
        commentsIH+="<div class='commentBox' style='text-align: right;'><a onclick=\"document.getElementById(\'"+divID+"\').innerHTML=originalContents[\'"+divID+"\'];\" href='javascript:void(0);'>Hide</a></div>";
    
    var collapsedIH="";
    
    if(comments.length>2)
    { //collapsed comments
        for(i=0;i<(comments.length-2);i++)
        {
            collapsedIH+="<div class='commentBox'><strong class='color'>"+names[i]+"</strong> "+comments[i]+"<br />";
            collapsedIH+="<span style='color: gray;'>"+times[i]+"</span></div>";
            
        }
        hiddenComments[divID]=collapsedIH;
        commentsIH+="<div id='moreBox"+item+"'><div class='commentBox' style='padding-top: 5px;'><img src='"+path+"gcr/images/icons/commentBubble.png' width='16' height='13' style='margin-right: 5px;' alt='' /><a onclick=\"document.getElementById('moreBox"+item+"').innerHTML=hiddenComments['"+divID+"']; return false;\" href='#'>View all "+comments.length+" comments</a></div></div>";
    }
    
    if(comments[comments.length-2]!=undefined)
    {
        commentsIH+="<div class='commentBox'><strong class='color'>"+names[names.length-2]+"</strong> "+comments[comments.length-2]+"<br />";
        commentsIH+="<span style='color: gray;'>"+times[times.length-2]+"</span></div>";
    }
    if(comments[comments.length-1]!=undefined)
    {
        commentsIH+="<div class='commentBox'><strong class='color'>"+names[names.length-1]+"</strong> "+comments[comments.length-1]+"<br />";
        commentsIH+="<span style='color: gray;'>"+times[times.length-1]+"</span></div>";
    }
    
    if(writeable)
    {
        commentsIH+="<div class='commentBox' id='comment"+ item +"' style='position: relative; height: 40px'>";
        commentsIH+="<textarea id='ajcb"+ item +"' class='ajaxCommentBox' style='border: 1px solid #A6C3DE; width: 290px; height: 20px; color: #656565; margin: 0px; position: absolute; left: 5px; top: 5px;' onfocus=\"this.value=''; expandTextbox('"+ item +"', true);\" onblur=\"this.value='Leave a comment ...'; expandTextbox('"+ item +"', false);\" onkeypress=\"if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {submitComment(document.getElementById('ajcb"+ item +"').value, "+ userID +", "+ location +", "+ item +", 'comment"+ item +"'); return false;}}\";>Leave a comment ...</textarea>";
        commentsIH+="<div style='text-align: right; margin-top: 5px; display: none;' id='inputButtons"+ item +"'>";
        commentsIH+="</div></div>";
    }
    
    document.getElementById(divID).innerHTML=commentsIH;
    //end writing
}

function loadXML(location, item, divID, hideable, writeable, userID)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      
    //do it
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4)
        {
            
              try //Internet Explorer
              {
                  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                  xmlDoc.async="false";
                  xmlDoc.loadXML(xmlhttp.responseText);
                  parseXML(xmlDoc, divID, location, item, hideable, writeable, userID);
              }
                catch(e)
              {
                  parser=new DOMParser();
                  xmlDoc=parser.parseFromString(xmlhttp.responseText,"text/xml");
                  parseXML(xmlDoc, divID, location, item, hideable, writeable, userID);
                  
              }
        }
    }
    
    xmlhttp.open("GET",""+appRoot+"gcr/xml/itemComments.aspx?location="+location+"&item="+item+"",true);
    xmlhttp.send(null);
}

function writeComments(divID, location, item, hideable, writeable, userID)
{
    originalContents[divID] = document.getElementById(divID).innerHTML;
    document.getElementById(divID).innerHTML="Loading ...";
    loadXML(location, item, divID, hideable, writeable, userID);
    
}
