﻿var SecureString = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
/* usage
var sb = new StringBuilder();
    sb.append("oho ");
    sb.append("aha");
var s = sb.toString();
*/
var cacheTime = new Date().getTime();
function StringBuilder(value)
{
    this.strings = new Array("");    
}

StringBuilder.prototype.append = function (value)
{
    this.strings.push(value);
}

StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}

StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}

function getRandom(min, max) {
     return Math.floor( Math.random() * max ) + min;
}

function GetRandomKey(maxCharacter)
{
    var sb = new StringBuilder();   

    for (var i = 0; i < maxCharacter; i++)
    {
        sb.append(SecureString.charAt(getRandom(0, SecureString.length)));
    }
    
    return sb.toString();
}

function ClearCharactersFromString(str){
    return str.replace(/'/g,"");
}

var Try = {
  These: function() {
    var returnValue;
    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;
  }
}

var Json = {
    StringToObject: function (str){
        if(str.length<1){
            return {};
        }        
        var s = eval("s = " + str + ";");                           
        return s;
    }
}; 
Array.prototype.exist = function(value){
	for(var i=0; i<this.length; i++){
		if(this[i] == value) return i;
	}
	return -1;
};

Array.prototype.remove=function(s){
  for(i=0;i<this .length;i++){
    if(s==this[i]) this.splice(i, 1);
  }
}

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

var $G = {
    $A: new Array(),
    $I:function(o){
        pk = GetRandomKey(5) + (new Date()).getTime();
        if(typeof(o)=="object") o.$ = pk;
        this.$A[pk] = o;
        return pk;
    }
}
// usage: $G.$I(obj);
// $G.$A[obj.$].a);

function copyClip(meintext)
{
    if(window.clipboardData) { window.clipboardData.setData("Text", meintext);   }
    else if (window.netscape) { 
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');   
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;      
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext=meintext;   
        str.data=copytext;
        trans.setTransferData("text/unicode",str,copytext.length*2);
        var clipid=Components.interfaces.nsIClipboard;
        if (!clip) return false;
            clip.setData(trans,null,clipid.kGlobalClipboard);
        }
   return false;
}

function hideDiv(divID)
{
    document.getElementById(''+divID+'').style.display = 'none';
}

function displayDiv(divID)
{
    document.getElementById(''+divID+'').style.display = 'block';
} 
function $(element) {return document.getElementById(element);}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
                var today = new Date();
                today.setTime( today.getTime() );
 
                expires = expires * 1000 * 60 * 60 * 1;
                var expires_date = new Date( today.getTime() + (expires) );
 
                document.cookie = name + "=" + escape( value ) +
                               ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
                               ( ( path ) ? ";path=" + path : "" ) + 
                               ( ( domain ) ? ";domain=" + domain : "" ) +
                               ( ( secure ) ? ";secure" : "" );
}
function Get_Cookie( name )
{
                var start = document.cookie.indexOf( name + "=" );
                var len = start + name.length + 1;
                if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
                {
                               return null;
                }
                if ( start == -1 )
                               return null;
                var end = document.cookie.indexOf( ";", len );
                if ( end == -1 )
                               end = document.cookie.length;
                return unescape( document.cookie.substring( len, end ) );
}
function Delete_Cookie( name, path, domain )
{
                if ( Get_Cookie( name ) )
                               document.cookie = name + "=" +
                                               ( ( path ) ? ";path=" + path : "") +
                                               ( ( domain ) ? ";domain=" + domain : "" ) +";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
