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