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