var g_lngGoogleIndex   = 0;			// initialize current google ad index
var g_strPartnerSearch = "";		// initialize Partner Search string


// Arrays for partener ads
if ( typeof( partnerAdsTitle       ) == "undefined" )  { partnerAdsTitle       = new Array(); }
if ( typeof( partnerAdsDescription ) == "undefined" )  { partnerAdsDescription = new Array(); }
if ( typeof( partnerAdsImageUrl    ) == "undefined" )  { partnerAdsImageUrl    = new Array(); }
if ( typeof( partnerAdsClickUrl    ) == "undefined" )  { partnerAdsClickUrl    = new Array(); }
if ( typeof( partnerAdsPrice       ) == "undefined" )  { partnerAdsPrice       = new Array(); }
if ( typeof( partnerAdsFooter      ) == "undefined" )  { partnerAdsFooter      = new Array(); }
if ( typeof( partnerAdsOtherText   ) == "undefined" )  { partnerAdsOtherText   = new Array(); }
if ( typeof( partnerAdsOtherUrl    ) == "undefined" )  { partnerAdsOtherUrl    = new Array(); }
if ( typeof( partnerAdsGAurl       ) == "undefined" )  { partnerAdsGAurl       = new Array(); }

if ( typeof( aryPartnerAdsBoxHeadline ) == "undefined" )  { aryPartnerAdsBoxHeadline = new Array(); }
if ( typeof( aryPartnerAdsMoreAdsUrl  ) == "undefined" )  { aryPartnerAdsMoreAdsUrl  = new Array(); }
if ( typeof( aryPartnerAdsMoreAdsText ) == "undefined" )  { aryPartnerAdsMoreAdsText = new Array(); }


function CreatePartnerAdsData( strPartner, lngIndex, strTitle, strDescription, strImageurl, strClickurl, strPrice, strFooter, strOthertext, strOtherurl, strGAnalyticsurl )
	{
	partnerAdsTitle[       strPartner][ lngIndex ] = strTitle;
	partnerAdsDescription[ strPartner][ lngIndex ] = strDescription;
	partnerAdsImageUrl[    strPartner][ lngIndex ] = strImageurl;
	partnerAdsClickUrl[    strPartner][ lngIndex ] = strClickurl;
	partnerAdsPrice[       strPartner][ lngIndex ] = strPrice;
	partnerAdsFooter[      strPartner][ lngIndex ] = strFooter;
	partnerAdsOtherText[   strPartner][ lngIndex ] = strOthertext;
	partnerAdsOtherUrl[    strPartner][ lngIndex ] = strOtherurl;
	partnerAdsGAurl[       strPartner][ lngIndex ] = strGAnalyticsurl;
	}


function WrapAdText ( strText, lngLength ) {

	// strText = "CD**SOFTWARE**digital-dance**1988**Excelent";
	// strText = "UNIVERSUM DVD-Heimkino-System-Anlage";
	// strText = "57234Wilnsdorf.1-Zimmer-K¸che-Bad-Wohnung mit TERR";

	// prepare Text for wrapping
	strWrapText = strText.replace( /([,:;\/\.\+\*-])([^0-9 ])/g, '$1 $2' );
	//strWrapText = strWrapText.replace( /([^\s]{2,})/g, '$1 ' );
	strWrapText = strWrapText.replace( /(\w{17})/g, '$1 ' );	// Insert ' ' after 17 chars
	strWrapText = strWrapText.replace( / +/g, ' ' );
	strWrapText = strWrapText.substr( 0, lngLength );
//	if ( strText != strWrapText )
//		alert( "DEBUG:\nText original: " + strText + "\nText wrapped: " + strWrapText );
	return( strWrapText );
}


function FormatPrice( strPriceText ) {
	var strPriceFormated = "";
	var aryPrice;

	if ( strPriceText != "" )
		{
		strPriceFormated = strPriceText;
		strPriceFormated = strPriceFormated.replace( /&nbsp;/, " " );
		strPriceFormated = strPriceFormated.replace( /EUR/, "" );
// 		strPriceFormated = strPriceFormated.replace( /\.([0-9]{2}$)/, ",$1" );
		strPriceFormated = strPriceFormated.replace( /\.([0-9]+$)/, ",$1" );
		strPriceFormated = strPriceFormated.replace(/^\s+|\s+$/g,"");
		aryPrice = strPriceFormated.split( "," );

		if ( aryPrice.length > 1 )
			strPriceCent = aryPrice[1];
		else
			strPriceCent = "";

		aryPriceEUR = aryPrice[0].split( " " );
		if ( aryPriceEUR.length > 1 )
			{
			strPrePrice = aryPriceEUR[0] + " ";		// Text before price e.g. "ab"
			strPriceEUR = aryPriceEUR[1];
			}
		else
			{
			strPrePrice = "";
			strPriceEUR = aryPriceEUR[0];
			}

		strPriceEUR  = strPriceEUR.replace( /^\s+|\s+$/g, "" );
		strPriceCent = strPriceCent.replace( /^\s+|\s+$/g, "" );
// 		strPriceText = strPriceText + "<br>" + strPriceEUR + " EUR " + strPriceCent + " Cent"

		if ( parseInt( strPriceCent ) > 0 )
			{
			while ( strPriceCent.length < 2 )
				{
				strPriceCent += "0"; // Nachkommastellen mit 0 auffuellen
				}
			}
		else if ( ( strPriceCent == "" ) || ( parseInt( strPriceCent ) == 0 ) )
			strPriceCent = "-";

		strNewPriceEUR = "";
		while ( strPriceEUR.length > 3 )
			{
			strNewPriceEUR = "." + strPriceEUR.substr(strPriceEUR.length - 3, 3) + strNewPriceEUR;
			strPriceEUR = strPriceEUR.substr( 0, strPriceEUR.length - 3 );
			}
		strNewPriceEUR = strPriceEUR + strNewPriceEUR;			
		strPriceFormated = strPrePrice + strNewPriceEUR + "," + strPriceCent;
		}
	
//	return strPriceText + "<br>" + strPriceFormated;
	return strPriceFormated;
}
	

function HighlightWords( strText, strWords )
	{
	var i, k;
	// alert( "DEBUG: HighlightWords: \n" + strText + "\n" + strWords );

	strWords = strWords.replace(/^\s+|\s+$/g,"");	// trim
	if ( strWords != '' )
		{
		aryWords = strWords.split( " " );
		for ( i = 0, k = aryWords.length; i < k; i++ )
			{
			strRegEx = '(' + aryWords[i] + ')';
			//alert( "DEBUG: HighlightWords strRegEx: " + strRegEx );
			
			objRegEx = new RegExp( strRegEx, "gi" );
			strText = strText.replace( objRegEx, "<font class='highlightcolor'>$1</font>" );
			}
		}

	// alert( "DEBUG: HighlightWords Result: " + strText );
	return( strText );
	}


// show Partner Ads 
function DisplayPartnerAd(dpaTemplate,dpaImageUrl,dpaClickUrl,dpaTitle,dpaDescription,
                          dpaPrice,dpaFooter,dpaOtherText,dpaOtherUrl,dpaGAurl,dpaType ) {	
  if (dpaTemplate == null) dpaTemplate="resultsmall";
  if (dpaImageUrl == null) dpaImageUrl="";
  if (dpaClickUrl == null) dpaClickUrl="";
  if (dpaTitle == null) dpaTitle="";
  if (dpaDescription == null) dpaDescription="";
  if (dpaPrice == null) dpaPrice="&nbsp;";
  if (dpaFooter == null) dpaFooter="";
  if (dpaOtherText == null) dpaOtherText="";
  if (dpaOtherUrl == null) dpaOtherUrl="";
  if (dpaGAurl == null) dpaGAurl="";
  if (dpaType == null) dpaType="Partner-Anzeige";
  
  if ((dpaClickUrl != "") && (dpaTitle != "")) {
    if (dpaTemplate == "resultsmall") {
        dpaImageWidth = "60";
    }
    else {
        dpaImageWidth = "80";
    }

	s_dpaGAHTML = ( dpaGAurl != "" ) ? ' OnClick="javascript:urchinTracker(' + "'" + dpaGAurl + "'" + ')"' : "";

    if (dpaImageUrl != "") {
      s_dpaIMAGEHTML="<a href=\""+dpaClickUrl+'"'+s_dpaGAHTML+" target=\"_blank\"><img src=\""+dpaImageUrl+"\" width=\""+dpaImageWidth+"\" border=\"0\"></a>";
    } else {
      s_dpaIMAGEHTML="&nbsp;";
    }

	s_dpaPRICEHTML = FormatPrice( dpaPrice ) + "&nbsp;<br/>&nbsp;<br/>";

    if (dpaFooter != "") {
      s_dpaFOOTERHTML="<br><a href=\""+dpaClickUrl+'"'+s_dpaGAHTML+" target=\"_blank\"><i>"+dpaFooter+"</i></a>";
    }
    else {
      s_dpaFOOTERHTML="&nbsp;";
    }

    if (dpaOtherText != "") {
      s_dpaOTHERHTML="<span class=\"paletextsmall\">&nbsp;|&nbsp;</span><a href=\""+dpaOtherUrl+"\" target=\"_blank\" class=\"paletextsmall\">"+dpaOtherText+"</a>";
    } else {
      s_dpaOTHERHTML="&nbsp;";
    }
    
    dpaRandNo=(Math.random()*100000);

    switch( dpaTemplate )
  		{
  		case "resultstart":
  		case "resultend":
			strHTMLAnchorEnd1  = ( dpaType == "Google-Anzeige" ) ? '</a>' : "";
			strHTMLAnchorEnd2  = ( dpaType == "Google-Anzeige" ) ? '' : "</a>";
			strHTMLPartnerHead = ( dpaType == "Google-Anzeige" ) ? '<div class="headline"><a href="http://services.google.com/feedback/online_hws_feedback" target="_blank">Google-Anzeige</a></div><!-- class="headline" -->' : "";

			if ( ( dpaType != "Google-Anzeige" ) && ( dpaDescription.length > 80 ) )
				 dpaDescription = dpaDescription.substr(0,80) + '...';

			if ( dpaType == "Google-Anzeige" )
				dpaType = "";	// No Text "Google-Anzeige" output because this will be displayed in the headline

			strTitle       = ( g_strPartnerSearch != "" ) ? HighlightWords( dpaTitle.substr(0,50), g_strPartnerSearch ) : '<b>' + dpaTitle.substr(0,50) + '</b>';
			strDescription = HighlightWords( dpaDescription, g_strPartnerSearch );

			strHTML  = '<table cellspacing="0" cellpadding="0" border="0">' + "\n";
			strHTML += '<tr>' + "\n";
			// strHTML += '<td class="info">&nbsp;</td>' + "\n";
			strHTML += '<td class="image">' + s_dpaIMAGEHTML + '</td>' + "\n";
			strHTML += '<td class="detail">' + strHTMLPartnerHead + '<a href="' + dpaClickUrl + '"' + s_dpaGAHTML + ' target="_blank"><b>' + strTitle + '</b>';			
			strHTML += strHTMLAnchorEnd1 +'<br>' + dpaDescription + strHTMLAnchorEnd2 + '<br>' + s_dpaFOOTERHTML + s_dpaOTHERHTML + '</td>' + "\n";
			// strHTML += '<td class="price">&nbsp;</td>' + "\n";
			strHTML += '<td class="adtype">' + dpaType + '</td>' + "\n";
			// strHTML += '<td class="date">&nbsp;</td>' + "\n";
			strHTML += '</tr>' + "\n";
			strHTML += '</table>' + "\n";
			strHTML += '<hr>' + "\n";
			document.write( strHTML );
			break;

		default:
			{
			document.writeln("<div class=\"standard\">");
			document.writeln("<a name=\""+dpaRandNo+"\">");
			document.writeln("<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">");
			document.writeln("<tr valign=\"top\">");
			document.writeln("<td class=\"image\">"+ s_dpaIMAGEHTML + "<br></td>");
			document.writeln("<td class=\"detail\">");
			document.writeln("<a href=\""+dpaClickUrl+'"'+s_dpaGAHTML+" target=\"_blank\">");
			document.writeln("<p class=\"noMargin\">");
			document.writeln("<strong>"+dpaTitle.substr(0,50)+"</strong>");
			document.writeln("<br>"+dpaDescription+s_dpaFOOTERHTML);
			document.writeln("</p></a></td>");
			document.writeln( '<td class="price">' + s_dpaPRICEHTML + '</td>' );
			document.writeln("<td class=\"location\" valign=\"middle\">");
			document.writeln("<p class=\"noMargin\">"+dpaType+"</p></td>");
			document.writeln("<td class=\"date\">&nbsp;</td>");
			document.writeln("</tr></table></a></div>");
			document.writeln("<hr>");
			}
		}
    
  	return 0;
  } 
  else return -1;
}

// show external ads

// Show google links in a block
function ShowSponsoredAds( lngCount, strTemplate, blnShowHeader ) {
	var i;

	if ( typeof(sponsoredAdsTitle) != "undefined" ) {
		if ( sponsoredAdsTitle.length > 0 ) {
			if ( lngCount > sponsoredAdsTitle.length )
				lngCount = sponsoredAdsTitle.length;

			document.writeln( '<div class="partnerblock">' );
			for ( i = 0; i < lngCount; i++ ) {
				ShowSponsoredLink( g_lngGoogleIndex, strTemplate, '', blnShowHeader, true, false );
			}
			document.writeln( '</div><!-- class="partnerblock" -->' );
		}
	}
}

function ShowSponsoredLink(adsIndex,strTemplate,adsNotation,blnShowHeader,blnShowGoogle,blnShowPartner) {
	// alert ( "DEBUG: ShowSponsoredLink( " + adsIndex + ", " + strTemplate + ", " + adsNotation + ", " + blnShowHeader + " )" );

	if (blnShowHeader  == null) blnShowHeader  = false;
	if (blnShowGoogle  == null) blnShowGoogle  = true;
	if (blnShowPartner == null) blnShowPartner = true;

	if ( typeof( b_HEADERDISP ) == "undefined")
		b_HEADERDISP = false;	// Set "Header not displayed"

	if (blnShowHeader && !b_HEADERDISP)	{
		document.writeln( '<div class="resultheadline">' );
		document.writeln( '<table cellspacing="0" cellpadding="2" border="0"><tr><td>' );
		document.writeln( '<h1>Angebote unserer Partner:</h1>' );
		document.writeln( '</td></tr></table>' );
		document.writeln( '</div><!-- class="resultheadline" -->' );
		b_HEADERDISP = true;	// Set "Header displayed"
	}

	// show google ads
	if (blnShowGoogle == true ) {
		if ( typeof(sponsoredAdsTitle) != "undefined" ) {
			if ( sponsoredAdsTitle.length > 0 ) {
				adsGoogleIndex = g_lngGoogleIndex++;
				// alert ( "DEBUG: adsIndex = " + adsIndex + "\n adsGoogleIndex: " + adsGoogleIndex + "\n g_lngGoogleIndex: " + g_lngGoogleIndex );

				// Get valid no of ad to display -> show the same ads again if there are not enough avalable
				while ( adsGoogleIndex >= sponsoredAdsTitle.length ) {
					adsGoogleIndex -= sponsoredAdsTitle.length;
				}

				strGAUrl = sponsoredAdsGAUrl[adsGoogleIndex];
				strGAUrl = strGAUrl.replace( /jsresult/g, strTemplate );

				// alert ( "DEBUG: sponsoredAds = " + sponsoredAdsTitle.length + "\n adsIndex = " + adsIndex + "\n adsGoogleIndex: " + adsGoogleIndex + "\n g_lngGoogleIndex: " + g_lngGoogleIndex + "\n strGAUrl: " + strGAUrl );
				DisplayPartnerAd( strTemplate, '',
					sponsoredAdsClickUrl[adsGoogleIndex],
					sponsoredAdsTitle[adsGoogleIndex],
					sponsoredAdsDescription[adsGoogleIndex],
					'',
					sponsoredAdsSiteHost[adsGoogleIndex],
					'',
					'',
					strGAUrl,
					'Google-Anzeige'
					);
			}
		}
	}

	// alert ( "DEBUG: blnShowPartner = " + ( ( blnShowPartner == true ) ? "true" : "false" ) );

	// show partner ads as delivered in array
	if ( ( blnShowPartner == true ) && typeof( partnerAdsTitle ) != "undefined" )	{
		for (var partnerName in partnerAdsTitle) {
			lngAdIndex = adsIndex - 1;
			// alert( "typeof( partnerAdsTitle[ " + partnerName + " ] ) = " + typeof( partnerAdsTitle[partnerName] ) );
			if ( ( typeof( partnerAdsTitle[partnerName] ) == "object" ) && partnerAdsTitle[partnerName].length > 0 ) {
				// Get valid no of ad to display -> show the same ads again if there are not enough avalable
				while ( lngAdIndex >= partnerAdsTitle[partnerName].length ) {
					lngAdIndex -= partnerAdsTitle[partnerName].length;
				}

				strAdType = "Partner-Anzeige";

				// alert ( "DEBUG: " + partnerAdsTitle[partnerName].length + " Anzeigen von Partner " + partnerName + "\n " + lngAdIndex + "(" + adsIndex + " ) Anzeige '" + partnerAdsTitle[partnerName][lngAdIndex] + "'" );
				strGAUrl = partnerAdsGAurl[partnerName][lngAdIndex];
				strGAUrl = strGAUrl.replace( /jsresult/g, strTemplate );

				DisplayPartnerAd(	strTemplate,
									partnerAdsImageUrl[partnerName][lngAdIndex],
									partnerAdsClickUrl[partnerName][lngAdIndex],
									partnerAdsTitle[partnerName][lngAdIndex],
									partnerAdsDescription[partnerName][lngAdIndex],
									partnerAdsPrice[partnerName][lngAdIndex],
									partnerAdsFooter[partnerName][lngAdIndex],
									partnerAdsOtherText[partnerName][lngAdIndex],
									partnerAdsOtherUrl[partnerName][lngAdIndex],
									strGAUrl,
									strAdType );
			}
		}
	}
}
