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