function success_handler(o) {
	replace_html('content', o.responseText);
}

function failure_handler(o) {
	replace_html('content', 'Server je momentálně nedostupný!');
}
// Handler userLogin Widget
function success_loginhandler(o) {
	replace_html('loginContent', o.responseText);
}
// Handler userLogin Widget
function failure_loginhandler(o) {
	replace_html('loginContent', 'Server je momentálně nedostupný!');
}
// Překóduje zdroj stránky
function replace_html(id, content) {
	document.getElementById(id).innerHTML = content;
}
// Zobrazení progress baru
function show_progressbar(id) {
	replace_html(id, '<img src="webFramework/objectInterface/graphicsObjects/loading.gif" border="0" alt="Načítám, prosím čekejte..." />');
}
// Přepnutí userLogin Widget - Zaslání nového kódu
function VerificationEmail() {
	show_progressbar('loginContent');
	var callback = { success:success_loginhandler,	failure:failure_loginhandler, timeout: 3600 };
	YAHOO.util.Connect.asyncRequest('GET', 'index.php?preload=frmNewVerification', callback);
}
// Přepnutí userLogin Widget - Verifikační email
function VerificationForm() {
	show_progressbar('loginContent');
	var callback = { success:success_loginhandler,	failure:failure_loginhandler, timeout: 3600 };
	YAHOO.util.Connect.asyncRequest('GET', 'index.php?preload=frmVerification', callback);
}
// Přepnutí userLogin Widget - Login Form
function LoginForm() {
	show_progressbar('loginContent');
	var callback = { success:success_loginhandler,	failure:failure_loginhandler, timeout: 3600 };
	YAHOO.util.Connect.asyncRequest('GET', 'index.php?preload=frmLogin', callback);
}
// Přepnutí userLogin Widget - Zapomenuté heslo
function ForeingForm() {
	show_progressbar('loginContent');
	var callback = { success:success_loginhandler,	failure:failure_loginhandler, timeout: 3600 };
	YAHOO.util.Connect.asyncRequest('GET', 'index.php?preload=frmForeing', callback);
}
// Vytvoření progress baru
var progress_bar = new Image();
progress_bar.src = 'webFramework/objectInterface/graphicsObjects/loading.gif';

// Nastavení widgetů
window.onload = function () {
    REDIPS.dialog.init();          // initialize dialog
    REDIPS.dialog.op_high = 40;    // set maximum transparency to 40%
    REDIPS.dialog.fade_speed = 18; // set fade spead (delay is 18ms)
}

// typy dialogů v aplikaci, které se budou zobrazovat
// Simple dialog
//// REDIPS.dialog.show(120, 100, 'Simple dialog') 
// Bigger dialog with renamed button
//// REDIPS.dialog.show(200, 200, 'Bigger dialog with renamed button', 'OK') 
// Dialog with two buttons
//// REDIPS.dialog.show(200, 100, 'Dialog with two buttons', 'Cancel', 'Yes') 
// Action on second button
//// REDIPS.dialog.show(200, 100, 'Action on second button', 'Cancel', 'Yes|button2') 
// Action on both buttons (with parameter on second button)
//// REDIPS.dialog.show(200, 100, 'Action on both buttons', 'Cancel|button1', 'Yes|button2|hello') 
// Dialog with image
//// REDIPS.dialog.show(100, 90, '/path/img1.png') 
// Dialog with more images
//// REDIPS.dialog.show(100, 90, '/path/img1.png,/path/img2.png,/path/img3.png') 
// Dialog with more images and text
//// REDIPS.dialog.show(100, 90, 'Hello!|/path/img1.png,/path/img2.png,/path/img3.png') 
// Dialog with image and text
//// REDIPS.dialog.show(150, 120, 'Have a nice day!|/path/spider1.png') 
// Dialog with image, text and action on second button
//// REDIPS.dialog.show(150, 120, 'Delete image?|/path/spider1.png', 'Cancel', 'Yes|button2')
show = function (width, height, text, button1, button2) {
    var input1 = '',    // define input1 button
        input2 = '',    // define input2 (optional) button
        param  = '',    // optional function parameter
        img_extensions, // needed for image search
        div_img = '',   // prepared DIV with images
        div_text = '',  // text wrapped with DIV
        img_text = '';  // text under image
    //  set button1 default value
    if (button1 === undefined) {
        button1 = 'Close';
    }
    // set dialog width, height and calculate central position
    dialog_width  = width;
    dialog_height = height;
    position();
    // if text ends with jpg, jpeg, gif or png, then prepare img tag
    img_extensions = /(\.jpg|\.jpeg|\.gif|\.png)$/i;
    if (img_extensions.test(text)) {
        // text can precede jpg, jpeg, gif or png image, so search for separator
        img_text = text.split('|');
        // separator doesn't exist, display only image without text
        if (img_text.length === 1) {
            div_img = image_tag(img_text[0]);
        }
        // otherwise, prepare image and text DIV
        else {
            div_img = image_tag(img_text[1]);
            div_text = '<div class="text">' + img_text[0] + '</div>';
        }
    }
    // else prepare text within DIV
    else {
        div_text = '<div class="text">' + text + '</div>';
    }
    // prepare input1 HTML
    input1 = input_html(button1);
    // prepare optional button2
    if (button2 !== undefined) {
        // prepare input2 HTML
        input2  = input_html(button2);
    }
    // set HTML for dialog box - use table to vertical align content inside
    // dialog box (this should work in all browsers)
    dialog_box.innerHTML = '<table><tr><td valign="center" height="' + height + '" width="' +
                            width + '">' + div_img +
                            div_text +
                            '<div>' + input1 + input2 + '</div>' +
                            '</td></tr></table>';
    // show shade and dialog box
    shade.style.display = dialog_box.style.display = 'block';
    // hide dropdown menus, iframes & flash
    visibility('hidden');
    // show shaded div slowly
    fade(REDIPS.dialog.op_low, 10);
};

// autocomplete text - zajistí autokompletační funkci
var div;             // reference to the div that contains iframe
var oInput;          // current input field
var timer = null;    // timer reference (needed to cancel previous call)
var delay = 500;     // time to wait after last typed char to send request (milliseconds)
var stayVisible = 0; // div visibility flag
var url = 'answers.php?query='; // autocomplete url

// set reference to the invisible div (after page is loaded)
window.onload = function () {div = document.getElementById('autocomplete')}

// called on keydown event in input field
function autocomplete(field, e){
  oInput = field; // set current input field
  switch(e.keyCode || window.event.keyCode){
    // tab, enter, shift, ctrl, alt, pause, caps lock
    case 9: case 13: case 16: case 17: case 18: case 19: case 20:
    // page up, page down, end, home, arrow left, arrow up, arrow right
    case 33:  case 34:  case 35:  case 36:  case 37:  case 38: case 39:
    case 45:  case 145: // insert, scroll lock
    case 112: case 113: case 114: case 115: case 116: case 117: // F1,F2,F3,F4,F5,F6
    case 118: case 119: case 120: case 121: case 122: case 123: // F7,F8,F9,F10,F11,F12
      break;
    case 27: hidePopup(); break; // escape (hide div element)
    case 40: setFocus();  break; // arrow down (set focus to the multiple select)
    case 8: case 46: default:    // backspace, delete and default
      if (timer != null) clearTimeout(timer);         // cancel previous call
      timer = setTimeout(function(){popup()}, delay); // set new popup call
  }
}
// div popup
function popup(){
  // if user deletes content from input box, then hide popup and return
  if (oInput.value.length == 0) {hidePopup(); return}
  // define oTop, oLeft and box variables
  var oTop = oLeft = 0;
  var box = oInput;
  // find input field position and set popup position
  do {oLeft+=box.offsetLeft; oTop+=box.offsetTop} while (box=box.offsetParent);
  div.style.top  = (oTop + 22) + 'px';
  div.style.left = oLeft + 'px';
  // find iframe in div and set src attribute with parameters query and field name
  div.getElementsByTagName('iframe')[0].src = url+oInput.value+'&fname='+oInput.name;
}
// set focus to the multiple select (contentWindow works for IE6 and FF)
function setFocus(){
  stayVisible = 1; // set stayVisible flag (needed for onblur event on input field)
  div.firstChild.contentWindow.document.getElementsByTagName('select')[0].focus();
}
// hide popup
function hidePopup(){
  if (stayVisible == 1) {stayVisible = 0; return} // don't hide if flag exisits
  if (timer != null) clearTimeout(timer);         // cancel previous call
  div.style.visibility = 'hidden';                // hide div
}
// called on keydown event in multiple select (from iframe)
function keydown(oSelect, e){
  switch(e.keyCode || window.event.keyCode){
    // escape, backspace (hide popup and set focus to the input field)
    case 27: case 8:  hidePopup(); oInput.focus(); break;
    // tab, return (set value to the input field)
    case 9:  case 13: setValue(oSelect); break;
  }
}
// set value to the input box (used by keydown and ondblclick event)
function setValue(oSelect){
  var idx = oSelect.selectedIndex;
  oInput.value = oSelect.options[idx].text;
  div.style.visibility = 'hidden';
  oInput.focus();
}
