//=============================================================================
// Rating init, this is called by the on-load and digs info from the display.
// Note in particular the form "document.displayinfoform" is expected.
//==============================================================================
var levusefulsectiondiv = "page-levuseful-table";
var levusefulsectionelement = null;

//------
// Initializes the ratings on the page.
//------
function levratingajax_init() {
	// alert("levratingajax_init() running...");
  // make sure the destination div is present.
  levusefulsectionelement = document.getElementById(levusefulsectiondiv);
	if (levusefulsectionelement == null) {
		//alert("Page is missing a required element (rating section div), please contact the administrator with any available information.");
		return;
	}
	// find and get the info from the page.
	diform = document.displayinfoform;  // <-- change this as necessary.
  if (diform == null) {
		alert("** Page is missing a required element (form containing document id), please contact the administrator with any available information.");
		return;
  }
  // get the resourceID
  if(diform.docid == null) {
		alert("** Page is missing a required element (document id), please contact the administrator with any available information.");
		return;
  }
 	resid = diform.docid.value;
  // get the userID
  if(diform.userid == null) {
		alert("** Page is missing a required element (user id), please contact the administrator with any available information.");
		return;
  }
 	userid = diform.userid.value;
	//alert("displayboxes_init calling getRelated("+resid+",...)");
	resourcetype = document.displayinfoform.type.value;
	//alert("resourcetype = "+resourcetype);
	
  if(resourcetype == "lev") {
    //alert("Resourcetype = "+resourcetype);
    LevUsefulAjaxJS.getSection(
  	  resid,
  	  userid,
  	  "yes",
  	  /* the callback function defined here */
  	  { callback:function(str) { 
  				levusefulsectionelement.innerHTML = str; 
  				levusefulsectionelement.className = "ratingsshow";
  	  	},
 	  		errorHandler:function(errorString, exception) {
  				alert("LevUseful DWR error: "+errorString);
  			}
  		} 
  	  );
  	}
  return;
}	

function levratingajax_getform(resid,userid) {
  LevUsefulAjaxJS.getSection(
	  resid,
	  userid,
	  "no",
	  /* the callback function defined here */
	  {	callback:function(str) { 
				levusefulsectionelement.innerHTML = str; 
				levusefulsectionelement.className = "ratingsshow";
	  	},
 	  	errorHandler:function(errorString, exception) {
  			alert("LevUseful DWR error: "+errorString);
  		}
  	} 
	  );
  return;
}	

//=============================================================================
// Submission support. (Sohyla)
//=============================================================================
var levsMax;	// Is the maximum number of stars
var levholder; // Is the holding pattern for clicked state
var levpreSet; // Is the PreSet value onces a selection has been made
var levrated;
var levrating_score; 
var levscore;

// Rollover for image Stars //
function levrating(num){
	//alert("In levrating: "+s);
	levsMax = 0;	// Is the maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "INPUT"){
			levsMax++;	
		}
	}
	if(!levrated){
		s = num.id.replace("_lev", ''); // Get the selected star
		a = 0;
		//alert("In rating: "+s);
		for(i=1; i<=levsMax; i++){		
			if(i<=s){
				//document.getElementById("_lev"+i).className = "on";
				document.getElementById("levrateStatus").innerHTML = num.name;	
				levholder = a+1;
				a++;
			}else{
				document.getElementById("_lev"+i).className = "";
			}
		}
	}
}

// When you actually rate something //
function levrateIt(me){
    //alert("levrated = "+levrated);   
	if(!levrated){
	    ratevar = me.id;
	    //alert ("ratevar = "+ratevar+" me = "+me);
	    if(ratevar == "_lev1") ratevar = "useful";
	    if(ratevar == "_lev3") ratevar = "not useful";
	    if(ratevar == "_lev2") ratevar = "somewhat useful";
	    if((ratevar == "_lev1") || (ratevar == "_lev3") || (ratevar == "_lev2")){
		  document.getElementById("levrateStatus").innerHTML = /*document.getElementById("levratingSaved").innerHTML + */ "You have found this leagle eyeView "+ratevar+".";
        }
		levpreSet = me;
		levrated=0;
		levscore = levsendRate(me);
		//alert("rating : "+levscore);
		document.levusefulsectionform.saverating.value = levscore;
		//alert("saverating = "+document.levusefulsectionform.saverating.value);
		//levrating(me);
	}
	return levscore;
}

// Send the rating information somewhere using Ajax or something like that.
function levsendRate(sel){
    levrating_score = 0;
    if(sel.id == '_lev1') levrating_score = 1;
    if(sel.id == '_lev2') levrating_score = 2;
    if(sel.id == '_lev3') levrating_score = 3;      
	//alert("Your rating was: "+levrating_score);
	return levrating_score;
}



//=============================================================================
// Ajax sections.
//=============================================================================
//alert("ratingajex.js implemented");

var levratediv = "levallratings";
var levrateelement = null;

var levratingshowclass = "ratingsshow";
var levratinghideclass = "ratingshide";
var levratingshowhidebtnid = "levratingshowbtn";

//------
// New rating submission.
//------
function levratingajax_setrating(objid,userid,ratingform) {
	//alert("levratingajax_setrating("+objid+","+userid+"...) with saverating="+ratingform.saverating.value+" and comment=\'"+ratingform.levcommentarea.value+"\'.");	
    levusefulsectionelement = document.getElementById(levusefulsectiondiv);
    //alert("levusefulsectionelement = "+levusefulsectionelement);
	if (levusefulsectionelement == null) {
		alert("*** Page is missing a required element (rating display div), please contact the administrator with any available information.");
		return;
	}
	if (ratingform == null) {
		alert("*** Page is missing a required element (rating form), please contact the administrator with any available information.");
		return;
	}
	if (ratingform.saverating == null) {
		alert("*** Page is missing a required element (rating form rating), please contact the administrator with any available information.");
		return;
	}
	if (ratingform.levcommentarea == null) {
		alert("*** Page is missing a required element (rating form comment area), please contact the administrator with any available information.");
		return;
	}
	if (ratingform.saverating.value == "1") {
	  if(ratingform.rate[0].checked == false){
	    //alert("Yes is false");
	  }
	  if(ratingform.rate[1].checked == true){
	    //alert("Somewhat is true");
	    ratingform.saverating.value = "2";
	  }
	  if(ratingform.rate[2].checked == true){
	    //alert("No is true");
	    ratingform.saverating.value = "3";
	  }
	}	 	
	LevUsefulAjaxJS.setRating(
		objid,
		userid,
		ratingform.saverating.value,
		ratingform.levcommentarea.value,
		{	callback:function(str) {
				levusefulsectionelement.innerHTML = str; 
				levusefulsectionelement.className = levratingshowclass;
			},
	  		errorHandler:function(errorString, exception) {
 				alert("LevUseful DWR error: "+errorString);
 			}
 		} 
		);
	return;
}	

//------
// Clear a rating (the whole thing). NOT USED FOR LEV RATING
//------
function levratingajax_clearrating(objid) {
	alert("levratingajax_clearrating("+objid+","+userid+"...)");	
  levusefulsectionelement = document.getElementById(levusefulsectiondiv);
	if (levusefulsectionelement == null) {
		alert("Page is missing a required element (rating display div), please contact the administrator with any available information.");
		return;
	}
	LevUsefulAjaxJS.clearRating(
	  objid,
	  {	callback:function(str) {
	  		levusefulsectionelement.innerHTML = str; 
	  		levusefulsectionelement.className = levratingshowclass;
	  	},
	  		errorHandler:function(errorString, exception) {
 				alert("LevUseful DWR error: "+errorString);
 			}
 		} 
	  );
	return;
}	

//------
// Present the form to add a rating. NOT USED FOR LEV RATING
//------
function levratingajax_newrating(objid,userid,divid) {
	alert("levratingajax_newrating("+objid+","+userid+","+divid+")");	
  levrateelement = document.getElementById(divid);
	if (levrateelement == null) {
		alert("Page is missing a required element (rating display div), please contact the administrator with any available information.");
		return;
	}
	LevUsefulAjaxJS.getForm(
		objid,
		userid,
		{ callback:function(str) {
				levrateelement.innerHTML = str; 
				levrateelement.className = levratingshowclass;
			},
  		errorHandler:function(errorString, exception) {
 				alert("LevUseful DWR error: "+errorString);
 			}
 		} 
		);
	return;
}	

//------
// Display a list of all ratings. NOT USED FOR LEV RATING
//------
var levshowbutton = null;

function levratingajax_showratings(objid,userid,levshowratingid,levshowbuttoninput) {
	alert("levratingajax_showratings("+objid+","+userid+","+levshowratingid+","+((levshowbuttoninput == null)? "null input":levshowbuttoninput.value)+")");
	
  levrateelement = document.getElementById(levshowratingid);
	if (levrateelement == null) {
		alert("Page is missing a required element (rating display div), please contact the administrator with any available information.");
		return;
	}
	levshowbutton = levshowbuttoninput;
	var levshowbuttontext = (levshowbutton == null)? "none" : levshowbutton.value;
	//alert("levShowbuttontext "+levshowbuttontext);
	if (levshowbuttontext == "Hide") {
		//alert("hide ratings");
		levrateelement.className = levratinghideclass;
		if (levshowbutton != null) levshowbutton.value = "View";
		return;
	}
	//alert("Getting list of ratings for "+objid);
	LevUsefulAjaxJS.getList(
	  objid,
	  userid,
	  {	callback:function(str) {
	  		levrateelement.innerHTML = str; 
	   		levrateelement.className = levratingshowclass;
				if (levshowbutton != null) levshowbutton.value = "Hide";
	  	},
	  		errorHandler:function(errorString, exception) {
 				alert("LevUseful DWR error: "+errorString);
 			}
 		} 
	  );

	return;
}	

//=================
// ALways last.
//=================
if(typeof window.initArray != 'object') var initArray = [];
initArray["levratingajax_init"] = levratingajax_init;


