$.fn.check = function(mode) {
	var mode = mode || 'on'; // if mode is undefined, use 'on' as default
	return this.each(function() {
		switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
		}
	});
};

$(document).ready(function(){

	// Added an asterisk to every label with the class 'required'
	$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');

});

// AJAX result message creator
var ajax_result_message_iteration = 0;
function ajax_result_message_add(ajax_result_message,ajax_result_type) {

	var ajax_result_message_id = "ajax_message_" + ajax_result_type + "_" + ++ajax_result_message_iteration;
	
	if (ajax_result_type == "success")
		var icon_src = "./images/icons/plus22.gif";
	else if (ajax_result_type == "error") 
		var icon_src = "./images/icons/minus22.gif";
	else if ($.trim(ajax_result_type).length == 0) {
		var icon_src = "./images/icons/bulb16.gif";
		ajax_result_type = "";
	}
	$("#s1").prepend("<div id='" + ajax_result_message_id + "' class='message" + (ajax_result_type != ""? " " + ajax_result_type:"") + "' style='display:none'><img src='./images/icons/action_delete1.gif' class='fltrt finger' onclick='ajax_result_message_remove(\"" + ajax_result_message_id + "\")' /><img src='" + icon_src + "' border='0' class='icon' /><span></span>&nbsp;</div>");
	
	$("#" + ajax_result_message_id + " span").html(ajax_result_message);
	
	// Animations
	if ($("#" + ajax_result_message_id + ":hidden")) $("#" + ajax_result_message_id).fadeIn(500);
	if ($("#" + ajax_result_message_id + ":hidden")) {
		$("#" + ajax_result_message_id + " span").fadeOut("fast",function(){
			$("#" + ajax_result_message_id).fadeIn("slow");
			$("#" + ajax_result_message_id).slideDown("slow");
			$("#" + ajax_result_message_id + " span").fadeIn("slow", function(){
			});
		});
	}
}
function ajax_result_message_remove(ajax_result_message) {
	$("#" + ajax_result_message).slideUp(300, function(){
		$("#" + ajax_result_message).remove();
	});
	$("#" + ajax_result_message).animate({ marginBottom: 0 }, 300);
}

// Clears form fields
function clearFields(id_name) {
	$(id_name + " input:text").val("");
	$(id_name + " textarea").val("");
	$(id_name + " :checkbox").check("off");
}

// Adds even/odd classes to elements
function addEvenOddClasses(ele_ident) {
	$(ele_ident+":even").each(function(){
		$(this).addClass("even");
		$(this).removeClass("odd");
	});
	$(ele_ident+":odd").each(function(){
		$(this).addClass("odd");
		$(this).removeClass("even");
	});
}

function nl2br(text){
	_text = escape(text);
	re_nlchar = null;
	if(_text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(_text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(_text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}
	if (re_nlchar != null) return unescape( _text.replace(re_nlchar,'<br />') )
	else return text
}