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;
    var related_id = document.getElementById('related_id').value;
    var type= document.getElementById('type').value;

    currentId = idName;
   
    http.open('get', 'rate.php?id='+id+'&idName='+idName+'&related_id='+related_id+'&type='+type);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        //alert(response);
	
        if (response == 'ERROR'){
            alert("Sorry... Failed to update rating.");
        }
        else {
         // location.reload(true);
        }
       
        displayRating = response.substr(0, 4);
        //document.getElementById(currentId+'_showrating').innerHTML = 'Rating: '+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 = './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) {

    document.write('<center>');
	
    for(i=0; i < 10; i++ ) {
        if(i%2 ==0) {
            if(i < rating) {
                document.write('<img src="./images/__odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" width="8px" height="9px" />');
            }
            else {
                document.write('<img src="./images/odd1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" width="8px" height="9px" />');
            }
        }
        else {
            if(i < rating) {
                document.write('<img  width="8px" height="9px" src="./images/__even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" />');
            }
            else {
                document.write('<img src="./images/even1.jpg" id="'+idName+'_'+(i+1)+'" title="'+(i+1)+'" onmouseout="changeout(this, '+rating+')" onmouseover="changeover(this, '+rating+')" onclick="updateRating(this, '+rating+')" width="8px" height="9px" />');
            }
        }
    }

    if (displayRating == '') {
        document.write('<br /><div  id="'+idName+'_showrating" >'+displayRating+'</div>');
    }
    else {
        document.write('<br /><div  id="'+idName+'_showrating" >'+totalRating+'</div>');
    }
    document.write('</center>');

}
