// DHTML prompt() function replacement inspirated by :
// http://www.hunlock.com/blogs/Working_around_IE7s_prompt_bug,_er_feature

var pfcPrompt = Class.create();
pfcPrompt.prototype = { 
  initialize: function(container)
  {
    if (container == undefined || (is_ie && !is_ie7))
      container = document.getElementsByTagName('body')[0];
    this.container    = container;
    this.box          = $('pfc_promptbox');
    this.bgbox        = $('pfc_promptbgbox');
    this.prompt_field = $('pfc_promptbox_field');
    this.prompt_title = $('pfc_promptbox_title');

    this.buildBox();
    this.buildBgBox();
  },

  buildBox: function()
  {
    if (!this.box)
    {
      this.box = document.createElement('div');
      this.box.id = 'pfc_promptbox';
      this.box.style.position = 'absolute';
      this.box.style.zIndex   = 100;
      this.box.style.display  = 'none';

      if (is_gecko) {
        this.box.style.overflow = 'hidden';
      }

      var div = document.createElement('h2');
      div.appendChild(document.createTextNode(pfc.res.getLabel('Input Required')));
      this.box.appendChild(div);

      this.prompt_title = document.createElement('p');
      this.prompt_title.id = 'pfc_promptbox_title';
      this.box.appendChild(this.prompt_title);

      var form = document.createElement('form');
      form.pfc_prompt = this;
      form.onsubmit = function(evt) { return this.pfc_prompt._doSubmit(); };
      this.box.appendChild(form);

      this.prompt_field = document.createElement('input');
      this.prompt_field.id = 'pfc_promptbox_field';
      this.prompt_field.type  = 'text';
      this.prompt_field.value = '';
      form.appendChild(this.prompt_field);

      var br = document.createElement('br');
      form.appendChild(br);

      var cancel = document.createElement('input');
      cancel.id = 'pfc_promptbox_cancel';
      cancel.type = 'button';
      cancel.value = pfc.res.getLabel('Cancel');
      cancel.pfc_prompt = this;
      cancel.onclick = function(evt) { return this.pfc_prompt._doSubmit(true); };
      form.appendChild(cancel);

      var submit = document.createElement('input');
      submit.id = 'pfc_promptbox_submit';
      submit.type = 'submit';
      submit.value = pfc.res.getLabel('OK');
      form.appendChild(submit);

      var ct = document.getElementsByTagName('body')[0];
      ct.appendChild(this.box);
    }
  },

  buildBgBox: function()
  {
    if (!this.bgbox)
    {
      this.bgbox = document.createElement('div');
      this.bgbox.id = 'pfc_promptbgbox';
      // assign the styles to the blackout division.
      this.bgbox.style.opacity = '.7';
      this.bgbox.style.position = 'absolute';
      this.bgbox.style.backgroundColor = '#555';
      this.bgbox.style.filter = 'alpha(opacity=70)';
      this.bgbox.style.display = 'none';
      this.bgbox.style.zIndex = 50;

      var ct = document.getElementsByTagName('body')[0];
      ct.appendChild(this.bgbox);
    }
  },

  prompt: function(text,def)
  {
	//IEprompt(text,def);return;
	 
    // if def wasn't actually passed, initialize it to null
    if (def==undefined) { def=''; }

    // Stretch the blackout division to fill the entire document
    // and make it visible.  Because it has a high z-index it should
    // make all other elements on the page unclickable.
    var pos = this._findPos(this.container);
    // @Dimi: IE fix - falsche pos. //alert(pos);
    pos[0]= 8;
    pos[1]= 8;
    this.bgbox.style.top     = pos[1]+'px';
    this.bgbox.style.left    = pos[0]+'px';
  
    if (this.container.scrollHeight > this.container.offsetHeight
          || this.container.scrollWidth > this.container.offsetWidth)
    {
      this.bgbox.style.height  = this.container.scrollHeight+'px';
      this.bgbox.style.width   = this.container.scrollWidth+'px';
    }
    else
    {
      this.bgbox.style.height  = this.container.offsetHeight+'px';
      this.bgbox.style.width   = this.container.offsetWidth+'px';
    }
    this.bgbox.style.display = 'block';

    // Position the dialog box on the screen and make it visible.
    this.box.style.display  = 'block';
    this.box.style.top      = parseInt(pos[1]+(this.bgbox.offsetHeight-this.box.offsetHeight)/2)+'px';
    this.box.style.left     = parseInt(pos[0]+(this.bgbox.offsetWidth-this.box.offsetWidth)/2)+'px';
    this.prompt_field.value = def;
    this.prompt_field.focus(); // Give the dialog box's input field the focus.
    this.prompt_title.innerHTML = text;
   
	  
  },

  _doSubmit: function(canceled)
  {
    // _doSubmit is called when the user enters or cancels the box.
    var val = this.prompt_field.value;
    if (is_gecko) this.box.focus(); // test is_gecko because it doesn't work on KHTML browser, the popup shows infinitly
    this.box.style.display   = 'none'; // clear out the dialog box
    this.bgbox.style.display = 'none'; // clear out the screen
    this.prompt_field.value  = ''; // clear out the text field
    // if the cancel button was pushed, force value to null.
    if (canceled) { val = '' }
    // call the user's function
    this.callback(val,this);
    return false;
  },

  _findPos: function(obj)
  {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
      curleft = obj.offsetLeft;
      curtop = obj.offsetTop;
      while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
      }
    }
    return [curleft,curtop];
  },

  focus: function()
  {
    this.prompt_field.focus();
  },

  callback: function(v,pfcp)
  {
  }


}


///////////////////////////////////////////////////////////
//Usage IEprompt("dialog descriptive text", "default starting value");
//
//IEprompt will call promptCallback(val)
//Where val is the user's input or null if the dialog was canceled.
///////////////////////////////////////////////////////////

function promptCallback(val){
	return val;
}//

///////////////////////////////////////////////////////////
//This source code has been released into the public domain
//January 14th, 2007.
//You may use it and modify it freely without compensation
//and without the need to tell everyone where you got it.
///////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////
//You must create a promptCallback(val) function to handle
//the user input.  If you don't this script will fail and
//Bunnies will die.
///////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////
//These are global scope variables, they should remain global.
///////////////////////////////////////////////////////////
var _dialogPromptID=null;
var _blackoutPromptID=null;
///////////////////////////////////////////////////////////

function IEprompt(innertxt,def) {

	   that=this;

	   // Check to see if this is MSIE 7.   This isn't a great general purpose
	   // detection system but it works well enough just to find MSIE 7.
	   var _isIE7=(navigator.userAgent.indexOf('MSIE 7')>0);

	   this.wrapupPrompt = function (cancled) {
	      // wrapupPrompt is called when the user enters or cancels the box.
	      // It's called only by the IE7 dialog box, not the non IE prompt box
	      if (_isIE7) {
	         // Make sure we're in IE7 mode and get the text box value
	         val=document.getElementById('iepromptfield').value;
	         // clear out the dialog box
	         _dialogPromptID.style.display='none';
	         // clear out the screen
	         _blackoutPromptID.style.display='none';
	         // clear out the text field
	         document.getElementById('iepromptfield').value = '';
	         // if the cancel button was pushed, force value to null.
	         if (cancled) { val = '' }
	         // call the user's function
	         promptCallback(val);
	      }
	      return false;
	   }

	   //if def wasn't actually passed, initialize it to null
	   if (def==undefined) { def=''; }

	   if (_isIE7) {
	      // If this is MSIE 7.0 then...
	      if (_dialogPromptID==null) {
	         // Check to see if we've created the dialog divisions.
	         // This block sets up the divisons
	         // Get the body tag in the dom
	         var tbody = document.getElementsByTagName("body")[0];
	         // create a new division
	         tnode = document.createElement('div');
	         // name it
	         tnode.id='IEPromptBox';
	         // attach the new division to the body tag
	         tbody.appendChild(tnode);
	         // and save the element reference in a global variable
	         _dialogPromptID=document.getElementById('IEPromptBox');
	         // Create a new division (blackout)
	         tnode = document.createElement('div');
	         // name it.
	         tnode.id='promptBlackout';
	         // attach it to body.
	         tbody.appendChild(tnode);
	         // And get the element reference
	         _blackoutPromptID=document.getElementById('promptBlackout');
	         // assign the styles to the blackout division.
	         _blackoutPromptID.style.opacity='.9';
	         _blackoutPromptID.style.position='absolute';
	         _blackoutPromptID.style.top='0px';
	         _blackoutPromptID.style.left='0px';
	         _blackoutPromptID.style.backgroundColor='#555555';
	         _blackoutPromptID.style.filter='alpha(opacity=90)';
	         _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
	         _blackoutPromptID.style.display='block';
	         _blackoutPromptID.style.zIndex='50';
	         // assign the styles to the dialog box
	         _dialogPromptID.style.border='2px solid blue';
	         _dialogPromptID.style.backgroundColor='#DDDDDD';
	         _dialogPromptID.style.position='absolute';
	         _dialogPromptID.style.width='330px';
	         _dialogPromptID.style.zIndex='100';
	      }
	      // This is the HTML which makes up the dialog box, it will be inserted into
	      // innerHTML later. We insert into a temporary variable because
	      // it's very, very slow doing multiple innerHTML injections, it's much
	      // more efficient to use a variable and then do one LARGE injection.
	      var tmp = '<div style="width: 100%; background-color: blue; color: white; font-family: verdana; font-size: 10pt; font-weight: bold; height: 20px">Input Required</div>';
	      tmp += '<div style="padding: 10px">'+innertxt + '<BR><BR>';
	      tmp += '<form action="" onsubmit="return that.wrapupPrompt()">';
	      tmp += '<input id="iepromptfield" name="iepromptdata" type=text size=46 value="'+def+'">';
	      tmp += '<br><br><center>';
	      tmp += '<input type="submit" value="&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;">';
	      tmp += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	      tmp += '<input type="button" onclick="that.wrapupPrompt(true)" value="&nbsp;Cancel&nbsp;">';
	      tmp += '</form></div>';
	      // Stretch the blackout division to fill the entire document
	      // and make it visible.  Because it has a high z-index it should
	      // make all other elements on the page unclickable.
	      _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
	      _blackoutPromptID.style.width='100%';
	      _blackoutPromptID.style.display='block';
	      // Insert the tmp HTML string into the dialog box.
	      // Then position the dialog box on the screen and make it visible.
	      _dialogPromptID.innerHTML=tmp;
	      _dialogPromptID.style.top=parseInt(document.documentElement.scrollTop+(screen.height/3))+'px';
	      _dialogPromptID.style.left=parseInt((document.body.offsetWidth-315)/2)+'px';
	      _dialogPromptID.style.display='block';
	      // Give the dialog box's input field the focus.
	      document.getElementById('iepromptfield').focus();
	   } else {
	      // we are not using IE7 so do things "normally"
	      promptCallback(prompt(innertxt,def));
	   }
	}

