Javascript libs caching
[fa-stable.git] / includes / ui / ui_view.inc
1 <?php
2
3 include_once($path_to_root . "/admin/db/voiding_db.inc");
4
5 //--------------------------------------------------------------------------------------
6
7 function get_supplier_trans_view_str($type, $trans_no, $label="")
8 {
9         global $path_to_root, $use_popup_windows;
10
11         $viewer = "";
12         if ($type == systypes::po())
13                 $viewer = "view_po.php";
14         elseif ($type == 20)
15                 $viewer = "view_supp_invoice.php";
16         elseif ($type == 21)
17                 $viewer = "view_supp_credit.php";
18         elseif ($type == 22)
19                 $viewer = "view_supp_payment.php";
20         elseif ($type == 25)
21                 $viewer = "view_grn.php";
22         else
23                 return null;
24
25         if ($label == "")
26                 $label = $trans_no;
27
28         if ($viewer != "")
29         {
30                 if ($use_popup_windows)
31                         $preview_str = "<a target='_blank' href='$path_to_root/purchasing/view/$viewer?trans_no=$trans_no' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$label</a>";
32                 else
33                         $preview_str = "<a target='_blank' href='$path_to_root/purchasing/view/$viewer?trans_no=$trans_no'>$label</a>";
34         }
35         else
36                 $preview_str = $label;
37
38         return $preview_str;
39 }
40
41 //--------------------------------------------------------------------------------------
42
43 function get_gl_view_str($type, $trans_no, $label="", $force=false)
44 {
45         global $path_to_root, $use_popup_windows;
46
47         if (!$force && !user_show_gl_info())
48                 return "";
49
50         if ($label == "")
51                 $label = _("GL");
52         if ($use_popup_windows)
53                 $gl_view = "<a target='_blank' href='$path_to_root/gl/view/gl_trans_view.php?type_id=$type&trans_no=$trans_no' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$label</a>";
54         else
55                 $gl_view = "<a target='_blank' href='$path_to_root/gl/view/gl_trans_view.php?type_id=$type&trans_no=$trans_no'>$label</a>";
56
57         return $gl_view;
58 }
59
60 //--------------------------------------------------------------------------------------
61
62 function get_gl_view_str_cell($type, $trans_no, $label="")
63 {
64         $str = get_gl_view_str($type, $trans_no, $label);
65         if ($str != "")
66                 return "<td>$str</td>";
67 }
68
69 //--------------------------------------------------------------------------------------
70
71 function get_customer_trans_view_str($type, $trans_no, $label="")
72 {
73         global $path_to_root, $use_popup_windows;
74
75         $viewer = "";
76         if ($type == 10)
77                 $viewer = "view_invoice.php";
78         elseif ($type == 11)
79                 $viewer = "view_credit.php";
80         elseif ($type == 12)
81                 $viewer = "view_receipt.php";
82         elseif ($type == 13)
83                 $viewer = "view_dispatch.php";
84         elseif ($type == 30)
85                 $viewer = "view_sales_order.php";
86         else
87                 return null;
88
89   if(!is_array($trans_no)) $trans_no = array($trans_no);
90
91   $lbl = $label;
92   $preview_str = '';
93   foreach($trans_no as $trans) {
94         if ($label == "")
95                 $lbl = $trans;
96
97         if($preview_str!='') $preview_str .= ',';
98
99         if ($viewer != "")
100         {
101                 if ($use_popup_windows)
102                         $preview_str .= "<a target='_blank' href='$path_to_root/sales/view/$viewer?trans_no=$trans' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$lbl</a>";
103                 else
104                         $preview_str .= "<a target='_blank' href='$path_to_root/sales/view/$viewer?trans_no=$trans'>$lbl</a>";
105         }
106         else
107                 $preview_str .= $lbl;
108   }
109
110         return $preview_str;
111 }
112
113 //--------------------------------------------------------------------------------------
114
115 function get_banking_trans_view_str($type, $trans_no, $label="")
116 {
117         global $path_to_root, $use_popup_windows;
118
119         $viewer = "";
120
121         if ($type == 4)
122                 $viewer = "bank_transfer_view.php";
123         elseif ($type == 1)
124                 $viewer = "gl_payment_view.php";
125         elseif ($type == 2)
126                 $viewer = "gl_deposit_view.php";
127         elseif ($type == 0)
128                 $viewer = "";
129         else
130                 return null;
131
132         if ($label == "")
133                 $label = $trans_no;
134
135         if ($viewer != "")
136         {
137                 if ($use_popup_windows)
138                         $preview_str = "<a target='_blank' href='$path_to_root/gl/view/$viewer?trans_no=$trans_no' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$label</a>";
139                 else
140                         $preview_str = "<a target='_blank' href='$path_to_root/gl/view/$viewer?trans_no=$trans_no'>$label</a>";
141         }
142         else
143                 $preview_str = $label;
144
145         return $preview_str;
146 }
147
148 //--------------------------------------------------------------------------------------
149
150 function get_inventory_trans_view_str($type, $trans_no, $label="")
151 {
152         global $path_to_root, $use_popup_windows;
153
154         $viewer = "";
155
156         if ($type == systypes::inventory_adjustment())
157                 $viewer = "view_adjustment.php";
158         elseif ($type == systypes::location_transfer())
159                 $viewer = "view_transfer.php";
160         else
161                 return null;
162
163         if ($label == "")
164                 $label = $trans_no;
165
166         if ($viewer != "")
167         {
168                 if ($use_popup_windows)
169                         $preview_str = "<a target='_blank' href='$path_to_root/inventory/view/$viewer?trans_no=$trans_no' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$label</a>";
170                 else
171                         $preview_str = "<a target='_blank' href='$path_to_root/inventory/view/$viewer?trans_no=$trans_no'>$label</a>";
172         }
173         else
174                 $preview_str = $label;
175
176         return $preview_str;
177 }
178
179 //--------------------------------------------------------------------------------------
180
181 function get_manufacturing_trans_view_str($type, $trans_no, $label="")
182 {
183         global $path_to_root, $use_popup_windows;
184
185         $viewer = "";
186
187         if ($type == 28)
188                 $viewer = "wo_issue_view.php";
189         elseif ($type == 29)
190                 $viewer = "wo_production_view.php";
191         elseif ($type == systypes::work_order())
192                 $viewer = "work_order_view.php";
193         else
194                 return null;
195
196         if ($label == "")
197                 $label = $trans_no;
198
199         if ($viewer != "")
200         {
201                 if ($use_popup_windows)
202                         $preview_str = "<a target='_blank' href='$path_to_root/manufacturing/view/$viewer?trans_no=$trans_no' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$label</a>";
203                 else
204                         $preview_str = "<a target='_blank' href='$path_to_root/manufacturing/view/$viewer?trans_no=$trans_no'>$label</a>";
205         }
206         else
207                 $preview_str = $label;
208
209         return $preview_str;
210 }
211
212 //--------------------------------------------------------------------------------------
213
214 function get_dimensions_trans_view_str($type, $trans_no, $label="")
215 {
216         global $path_to_root, $use_popup_windows;
217
218         $viewer = "";
219
220         if ($type == 40)
221                 $viewer = "view_dimension.php";
222         else
223                 return null;
224
225         if ($label == "")
226                 $label = $trans_no;
227
228         if ($viewer != "")
229         {
230                 if ($use_popup_windows)
231                         $preview_str = "<a target='_blank' href='$path_to_root/dimensions/view/$viewer?trans_no=$trans_no' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$label</a>";
232                 else
233                         $preview_str = "<a target='_blank' href='$path_to_root/dimensions/view/$viewer?trans_no=$trans_no'>$label</a>";
234         }
235         else
236                 $preview_str = $label;
237
238         return $preview_str;
239 }
240
241 //--------------------------------------------------------------------------------------
242
243 function get_trans_view_str($type, $trans_no, $label="")
244 {
245         $view_str = get_customer_trans_view_str($type, $trans_no, $label);
246         if ($view_str != null)
247                 return $view_str;
248
249         $view_str = get_supplier_trans_view_str($type, $trans_no, $label);
250         if ($view_str != null)
251                 return $view_str;
252
253         $view_str = get_banking_trans_view_str($type, $trans_no, $label);
254         if ($view_str != null)
255                 return $view_str;
256
257         $view_str = get_inventory_trans_view_str($type, $trans_no, $label);
258         if ($view_str != null)
259                 return $view_str;
260
261         $view_str = get_manufacturing_trans_view_str($type, $trans_no, $label);
262         if ($view_str != null)
263                 return $view_str;
264
265         $view_str = get_dimensions_trans_view_str($type, $trans_no, $label);
266         if ($view_str != null)
267                 return $view_str;
268
269         return null;
270 }
271
272 //--------------------------------------------------------------------------------------
273
274 function exchange_rate_display($from_currency, $to_currency, $date_, $buttons=true)
275 {
276         if ($from_currency != $to_currency)
277         {
278                 if ($buttons && isset($_POST['get_rate']))
279                 {
280                         $comp_currency = get_company_currency();
281                         if ($from_currency == $comp_currency)
282                                 $currency = $to_currency;
283                         else
284                                 $currency = $from_currency;
285                         $rate = get_ecb_rate($currency);
286                         if (get_date_exchange_rate($currency, $date_))
287                                 update_exchange_rate($currency, $date_, $rate, $rate);
288                         else
289                                 add_exchange_rate($currency, $date_, $rate, $rate);
290                         if ($from_currency == $comp_currency)
291                                 $rate = 1 / $rate;
292                 }
293                 else
294                         $rate = get_exchange_rate_from_to($to_currency, $from_currency, $date_);
295                 $rate = number_format2($rate, user_exrate_dec());
296         label_row(_("Exchange Rate:"),"1 " . $from_currency . " = " .  $rate . " " . $to_currency .
297                         ($buttons?"  " . submit('get_rate',_("Get"), false):""));
298         }
299 }
300
301 //--------------------------------------------------------------------------------------
302
303 function is_voided_display($type, $id, $label)
304 {
305         global $table_style;
306         $void_entry = get_voided_entry($type, $id);
307
308         if ($void_entry == null)
309                 return false;
310
311         start_table("width=50% $table_style");
312         echo "<tr><td align=center><font color=red>$label</font><br>";
313         echo "<font color=red>" . _("Date Voided:") . " " . sql2date($void_entry["date_"]) . "</font><br>";
314         if (strlen($void_entry["memo_"]) > 0)
315                 echo "<center><font color=red>" . _("Memo:") . " " . $void_entry["memo_"] . "</font><br>";
316         echo "</td></tr>";
317         end_table(1);
318
319         return true;
320 }
321
322 //--------------------------------------------------------------------------------------
323
324 function comments_display_row($type, $id)
325 {
326         $comments = get_comments($type, $id);
327         if ($comments and db_num_rows($comments))
328         {
329                 echo "<tr><td colspan=15>";
330         while ($comment = db_fetch($comments))
331         {
332                 echo $comment["memo_"] . "<br>";
333         }
334                 echo "</td></tr>";
335         }
336 }
337
338 //--------------------------------------------------------------------------------------
339
340 function get_comments_string($type, $type_no)
341 {
342         $str_return = "";
343         $result = get_comments($type, $type_no);
344         while ($comment = db_fetch($result))
345         {
346                 if (strlen($str_return))
347                         $str_return = $str_return . " \n";
348                 $str_return = $str_return . $comment["memo_"];
349         }
350         return $str_return;
351 }
352
353 //--------------------------------------------------------------------------------------
354
355 function view_stock_status($stock_id, $description=null)
356 {
357         global $path_to_root;
358         if ($description)
359                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", (user_show_codes()?$stock_id . " - ":"") . $description, "stock_id=$stock_id");
360                 $preview_str = "<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?stock_id=$stock_id' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >". (user_show_codes()?$stock_id . " - ":"") . $description."</a>";
361         else
362                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", $stock_id, "stock_id=$stock_id");
363                 $preview_str = "<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?stock_id=$stock_id' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$stock_id</a>";
364         echo $preview_str;
365 }
366
367 function view_stock_status_cell($stock_id, $description=null)
368 {
369         echo "<td>";
370         view_stock_status($stock_id, $description);
371         echo "</td>";
372 }
373
374 //--------------------------------------------------------------------------------------
375
376 function display_debit_or_credit_cells($value)
377 {
378         if ($value > 0)
379         {
380                 amount_cell($value);
381                 label_cell("");
382         }
383         elseif ($value < 0)
384         {
385                 label_cell("");
386                 amount_cell(abs($value));
387         }
388         else
389         {
390                 //label_cell("");
391                 amount_cell(0);
392                 label_cell("");
393         }
394 }
395
396 //--------------------------------------------------------------------------------------
397
398 function display_customer_trans_tax_details($tax_items, $columns)
399 {
400     while ($tax_item = db_fetch($tax_items))
401     {
402         $tax = number_format2($tax_item['amount'],user_price_dec());
403         if ($tax_item['included_in_price'])
404                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
405                         _("Amount") . ": $tax", "", "colspan=$columns align=right", "align=right");
406         else
407                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
408                         $tax, "colspan=$columns align=right", "align=right");
409     }
410 }
411
412 //--------------------------------------------------------------------------------------
413
414 function display_supp_trans_tax_details($tax_items, $columns)
415 {
416     while ($tax_item = db_fetch($tax_items))
417     {
418         $tax = number_format2(abs($tax_item['amount']),user_price_dec());
419         if ($tax_item['included_in_price'])
420                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
421                         _("Amount:") . ": $tax", "colspan=$columns align=right", "align=right");
422         else
423                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
424                         $tax, "colspan=$columns align=right", "align=right");
425     }
426 }
427
428 //--------------------------------------------------------------------------------------
429
430 function display_edit_tax_items($taxes, $columns, $tax_included, $leftspan=0)
431 {
432         $total = 0;
433
434     foreach ($taxes as $taxitem)
435     {
436         if ($tax_included)
437         {
438                 label_row(_("Included") . " " . $taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%) " .
439                         _("Amount:") . " " . number_format2($taxitem['Value'],user_price_dec()), "", "colspan=$columns align=right", "align=right",$leftspan);
440         }
441         else
442         {
443                 label_row($taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%)",
444                         number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
445                 $total +=  $taxitem['Value'];
446         }
447     }
448
449     return $total;
450 }
451
452 //--------------------------------------------------------------------------------------
453
454 function display_footer_exit()
455 {
456         global $path_to_root;
457         br(2);
458         end_page();
459         exit;
460 }
461
462 //--------------------------------------------------------------------------------------
463
464 function display_allocations($alloc_result, $total)
465 {
466         global $table_style;
467
468         if (!$alloc_result || db_num_rows($alloc_result) == 0)
469                 return;
470
471     display_heading2(_("Allocations"));
472
473     start_table("$table_style width=80%");
474
475     $th = array( _("Type"), _("Number"), _("Date"), _("Total Amount"),
476         _("Left to Allocate"), _("This Allocation"));
477         table_header($th);
478     $k = $total_allocated = 0;
479
480     while ($alloc_row = db_fetch($alloc_result))
481     {
482
483         alt_table_row_color($k);
484
485         label_cell(systypes::name($alloc_row['type']));
486         label_cell(get_trans_view_str($alloc_row['type'],$alloc_row['trans_no']));
487         label_cell(sql2date($alloc_row['tran_date']));
488         amount_cell($alloc_row['Total']);
489         //amount_cell($alloc_row['Total'] - $alloc_row['PrevAllocs'] - $alloc_row['amt']);
490         amount_cell($alloc_row['Total'] - $alloc_row['amt']);
491         amount_cell($alloc_row['amt']);
492         end_row();
493
494         $total_allocated += $alloc_row['amt'];
495     }
496     start_row();
497         label_cell(_("Total Allocated:"), "align=right colspan=5");
498         amount_cell($total_allocated);
499         end_row();
500         start_row();
501     label_cell(_("Left to Allocate:"), "align=right colspan=5");
502     amount_cell($total - $total_allocated);
503     end_row();
504
505     end_table(1);
506 }
507
508 //--------------------------------------------------------------------------------------
509
510 function display_allocations_from($person_type, $person_id, $type, $type_no, $total)
511 {
512         switch ($person_type)
513         {
514                 case payment_person_types::customer() :
515                         $alloc_result = get_allocatable_to_cust_transactions($person_id, $type_no, $type);
516                         display_allocations($alloc_result, $total);
517                         return;
518                 case payment_person_types::supplier() :
519                         $alloc_result = get_allocatable_to_supp_transactions($person_id, $type_no, $type);
520                         display_allocations($alloc_result, $total);
521                         return;
522         }
523 }
524
525 function get_js_go_back()
526 {
527         $js = "\n<script type=\"text/javascript\">\n"
528                 . "<!--\n"
529                 . "function goBack()\n"
530                 . "{\n"
531                 . "     if (window.history.length <= 1)\n"
532                 . "      window.close();\n"
533                 . "     else\n"
534                 . "      window.history.go(-1);\n"
535                 . "}\n"
536                 . "-->\n"
537                 . "</script>\n";
538         return $js;
539 }
540
541 function get_js_open_window($width, $height)
542 {
543         $js = "\n<script type=\"text/javascript\">\n"
544                 . "<!--\n"
545                 . "function openWindow(url, title)\n"
546                 . "{\n"
547                 . " var left = (screen.width - $width) / 2;\n"
548                 . " var top = (screen.height - $height) / 2;\n"
549                 . " window.open(url, title, 'width=$width,height=$height,left='+left+',top='+top+',screenX='+left+',screenY='+top+',status=no,scrollbars=yes');\n"
550                 . "}\n"
551                 . "-->\n"
552                 . "</script>\n";
553         return $js;
554 }
555
556 function get_js_form_entry($edit_name, $sel_name, $next_name)
557 {
558     global $comp_path;
559     $fpath = $comp_path.'/'.user_company().'/js_cache/form_entry.js';
560
561     if (!file_exists($fpath)) {
562     
563
564         $js = "\n<script type=\"text/javascript\">\n"
565                 . "<!--\n"
566                 . "function recalcAccounts()\n"
567                 . "{\n"
568                 . "     var myForm = document.forms[0];\n"
569                 . "     var len = myForm.$sel_name.length;\n"
570                 . "     var txt;\n"
571                 . "     var ac = myForm.$edit_name.value;\n"
572                 . "     var i;\n"
573                 . "     myForm.$sel_name.options[myForm.$sel_name.selectedIndex].selected = false;\n"
574                 . "     for (i = 0; i < len; i++)\n"
575                 . "     {\n"
576                 . "             txt = myForm.$sel_name.options[i].text;\n"
577                 . "             if (txt.indexOf(ac) == 0)\n"
578                 . "             {\n"
579                 . "                     myForm.$sel_name.options[i].selected = true;\n"
580                 . "                     break;\n"
581                 . "             }\n"
582                 . "     }\n"
583                 . "}\n"
584                 . "function setAccount(p, s)\n"
585                 . "{\n"
586                 . "     var myForm = document.forms[0];\n"
587                 . " if (p == 0 && myForm.$edit_name.value == \"\")\n"
588                 . "             return true;\n"
589                 . "     myForm.$edit_name.value = myForm.$sel_name.options[myForm.$sel_name.selectedIndex].value;\n"
590                 . " if (s)\n"
591                 . "  myForm.submit();\n"
592                 . "     myForm.$next_name.focus();\n"
593                 . "     return true;\n"
594                 . "}\n"
595                 . "-->\n"
596                 . "</script>\n";
597      cache_js_file($fpath, $js);
598     } 
599     add_js_ufile($fpath);
600
601         return '';
602 }
603
604 function get_js_set_focus($name)
605 {
606         $js = "\n<script type=\"text/javascript\">\n"
607                 . "<!--\n"
608                 . "function setFocus()\n"
609                 . "{\n"
610                 . "     document.forms[0].$name.focus();\n"
611                 . "}\n"
612                 . "-->\n"
613                 . "</script>\n";
614         return $js;
615 }
616
617 function get_js_png_fix()
618 {
619         $js = "<script type=\"text/javascript\">\n"
620                 . "function fixPNG(myImage)\n"
621                 . "{\n"
622                 . " var arVersion = navigator.appVersion.split(\"MSIE\")\n"
623                 . " var version = parseFloat(arVersion[1])\n"
624         . " if ((version >= 5.5) && (version < 7) && (document.body.filters))\n"
625         . " {\n"
626         . "  var imgID = (myImage.id) ? \"id='\" + myImage.id + \"' \" : \"\"\n"
627                 . "  var imgClass = (myImage.className) ? \"class='\" + myImage.className + \"' \" : \"\"\n"
628                 . "  var imgTitle = (myImage.title) ?\n"
629                 . "    \"title='\" + myImage.title  + \"' \" : \"title='\" + myImage.alt + \"' \"\n"
630                 . "  var imgStyle = \"display:inline-block;\" + myImage.style.cssText\n"
631                 . "  var strNewHTML = \"<span \" + imgID + imgClass + imgTitle\n"
632         . "    + \" style=\\\"\" + \"width:\" + myImage.width\n"
633         . "    + \"px; height:\" + myImage.height\n"
634         . "    + \"px;\" + imgStyle + \";\"\n"
635         . "    + \"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader\"\n"
636         . "    + \"(src=\'\" + myImage.src + \"\', sizingMethod='scale');\\\"></span>\"\n"
637                 . "  myImage.outerHTML = strNewHTML\n"
638         . " }\n"
639                 . "}\n"
640                 . "</script>\n";
641         return $js;
642 }
643
644 function get_js_date_picker()
645 {
646     global $comp_path;
647     $fpath = $comp_path.'/'.user_company().'/js_cache/'.'date_picker.js';
648
649         $js = "<style>
650 #CCIFrame { display: none; left: 0px; position: absolute; top: 0px; height: 250px; width: 250px; z-index: 99;}
651 #CC { position:absolute; background-color:#FFF; margin:0; padding:0; display:none; z-index: 100;}
652 #CC table { font-family: arial, verdana, helvetica, sans-serif;font-size: 8pt; border-left: 1px solid #336; border-right: 1px solid #336;}
653 #CC th { font-weight: normal;}
654 #CC th a { font-weight: normal; text-decoration: none; color: #FFF; padding: 1px;}
655 #CC td { text-align: center;}
656 #CC .header { background-color: #336;}
657 #CC .weekday { background-color: #DDD; color: #000;}
658 #CC .weekend { background-color: #FFC; color: #000;}
659 #CC .current { border: 1px solid #339; background-color: #336; color: #FFF;}
660 #CC .weekday,#CC .weekend,#CC .current { display: block; text-decoration: none; border: 1px solid #FFF; width: 2em;}
661 #CC .weekday:hover,#CC .weekend:hover,#CC .current:hover { color: #FFF; background-color: #336; border: 1px solid #999;}
662 #CC .previous { text-align: left;}
663 #CC .next { text-align: right;}
664 #CC .previous,#CC .next { padding: 1px 3px 1px 3px; font-size: 1.4em;}
665 #CC .previous a,#CC .next a { color: #FFF; text-decoration: none; font-weight: bold;}
666 #CC .title { text-align: center; font-weight: bold; color: #FFF;}
667 #CC .empty { background-color: #CCC; border: 1px solid #FFF;}
668 </style>
669 ";
670     add_js_source($js);
671     
672     if (!file_exists($fpath)) {
673     
674         global $dateseps, $date_system;
675
676         $how = user_date_format();                              // 0 = us/ca, 1 = eu, au, nz, 2 = jp, sw
677         $sep = $dateseps[user_date_sep()];              // date separator
678         $wstart = (($date_system == 1 || $date_system == 2) ? 6 : ($how == 0 ? 0 : 1));                 // weekstart (sun = 0, mon = 1)
679         $months = array(_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),_("July"),_("August"),_("September"),_("October"),_("November"),_("December"));
680         $wdays = array(_("Su"),_("Mo"),_("Tu"),_("We"),_("Th"),_("Fr"),_("Sa"));
681         $back = _("Back");
682         if ($date_system == 1)
683                 list($cyear, $cmon, $cday) = gregorian_to_jalali(date("Y"), date("n"), date("j"));
684         else if ($date_system == 2)
685                 list($cyear, $cmon, $cday) = gregorian_to_islamic(date("Y"), date("n"), date("j"));
686
687
688         $js = "
689 function positionInfo(object) {
690   var p_elm = object;
691   this.getElementLeft = getElementLeft;
692   function getElementLeft() {
693     var x = 0;
694     var elm;
695     if(typeof(p_elm) == 'object'){
696       elm = p_elm;
697     } else {
698       elm = document.getElementById(p_elm);
699     }
700     while (elm != null) {
701       x+= elm.offsetLeft;
702       elm = elm.offsetParent;
703     }
704     return parseInt(x);
705   }
706   this.getElementWidth = getElementWidth;
707   function getElementWidth(){
708     var elm;
709     if(typeof(p_elm) == 'object'){
710       elm = p_elm;
711     } else {
712       elm = document.getElementById(p_elm);
713     }
714     return parseInt(elm.offsetWidth);
715   }
716   this.getElementRight = getElementRight;
717   function getElementRight(){
718     return getElementLeft(p_elm) + getElementWidth(p_elm);
719   }
720   this.getElementTop = getElementTop;
721   function getElementTop() {
722     var y = 0;
723     var elm;
724     if(typeof(p_elm) == 'object'){
725       elm = p_elm;
726     } else {
727       elm = document.getElementById(p_elm);
728     }
729     while (elm != null) {
730       y+= elm.offsetTop;
731       elm = elm.offsetParent;
732     }
733     return parseInt(y);
734   }
735   this.getElementHeight = getElementHeight;
736   function getElementHeight(){
737     var elm;
738     if(typeof(p_elm) == 'object'){
739       elm = p_elm;
740     } else {
741       elm = document.getElementById(p_elm);
742     }
743     return parseInt(elm.offsetHeight);
744   }
745   this.getElementBottom = getElementBottom;
746   function getElementBottom(){
747     return getElementTop(p_elm) + getElementHeight(p_elm);
748   }
749 }
750 function CC() {
751   var calendarId = 'CC';
752   var currentYear = 0;
753   var currentMonth = 0;
754   var currentDay = 0;
755   var selectedYear = 0;
756   var selectedMonth = 0;
757   var selectedDay = 0;
758   var months = ['$months[0]','$months[1]','$months[2]','$months[3]','$months[4]','$months[5]','$months[6]','$months[7]','$months[8]','$months[9]','$months[10]','$months[11]'];
759   var wdays = ['$wdays[0]', '$wdays[1]', '$wdays[2]', '$wdays[3]', '$wdays[4]', '$wdays[5]', '$wdays[6]'];
760   var dateField = null;
761   function getProperty(p_property){
762     var p_elm = calendarId;
763     var elm = null;
764     if(typeof(p_elm) == 'object'){
765       elm = p_elm;
766     } else {
767       elm = document.getElementById(p_elm);
768     }
769     if (elm != null){
770       if(elm.style){
771         elm = elm.style;
772         if(elm[p_property]){
773           return elm[p_property];
774         } else {
775           return null;
776         }
777       } else {
778         return null;
779       }
780     }
781   }
782   function setElementProperty(p_property, p_value, p_elmId){
783     var p_elm = p_elmId;
784     var elm = null;
785     if(typeof(p_elm) == 'object'){
786       elm = p_elm;
787     } else {
788       elm = document.getElementById(p_elm);
789     }
790     if((elm != null) && (elm.style != null)){
791       elm = elm.style;
792       elm[ p_property ] = p_value;
793     }
794   }
795   function setProperty(p_property, p_value) {
796     setElementProperty(p_property, p_value, calendarId);
797   }
798   function getDaysInMonth(year, month) {
799 ";
800         if ($date_system == 1)
801                 $js .= "
802     return [31,31,31,31,31,31,30,30,30,30,30,(((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682 ? 30 : 29)][month-1];
803 ";
804         else if ($date_system == 2)
805                 $js .= "
806     return [30,29,30,29,30,29,30,29,30,29,30,(((((11 * year) + 14) % 30) < 11) ? 30 : 29)][month-1];
807 ";
808         else
809                 $js .= "
810     return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
811 ";
812         $js .= "
813   }
814   function getDayOfWeek(year, month, day) {
815 ";
816         if ($date_system == 1 || $date_system == 2)
817         {
818                 $js .= "
819         function mod(a, b)
820         {
821             return a - (b * Math.floor(a / b));
822         }
823         function jwday(j)
824         {
825             return mod(Math.floor((j + 1.5)), 7);
826         }
827 ";
828         if ($date_system == 1)
829                 $js .= "
830     var epbase, epyear, t;
831     epbase = year - ((year >= 0) ? 474 : 473);
832     epyear = 474 + mod(epbase, 2820);
833     t = day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6)) +
834       Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 +
835       Math.floor(epbase / 2820) * 1029983 + (1948320.5 - 1);
836 ";
837         else if ($date_system == 2)
838                 $js .= "
839         var t;
840         t = Math.floor((11 * year + 3) / 30) + 354 * year + 30 * month -
841           Math.floor((month - 1) / 2) + day + 1948440 - 385;
842 ";
843         $js .= "
844     return jwday(t);
845 ";
846         }
847         else
848                 $js .= "
849     var date = new Date(year,month-1,day)
850     return date.getDay();
851 ";
852         $js .= "
853   }
854   this.clearDate = clearDate;
855   function clearDate() {
856     dateField.value = '';
857     hide();
858   }
859   this.setDate = setDate;
860   function setDate(year, month, day) {
861     if (dateField) {
862       if (month < 10) {month = '0' + month;}
863       if (day < 10) {day = '0' + day;}
864 ";
865         if ($how == 0)
866                 $js .= "
867       var dateString = month+'$sep'+day+'$sep'+year;
868 ";
869         else if ($how == 1)
870                 $js .= "
871       var dateString = day+'$sep'+month+'$sep'+year;
872 ";
873         else
874                 $js .= "
875       var dateString = year+'$sep'+month+'$sep'+day;
876 ";
877         $js .= "
878       dateField.value = dateString;
879       hide();
880     }
881     return;
882   }
883   this.changeMonth = changeMonth;
884   function changeMonth(change) {
885     currentMonth += change;
886     currentDay = 0;
887     if(currentMonth > 12) {
888       currentMonth = 1;
889       currentYear++;
890     } else if(currentMonth < 1) {
891       currentMonth = 12;
892       currentYear--;
893     }
894     calendar = document.getElementById(calendarId);
895     calendar.innerHTML = calendarDrawTable();
896   }
897   this.changeYear = changeYear;
898   function changeYear(change) {
899     currentYear += change;
900     currentDay = 0;
901     calendar = document.getElementById(calendarId);
902     calendar.innerHTML = calendarDrawTable();
903   }
904   function getCurrentYear() {
905     var year = new Date().getYear();
906     if(year < 1900) year += 1900;
907     return year;
908   }
909   function getCurrentMonth() {
910     return new Date().getMonth() + 1;
911   }
912   function getCurrentDay() {
913     return new Date().getDate();
914   }
915   function calendarDrawTable() {
916     var dayOfMonth = 1;
917     var wstart = $wstart;
918     var validDay = 0;
919     var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
920     var daysInMonth = getDaysInMonth(currentYear, currentMonth);
921     var css_class = null; //CSS class for each day
922     var table = \"<table cellspacing='0' cellpadding='0' border='0'>\";
923     table += \"<tr class='header'>\";
924     table += \"  <td colspan='2' class='previous'><a href='javascript:changeCCMonth(-1);'>&lt;</a><br><a href='javascript:changeCCYear(-1);'>&laquo;</a></td>\";
925     table += \"  <td colspan='3' class='title'>\" + months[currentMonth-1] + \"<br>\" + currentYear + \"</td>\";
926     table += \"  <td colspan='2' class='next'><a href='javascript:changeCCMonth(1);'>&gt;</a><br><a href='javascript:changeCCYear(1);'>&raquo;</a></td>\";
927     table += \"</tr>\";
928     table += \"<tr>\";
929     for (var n=0; n<7; n++)
930         table += \"<th>\" + wdays[(wstart+n)%7]+\"</th>\";
931     table += \"</tr>\";
932     for(var week=0; week < 6; week++) {
933       table += \"<tr>\";
934       for(var n=0; n < 7; n++) {
935         dayOfWeek = (wstart+n)%7;
936         if(week == 0 && startDayOfWeek == dayOfWeek) {
937           validDay = 1;
938         } else if (validDay == 1 && dayOfMonth > daysInMonth) {
939           validDay = 0;
940         }
941         if(validDay) {
942           if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
943             css_class = 'current';
944 ";
945         if ($date_system == 1 || $date_system == 2)
946                 $js .= "
947           } else if (dayOfWeek == 5) {
948 ";
949         else
950                 $js .= "
951           } else if (dayOfWeek == 0 || dayOfWeek == 6) {
952 ";
953                 $js .= "
954             css_class = 'weekend';
955           } else {
956             css_class = 'weekday';
957           }
958           table += \"<td><a class='\"+css_class+\"' href=\\\"javascript:setCCDate(\"+currentYear+\",\"+currentMonth+\",\"+dayOfMonth+\")\\\">\"+dayOfMonth+\"</a></td>\";
959           dayOfMonth++;
960         } else {
961           table += \"<td class='empty'>&nbsp;</td>\";
962         }
963       }
964       table += \"</tr>\";
965     }
966     table += \"<tr class='header'><th colspan='7' style='padding: 3px;'><a href='javascript:hideCC();'>$back</a></td></tr>\";
967     table += \"</table>\";
968     return table;
969   }
970   this.show = show;
971   function show(field) {
972     can_hide = 0;
973     if (dateField == field) {
974       return;
975     } else {
976       dateField = field;
977     }
978     if(dateField) {
979       try {
980         var dateString = new String(dateField.value);
981         var dateParts = dateString.split('$sep');
982 ";
983         if ($how == 0)
984                 $js .= "
985         selectedMonth = parseInt(dateParts[0],10);
986         selectedDay = parseInt(dateParts[1],10);
987         selectedYear = parseInt(dateParts[2],10);
988 ";
989         else if ($how == 1)
990                 $js .= "
991         selectedDay = parseInt(dateParts[0],10);
992         selectedMonth = parseInt(dateParts[1],10);
993         selectedYear = parseInt(dateParts[2],10);
994 ";
995         else
996                 $js .= "
997         selectedYear = parseInt(dateParts[0],10);
998         selectedMonth = parseInt(dateParts[1],10);
999         selectedDay = parseInt(dateParts[2],10);
1000 ";
1001         $js .= "
1002       } catch(e) {}
1003     }
1004     if (!(selectedYear && selectedMonth && selectedDay)) {
1005 ";
1006         if ($date_system == 1 || $date_system == 2)
1007         {
1008                 $js .= "
1009       selectedMonth = $cmon;
1010       selectedDay = $cday;
1011       selectedYear = $cyear;
1012 ";
1013         }
1014         else
1015         {
1016                 $js .= "
1017       selectedMonth = getCurrentMonth();
1018       selectedDay = getCurrentDay();
1019       selectedYear = getCurrentYear();
1020 ";
1021         }
1022         $js .= "
1023     }
1024     currentMonth = selectedMonth;
1025     currentDay = selectedDay;
1026     currentYear = selectedYear;
1027     if(document.getElementById){
1028       calendar = document.getElementById(calendarId);
1029       calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
1030       setProperty('display', 'block');
1031       var fieldPos = new positionInfo(dateField);
1032       var calendarPos = new positionInfo(calendarId);
1033       var x = fieldPos.getElementLeft();
1034       var y = fieldPos.getElementBottom();
1035       setProperty('left', x + 'px');
1036       setProperty('top', y + 'px');
1037       if (document.all) {
1038         setElementProperty('display', 'block', 'CCIframe');
1039         setElementProperty('left', x + 'px', 'CCIframe');
1040         setElementProperty('top', y + 'px', 'CCIframe');
1041         setElementProperty('width', calendarPos.getElementWidth() + 'px', 'CCIframe');
1042         setElementProperty('height', calendarPos.getElementHeight() + 'px', 'CCIframe');
1043       }
1044     }
1045   }
1046   this.hide = hide;
1047   function hide() {
1048     if(dateField) {
1049       setProperty('display', 'none');
1050       setElementProperty('display', 'none', 'CCIframe');
1051       dateField = null;
1052     }
1053   }
1054   this.visible = visible;
1055   function visible() {
1056     return dateField
1057   }
1058   this.can_hide = can_hide;
1059   var can_hide = 0;
1060 }
1061 var cC = new CC();
1062 function date_picker(textField) {
1063   cC.show(textField);
1064 }
1065 function hideCC() {
1066   if (cC.visible()) {
1067     cC.hide();
1068   }
1069 }
1070 function setCCDate(year, month, day) {
1071   cC.setDate(year, month, day);
1072 }
1073 function changeCCYear(change) {
1074   cC.changeYear(change);
1075 }
1076 function changeCCMonth(change) {
1077   cC.changeMonth(change);
1078 }
1079 document.write(\"<iframe id='CCIframe' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>\");
1080 document.write(\"<div id='CC'></div>\");";
1081
1082      cache_js_file($fpath, $js);
1083     } 
1084     add_js_ufile($fpath);
1085
1086  return '';
1087 }
1088
1089 //
1090 //      Javascript conversions to/from user numeric format.
1091 //
1092 function add_js_user_num() {
1093
1094     global $comp_path;
1095     $fpath = $comp_path.'/'.user_company().'/js_cache/'.'user_num.js';
1096
1097     if (!file_exists($fpath)) {
1098     
1099         global $thoseps, $decseps;
1100         $ts = $thoseps[user_tho_sep()];
1101         $ds = $decseps[user_dec_sep()];
1102
1103         $js = "function price_format(post, num, dec, label) {
1104         //num = num.toString().replace(/\\$|\\,/g,'');
1105         if(isNaN(num))
1106                 num = \"0\";
1107         sign = (num == (num = Math.abs(num)));
1108         if(dec<0) dec = 2;
1109         decsize = Math.pow(10, dec);
1110         num = Math.floor(num*decsize+0.50000000001);
1111         cents = num%decsize;
1112         num = Math.floor(num/decsize).toString();
1113         for( i=cents.toString().length; i<dec; i++){
1114                 cents = \"0\" + cents;
1115         }
1116         for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
1117                 num = num.substring(0,num.length-(4*i+3))+'".$ts."'+
1118                         num.substring(num.length-(4*i+3));
1119          num = ((sign)?'':'-') + num;
1120          if(dec!=0) num = num + '".$ds."' + cents;
1121         if(label)
1122             document.getElementById(post).innerHTML = num;
1123         else
1124             document.getElementsByName(post)[0].value = num;
1125         }
1126         function get_amount(doc, label) {
1127             if(label)
1128                 var val = document.getElementById(doc).innerHTML;
1129             else
1130                 var val = document.getElementsByName(doc)[0].value;
1131                 val = val.replace(/\\".$ts."/g,'');
1132                 val = val.replace(/\\".$ds."/g,'.');
1133                 return 1*val;
1134         }
1135         ";
1136
1137      cache_js_file($fpath, $js);
1138     } 
1139     add_js_ufile($fpath);
1140 }
1141
1142 function add_js_allocate() {
1143     global $comp_path;
1144     $fpath = $comp_path.'/'.user_company().'/js_cache/'.'allocate.js';
1145
1146     if (!file_exists($fpath)) {
1147     
1148         $js =
1149         "function allocate_all(doc) {
1150         var amount = get_amount('amount'+doc);
1151         var unallocated = get_amount('un_allocated'+doc);
1152         var total = get_amount('total_allocated', 1);
1153         var left = get_amount('left_to_allocate', 1);
1154
1155         if(unallocated<amount) amount = unallocated;
1156         if((unallocated-amount)<=left){
1157             left-=unallocated-amount;
1158             total+=unallocated-amount;
1159             amount=unallocated;
1160         }else{
1161         total+=left;
1162         amount+=left;
1163         left=0;
1164         }
1165         price_format('amount'+doc, amount, ".user_price_dec().");
1166         price_format('left_to_allocate', left, ".user_price_dec().", 1);
1167         price_format('total_allocated', total, ".user_price_dec().", 1);
1168         }
1169         function allocate_none(doc) {
1170         amount = get_amount('amount'+doc);
1171         left = get_amount('left_to_allocate', 1);
1172         total = get_amount('total_allocated', 1);
1173         price_format('left_to_allocate',amount+left, ".user_price_dec().", 1);
1174         price_format('amount'+doc, 0, ".user_price_dec().");
1175         price_format('total_allocated', total-amount, ".user_price_dec().", 1);
1176         }";
1177
1178      cache_js_file($fpath, $js);
1179     } 
1180     add_js_ufile($fpath);
1181     add_js_user_num();
1182 }
1183
1184 function alert($msg)
1185 {
1186         echo "\n<script type=\"text/javascript\">\n"
1187                 . "<!--\n"
1188                 . "alert('$msg');\n"
1189                 . "-->\n"
1190                 . "</script>\n";
1191 }
1192
1193 if (!function_exists('_vd'))
1194 {
1195         function _vd($mixed, $title = '', $exit = false)
1196         {
1197         // Only the site admin is able to proceed here.
1198         echo (!empty($title) ? ($title .':') : '') .'<pre>';
1199         var_dump($mixed);
1200         echo "</pre>\n";
1201         if ($exit)
1202                 exit;
1203         }
1204 }
1205
1206 ?>