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