/**
* Generates random images on main page.
* @param aNumOfImages The number of random images in directory:
*                     'images/misc/'. Name of images in this folder
                      must be 1.jpg, 2.jpg, 3.jpg....
*/
function RandomImages( aNumOfImages )
     {
     var num = Math.round( Math.random()*( aNumOfImages )  );
     document.write('<img src=\"images/mainpage_random_images/'+num+'.jpg\" alt=\"\" class=\"image_border\" />');
     }

/**
* @param aImageIdPrefix The prefix of the Id of the image
* @param aNumOfImages How many images thre are with this prefix
* @param aScaleValue New hight of the image
*/
function ScaleImage( aImageIdPrefix, aNumOfImages, aHeight )
     {
     for ( i=1; i<=aNumOfImages;i++ )
          {
          var img = document.getElementById( aImageIdPrefix+i );
          if ( img != null )
               {
               img.style.height = aHeight+'px';
               }
          }
     }

function GetImageHeight( aImage )
     {
     image = new Image();
     image.src =(aImage);

     return image.height;
     }

function GetImageWidth( aImage )
     {
     image = new Image();
     image.src =(aImage);

     return image.width;
     }

function ShowDiv( id )
     {
     // safe function to show an element with a specified id
     if (document.getElementById)
          { // DOM3 = IE5, NS6
          document.getElementById(id).style.display = 'block';
          }
     else
          {
          if (document.layers)
               { // Netscape 4
               document.id.display = 'block';
               }
          else
               { // IE 4
               document.all.id.style.display = 'block';
               }
          }
     }

function HideDiv(id)
     {
     //safe function to hide an element with a specified id
     if (document.getElementById)
          { // DOM3 = IE5, NS6
          document.getElementById(id).style.display = 'none';
          }
     else
          {
          if (document.layers)
               { // Netscape 4
               document.id.display = 'none';
               }
          else
               { // IE 4
               document.all.id.style.display = 'none';
               }
          }
     }

/**
* Usage:
* <a href="javascript:show_hide('bird_flu_details');">A Bird Flu vaccine to be discovered</a>
* <div id="bird_flu_details" style="display:none">
*/
function ShowHideDiv(id, show)
     {
     if (el = document.getElementById(id))
          {
          if (null==show) show = el.style.display=='none';
          el.style.display = (show ? '' : 'none');
          }
     }

function PopupWindow( aWidth, aHeight, aUrl )
     {
     var w = aWidth; // window width
     var h = aHeight; // window height
     // Position window to screen center.
     var t=window.screen.height/2-(h/2); // window`s top position
     var l=window.screen.width/2-(w/2); // window`s left position
     // Open window.
     winid=window.open(aUrl,'guide','scrollbars=no,top='+t+',left='+l+',width='+w+',height='+h+'');
     winid.focus();
     }
function PopupWindow2( aWidth, aHeight, aUrl )
     {
     var w = aWidth; // window width
     var h = aHeight; // window height
     // Position window to screen center.
     var t=window.screen.height/2-(h/2); // window`s top position
     var l=window.screen.width/2-(w/2); // window`s left position
     // Open window.
     winid=window.open(aUrl,'guide','scrollbars=yes,top='+t+',left='+l+',width='+w+',height='+h+'');
     winid.focus();
     }

function OpenWindow( aUrl )
     {
     url = new String( aUrl );

     // Add schema if missing
     if ( ( 'http://' != url.substr( 0, 7 ) ) && ( 'https://' != url.substr( 0, 8 ) ) )
          {
          url = 'http://' + url;
          }

     winid=window.open( url );
     winid.focus();
     }

function OpenWindow2( aUrl )
     {
     winid=window.open( aUrl );
     winid.focus();
     }

function CheckTextareaLength( aTextarea, aMaxLength, aCounter )
     {
     var text = aTextarea.value;
     aCounter.value =  text.length+1;
     if ( text.length+1 >= aMaxLength ) //i is the maxlength of textarea which we have set to 80
          {
          alert('Maximum 255 karaktert tartalmazhat az üzenet!');
          aTextarea.value = text.substr( 0,aMaxLength-1 );
          return;
          }
     }

function TrimLeft( sString )
     {
     while ( sString.substring( 0, 1 ) == ' ' )
          {
          sString = sString.substring( 1, sString.length );
          }
     return sString;
     }

function TrimRight( aString )
     {
     while( aString.substring( aString.length-1, aString.length ) == ' ' )
          {
          aString = aString.substring( 0, aString.length-1 );
          }
     return aString;
     }

function ChangeShirtImage( aShirtId, aColorId )
     {
     document.getElementById( 'shirt_image_'+aShirtId ).setAttribute('src', 'images/shirts/'+aShirtId+'/'+aColorId+'.jpg');
     document.getElementById( 'shirt_anchor_'+aShirtId ).setAttribute('href', 'javascript: OpenWindow( \''+document.location.hostname+'/images/shirts/'+aShirtId+'/'+aColorId+'.jpg\');');
     }


function Trim( aString )
     {
     while ( aString.substring(0,1) == ' ' )
          {
          aString = aString.substring( 1, aString.length );
          }
     while ( aString.substring( aString.length-1, aString.length ) == ' ' )
          {
          aString = aString.substring( 0, aString.length-1 );
          }
     return aString;
     }

function CharCounter( aInputField, aMaxLength, counterElementId )
    {
    var str = aInputField.value;
    var length = aInputField.value.length;

    if ( length > aMaxLength )
        {
        str = str.substring( 0, aMaxLength );
        aInputField.value = str;
        }

    counter = document.getElementById( counterElementId );
    var size = aMaxLength - length;
    if ( size < 0 ) size = 0;
    counter.innerHTML = size;
    }

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

var handleYes = function() {
 this.hide();
}

var handleNo = function() {
 this.hide();
}

function MessageBox( aTitle, aMessage )
    {
    var myButtons = [ { text:"OK", handler:handleYes } ];

    mySimpleDialog = new YAHOO.widget.SimpleDialog("dlg", {
        width: "20em",
        fixedcenter:true,
        modal:true,
        close:false,
        draggable:false });
    mySimpleDialog.setHeader( aTitle );
    mySimpleDialog.setBody( aMessage );
    mySimpleDialog.cfg.queueProperty("buttons", myButtons);
    mySimpleDialog.render(document.body);
    mySimpleDialog.show();
    }

function WordWrap( aText, aMaxWordLength, aBreakString )
    {
    var words = aText.split( ' ' ); // Split words to array

    for ( arrayIndex = 0; arrayIndex < words.length; arrayIndex++ )
        {
        if ( words[arrayIndex].length > aMaxWordLength ) // If word is longer than max length
            {
            var newWords = new String(); 
            var charCounter = 0;

            // Go through chars ow word
            for ( charIndex = 0; charIndex < words[arrayIndex].length; charIndex++ )
                {
                // If word limit is reached
                if ( charCounter == aMaxWordLength ) 
                    {
                    // Add break string and the next char in line
                    newWords += aBreakString + words[arrayIndex].charAt(charIndex);
                    charCounter = 0; // Reset char counter 
                    }
                else
                    {
                    // Add next char in line
                    newWords += words[arrayIndex].charAt(charIndex);
                    }
                charCounter++;
                }
            // Assign the breaked string replacing the original.
            words[arrayIndex] = newWords;
            }
        }
    // Joint the array back into a string. 
    return words.join( ' ' );
    }    
