﻿/* Copyright 2011 Daniel Soutter - Code-Tips.com */
function toggleRatingDetails(rating_type){
	if(document.getElementById('rating-info-' + rating_type).style.display == 'block')
		document.getElementById('rating-info-' + rating_type).style.display = 'none';
	else
		document.getElementById('rating-info-' + rating_type).style.display = 'block';
}

function toggleMoreRatings(){
	if(document.getElementById('more-ratings').style.display == 'block')
	{
		document.getElementById('more-ratings').style.display = 'none';
		document.getElementById('more-link').style.display = 'block';
		document.getElementById('less-link').style.display = 'none';
	}
	else
	{
		document.getElementById('more-ratings').style.display = 'block';
		document.getElementById('more-link').style.display = 'none';
		document.getElementById('less-link').style.display = 'block';
	}
}

window.RatingsManager = function() {
	this.submitRating = function(type,rating,button) {
		pageRequest("GET","/rating.php?type="+type+"&rating="+rating+"&loc="+encodeURIComponent(window.location),true,function(r){
		//pageRequest("GET","rating.php",true,function(r){
			var message = "no message";
			if ( typeof r.childNodes[0].textContent !== 'undefined' ) { 
				message = r.childNodes[0].textContent;
			} else if(typeof r.childNodes[0].innerText !== 'undefined') { 
				message = r.childNodes[0].innerText;
			} else if(typeof r.childNodes(0).text !== 'undefined') { 
				message = r.childNodes[0].text;
			} 
			//alert(message);
			if (r.getElementsByTagName("success").length==0) {
				//alert('failed');
				button.parentNode.innerHTML = '<span class="error">' + message + '</span>';
			} else {
				//alert('success');
				button.parentNode.innerHTML = '<span class="rated">' + message + '</span>';
			}
		});
	}
}

function pageRequest(method,url,async,runfunction) {
	try {
		if(window.XMLHttpRequest) { 
			var xmlhttp = new XMLHttpRequest(); 
		} 
		else if(window.ActiveXObject) { 
			var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	} catch (e) {
		//alert(e);	
		return false; 
	}
	xmlhttp.open(method, url, async); 
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			runfunction(xmlhttp.responseXML); 
		}
	};
	xmlhttp.send('');
	if (async==false) 
		runfunction(xmlhttp.responseXML);
}
