/*
	Used for the rating Feature, this file contais a copule of ajax calls to add a new
	rating to a reference, as well as the necesary scripts to highligt/fade the stars
*/

var imgPath="../assets/images/";
var backendPath="../model/";


//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	try{// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}catch (e){
		try{// Internet Explorer 6.0+
			return new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{// Internet Explorer 5.5
				return new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				alert("Your browser does not support AJAX!");
			}
		}
	}
}

//XmlHttpRequest object to update the rankings
var searchReq=getXmlHttpRequestObject();

/*
	higlight a star and all the stars that are before it (they can be identified by
	the final number of its id)
*/
function overStar(starid, referenceId){
	if (readCookie(referenceId) == 0) {
		var starAt = starid.substring(starid.length - 1, starid.length);
		for (i = 1; i <= starAt; i++) {
			document.getElementById("star" + i).src = imgPath + "yeellowstar.gif";
		}
		for (i = 5; i > starAt; i--) {
			document.getElementById("star" + i).src = imgPath + "withestar.gif";
		}
	}
}

function readCookie(referenceId)
{
	/**
	* read Cookie
	*/	
	var cookiewert = "";
	var rating = 0;
	var position = "";
	var ratingposition = "";
	
	if(referenceId-100000 < 0) ratingposition = 7;
	if(referenceId-10000 < 0) ratingposition = 6;
	if(referenceId-1000 < 0) ratingposition = 5;
	if(referenceId-100 < 0) ratingposition = 4;
	if(referenceId-10 < 0) ratingposition = 3;

	
		if(document.cookie){	
			var keks = document.cookie;
			cookiewert = keks.substring(keks.indexOf("ratedpages=")+11,keks.indexOf(';'));
			position = cookiewert.search(referenceId+"-");
			if(position >= 0)
			{
				rating = cookiewert.charAt(position+ratingposition-1);		
				return rating;
			}
			else{
				return rating;
			}
					
		} 
}

function saveCookie(referenceId, rating)
{
	var cookiewert = "";
	
		if(document.cookie){	
			var keks = document.cookie;
			if(keks.search("ratedpages=") < 0)
			{
				cookiewert = referenceId+'-'+rating +',';
			}else{
				cookiewert = keks.substring(keks.indexOf("ratedpages=")+11,keks.indexOf(';'));
				//cookiewert = keks.substring(0, keks.search(';'));
				cookiewert = cookiewert + ',' + referenceId +'-'+rating+',';
			}		
		} 
		
		//alert(cookiewert);
		/**
		* save Cookie
		*/
		var expiredate = new Date();
		expiredate = new Date(expiredate.getTime() +1000*60*60*24*10);
		document.cookie = 'ratedpages='+cookiewert+'; expires='+expiredate+';'; 	
}

/*
	The ajax call to update the rating of the page, also updates the stars, so
	the user can not rate again
*/
function saveRating(starId,referenceId, url){

	if (readCookie(referenceId) == 0) {
		 var rating=starId.substring(starId.length-1,starId.length);
		
		 document.getElementById("activeStars").style.display="none";
		 for(i=1;i<=rating;i++){
		 document.getElementById("star"+(i+5)).src=imgPath+"yeellowstar.gif";
		 }
		 document.getElementById("inactiveStars").style.display="";
		 if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		 searchReq.open("GET", url+'&method=saveRating&iRating='+rating+'&iReferenceId='+referenceId, true);
		 //searchReq.open("GET", backendPath+'qry_references.cfc?method=saveRating&iRating='+rating+'&iReferenceId='+referenceId, true);
		 searchReq.send(null);
		 }
		 
		saveCookie(referenceId,rating);
	}else{
		document.getElementById("inactiveTextAlreadyVoted").style.display="";
	}
	
}

function setVotedStars(id)
{
	var rating = readCookie(id);
	for(var i = 1; i<= rating; i++)
	{
		document.getElementById("star"+i).src=imgPath+"yeellowstar.gif";
	}
}


