/** --------------- Comments functions ---------------------- **/
function contact_resetCapctha(){
   $('captcha').src = '/public/media/captcha/securimage_show.php?sid=' + Math.random(); 
   $('validation').value = '';
   return false;    
}

function sendComment() {
	// comrpvoacions: ja les fa el validation.js
	$('error').hide();
	// enviar
	$('frm_comment').request({
		onSuccess: function(transport) {
			var response = transport.responseText || "no response text";
			if (response == 'OK') {
				$('info').update('Comentari enviat correctament').show();
				$('bt_save_comment').hide();
			}
			else {
				//$('info').update(response).show();
				$('error').update('Error: El text i la imatge no coincideixen').show();
				$('bt_save_comment').update('Envia comentari');
				$('bt_save_comment').enable();
				contact_resetCapctha();
			}
		},
		onFailure: function() { alert('Error calling Ajax...') },
		onLoading: function() { $('bt_save_comment').disable(); $('bt_save_comment').update('Enviant...');}
	});
	return false;
}

function correctColumnHeights() {
	/* atencio control de les altures */
	$('col_cen').style.height = 'auto';
	$('col_der').style.height = 'auto';
	BoxHeights.equalise('col_der','col_cen');
}

function showAllComments(idNot) {
	
	new Ajax.Updater('comments', '/ajax/comentari/?action=seeall', {
		parameters: {idobj:idNot},
		onComplete: function(transport) {correctColumnHeights();}
		}
	);
}

/** --------------- AGENDA --------------- */
function showMoreInfoEventAgenda(id) {
	$('agmoreinfo' + id).toggle();
	correctColumnHeights();
}

/** -------------------- recomana -----------------*/
function sendRecommend() {
	// enviar
	$('frm_recommend').request({
		onSuccess: function(transport) {
			var response = transport.responseText || "no response text";
			if (response == 'OK') {
				$('rec_sendok').show();
				$('bt_send_recommend').hide();
			}
			else {
				$('rec_sendok').update(response).show();
				$('bt_send_recommend').enable();
			}
		},
		onFailure: function() { alert('Error calling Ajax...') },
		onLoading: function() { $('bt_send_recommend').disable(); $('bt_send_recommend').value='Enviant...'; }
	});
	return false;
}

/* --------------- votacios ------------------ */

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
	if($(el).type && $(el).type.toLowerCase() == 'radio') {
		 var radioGroup = $(el).name;
		 var el = $(el).form;
	} else if ($(el).tagName.toLowerCase() != 'form') {
		 return false;
	}

	var checked = $(el).getInputs('radio', radioGroup).find(
		 function(re) {return re.checked;}
	);
	return (checked) ? $F(checked) : null;
}

function voteSurvey() {
	var idSurvey = $('survey_id').value;
	var idOption = $RF('frm_survey', 'survey_option');

	if (idOption > 0) {
		// testejar cookies
		var jar = new CookieJar("survey", {expires:3600*24*364, path: "/"});
		if (jar.enable()) {
			if (jar.get('voted'+idSurvey) != 1) { // comprovar que no s'ha votat
				$('bt_vote_survey').value="Enviant...";
				// guardar vot
				new Ajax.Request("/ajax/enquesta/?action=vote", {
					method:'get',
					parameters: {survey:idSurvey , option:idOption},
					onSuccess: function(transport) {
						var result;
						result =  transport.responseText.evalJSON();
						if (result.CODE == 'OK') {
							// guardar cookie
							jar.put('voted'+idSurvey, 1);
							// presentar imatge
							//alert(result.IMG);
							$('cont_result_survey').insert(new Element('img', {src: encodeURI(result.IMG)}));
							$('cont_frm_survey').hide();
							$('cont_result_survey').show();
							// actualitzar participants
							$('nvotes_survey').update(result.CNT);
						}
					},
					onFailure: function() { alert('Error calling Ajax...') }
				});
			}
			else { // ja votat
				$('bt_vote_survey').hide();
				$('cont_ko_survey').show();				
			}
		}
		else {
			$('bt_vote_survey').hide();
			$('cont_ko_survey').show();
		}
	}
	else {
		alert("Ha d'escollir una opció per poder votar");
	}
	
}