
function Vote(amount) {
	var index;
	var votecount = $('#votes').raw();
	var ranktotal = $('#rank').raw();
	if(!tutorialid) {
		tutorialid = $('#tutorialid').raw().value;
		index = false;
	}
	
	ajax.get('ajax.php?action=ranktutorial&id=' + tutorialid + '&auth=' + $('#auth').raw().value + '&rank=' + amount, function (response) {
			if(response == 'dupe') {
				error_message("You have already ranked this tutorial!");	
			} else {
				if(response != 'no') {
					error_message("Your rank of " + amount + " has been added to the tutorial ranking for a total of " + response);
					votecount.innerHTML = (parseInt(votecount.innerHTML)) + 1;
					ranktotal.innerHTML = response;
				} else {
					error_message("There was an error with the Ajax function, returned 'no'");
				}
			}
		}
	);
}

function Toggle(id, disable) {
	var arr = document.getElementsByName(id + '[]');
	var master = $('#toggle_' + id).raw().checked;
	for (var x in arr) {
		arr[x].checked = master;
		if(disable == 1) {
			arr[x].disabled = master;
		}
	}
}

function addTutorialTag() {
	if($('#tagname').raw().value == "") {
		$('#tagname').raw().value = $('#tutorial_tags').raw().options[$('#tutorial_tags').raw().selectedIndex].value;
	} else if($('#tutorial_tags').raw().options[$('#tutorial_tags').raw().selectedIndex].value == '---') {
	} else {
		$('#tagname').raw().value = $('#tagname').raw().value + ', ' + $('#tutorial_tags').raw().options[$('#tutorial_tags').raw().selectedIndex].value;
	}
}

/*if($('#tags').raw().value == "") {
		$('#tags').raw().value = $('#t_tags').raw().options[$('#t_tags').raw().selectedIndex].value;
	} else if($('#t_tags').raw().options[$('#t_tags').raw().selectedIndex].value == '---') {
	} else {
		$('#tags').raw().value = $('#tags').raw().value + ', ' + $('#t_tags').raw().options[$('#t_tags').raw().selectedIndex].value;
	}
}*/

/*function tagOps(authkey, opt, cmd) {
	tutorialid = $('#tutorialid').raw().value;
	ajax.get('tutorials.php?action=managetags&id=' + tutorialid + '&cmd=' + cmd + '&auth=' + authkey + '&opt=' + opt, function (response) {
		if(response == "yes") {
			return true;
		}
		if(response == "no") {
			send_error("Error executing AJAX command for tag management!");
			return false;
		}
		
	}
);
}*/

//Ranking code
/* AJAX Star Rating : v1.0.3 : 2008/05/06 */
/* http://www.nofunc.com/AJAX_Star_Rating/ */

function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.documentElement.scrollLeft,'Y':event.clientY+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};

star.mouse=function(e,o) { if(star.stop || isNaN(star.stop)) { star.stop=0;

    document.onmousemove=function(e) { var n=star.num;
    
        var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);

        if(oX<1 || oX>84 || oY<0 || oY>19) { star.stop=1; star.revert(); }
        
        else {

            $S('starCur'+n).width=oX+'px';
            $S('starUser'+n).color='#999';
            $('starUser'+n).innerHTML=Math.round(oX/84*100)+'%';
        }
    };
} };

star.update=function(e,o) { var n=star.num, v=parseInt($('starUser'+n).innerHTML);

   	 n=o.id.substr(4); $('starCur'+n).title=v;

	var ammount = v/10;
	var votecount = $('#votes').raw();
	var ranktotal = $('#rank').raw();
	var tutorialid = $('#tutorialid').raw().value;

	var req = (typeof(window.ActiveXObject) === 'undefined') ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		req.onreadystatechange = function () {
			if (req.readyState !== 4 || req.status !== 200) {
				return;
			}
			response = req.responseText;
			ranktotal.innerHTML = response;
		};
	req.open("GET", "ajax.php?action=ranktutorial&id="+tutorialid+"&auth="+$('#auth').raw().value+"&rank="+ammnount,true);
	req.send(null);

	votecount.innerHTML = (parseInt(votecount.innerHTML)) + 1;
	
};

star.revert=function() { var n=star.num, v=parseInt($('starCur'+n).title);

    $S('starCur'+n).width=Math.round(v*84/100)+'px';
    $('starUser'+n).innerHTML=(v>0?Math.round(v)+'%':'');
    $('starUser'+n).style.color='#888';
    
    document.onmousemove='';

};

star.num=0;

