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) {
	var id = obj.title;
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	var totalRating = rating;
	currentId = idName;

    http.open('get', 'ajax.php?id='+id+'&idName='+idName);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	if(http.readyState == 4){
        var response = http.responseText;
	
		if (response == 'ERROR'){
			alert("Sorry... Failed to update rating.");
		}
		else {
			alert("Thanks for your time.");
		}
       
        displayRating = response.substr(0, 4);
        document.getElementById(currentId+'_showrating').innerHTML = 'Score: '+displayRating;
        totalRating = Math.ceil(response);   
        var obj = document.getElementById(currentId+'_'+totalRating);
        changeover(obj, totalRating);
		displayStars(rating, currentId);
    }
}

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 = 'img/icon/_even1.gif';			
		}
		else {
			document.getElementById(idName+'_'+num).src = 'img/icon/_odd1.gif';
		}
	}

}

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 = 'img/icon/__even1.gif';			
			}
			else {
				document.getElementById(idName+'_'+num).src = 'img/icon/even1.gif';			
			}
		}
		else {
			if(i < totalRating) {
				document.getElementById(idName+'_'+num).src = 'img/icon/__odd1.gif';			
			}
			else {
				document.getElementById(idName+'_'+num).src = 'img/icon/odd1.gif';			
			}
		}
	}
}

function displayStars(rating, idName) {

	document.write('');
	
	for(i=0; i < 10; i++ ) {
		if(i%2 ==0) {
			if(i < rating) {
				document.write('<img align="bottom" style="cursor:pointer" src="img/icon/__odd1.gif" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
			else {
				document.write('<img align="bottom" style="cursor:pointer" src="img/icon/odd1.gif" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
		}
		else {
			if(i < rating) {
				document.write('<img align="bottom" style="cursor:pointer" src="img/icon/__even1.gif" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
			else {
				document.write('<img align="bottom" style="cursor:pointer" src="img/icon/even1.gif" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
			}
		}
	}

	if (displayRating == '') {
		document.write('<span style="font-weight:bold;color:red;padding:0px 4px 0px 11px" id="'+idName+'_showrating" >'+displayRating+'</span>');
	}
	else {
		document.write('<span style="font-weight:bold;color:red;padding:0px 4px 0px 11px" id="'+idName+'_showrating" >'+totalRating+'</span>');
	}
	document.write('');

}
