

/**
 AJAX RATING SCRIPT
======================

The rating function is called using ajaxRate(ID,rating) where ID is the ID of current template
to be rated and rating is a standard rating from 0 to 5. 

An image could then be linked this way:

<a href="javascript: ajaxRate(007, 2)<img src="2-star.png" /></a>

The script has to be included in either the head or body part of the HTML file.

<script language="JavaScript" src="ajaxrate.js"></script>


**/


/**
 * OPTIONS *
 ************/
function rating_stars(number, prefix)
{
    for(var i = 1; i < 6; i++)
    {
        if(number >= i)
        {
            document.getElementById(prefix + i).src = "http://images.all4myspace.com/static/rating_star_filled.png";
        }else
        {
            document.getElementById(prefix + i).src = "http://images.all4myspace.com/static/rating_star_unfilled.png";
        }
    }
}

// url to the rating script that will update the rating in the DB
// and return the new avg. rating
var url = "/rating.php";
var url_2_0 = "/rating_2_0.php";

var request = false;

// Request senden
function setRequest() {
	// Request erzeugen
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}
}

function ajaxRate(id, rating, type, hideId, showId) {
	setRequest();

	// Check request
	if (!request) {
		alert("Couldn't create XMLHTTP-Instance");
		return false;
	} else {
		// Open request
		request.open('post', url, true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Send request 
		request.send("id="+id+"&rating="+rating+"&type="+type);
		
		document.getElementById(hideId).style.display = 'none';
		document.getElementById(showId).style.display = '';
	}
}

function ajaxRate_2_0(id, rating, type, hideId, showId) {
    setRequest();

    // Check request
    if (!request) {
        alert("Couldn't create XMLHTTP-Instance");
        return false;
    } else {
        // Open request
        request.open('post', url_2_0, true);
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Send request 
        request.send("id="+id+"&rating="+rating+"&type="+type);
        
        document.getElementById(hideId).style.display = 'none';
        document.getElementById(showId).style.display = '';
    }
}