/*http://www.prototypejs.org/learn*/

var ajax_action_handler_url = 'http://'+window.location.hostname+'/subportals1/ajax_action.php';

function ajax_info (display) {
	if (display == 1) {
		document.getElementById('ajax_waiting').style.display = '';
	} else {
		document.getElementById('ajax_waiting').style.display = 'none';
	}
}


function add_blocked_words (ajax_action, title, e) {

	if (test_right_click(e)) {
	
		if (document.selection && document.selection.createRange) {
			// the Internet Explorer 4.0x code
			var range = document.selection.createRange();
		    var value = range.text;		
			
			if ((value != '')) {
				write_blocked_words(ajax_action, title, value);
			}
		} else if (document.getSelection && (document.getSelection() != '')) {
			// the Navigator 4.0x code
			write_blocked_words(ajax_action, title, document.getSelection());
		} else {
		}	
		
	}	
}

function write_blocked_words (ajax_action, title, value) {
	
	var reply = prompt(title, value);
	if (reply) {
		ajax_info(1);
		var url = ajax_action_handler_url;
		var pars =
				'ajax_action=' + ajax_action +  
				'&term=' + reply + 
				''
		;
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: pars,
				onComplete: response_write_blocked_words,
				evalScripts: true
			}
		);
		
	}	
}

function response_write_blocked_words (response) {
	//alert(response.responseText);
	
	if (response.responseText) {
		ajax_info(0);
		alert(response.responseText);		
	}	
}


/************************************************************************************************************************************************************************************************
 * MAIL MESSAGE TEMPLATE
 */
function get_mail_message_template (ajax_action, id_communication) {
	ajax_info(1);
	
	var source = document.getElementById('id_message_tpl_' + id_communication);
	var url = ajax_action_handler_url;
	var pars =
			'ajax_action=' + ajax_action +  
			'&id_message_tpl=' + source.value + 
			'&id_target=' + 'message_text_' + id_communication + 
			''
	;
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: response_mail_message_template_handler,
			evalScripts: true
		}
	);
}

function response_mail_message_template_handler (response) {
	//alert(response.responseText);
	
	if (response.responseText) {
		ajax_info(0);
		
		var text = response.responseXML.getElementsByTagName("text")[0].firstChild.data;
		var id_target = response.responseXML.getElementsByTagName("id_target")[0].firstChild.data;
		var target = document.getElementById(id_target);
		
		target.value = text 
			+ "\n----------------------------------------\n" 
			+ target.value;
	}	
}

/************************************************************************************************************************************************************************************************
/* REACTION MESSAGE TEMPLATE
/*/

function get_box_template (id_box, get) {
	//ajax_info(1);
	//alert(id_box); 
	var id_target = 'box_' + id_box;
	var url = ajax_action_handler_url;
	var pars = 'content_name=' + id_box + 
							  '&' + get;
	new Ajax.Updater(
		id_target, 
		url,
  		{
	    	method: 'get',
  			parameters: pars,
  			onComplete: box_template
  		}
  	);
}

function box_template (response) {
	if (response.responseText) {
		var reaction_box = document.getElementById(id_target);
		reaction_box.innerHTML = response.responseText;
	}
}


/************************************************************************************************************************************************************************************************
/* REACTION MESSAGE TEMPLATE
/*/

function get_reaction_message_template (ajax_action, id_complaining, id_communication) {
	//ajax_info(1);

	var id_target = 'reaction_' + id_communication;
	var url = ajax_action_handler_url;
	var pars =
			'ajax_action=' + ajax_action +  
			'&id_complaining=' + id_complaining + 
			'&id_communication=' + id_communication + 
			''
	;
	new Ajax.Updater(
		id_target, 
		url,
  		{
	    	method: 'get',
			parameters: pars,
			onComplete: reaction_message_template_handler
  		}
  	);
}

function reaction_message_template_handler (response) {
	//alert(response.responseText);
	if (response.responseText) {
		ajax_info(0);
		
		var id_communication = document.getElementById('id_communication').value;
		var reaction_box = document.getElementById('reaction_' + id_communication);
		var message_text_textarea = document.getElementById('message_text_' + id_communication);

		reaction_box.style.display = ''; 
		message_text_textarea.focus();		
	}
}


/************************************************************************************************************************************************************************************************
 * LAYER BOX
 */
function layer_box_window (url, height, width) {
   /**
	* new LITBox(Message or HREF, {options});
	* -----------------
	* Option Parameters
	* -----------------
    * width: pixel width
    * height: pixel height
    * type: 'window','alert','confirm'
    * func: function to call when confirming yes
    * draggable: ability to drag around screen
    * resizable: ability to resize
    * overlay: show overlay mask
    * opacity: final opacity [0,1]
    * left: how far from the left of the screen
    * top: how far from the top of the screen
	*/
	new LITBox(url, {type:'window', resizable:true, draggable:true, overlay:true, height:height, width:width});
	return false;
}

/************************************************************************************************************************************************************************************************
 * PERIODICAL UPDATER
 */

function check_user_new_message(ajax_action, object_id) {
	
	var url = ajax_action_handler_url;
	var pars =
			'ajax_action=' + ajax_action +  
			''
	;
	new Ajax.PeriodicalUpdater(
		object_id, 
		url,
  		{
	    	method: 'get',
			parameters: pars,
	    	frequency: 1,
	    	decay: 2
  		}
  	);
}


/************************************************************************************************************************************************************************************************
 * UPDATER
 */

function check_provider_by_ip(ajax_action, ip_address, object_id) {
	
	var url = ajax_action_handler_url;
	var pars =
			'ajax_action=' + ajax_action +  
			'&ip_address=' + ip_address +  
			''
	;

	new Ajax.Updater(
		object_id, 
		url, 
		{
  			method: 'get',
			parameters: pars
  		}
  	);
}  