
/*
* Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html
*/

//**********************************************************
//constants
//**********************************************************

var TOP_WEIGHT_VISIBLE = 5;
var TOP_WEIGHT_NO_VISIBLE = 10;

var POS_VISIBLE_IMG = 0;
var POS_COUPLE_IMG = 1;

var MODE_SWAP = 0;
var MODE_SWAP_EQ = 1;

var DEFAULT_SPEED = 100;
var DEFAULT_PAUSE = 1500;

var DEFAULT_MAX_TRY = 4;

var C_POS = 0;
var C_TOP = 1;
var C_NTOP = 2;
var C_IMG = 3;
var C_TOP_VISIBLE = 4;
var C_NTOP_VISIBLE = 5;
var C_TOP_NVISIBLE = 6;
var C_NTOP_NVISIBLE = 7;
var C_MAX_TOP2NTOP = 8;
var C_MIN_NTOP_NVISIBLE = 9;

//**********************************************************
//vars
//**********************************************************

var detectedBrowser = detectBrowser();

//**********************************************************
// Auxiliary functions
//**********************************************************

function anchor(href) {
  window.location.href = href;
}

function random(n) {
  return Math.floor(Math.random() * n);
}

function getElementsByRegExp(pattern,tagName) {
    var t = new Array();
    var tmp = new Array();
    var tagsResult = new Array();
    var j = 0;
    t = document.getElementsByTagName(tagName);
    for (var i in t) {
        if (t[i].id && pattern.test(t[i].id) && ! tmp[t[i].id]) {
            tagsResult[j++] = t[i];
            tmp[t[i].id] = true;
        }
    }
    return tagsResult;
}

function detectBrowser()
{
  var browser = 'unknown';
  var b_version = navigator.appVersion;
  var version = parseFloat(b_version);
  var pattern = /(Safari|MSIE|Chrome)/i;
  if( pattern.test(b_version) ) {
    var matches = b_version.match(pattern);
    var browser = matches[1];
    if( browser.toUpperCase() == 'MSIE' ) {
      var pattern = /MSIE\s*([0-9.,]+)/i;
      var matches = b_version.match(pattern);
      var version = matches[1];
    }
  } else {
    var pattern = /(Netscape)/i;
    if( pattern.test(navigator.appName) ) {
      var browser = 'Firefox';
    } else {
      var pattern = /(Opera)/i;
      if( pattern.test(navigator.appName) ) var browser = 'Opera';
    }  
  }
  return { browser: browser, version: version };
}

function getPermutation(p) {
  var perm = new Array();
  for( var i=0; i<p; i++ ) perm[i] = i;
  var changes = Math.floor(Math.random() * p/2);
  for( var k=0; k<changes; k++ ) {
    var i = Math.floor(Math.random() * p);
    var j = Math.floor(Math.random() * p);
    if (i == j) {
      j++;
      if (j == p) j = 0;
    }
    var temp = perm[i];
    perm[i] = perm[j];
    perm[j] = temp;
  }
  return perm;
}

function createDelegate(that, thatMethod)
{   
  var args = [];
  var l = arguments.length;
  if (l > 2) {
     for (var i = 2; i < l; i++) {
        args[i-2] = arguments[i];
     }
  }
	return function() { return thatMethod.apply(that, args); }
}

//**********************************************************
// ClassImg
//**********************************************************

function ClassImg(debug) {
  //vars
  this.className = 'ClassImg';
  this.debug = debug;
}

ClassImg.prototype.CreateCopyOfImage = function(img) {
  var newImg = document.createElement('IMG');
  newImg.src = img.src;
  newImg.onclick = img.onclick;
  newImg.alt = img.alt;
  newImg.title = img.title;
  newImg.top = img.top;
  newImg.style.display = img.style.display;
  this.SetOpacity(newImg,img.opacity);
  if( this.debug ) { 
    newImg.countV = img.countV;
    newImg.countNV = img.countNV;
    newImg.maxNV = img.maxNV;
    newImg.style.border = img.style.border;
  }  
  return newImg;
}

ClassImg.prototype.SetOpacity = function(img, opacity) {
    img.opacity = opacity;
    img.style.opacity = (opacity / 100);
    img.style.MozOpacity = (opacity / 100);
    img.style.KhtmlOpacity = (opacity / 100);
    img.style.filter = 'alpha(opacity=' + opacity + ')';
}

//**********************************************************
// ClassReplaceImages
//**********************************************************

// konstruktor
function ClassReplaceImages(countTOPImg,countMaxTOP2NTOP,debug) {
  
  // vars
  this.debug = debug;
  this.cImg = new ClassImg(this.debug);
  this.className = 'ClassReplaceImages';
  if(this.debug) this.elDebug = this.PRIVATE_InitDebug();
  this.Images = new Array();
  this.Positions = new Array();
  this.count = new Array();
  this.count[C_TOP] = Math.abs(new Number(countTOPImg));
  this.count[C_MAX_TOP2NTOP] = Math.abs(new Number(countMaxTOP2NTOP));
  this.mode = null;
  this.speed = DEFAULT_SPEED;
  this.pause = DEFAULT_PAUSE;
  this.initResult = null;
  this.lastP = new Array();
  for( var i=0; i < DEFAULT_MAX_TRY - 1; i++ ) this.lastP[i] = -1;
 
  // inicializace
  this.initResult = this.PRIVATE_Init();
}

// Init
ClassReplaceImages.prototype.PRIVATE_Init = function() {
  // naplneni pole Positions
  var pattern = /^blend([0-9]+)$/i;
  this.Positions = getElementsByRegExp(pattern,'DIV');
  if( !this.Positions.length ) return false;
  if(this.debug) {
    for(var p=0; p < this.Positions.length; p++)
      this.Positions[p].style.border = '1px solid gray';
  }
  // naplneni pole Images (z pozice 0)
  this.Images[0] = new Array();
  var i = 0;
  var el = this.Positions[0].firstChild;
  while (el && el.nodeName.toUpperCase() != 'IMG') el = el.nextSibling;
  if( !el ) return false;
  this.Images[0][i++] = el;
  while( el ) {
    do el = el.nextSibling; while (el && el.nodeName != 'IMG');
    if( el ) this.Images[0][i++] = el;
  }    
  // pokud je pozic vice nez obrazku, zmensi pocet pozic 
  while( this.Positions.length > this.Images[0].length ) {
    var el = this.Positions.pop();
    el.style.display = 'none';
  }
  if( this.count[C_TOP] > this.Images[0].length ) this.count[C_TOP] = this.Images[0].length;
  // inicializace vlastnosti obrazku
  for (var i=0; i < this.Images[0].length; i++ ) {
    this.Images[0][i].style.display = 'none';
    this.Images[0][i].top = (i < this.count[C_TOP]) ? 1 : 0;
    this.cImg.SetOpacity(this.Images[0][i],0);
    if( this.debug ) { 
      this.Images[0][i].countV = 0;
      this.Images[0][i].countNV = 0;
      this.Images[0][i].maxNV = 0;
      if(this.Images[0][i].top)
        this.Images[0][i].style.border = '3px solid red';
      else
        this.Images[0][i].style.border = '3px solid green';
    }  
  }
  // kopirovani nactenych obrazku na dalsi pozice
  for (var p=1; p < this.Positions.length; p++ ) {
    this.Images[p] = new Array();
    for (var i=0; i < this.Images[0].length; i++ ) {
        this.Images[p][i] = this.cImg.CreateCopyOfImage(this.Images[0][i]);
        this.Positions[p].appendChild(this.Images[p][i]);
    }
  }
  // zjisteni poctu
  this.count[C_POS] = this.Positions.length;
  this.count[C_IMG] = this.Images[0].length;
  this.count[C_TOP_VISIBLE] = (this.count[C_POS] < this.count[C_TOP] ) ? this.count[C_POS] : this.count[C_TOP];
  this.count[C_TOP_NVISIBLE] = this.count[C_TOP] - this.count[C_TOP_VISIBLE];
  this.count[C_NTOP] = this.count[C_IMG] - this.count[C_TOP];
  this.count[C_NTOP_VISIBLE] = (this.count[C_NTOP] > 0 && this.count[C_POS] > this.count[C_TOP_VISIBLE]) ? this.count[C_POS] - this.count[C_TOP_VISIBLE] : 0;
  this.count[C_NTOP_NVISIBLE] = this.count[C_NTOP] - this.count[C_NTOP_VISIBLE];
  // inicializace vlastnosti obrazku podle zjistenych poctu  
  var perm = getPermutation(this.Positions.length);
  var j = 0;
  for(var i = 0; i < this.count[C_TOP_VISIBLE]; i++) {
    var p = perm[j];
    this.Images[p][i].style.display = '';
    this.cImg.SetOpacity(this.Images[p][i],100);
    this.Positions[p].visible = i;
    j++;
  }
  for(var i = this.count[C_TOP]; i < this.count[C_TOP] + this.count[C_NTOP_VISIBLE]; i++) {
    var p = perm[j];
    this.Images[p][i].style.display = '';
    this.cImg.SetOpacity(this.Images[p][i],100);
    this.Positions[p].visible = i;
    j++;
  }
  // zjisteni modu
  this.mode = (this.count[C_IMG] == this.count[C_POS]) ? MODE_SWAP_EQ : MODE_SWAP;
  if(this.mode == MODE_SWAP_EQ || this.count[C_TOP] == 0 || this.count[C_NTOP] == 0) return true;
  if( this.count[C_TOP_VISIBLE] > 0 && this.count[C_NTOP_NVISIBLE] > 0 ) {
    if( this.count[C_MAX_TOP2NTOP] <= 0 ) this.count[C_MAX_TOP2NTOP] = 1;
    if( this.count[C_MAX_TOP2NTOP] > this.count[C_TOP_VISIBLE] ) this.count[C_MAX_TOP2NTOP] = this.count[C_TOP_VISIBLE];
    if( this.count[C_MAX_TOP2NTOP] > this.count[C_NTOP_NVISIBLE] ) this.count[C_MAX_TOP2NTOP] = this.count[C_NTOP_NVISIBLE];
    this.count[C_MIN_NTOP_NVISIBLE] = this.count[C_NTOP_NVISIBLE] - this.count[C_MAX_TOP2NTOP];
    if( this.count[C_MIN_NTOP_NVISIBLE] < 0 ) { alert('ERROR C_MIN_NTOP_NVISIBLE=' + this.count[C_MIN_NTOP_NVISIBLE]); return false; }
  } else {
    this.count[C_MAX_TOP2NTOP] = null;
    this.count[C_MIN_NTOP_NVISIBLE] = 0;
  }
  return true;
}

// Start - spusti vymenu obrazku
ClassReplaceImages.prototype.Start = function(speed, pause) {
  if( ! this.initResult ) return false;
  this.speed = (speed == null) ? DEFAULT_SPEED : Math.abs(speed);
  this.pause = (pause == null) ? DEFAULT_PAUSE : Math.abs(pause);
  this.PRIVATE_SetActualStateOfImages();
  if( this.debug ) this.Debug(true);
  switch (this.mode) {
      case MODE_SWAP_EQ:
        var p1 = Math.floor(Math.random() * this.Positions.length);
        var p2 = Math.floor(Math.random() * this.Positions.length);
        if (p1 == p2) p2--;
        if (p2 < 0) p2 = this.Positions.length - 1;
        this.Positions[p1].couple = this.Positions[p2].visible;
        this.Positions[p2].couple = this.Positions[p1].visible;
        this.PRIVATE_SwapImages(p1, POS_VISIBLE_IMG, this.speed);
        this.PRIVATE_SwapImages(p2, POS_VISIBLE_IMG, this.speed);
        setTimeout(createDelegate(this, this.PRIVATE_Wait, p1, p2), this.pause);
        break;
      case MODE_SWAP:
        var kinds = null;
        if( this.count[C_NTOP_NVISIBLE] > this.count[C_MIN_NTOP_NVISIBLE] ) {
          var kinds = { 0: {kind: C_TOP_VISIBLE, weight: 1}, 1: {kind: C_NTOP_VISIBLE, weight: 2} };
        } else if ( this.count[C_NTOP_NVISIBLE] == this.count[C_MIN_NTOP_NVISIBLE] ) {
          var kinds = { 0: {kind: C_NTOP_VISIBLE, weight: 1} };
        } else if ( this.count[C_TOP] == 0 ) {
          var kinds = { 0: {kind: C_NTOP_VISIBLE, weight: 1} };
        } else if ( this.count[C_NTOP] == 0 ) {
          var kinds = { 0: {kind: C_TOP_VISIBLE, weight: 1} };
        } else {
          alert("ERROR MODE_SWAP");
        }        
        var countTry = 0;
        do {
          var i1 = this.PRIVATE_GetRandomImg( kinds );
          var i2 = this.PRIVATE_GetRandomImg( {0: {kind: C_TOP_NVISIBLE, weight: 2}, 1: {kind: C_NTOP_NVISIBLE, weight: 1}} );
          if( i1 === false || i2 === false ) { alert(' ERROR: i1=' + i1 + ', i2=' + i2); return false; }
          var p = this.PRIVATE_GetPositionOfImg(i1);
          if( p === false ) { alert(' ERROR: p=' + p); return false; }
          countTry++;
        } while(countTry < DEFAULT_MAX_TRY && this.PRIVATE_IsInLastPositions(p) );
        this.PRIVATE_Set2LastPositions(p);
        this.Positions[p].couple = i2;
        this.PRIVATE_SwapImages(p, POS_VISIBLE_IMG, this.speed);
        setTimeout(createDelegate(this, this.PRIVATE_Wait, p, p), this.pause);
        break;
  }
  return true;
}

// IsInLastPositions - zjisti, zda vygenerovana pozice byla vygenerovana poslednich DEFAULT_MAX_TRY-1 pozicich
ClassReplaceImages.prototype.PRIVATE_IsInLastPositions = function(p) {
  for( var i = 0; i < this.lastP.length; i++ ) {
    if( this.lastP[i] == p ) return true;
  }
  return false;
}

// Set2LastPositions - zadej dalsi vygenerovanou pozici
ClassReplaceImages.prototype.PRIVATE_Set2LastPositions = function(p) {
  for( var i = 0; i < this.lastP.length; i++ ) {
    if( this.lastP[i] < 0 ) {
      this.lastP[i] = p;
      return;
    }  
  }
  for( var i = 1; i < this.lastP.length; i++ ) 
    this.lastP[i-1] = this.lastP[i];
  this.lastP[this.lastP.length-1] = p;
  return;
}

// SetActualStateOfImages - zjisti aktualni stav vlastnosti obrazku
ClassReplaceImages.prototype.PRIVATE_SetActualStateOfImages = function() {
  for( var i=0; i<this.Images[0].length; i++) this.Images[0][i].actual = null;
  this.count[C_TOP_VISIBLE] = 0;
  this.count[C_NTOP_VISIBLE] = 0;
  this.count[C_TOP_NVISIBLE] = 0;
  this.count[C_NTOP_NVISIBLE] = 0;
  for( var p=0; p<this.Positions.length; p++) {
    var i = this.Positions[p].visible;
    if( this.Images[0][i].top ) {
      if( this.Images[0][i].actual !== null ) { alert('ERROR TV' + i); return false; }
      this.Images[0][i].actual = C_TOP_VISIBLE;
      this.count[C_TOP_VISIBLE]++;
    } else {
      if( this.Images[0][i].actual !== null ) { alert('ERROR NTV' + i); return false; }
      this.Images[0][i].actual = C_NTOP_VISIBLE;
      this.count[C_NTOP_VISIBLE]++;
    }  
    if( this.debug ) { 
      this.Images[0][i].countV++;
      this.Images[0][i].countNV = 0;  
    }  
  }  
  for( var i=0; i<this.Images[0].length; i++) {
    if( this.Images[0][i].actual !== null ) continue;
    if( this.Images[0][i].top ) {
      if( this.Images[0][i].actual !== null ) { alert('ERROR TNV' + i); return false; }
      this.Images[0][i].actual = C_TOP_NVISIBLE;
      this.count[C_TOP_NVISIBLE]++;
    } else {
      if( this.Images[0][i].actual !== null ) { alert('ERROR NTNV' + i); return false; }
      this.Images[0][i].actual = C_NTOP_NVISIBLE;
      this.count[C_NTOP_NVISIBLE]++;
    }
    if( this.debug ) { 
      this.Images[0][i].countNV++;
      if( this.Images[0][i].maxNV < this.Images[0][i].countNV ) this.Images[0][i].maxNV = this.Images[0][i].countNV;
    }
  }
  var s = '';
  for( var i=0; i<this.Images[0].length; i++) {
   s += (this.Images[0][i].actual===null) + ',';
  }
  var n = this.count[C_TOP_VISIBLE] + this.count[C_NTOP_VISIBLE] + this.count[C_TOP_NVISIBLE] + this.count[C_NTOP_NVISIBLE];
  if( n != this.Images[0].length) {
    alert('ERROR SET ACTUAL (' + n + ') ' + s);  
    return true;
  }  
  return true;  
}

// GetRandomImg - vybere nahodne obrazek zadanych vlastnosti
ClassReplaceImages.prototype.PRIVATE_GetRandomImg = function(kinds) {
  var KindsNotEmpty = new Array();
  var sumWeight = 0;
  for( var i in kinds ) {
    if( this.count[kinds[i].kind] > 0 ) {
      KindsNotEmpty.push(kinds[i]);
      sumWeight += kinds[i].weight;
    }  
  }
  if( !KindsNotEmpty.length ) {
    alert('ERROR: ALL count of kinds is 0');
    return false;
  }
  var tmp = Math.floor(Math.random() * sumWeight);
  var sumW = 0;
  var kind = null;
  for( var i in KindsNotEmpty ) {
    sumW += KindsNotEmpty[i].weight;
    if(tmp < sumW) {
      var kind = KindsNotEmpty[i].kind;
      break;
    }
  }
  if(kind === null) alert("ERROR: kind is null");
  var index = Math.floor(Math.random() * this.count[kind]);
  var j = index;
  for( var i=0; i<this.Images[0].length; i++) {
    if( this.Images[0][i].actual == kind ) {
      if(!j) return i;
      j--;
    }  
  }
  alert('ERROR: KindsNotEmpty=' +  KindsNotEmpty + ', index=' + index + ', j=' + j);
  return false;
}

// GetPositionOfImg - najde pozici, na ktere je obrazek zobrazen
ClassReplaceImages.prototype.PRIVATE_GetPositionOfImg = function(index) {
  for( var p=0; p<this.Positions.length; p++) {
    if( this.Positions[p].visible == index ) return p;
  }
  return false;
}

// SwapImages - zajisti prohozeni dvou obrazku
ClassReplaceImages.prototype.PRIVATE_SwapImages = function(p, pos_which_img, speed) {
    var elPos = this.Positions[p];
    switch (pos_which_img) {
        case POS_VISIBLE_IMG:
            var image = this.Images[p][elPos.visible]
            var opacity = image.opacity - 3;
            break;
        case POS_COUPLE_IMG:
            var image = this.Images[p][elPos.couple]
            var opacity = image.opacity + 3;
            break;
    }
    if (opacity < 0) opacity = 0; else if (opacity > 100) opacity = 100;
    this.cImg.SetOpacity(image, opacity);
    if (opacity > 0 && opacity < 100) {
        elPos.swap = setTimeout(createDelegate(this, this.PRIVATE_SwapImages, p, pos_which_img, this.speed), this.speed);
    } else {
        switch (pos_which_img) {
            case POS_VISIBLE_IMG:
                this.PRIVATE_SetSwappedImageProperty(p, POS_VISIBLE_IMG, null, 'none');
                this.PRIVATE_SetSwappedImageProperty(p, POS_COUPLE_IMG, null, '');
                elPos.swap = setTimeout(createDelegate(this, this.PRIVATE_SwapImages, p, POS_COUPLE_IMG, this.speed), this.speed);
                break;
            case POS_COUPLE_IMG:
                elPos.visible = elPos.couple;
                elPos.couple = null;
                elPos.swap = null;
                break;
        }
    }
}

// SetSwappedImageProperty - set image properties
ClassReplaceImages.prototype.PRIVATE_SetSwappedImageProperty = function(p, pos_which_img, opacity, display) {
    var elPos = this.Positions[p];
    switch (pos_which_img) {
        case POS_VISIBLE_IMG:
            var image = this.Images[p][elPos.visible];
            break;
        case POS_COUPLE_IMG:
            var image = this.Images[p][elPos.couple];
            break;
    }
    if (opacity != null) this.cImg.SetOpacity(image, opacity);
    if (display != null) image.style.display = display;
}

// Wait - cekani na dokonceni prohozeni dvou obrazku
ClassReplaceImages.prototype.PRIVATE_Wait = function(p1, p2) {
   if(this.Positions[p1].swap !== null || this.Positions[p2].swap !== null) {
    setTimeout(createDelegate(this, this.PRIVATE_Wait, p1, p2), this.speed);
   } else {
    setTimeout(createDelegate(this, this.Start, this.speed, this.pause), this.pause);
   }
}

// Init debug
ClassReplaceImages.prototype.PRIVATE_InitDebug = function() {
  var table = document.createElement("TABLE");
  table.id = "ClassReplaceImages_Debug";
  table.align = "left";
  var tbody = document.createElement("TBODY");
  var tr = document.createElement("TR");
  var td = document.createElement("TD");
  var textarea = document.createElement("TEXTAREA");
  textarea.id = "ClassReplaceImages_Debug_TEXT";
  textarea.rows = 30;
  textarea.cols = 30;
  td.appendChild(textarea);
  tr.appendChild(td);
  tbody.appendChild(tr);
  table.appendChild(tbody);
  var body = document.getElementsByTagName("BODY");
  body[0].insertBefore(table,body[0].firstChild);
  var elDebug = document.getElementById(this.className + '_Debug');
    if(elDebug) {
      elDebug.style.display = '';
      elDebug = document.getElementById(this.className + '_Debug_TEXT');
      this.crLf = String.fromCharCode(13,10);
      elDebug.value = '';
    }
  return elDebug  
}
// Debug
ClassReplaceImages.prototype.Debug = function(clear) {
  if( !this.elDebug ) return false;
  var str = clear ? '' : this.elDebug.value;
  var name = '';
  /**/
  for(var i in this.count) {
    switch(i*1) {
      case C_POS: name = 'C_POS'; break;
      case C_TOP: name = 'C_TOP'; break;
      case C_NTOP: name = 'C_NTOP'; break;
      case C_IMG: name = 'C_IMG'; break;
      case C_TOP_VISIBLE: name = 'C_TOP_VISIBLE'; break;
      case C_NTOP_VISIBLE: name = 'C_NTOP_VISIBLE'; break;
      case C_TOP_NVISIBLE: name = 'C_TOP_NVISIBLE'; break;
      case C_NTOP_NVISIBLE: name = 'C_NTOP_NVISIBLE'; break;
      case C_MAX_TOP2NTOP: name = 'C_MAX_TOP2NTOP'; break;
      case C_MIN_NTOP_NVISIBLE: name = 'C_MIN_NTOP_NVISIBLE'; break;
    }
    str += name + '(' + i + ')=' + this.count[i] + this.crLf;
  }
  str += 'mode='; 
  switch( this.mode ) {
    case MODE_SWAP_EQ:
      str += 'MODE_SWAP_EQ';
      break;
    case MODE_SWAP:
      str += 'MODE_SWAP';
      break;
    default:  
      str += 'ERROR';
  }
  str += this.crLf; 
  str += 'initResult=' + this.initResult + this.crLf; 
  str += '===========================' + this.crLf; 
  /**/  
  str += 'C_TOP_VISIBLE: '; 
  for(var i=0; i<this.Images[0].length; i++) if( this.Images[0][i].actual == C_TOP_VISIBLE ) 
    str+= i + '(' + this.Images[0][i].countV + '-' + this.Images[0][i].maxNV + '),';
  str += this.crLf; 
  str += 'C_TOP_NVISIBLE: '; 
  for(var i=0; i<this.Images[0].length; i++) if( this.Images[0][i].actual == C_TOP_NVISIBLE ) 
    str+= i + '(' + this.Images[0][i].countV + '-' + this.Images[0][i].maxNV + '),';
  str += this.crLf; 
  str += 'C_NTOP_VISIBLE: '; 
  for(var i=0; i<this.Images[0].length; i++) if( this.Images[0][i].actual == C_NTOP_VISIBLE ) 
    str+= i + '(' + this.Images[0][i].countV + '-' + this.Images[0][i].maxNV + '),';
  str += this.crLf; 
  str += 'C_NTOP_NVISIBLE: '; 
  for(var i=0; i<this.Images[0].length; i++) if( this.Images[0][i].actual == C_NTOP_NVISIBLE ) 
    str+= i + '(' + this.Images[0][i].countV + '-' + this.Images[0][i].maxNV + '),';
  str += this.crLf; 
  str += '===========================' + this.crLf; 
  this.elDebug.value = str;  
  return true;
}

