1// DHTML prompt() function replacement inspirated by :
2// http://www.hunlock.com/blogs/Working_around_IE7s_prompt_bug,_er_feature
3
4var pfcPrompt = Class.create();
5pfcPrompt.prototype = {
6  initialize: function(container)
7  {
8    if (container == undefined || (is_ie && !is_ie7))
9      container = document.getElementsByTagName('body')[0];
10    this.container    = container;
11    this.box          = $('pfc_promptbox');
12    this.bgbox        = $('pfc_promptbgbox');
13    this.prompt_field = $('pfc_promptbox_field');
14    this.prompt_title = $('pfc_promptbox_title');
15
16    this.buildBox();
17    this.buildBgBox();
18  },
19
20  buildBox: function()
21  {
22    if (!this.box)
23    {
24      this.box = document.createElement('div');
25      this.box.id = 'pfc_promptbox';
26      this.box.style.position = 'absolute';
27      this.box.style.zIndex   = 100;
28      this.box.style.display  = 'none';
29
30      if (is_gecko) {
31        this.box.style.overflow = 'auto';
32      }
33
34      var div = document.createElement('h2');
35      div.appendChild(document.createTextNode(pfc.res.getLabel('Input Required')));
36      this.box.appendChild(div);
37
38      this.prompt_title = document.createElement('p');
39      this.prompt_title.id = 'pfc_promptbox_title';
40      this.box.appendChild(this.prompt_title);
41
42      var form = document.createElement('form');
43      form.pfc_prompt = this;
44      form.onsubmit = function(evt) { return this.pfc_prompt._doSubmit(); };
45      this.box.appendChild(form);
46
47      this.prompt_field = document.createElement('input');
48      this.prompt_field.id = 'pfc_promptbox_field';
49      this.prompt_field.type  = 'text';
50      this.prompt_field.value = '';
51      form.appendChild(this.prompt_field);
52
53      var br = document.createElement('br');
54      form.appendChild(br);
55
56      var cancel = document.createElement('input');
57      cancel.id = 'pfc_promptbox_cancel';
58      cancel.type = 'button';
59      cancel.value = pfc.res.getLabel('Cancel');
60      cancel.pfc_prompt = this;
61      cancel.onclick = function(evt) { return this.pfc_prompt._doSubmit(true); };
62      form.appendChild(cancel);
63
64      var submit = document.createElement('input');
65      submit.id = 'pfc_promptbox_submit';
66      submit.type = 'submit';
67      submit.value = pfc.res.getLabel('OK');
68      form.appendChild(submit);
69
70      var ct = document.getElementsByTagName('body')[0];
71      ct.appendChild(this.box);
72    }
73  },
74
75  buildBgBox: function()
76  {
77    if (!this.bgbox)
78    {
79      this.bgbox = document.createElement('div');
80      this.bgbox.id = 'pfc_promptbgbox';
81      // assign the styles to the blackout division.
82      this.bgbox.style.opacity = '.7';
83      this.bgbox.style.position = 'absolute';
84      this.bgbox.style.backgroundColor = '#555';
85      this.bgbox.style.filter = 'alpha(opacity=70)';
86      this.bgbox.style.display = 'none';
87      this.bgbox.style.zIndex = 50;
88
89      var ct = document.getElementsByTagName('body')[0];
90      ct.appendChild(this.bgbox);
91    }
92  },
93
94  prompt: function(text,def)
95  {
96    // if def wasn't actually passed, initialize it to null
97    if (def==undefined) { def=''; }
98
99    // Stretch the blackout division to fill the entire document
100    // and make it visible.  Because it has a high z-index it should
101    // make all other elements on the page unclickable.
102    var pos = this._findPos(this.container);
103    this.bgbox.style.top     = pos[1]+'px';
104    this.bgbox.style.left    = pos[0]+'px';
105    /* Some older IE browsers (e.g., IE 5.5) need scrollHeight/scrollWidth.
106       See: http://www.quirksmode.org/viewport/compatibility.html */
107    if (this.container.scrollHeight > this.container.offsetHeight
108          || this.container.scrollWidth > this.container.offsetWidth)
109    {
110      this.bgbox.style.height  = this.container.scrollHeight+'px';
111      this.bgbox.style.width   = this.container.scrollWidth+'px';
112    }
113    else
114    {
115      this.bgbox.style.height  = this.container.offsetHeight+'px';
116      this.bgbox.style.width   = this.container.offsetWidth+'px';
117    }
118    this.bgbox.style.display = 'block';
119
120    // Position the dialog box on the screen and make it visible.
121    this.box.style.display  = 'block';
122    this.box.style.top      = parseInt(pos[1]+(this.bgbox.offsetHeight-this.box.offsetHeight)/2)+'px';
123    this.box.style.left     = parseInt(pos[0]+(this.bgbox.offsetWidth-this.box.offsetWidth)/2)+'px';
124    this.prompt_field.value = def;
125    this.prompt_field.focus(); // Give the dialog box's input field the focus.
126    this.prompt_title.innerHTML = text;
127  },
128
129  _doSubmit: function(canceled)
130  {
131    // _doSubmit is called when the user enters or cancels the box.
132    var val = this.prompt_field.value;
133    if (is_gecko) this.box.focus(); // test is_gecko because it doesn't work on KHTML browser, the popup shows infinitly
134    this.box.style.display   = 'none'; // clear out the dialog box
135    this.bgbox.style.display = 'none'; // clear out the screen
136    this.prompt_field.value  = ''; // clear out the text field
137    // if the cancel button was pushed, force value to null.
138    if (canceled) { val = '' }
139    // call the user's function
140    this.callback(val,this);
141    return false;
142  },
143
144  _findPos: function(obj)
145  {
146    var curleft = curtop = 0;
147    if (obj.offsetParent) {
148      curleft = obj.offsetLeft;
149      curtop = obj.offsetTop;
150      while (obj = obj.offsetParent) {
151        curleft += obj.offsetLeft;
152        curtop += obj.offsetTop;
153      }
154    }
155    return [curleft,curtop];
156  },
157
158  focus: function()
159  {
160    this.prompt_field.focus();
161  },
162
163  callback: function(v,pfcp)
164  {
165  }
166
167
168}
169
170