//call ajax script
function call_ajax(method, element_id, script_link, parameters) {
	var xmlHttp;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var result = xmlHttp.responseText;

			if (!element_id) {
				alert(result);
			} else {
				document.getElementById(element_id).innerHTML = result;
			}
		}
	}

	if (method == 'post') {
		xmlHttp.open('POST', script_link, true);
		xmlHttp.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", parameters.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(parameters);
	} else {
		xmlHttp.open('GET', script_link, true);
		xmlHttp.send(null);
	}
}

// used to auto-start function on page load
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/** ******************* custom functions ******************* */

// load rating
function load_rating() {
	// load rating for each "rating spot"
	for ( var i = 1; i <= ratings; i++) {
		// element id name
		var element_id = (i == 1) ? 'rating' : 'rating_' + i;

		// load rating
		if (document.getElementById(element_id)) {
			show_rating(i);
		}
	}
}

// show rating
function show_rating(ident) {
	// prepare variables
	var element_id = (ident == 1) ? 'rating' : 'rating_' + ident;
	var script_link = 'rating.php?ident=' + ident;

	// show 'loading'
	document.getElementById(element_id).innerHTML = '<div style="padding: 0px 0px 0px 15px; font-size:1; font-family:arial;"><img src="loading.gif" class="loading" /><font size="1" face="arial">Please wait ...</font></div>';

	// call php script
	call_ajax('get', element_id, script_link);
}

// update rating
function update_rating(ident, rating) {
	// prepare variables
	var element_id = (ident == 1) ? 'rating' : 'rating_' + ident;
	var script_link = 'rating.php?ident=' + ident + '&rating=' + rating;

	// show 'loading'
	document.getElementById(element_id).innerHTML = '<div style="padding: 0px 0px 0px 15px; font-size:1; font-family:arial;"><img src="loading.gif" class="loading" /><font size="1" face="arial">Please wait ...</font></div>';

	// call php script
	call_ajax('get', element_id, script_link);
}

/** ******************* auto-initiate function ******************* */

var ratings = 50; // max number of ratings per page

addLoadEvent(load_rating);

