/********************************************************************************
* dodeley newsletter system                                                     *
* Copyright 2008 - 2011 indual GmbH (www.indual.ch)                             *
********************************************************************************/


/*** load tiny mce wysiwyg editor ***/
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	language: "de",
	editor_selector : "wysiwyg-big",
	editor_deselector : "no-wysiwyg-big",
	plugins : "paste,table,advhr,advimage,searchreplace,dodeley",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,bullist,numlist,undo,redo,separator,link,unlink,separator,cleanup,removeformat,code",
	theme_advanced_buttons2 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	content_css: "template/tinymce.css",
	paste_text_sticky: true,
	
	setup: function(ed) {
		ed.onInit.add(function(ed) {
			ed.pasteAsPlainText = true;
		});
	}
});

tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	language: "de",
	editor_selector : "wysiwyg-big-tags",
	editor_deselector : "no-wysiwyg-big-tags",
	plugins : "paste,table,advhr,advimage,searchreplace,dodeley",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,bullist,numlist,undo,redo,separator,link,unlink,separator,cleanup,removeformat,code,dodeley",
	theme_advanced_buttons2 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	content_css: "template/tinymce.css",
	paste_text_sticky: true,
	
	setup: function(ed) {
		ed.onInit.add(function(ed) {
			ed.pasteAsPlainText = true;
		});
	}
});


function ask(question, href) {
	if(confirm(question) == true) {
		window.location.href = href;
	}
}


function submit_filter_form() {
	document.filter.submit();
}


function submit_selection_form() {
	document.selection.submit();
}


function show_element(id_checker, id_element) {
	if (document.getElementById(id_checker).checked == true) {
		document.getElementById(id_element).style.display = 'block';
	}
	else {
		document.getElementById(id_element).style.display = 'none';
	}
}


function preview_popup(url) {
    var options = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0";
    var width = 800;
    var height = 800;
    new_window = window.open(url, '', (options + ',width=' + width + ',height=' + height));
    
    new_window.focus();
}


function select_all_record_checkboxes() {
	var records = document.getElementsByName('record');

	for (i = 0; i < records.length; i++) {
		if (document.getElementsByName('record')[i].checked == true) {
			document.getElementsByName('record')[i].checked = false;
		}
		else {
			document.getElementsByName('record')[i].checked = true;
		}
	}
}


function get_marked_records() {
	records = document.getElementsByName('record');
	ids = '';
	for (i = 0; i < records.length; ++i) {
		if (records[i].checked) {
			ids = ids + records[i].value + ',';
		}
	}
	return ids;
}


function toggle_checkbox(id, id_colorchange, color_true, color_false) {
	element = $(id);
	
	if (element.checked) {
		element.checked = false;
		if (isset(id_colorchange) && isset(color_true)) {
			$(id_colorchange).style.backgroundColor = color_true;
		}
	}
	else {
		element.checked = true;
		if (isset(id_colorchange) && isset(color_false)) {
			$(id_colorchange).style.backgroundColor = color_false;
		}
	}
}


function toggle_radio(id, id_colorchange, color_true, color_false) {
	element = $(id);
	
	if (!element.checked) {
		element.checked = true;
		if (isset(id_colorchange) && isset(color_false)) {
			$(id_colorchange).style.backgroundColor = color_false;
		}
	}
}


// ajax_count_sending_recipients
function count_sending_recipients() {
	// get recipient_list_ids
	var recipient_list_ids = '';
	$$('.recipient_list_checkbox_input').each(function(s) {
		if ($(s).checked == true) {
			recipient_list_ids += $(s).id.substring(18) + ',';
		}
	});
	
	// get filter
	var filter_language 		= $('filter_language').value;
	var filter_country_code 	= $('filter_country_code').value;
	// var filter_email 		= $('filter_email').value;
	var filter_email = '';
	
	// get count sending recipients
	new Ajax.Request(		'index.php?action=ajax_count_sending_recipients&recipient_list_ids=' + recipient_list_ids + '&filter_language=' + filter_language + '&filter_country_code=' + filter_country_code + '&filter_email=' + filter_email,
							{asynchronous: true, onComplete: insert_count_sending_recipients}
							);
	
	function insert_count_sending_recipients(request) {
		$('count_sending_recipients').innerHTML = request.responseText;
	}
}


function colorize_background(element, reset_elements_as_classes, standard_color, colorize_color) {
	// reset all colors of all elements
	$$('.' + reset_elements_as_classes).each(function(s) {
		$(s).style.backgroundColor = standard_color;
	});
	
	// set new color
	$(element).style.backgroundColor = colorize_color;
}





