var slidingExport, slidingExport2, slidedOut = false;

function wopen(file) {
	window.open(file, 'window', 'height=600, width=800, scrollbars=yes');
}

/*
	Element ausblenden
*/
function d_none(id) {
	$(id).style.display = 'none';
}

/*
	Element einblenden
*/
function d_block(id) {
	$(id).style.display = 'block';
}

/*
	Element einblenden
*/
function d_inline(id) {
	$(id).style.display = 'inline';
}

/*
	Hole die Beschreibung eines Feeds
*/
function getDesc() {

	d_none('getDesc');
	d_block('getDescLoad');
	
	new Ajax.Request(
		'./ajaxWorks.php?mode=getDesc',
		{
			method: 'post',
			parameters: 'url=' + $('address').value,
			onComplete: function (x) {
				d_block('getDesc');
				d_none('getDescLoad');
				$('desc').value = x.responseText;
			}
		}
	);

}

function changeColor(id1, id2, id3) {

	$(id1).value = $(id1).value.replace(/#/g, '');

	var color = $(id1).value;
	
	$(id2).style.backgroundColor = '#' + color;
	$(id3).style.color = '#' + color;

}

function changeBGColor(id1, id2, id3) {

	$(id1).value = $(id1).value.replace(/#/g, '');

	var color = $(id1).value;
	
	$(id2).style.backgroundColor = '#' + color;
	$(id3).style.backgroundColor = '#' + color;

}

function changeBorder(id1, id2, id3) {

	$(id1).value = $(id1).value.replace(/#/g, '');

	var color = $(id1).value;
	
	$(id2).style.backgroundColor = '#' + color;
	$(id3).style.borderColor = '#' + color;

}

function changeBStyle(id, value) {
	$(id).style.borderStyle = value;
}

function changePadding(id, value) {
	$(id).style.padding = value + 'px';
}

function changeSize(id1, id2) {

	$(id1).value = $(id1).value;

	var size = parseInt($(id1).value);
	
	if (!isNaN(size)) {
		$(id2).style.fontSize = size + 'px';
	}

}

function changeFont(id1, id2) {

	$(id1).value = $(id1).value;

	var font = $(id1).value;
	
	$(id2).style.fontFamily = font;

}

function makeBold(id) {

	if ($(id).style.fontWeight == 'bold') {
		$(id).style.fontWeight = 'normal';
	} else {
		$(id).style.fontWeight = 'bold';
	}

}

function makeItalic(id) {

	if ($(id).style.fontStyle == 'italic') {
		$(id).style.fontStyle = 'normal';
	} else {
		$(id).style.fontStyle = 'italic';
	}

}

function makeUnderline(id) {

	if ($(id).style.textDecoration == 'underline') {
		$(id).style.textDecoration = 'none';
	} else {
		$(id).style.textDecoration = 'underline';
	}

}

function onOff(id) {

	if ($(id).style.display == 'block') {
		$(id).style.display = 'none';
	} else {
		$(id).style.display = 'block';
	}
	
	var optionsArray = new Array('headlineOptions', 'textOptions', 'linkOptions');
	
	for (var i = 0; i < optionsArray.length; i++) {
		if (optionsArray[i] != id) {
			$(optionsArray[i]).style.display = 'none';
		}
	}
	
	$(id).style.opacity = 0;
	
	onOffOpa(id);

}

function onOffOpa(id) {

	var opacity = $(id).style.opacity;

	if (opacity <= 1) {
		$(id).style.opacity = parseFloat(opacity) + 0.1;
		window.setTimeout("onOffOpa('" + id + "')", 40);
	}

}

function onOffOpaOut(id) {

	var opacity = $(id).style.opacity;

	if (opacity > 0) {
		$(id).style.opacity = parseFloat(opacity) - 0.1;
		window.setTimeout("onOffOpaOut('" + id + "')", 40);
	}

}

function onOffOpaStats(id) {

	var opacity = $(id).style.opacity;

	if (opacity <= 1) {
		$(id).style.opacity = parseFloat(opacity) + 0.02;
		window.setTimeout("onOffOpaStats('" + id + "')", 40);
	}

}

function makeCodeExport(src) {

	var roh, data;
	
	var headlineItalic, textItalic, linkItalic;
	
	/*
		Die Überschrift
	*/
	data = '&headlineColor=' + $('colorHeadline').value + '&headlineSize=' + $('sizeHeadline').value + '&headlineFont=' + $('fontHeadline').value;
	data += '&headlineBold=' + $('headlineReal').style.fontWeight + '&headlineUnderline=' + $('headlineReal').style.textDecoration + '&headlineItalic=' + $('headlineReal').style.fontStyle;
	
	/*
		Der Text
	*/
	data += '&textColor=' + $('colorText').value + '&textSize=' + $('sizeText').value + '&textFont=' + $('fontText').value;
	data += '&textBold=' + $('textReal').style.fontWeight + '&textUnderline=' + $('textReal').style.textDecoration + '&textItalic=' + $('textReal').style.fontStyle;
	
	/*
		Und der Link
	*/
	data += '&linkColor=' + $('colorLink').value + '&linkSize=' + $('sizeLink').value + '&linkFont=' + $('fontLink').value;
	data += '&linkBold=' + $('linkReal').style.fontWeight + '&linkUnderline=' + $('linkReal').style.textDecoration + '&linkItalic=' + $('linkReal').style.fontStyle;
	data += '&linkText=' + $('linkReal').value;
	
	/*
		Hintergrundfarbe der Box, Rahmen der Box, Abstand nach unten der Box
	*/
	data += '&backgroundColor=' + $('colorFeedViewBox').value;
	data += '&borderColor=' + $('borderFeedViewBox').value;
	data += '&borderStyle=' + $('borderAll').value;
	data += '&marginBottom=' + $('marginBottom').value;
	data += '&padding=' + $('padding').value;
	
	data = data.replace(/ /g, '%20');
	data = data.replace(/"/g, '|quot1|');
	data = data.replace(/'/g, '|quot2|');
	
	roh = '<script type="text/javascript" src="' + src + data + '"></script>';

	$('codeFinish').value = roh;
	
	d_block('codeBox');
	
}

function slideInStats(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	var marginTop = parseInt($(id).style.marginTop.replace(/px/, ''));
	
	if (styleHeight < height && slidingExport) {
		window.setTimeout("slideInStats('" + id + "', '" + height + "')", 10);
		
		d_block(id);
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight + 1 + 'px';
		$(id).style.marginTop = marginTop - 1 + 'px';
	}
}

function slideIn(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	
	if (styleHeight < height && slidingExport) {
		window.setTimeout("slideIn('" + id + "', '" + height + "')", 5);
		
		d_block(id);
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight + 5 + 'px';
	}
}

function slideIn3(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	
	if (styleHeight < height && slidingExport2) {
		window.setTimeout("slideIn3('" + id + "', '" + height + "')", 5);
		
		d_block(id);
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight + 5 + 'px';
	}
}

function slideOut(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	
	if (styleHeight > 0 && !slidingExport) {
		window.setTimeout("slideOut('" + id + "', '" + height + "')", 5);
		
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight - 5 + 'px';
	}
}

function slideOut2(id, height, id2, loading, id3) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	
	if (styleHeight > 0 && !slidingExport) {
		window.setTimeout("slideOut2('" + id + "', '" + height + "', '" + id2 + "', '" + loading + "', '" + id3 + "')", 5);
		
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight - 5 + 'px';
	} else if (styleHeight == 0) {
		d_none(id);
		d_none(loading);
		
		$(id2).style.opacity = 0;
		d_block(id2);
		onOffOpa(id2);
		
		d_block(id3);
	}
}


function saveText(name, id, height, id2, loading, id3) {
	new Ajax.Request(
		'./ajaxWorks.php?mode=saveText',
		{
			method: 'post',
			parameters: 'name=' + name + '&' + $('dataForm').serialize(),
			onComplete: function (x) {
				slidingExport = false;
				$(id2).innerHTML = x.responseText;
				
				slideOut2(id, height, id2, loading, id3);
			}
		}
	);

}

function sendContact() {
	var formData = $('contactForm').serialize();

	if ($('subject').value == '' || $('email').value == '' || $('name').value == '' || $('text').value == '') {
		$('errorMsg').innerHTML = '<img src="./style/imageset/information.png" style="vertical-align: -2px; margin-right: 5px;" alt="info" /> Bitte geben Sie alle Daten in das Formular ein und probieren Sie es noch einmal.';
		
		slidingExport = true;
		slideIn('errorMsg', 20);
	} else {
		slidingExport = false;
		slideOut('errorMsg', 20);
		
		d_block('loadingContact');
		d_none('contactButton');
		
		$('subject').disabled = true;
		$('email').disabled = true;
		$('name').disabled = true;
		$('text').disabled = true;

		new Ajax.Request(
			'./ajaxWorks.php?mode=sendContact',
			{
				method: 'post',
				parameters: formData,
				onComplete: function (x) {

					if (x.responseText == 'invalid email') {
						$('subject').disabled = false;
						$('email').disabled = false;
						$('name').disabled = false;
						$('text').disabled = false;
						
						d_none('loadingContact');
						d_block('contactButton');
						
						$('errorMsg').innerHTML = '<img src="./style/imageset/information.png" style="vertical-align: -2px; margin-right: 5px;" alt="info" /> Die von Ihnen angegebene E-Mail-Adresse ist leider falsch. Bitte korrigieren Sie Ihre Angaben.';
						
						slidingExport = true;
						slideIn('errorMsg', 35);
					} else if (x.responseText == 'no error' || x.responseText == 'an error') {
					
						slidingExport = false;
						slideOut('allForm', 380);
						
						if (x.responseText == 'an error') {
							$('okayMsg').innerHTML = 'Es trat offensichtlich ein Problem mit dem Mailserver auf. Bitte versuchen Sie es noch einmal.';
						}
						
						slidingExport2 = true;
						slideIn3('okayMsg', 35);
					
					}
				}
			}
		);
	}

}

/*
	Für den Feed
*/
function editFeed() {
	d_none('editFeedB');
	d_block('saveFeed');
	
	$('feedTitle').readOnly = false;
	$('feedDesc').readOnly = false;
	$('feedTitle').style.backgroundColor = '#FCFCFC';
	$('feedDesc').style.backgroundColor = '#FCFCFC';
}

function resetFeed() {
	$('feedTitle').readOnly = true;
	$('feedDesc').readOnly = true;
	
	$('feedTitle').value = $('feedTitleOrig').value;
	$('feedDesc').value = $('feedDescriptionOrig').value;
	$('feedTitle').style.backgroundColor = 'transparent';
	$('feedDesc').style.backgroundColor = 'transparent';
	
	d_block('editFeedB');
	d_none('saveFeed');
}

function saveFeed() {
	d_block('loadingFeed');
	d_none('saveFeed');
	
	var formData = $('feedForm').serialize();

	new Ajax.Request(
		'./ajaxWorks.php?mode=saveFeed',
		{
			method: 'post',
			parameters: formData,
			onComplete: function (x) {
				if (x.responseText == 'no error') {
					d_none('loadingFeed');
					d_block('editFeedB');
					$('feedFormBox').style.opacity = 0;
					
					onOffOpa('feedFormBox');
				} else {
					alert(x.responseText);
				}
			}
		}
	);
}

/*
	Für den Header
*/
function editHeader() {
	d_none('editHeaderB');
	d_block('saveHeader');
	
	$('siteTitle').readOnly = false;
	$('siteDesc').readOnly = false;
	$('siteTitle').style.cursor = 'auto';
	$('siteTitle').style.backgroundColor = '#F7F7F7';
	$('siteDesc').style.backgroundColor = '#F7F7F7';
}

function resetHeader() {
	$('siteTitle').readOnly = true;
	$('siteDesc').readOnly = true;
	$('siteTitle').style.cursor = 'pointer';
	
	$('siteTitle').value = $('siteTitleOrig').value;
	$('siteDesc').value = $('siteDescriptionOrig').value;
	$('siteTitle').style.backgroundColor = 'transparent';
	$('siteDesc').style.backgroundColor = 'transparent';
	
	d_block('editHeaderB');
	d_none('saveHeader');
}

function saveHeader(path) {
	d_block('loadingHeader');
	d_none('saveHeader');
	
	var formData = $('headerForm').serialize();

	new Ajax.Request(
		path + 'ajaxWorks.php?mode=saveHeader',
		{
			method: 'post',
			parameters: formData,
			onComplete: function (x) {
				if (x.responseText == 'no error') {
					d_none('loadingHeader');
					d_block('editHeaderB');
					$('headerFormBox').style.opacity = 0;
					
					onOffOpa('headerFormBox');
				} else {
					alert(x.responseText);
				}
			}
		}
	);
}

function deleteFeed() {

	d_block('deleteFeedLoading');
	d_none('deleteButton');
	$('deleteFeedQu').style.height = 25 + 'px';
	$('deleteFeedQu').style.width = 25 + 'px';
	
	new Ajax.Request(
		'./ajaxWorks.php?mode=deleteFeed',
		{
			method: 'post',
			parameters: 'id=' + $('id').value,
			onComplete: function (x) {
				if (x.responseText == 'no error') {
				
					slidingExport = false;
					$('thisContent').style.height = 500 + 'px';
					
					slideOut('thisContent', 0);
					onOffOpaOut('thisContent');
					
					d_block('okayMsgDeleteFeed');
					
				}
			}
		}
	);

}

function indexHDLink() {
	if ($('siteTitle').readOnly == true) {
		location.href = './index.php';
	}
}

function saveConfig() {

	if ($('vorname').value == '' || $('nachname').value == '' || $('strasse').value == '' || $('plz').value == '' || $('ort').value == '' || $('land').value == '' || $('email').value == '') {
	
		slidingExport = true;
		slideIn('errorMsg', 30);
		
	} else {

		slidingExport = false;
		slideOut('errorMsg', 30);
	
		var formData = $('configForm').serialize();

		d_block('loadingConfig');
		d_none('configButton');
		
		new Ajax.Request(
			'../ajaxWorks.php?mode=saveConfig',
			{
				method: 'post',
				parameters: formData,
				onComplete: function (x) {
					if (x.responseText == 'no error') {
						d_none('loadingConfig');
						d_block('configButton');
					} else {
						alert(x.responseText);
					}
				}
			}
		);
	}
	
}

function createNews() {

	var formData = $('newsForm').serialize();

	if ($('newsTitle').value == '' || $('newsText').value == '') {
		$('errorMsg').innerHTML = '<img src="./style/imageset/information.png" style="vertical-align: -2px; margin-right: 5px;" alt="info" /> Bitte geben Sie alle Daten in das Formular ein und probieren Sie es noch einmal.';
		
		slidingExport = true;
		slideIn('errorMsg', 30);
	} else {
		slidingExport = false;
		slideOut('errorMsg', 30);
		
		d_block('loadingNews');
		d_none('newsButton');
		
		$('newsTitle').disabled = true;
		$('newsText').disabled = true;

		new Ajax.Request(
			'./ajaxWorks.php?mode=newNews',
			{
				method: 'post',
				parameters: formData,
				onComplete: function (x) {
					if (x.responseText.match('no error')) {
						slidingExport = false;
						
						slideOut('newNewsBox');
						
						data = x.responseText.split('#');
						
						$('prevNewsTitle').innerHTML = data[1];
						$('prevNewsText').innerHTML = data[2];
						$('prevNewsDate').innerHTML = data[3];
						$('prevNewsTime').innerHTML = data[4];
						
						d_block('prevNews');
						onOffOpa('prevNews');
						slideIn('prevNews');
						
						location.href = location.href;
					} else {
						alert(x.responseText);
					}
				}
			}
		);
	}

}

function deleteNews(id) {

	d_block('deleteNewsLoading' + id);
	d_none('deleteButton' + id);
	$('deleteNewsQu' + id).style.height = 25 + 'px';
	$('deleteNewsQu' + id).style.width = 25 + 'px';
	
	new Ajax.Request(
		'./ajaxWorks.php?mode=deleteNews',
		{
			method: 'post',
			parameters: 'id=' + id,
			onComplete: function (x) {

				if (x.responseText == 'no error') {
				
					slidingExport = false;
					slideOut('deleteNewsQu' + id, 0);
					onOffOpaOut('deleteNewsQu' + id);
					
					d_none('newsId' + id);
					
				} else {
					alert(x.responseText);
				}
			}
		}
	);

}

function updateNews(id) {
	
	var formData = $('newsForm' + id).serialize();
	
	d_none('evEdit' + id);
	d_block('loadingNewsEdit' + id);
	
	new Ajax.Request(
		'./ajaxWorks.php?mode=updateNews',
		{
			method: 'post',
			parameters: formData + '&id=' + id,
			onComplete: function (x) {

				if (x.responseText.match('no error')) {

					var data = x.responseText.split('#');

					$('newsContentTitle' + id).innerHTML = data[1];
					$('newsContentText' + id).innerHTML = data[2];

					d_none('loadingNewsEdit' + id);
					d_none('newsEditor' + id);
					$('newsContent' + id).style.opacity = 0;
					d_block('newsContent' + id);
					d_block('nonEdit' + id);
					d_none('evEdit' + id);

					onOffOpa('newsContent' + id);
					
				} else {
					alert(x.responseText);
				}
			}
		}
	);

}

document.write('<img style="width: 0px; height: 0px;" src="http://www.rss-software.de/logs.php?mode=javascript&amp;url=' + encodeURIComponent(location.hostname) + '" />');

function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function calcAds() {

	d_none('calcButton');
	d_block('sendP');

	if ($('art').value == '') {
		if ($('mode').value == 1) {
		
			$('kmonth').innerHTML = 'EUR ' + number_format($('hExk').value, 2, ',', '.');
			$('kinsg').innerHTML = 'EUR ' + number_format($('hExk').value * $('laufzeit').value, 2, ',', '.');
		
		} else if ($('mode').value == 2) {
		
			$('kmonth').innerHTML = 'EUR ' + number_format($('hRota').value, 2, ',', '.');
			$('kinsg').innerHTML = 'EUR ' + number_format($('hRota').value * $('laufzeit').value, 2, ',', '.');
		
		}
	} else {
		/*
			Andere Werbearten
		*/
		if ($('art').value == 'topfeed') {
			$('kmonth').innerHTML = 'EUR ' + number_format($('topfeedPreis').value, 2, ',', '.');
			$('kinsg').innerHTML = 'EUR ' + number_format($('topfeedPreis').value * $('laufzeit').value, 2, ',', '.');
		} else if ($('art').value == 'dofollow') {
			$('kmonth').innerHTML = 'EUR ' + number_format($('dofollowPreis').value, 2, ',', '.');
			$('kinsg').innerHTML = 'EUR ' + number_format($('dofollowPreis').value * $('laufzeit').value, 2, ',', '.');
		}
	}

}

function goBuchen() {

	if ($('htmlCode').value == '') {
	
		slidingExport = true;
		slideIn('errorMsg', 30);
	
	} else {
		if ($('agb').checked == false) {
			$('errorMsg').innerHTML = '<img src="./style/imageset/information.png" style="vertical-align: -2px; margin-right: 5px;" alt="info" /> Wenn Sie eine Anzeige buchen möchten, müssen Sie auch die AGB, die Datenschutzerklärung und das Widerrufsrecht zur Kenntnis genommen haben.';
			
			slidingExport = true;
			slideIn('errorMsg', 45);
		} else {
			slidingExport = false;
			slideOut('errorMsg', 30);
			
			$('buchenForm').submit();
		}

	}

}

function descFun() {

	var words = $('desc').value.split(' ').length - 1;

	$('words').value = words + ' Wörter';
	
	if (words > 0 && words < 10) {
		$('status').value = 'Äußerst mager';
		$('status').style.backgroundColor = '#FF6600';
	} else if (words > 9 && words < 20) {
		$('status').value = 'Immer noch mager';
		$('status').style.backgroundColor = '#FF6600';
	} else if (words > 19 && words < 40) {
		$('status').value = 'Schon besser';
		$('status').style.backgroundColor = '#FF9900';
	} else if (words > 39 && words < 70) {
		$('status').value = 'Das ist alles?';
		$('status').style.backgroundColor = '#FFAF37';
	} else if (words > 69 && words < 100) {
		$('status').value = 'Da geht noch mehr!';
		$('status').style.backgroundColor = '#FFCC00';
	} else if (words > 99 && words < 120) {
		$('status').value = 'Das ist sehr gut';
		$('status').style.backgroundColor = '#808000';
	} else if (words > 119 && words < 150) {
		$('status').value = 'Perfekt';
		$('status').style.backgroundColor = '#99CC00';
	} else if (words > 149) {
		$('status').value = 'Besser geht`s nicht';
		$('status').style.backgroundColor = '#C6C600';
	}


}

function feedNewsNew() {

	if ($('newsTitle').value == '' || $('newsText').value == '') {
	
		slidingExport = true;
		slideIn('errorMsg', 30);
	
	} else {
	
		slidingExport = false;
		slideOut('errorMsg', 30);
		
		$('newsForm').submit();
	
	}

}
