--- templates.php	2010-03-09 01:40:00.000000000 +0530
+++ /var/www/limesurvey1/admin/templates.php	2010-03-27 13:44:58.900728052 +0530
@@ -980,7 +980,7 @@ if (is_template_editable($templatename)=
     ."<tr><td>"
     ."<input type='submit' value='".$clang->gT("Delete")."' onclick=\"javascript:return confirm('".$clang->gT("Are you sure you want to delete this file?","js")."')\"";
     if (!is_template_editable($templatename))  {
-		    $templatesoutput.= " style='color: #BBBBBB;' disabled='disabled' alt='".$clang->gT("Files in a standard template cannot be deleted.")."'";
+$templatesoutput.= " style='color: #BBBBBB;' disabled='disabled' alt='".$clang->gT("Files in a standard template cannot be deleted.")."'";
     }
     $templatesoutput.= " />\n"
     ."<input type='hidden' name='screenname' value='".html_escape($screenname)."' />\n"
@@ -1026,22 +1026,26 @@ $templatesoutput.= "\t<div class='header
 
 // The following lines are forcing the browser to refresh the templates on each save
 $time=date("ymdHis");
-$fnew=fopen("$tempdir/template_temp_$time.html", "w+");
-fwrite ($fnew, getHeader());
+@$fnew=fopen("$tempdir/template_temp_$time.html", "w+"); 
+@fwrite ($fnew, getHeader());
 foreach ($cssfiles as $cssfile)
 {
     $myoutput=str_replace($cssfile['name'],$cssfile['name']."?t=$time",$myoutput);
 }
-
 foreach($myoutput as $line) {
-	fwrite($fnew, $line);
+	@fwrite($fnew, $line);
 }
-fclose($fnew);
+@fclose($fnew);
 $langdir_template="$publicurl/locale/".$_SESSION['adminlang']."/help";
-$templatesoutput.= "<p>\n"
-."<iframe id='previewiframe' src='$tempurl/template_temp_$time.html' width='95%' height='768' name='previewiframe' style='background-color: white;'>Embedded Frame</iframe>\n"
-."</p></div>\n";
+if(!$fnew){
+	echo "<center><h2>";
+	echo sprintf($clang->gT("Please change the directory permission to writable for the directory %s"), $tempdir);
+	echo "</center></h2>";
 }
+else
+{
+$templatesoutput.= "<p>\n"."<iframe id='previewiframe' src='$tempurl/template_temp_$time.html' width='95%' height='768' name='previewiframe' style='background-color: white;'>Embedded Frame</iframe>\n"."</p></div>\n";
+}}
 
 function doreplacement($file) { //Produce sample page from template file
 	$output=array();
