Javascript/ajax code reorganizations
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 16 May 2008 18:27:07 +0000 (18:27 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 16 May 2008 18:27:07 +0000 (18:27 +0000)
includes/current_user.inc
includes/errors.inc
includes/lang/language.php
includes/main.inc
includes/page/footer.inc
includes/page/header.inc
includes/session.inc
includes/ui/ui_controls.inc
includes/ui/ui_input.inc
includes/ui/ui_msgs.inc
includes/ui/ui_view.inc

index a913bf5e56789ef8ddfc66e85aafd6d3a8a4dc21..12d559dd178d752d4f4d48fa75c43352785256de 100644 (file)
@@ -45,14 +45,15 @@ class current_user
                {
             $myrow = db_fetch($Auth_Result);
 
-            $this->access = $myrow["full_access"];
-            $this->name = $myrow["real_name"];
-            $this->loginname = $loginname;
-            $this->username = $this->loginname;
-            $this->prefs = new user_prefs($myrow);
-
-                       update_user_visitdate($loginname);
-                       $this->logged = true;
+                   $this->access = $myrow["full_access"];
+                   $this->name = $myrow["real_name"];
+                   $this->loginname = $loginname;
+                   $this->username = $this->loginname;
+                   $this->prefs = new user_prefs($myrow);
+
+                   update_user_visitdate($loginname);
+                   $this->logged = true;
+
                } 
                else 
                {
@@ -235,6 +236,24 @@ function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl
                $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize);
 }
 
+function add_user_js_data() {
+       global $path_to_root, $thoseps, $decseps;
+
+       $ts = $thoseps[user_tho_sep()];
+       $ds = $decseps[user_dec_sep()];
+  
+    $js = "\n<script type=\"text/javascript\">\n"
+         . "<!--\n"
+         . "var user = {\n"
+         . "theme: '". $path_to_root . '/themes/'. 'default' /*user_theme()*/.'/'."',\n"
+         . "loadtxt: '"._('Requesting data...')."',\n"
+         . "ts: '$ts',\n"
+         . "ds: '$ds',\n"
+         . "pdec : " . user_price_dec() . "}\n--></script>";
+
+  add_js_source($js);
+}
+
 //--------------------------------------------------------------------------
 
 ?>
\ No newline at end of file
index ed7f281ffbc56f31bab76d7edc7e1e0713646da3..9616313f433d91e8187d54eacf25d1c0ad45b467 100644 (file)
@@ -1,25 +1,96 @@
 <?php
 
+$messages = array(); // container for system messages
+$before_box = ''; // temporary container for output html data before error box
+
+$msg_colors = array( 
+       E_USER_ERROR => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'),
+    E_USER_WARNING => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'),
+    E_USER_NOTICE => array('bg'=>'#ccffcc', 'txt'=>'#007700', 'bd'=>'#33cc00'));
+
+//-----------------------------------------------------------------------------
+//    Error handler - collects all php/user messages for
+//    display in message box.
+// FIX: fatal errors ?
+
+function error_handler($errno, $errstr, $file, $line) {
+    global $messages;
+
+       // error_reporting==0 when messages are set off with @ 
+       if ($errno & error_reporting())
+               $messages[] = array($errno, $errstr, $file, $line);
+
+    return true;
+}
+//------------------------------------------------------------------------------
+//     Formats system messages before insert them into message <div>
+// FIX center is unused now
+function fmt_errors($center=true) {
+    global $messages, $msg_colors;
+  
+  $type = E_USER_NOTICE;
+  
+  if (count($messages)) {
+       $content = '';
+       foreach($messages as $msg) {
+               if ($msg[0]>$type) continue;
+               if ($msg[0]<$type && $type>E_USER_ERROR) {
+                       $content = ''; // clean notices when we have errors
+                       $type = E_USER_ERROR; // php or user errors 
+               }
+           $str = $msg[1];
+               $c = $msg_colors[$type];
+               if ($msg[0]<E_USER_ERROR && $msg[2]!=null)
+                 $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
+               $content .= "<tr><td  " . ($center?"align='center' ":"").
+                 " width='100%' bgcolor='{$c['bg']}'><font color='{$c['txt']}'>"
+                 .$str."</font></td></tr>";
+       }
+
+       $str = "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='{$c['bd']}' width='98%'>"
+         . $content . "</table></center><br>\n";       
+               
+  }
+     else
+        $str = '';
+       
+    return $str;
+}
+//-----------------------------------------------------------------------------
+// Error box <div> element.
+//
+function error_box() {
+    global $before_box;
+    
+    echo "<div id='msgbox'>";
+    $before_box = ob_get_clean(); // save html content before error box 
+// Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
+    ob_start('output_html');
+    echo "</div>";
+}
+
 function display_db_error($msg, $sql_statement=null, $exit=true)
 {
        global $db, $debug;
        $db_error = db_error_no();
        
-       echo "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
+       $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
        
        if ($db_error != 0) 
        {
-               echo "error code : " . $db_error . "<br>";
-               echo "error message : " . db_error_msg($db) . "<br>";
+               $str .= "error code : " . $db_error . "<br>";
+               $str .= "error message : " . db_error_msg($db) . "<br>";
        }
        
        if ($debug == 1) 
        {
-               echo "sql that failed was : " . $sql_statement . "<br>";
+               $str .= "sql that failed was : " . $sql_statement . "<br>";
        }
        
-       echo "<br><br>";
-       
+       $str .= "<br><br>";
+
+       trigger_error($str, E_USER_ERROR);
+
        if ($exit)
                exit;
 }
index 876abd1544fd4bf35e79689ed6f3b38ba5019e44..629a15a0c28c176805d8837eb188b47b835eaf3b 100644 (file)
@@ -35,11 +35,16 @@ class language
 
        function set_language($code) 
        {
+           global $comp_path;
+           
                if (isset($_SESSION['languages'][$code]) &&
                        $_SESSION['language'] != $_SESSION['languages'][$code]) 
                {
-                       $_SESSION['language'] = $_SESSION['languages'][$code];
-                       reload_page("");
+           
+               // flush cache as we can use several languages in one account
+                   flush_dir($comp_path.'/'.user_company().'/js_cache');
+                   $_SESSION['language'] = $_SESSION['languages'][$code];
+                   reload_page("");
                }
        }
 
@@ -90,7 +95,7 @@ get_text::set_language($lang->code, $lang->encoding);
 //get_text::add_domain("wa", $path_to_root . "/lang");
 get_text::add_domain($lang->code, $path_to_root . "/lang");
 // Unnecessary for ajax calls. 
-// Due to bug in php 4.3.10 for this version set globally in php4.ini
+// Due to bug in php 4.3.10 for this version set globally in php.ini
 ini_set('default_charset', $_SESSION['language']->encoding);
 
 if (!function_exists("_")) 
index fdad9cb7999f691e858a4cea0ecfb0193f614fe0..2180ec05793a0b3d0ab91823227eae9f36cea835 100644 (file)
     include_once($path_to_root . "/admin/db/users_db.inc");
     include_once($path_to_root . "/includes/ui/ui_view.inc");
        
+       function output_html($text) 
+       {
+         global $before_box;
+         return  in_ajax() ? fmt_errors() : ($before_box.fmt_errors().$text);
+       }
+
     function page($title, $no_menu=false, $is_index=false, $onload="", $js="")
     {
 
-       global $path_to_root, $js_lib;
+       global $path_to_root;
 
        $hide_menu = $no_menu;
 
+               // intercept all output to destroy it in case of ajax call
+               ob_start('output_html');
+               // colect all error msgs
+               set_error_handler('error_handler' /*, errtypes */);
+
        include($path_to_root . "/includes/page/header.inc");
 
        page_header($title, $no_menu, $is_index, $onload, $js);
+               error_box();
     }
 
     function end_page($no_menu=false, $is_index=false)
     {
-       global $path_to_root;
+       global $path_to_root, $Ajax;
 
        $hide_menu = $no_menu;
 
        include($path_to_root . "/includes/page/footer.inc");
 
+               $Ajax->run();
        page_footer($no_menu, $is_index);
     }
 
                if($fname=='.' || $fname=='..') continue;
                if(is_dir($path.'/'.$fname)) {
                    flush_dir($path.'/'.$fname);
-                   rmdir($path.'/'.$fname);
+                   @rmdir($path.'/'.$fname);
                } else
-                   unlink($path.'/'.$fname);
+                   @unlink($path.'/'.$fname);
        }
     }
 
     function cache_js_file($fpath, $text) 
     {
+       global $go_debug;
 
-    // FIX compress text ...
+       if(!$go_debug) $text = js_compress($text);
 
-    $file = fopen($fpath, 'w');
-    if (!$file) return false;
-    if (!fwrite($file, $text)) return false;
-    return fclose($file);
+        $file = fopen($fpath, 'w');
+       if (!$file) return false;
+       if (!fwrite($file, $text)) return false;
+       return fclose($file);
 
     }
 
                $js_lib[] = $text;
     }
 
+/**
+ * Compresses the Javascript code for more efficient delivery.
+ * copyright (c) 2005 by Jared White & J. Max Wilson
+ * http://www.xajaxproject.org
+ * Added removing comments from output.
+ * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
+ */
+function js_compress($sJS)
+{
+       //remove windows cariage returns
+       $sJS = str_replace("\r","",$sJS);
+       
+       //array to store replaced literal strings
+       $literal_strings = array();
+       
+       //explode the string into lines
+       $lines = explode("\n",$sJS);
+       //loop through all the lines, building a new string at the same time as removing literal strings
+       $clean = "";
+       $inComment = false;
+       $literal = "";
+       $inQuote = false;
+       $escaped = false;
+       $quoteChar = "";
+       
+       for($i=0;$i<count($lines);$i++)
+       {
+               $line = $lines[$i];
+               $inNormalComment = false;
+       
+               //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
+               for($j=0;$j<strlen($line);$j++)
+               {
+                       $c = substr($line,$j,1);
+                       $d = substr($line,$j,2);
+       
+                       //look for start of quote
+                       if(!$inQuote && !$inComment)
+                       {
+                               //is this character a quote or a comment
+                               if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
+                               {
+                                       $inQuote = true;
+                                       $inComment = false;
+                                       $escaped = false;
+                                       $quoteChar = $c;
+                                       $literal = $c;
+                               }
+                               else if($d=="/*" && !$inNormalComment)
+                               {
+                                       $inQuote = false;
+                                       $inComment = true;
+                                       $escaped = false;
+                                       $quoteChar = $d;
+                                       $literal = $d;  
+                                       $j++;   
+                               }
+                               else if($d=="//") //ignore string markers that are found inside comments
+                               {
+                                       $inNormalComment = true;
+                                       $clean .= $c;
+                               }
+                               else
+                               {
+                                       $clean .= $c;
+                               }
+                       }
+                       else //allready in a string so find end quote
+                       {
+                               if($c == $quoteChar && !$escaped && !$inComment)
+                               {
+                                       $inQuote = false;
+                                       $literal .= $c;
+       
+                                       //subsitute in a marker for the string
+                                       $clean .= "___" . count($literal_strings) . "___";
+       
+                                       //push the string onto our array
+                                       array_push($literal_strings,$literal);
+       
+                               }
+                               else if($inComment && $d=="*/")
+                               {
+                                       $inComment = false;
+                                       $literal .= $d;
+       
+                                       //subsitute in a marker for the string
+                                       $clean .= "___" . count($literal_strings) . "___";
+       
+                                       //push the string onto our array
+                                       array_push($literal_strings,$literal);
+       
+                                       $j++;
+                               }
+                               else if($c == "\\" && !$escaped)
+                                       $escaped = true;
+                               else
+                                       $escaped = false;
+       
+                               $literal .= $c;
+                       }
+               }
+               if($inComment) $literal .= "\n";
+               $clean .= "\n";
+       }
+       //explode the clean string into lines again
+       $lines = explode("\n",$clean);
+       
+       //now process each line at a time
+       for($i=0;$i<count($lines);$i++)
+       {
+               $line = $lines[$i];
+       
+               //remove comments
+               $line = preg_replace("/\/\/(.*)/","",$line);
+       
+               //strip leading and trailing whitespace
+               $line = trim($line);
+       
+               //remove all whitespace with a single space
+               $line = preg_replace("/\s+/"," ",$line);
+       
+               //remove any whitespace that occurs after/before an operator
+               $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
+       
+               $lines[$i] = $line;
+       }
+       
+       //implode the lines
+       $sJS = implode("\n",$lines);
+       
+       //make sure there is a max of 1 \n after each line
+       $sJS = preg_replace("/[\n]+/","\n",$sJS);
+       
+       //strip out line breaks that immediately follow a semi-colon
+       $sJS = preg_replace("/;\n/",";",$sJS);
+       
+       //curly brackets aren't on their own
+       $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
+       
+       //finally loop through and replace all the literal strings:
+       for($i=0;$i<count($literal_strings);$i++) {
+           if (strpos($literal_strings[$i],"/*")!==false) 
+               $literal_strings[$i]= '';
+               $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
+       }
+       return $sJS;
+}
 ?>
\ No newline at end of file
index 8f3db720db2aa8563fc4d4dd36ddade677c8ada3..f0a9b65e92435dc16b97669dfa8dd0e68ccc3db5 100644 (file)
@@ -7,9 +7,9 @@ function page_footer($no_menu=false, $is_index=false)
        global $version, $allow_demo_mode;
        global $app_title, $power_url, $power_by;
 
-       if (isset($_GET['ajax'])) return;
+  if(in_ajax()) return;// just for speed up
 
-       if (function_exists('hyperlink_back'))
+  if (function_exists('hyperlink_back'))
                hyperlink_back();
        if ($no_menu == false) 
        {
index bf93e2d5e1bf15301a9c4bea8bc5a0bc3b1545a2..50c562521d2e72d7d277b42a463aecef03d4d6b4 100644 (file)
@@ -27,14 +27,41 @@ function help_url($title, $app)
        return $help_base_url.urlencode(str_replace(' ', '', ucwords($help_page_url)));
 }
 
+function send_scripts() 
+{
+    global $js_lib, $js_static, $js_path, $js_userlib, $comp_path,
+       $path_to_root, $go_debug;
+    
+       add_user_js_data();
+
+       $js ='';
+       foreach($js_static as $jsfile) {
+           $fpath = $comp_path.'/'.user_company().'/js_cache/'.$jsfile;
+           // compress also static files
+         if (!file_exists($fpath) || $go_debug)
+               cache_js_file($fpath, file_get_contents($js_path.$jsfile));
+       
+         $js .= '<script language="javascript" type="text/javascript" src="'.
+               $fpath . '"></script>';
+       }
+       foreach($js_userlib as $jsfile) {
+         $js .= '<script language="javascript" type="text/javascript" src="'.
+           $jsfile . '"></script>';
+       }
+
+       foreach($js_lib as $text) {
+         $js .= $text;
+       }
+        echo $js;
+}
+
 function page_header($title, $no_menu=false, $is_index=false, $onload="", $js="")
 {
        // titles and screen header
        global $db_connections, $path_to_root, $def_app, $applications,
-               $help_base_url, $help_page_url, $use_popup_windows,
-               $js_lib, $js_static, $js_path, $js_userlib, $comp_path;
+               $help_base_url, $help_page_url, $use_popup_windows;
 
-       if (isset($_GET['ajax'])) return;
+       if (in_ajax()) return; // just for speed up
 
        $theme = user_theme();
        $local_path_to_root = $path_to_root;
@@ -45,24 +72,11 @@ function page_header($title, $no_menu=false, $is_index=false, $onload="", $js=""
                {
                        include_once($path_to_root.'/includes/ui/ui_view.inc');
                }
-               $js = get_js_open_window(900, 500);
+               add_js_source(get_js_open_window(900, 500));
        }
+       if ($js!='')
+         add_js_source($js);
 
-       add_js_user_num(); // add user native numeric input functions
-       // javascript includes collect
-       add_js_source($js);
-       $js ='';
-       foreach($js_static as $jsfile) {
-         $js .= '<script language="javascript" type="text/javascript" src="'.
-               $js_path . $jsfile . '"></script>';
-       }
-       foreach($js_userlib as $jsfile) {
-         $js .= '<script language="javascript" type="text/javascript" src="'.
-           $jsfile . '"></script>';
-       }
-       foreach($js_lib as $text) {
-         $js .= $text;
-       }
        if (!isset($no_menu))
        {
                $no_menu = false;
@@ -89,15 +103,14 @@ function page_header($title, $no_menu=false, $is_index=false, $onload="", $js=""
        echo "<link href='$local_path_to_root/themes/$theme/default.css' rel='stylesheet' type='text/css' /> \n";
        if (file_exists($local_path_to_root. "/".$local_stylesheet))
                echo "<link href='$local_path_to_root/$local_stylesheet' rel='stylesheet' type='text/css' /> \n";
-       if ($js != "")
-               echo $js;
+       send_scripts($js);
+
        echo "</head> \n";
        if ($onload == "")
                echo "<body>";
        else
                echo "<body onload=\"$onload\">";
-       echo "<script language='javascript' src='".
-         $path_to_root. "/js/inserts.js' type='text/javascript'></script>\n";
+
        echo "<table class='callout_main' border='0' cellpadding='0' cellspacing='0'>\n";
        echo "<tr>\n";
        echo "<td colspan='2' rowspan='2'>\n";
index e8361df82db0c3221dd5cdef62b194190d5e0956..ee1ac295e2714f1b8cffd99401b5c5d0af089b82 100644 (file)
        }
        if (!isset($path_to_root) || isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
                die("Restricted access");
-       include_once($path_to_root . "/config_db.php");
        include_once($path_to_root . "/includes/lang/language.php");
+       include_once($path_to_root . "/config_db.php");
+       include_once($path_to_root . "/config.php");
 
-    include_once($path_to_root . "/config.php");
 
     include_once($path_to_root . "/includes/main.inc");
 
     }*/
 
        //----------------------------------------------------------------------------------------
+       include_once("ajax.inc");
+
+       $Ajax =& new Ajax();
+    
        check_page_security($page_security);
 
 
index 7d731525509af37f3f835a32ddf2d380ea5d99cf..ad0c8a2468e65e787d7c96bfdbe64a615abdcb54 100644 (file)
@@ -49,9 +49,11 @@ function end_table($breaks=0)
 
 function meta_forward($forward_to, $params="")
 {
+    global $Ajax;
        echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
        echo "<center><br>" . _("You should automatically be forwarded.");
        echo " " . _("If this does not happen") . " " . "<a href='$forward_to'>" . _("click here") . "</a> " . _("to continue") . ".<br><br>\n";
+       $Ajax->redirect($forward_to.'?'.$params);
        exit;
 }
 
@@ -59,8 +61,6 @@ function meta_forward($forward_to, $params="")
 
 function hyperlink_back($center=true)
 {
-       echo  get_js_go_back();
-
        if ($center)
                echo "<center>";
        echo "<a href='javascript:goBack();'>"._("Back")."</a>\n";
@@ -184,5 +184,27 @@ function br($num=1)
 {
        for ($i = 0; $i < $num; $i++)
                echo "<br>";
-}              
+}
+
+$ajax_divs = array();
+
+function div_start($id='')
+{
+    global $ajax_divs;
+
+    array_push($ajax_divs, $id);
+    echo "<div ". ($id !='' ? "id='$id'" : '').">";
+    ob_start();
+}
+
+function div_end()
+{
+    global $ajax_divs, $Ajax;
+
+    if (count($ajax_divs)) {
+       $id = array_pop($ajax_divs);
+       $Ajax->addUpdate($id, $id, ob_get_flush());
+       echo "</div>"; 
+    }
+}
 ?>
\ No newline at end of file
index 6c09d0979f83f6fcf806ed45628396a49152249a..0f20919277ad65138640d96f9a08b12e3d4d5b1c 100644 (file)
@@ -1,5 +1,18 @@
 <?php
 
+function get_post($name, $dflt='') {
+       return ((!isset($_POST[$name]) || $_POST[$name] === '') ? $dflt : $_POST[$name]);
+}
+//
+// Sets local POST value and adds Value to ajax posting if needed 
+//
+/*function set_post($name, $value, $ajax_trigger=true) {
+    global $Ajax;
+
+    $_POST[$name] = $value;
+    if ($ajax_trigger) $Ajax->activate($name);
+}
+*/
 //------------------------------------------------------------------------------
 //    Seek for _POST variable with $prefix.
 //    If var is found returns variable name with prefix stripped,
@@ -31,54 +44,58 @@ function input_num($postname=null) {
 
 function hidden($name, $value)
 {
+  global $Ajax;
        echo "<input type=\"hidden\" name=\"$name\" value=\"$value\">\n";
+       $Ajax->addUpdate($name, $name, $value);
 }
 
 //---------------------------------------------------------------------------------
 
-function submit($name, $value, $echo=true)
+function submit($name, $value, $echo=true, $async=false)
 {
        default_focus($name);
-       $submit_str = "<input type=\"submit\" class=\"inputsubmit\" name=\"$name\" value=\"$value\">\n";
+       $submit_str = "<input type=\"submit\" class=\""
+           .($async ? 'ajaxsubmit' : 'inputsubmit') 
+           ."\" name=\"$name\"  id=\"$name\" value=\"$value\" >\n";
        if ($echo)
                echo $submit_str;
        else
                return $submit_str;
 }
 
-function submit_center($name, $value, $echo=true)
+function submit_center($name, $value, $echo=true, $async=false)
 {
        echo "<center>";
-       submit($name, $value, $echo);
+       submit($name, $value, $echo, $async);
        echo "</center>";
 }
 
-function submit_center_first($name, $value)
+function submit_center_first($name, $value, $async=false)
 {
        echo "<center>";
-       submit($name, $value);
+       submit($name, $value, true, $async);
        echo "&nbsp;";
 }
 
-function submit_center_last($name, $value)
+function submit_center_last($name, $value, $async=false)
 {
        echo "&nbsp;";
-       submit($name, $value);
+       submit($name, $value, true, $async);
        echo "</center>";
 }
 
-function submit_add_or_update($add=true)
+function submit_add_or_update($add=true, $async=false)
 {
        if ($add)
-               submit('ADD_ITEM', _("Save"));
+               submit('ADD_ITEM', _("Save"), true, $async);
        else
-               submit('UPDATE_ITEM', _("Update"));
+               submit('UPDATE_ITEM', _("Update"), true, $async);
 }
 
-function submit_add_or_update_center($add=true)
+function submit_add_or_update_center($add=true, $async=false)
 {
        echo "<center>";
-       submit_add_or_update($add);
+       submit_add_or_update($add, $async);
        echo "</center>";
 }
 
@@ -90,24 +107,24 @@ function submit_add_or_update_row($add=true)
        echo "</td></tr>\n";
 }
 */
-function submit_add_or_update_row($add=true, $right=true, $extra="")
+function submit_add_or_update_row($add=true, $right=true, $extra="", $async=false)
 {
        echo "<tr>";
        if ($right)
                echo "<td>&nbsp;</td>\n";
        echo "<td $extra>";
-       submit_add_or_update($add);
+       submit_add_or_update($add, $async);
        echo "</td></tr>\n";
 }
 
-function submit_cells($name, $value, $extra="")
+function submit_cells($name, $value, $extra="", $async=false)
 {
        echo "<td $extra>";
-       submit($name, $value);
+       submit($name, $value, true, $async);
        echo "</td>\n";
 }
 
-function submit_row($name, $value, $right=true, $extra="")
+function submit_row($name, $value, $right=true, $extra="", $async=false)
 {
        echo "<tr>";
        if ($right)
@@ -141,6 +158,8 @@ function check_value($name)
 
 function check($label, $name, $value, $submit_on_change=false)
 {
+  global $Ajax;
+
        default_focus($name);
        if ($label)
                echo $label . "  ";
@@ -154,6 +173,7 @@ function check($label, $name, $value, $submit_on_change=false)
        if ($submit_on_change)
                echo " onclick='this.form.submit();' ";
        echo " >\n";
+       $Ajax->addUpdate($name, $name, $value);
 }
 
 function check_cells($label, $name, $value, $submit_on_change=false)
@@ -179,33 +199,41 @@ function labelheader_cell($label, $params="")
        echo "<td class='tableheader' $params>$label</td>\n";
 }
 
-function label_cell($label, $params="")
+function label_cell($label, $params="", $id=null)
 {
+    global $Ajax;
+    
+       if(isset($id)) {
+           $params .= " id='$id'";
+           $Ajax->addUpdate($id, $id, $label);
+       }
        echo "<td $params>$label</td>\n";
+
+       return $label;
 }
 
-function amount_cell($label, $bold=false)
+function amount_cell($label, $bold=false, $params="", $id=null)
 {
        if ($bold)
-               label_cell("<b>".price_format($label)."</b>", "nowrap align=right");
+               label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
        else
-               label_cell(price_format($label), "nowrap align=right");
+               label_cell(price_format($label), "nowrap align=right ".$params, $id);
 }
 
-function percent_cell($label, $bold=false)
+function percent_cell($label, $bold=false, $id=null)
 {
        if ($bold)
-               label_cell("<b>".percent_format($label)."</b>", "nowrap align=right");
+               label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
        else
-               label_cell(percent_format($label), "nowrap align=right");
+               label_cell(percent_format($label), "nowrap align=right", $id);
 }
 
-function qty_cell($label, $bold=false, $dec=null)
+function qty_cell($label, $bold=false, $dec=null, $id=null)
 {
        if ($bold)
-               label_cell("<b>".qty_format($label, $dec)."</b>", "nowrap align=right");
+               label_cell("<b>".qty_format($label, $dec)."</b>", "nowrap align=right", $id);
        else
-               label_cell(qty_format($label, $dec), "nowrap align=right");
+               label_cell(qty_format($label, $dec), "nowrap align=right", $id);
 }
 
 function label_cells($label, $value, $params="", $params2="")
@@ -228,6 +256,8 @@ function label_row($label, $value, $params="", $params2="", $leftfill=0)
 
 function text_cells($label, $name, $value, $size="", $max="", $params="", $post_label="", $disabled="")
 {
+  global $Ajax;
+
        default_focus($name);
        if ($label != null)
                label_cell($label, $params);
@@ -241,10 +271,13 @@ function text_cells($label, $name, $value, $size="", $max="", $params="", $post_
                echo " " . $post_label;
 
        echo "</td>\n";
+       $Ajax->addUpdate($name, $name, $value);
 }
 
 function text_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null)
 {
+  global $Ajax;
+
        default_focus($name);
        if (!isset($_POST[$name]) || $_POST[$name] == "")
        {
@@ -267,6 +300,7 @@ function text_cells_ex($label, $name, $size, $max=null, $init=null, $params=null
                echo " " . $post_label;
 
        echo "</td>\n";
+       $Ajax->addUpdate($name, $name, $_POST[$name]);
 }
 
 function text_row($label, $name, $value, $size, $max, $params="", $post_label="")
@@ -361,6 +395,8 @@ function percent_row($label, $name, $init=null)
 
 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
 {
+global $Ajax;
+
        if (!isset($dec))
          $dec = user_price_dec();
        if (!isset($_POST[$name]) || $_POST[$name] == "")
@@ -384,6 +420,7 @@ function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=nu
                echo " " . $post_label;
 
        echo "</td>\n";
+       $Ajax->addUpdate($name, $name, $_POST[$name]);
 }
 
 
@@ -458,12 +495,15 @@ function small_qty_cells($label, $name, $init=null, $params=null, $post_label=nu
 
 function textarea_cells($label, $name, $value, $cols, $rows, $params="")
 {
+  global $Ajax;
+  
        default_focus($name);
        if ($label != null)
                echo "<td $params>$label</td>\n";
        if ($value == null)
                $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
        echo "<td><textarea name='$name' cols='$cols' rows='$rows'>$value</textarea></td>\n";
+       $Ajax->addUpdate($name, $name, $value);
 }
 
 function textarea_row($label, $name, $value, $cols, $rows, $params="")
@@ -477,6 +517,8 @@ function textarea_row($label, $name, $value, $cols, $rows, $params="")
 
 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
 {
+  global $Ajax;
+  
        default_focus($name);
        echo "<tr><td>$label</td>\n";
        echo "<td>";
@@ -488,6 +530,7 @@ function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $
        submit($input_name, $input_value);
 
        echo "</td></tr>\n";
+       $Ajax->addUpdate($name, $name, $value);
 }
 
 //-----------------------------------------------------------------------------------
index 0120b7f88f8ee7880f774ee5861eef3825b55152..a0c1aff4252b960cfdaf7c6b9342dc15cd2bf989 100644 (file)
@@ -2,22 +2,17 @@
 
 function display_error($msg, $center=true)
 {
-    echo "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#CC3300' width='98%'>
-      <tr>
-        <td  " . ($center?"align='center' ":"") . " width='100%' bgcolor='#ffcccc'><font color='#dd2200'>$msg</font></td>
-      </tr>
-    </table></center><br>\n";  
-       
-       //echo "<span class='errortext'><B>" . _("ERROR :") . "</B> $msg</span><BR>";
+       trigger_error($msg, E_USER_ERROR);
 }
 
 function display_notification($msg, $center=true)
 {
-    echo "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#33cc00' width='98%'>
-      <tr>
-        <td " . ($center?"align='center' ":"") . " width='100%' bgcolor='#ccffcc'><font color='#007700'>$msg</font></td>
-      </tr>
-    </table></center><br>\n";  
+       trigger_error($msg, E_USER_NOTICE);
+}
+
+function display_warning($msg, $center=true)
+{
+       trigger_error($msg, E_USER_WARNING);
 }
 
 function display_notification_centered($msg)
index ddd62fef061c4a720122da39805cb9415ebf9c0b..6ef7655f16cf8e38299946854f5f66a5c0635e7a 100644 (file)
@@ -274,6 +274,8 @@ function get_trans_view_str($type, $trans_no, $label="")
 
 function exchange_rate_display($from_currency, $to_currency, $date_, $buttons=true)
 {
+    global $Ajax;
+    
        if ($from_currency != $to_currency)
        {
                if ($buttons && isset($_POST['get_rate']))
@@ -290,12 +292,16 @@ function exchange_rate_display($from_currency, $to_currency, $date_, $buttons=tr
                                add_exchange_rate($currency, $date_, $rate, $rate);
                        if ($from_currency == $comp_currency)
                                $rate = 1 / $rate;
+                       $Ajax->activate('get_rate');
                }
                else
                        $rate = get_exchange_rate_from_to($to_currency, $from_currency, $date_);
                $rate = number_format2($rate, user_exrate_dec());
-       label_row(_("Exchange Rate:"),"1 " . $from_currency . " = " .  $rate . " " . $to_currency .
-                       ($buttons?"  " . submit('get_rate',_("Get"), false):""));
+       label_row(_("Exchange Rate:"),"1 " . $from_currency . " = <span id='_ex_rate'>" . $rate . "</span> " . $to_currency .
+                       ($buttons?"  " . submit('get_rate',_("Get"), false, true):""));
+           if($buttons) {
+                 $Ajax->addUpdate('get_rate','_ex_rate', $rate);
+               }
        }
 }
 
@@ -523,22 +529,6 @@ function display_allocations_from($person_type, $person_id, $type, $type_no, $to
        }
 }
 
-function get_js_go_back()
-{
-       $js = "\n<script type=\"text/javascript\">\n"
-               . "<!--\n"
-               . "function goBack()\n"
-               . "{\n"
-               . "     if (window.history.length <= 1)\n"
-               . "      window.close();\n"
-               . "     else\n"
-               . "      window.history.go(-1);\n"
-               . "}\n"
-               . "-->\n"
-               . "</script>\n";
-       return $js;
-}
-
 function get_js_open_window($width, $height)
 {
        $js = "\n<script type=\"text/javascript\">\n"
@@ -554,46 +544,22 @@ function get_js_open_window($width, $height)
        return $js;
 }
 
-//$focus_on = array();
 /*
   Setting focus on element $name in $form.
   If $form<0 $name is element id.
 */
 function set_focus($name, $form_no=0) {
-//  global $focus_on;
-//  $focus_on['name'] = $name;
-//  $focus_on['form'] = $form_no;
     $_POST['_focus'] = $name;
 }
 //
 //     Set default focus on first field $name if not set yet
 //
 function default_focus($name, $form_no=0) {
-//  global $focus_on;
-//    if (!isset($focus_on['name']) && (!isset($_POST['_focus']) || $_POST['_focus']=='') ) {
-//echo "<h2>$name</h2>";
-//     $focus_on['name'] = $name;
-//     $focus_on['form'] = $form_no;
-//    }
     if (!isset($_POST['_focus'])) {
        $_POST['_focus'] = $name;
     }
 }
-/*
-function set_js_focus() {
-  global $focus_on;
-  $js = '';
-
-//  if (isset($focus_on['name'])) {
-
-    $js =  "<script type=\"text/javascript\">\n"
-       ."Behaviour.addLoadEvent(function(){ setFocus('".$focus_on['name']."'".( $focus_on['form']>=0 ?  (",".$focus_on['form']): '').
-       ");});"
-       ."</script>";
-//  }
-  return $js;
-}
-*/
+
 function get_js_png_fix()
 {
        $js = "<script type=\"text/javascript\">\n"
@@ -1066,101 +1032,6 @@ document.write(\"<div id='CC'></div>\");";
  return '';
 }
 
-//
-//     Javascript conversions to/from user numeric format.
-//
-function add_js_user_num() {
-
-    global $comp_path;
-    $fpath = $comp_path.'/'.user_company().'/js_cache/'.'user_num.js';
-
-    if (!file_exists($fpath)) {
-    
-       global $thoseps, $decseps;
-       $ts = $thoseps[user_tho_sep()];
-       $ds = $decseps[user_dec_sep()];
-
-       $js = "function price_format(post, num, dec, label) {
-       //num = num.toString().replace(/\\$|\\,/g,'');
-       if(isNaN(num))
-               num = \"0\";
-       sign = (num == (num = Math.abs(num)));
-       if(dec<0) dec = 2;
-       decsize = Math.pow(10, dec);
-       num = Math.floor(num*decsize+0.50000000001);
-       cents = num%decsize;
-       num = Math.floor(num/decsize).toString();
-       for( i=cents.toString().length; i<dec; i++){
-               cents = \"0\" + cents;
-       }
-       for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
-               num = num.substring(0,num.length-(4*i+3))+'".$ts."'+
-                       num.substring(num.length-(4*i+3));
-        num = ((sign)?'':'-') + num;
-        if(dec!=0) num = num + '".$ds."' + cents;
-       if(label)
-           document.getElementById(post).innerHTML = num;
-       else
-           document.getElementsByName(post)[0].value = num;
-       }
-       function get_amount(doc, label) {
-           if(label)
-               var val = document.getElementById(doc).innerHTML;
-           else
-               var val = document.getElementsByName(doc)[0].value;
-               val = val.replace(/\\".$ts."/g,'');
-               val = val.replace(/\\".$ds."/g,'.');
-               return 1*val;
-       }
-       ";
-
-     cache_js_file($fpath, $js);
-    } 
-    add_js_ufile($fpath);
-}
-
-function add_js_allocate() {
-    global $comp_path;
-    $fpath = $comp_path.'/'.user_company().'/js_cache/'.'allocate.js';
-
-    if (!file_exists($fpath)) {
-    
-       $js =
-       "function allocate_all(doc) {
-       var amount = get_amount('amount'+doc);
-       var unallocated = get_amount('un_allocated'+doc);
-       var total = get_amount('total_allocated', 1);
-       var left = get_amount('left_to_allocate', 1);
-
-       if(unallocated<amount) amount = unallocated;
-       if((unallocated-amount)<=left){
-           left-=unallocated-amount;
-           total+=unallocated-amount;
-           amount=unallocated;
-       }else{
-       total+=left;
-       amount+=left;
-       left=0;
-       }
-       price_format('amount'+doc, amount, ".user_price_dec().");
-       price_format('left_to_allocate', left, ".user_price_dec().", 1);
-       price_format('total_allocated', total, ".user_price_dec().", 1);
-       }
-       function allocate_none(doc) {
-       amount = get_amount('amount'+doc);
-       left = get_amount('left_to_allocate', 1);
-       total = get_amount('total_allocated', 1);
-       price_format('left_to_allocate',amount+left, ".user_price_dec().", 1);
-       price_format('amount'+doc, 0, ".user_price_dec().");
-       price_format('total_allocated', total-amount, ".user_price_dec().", 1);
-       }";
-
-     cache_js_file($fpath, $js);
-    } 
-    add_js_ufile($fpath);
-    add_js_user_num();
-}
-
 function alert($msg)
 {
        echo "\n<script type=\"text/javascript\">\n"