7ca792aee8e30cd76afdd4a81d76d7f4bc888f4e
[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                         $rate = get_ecb_rate($currency);
289                         if (get_date_exchange_rate($currency, $date_))
290                                 update_exchange_rate($currency, $date_, $rate, $rate);
291                         else
292                                 add_exchange_rate($currency, $date_, $rate, $rate);
293                         if ($from_currency == $comp_currency)
294                                 $rate = 1 / $rate;
295                         $Ajax->activate('get_rate');
296                 }
297                 else
298                         $rate = get_exchange_rate_from_to($to_currency, $from_currency, $date_);
299                 $rate = number_format2($rate, user_exrate_dec());
300         label_row(_("Exchange Rate:"),"1 " . $from_currency . " = <span id='_ex_rate'>" . $rate . "</span> " . $to_currency .
301                 ($buttons?"  " . submit('get_rate',_("Get"), false, $hint, true):""));
302             if($buttons) {
303                   $Ajax->addUpdate('get_rate','_ex_rate', $rate);
304                 }
305         }
306 }
307
308 //--------------------------------------------------------------------------------------
309
310 function is_voided_display($type, $id, $label)
311 {
312         global $table_style;
313         $void_entry = get_voided_entry($type, $id);
314
315         if ($void_entry == null)
316                 return false;
317
318         start_table("width=50% $table_style");
319         echo "<tr><td align=center><font color=red>$label</font><br>";
320         echo "<font color=red>" . _("Date Voided:") . " " . sql2date($void_entry["date_"]) . "</font><br>";
321         if (strlen($void_entry["memo_"]) > 0)
322                 echo "<center><font color=red>" . _("Memo:") . " " . $void_entry["memo_"] . "</font></center><br>";
323         echo "</td></tr>";
324         end_table(1);
325
326         return true;
327 }
328
329 //--------------------------------------------------------------------------------------
330
331 function comments_display_row($type, $id)
332 {
333         $comments = get_comments($type, $id);
334         if ($comments and db_num_rows($comments))
335         {
336                 echo "<tr><td colspan=15>";
337         while ($comment = db_fetch($comments))
338         {
339                 echo $comment["memo_"] . "<br>";
340         }
341                 echo "</td></tr>";
342         }
343 }
344
345 //--------------------------------------------------------------------------------------
346
347 function get_comments_string($type, $type_no)
348 {
349         $str_return = "";
350         $result = get_comments($type, $type_no);
351         while ($comment = db_fetch($result))
352         {
353                 if (strlen($str_return))
354                         $str_return = $str_return . " \n";
355                 $str_return = $str_return . $comment["memo_"];
356         }
357         return $str_return;
358 }
359
360 //--------------------------------------------------------------------------------------
361
362 function view_stock_status($stock_id, $description=null)
363 {
364         global $path_to_root;
365         if ($description)
366                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", (user_show_codes()?$stock_id . " - ":"") . $description, "stock_id=$stock_id");
367                 $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>";
368         else
369                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", $stock_id, "stock_id=$stock_id");
370                 $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>";
371         echo $preview_str;
372 }
373
374 function view_stock_status_cell($stock_id, $description=null)
375 {
376         echo "<td>";
377         view_stock_status($stock_id, $description);
378         echo "</td>";
379 }
380
381 //--------------------------------------------------------------------------------------
382
383 function display_debit_or_credit_cells($value)
384 {
385         if ($value > 0)
386         {
387                 amount_cell($value);
388                 label_cell("");
389         }
390         elseif ($value < 0)
391         {
392                 label_cell("");
393                 amount_cell(abs($value));
394         }
395         else
396         {
397                 //label_cell("");
398                 amount_cell(0);
399                 label_cell("");
400         }
401 }
402
403 //--------------------------------------------------------------------------------------
404
405 function display_customer_trans_tax_details($tax_items, $columns)
406 {
407     while ($tax_item = db_fetch($tax_items))
408     {
409         $tax = number_format2($tax_item['amount'],user_price_dec());
410         if ($tax_item['included_in_price'])
411                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
412                         _("Amount") . ": $tax", "", "colspan=$columns align=right", "align=right");
413         else
414                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
415                         $tax, "colspan=$columns align=right", "align=right");
416     }
417 }
418
419 //--------------------------------------------------------------------------------------
420
421 function display_supp_trans_tax_details($tax_items, $columns)
422 {
423     while ($tax_item = db_fetch($tax_items))
424     {
425         $tax = number_format2(abs($tax_item['amount']),user_price_dec());
426         if ($tax_item['included_in_price'])
427                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
428                         _("Amount:") . ": $tax", "colspan=$columns align=right", "align=right");
429         else
430                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
431                         $tax, "colspan=$columns align=right", "align=right");
432     }
433 }
434
435 //--------------------------------------------------------------------------------------
436
437 function display_edit_tax_items($taxes, $columns, $tax_included, $leftspan=0)
438 {
439         $total = 0;
440
441     foreach ($taxes as $taxitem)
442     {
443         if ($tax_included)
444         {
445                 label_row(_("Included") . " " . $taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%) " .
446                         _("Amount:") . " " . number_format2($taxitem['Value'],user_price_dec()), "", "colspan=$columns align=right", "align=right",$leftspan);
447         }
448         else
449         {
450                 label_row($taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%)",
451                         number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
452                 $total +=  $taxitem['Value'];
453         }
454     }
455
456     return $total;
457 }
458
459 //--------------------------------------------------------------------------------------
460
461 function display_footer_exit()
462 {
463         br(2);
464         end_page();
465         exit;
466 }
467
468 //--------------------------------------------------------------------------------------
469
470 function display_allocations($alloc_result, $total)
471 {
472         global $table_style;
473
474         if (!$alloc_result || db_num_rows($alloc_result) == 0)
475                 return;
476
477     display_heading2(_("Allocations"));
478
479     start_table("$table_style width=80%");
480
481     $th = array( _("Type"), _("Number"), _("Date"), _("Total Amount"),
482         _("Left to Allocate"), _("This Allocation"));
483         table_header($th);
484     $k = $total_allocated = 0;
485
486     while ($alloc_row = db_fetch($alloc_result))
487     {
488
489         alt_table_row_color($k);
490
491         label_cell(systypes::name($alloc_row['type']));
492         label_cell(get_trans_view_str($alloc_row['type'],$alloc_row['trans_no']));
493         label_cell(sql2date($alloc_row['tran_date']));
494         amount_cell($alloc_row['Total']);
495         //amount_cell($alloc_row['Total'] - $alloc_row['PrevAllocs'] - $alloc_row['amt']);
496         amount_cell($alloc_row['Total'] - $alloc_row['amt']);
497         amount_cell($alloc_row['amt']);
498         end_row();
499
500         $total_allocated += $alloc_row['amt'];
501     }
502     start_row();
503         label_cell(_("Total Allocated:"), "align=right colspan=5");
504         amount_cell($total_allocated);
505         end_row();
506         start_row();
507     label_cell(_("Left to Allocate:"), "align=right colspan=5");
508     amount_cell($total - $total_allocated);
509     end_row();
510
511     end_table(1);
512 }
513
514 //--------------------------------------------------------------------------------------
515
516 function display_allocations_from($person_type, $person_id, $type, $type_no, $total)
517 {
518         switch ($person_type)
519         {
520                 case payment_person_types::customer() :
521                         $alloc_result = get_allocatable_to_cust_transactions($person_id, $type_no, $type);
522                         display_allocations($alloc_result, $total);
523                         return;
524                 case payment_person_types::supplier() :
525                         $alloc_result = get_allocatable_to_supp_transactions($person_id, $type_no, $type);
526                         display_allocations($alloc_result, $total);
527                         return;
528         }
529 }
530
531 function get_js_open_window($width, $height)
532 {
533         $js = "\n<script type=\"text/javascript\">\n"
534                 . "<!--\n"
535                 . "function openWindow(url, title)\n"
536                 . "{\n"
537                 . " var left = (screen.width - $width) / 2;\n"
538                 . " var top = (screen.height - $height) / 2;\n"
539                 . " return window.open(url, title, 'width=$width,height=$height,left='+left+',top='+top+',screenX='+left+',screenY='+top+',status=no,scrollbars=yes');\n"
540                 . "}\n"
541                 . "-->\n"
542                 . "</script>\n";
543         return $js;
544 }
545
546 /*
547   Setting focus on element $name in $form.
548   If $form<0 $name is element id.
549 */
550 function set_focus($name, $form_no=0) {
551   global $Ajax;
552         $Ajax->addFocus(true, $name);
553     $_POST['_focus'] = $name;
554 }
555 //
556 //      Set default focus on first field $name if not set yet
557 //
558 function default_focus($name, $form_no=0) {
559     if (!isset($_POST['_focus'])) {
560           set_focus($name);
561     }
562 }
563
564 function get_js_png_fix()
565 {
566         $js = "<script type=\"text/javascript\">\n"
567                 . "function fixPNG(myImage)\n"
568                 . "{\n"
569                 . " var arVersion = navigator.appVersion.split(\"MSIE\")\n"
570                 . " var version = parseFloat(arVersion[1])\n"
571         . " if ((version >= 5.5) && (version < 7) && (document.body.filters))\n"
572         . " {\n"
573         . "  var imgID = (myImage.id) ? \"id='\" + myImage.id + \"' \" : \"\"\n"
574                 . "  var imgClass = (myImage.className) ? \"class='\" + myImage.className + \"' \" : \"\"\n"
575                 . "  var imgTitle = (myImage.title) ?\n"
576                 . "    \"title='\" + myImage.title  + \"' \" : \"title='\" + myImage.alt + \"' \"\n"
577                 . "  var imgStyle = \"display:inline-block;\" + myImage.style.cssText\n"
578                 . "  var strNewHTML = \"<span \" + imgID + imgClass + imgTitle\n"
579         . "    + \" style=\\\"\" + \"width:\" + myImage.width\n"
580         . "    + \"px; height:\" + myImage.height\n"
581         . "    + \"px;\" + imgStyle + \";\"\n"
582         . "    + \"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader\"\n"
583         . "    + \"(src=\'\" + myImage.src + \"\', sizingMethod='scale');\\\"></span>\"\n"
584                 . "  myImage.outerHTML = strNewHTML\n"
585         . " }\n"
586                 . "}\n"
587                 . "</script>\n";
588         return $js;
589 }
590
591 function get_js_date_picker()
592 {
593     global $comp_path, $go_debug;
594     $fpath = $comp_path.'/'.user_company().'/js_cache/'.'date_picker.js';
595
596         $js = "<style>
597 #CCIframe { display: none; left: 0px; position: absolute; top: 0px; height: 250px; width: 270px; z-index: 99;}
598 #CC { position:absolute; background-color:#FFF; margin:0; padding:0; display:none; z-index: 100;}
599 #CC table { font-family: arial, verdana, helvetica, sans-serif;font-size: 8pt; border-left: 1px solid #336; border-right: 1px solid #336;}
600 #CC th { font-weight: normal; text-align: center;}
601 #CC th a { font-weight: normal; text-decoration: none; color: #FFF; padding: 1px;}
602 #CC td { text-align: center;}
603 #CC .header { background-color: #336;}
604 #CC .weekday { background-color: #DDD; color: #000;}
605 #CC .weekend { background-color: #FFC; color: #000;}
606 #CC .weekno { background-color: #c0daf8; color: #555;}
607 #CC .current { border: 1px solid #339; background-color: #336; color: #FFF;}
608 #CC .weekday,#CC .weekend,#CC .current { display: block; text-decoration: none; border: 1px solid #FFF; width: 2em;}
609 #CC .weekday:hover,#CC .weekend:hover,#CC .current:hover { color: #FFF; background-color: #336; border: 1px solid #999;}
610 #CC .previous { text-align: left;}
611 #CC .next { text-align: right;}
612 #CC .previous,#CC .next { padding: 1px 3px 1px 3px; font-size: 1.4em;}
613 #CC .previous a,#CC .next a { color: #FFF; text-decoration: none; font-weight: bold;}
614 #CC .title { text-align: center; font-weight: bold; color: #FFF;}
615 #CC .empty { background-color: #CCC; border: 1px solid #FFF;}
616 </style>
617 ";
618     add_js_source($js);
619
620     if (!file_exists($fpath) || $go_debug) {
621
622         global $dateseps, $date_system;
623
624         $how = user_date_format();                              // 0 = us/ca, 1 = eu, au, nz, 2 = jp, sw
625         $sep = $dateseps[user_date_sep()];              // date separator
626         $wstart = (($date_system == 1 || $date_system == 2) ? 6 : ($how == 0 ? 0 : 1));                 // weekstart (sun = 0, mon = 1)
627         $months = array(_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),_("July"),_("August"),_("September"),_("October"),_("November"),_("December"));
628         $wdays = array(_("Su"),_("Mo"),_("Tu"),_("We"),_("Th"),_("Fr"),_("Sa"));
629         $wno = _("W"); // week no
630         $back = _("Back");
631         if ($date_system == 1)
632                 list($cyear, $cmon, $cday) = gregorian_to_jalali(date("Y"), date("n"), date("j"));
633         else if ($date_system == 2)
634                 list($cyear, $cmon, $cday) = gregorian_to_islamic(date("Y"), date("n"), date("j"));
635
636
637         $js = "
638 function positionInfo(object) {
639   var p_elm = object;
640   this.getElementLeft = getElementLeft;
641   function getElementLeft() {
642     var x = 0;
643     var elm;
644     if(typeof(p_elm) == 'object'){
645       elm = p_elm;
646     } else {
647       elm = document.getElementById(p_elm);
648     }
649     while (elm != null) {
650       x+= elm.offsetLeft;
651       elm = elm.offsetParent;
652     }
653     return parseInt(x);
654   }
655   this.getElementWidth = getElementWidth;
656   function getElementWidth(){
657     var elm;
658     if(typeof(p_elm) == 'object'){
659       elm = p_elm;
660     } else {
661       elm = document.getElementById(p_elm);
662     }
663     return parseInt(elm.offsetWidth);
664   }
665   this.getElementRight = getElementRight;
666   function getElementRight(){
667     return getElementLeft(p_elm) + getElementWidth(p_elm);
668   }
669   this.getElementTop = getElementTop;
670   function getElementTop() {
671     var y = 0;
672     var elm;
673     if(typeof(p_elm) == 'object'){
674       elm = p_elm;
675     } else {
676       elm = document.getElementById(p_elm);
677     }
678     while (elm != null) {
679       y+= elm.offsetTop;
680       elm = elm.offsetParent;
681     }
682     return parseInt(y);
683   }
684   this.getElementHeight = getElementHeight;
685   function getElementHeight(){
686     var elm;
687     if(typeof(p_elm) == 'object'){
688       elm = p_elm;
689     } else {
690       elm = document.getElementById(p_elm);
691     }
692     return parseInt(elm.offsetHeight);
693   }
694   this.getElementBottom = getElementBottom;
695   function getElementBottom(){
696     return getElementTop(p_elm) + getElementHeight(p_elm);
697   }
698 }
699 function CC() {
700   var calendarId = 'CC';
701   var currentYear = 0;
702   var currentMonth = 0;
703   var currentDay = 0;
704   var selectedYear = 0;
705   var selectedMonth = 0;
706   var selectedDay = 0;
707   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]'];
708   var wdays = ['$wdays[0]', '$wdays[1]', '$wdays[2]', '$wdays[3]', '$wdays[4]', '$wdays[5]', '$wdays[6]'];
709   var dateField = null;
710   function getProperty(p_property){
711     var p_elm = calendarId;
712     var elm = null;
713     if(typeof(p_elm) == 'object'){
714       elm = p_elm;
715     } else {
716       elm = document.getElementById(p_elm);
717     }
718     if (elm != null){
719       if(elm.style){
720         elm = elm.style;
721         if(elm[p_property]){
722           return elm[p_property];
723         } else {
724           return null;
725         }
726       } else {
727         return null;
728       }
729     }
730   }
731   function setElementProperty(p_property, p_value, p_elmId){
732     var p_elm = p_elmId;
733     var elm = null;
734     if(typeof(p_elm) == 'object'){
735       elm = p_elm;
736     } else {
737       elm = document.getElementById(p_elm);
738     }
739     if((elm != null) && (elm.style != null)){
740       elm = elm.style;
741       elm[ p_property ] = p_value;
742     }
743   }
744   function setProperty(p_property, p_value) {
745     setElementProperty(p_property, p_value, calendarId);
746   }
747   function getDaysInMonth(year, month) {
748 ";
749         if ($date_system == 1)
750                 $js .= "
751     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];
752 ";
753         else if ($date_system == 2)
754                 $js .= "
755     return [30,29,30,29,30,29,30,29,30,29,30,(((((11 * year) + 14) % 30) < 11) ? 30 : 29)][month-1];
756 ";
757         else
758                 $js .= "
759     return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
760 ";
761         $js .= "
762   }
763   function getDayOfWeek(year, month, day) {
764 ";
765         if ($date_system == 1 || $date_system == 2)
766         {
767                 $js .= "
768         function mod(a, b)
769         {
770             return a - (b * Math.floor(a / b));
771         }
772         function jwday(j)
773         {
774             return mod(Math.floor((j + 1.5)), 7);
775         }
776 ";
777         if ($date_system == 1)
778                 $js .= "
779     var epbase, epyear, t;
780     epbase = year - ((year >= 0) ? 474 : 473);
781     epyear = 474 + mod(epbase, 2820);
782     t = day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6)) +
783       Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 +
784       Math.floor(epbase / 2820) * 1029983 + (1948320.5 - 1);
785 ";
786         else if ($date_system == 2)
787                 $js .= "
788         var t;
789         t = Math.floor((11 * year + 3) / 30) + 354 * year + 30 * month -
790           Math.floor((month - 1) / 2) + day + 1948440 - 385;
791 ";
792         $js .= "
793     return jwday(t);
794 ";
795         }
796         else
797                 $js .= "
798     var date = new Date(year,month-1,day)
799     return date.getDay();
800 ";
801         $js .= "
802   }
803   this.clearDate = clearDate;
804   function clearDate() {
805     dateField.value = '';
806     hide();
807   }
808   this.getWeek = getWeek;
809   function getWeek(year, month, day) {
810 ";
811         if ($how == 0)
812                 $js .= "  day++;";
813         $js .= "
814     var date = new Date(year,month-1,day);
815     var D = date.getDay();
816     if(D == 0) D = 7;
817     date.setDate(date.getDate() + (4 - D));
818     var YN = date.getFullYear();
819     var ZBDoCY = Math.floor((date.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
820     var WN = 1 + Math.floor(ZBDoCY / 7);
821     return WN;
822   }
823   this.setDate = setDate;
824   function setDate(year, month, day) {
825     if (dateField) {
826       if (month < 10) {month = '0' + month;}
827       if (day < 10) {day = '0' + day;}
828 ";
829         if ($how == 0)
830                 $js .= "
831       var dateString = month+'$sep'+day+'$sep'+year;
832 ";
833         else if ($how == 1)
834                 $js .= "
835       var dateString = day+'$sep'+month+'$sep'+year;
836 ";
837         else
838                 $js .= "
839       var dateString = year+'$sep'+month+'$sep'+day;
840 ";
841         $js .= "
842       dateField.value = dateString;
843       hide();
844     }
845     return;
846   }
847   this.changeMonth = changeMonth;
848   function changeMonth(change) {
849     currentMonth += change;
850     currentDay = 0;
851     if(currentMonth > 12) {
852       currentMonth = 1;
853       currentYear++;
854     } else if(currentMonth < 1) {
855       currentMonth = 12;
856       currentYear--;
857     }
858     calendar = document.getElementById(calendarId);
859     calendar.innerHTML = calendarDrawTable();
860   }
861   this.changeYear = changeYear;
862   function changeYear(change) {
863     currentYear += change;
864     currentDay = 0;
865     calendar = document.getElementById(calendarId);
866     calendar.innerHTML = calendarDrawTable();
867   }
868   function getCurrentYear() {
869     var year = new Date().getYear();
870     if(year < 1900) year += 1900;
871     return year;
872   }
873   function getCurrentMonth() {
874     return new Date().getMonth() + 1;
875   }
876   function getCurrentDay() {
877     return new Date().getDate();
878   }
879   function calendarDrawTable() {
880     var dayOfMonth = 1;
881     var wstart = $wstart;
882     var wno = '&nbsp;$wno&nbsp;';
883     var validDay = 0;
884     var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
885     var daysInMonth = getDaysInMonth(currentYear, currentMonth);
886     var css_class = null; //CSS class for each day
887     var table = \"<table cellspacing='0' cellpadding='0' border='0'>\";
888     table += \"<tr class='header'>\";
889     table += \"  <td colspan='2' class='previous'><a href='javascript:changeCCMonth(-1);'>&lt;</a><br><a href='javascript:changeCCYear(-1);'>&laquo;</a></td>\";
890     table += \"  <td colspan='4' class='title'>\" + months[currentMonth-1] + \"<br>\" + currentYear + \"</td>\";
891     table += \"  <td colspan='2' class='next'><a href='javascript:changeCCMonth(1);'>&gt;</a><br><a href='javascript:changeCCYear(1);'>&raquo;</a></td>\";
892     table += \"</tr>\";
893     table += \"<tr>\";
894     table += \"<th class='weekno'>\"+wno+\"</th>\";
895     for (var n=0; n<7; n++)
896         table += \"<th>\" + wdays[(wstart+n)%7]+\"</th>\";
897     table += \"</tr>\";
898     for(var week=0; week < 6; week++) {
899       table += \"<tr>\";
900       for(var n=0; n < 7; n++) {
901         dayOfWeek = (wstart+n)%7;
902         if(week == 0 && startDayOfWeek == dayOfWeek) {
903           validDay = 1;
904         } else if (validDay == 1 && dayOfMonth > daysInMonth) {
905           validDay = 0;
906         }
907         if (n==0)
908         {
909                 if (dayOfMonth > daysInMonth)
910                         table += \"<td class='empty'>&nbsp;</td>\";
911                 else
912                         table += \"<td class='weekno'>\"+getWeek(currentYear, currentMonth, dayOfMonth)+\"</td>\";
913         }
914         if(validDay) {
915           if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
916             css_class = 'current';
917 ";
918         if ($date_system == 1 || $date_system == 2)
919                 $js .= "
920           } else if (dayOfWeek == 5) {
921 ";
922         else
923                 $js .= "
924           } else if (dayOfWeek == 0 || dayOfWeek == 6) {
925 ";
926                 $js .= "
927             css_class = 'weekend';
928           } else {
929             css_class = 'weekday';
930           }
931           table += \"<td><a class='\"+css_class+\"' href=\\\"javascript:setCCDate(\"+currentYear+\",\"+currentMonth+\",\"+dayOfMonth+\")\\\">\"+dayOfMonth+\"</a></td>\";
932           dayOfMonth++;
933         } else {
934           table += \"<td class='empty'>&nbsp;</td>\";
935         }
936       }
937       table += \"</tr>\";
938     }
939     table += \"<tr class='header'><th colspan='8' style='padding: 3px;text-align:center;'><a href='javascript:hideCC();'>$back</a></td></tr>\";
940     table += \"</table>\";
941     return table;
942   }
943   this.show = show;
944   function show(field) {
945     can_hide = 0;
946     if (dateField == field) {
947       return;
948     } else {
949       dateField = field;
950     }
951     if(dateField) {
952       try {
953         var dateString = new String(dateField.value);
954         var dateParts = dateString.split('$sep');
955 ";
956         if ($how == 0)
957                 $js .= "
958         selectedMonth = parseInt(dateParts[0],10);
959         selectedDay = parseInt(dateParts[1],10);
960         selectedYear = parseInt(dateParts[2],10);
961 ";
962         else if ($how == 1)
963                 $js .= "
964         selectedDay = parseInt(dateParts[0],10);
965         selectedMonth = parseInt(dateParts[1],10);
966         selectedYear = parseInt(dateParts[2],10);
967 ";
968         else
969                 $js .= "
970         selectedYear = parseInt(dateParts[0],10);
971         selectedMonth = parseInt(dateParts[1],10);
972         selectedDay = parseInt(dateParts[2],10);
973 ";
974         $js .= "
975       } catch(e) {}
976     }
977     if (!(selectedYear && selectedMonth && selectedDay)) {
978 ";
979         if ($date_system == 1 || $date_system == 2)
980         {
981                 $js .= "
982       selectedMonth = $cmon;
983       selectedDay = $cday;
984       selectedYear = $cyear;
985 ";
986         }
987         else
988         {
989                 $js .= "
990       selectedMonth = getCurrentMonth();
991       selectedDay = getCurrentDay();
992       selectedYear = getCurrentYear();
993 ";
994         }
995         $js .= "
996     }
997     currentMonth = selectedMonth;
998     currentDay = selectedDay;
999     currentYear = selectedYear;
1000     if(document.getElementById){
1001       calendar = document.getElementById(calendarId);
1002       calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
1003       setProperty('display', 'block');
1004       var fieldPos = new positionInfo(dateField);
1005       var calendarPos = new positionInfo(calendarId);
1006       var x = fieldPos.getElementLeft();
1007       var y = fieldPos.getElementBottom();
1008       setProperty('left', x + 'px');
1009       setProperty('top', y + 'px');
1010       if (document.all) {
1011         setElementProperty('display', 'block', 'CCIframe');
1012         setElementProperty('left', x + 'px', 'CCIframe');
1013         setElementProperty('top', y + 'px', 'CCIframe');
1014         setElementProperty('width', calendarPos.getElementWidth() + 'px', 'CCIframe');
1015         setElementProperty('height', calendarPos.getElementHeight() + 'px', 'CCIframe');
1016       }
1017     }
1018   }
1019   this.hide = hide;
1020   function hide() {
1021     if(dateField) {
1022       setProperty('display', 'none');
1023       setElementProperty('display', 'none', 'CCIframe');
1024       dateField = null;
1025     }
1026   }
1027   this.visible = visible;
1028   function visible() {
1029     return dateField
1030   }
1031   this.can_hide = can_hide;
1032   var can_hide = 0;
1033 }
1034 var cC = new CC();
1035 function date_picker(textField) {
1036   cC.show(textField);
1037 }
1038 function hideCC() {
1039   if (cC.visible()) {
1040     cC.hide();
1041   }
1042 }
1043 function setCCDate(year, month, day) {
1044   cC.setDate(year, month, day);
1045 }
1046 function changeCCYear(change) {
1047   cC.changeYear(change);
1048 }
1049 function changeCCMonth(change) {
1050   cC.changeMonth(change);
1051 }
1052 document.write(\"<iframe id='CCIframe' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>\");
1053 document.write(\"<div id='CC'></div>\");";
1054
1055      cache_js_file($fpath, $js);
1056     }
1057     add_js_ufile($fpath);
1058
1059  return '';
1060 }
1061
1062 function alert($msg)
1063 {
1064         echo "\n<script type=\"text/javascript\">\n"
1065                 . "<!--\n"
1066                 . "alert('$msg');\n"
1067                 . "-->\n"
1068                 . "</script>\n";
1069 }
1070
1071 if (!function_exists('_vd'))
1072 {
1073         function _vd($mixed, $title = '', $exit = false)
1074         {
1075         // Only the site admin is able to proceed here.
1076         echo (!empty($title) ? ($title .':') : '') .'<pre>';
1077         var_dump($mixed);
1078         echo "</pre>\n";
1079         if ($exit)
1080                 exit;
1081         }
1082 }
1083
1084 ?>