--- loncom/interface/lonhtmlcommon.pm	2009/12/11 17:25:01	1.254
+++ loncom/interface/lonhtmlcommon.pm	2009/12/22 06:02:44	1.256
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.254 2009/12/11 17:25:01 bisitz Exp $
+# $Id: lonhtmlcommon.pm,v 1.256 2009/12/22 06:02:44 faziophi Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -383,8 +383,7 @@ dname_hour, dname_min, and dname_sec.
 
 The current setting for this time parameter.  A unix format time
 (time in seconds since the beginning of Jan 1st, 1970, GMT.  
-An undefined value is taken to indicate the value is the current time
-unless it is requested to leave it empty. See $includeempty. 
+An undefined value is taken to indicate the value is the current time.
 Also, to be explicit, a value of 'now' also indicates the current time.
 
 =item $special
@@ -394,9 +393,6 @@ the date_setter.  See lonparmset for exa
 
 =item $includeempty 
 
-If it is set (true) and no date/time value is provided,
-the date/time fields are left empty.
-
 =item $state
 
 Specifies the initial state of the form elements.  Either 'disabled' or empty.
@@ -416,40 +412,42 @@ sub date_setter {
     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
     my $now = time;
-
-    my $tzname;
-    my ($sec,$min,$hour,$mday,$month,$year) = ('','',undef,'','','');
-    # other potentially useful values:     wkday,yrday,is_daylight_savings
-
+    my $wasdefined=1;
     if (! defined($state) || $state ne 'disabled') {
         $state = '';
     }
     if (! defined($no_hh_mm_ss)) {
         $no_hh_mm_ss = 0;
     }
-    
     if ($currentvalue eq 'now') {
-        $currentvalue = $now;
+	$currentvalue = $now;
     }
-        
-    # Default value: Set empty date field to current time
-    # unless empty inclusion is requested
-    if ((!$includeempty) && (!$currentvalue)) {
-            $currentvalue = $now;
+    if ((!defined($currentvalue)) || ($currentvalue eq '')) {
+	$wasdefined=0;
+	if ($includeempty) {
+	    $currentvalue = 0;
+	} else {
+	    $currentvalue = $now;
+	}
     }
-    # Do we have a date? Split it!
+    # other potentially useful values:     wkday,yrday,is_daylight_savings
+    my $tzname;
+    my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
     if ($currentvalue) {
-        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue);
-
-        # No values provided for hour, min, sec? Use default 0
-        if (($defhour) || ($defmin) || ($defsec)) {
-            $sec  = ($defsec  ? $defsec  : 0);
-            $min  = ($defmin  ? $defmin  : 0);
-            $hour = ($defhour ? $defhour : 0);
-        }
+        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue); 
+    }
+    unless ($wasdefined) {
+        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now);
+	if (($defhour) || ($defmin) || ($defsec)) {
+	    $sec=($defsec?$defsec:0);
+	    $min=($defmin?$defmin:0);
+	    $hour=($defhour?$defhour:0);
+	} elsif (!$includeempty) {
+	    $sec=0;
+	    $min=0;
+	    $hour=0;
+	}
     }
-
-    # Create Output
     my $result = "\n<!-- $dname date setting form -->\n";
     $result .= <<ENDJS;
 <script type="text/javascript">
@@ -1243,11 +1241,18 @@ ENDLINK
 }
 
 sub htmlareaheaders {
-    return if (&htmlareablocked());
-    return if (!&htmlareabrowser());
-    return (<<ENDHEADERS);
+	my $s="";
+	if (!&htmlareablocked() && &htmlareabrowser()) {
+		$s.=(<<ENDEDITOR);
 <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
-ENDHEADERS
+<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
+ENDEDITOR
+	}
+    $s.=(<<ENDJQUERY);
+<script type="text/javascript" src="/adm/jQuery/js/jquery-1.3.2.min.js"></script>
+<script type="text/javascript" src="/adm/jQuery/js/jquery-ui-1.7.2.custom.min.js"></script>
+ENDJQUERY
+	return $s;
 }
 
 # ----------------------------------------------------------------- Preferences
@@ -1286,16 +1291,98 @@ sub htmlareaselectactive {
     my $output='<script type="text/javascript" defer="1">'."\n"
               .'// <![CDATA['."\n";
     my $lang = &htmlarea_lang();
+    $output.='
+    
+    function containsBlockHtml(id) {
+		var re = $("#"+id).html().search(/(?:\&lt\;|\<)(br|h1|h2|h3|h4|h5|h6|p|ol|ul|table|pre|address|blockquote|center|div)[\s]*((?:\/[\s]*(?:\&gt\;|\>)|(?:\&gt\;|\>)[\s\S]*(?:\&lt\;|\<)\/[\s]*\1[\s]*\(?:\&gt\;|\>))/im);
+    	return (re >= 0);
+    }
+    
+    function startRichEditor(id) {
+    	CKEDITOR.replace(id, 
+    		{
+    			customConfig: "/ckeditor/loncapaconfig.js",
+    		}
+    	);
+    }
+    
+    function destroyRichEditor(id) {
+    	CKEDITOR.instances[id].destroy();
+    }
+    
+    function editorHandler(event) {
+    	var rawid = $(this).attr("id");
+    	var id = new RegExp("LC_rt_(.*)").exec(rawid)[1]
+    	event.preventDefault();
+    	if ($(this).hasClass("LC_enable_rt")) {
+    		startRichEditor(id);
+			$("#LC_rt_"+id).html("<b>&laquo; Plain text</b>");
+			$("#LC_rt_"+id).attr("title", "Disable rich text formatting and edit in plain text");
+			$("#LC_rt_"+id).addClass("LC_disable_rt");
+			$("#LC_rt_"+id).removeClass("LC_enable_rt");
+    	} else {
+			destroyRichEditor(id);
+			$("#LC_rt_"+id).html("<b>Rich formatting &raquo;</b>");
+			$("#LC_rt_"+id).attr("title", "Enable rich text formatting (bold, italic, etc.)");
+			$("#LC_rt_"+id).addClass("LC_enable_rt");
+			$("#LC_rt_"+id).removeClass("LC_disable_rt");
+		}
+	}
+    
+    $(document).ready(function(){
+		$(".LC_richAlwaysOn").each(function() {
+			startRichEditor($(this).attr("id"));
+		});
+		$(".LC_richDetectHtml").each(function() {
+			var id = $(this).attr("id");
+			if(containsBlockHtml(id)) {
+				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
+				startRichEditor(id);
+				$("#LC_rt_"+id).click(editorHandler);				
+			}
+			else {
+				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
+				$("#LC_rt_"+id).click(editorHandler);
+			}
+		});
+		$(".LC_richDefaultOn").each(function() {
+			var id = $(this).attr("id");
+			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
+			startRichEditor(id);
+			$("#LC_rt_"+id).click(editorHandler);
+		});
+		$(".LC_richDefaultOff").each(function() {
+			var id = $(this).attr("id");
+			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
+			$("#LC_rt_"+id).click(editorHandler);			
+		});
+		
+	});
+';
+    
     foreach my $field (@fields) {
-	$output.="
+	$output.='
+	
 {
-    var oFCKeditor = new FCKeditor('$field');
-    oFCKeditor.Config['CustomConfigurationsPath'] = 
-	'/fckeditor/loncapaconfig.js';    
-    oFCKeditor.ReplaceTextarea();
-    oFCKeditor.Config['AutoDetectLanguage'] = false;
-    oFCKeditor.Config['DefaultLanguage'] = '$lang';
-}";
+	$(document).ready(function() {
+		if (!($("#'.$field.'").hasClass("LC_richAlwaysOn"))) {
+			if (!($("#'.$field.'").hasClass("LC_richAlwaysOff"))) {
+				if (!($("#'.$field.'").hasClass("LC_richDetectHtml"))) {
+					if (!($("#'.$field.'").hasClass("LC_richDefaultOn"))) {
+						if (!($("#'.$field.'").hasClass("LC_richDefaultOff"))) {
+							var oFCKeditor = new FCKeditor("'.$field.'");
+							oFCKeditor.Config["CustomConfigurationsPath"] = 
+							"/fckeditor/loncapaconfig.js";    
+							oFCKeditor.ReplaceTextarea();
+							oFCKeditor.Config["AutoDetectLanguage"] = false;
+							oFCKeditor.Config["DefaultLanguage"] = "'.$lang.'";
+						}
+					}
+				}		
+			}
+		}
+    });
+}';
     }
     $output.="\nwindow.status='Activated Editfields';\n"
             .'// ]]>'."\n"