if (document.images){
  pic1 = new Image(220,19); 
  pic1.src = "/themes/TOITheme/images/rating_loading.gif"; 

  pic2 = new Image(16,48); 
  pic2.src = "/themes/TOITheme/images/rating_star_new.gif"; 

  pic3 = new Image(16,48); 
  pic3.src = "/themes/TOITheme/images/rating_star_2_new.gif"; 
  
  pic4 = new Image(16,13); 
  pic4.src = "/themes/TOITheme/images/rating_tick.gif";
  
  pic5 = new Image(14,14); 
  pic5.src = "/themes/TOITheme/images/rating_warning.gif";
}

// AJAX ----------------------------------------

var xmlHttp

function GetXmlHttpObject(){

var xmlHttp = null;

	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp = new XMLHttpRequest();
	  }
	catch (e) {
	  // Internet Explorer
	  try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	  
	return xmlHttp;

}

// Calculate the rating
function rate(rating,id,hdl,show5,showPerc,showVotes){

	xmlHttp = GetXmlHttpObject()
	
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	  }

	/*for saving cookie */
	var coki=readIt(id);	  
	if(coki!=''&& coki!='undefined' && coki!=null){							
		var loader = document.getElementById('loading_'+id);
		loader.style.display = 'block';
		loader.innerHTML = '<div class="voted_twice">You already voted!</div>';							
		return false;
	}

	xmlHttp.onreadystatechange = function(){
		
	var loader = document.getElementById('loading_'+id);
	var uldiv = document.getElementById('ul_'+id);
	
		if (xmlHttp.readyState == 4){ 
			
			//loader.style.display = 'none';
			var res = xmlHttp.responseText;
					
			if(res == 'already_voted'){
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted_twice">You already voted!</div>';
			} else {
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted">Thanks for voting!</div>';
				saveIt(id);
				var temp;
				temp = res.split(',');
				voteCount = temp[1];
				avgRate = temp[0];
				res = avgRate * 20 ; // set res
				var newPerc ;
				
				// alert(res) ;
				
				if(show5 == 'true'){
					var out = document.getElementById('outOfFive_'+id);
					var calculate = res/20;
					//out.innerHTML = Math.round(calculate*100)/100; // 3.47;
					out.innerHTML =  avgRate ; // Math.round((calculate*2),0)/2; // 3.5;
				} 
				
				if(showPerc == 'true'){
					var perc = document.getElementById('percentage_'+id);
					//var newPerc = Math.round(Math.ceil(res/5))*5;
					newPerc = res;
					perc.innerHTML = newPerc+'%';
				}
				else if(showPerc == 'false'){
					newPerc = res;
				}
				
				if(showVotes == 'true'){
					var votediv = document.getElementById('showvotes_'+id).firstChild.nodeValue;
					var splitted = votediv.split(' ');
					//var newval = parseInt(splitted[0]) + 1;					
					var newval=voteCount;
					
					if(newval == 1){
						document.getElementById('showvotes_'+id).innerHTML ='('+ newval+' Vote)';
					} else {
						document.getElementById('showvotes_'+id).innerHTML = '('+ newval+' Votes)';
					}
				}
				
				var ulRater = document.getElementById('rater_'+id);
				ulRater.className = 'star-rating2';
				
				var all_li = ulRater.getElementsByTagName('li');
				
				// start at 1 because the first li isn't a star
				for(var i=1;i<all_li.length;i++){
					
					all_li[i].getElementsByTagName('a')[0].onclick = 'return false;';
					all_li[i].getElementsByTagName('a')[0].setAttribute('href','#');
					
				}
				
				//alert("newPerc:" + newPerc) ;
				if(navigator.appName == 'Microsoft Internet Explorer'){
					uldiv.style.setAttribute('width',newPerc+'%'); // IE
				 } else {
					uldiv.setAttribute('style','width:'+newPerc+'%'); // Everyone else
				 }
				
			}
		} else {
			loader.innerHTML = '<img src="/themes/TOITheme/images/rating_loading.gif" alt="loading" width="110" height="10" />';	
		}
	
	}
	var url = "/tilblogs/ratepost.do";
	var params = "an="+ id + "&hdl=" + hdl + "&rt="+rating;
	
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);

} 


//Calculate the rating
function pullRating(id,hdl,show5,showPerc,showVotes){
	
	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	  }

	xmlHttp.onreadystatechange = function(){
		
	var loader = document.getElementById('rating');
	
		if (xmlHttp.readyState == 4){ 
			
			//loader.style.display = 'none';
			var res = xmlHttp.responseText;
			
			if(res == 'already_voted'){
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted_twice">You already voted!</div>';
			} else {
				var temp;
				temp = res.split(',');
				voteCount = temp[1];
				avgRate = temp[0];
				var avRate=avgRate.substring(0,3);
				
				var avg = avgRate ; // to be parsed from res value
				var per = 80 ; // not to be used
				var votes = getVotes(voteCount) ; // to be parsed from the res value
				if( show5 || showPerc || showVotes){
					// text = '<div class="rated_text">';
					text = '<span class="rated_text">';

				}
					if(show5){
						show5bool = 'true';
							if(avg==""){avg="0";}
						text += (' <span id="outOfFive_' + id + '" class="out5Class">' + avg + '</span><span><strong>/5</strong></span>');
					} else {
						show5bool = 'false';
					}
					if(showPerc){
						showPercbool = 'true';
						text += (' <span id="percentage_' + id + '" class="percentClass">(' + per + ')</span>') ;
					} else {
						showPercbool = 'false';
					}
					if(showVotes){
						showVotesbool = 'true';
						if(votes=="undefined Votes"){
							votes="0 Vote";
							}
						text += (' <span id="showvotes_' + id + '" class="votesClass" >(' + votes + ')</span>' ) ;
					} else {
						showVotesbool = 'false';	
					}
					
				if(show5 || showPerc || showVotes){	
					// text += '</div>';
                                  text += '</span>';
				}
				
				var wdth = getRating(avg) ;
                           text += ('<div id="starlisting" class="rated_text">') ;
				text += ( '<ul class="star-rating" id="rater_' + id + '">') ;
				text += ( '<li class="current-rating" style="width:' + wdth  + ';" id="ul_' + id + '"></li>' ) ;
				text += ( '<li><a onclick="rate(\'1\',\'' + id + '\',\'' + hdl + '\',\'' + show5bool + '\',\'' + showPercbool + '\',\'' + showVotesbool + '\'); return false;" href="#" title="1 star out of 5" class="one-star" >1</a></li>' ) ;
				text += ( '<li><a onclick="rate(\'2\',\'' + id + '\',\'' + hdl + '\',\'' + show5bool + '\',\'' + showPercbool + '\',\'' + showVotesbool + '\'); return false;" href="#" title="2 stars out of 5" class="two-stars" >2</a></li>' ) ;
				text += ( '<li><a onclick="rate(\'3\',\'' + id + '\',\'' + hdl + '\',\'' + show5bool + '\',\'' + showPercbool + '\',\'' + showVotesbool + '\'); return false;" href="#" title="3 stars out of 5" class="three-stars" >3</a></li>' ) ;
				text += ( '<li><a onclick="rate(\'4\',\'' + id + '\',\'' + hdl + '\',\'' + show5bool + '\',\'' + showPercbool + '\',\'' + showVotesbool + '\'); return false;" href="#" title="4 stars out of 5" class="four-stars" >4</a></li>' ) ;
				text += ( '<li><a onclick="rate(\'5\',\'' + id + '\',\'' + hdl + '\',\'' + show5bool + '\',\'' + showPercbool + '\',\'' + showVotesbool + '\'); return false;" href="#" title="5 stars out of 5" class="five-stars" >5</a></li>' ) ;	
				text += ( '</ul>' ) ;
				text += ( '<div id="loading_' + id + '"></div>') ;
				text += '</div>';
	
				loader.innerHTML = text ;
			}
		} else {
			loader.innerHTML = '<img src="/themes/TOITheme/images/rating_loading.gif" alt="loading" />';	
		}
	}

	var url = "/tilblogs/fetchrating.do";
	var params = "an="+ id + "&hdl=" + hdl + "&dt="+new Date();
	
	xmlHttp.open("GET",url + "?" + params,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	// xmlHttp.send(params);
	xmlHttp.send(null);

}

function getVotes(vote){
	var votes = '' ;
	if(vote == 0){
		votes = '0 Votes';
	}
	else if(vote == 1){
		votes = '1 Vote';
	}
	else {
		votes = vote + ' Votes';
	}

	//alert("getVoting(" + vote + "):" + votes ) ;
	return votes ;
}

function getRating(rate) {
	var rating = '0%' ;
	
	if(rate > 0) {	
		perc = (rate) * 20;		
		rating = perc ;
		rating += '%' ;
	}	
	
	// alert("getRating(" + rate + "):" + rating ) ;
	return rating ;
}

/* for cookie */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		//alert(date.getTime());
	//	date.setTime(date.getTime()+(days*24*60*60*1000));2*60*60*1000
	date.setTime(date.getTime()+(1*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	//alert(name);
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	//alert(ca);
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function saveIt(name) {
	
		//Cookies.create(name,x,7);
		createCookie(name,name,7)
		//alert('Cookie created');
	
}

function readIt(id) {
var x = readCookie(id)
	var date = new Date();
//alert('The value of the cookie is ' + x);
return x;
}