ObjectIdGenerator = Class.create();
ObjectIdGenerator.prototype = {
    initialize: function() {
        this.id = 0;
    },
    setId: function(id) {
        this.id = id;
    },
    getId: function() {
        return ++this.id;
    }
};
ObjectIdGenerator = new ObjectIdGenerator();

CommonUtils = Class.create();
CommonUtils.showErrorMsgFromHtml = function(html) {
    html = html.slice(html.search('<p class="error">') + 17);
    html = html.slice(0, html.search('</p>'));
    if (html != '') {
        alert(html);
    }
}
CommonUtils.cursorWait = function() {
    document.body.style.cursor = "wait";
}
CommonUtils.cursorDefault = function() {
    document.body.style.cursor = "default";
}

XMLUtils = Class.create();
XMLUtils.getXMLValue = function(element) {
    if (element.firstChild != null) {
        return element.firstChild.nodeValue;
    } else {
        return '';
    }
}

URLUtils = Class.create();
URLUtils.hasParams = function(url) {
	var reg = new RegExp("\\?");
	return reg.test(url);
}

StringUtils = Class.create();
StringUtils.startsWith = function(string, pattern) {
    var reg = new RegExp("^" + pattern);
    return reg.test(string);
}
StringUtils.endsWith = function(string, pattern) {
    // prototype function
    return string.endsWith(pattern);
}
StringUtils.nl2br = function(string) {
    if (string == null) {
        return '';
    }
    var reg = new RegExp(/\n/g);
    return string.replace(reg, '<br/>');
}
StringUtils.errorObjectToString = function(object) {
    var buffer = '';
    for (var i in object) {
        buffer += object[i] + "\n";
    }
    return buffer;
}
StringUtils.isEmail = function(string) {
    var reg = new RegExp("^.*@.*$");
    return reg.test(string);
}

ObjectUtils = Class.create();
ObjectUtils.dynamicHeight = function(element, maxHeight, parent) {
	var obj = $(element);
	var tol = 15;
	var parentObj = $(parent);
	var diff = parentObj.offsetHeight - obj.offsetHeight;
	
	if ((diff+maxHeight) > 264) {
		maxHeight = (maxHeight - ((diff+maxHeight) - 264)) - tol;
	}			
		
	if (obj.offsetHeight > maxHeight) {
		obj.style.height = maxHeight + "px";
	}
}
ObjectUtils.empty = function(object) {
    if (object == null || object == 'undefined' || object == '') {
        return true;
    }
    return false;
}

FileUtils = Class.create();
FileUtils.getFileSize = function(fileSize) {
	var size = '';
	if (fileSize >= 1073741824) {
		size = Math.round(fileSize / 1024 / 1024 / 1024) + " GB";
	} else if (fileSize >= 1048576) {
		size = Math.round(fileSize / 1024 / 1024) + " MB";
	} else if (fileSize >= 1024) {
		size = Math.round(fileSize / 1024) + " KB";
	} else {
		size = fileSize + " Bytes";
	}
	return size;
}

/**
 * This Class makes a request and calls back on caller, by the given object and the function given as string.
 */
 
// @todo: CommonUtils.showErrorMsgFromHtml(text);
RequestUtils = Class.create();
RequestUtils.prototype = {
    initialize: function() {
    },
	request: function(callerObject, callbackFunction, url, params) {
	    params = params || {};
		var opt = {
		    method: 'post',
		    parameters: params,
		    onSuccess: function(t) {
		        callerObject[callbackFunction](json_parse(t.responseText));
		    }
		};
	
	    Logger.debug('request to [' + url + ']');
		new Ajax.Request(url, opt);
    }
};
RequestUtils = new RequestUtils();

Logger = Class.create();
Logger.isDebug = false;
Logger.debugType = 'alert';
Logger.enable = function(debugType) {
    Logger.isDebug = true;
    Logger.debugType = debugType || 'alert';
    Logger.debug('Logging enabled');
}
Logger.debug = function(msg) {
    if (Logger.isDebug) {
        if (Logger.debugType == 'console') {
            console.debug(msg);
        } else if (Logger.debugType == 'alert') {
            alert(msg);
        }
    }
}
 
/*
 * HTML Output
 */
HTMLDateOutput = Class.create();
HTMLDateOutput.prototype = {
    initialize: function() {
        this.months = {
            '01': 'Jan',
            '02': 'Feb',
            '03': 'Mar',
            '04': 'Apr',
            '05': 'May',
            '06': 'Jun',
            '07': 'Jul',
            '08': 'Aug',
            '09': 'Sep',
            '10': 'Oct',
            '11': 'Nov',
            '12': 'Dec'
        };
    },
    toDate: function(value) {
        if (value == null || value == '') {
            return '';
        }
        var splitted = value.split('-');
        
        return this.months[splitted[1]] + ' ' + splitted[2] + ', ' + splitted[0];
    },
    toDateMonth: function(value) {
        if (value == null || value == '') {
            return '';
        }
        var splitted = value.split('-');
        
        return this.months[splitted[1]] + ', ' + splitted[0];
    }
}
HTMLDateOutput = new HTMLDateOutput();

/*
 * Functions
 */
function forwardToSearchAgent(queryElementId, agentElements) {

    var url = 'search.SearchAgentCreate.html';
    
    var query = $(queryElementId).value;
    
    if (query == '') {
        alert('You have to specify a search query.');
        return;
    }
    
    var params = new Array;
    for (var i = 0; i < agentElements.length; i++) {
        if ($(agentElements[i]).checked) {
            params.push('search_sources[' + $(agentElements[i]).name + ']=true');
        }
    }
    var forwardUrl = url + '?search_query=' + escape(query) + '&' + params.join('&');
//    Logger.debug(forwardUrl);

    document.location = forwardUrl;
}

function get_html_translation_table(table, quote_style) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js. Meaning the constants are not
    // %          note: real constants, but strings instead. integers are also supported if someone
    // %          note: chooses to create the constants themselves.
    // %          note: Table from http://www.the-art-of-web.com/html/character-codes/
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
    
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    useTable      = (table ? table.toUpperCase() : 'HTML_SPECIALCHARS');
    useQuoteStyle = (quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT');
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
    
    // Map numbers to strings for compatibilty with PHP constants
    if (!isNaN(useTable)) {
        useTable = constMappingTable[useTable];
    }
    if (!isNaN(useQuoteStyle)) {
        useQuoteStyle = constMappingQuoteStyle[useQuoteStyle];
    }
    
    if (useQuoteStyle != 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
 
    if (useQuoteStyle == 'ENT_QUOTES') {
        entities['39'] = '&#039;';
    }
 
    if (useTable == 'HTML_SPECIALCHARS') {
        // ascii decimals for better compatibility
        entities['38'] = '&amp;';
        entities['60'] = '&lt;';
        entities['62'] = '&gt;';
    } else if (useTable == 'HTML_ENTITIES') {
        // ascii decimals for better compatibility
      entities['38']  = '&amp;';
      entities['60']  = '&lt;';
      entities['62']  = '&gt;';
      entities['160'] = '&nbsp;';
      entities['161'] = '&iexcl;';
      entities['162'] = '&cent;';
      entities['163'] = '&pound;';
      entities['164'] = '&curren;';
      entities['165'] = '&yen;';
      entities['166'] = '&brvbar;';
      entities['167'] = '&sect;';
      entities['168'] = '&uml;';
      entities['169'] = '&copy;';
      entities['170'] = '&ordf;';
      entities['171'] = '&laquo;';
      entities['172'] = '&not;';
      entities['173'] = '&shy;';
      entities['174'] = '&reg;';
      entities['175'] = '&macr;';
      entities['176'] = '&deg;';
      entities['177'] = '&plusmn;';
      entities['178'] = '&sup2;';
      entities['179'] = '&sup3;';
      entities['180'] = '&acute;';
      entities['181'] = '&micro;';
      entities['182'] = '&para;';
      entities['183'] = '&middot;';
      entities['184'] = '&cedil;';
      entities['185'] = '&sup1;';
      entities['186'] = '&ordm;';
      entities['187'] = '&raquo;';
      entities['188'] = '&frac14;';
      entities['189'] = '&frac12;';
      entities['190'] = '&frac34;';
      entities['191'] = '&iquest;';
      entities['192'] = '&Agrave;';
      entities['193'] = '&Aacute;';
      entities['194'] = '&Acirc;';
      entities['195'] = '&Atilde;';
      entities['196'] = '&Auml;';
      entities['197'] = '&Aring;';
      entities['198'] = '&AElig;';
      entities['199'] = '&Ccedil;';
      entities['200'] = '&Egrave;';
      entities['201'] = '&Eacute;';
      entities['202'] = '&Ecirc;';
      entities['203'] = '&Euml;';
      entities['204'] = '&Igrave;';
      entities['205'] = '&Iacute;';
      entities['206'] = '&Icirc;';
      entities['207'] = '&Iuml;';
      entities['208'] = '&ETH;';
      entities['209'] = '&Ntilde;';
      entities['210'] = '&Ograve;';
      entities['211'] = '&Oacute;';
      entities['212'] = '&Ocirc;';
      entities['213'] = '&Otilde;';
      entities['214'] = '&Ouml;';
      entities['215'] = '&times;';
      entities['216'] = '&Oslash;';
      entities['217'] = '&Ugrave;';
      entities['218'] = '&Uacute;';
      entities['219'] = '&Ucirc;';
      entities['220'] = '&Uuml;';
      entities['221'] = '&Yacute;';
      entities['222'] = '&THORN;';
      entities['223'] = '&szlig;';
      entities['224'] = '&agrave;';
      entities['225'] = '&aacute;';
      entities['226'] = '&acirc;';
      entities['227'] = '&atilde;';
      entities['228'] = '&auml;';
      entities['229'] = '&aring;';
      entities['230'] = '&aelig;';
      entities['231'] = '&ccedil;';
      entities['232'] = '&egrave;';
      entities['233'] = '&eacute;';
      entities['234'] = '&ecirc;';
      entities['235'] = '&euml;';
      entities['236'] = '&igrave;';
      entities['237'] = '&iacute;';
      entities['238'] = '&icirc;';
      entities['239'] = '&iuml;';
      entities['240'] = '&eth;';
      entities['241'] = '&ntilde;';
      entities['242'] = '&ograve;';
      entities['243'] = '&oacute;';
      entities['244'] = '&ocirc;';
      entities['245'] = '&otilde;';
      entities['246'] = '&ouml;';
      entities['247'] = '&divide;';
      entities['248'] = '&oslash;';
      entities['249'] = '&ugrave;';
      entities['250'] = '&uacute;';
      entities['251'] = '&ucirc;';
      entities['252'] = '&uuml;';
      entities['253'] = '&yacute;';
      entities['254'] = '&thorn;';
      entities['255'] = '&yuml;';
    } else {
        throw Error("Table: "+useTable+' not supported');
        return false;
    }
    
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal)
        histogram[symbol] = entities[decimal];
    }
    
    return histogram;
}

function htmlentities (string, quote_style) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: nobbler
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: get_html_translation_table
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
    if (string == undefined) {
        return '';
    }
 
    var histogram = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (histogram = get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
    
    for (symbol in histogram) {
        entity = histogram[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}

/*
 * Security Funktionen für E-Mails
 */

function b64arrays() {
    var b64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    b64 = [];
    f64 =[];
    for (var i=0; i<b64s.length ;i++) {
        b64[i] = b64s.charAt(i);
        f64[b64s.charAt(i)] = i;
    }
}

function b64t2d(t) {
    b64arrays();
    var d=[];
    var i=0;
    // zur decodierung die Umbrueche killen
    t=t.replace(/\n|\r/g,""); t=t.replace(/=/g,"");
    while (i<t.length) {
        d[d.length] = (f64[t.charAt(i)]<<2) | (f64[t.charAt(i+1)]>>4);
        d[d.length] = (((f64[t.charAt(i+1)]&15)<<4) | (f64[t.charAt(i+2)]>>2));
        d[d.length] = (((f64[t.charAt(i+2)]&3)<<6) | (f64[t.charAt(i+3)]));
        i+=4;
    }
    
    if (t.length%4 == 2)
        d = d.slice(0, d.length-2);
        
    if (t.length%4 == 3)
        d = d.slice(0, d.length-1); 
    
    return d;
}

function decodeEmail(base64String) {
    var base64Array = b64t2d(base64String);
    document.write("&#" + base64Array.join("&#"));
}

function decodeShortedEmail(base64String, strLen) {
    var base64Array = b64t2d(base64String);
    var decodeShortedEmail = [];
    var endString = "...";
    if (strLen > base64Array.length) {
        strLen = base64Array.length;
        endString = "";
    }
    for (var i = 0; i < strLen; i++) {
        decodeShortedEmail[i] = base64Array[i];
    }
    var decodeEmail = "&#" + decodeShortedEmail.join("&#"); 
    document.write(decodeEmail + endString);    
}

function decodeEmailAsLink(base64String) {
    var base64Array = b64t2d(base64String);
    document.write("<a href=\"mailto:&#" + base64Array.join("&#") + "\">");
}
 