var myNoticesEncoding = 'iso-8859-1';

function getSendToFriendForm(link) {
	
	var url = '/client/avui_cat/components/noticies/templates/sendToFriend.php';
	var params = 'link=' + link; 

	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			encoding: myNoticesEncoding,
			parameters: params,
			onSuccess: init_sendToFriend
		});
}

function init_sendToFriend(originalRequest) {
	$('login_content').style.display ="block";
	$(document.body).style.overflow ="hidden";
	$('login_content').innerHTML = originalRequest.responseText;
	$('sendFriend_ko_error').hide();
}

function sendToFriend() {
		
	var url = '/client/avui_cat/components/noticies/behaviour/sendNoticeToFriend.php';
	
	try {
		var name = $F('sendFriendName');
		var email = $F('sendFriendMail');
		var comment = $F('sendFriendComment');
		var link = $F('sendFriendLink');
		var captcha = $F('sendFriendCaptcha');
		
		var params = 'name=' + name + '&email=' + email + '&comment=' + comment + '&link=' + link + '&captcha=' + captcha;

		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				encoding: myNoticesEncoding,
				parameters: params,
				onComplete: function(transport) {
    				if (200 == transport.status) { sendToFriend_ok(transport.responseText); }
    				else { sendToFriend_ko(transport.responseText); }
  					}
			});
	}
	
	catch(error) {
		sendToFriend_ko('No es pot enviar la notícia');
	}
}

function sendToFriend_ok(responseText) {
	$('login_content').innerHTML = responseText;
}

function sendToFriend_ko(responseText) {
	$('sendFriend_ko_error').innerHTML = responseText;
	$('sendFriend_ko_error').show();
}

function getCorrection(link) {
	
	var url = '/client/avui_cat/components/noticies/templates/sendCorrection.php';
	var params = 'link=' + link;
	
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: params,
			encoding: myNoticesEncoding,
			onSuccess: init_sendCorrection
		});
}

function init_sendCorrection(originalRequest) {
	$('login_content').style.display ="block";
	$(document.body).style.overflow ="hidden";
	$('login_content').innerHTML = originalRequest.responseText;
	$('sendCorrection_ko_error').hide();
}

function sendCorrection() {
	
	var url = '/client/avui_cat/components/noticies/behaviour/sendNoticeCorrection.php';
	
	try {
		var comment = $F('sendCorrectionComment');
		var link = $F('sendCorrectionLink');
		var captcha = $F('sendCorrectionCaptcha');
		
		var params = 'comment=' + comment + '&captcha=' + captcha + '&link=' + link; // Falta el link de la notícia
		
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				encoding: myNoticesEncoding,
				parameters: params,
				onComplete: function(transport) {
    				if (200 == transport.status) { sendCorrection_ok(transport.responseText); }
    				else { sendCorrection_ko(transport.responseText); }
  					}
			});
	}
	
	catch(error) {
		sendCorrection_ko('No es pot enviar la rectificació');
	}	
	
}

function sendCorrection_ok(responseText) {
	$('login_content').innerHTML = responseText;
}

function sendCorrection_ko(responseText) {
	$('sendCorrection_ko_error').innerHTML = responseText;
	$('sendCorrection_ko_error').show();
}

function close_window() {
// Funció genèrica per tancar totes les finestres
	$('login_content').innerHTML= "";
	$('login_content').style.display="none";
	$(document.body).style.overflow ="";
}