var NUMBER_OF_STARS = 5;

var aCall = function( url )
{
    var img = document.createElement( 'img' );
    img.src = url;
};


function init_rating()
{
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i < ratings.length; i++)
    {
        if (ratings[i].className != 'rating')
            continue;

        var rating = ratings[i].firstChild.nodeValue;
        ratings[i].removeChild(ratings[i].firstChild);
        if (rating > NUMBER_OF_STARS || rating < 0)
            continue;
        for (var j = 0; j < NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating >= 1)
            {
                star.setAttribute('src', '/images/stars/rating_on.gif');
                star.className = 'on';
                rating--;
            }
            else if(rating == 0.5)
            {
                star.setAttribute('src', '/images/stars/rating_half.gif');
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', '/images/stars/rating_off.gif');
                star.className = 'off';
            }
            var widgetId = ratings[i].getAttribute('id').substr(7);
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            star.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
            star.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
	        star.onclick = new Function("evt", "rateInsight("+j+");");
            ratings[i].appendChild(star);
        }
    }
}

function displayHover(ratingId, star)
{
    for (var i = 0; i <= star; i++)
    {
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_over.gif');
    }
}

function displayNormal(ratingId, star)
{
    for (var i = 0; i <= star; i++)
    {
        var status = document.getElementById('star_'+ratingId+'_'+i).className;
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_'+status+'.gif');
    }
}

function rateInsight(rating, loggedin) {
        var recordRating = new XMLHttpRequest(); //window.location = location.href;
        recordRating.open("POST", location.href + "&rating=" + rating, false);
        recordRating.send(null);
        document.getElementById("ratingmsg").innerHTML = "Thank you!";
}

function commentOnInsight() {
    var recordRating = new XMLHttpRequest(); //window.location = location.href;
    var commentString = document.getElementById("usercomment").value;
    commentString = commentString.replace(/\n/g, "<br>");
    recordRating.open("POST", location.href + "&comment=" + commentString, false);
    recordRating.send(null);
    document.getElementById("commentpost").innerHTML = "Your comment has been posted.";    
}


function textCounter(field,cntfield,maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
    else {
        cntfield.innerHTML = maxlimit - field.value.length;
    }
}


function addToFolder(insightID, favoriteID) {
    //var recordFavorite = new XMLHttpRequest(); //window.location = location.href;
    aCall(location.href + "&i=" + insightID + "&folderid=" + favoriteID + "&addtofolder=true");
    document.getElementById(favoriteID + "_desc").innerHTML = "added";
    /*recordFavorite.open("POST", location.href + "&i=" + insightID + "&folderid=" + favoriteID + "&addtofolder=true", false);
    recordFavorite.send(null);
    if (recordFavorite.readyState == 4) {
        if (recordFavorite.status == 200) {
            document.getElementById(favoriteID + "_desc").innerHTML = "added";
        } else {
            alert("There was a problem retrieving the XML data - please try again.:n");
        }
    } */
}

function removeFromFolder(insightID, favoriteID) {
    //var recordFavorite = new XMLHttpRequest(); //window.location = location.href;
    aCall(location.href + "&i=" + insightID + "&folderid=" + favoriteID + "&removefromfolder=true");
    /*recordFavorite.open("POST", location.href + "&i=" + insightID + "&folderid=" + favoriteID + "&removefromfolder=true", false);
    recordFavorite.send(null);
    if (recordFavorite.readyState == 4) {
        if (recordFavorite.status == 200) {
            document.getElementById(favoriteID + "_desc").innerHTML = "removed";
        } else {
            alert("There was a problem retrieving the XML data - please try again.:n");
        }
    }*/

    document.getElementById(favoriteID + "_desc").innerHTML = "removed";
}


function addFolder(insightID, favoriteID) {
    //var recordFavorite = new XMLHttpRequest(); //window.location = location.href;
    aCall(location.href + "&i=" + insightID + "&folderid=" + favoriteID + "&removefromfolder=true");
    /*recordFavorite.open("POST", location.href + "&i=" + insightID + "&folderid=" + favoriteID + "&removefromfolder=true", false);
    recordFavorite.send(null);
    if (recordFavorite.readyState == 4) {
        if (recordFavorite.status == 200) {
            document.getElementById(favoriteID + "_desc").innerHTML = "removed";
        } else {
            alert("There was a problem retrieving the XML data - please try again.:n");
        }
    }*/

    document.getElementById(favoriteID + "_desc").innerHTML = "removed";
}




