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

var sMax;	// Isthe 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;

// 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(!rated){
		preSet = me;
		//rated=1;
		document.getElementById("rateStatus").value = me.title;
		rating(me);
	}
}

//////////////////////////////////////

function rateFormNLI(url,pID) {
	var div = document.getElementById('ratingForm');
	var str = "url="+url+"&pID="+pID;
	
	if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			  xmlhttp = new XMLHttpRequest();
			}
		
			xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						div.innerHTML = xmlhttp.responseText;	
					} 
			
			}
	
	   xmlhttp.open("POST", "ratings.php?load=snapshot");
	   xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	   xmlhttp.send(str);
}

function rateForm(pID) {
	var div = document.getElementById('ratingForm');
		var str = "pID="+pID;
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			  xmlhttp = new XMLHttpRequest();
			}
		
			xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						div.innerHTML = xmlhttp.responseText;	
						document.getElementById('rf').innerHTML='<a href="javascript:void(0);" onclick="rateClose(\'rf\','+pID+')">Rate This Item</a>';
					} 
			
			}
	
	   xmlhttp.open("POST", "ratings.php?load=form");
	   xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	   xmlhttp.send(str);
}

function rateClose(id, pID, score, count) {
	document.getElementById('ratingForm').innerHTML = '';
	if (id == 'rv') { 
		document.getElementById('rv').innerHTML = '<a href="javascript:void(0);" onclick="rateView('+pID+','+score+','+count+')">View Ratings ('+count+')</a>';
	}else{
		document.getElementById('rf').innerHTML = '<a href="javascript:void(0);" onclick="rateForm('+pID+')">Rate This Item</a>';
	}
}

function rateView(pID, score, count) {
	var div = document.getElementById('ratingForm');
		var str = "pID="+pID+'&score='+score+'&count='+count;
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			  xmlhttp = new XMLHttpRequest();
			}
		
			xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						div.innerHTML = xmlhttp.responseText;	
						document.getElementById('rv').innerHTML='<a href="javascript:void(0);" onclick="rateClose(\'rv\','+pID+','+score+','+count+')">View Ratings ('+count+')</a>';						
					} 
			
			}
	
	   xmlhttp.open("POST", "ratings.php?load=view");
	   xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	   xmlhttp.send(str);
}

 function hideMsg() {
	document.getElementById('ratingForm').innerHTML = ''
 }

 function rateFormPost(fobj) {
	 
	 	var div = document.getElementById('ratingForm');
		var error = document.getElementById('ratingError');
		var str = "";
					
			   if (fobj != undefined && fobj != '') {
					str += parseObj(fobj);
				}
		   str = str.substr(0,(str.length - 1));
		   
		   
		  
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			  xmlhttp = new XMLHttpRequest();
			}
		
			xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						switch (xmlhttp.responseText) {
							case 'ok':
								div.innerHTML = '<div id="ratingSuccess">Thank you, your rating has been submitted.</div>';
								document.getElementById('rf').innerHTML = '';
								window.scrollTo(0,0);
								setTimeout("hideMsg()", 5000);
							break;
							default:
								error.innerHTML = xmlhttp.responseText;
							break;
						}
					} 
					
			
			}
	
	   xmlhttp.open("POST", "ratings.php?load=post"); 
	   xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	   xmlhttp.send(str);
 
 }
 
 function parseObj(fobj) {
	var str = '';
	for(var i = 0;i < fobj.elements.length;i++) {
	   switch(fobj.elements[i].type) {
		   case "text":
		   case "textarea":
		   case "password":
		   case "hidden":
				str += fobj.elements[i].name +
				 "=" + fobj.elements[i].value + "&";
		   break;
		   case "select-one":
				str += fobj.elements[i].name +
				"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
		   break;
		   case "radio":
		    case "checkbox":
				if (fobj.elements[i].checked) {
				str += fobj.elements[i].name +
				"=" + fobj.elements[i].value + "&";
				}
		   break;
		   case "button":
		   	continue;
		   break;
		   default:
				//alert('Unknown form type: ' + fobj.elements[i].type);
		   break;
		}
	}
	return str;
}
