/*
Original Author: Addam M. Driver
Date: 10/31/2006
*/

var sMax;	// Is the maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;
var saved;

function initSaved(me){
    if (IsSaved)
    {
        saved = 1;
        me.innerHTML = textSaved;
    }
}

// Rollover for image Stars //
function initRating(num){
    if (num != null && num != 0)
    {
        preSet = document.getElementById("_"+num);
        rating(preSet);
    }
}

// Rollover for image Stars //
function rating(num){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				holder = a+1;
				a++;
			}else{
				document.getElementById("_"+i).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
			}
		}else{
			rating(preSet);
		}
	}
}

// When you actually rate something //
function rateIt(me){
    if (HasArticlesUserCookie())
    {
	    if(!rated){
		    preSet = me;
		    rated=1;
		    sendRate(me);
		    rating(me);
	    }
	}
	else
	{
	    ShowJoin();	
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel){
    var currentPage = location.toString();

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", currentPage);
                                                
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "action");
    hiddenField.setAttribute("value", "rate");
    form.appendChild(hiddenField);

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "ArticleId");
    hiddenField.setAttribute("value", ArticleId);
    form.appendChild(hiddenField);
    
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "Rating");
    hiddenField.setAttribute("value", sel.title);
    form.appendChild(hiddenField);

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "ArticlesUserId");
    hiddenField.setAttribute("value", getCookie('ArticlesUserId'));
    form.appendChild(hiddenField);
    
    setCookie("Rating-" + ArticleId, sel.title, 5); // set cookie for 5 days
    
    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();

    location.replace(currentPage);
    return false;
}

// When you actually rate something //
function saveIt(me){
    if (HasArticlesUserCookie())
    {
	    if(!saved){
		    saved=1;
		    sendSave(me);
	    }
	}
	else
	{
	    ShowJoin();	
	}
}

// Send the saved information somewhere using Ajax or something like that.
function sendSave(sel){
    var currentPage = location.toString();
    
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", currentPage);
                                                
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "action");
    hiddenField.setAttribute("value", "save");
    form.appendChild(hiddenField);

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "ArticleId");
    hiddenField.setAttribute("value", ArticleId);
    form.appendChild(hiddenField);
    
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "ArticlesUserId");
    hiddenField.setAttribute("value", getCookie('ArticlesUserId'));
    form.appendChild(hiddenField);
    
    setCookie("Saved-" + ArticleId, true, 5); // set cookie for 5 days

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();

    location.replace(currentPage);
    return false;
}
