function loadAds(){

	url = '/CEconomist/load-ads.cgi';
	xmlGet(url, function(){ showAds(); });

};

function showAds(){
	if (req.readyState == 4){
		if (req.status == 200){
			document.getElementById('banner-width').innerHTML = req.responseText;
		}
	}
};

function submitConcern(){

	url = '/CEconomist/submit-concern.cgi';
	data = 'ratingId=' + document.concernForm.ratingId.value;
	data = data + '&concern=' + encodeURIComponent(document.concernForm.concern.value);
	xmlPost(url, data, function(){ confirmConcern(); });

};

function confirmConcern(){

	if (req.readyState == 4){
		if (req.status == 200){
			document.getElementById('concern-content').style.display = "none";
			document.getElementById('concern-form').style.display = "none";
			document.getElementById('concern-submitted').style.display = 'block';
		}else{
			if (req.status == 400){
				alert("Invalid method.");
			}else{
				if (req.status == 404){
					alert("Link not found.");
				}else{
					if (req.status == 405){
						alert("Invalid Referrer");
					}
				}
			}
		}
	}
};

function reportConcern(id){

	rating = document.getElementById('user-rating-'+id);
	popuprating = document.getElementById('user-rating').innerHTML = rating.innerHTML;

    comments = document.getElementById('user-comments-'+id);
    popupcomments = document.getElementById('user-comments').innerHTML = comments.innerHTML;
    
    document.concernForm.ratingId.value = id;
    
    showWindow('fade-container', 'report-concern-popup');
    
};

function loadPolls(){

	var hasVoted = readCookie('CEVotes');

	url = '/CEconomist/load-polls.cgi?c=' + encodeURIComponent(hasVoted);
	xmlGet(url, function(){ processPolls();});
};

function processPolls(){
	if (req.readyState == 4){
		if (req.status == 200){
			pollcontent = document.getElementById('poll-content');
			pollcontent.innerHTML = req.responseText;
		}else{
			pollcontent = document.getElementById('poll-content');
			pollcontent.innerHTML = '';
		}
	}
};

function doVote(form){

	var hasVoted = readCookie('CEVotes');
	if ((hasVoted == null) || (hasVoted.indexOf('(' + form.id.value + ')') == -1)){ // not found
		for(var i=0; i<form.poll.length; i++){
			if (form.poll[i].checked){
				url = '/CEconomist/post-vote.cgi?id=' + form.id.value + '&v=' + form.poll[i].value;
				xmlGet(url, function(){ processVote(form.id.value);});
			}
		}
	}
};

function processVote(id){
	if (req.readyState == 4){
		if (req.status == 200){
			var hasVoted = readCookie('CEVotes');
			if (hasVoted == null){
				hasVoted = '(' + id + ')';
			}else{
				hasVoted = hasVoted + '(' + id + ')';
			}
			createCookie('CEVotes', hasVoted, 1068);
			loadPolls();
		}
	}
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

function eraseCookie(name) {
	createCookie(name,"",-1);
};

