// rootUrlPrefixPath
if (window.location.hostname == "linux2")
{
	var rootUrlPrefixPath = '/abodir';
}
else
{
	var rootUrlPrefixPath = '';
}


// bankdaten aus/an
function bankdatenAus()
{
	document.getElementById('konto').disabled = true;
	document.getElementById('blz').disabled = true;
	document.getElementById('bank').disabled = true;
	document.getElementById('inhaber').disabled = true;
}
function bankdatenAn()
{
	document.getElementById('konto').disabled = false;
	document.getElementById('blz').disabled = false;
	document.getElementById('bank').disabled = false;
	document.getElementById('inhaber').disabled = false;
}

// strstr wie in PHP
function strstr( haystack, needle, bool )
{
    var pos = 0;
 
    haystack += '';
    pos = haystack.indexOf( needle );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}


$(document).ready(function(){
	$(function() {
		$('#tabmenu a').tooltip({
			track: true,
			delay: 200,
			showURL: false,
			showBody: " - ",
			opacity: 0.85
		});
		$('.sover').tooltip({
			track: true,
			delay: 200,
			showURL: false,
			showBody: " - ",
			opacity: 0.85
		});
	});
	
	$(".expand").click(function(){
		$(this).toggleClass("noexpand");
		$("." + this.id + "_list").slideToggle();
	});

	$(".noexpand").click(function(){
		$(this).toggleClass("zexpand");
		$("." + this.id + "_list").slideToggle();
	});	
	
	// Suchfunktion
	function formatResult(row)	{return row[1];}
	function formatItem(row)	{return '<span style="float:right;">'+row[2]+'</span>'+row[0];}
	
	// lies Wert der SelectBox aus
	function getSelectedIndex()	{return document.sucheFormular.art.selectedIndex;}

	// sende Ajax Request
	$("#searchQuery").autocomplete(rootUrlPrefixPath + '/r2/search_dropdown.php', {
		minChars:2,
		width:400,
		max:30,
		matchContains: true,
		extraParams:{'item':getSelectedIndex},
		before : function(input,text) {},
		formatItem: formatItem,
		formatResult: formatResult,
		cacheLength: 0,
		selectFirst:0
	}).flushCache();
	$('input#searchQuery').flushCache();

	
	// bei Klick auf einen Suchvorschlag Weiterleitung auf die entsprechende Angebotsseite
	$("#searchQuery").result(function(event, data, formatted)
	{
		var searchPath = $("#searchpath").attr('value');
		var getParams = $("#getParams").attr('value');
		var q = $("#searchQuery").attr('value');
		
		// URLs Prefixs
		if (window.location.hostname == "linux2") {var rootUrl = 'http://linux2';var rootUrlPrefixPath = '/abodir';}
		else					{var rootUrl = 'http://www.abo-direkt.de';var rootUrlPrefixPath = '';}
		
		// TZ oder nicht TZ
		if(strstr(data,"Tageszeitung")!="") location.href = rootUrl + rootUrlPrefixPath + '/abodir/tageszeitungen/tageszeitung_abo.php?id='+getParams+'&lfd_nr=' + $(this).val();
		else location.href = searchPath+"?id="+getParams+"&aktion=A&objnr="+$(this).val()+"&progress=angebot";
		
		if(data[1]!= '') {$(this).val(data[0]); // die Zeitschrift in das Suchfeld schreiben
		}
	});
});

// Großansicht einer Prämie - als Layer
function showPramDetail(element){
	thisElement=document.getElementById(element);
	if(thisElement.getAttributeNode("class").nodeValue == "productDetailHidden")
	{
		newClass = document.createAttribute("class");
		newClass.nodeValue = "productDetailVisible";
		thisElement.setAttributeNode(newClass);
	} else
	{
		newClass = document.createAttribute("class");
		newClass.nodeValue = "productDetailHidden";
		thisElement.setAttributeNode(newClass);
	}
}

function openwindow(url)
{	fenster_n=window.open(url,"NewUser","width=565,height=600,resizable=no,scrollbars=yes");
	fenster_n.focus();
	return false;
}

function checkzahlung(object)
{
	if (typeof object == 'undefined') {ti = document.abfrage.zahlung;}
	else {ti = object;}
	si = ti.options.selectedIndex;
	
	zahlung = ti.options[si].text;
 	if(zahlung.indexOf('Rechnung') > -1)
	{	
		tb_show('Hinweis', 'https://www.sicherheitsabo.de/r2/tpl/hinweis_rechnung_ad.html?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=175&width=250', false);
		bankdatenAus();
	}
	else {bankdatenAn();}
	return true;
}	

function checkzahlung_6m(object)
{
	if (typeof object == 'undefined') {ti = document.abfrage.zahlung;}
	else {ti = object;}
	si = ti.options.selectedIndex;
	
	zahlung = ti.options[si].text;
 	if(zahlung.indexOf('Rechnung') > -1)
	{	
		tb_show('Hinweis', 'https://www.sicherheitsabo.de/r2/tpl/hinweis_rechnung_ad_6m.html?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=175&width=250', false);
		bankdatenAus();
	}
	else {bankdatenAn();}
	return true;
}

function checkzahlung_radio()
{
	//fenster_h=window.open("http://www.abo-direkt.de/abodir/hinweis_rechnung_ad.html","Hinweis","width=160,height=186,left=170,top=255;");
	fenster_h=window.open("https://www.sicherheitsabo.de/r2/tpl/hinweis_rechnung_ad.html","Hinweis","width=160,height=186,left=170,top=255;");
	return true;
}

function SetCookie(Bezeichner,Wert,Dauer){
	jetzt=new Date();
	Auszeit=new Date(jetzt.getTime()+Dauer*86400000);
	
	if(document.cookie){
		 var cookiewert = getCookie(Bezeichner);
		 if(cookiewert == 1)
			Wert = 0;
		else
			Wert = 1;
	}
	document.cookie=Bezeichner+"="+Wert+";expires="+Auszeit.toGMTString()+";";
}

function getCookie(name) {
   var cookiewert = document.cookie;

   // Anfangsposition des Name=Wert-Paars suchen
   var posName = cookiewert.indexOf("; " + name + "=");
   if (posName == -1) {
      // vielleicht war's der erste Name in der Liste?
      if (cookiewert.indexOf(name + "=") == 0) posName = 0;
      // nein? dann abbrechen mit Rückgabewert null
      else return null;
   }

   // Anfangs- und Endposition des Krümelwerts suchen
   var wertAnfang = cookiewert.indexOf("=", posName)+1;
   var wertEnde = cookiewert.indexOf(";", posName+1);
   if (wertEnde == -1) wertEnde = cookiewert.length;

   // Krümelwert auslesen und zurückgeben
   var wert = cookiewert.substring(wertAnfang, wertEnde);
   return unescape(wert);
}



// Check, ob Praesent ausgewaehlt
function radioWert(rObj, item)
{
	var a = 0;
	for (var i = 0; i < rObj.length; i++){if (rObj[i].checked) { a = 1;}}
	if (a == 1)	{return true;}
	else		{alert("Bitte wählen Sie " + item  + "aus.");return false;}
}
