var http = createRequestObject();
var displayRating = '';
var currentId = '';

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        ro = new XMLHttpRequest();
    }
    return ro;    
}

function updateRating(obj, rating, primary_id) {
	var id = obj.title;
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;
	currentId = idName;

	 http.open('get', '/script/rating.php?primary_id='+primary_id+'&id='+id+'&idName='+idName);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	if(http.readyState == 4){
		response  = 	http.responseXML.documentElement;
		
		var error_msg	=	'';
		var primary_id	=	'';
		
		if(response.getElementsByTagName('result_flag')[0].childNodes[0].firstChild.nodeValue=="true") {
			
			totalRating = response.getElementsByTagName('result_field')[0].childNodes[2].firstChild.nodeValue;
			primary_id = response.getElementsByTagName('result_field')[0].childNodes[0].firstChild.nodeValue;
			
			var obj = document.getElementById(currentId+'_'+totalRating);
			
			changeover(obj, totalRating);
			document.getElementById(currentId+"_rating").innerHTML	=	"Thanks for your rating.";
			
		} else {
				
			document.getElementById(currentId+"_rating").innerHTML	=	response.getElementsByTagName('error_field')[0].childNodes[0].firstChild.nodeValue;
		}
       
    }
}

function changeover(obj, rating) {
	
	var imageName = obj.src;
	var id = obj.title;
	var index = imageName.lastIndexOf('/');
	var filename = imageName.substring(index+1);
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;

	for(i=0; i<id; i++) {
		var num = i+1;
		
		if (num%2 == 0) {
			document.getElementById(idName+'_'+num).src = '/images/_even1.jpg';			
		}
		else {
			document.getElementById(idName+'_'+num).src = '/images/_odd1.jpg';
		}
	}

}

function changeout(obj, rating) {

	var imageName = obj.src;
	var id = obj.title;
	var index = imageName.lastIndexOf('/');
	var filename = imageName.substring(index+2);
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;
	
	for(i=0; i<id; i++) {
		var num = i+1;
		
		if (num%2 == 0) {
			if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = '/images/__even1.jpg';			
			}
			else {
				document.getElementById(idName+'_'+num).src = '/images/even1.jpg';			
			}
		}
		else {
			if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = '/images/__odd1.jpg';			
			}
			else {
				document.getElementById(idName+'_'+num).src = '/images/odd1.jpg';			
			}
		}
	}
}

function displayStars(rating, idName, primary_id, is_login) {
	
	for(i=0; i < 10; i++ ) {
		if(i%2 ==0) {
			if(i < rating) {
				
				if(is_login=="false") {
					document.write('<a href="/popup-login.html" rel="lyteframe" rev="width: 360px; height: 215px; scrolling: no;"><img border="0" src="/images/__odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'"  /></a>');
				} else {
					document.write('<a href="#"><img border="0" src="/images/__odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+', '+primary_id+')" /></a>');
				}
			}
			else {
				if(is_login=="false") {
					document.write('<a href="/popup-login.html" rel="lyteframe" rev="width: 360px; height: 215px; scrolling: no;"><img border="0" src="/images/odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" /></a>');
				} else {	
					document.write('<a href="#"><img border="0" src="/images/odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+', '+primary_id+')" /></a>');
				}
			}
		}
		else {
			if(i < rating) {
				if(is_login=="false") {	
					document.write('<a href="/popup-login.html" rel="lyteframe" rev="width: 360px; height: 215px; scrolling: no;"><img border="0" src="/images/__even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'"  /></a>');
				} else {
					document.write('<a href="#"><img border="0" src="/images/__even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+', '+primary_id+')" /></a>');
				}
			}
			else {
				if(is_login=="false") {		
					document.write('<a href="/popup-login.html" rel="lyteframe" rev="width: 360px; height: 215px; scrolling: no;"><img border="0" src="/images/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'"  /></a>');
				} else {
					document.write('<a href="#"><img border="0" src="/images/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+', '+primary_id+')" /></a>');						
				}
			}
		}
	}

}

function DisplayStarsProfile(rating, idName, primary_id, is_login) {
	
	for(i=0; i < 10; i++ ) {
		if(i%2 ==0) {
			if(i < rating) {
				document.write('<img border="0" src="/images/__odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'"/>');
			}
			else {
				document.write('<img border="0" src="/images/odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" />');
			}
		}
		else {
			if(i < rating) {
				document.write('<img border="0" src="/images/__even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'"  />');
			}
			else {
				document.write('<img border="0" src="/images/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'"  />');
			
			}
		}
	}

}