var w3c = (document.getElementById) ? 1:0
var ns4 = (document.layers) ? 1:0  //browser detect for NS4 & W3C standards
var hasCookies = false;
  
//tests whether the user accepts cookies, and sets a flag.
if(document.cookie == '') {
	document.cookie = 'hasCookies=yes';
	if (document.cookie.indexOf('hasCookies=yes') != -1) hasCookies = true;
}
else hasCookies = true;

// sets a cookie in the browser.
function setCookie(name, value, hours, path) {
	if (hasCookies) {
		if(hours) {
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) var numHours = hours;
			else if (typeof(hours) == 'number') var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
		}
		document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'');
	}
}
 
// reads a cookie from the browser
function readCookie(name) {
	if (document.cookie == '') return '';
	else {
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);
		if (firstChar != -1) {
			firstChar += name.length + 1;
			lastChar = theBigCookie.indexOf(';', firstChar);
			if (lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		}
		else return '';
	}
}

function toggleFoldyPersistState(divID) {
	var theCookie = readCookie(divID);
	var state='none';
	if (theCookie == 'none') {
		state='block';
	}
	setCookie(divID, state, 'Wed 01 Jan 2020 00:00:00 GMT', '/');
	return state;
}

function MM_findObj(n, d) { //v4.0
	var p,i,x;
	if (!d) d=document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if (!(x=d[n]) && d.all)
		x=d.all[n];
	for (i=0; !x && i<d.forms.length; i++)
		x=d.forms[i][n];
	for (i=0; !x && d.layers && i<d.layers.length; i++)
		x=MM_findObj(n,d.layers[i].document);
	if (!x && document.getElementById)
		x=document.getElementById(n);
	return x;
}

function showHideModuleMouseOver(divID) {
	var theCookie = readCookie(divID);
	if (theCookie == 'none') {
		window.status = 'Hide';
	}
	else {
		window.status = 'Show';
	}
}

function showHideModule(divID) {
	var state = toggleFoldyPersistState(divID);
	var ok=false;
	if(w3c) {
		var divIDobj = MM_findObj(divID);
		var toggleobj = MM_findObj(divID + 'Bullet');
		if(divIDobj != null && toggleobj != null) {
			ok = true;
			if (state == 'none') {
				toggleobj.src = '/images/administration/bulletRight.gif';
				divIDobj.style.display = 'none';
			} else {
				toggleobj.src = '/images/administration/bulletDown.gif';
				divIDobj.style.display = 'block';
			}
		}
	}

	if(!ok){
		document.location = document.location;
	}

	showHideModuleMouseOver(divID);
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments;
	document.MM_sr=new Array;
	for(i=0; i<(a.length-2); i+=3)
		if ((x=MM_findObj(a[i]))!=null){
			document.MM_sr[j++]=x;
			if(!x.oSrc) x.oSrc=x.src;
			x.src=a[i+2];
		}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr;
	for(i=0;a && i<a.length && (x=a[i]) && x.oSrc;i++)
		x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document;
	if(d.images) {
		if(!d.MM_p)
			d.MM_p=new Array();
		    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
			for(i=0; i<a.length; i++)
				if (a[i].indexOf("#")!=0) {
					d.MM_p[j]=new Image;
					d.MM_p[j++].src=a[i];
				}
	}
}

function MM_getButtonWithName(form, buttonName) {
	if (form.buttons) {
		var buttonCount = form.buttons.length;
		for (i = 0; i < buttonCount; i++) {
			var button = form.buttons[i];
			if (button.mName == buttonName) {
				return button;
			}
		}
	}
	return null;
}

function MMCommandButton(name,
	form,
	action,
	enabledImage,
	overImage,
	downImage,
	disabledImage,
	enabled,
	enableOnNoSelection,
	enableOnSingleSelection,
	enableOnMultipleSelection,
	altText,
	confirmation,
	confirmationMessage) {
		this.mName = name;						// Name of the image
		this.mForm = form;						// The form object enclosing this button (to retrieve selections)
		this.mAction = action;					// Action to perform when clicking
		this.mEnabledImage = enabledImage;		// enabled image (String)
		this.mOverImage = overImage;			// over image (String)
		//this.mDownImage = downImage;			// down image (String)
		this.mDisabledImage = disabledImage;	// disabled image (String)
		this.mEnableOnNoSelection = enableOnNoSelection;
		this.mEnableOnSingleSelection = enableOnSingleSelection;
		this.mEnableOnMultipleSelection = enableOnMultipleSelection;
		this.mAltText = altText;
		this.mConfirmation = confirmation;
		this.mConfirmationMessage = confirmationMessage;
		this.mEnabled = enabled;

		this.update = MMCommandButton_update;
		this.over = MMCommandButton_over;
		this.out = MMCommandButton_out;
		this.click = MMCommandButton_click;
}

function MMCommandButton_update(selectedItems) {

	if (selectedItems == 0) {
		if (this.mEnableOnNoSelection == true) {
			document[this.mName].src = this.mEnabledImage;
			this.mEnabled = true;
		} else {
			document[this.mName].src = this.mDisabledImage;
			this.mEnabled = false;
		}
	}

	if (selectedItems == 1) {
		if (this.mEnableOnSingleSelection == true) {
			document[this.mName].src = this.mEnabledImage;
			this.mEnabled = true;
		} else {
			document[this.mName].src = this.mDisabledImage;
			this.mEnabled = false;
		}
	}

	if (selectedItems > 1) {
		if (this.mEnableOnMultipleSelection == true) {
			document[this.mName].src = this.mEnabledImage;
			this.mEnabled = true;
		} else {
			document[this.mName].src = this.mDisabledImage;
			this.mEnabled = false;
		}
	}
}

// returns an object reference.
function getObject(obj) {
	if (w3c) {
		var theObj = document.getElementById(obj);
	} else
		if (ns4)
			var theObj = eval('document.' + obj);
	return theObj;
}

// swaps text in a layer.
function swapText(text, divID, innerDivID) {
	var innerDivID=divID + 'NN';

	if (w3c) {
		var theObj = getObject(divID);
		if (theObj) {
			theObj.innerHTML = text;
		}
	}
   else if (ns4) {
		var innerObj = divID + '.document.' + innerDivID;
		var theObj = getObject(innerObj);
		if (theObj) {
			theObj.document.open();
			theObj.document.write(text);
			theObj.document.close();
		}
	}
}

function MMCommandButton_over() {
	if (this.mEnabled) {
		document[this.mName].src = this.mOverImage;
	}

// To whom it may concern. If you are revisiting this code in order
// to speed it up, note that the thing slowing down the rollovers is
// this call to swapText.
	swapText(this.mAltText, this.mForm.actionDescription);

	window.status= this.mAltText;
}

function MMCommandButton_out() {
	if (this.mEnabled) {
		document[this.mName].src = this.mEnabledImage;
	}
	swapText('', this.mForm.actionDescription);
	window.status = '';
}

function submitAction(form, value) {
	if (value.indexOf('previewInWindow') != -1) {
		form.target = 'previewInWindow'
		windowOpen('previewInWindow', '', 700, 650, 0, 0, 0, 1, 1);
	}

	for (i=0; i<form.lenght; i++) {
		if (form.elements[i].name == 'do')
			break;
	}

	if (i != form.length) {
		old = form.elements[i].value;
		form.elements[i].value = value;
		form.submit();
		form.elements[i].value = old;
	}
}

function MMCommandButton_click() {
	if (this.mEnabled) {
	//document[this.mName].src = this.mDownImage;

		if (this.mConfirmation) {
			if (!confirm(this.mConfirmationMessage)) {
				return;
			}
		}
		submitAction(this.mForm, this.mAction);
	}
	swapText('', this.mForm.actionDescription);
	window.status = '';
}

function MM_toggleItem(form, itemName) {
	$count = 0;
	for ($i=0; $i<form.length; $i++) {
		if (form.elements[$i].name == itemName &&
			form.elements[$i].checked == true &&
			(form.elements[$i].type.toLowerCase() == 'checkbox' || form.elements[$i].type.toLowerCase() == 'radio'))
		$count++;
	}
	MM_updateButtonsState(form, $count);
}

function MM_updateButtonsState(form, selectedItems) {
	if (form.buttons) {
		var buttonCount = form.buttons.length;

		for (i = 0; i < buttonCount; i++) {
			var button = form.buttons[i];
			if (button) {
				button.update(selectedItems);
			}
		}
	}
}

//common functions
function selectAll(form, itemName, mark) {
	for (i = 0; i < form.elements.length; i++) {
    	if (form.elements[i].name == itemName) {
			form.elements[i].checked = mark;
		};
	}
}

function installSelectElement(element, value) {
	for(i=0; i<element.length; i++) {
		if (element.options[i].value == value)
			element.options[i].selected = true;
	}
}

function sendOrderForm(pathForm, objectName, orderPosition, defaultOrderDirection) {
	cookieOrderPosition		= readCookie(objectName + '[orderPosition]');
	cookieOrderDirection	= readCookie(objectName + '[orderDirection]');

	if (cookieOrderPosition == '') {
		cookieOrderDirection = defaultOrderDirection;
	} else if (cookieOrderPosition == orderPosition) {
		if (cookieOrderDirection == 'desc') {
			cookieOrderDirection = 'asc';
		} else {
			cookieOrderDirection = 'desc';
		}
	}

	var path = '' + document.location;
	path = path.substr(path.indexOf('/', 10), path.indexOf('index.php') - path.indexOf('/', 10));

	setCookie(objectName + '[orderPosition]', orderPosition, 'Wed 01 Jan 2020 00:00:00 GMT', path);
	setCookie(objectName + '[orderDirection]', cookieOrderDirection, 'Wed 01 Jan 2020 00:00:00 GMT', path);

	pathForm.submit();
}

function windowOpen(name, url, width, height, toolbar, menubar, statusbar, scrollbar, resizable) {
	var wint = (screen.height - height) / 2;
	var winl = (screen.width - width) / 2;
	toolbar_str = toolbar ? 'yes' : 'no';
	menubar_str = menubar ? 'yes' : 'no';
	statusbar_str = statusbar ? 'yes' : 'no';
	scrollbar_str = scrollbar ? 'yes' : 'no';
	resizable_str = resizable ? 'yes' : 'no';
	window.open(url, name, 'left='+winl+',top='+wint+',width='+width+',height='+height+',toolbar='+toolbar_str+',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
}

function showGroupItems(action, objects, value, mForm) {

	setCookie(action + objects + 'Group', value, 'Wed 01 Jan 2020 00:00:00 GMT','/');

	for (i=0; i<mForm.elements.length; i++) {
		if (mForm.elements[i].name == 'offset' + objects + 'Block') {
			mForm.elements[i].value = '';
			break;
		}
	}
	mForm.submit();
}

function changeLocation(path, i) {
	path.position.value = i;
	path.submit();
}

function changeAction(myForm, action) {
	for(i=0; i < myForm.length; i++) {
		if (myForm.elements[i].name == 'do') {
			myForm.elements[i].value = action;
		}
	}
	message = 'Are you sure you want to delete it?';
	if (action.indexOf('delete') == -1 || (action.indexOf('delete') != -1 && confirm(message))) {
		myForm.submit();
	}
}

function showHideBlock(block, display) {
	obj = document.getElementById(block);
	obj.style.display = display;
}
function showHideBlockByElement(element, block) {
	obj = document.getElementById(block);
	obj.style.display = (element.checked) ? 'block' : 'none';
}

//just in maklerseite
function setPeriodicalField() {
	var typesId = document.getElementById('typesId');

	selectedIndex	= typesId.selectedIndex;

	if (selectedIndex >= 0) {
		var periodical		= document.getElementById('periodical');
		var periodicalField	= document.getElementById('periodicalField');

		switch (typesId.options[selectedIndex].value) {
			case '1'://calculator
				periodical.checked		= false;
				periodical.style.display	= 'none';
				periodicalField.style.color	= '#FFFFFF';
				break;
			case '2'://newsletter
				periodical.checked		= true;
				periodical.style.display	= 'none';
				periodicalField.style.color	= '#FFFFFF';
				break;
			case '3'://other
				periodical.style.display	= 'block';
				periodicalField.style.color	= '#000000';
				break;
		}
	} 
}
function checkCreateDefaultStructure(myForm, message) {
	if ((myForm.createStructure.checked && confirm(message)) || !myForm.createStructure.checked) {
		myForm.submit();
	}
}

function changeTabWizard(form, redirect) {
	form.redirect.value = redirect;
	form.submit();
}