function popup(url, windowname, width, height, menue, scroller, resizable) {
	var sizeParams = '';
	if(width) {
		sizeParams += 'width=' + width + ',';
	}
	if(height) {
		sizeParams += 'height=' + height + ',';
	}

	newWindow = window.open(url, windowname, sizeParams + 'toolbar=0,location=0,directories=0,status=0,menubar=' + menue + ',scrollbars=' + scroller + ',resizable=' + resizable);
	newWindow.focus();
}



function show(elementId) {
	var element = document.getElementById(elementId);
	if(element) {
		element.style.display = 'block';
	}
}

function hide(elementId) {
	var element = document.getElementById(elementId);
	if(element) {
		element.style.display = 'none';
	}
}

function growProgressBar(divID, procentGrow) {
	var bar = $(divID);
	if(bar) {
		bar.style.width = procentGrow + '%';
	}
}

function showProgressBar(divID, timeout, callback) {
	var seconds = timeout / 1000;
	var procentGrow = 100 / seconds;

	
	for(var i=1; i <= seconds; i++) {
		window.setTimeout('growProgressBar("' + divID  + '", ' + (i * procentGrow) + ')', i * 1000);
	}

	window.setTimeout(callback, timeout);
}

function showRequestAgainButton() {
	var button = $('requestAgainButton');
	if(button) {
		new Effect.Appear(button);
	}
}


function replaceContent(fromContentID, toContentId) {
	var fromContent = document.getElementById(fromContentID);
	var toContent = document.getElementById(toContentId);
	if(fromContent && toContent) {
		toContent.innerHTML = fromContent.innerHTML;
		fromContent.innerHTML = '';
		fromContent.style.display = 'none';
	}
}


var on_page_load = '';
function add_on_page_load(code) {
	on_page_load += code + "\n";
}

function init() {
	if (on_page_load) {
		eval(on_page_load);
	}
}


function closeNewUserInfo() {
	var info = $('new-at-here');
	if(info) {
		new Effect.DropOut(info);
		var c = new CookieHandler();
		c.setCookie('newUserMessageShown', 1, 311040000); // For 10 years...
	}
}

function showNewUserInfo() {
	var c = new CookieHandler();
	var messageWasShown = c.getCookie('newUserMessageShown');
	if(! messageWasShown) {
		var message = $('new-at-here');
		message.style.display = 'block';
	}
}

function CookieHandler() {
	this.setCookie = function (name, value, seconds) {

		if (typeof(seconds) != 'undefined') {
			var date = new Date();
			date.setTime(date.getTime() + (seconds*1000));
			var expires = "; expires=" + date.toGMTString();
		} else {
			var expires = "";
		}

		document.cookie = name+"="+value+expires+"; path=/";
	}

	this.getCookie = function (name) {

		name = name + "=";
		var carray = document.cookie.split(';');

		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) {
				return c.substring(name.length,c.length);
			}
		}
		return null;
	}

	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}
}


function arrayShuffle(){
	var tmp, rand;
	for(var i =0; i < this.length; i++){
		rand = Math.floor(Math.random() * this.length);
		tmp = this[i]; 
		this[i] = this[rand]; 
		this[rand] =tmp;
	}
}

Array.prototype.shuffle = arrayShuffle;


var formIsSending = false;
function sendSMSSubscriptionForm(id, position) {
	if(!formIsSending) {
		$('sms-checkbox-wrap-' + id + '-' + position).style.display = 'none';
		$('sms-checkbox-loader-' + id + '-' + position).style.display = 'block';
		$('sms-nofication-form-' + id + '-' + position).submit();
		formIsSending = true;
	}
	
	return false;
}

function sendEMailSubscriptionForm(id, position) {
	if(!formIsSending) {
		$('email-checkbox-wrap-' + id + '-' + position).style.display = 'none';
		$('email-checkbox-loader-' + id + '-' + position).style.display = 'block';
		$('email-nofication-form-' + id + '-' + position).submit();
		formIsSending = true;
	}
	
	return false;
}

function showMerker() {
	if($('merker')) {
		new Effect.Appear($('merker'));
	}
}

function showNotValidatedMessage() {
	if($('not-validated')) {
		new Effect.Shake($('not-validated'));
		window.setTimeout('showNotValidatedMessage()', 10000);
	}
}

function runde(x, n) {
  if (n < 1 || n > 14) return false;
  var e = Math.pow(10, n);
  var k = (Math.round(x * e) / e).toString();
  if (k.indexOf('.') == -1) k += '.';
  k += e.toString().substring(1);
  return k.substring(0, k.indexOf('.') + n+1);
}

function sendAbonnementListForm(el) {
	dojo.xhrPost({
		url: "/investorsms/edit_user_contentobject",
		form: el.id,
		handleAs: "json",
		handle: function(data, args){
			if(typeof data == "error"){
				alert(data);
			} else {
				handleUserSubscriptionData(data);				
			}
		}
	});
}


function handleUserSubscriptionData(data) {
	if(data.action == 'exists') {
		// nothing
	} else if(data.action == 'added') {
		addUserSubscription(data);
	} else if(data.action == 'removed') {
		removeUserSubscription(data);
	} else if(data.action == 'limited') {
		showSubscriptionLimit(data);
	}
}

function sendAbonnementForm(e, preventDefault, medium, objectid, position) {
	if(preventDefault) {
		e.preventDefault();
	}
	dojo.xhrPost({
		url: "/investorsms/edit_user_contentobject",
		form: medium + '-nofication-form-' + objectid + '-' + position,
		handleAs: "json",
		handle: function(data, args){
			if(typeof data == "error"){
				alert(data);
			} else {
				handleUserSubscriptionData(data);
			}
		}
	});
}

function showSubscriptionLimit(data) {
	var elID = 'limited-' + data.contentobject_id + '-' + data.position + '-' + data.medium;
	if(dojo.byId(elID)) {
		dojo.byId(elID).style.display = 'block';
		var checkboxID = data.medium + '-notificator-' + data.contentobject_id + '-content';
		if(dojo.byId(checkboxID)) {
			var checkbox = dojo.byId(checkboxID);
			checkbox.checked = false;
		}
		
		var anim = dojo.animateProperty({
		    node: 'notificator-' + data.contentobject_id + '-' + data.position + '-' + data.medium,
		    delay: 500,
		    properties: {
				backgroundColor: { end: '#CC0000' },
				borderColor: {end: '#CC0000'},
				color: {end: '#FFFFFF'},
		    }
		}); 
		anim.play();
	}
}

function addUserSubscription(data) {
	// Handle subscriptionlist..
	highlightElement('company-row-' + data.contentobject_id);
	
	checkMaxSmsSubscription(data);
	
	// Handle subscriptionbox..
	var elID = 'subscription_' + data.type + '_' + data.contentobject_id;
	if(! dojo.byId(elID)) {
		var ULForMySubscribedObjects = document.createElement('ul');
		ULForMySubscribedObjects.className = 'medium-abonnements';
		var li = document.createElement('li');
		li.className = data.medium;
		li.id = 'subscription_' + data.type + '_' + data.contentobject_id;
		
		var a = document.createElement('a');
		a.href = data.url;
		
		var aText = document.createTextNode(data.name);
		a.appendChild(aText);
		li.appendChild(a);
		ULForMySubscribedObjects.appendChild(li);
		
		dojo.byId('my-subscribed-' + data.type).appendChild(ULForMySubscribedObjects);
		
		countUserSubscriptions(data.type);
	} else {
		var el = dojo.byId(elID);
		el.className = 'email_sms';		
	}	
	highlightElement('notificator-' + data.contentobject_id + '-' + data.position + '-' + data.medium);
	
	// Handle label
	var labelID = data.medium + '-notificator-' + data.contentobject_id + '-company-label';
	if(dojo.byId(labelID)) {
		highlightLabel(labelID);
	}
}

function checkMaxSmsSubscription(data) {
	if(data.user_company_sms_subscription >= data.max_company_sms_subscription) {
		disableUserSubscriptionCheckboxes();
	} else {
		enableUserSubscriptionCheckboxes();
	}
}

function disableUserSubscriptionCheckboxes() {
	var smsCheckbox = dojo.query('.subscription-sms');
	for(var i=0; i < smsCheckbox.length; i++) {
		var checkbox = smsCheckbox[i];
		if(! checkbox.checked) {
			checkbox.disabled = true;
		}
	}
}

function enableUserSubscriptionCheckboxes() {
	var smsCheckbox = dojo.query('.subscription-sms');
	for(var i=0; i < smsCheckbox.length; i++) {
		var checkbox = smsCheckbox[i];
		checkbox.disabled = false;
	}
}

function removeUserSubscription(data) {
	// Handle subscriptionlist..
	var row = 'company-row-' + data.contentobject_id;
	if(dojo.byId(row)) {
		var smsCheckbox = dojo.byId('user-subscription-checkbox-sms-' + data.contentobject_id);
		var emailCheckbox = dojo.byId('user-subscription-checkbox-email-' + data.contentobject_id);
		if(! smsCheckbox.checked && ! emailCheckbox.checked) {
			var anim = dojo.animateProperty({
			    node: dojo.byId(row),
			    delay: 500,
			    properties: {
					backgroundColor: { end: '#FFFFFF' },
					color: {end: '#333333'},
			    }
			}); 
			anim.play();
		}
	}
	
	checkMaxSmsSubscription(data);
	
	// Handle subscriptionbox..
	var elID = 'subscription_' + data.type + '_' + data.contentobject_id;
	if(dojo.byId(elID)) {
		var el = dojo.byId(elID);
		if(data.medium == 'sms') {
			if(el.className == 'email_sms') {
				el.className = 'email';
			} else {
				el.parentNode.removeChild(el);
			}
		} else if(data.medium == 'email') {
			if(el.className == 'email_sms') {
				el.className = 'sms';
			} else {
				el.parentNode.removeChild(el);
			}
		}
		
		countUserSubscriptions(data.type);
		
		lowlightElement('notificator-' + data.contentobject_id + '-' + data.position + '-' + data.medium);
	}
	
	// Handle label
	var labelID = data.medium + '-notificator-' + data.contentobject_id + '-company-label';
	if(dojo.byId(labelID)) {
		lowlightLabel(labelID);
	}
}

function highlightLabel(elId) {
	var anim = dojo.animateProperty({
	    node: elId,
	    delay: 200,
	    properties: {
			color: {end: '#669933'},
	    }
	}); 
	anim.play();
}

function lowlightLabel(elId) {
	var anim = dojo.animateProperty({
	    node: elId,
	    delay: 200,
	    properties: {
			color: {end: '#999999'},
	    }
	}); 
	anim.play();
}

function highlightElement(elId) {
	var anim = dojo.animateProperty({
	    node: elId,
	    delay: 500,
	    properties: {
			backgroundColor: { end: '#CCFFCC' },
			borderColor: {end: '#669933'},
			color: {end: '#336600'},
	    }
	}); 
	anim.play();
}

function lowlightElement(elId) {
	var anim = dojo.animateProperty({
	    node: elId,
	    delay: 500,
	    properties: {
			backgroundColor: { end: '#FFFFFF' },
			borderColor: {end: '#FFFFFF'},
			color: {end: '#333333'},
	    }
	}); 
	anim.play();
}

function userSubscriptionSort(a, b) {
  return a.contentobject_id - b.contentobject_id;
}


function createUserSubscritionList(userSubscribedObjects, type) {
	var userSubscribedObjectsMerker = '';
	userSubscribedObjects.sort(userSubscriptionSort);
	
	var uniqueuserSubscribedObjects = new Array();
	for(var i=0; i<userSubscribedObjects.length; i++) {	
		var userSubscribedObjectsElement = userSubscribedObjects[i];
		userSubscribedObjectsElement.all_media = '';
		
		// Highlight notificator boxes...
		var notificatorID = 'notificator-' + userSubscribedObjectsElement.contentobject_id + '-content-' + userSubscribedObjectsElement.medium;
		if(dojo.byId(notificatorID)) {
			highlightElement(notificatorID);
			var checkboxID = userSubscribedObjectsElement.medium + '-notificator-' + userSubscribedObjectsElement.contentobject_id + '-content';
			if(dojo.byId(checkboxID)) {
				var checkbox = dojo.byId(checkboxID);
				checkbox.checked = true;
			}
		}
		
		// Highlight subscription list...
		var listRowID = 'company-row-' + userSubscribedObjectsElement.contentobject_id;
		if(dojo.byId(listRowID)) {
			highlightElement(listRowID);
			var checkboxID = 'user-subscription-checkbox-' + userSubscribedObjectsElement.medium + '-' + userSubscribedObjectsElement.contentobject_id;
			if(dojo.byId(checkboxID)) {
				var checkbox = dojo.byId(checkboxID);
				checkbox.checked = true;
			}
		}
		
		// Highlight company box...
		var checkboxID = userSubscribedObjectsElement.medium + '-notificator-' + userSubscribedObjectsElement.contentobject_id + '-company';
		if(dojo.byId(checkboxID)) {
			var checkbox = dojo.byId(checkboxID);
			checkbox.checked = true;
			
			// Highlight label
			var labelID = userSubscribedObjectsElement.medium + '-notificator-' + userSubscribedObjectsElement.contentobject_id + '-company-label';
			if(dojo.byId(labelID)) {
				highlightLabel(labelID);
			}
		}
		
		
		if(userSubscribedObjectsElement.contentobject_id != userSubscribedObjectsMerker) {
			uniqueuserSubscribedObjects.push(userSubscribedObjectsElement);
			userSubscribedObjectsMerker = userSubscribedObjectsElement.contentobject_id;
		} else {
			uniqueuserSubscribedObjects[uniqueuserSubscribedObjects.length - 1].medium  = userSubscribedObjectsElement.medium + '_' + uniqueuserSubscribedObjects[uniqueuserSubscribedObjects.length - 1].medium;
		}
	}
	
	if(uniqueuserSubscribedObjects.length > 0) {
		var ULForMySubscribedObjects = document.createElement('ul');
		ULForMySubscribedObjects.className = 'medium-abonnements';
		for(var i=0; i < uniqueuserSubscribedObjects.length; i++) {
			var letterData = uniqueuserSubscribedObjects[i];
			var li = document.createElement('li');
			li.className = letterData.medium;
			li.id = 'subscription_' + type + '_' + letterData.contentobject_id;
			var a = document.createElement('a');
			a.href = letterData.url;
			
			var aText = document.createTextNode(letterData.name);
			a.appendChild(aText);
			li.appendChild(a);
			ULForMySubscribedObjects.appendChild(li);
		}
		dojo.byId('my-subscribed-' + type).appendChild(ULForMySubscribedObjects);
		
		countUserSubscriptions(type);
	}
}

function countUserSubscriptions(type) {
	var countMyObjects = dojo.byId('my-subscribed-' + type + '-wrap').getElementsByTagName('li').length;
	dojo.byId('counter_' + type).innerHTML = countMyObjects;
}

function setContentBlocksInArticle() {
	var mainPs = dojo.query('#intelliTXT p ');
	if(mainPs.length > 5) {
		var setAdAfter = Math.ceil(mainPs.length / 3);
		var paragraph = mainPs[setAdAfter];
		var newDiv = document.createElement('div');
		newDiv.className = 'ad-main-content';
		var tempDiv = document.getElementById('ad-main-content');
		newDiv.innerHTML = tempDiv.innerHTML;
		tempDiv.innerHTML = '';
		dojo.byId('intelliTXT').insertBefore(newDiv, paragraph);
	}
}

function showStocks(params) {
	dojo.xhrGet({
	    url: '/httpcontent/get_base64_encoded/' + params.url,
	    handleAs: "json",
	    load: function(data,args){
	    	if(data.length > 0) {
				for(var i=0; i < data.length; i++) {	
			    	var el = data[i];
			    	
			    	var name = dojo.byId(params.name + el.isin);
			    	if(name) {
			    		name.innerHTML = el.name;
			    	}
			    	
			    	var price = dojo.byId(params.price + el.isin);
			    	if(price) {
			    		price.innerHTML = el.price.replace(/\./,',') + "&nbsp;&euro;";
			    		
			    		if(dojo.byId(params.pricewrap + el.isin)) {
			    			if(el.performance > price.getAttribute('oldperformance')) {
				    			var anim1 = dojo.animateProperty({
									node: dojo.byId(params.pricewrap + el.isin),
									duration: 200,
									properties: {
										backgroundColor: { start:"#ffffff", end: "#99CC99" }
									}
								});
								var anim2 = dojo.animateProperty({
									node: dojo.byId(params.pricewrap + el.isin),
									duration: 200,
									properties: {
										backgroundColor: { start:"#99CC99", end: "#ffffff" }
									}
								});
								dojo.fx.chain([anim1,anim2]).play();
				    		} else if(el.performance < price.getAttribute('oldperformance')) {
				    			var anim1 = dojo.animateProperty({
									node: dojo.byId(params.pricewrap + el.isin),
									duration: 200,
									properties: {
										backgroundColor: { start:"#ffffff", end: "#CC9999" }
									}
								});
								var anim2 = dojo.animateProperty({
									node: dojo.byId(params.pricewrap + el.isin),
									duration: 200,
									properties: {
										backgroundColor: { start:"#CC9999", end: "#ffffff" }
									}
								});
								dojo.fx.chain([anim1,anim2]).play();
				    		}
				    	}
				    	
				    	price.setAttribute('oldperformance', el.performance);
			    	}
			    	
			    	
			    	var performance = dojo.byId(params.performance + el.isin);
			    	if(performance) {
			    		var performanceString = el.performance;
			    		if(el.performance > 0) {
			    			performance.className = 'positive-performance';
			    			performanceString = '+&nbsp;' + el.performance;
			    			
			    			price.className = 'positive-price';
			    			 
			    		} else if(el.performance < 0) {
			    			performance.className = 'negative-performance';
			    			performanceString = performanceString.replace(/-/, '-&nbsp;');
			    			
			    			price.className = 'negative-price';
			    			 
			    		} else {
			    			performance.className = 'neutral-performance';
			    			price.className = 'neutral-price';
			    		}
			    		
			    		performanceString += '%';
			    		
			    		performance.innerHTML = performanceString;
			    	}
			    	
			    	var ask = dojo.byId(params.ask + el.isin);
			    	if(ask) {
			    		ask.innerHTML = el.ask.replace(/\./,',');
			    		if(el.ask > el.old_ask) {
			    			ask.className = 'positive-price';
			    		} else if(el.ask < el.old_ask) {
			    			ask.className = 'negative-price';
			    		} else {
			    			ask.className = 'neutral-price';
			    		}
			    	}
			    	
			    	var bid = dojo.byId(params.bid + el.isin);
			    	if(bid) {
			    		bid.innerHTML = el.bid.replace(/\./,',');
			    		if(el.bid > el.old_bid) {
			    			bid.className = 'positive-price';
			    		} else if(el.bid < el.old_bid) {
			    			bid.className = 'negative-price';
			    		} else {
			    			bid.className = 'neutral-price';
			    		}
			    	}
			    	
			    	
			    	var timestamp = dojo.byId(params.time + el.isin);
			    	if(timestamp) {
			    		var timestampArray =  el.timestamp.split(' ');
			    		timestamp.innerHTML = timestampArray[1];
			    		//timestamp.innerHTML = data.timestamp;
			    	}
		    	}
		    	
		    	window.setTimeout("showStocks(params_" + params.size + ");", 2500);
		    }
	    },
	    error: function(error,args){
			//console.warn("error!",error);
	    }
	});
}

/*
*	TabsController
*/

function tabsController() {
	this.controller = false;
	this.contentDivs = new Array;
	
	this.setController = TC_setController;
	this.setContentDivs = TC_setContentDivs;
	this.setActiveTabIndex = TC_setActiveTabIndex;
	this.initialize = TC_initialize;
	this.showDiv = TC_showDiv;
	this.setSimpleController = TC_setSimpleController;
	this.disableBlur = TC_disableBlur;
	this.checkAnchors = TC_checkAnchors;
}


function TC_setController(controllElement) {
	this.controller = controllElement;
}

function TC_setContentDivs(divs) {
	this.contentDivs = TC_setContentDivs.arguments;
}

function TC_initialize() {
	/*
	 * Hide all Divs, except the first one
	 */
	 for(var i=0; i < this.contentDivs.length; i++) {
	 	var div = document.getElementById(this.contentDivs[i]);
	 	if(div && i > 0) {
	 		div.style.display = 'none';
	 	}
	 }
	 	 
	 /*
	  * Create the Tab-Controller
	  */
 	 var rootElement = document.getElementById(this.controller);
 	 if(rootElement) {
		var as = rootElement.getElementsByTagName('a');
		for(var j=0; j < as.length; j++) {
			var a = as[j];
			if(a.className != 'notab') {
				a.setAttribute('control', this.contentDivs[j]);
				a.setAttribute('number', j);
				
				var owner = this;
				
				a.onclick = function() {
					owner.showDiv(this.getAttribute('control'));
					owner.setActiveTabIndex(this.getAttribute('number'));
					
					// Anpassen des InnerFrames
					if(typeof(resize_iframe) == 'function') {
						resize_iframe();
					}
					
					return false;
				};
			}	
			this.disableBlur(a);
		}
 	 }
 	 
 	 this.checkAnchors();
}

function TC_showDiv(divId) {
	
	/*
	 * Hide all Divs first
	 */
	 for(var i=0; i < this.contentDivs.length; i++) {
	 	var div = document.getElementById(this.contentDivs[i]);
	 	if(div) {
	 		div.style.display = 'none';
	 	}
	 }
	
	var div = document.getElementById(divId);
	if(div) {
		div.style.display = 'block';
	}
}

function TC_setActiveTabIndex(index) {
	var controller = document.getElementById(this.controller);
	if(controller) {
 		var lis = controller.getElementsByTagName('li');
		for(var i=0; i < lis.length; i++) {
			var li = lis[i];
			if(i == index) {
				li.className = 'active';
			} else {
				li.className = '';
		 	}
		}
	}
}

function TC_setSimpleController(id, contentId, index) {
	var rootElement = document.getElementById(id);
	if(rootElement) {
		var as = rootElement.getElementsByTagName('a');
		for(var j=0; j < as.length; j++) {
			var a = as[j];
			var owner = this;
						
			a.onclick = function() {
				owner.showDiv(contentId);
				owner.setActiveTabIndex(index);
				return false;
			};
			
			this.disableBlur(a);

		}
	}
}

function TC_disableBlur(e) {
	e.onfocus = function() {
		this.blur();
	};
}

function TC_checkAnchors() {
	var anchor = window.location.hash;
	if(anchor) {
		anchor = anchor.substr(1);
		for(var i=0; i < this.contentDivs.length; i++) {
			var actDiv = this.contentDivs[i];
			if(actDiv == anchor) {				
				this.showDiv(actDiv);
				this.setActiveTabIndex(i);
				break;
			}
		}
	}
}