var globals = {};
var strings = {};
globals.mailAddrRegexp = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/; //match(globals.mailAddrRegexp)
globals.isFirefox = ( navigator.userAgent.indexOf("Firefox") != -1 );
globals.isIE = ( navigator.userAgent.indexOf("MSIE") != -1 );
globals.isIE6 = ( navigator.appVersion.match(/MSIE 6/) );
globals.isIE7 = ( navigator.appVersion.match(/MSIE 7/) );
globals.debug = false;

function getIEVersion(){
	if (navigator.appVersion.match(/MSIE 1/)) return 1;
	if (navigator.appVersion.match(/MSIE 2/)) return 2;
	if (navigator.appVersion.match(/MSIE 3/)) return 3;
	if (navigator.appVersion.match(/MSIE 4/)) return 4;
	if (navigator.appVersion.match(/MSIE 5/)) return 5;
	if (navigator.appVersion.match(/MSIE 6/)) return 6;
	if (navigator.appVersion.match(/MSIE 7/)) return 7;
	if (navigator.appVersion.match(/MSIE 8/)) return 8;
	if (navigator.appVersion.match(/MSIE 9/)) return 9;
	return 0;
}

/* Tools */

function utf8_encode(string) {
		//http://www.webtoolkit.info/javascript-url-decode-encode.html
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
    return escape(utftext);
}

Object.extend(Array.prototype, {
	indexedIndexOf: function(object, index) {
		for (var i = 0, length = this.length; i < length; i++)
			if (this[i][index] == object) return i;
		return -1;
	}
});

Object.extend(String.prototype, {
	trim: function() {
		return this.replace (/^\s+/, '').replace (/\s+$/, '');
	}
});

function isoDateParse(s) {
	if(s == "" || s == null) return "";
	return s.substr(8, 2)+"."+s.substr(5, 2)+"."+s.substr(0, 4);
}

function isoDateConvertTo(d) {
	if(d == '') return null;
	var da = d.split('.');
	return da[2]+'-'+da[1]+'-'+da[0]+'T00:00:00.0000000+01:00';
}

/* returns the value of the selected radio button in the radio group */

function $RF(el, radioGroup) {
	if($(el).type == 'radio') {
		var el = $(el).form;
		var radioGroup = $(el).name;
	} else if ($(el).tagName.toLowerCase() != 'form') {
		return false;
	}
	return $F($(el).getInputs('radio', radioGroup).find(
		function(re) {return re.checked;}
	));
}

/* cancels an event from propagation */

function cancelEvent(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

/* document info */

function getDocumentWidth() {
	return Math.max(document.body.scrollWidth, getWindowWidth());
}

function getDocumentHeight() {
	return Math.max(document.body.scrollHeight, getWindowHeight());
}

function getWindowWidth() {
	return (self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0);
}

function getWindowHeight() {
	return (self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0);
}

/* marrying prototype and jayrock */

function prototypeChannel(call) {
	return new Ajax.Request(
		call.url, {
			method: "post",
			postBody: Object.toJSON(call.request),
			contentType: "application/json",
			onSuccess: function (xhr) {
				var response = xhr.responseText.evalJSON();
				if (response.error != null)
					ui.liste.innerHTML = '<div style="padding-top:8px;padding-bottom:12px;height:16px;"><div class="errorfett">'+response.error.message+'</div></div>';
				else if(call.callback != null)
					call.callback(response.result);
		}
	});
}

/* hilite searchword in string */

function hilite(string, search) {
	var regexp = new RegExp('(' + search + ')', 'gi');
	return string.toString().replace(regexp, '<span class="hilite">$1</span>');
}

/* keyboard */

function isNavigationalKey(evt) {
	var key = evt.which || evt.keyCode;
	if(globals.isFirefox) {
		if(evt.keyCode == 0 && key == 37) key = 255; //%
		if(evt.keyCode == 0 && key == 38) key = 255; //&
		if(evt.keyCode == 0 && key == 39) key = 255; //arr
		if(evt.keyCode == 0 && key == 40) key = 255; //(
		if(evt.keyCode == 46 && key == 46) key = 255; //del
	} else {
		if(evt.keyCode == 37 && key == 37) key = 255; //%
		if(evt.keyCode == 38 && key == 38) key = 255; //&
		if(evt.keyCode == 40 && key == 40) key = 255; //(
	}
	if(key != 8 && key != 9 && key != 27 && key != 37 && key != 38 && key != 39 && key != 40 && key != 46) {
		return key;
	}
	return 0;
}

function isNumeric(evt) {
	var key = isNavigationalKey(evt);
	if(key > 0) {
		if(key < 48 || key > 57 || key == 32) {
			if(!(globals.isIE && key == 59)) {
				Event.stop(evt);
			}
		} else if(!globals.isFirefox && window.event.shiftKey && key != 49) {
			Event.stop(evt);
		}
	//} else {
	//	Event.stop(evt);
	}
}

function isDateChars(evt) {
	var key = isNavigationalKey(evt);
	if(key > 0) {
		if(key < 48 || key > 57 || key == 32) {
			if(!(globals.isIE && key == 59)) {
				Event.stop(evt);
			}
		} else if(!globals.isFirefox && window.event.shiftKey && key != 49) {
			Event.stop(evt);
		}
	}
}
	
/* sorting */

function sortByCompare(a, b) {
	if(a[sort.key1] == b[sort.key1]){
		return 0;
	}
	return (a[sort.key1] < b[sort.key1]) ? -1 : 1;
}

function sortByCompare2(a, b) {
	if(a[sort.key1] == b[sort.key1]){
		if(a[sort.key2] == b[sort.key2]){
			return 0;
		}
		return (a[sort.key2] < b[sort.key2]) ? -1 : 1;
	}
	return (a[sort.key1] < b[sort.key1]) ? -1 : 1;
}

function sortByCompare3(a, b) {
	if(a[sort.key1] == b[sort.key1]){
		if(a[sort.key2] == b[sort.key2]){
			if(a[sort.key3] == b[sort.key3]){
				return 0;
			}
			return (a[sort.key3] < b[sort.key3]) ? -1 : 1;
		}
		return (a[sort.key2] < b[sort.key2]) ? -1 : 1;
	}
	return (a[sort.key1] < b[sort.key1]) ? -1 : 1;
}

/* formatters */

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'\''+ num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function hideSubmitButton() {
    $('submit').hide();
    $('processing').show();
}
