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