/************************************************************************************/
/* $Revision: $
 * $Id: $
 *
 * Author: Coremetrics/PSD 
 * Coremetrics  v1.2, 2009/12/10
 * COPYRIGHT 1999-2008 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 * Disclaimer: Coremetrics is not responsible for hosting or maintenance or this file
 *
 */
// test: javascript: alert (document.getElementById("af_test").src)
/*===========================GLOBAL VARIABLES ===============================*/

var G_PS_ARR_DOMAIN = new Array("www.5vorflug.de","drivefti.de","fti-campermarkt.de","reiseinfos.5vorflug.de","sonnenklar.TV","FTI.de","FTI.ch","FTI.at","drivefti.fti.de","secure2.traveltainment.de","secure6.booking-ibe.de","sonnenklar.e-domizil.de","pauschalreisen.sonnenklar.tv","urlaub21.sonnenklar.tv","fluege.sonnenklar.tv","mietwagen.sonnenklar.tv","icbe.e-domizil.de","comfort.traffics-switch.de","fluege.sonnenklar.tv","urlaub20.sonnenklar.tv","urlaub22.sonnenklar.tv","urlaub23.sonnenklar.tv","urlaub24.sonnenklar.tv","comfort21.traffics-switch.de","5vorflug.de","funfvorflug.de","staedtereisen.5vorflug.de","secure.booking-ibe.de","secure.holidayandmore.de");//value must be one array of domains or null
var G_PS_ARR_N_SEARCH = new Array("google.co.uk","google.de","google.com","suche.t-online.de","bing.com","google.at","icq.com","de.yahoo.com","suche.web.de","google.ch","search.conduit.com","search.incredimail.com","google.fr","suche.aolsvc.de","suche.freenet.de","alicesuche.aol.de","suche.aol.de","google.hr","yahoo.com","google.ru","search.sweetim.com","search.babylon.com","google.be","search.imesh.com","ask.com","google.cz","search.seznam.cz","google.dk","google.nl","alltheweb.com","altavista.com","aol.com","aolrecherche.aol.fr","google.pl","google.si","baidu.com");
// current page url
var G_PS_URL_PATH = "" + document.location.href.toLowerCase();
var G_PS_PATHNAME = document.location.pathname.toLowerCase();
var G_PS_QUERYSTRING = document.location.search.toLowerCase();
var G_PS_URL_REFERRER = document.referrer.toLowerCase();
var G_PS_COOKIE_LIFETIME = 2592000; // 30*24*60*60 = 30 days
var G_PS_COOKIE_AFFILIATE = "AFFILIATE";
var G_PS_NEWCOOKIE = "SearchEngine"

/*========================= END GLOBAL VARIABLES =============================*/

/*=========================== BEGIN NAVIGATION ===============================*/
// Navigation logic should go here!
var refDomain = psGetDomain(G_PS_URL_REFERRER);
if(refDomain!=null)
{
	for(var i =0; i<G_PS_ARR_N_SEARCH.length; i++)
	{
		if(refDomain.indexOf(G_PS_ARR_N_SEARCH[i])>-1)
		{
		
			psSetCookie(G_PS_COOKIE_AFFILIATE,"","delete");
			
			CC(G_PS_COOKIE_AFFILIATE, "www.5vorflug.de");//delete cookie by calling coremetrics's cookie function
			
			psSetCookie(G_PS_COOKIE_AFFILIATE,G_PS_NEWCOOKIE,G_PS_COOKIE_LIFETIME);
			
			break;
		}
	}
}
if (psIsOrderConfirmationPage())
{

	var mmc = psGetCookie(G_PS_COOKIE_AFFILIATE);
	if(mmc!=null && psIsEqual(mmc,"Affiliate"))
	{
		makeFrame();
	}
}
else if(typeof(af_traveltype)!="undefined" && af_traveltype!=null && psIsEqual(af_traveltype.toLowerCase(),"email"))
{
	var mmc = psGetCookie(G_PS_COOKIE_AFFILIATE);
	if(mmc!=null && psIsEqual(mmc,"Affiliate"))
	{
		makeFrame();
	}
}
else 
{	
	var affiliate_Return = psGetValueFromUrl(G_PS_QUERYSTRING,"cm_ven");
	if(affiliate_Return!=null && typeof(affiliate_Return)!="undefined" && psIsEqual(affiliate_Return,"Affiliate"))
	{
		CC(G_PS_COOKIE_AFFILIATE, "www.5vorflug.de");//delete cookie by calling coremetrics's cookie function
		psSetCookie(G_PS_COOKIE_AFFILIATE,affiliate_Return,G_PS_COOKIE_LIFETIME);
	}
	else
	{
		affiliate_Return = psGetValueFromUrl(G_PS_QUERYSTRING,"cm_mmc");
		var se = /^Affiliate-_-/gi;
		if(typeof(affiliate_Return)!="undefined" && affiliate_Return!=null && affiliate_Return.search(se)>-1)
		{
			CC(G_PS_COOKIE_AFFILIATE, "www.5vorflug.de");//delete cookie by calling coremetrics's cookie function
			psSetCookie(G_PS_COOKIE_AFFILIATE,"Affiliate",G_PS_COOKIE_LIFETIME);
		}
		else if(typeof(affiliate_Return)!="undefined" && affiliate_Return!=null)//cm_mmc without Affiliate at the beginning
		{
			psSetCookie(G_PS_COOKIE_AFFILIATE,"","delete");
		}
	}
}



function makeFrame() {
	var ifrmSrc = "https://track.nonstoppartner.de/action.php?i=if&cid=188&aid=booking";
	if(typeof(af_orderID)!="undefined" && af_orderID!=null)
	{
		ifrmSrc = ifrmSrc + "&oid=" + af_orderID;
	}
	if(typeof(af_subtotal)!="undefined" && af_subtotal!=null)
	{
		ifrmSrc = ifrmSrc + "&ov1=" + af_subtotal;
	}
	if(typeof(af_traveltype)!="undefined" && af_traveltype!=null)
	{
		ifrmSrc = ifrmSrc + "&oi1=" + af_traveltype;
	}
	if(typeof(af_dlcfrom)!="undefined" && af_dlcfrom!=null)
	{
		ifrmSrc = ifrmSrc + "&oi2=" + af_dlcfrom;
	}
	if(typeof(af_dlcto)!="undefined" && af_dlcto!=null)
	{
		ifrmSrc = ifrmSrc + "&oi3=" + af_dlcto;
	}
	if(typeof(af_region)!="undefined" && af_region!=null)
	{
		ifrmSrc = ifrmSrc + "&oi4=" + af_region;
	}

	var ifrm = document.createElement("IFRAME");
	ifrm.setAttribute("id","af_test");
	ifrm.setAttribute("src", ifrmSrc);
	ifrm.setAttribute("width","1");
	ifrm.setAttribute("height","1");
	ifrm.setAttribute("scrolling","no");
	ifrm.setAttribute("marginwidth","0");
	ifrm.setAttribute("marginheight","0");
	ifrm.setAttribute("frameborder","0");
	document.body.appendChild(ifrm);
	
} 

/*
 * Determine if the page is the order confirmation page
 */


function psIsOrderConfirmationPage() 

{
	if(typeof(cm_is_order_tag)!="undefined" && cm_is_order_tag!=null)
	{
		if (cm_is_order_tag)
		{
			 return true;
		}
	}
	return false;

}

/*============================ END NAVIGATION ================================*/




/*======================= GENERAL UTILITY FUNCTION ===========================*/

/* PURPOSE: Compare case-insensitive strings
 * RETURN: true: strings are not null and the same
 *         false: any of the string is null or not the same
 */
function psIsEqual()
{
	for (var i=0; i<arguments.length; i++)
	{
		if(arguments[0] == null || arguments[i] == null)
		{
			return false;
		}
		else if(arguments[0].toUpperCase() != arguments[i].toUpperCase())
		{
			return false;
		}
	}
	return true;
}

/* PURPOSE: Get inner text of an object or remove html tags of a particular string
 *          work properly even when the designated tag/text has script tag inside
 * RETURN: resultant string or null object
 */
function psGetInnerText(pTagOjb){
	var pattern = /<script[\s\S]*?<\/script>/gi; // question mark means non-greedy
	if (pTagOjb != null)
	{
		var sT = (typeof(pTagOjb) == "object") ? pTagOjb.innerHTML : pTagOjb;
		// remove all script tags and its content
		while (sT.search(pattern) > -1)
		{
			sT = sT.replace(pattern, "");
		}
		return sT.replace(/\<+.+?\>+/g, "");
	}
	return null;
}

/* PURPOSE: Remove all unaccepted characters in categoryid, including
 * [, ', ", :, comma,]
 * RETURN: string
 */
function psCleanCatId(pCatId)
{
    return (pCatId != null) ? pCatId.replace(/[\'\":,\™\®]/g, "") : null;
}

function psCleanPageId(pPageId)
{
	return (pPageId != null) ? pPageId.replace(/[\n\t\v\r’\'\"\™\®]/gi, "") : null; 
}

function psCleanProductName(pProductName)
{
	return (pProductName != null) ? pProductName.replace(/[\n\t\v\r’\'\"\™\®]/gi, "") : null; 
}

/* PURPOSE: Remove all leading & trailing spaces of a string
 * Note: [&nbsp;] is also considered as a space
 * RETURN: string
 */
function psTrim(pStr)
{
	if (pStr == null || typeof(pStr) != "string")
		return pStr;
	return (pStr != null) ? pStr.replace(/&nbsp;|\u00A0/gi, ' ').replace(/^\s+|\s+$/g, '') : null;
}

/* PURPOSE: extract value from the URL
 * in format of http://xxx.com/page.ext?key1=value1&key2=value2
 * or key1=value1&key2=value2
 * RETURN: string value of the parameter
 */
function psGetValueFromUrl(pUrl, pKey)
{
	pUrl = (pUrl != null) ? "?" + psTrim(pUrl.toLowerCase()) : null;
	pKey = (pKey != null) ? psTrim(pKey.toLowerCase()) : null;

	if (pUrl == null || pKey == null || pUrl.indexOf(pKey) == -1) 
		return null;
	
	var start = pUrl.indexOf('&' + pKey + '=');
	start = (start == -1) ? pUrl.indexOf('?' + pKey + '=') : start;
	if (start >= 0)
	{
		start = start + pKey.length;
		var end = pUrl.indexOf("&", start);
		if(end == -1) 
			end = pUrl.length;
		var middle = pUrl.indexOf("=", start);
		return pUrl.substring(middle + 1, end);
	}
	return null;
}

/* PURPOSE: returns the value of an element based on element_id
 * @pValueFlag: TRUE means VALUE  attribute of SELECT object returned, not innerHTML
 * RETURN: 
 *  Normal tag: decoded innerHTML
 *  INPUT tag: value attribute
 *  SELECT tag: decoded label of the selected option
 */
function psGetElementValueById(pTagId, pValueFlag)
{
    var tag = document.getElementById(pTagId);
    return psGetElementValue(tag, pValueFlag);
}

/* PURPOSE: returns the value of an element based on element object
 * Note: this function returns decoded text
 * to avoid "double" decode, don't invoke psHtmlDecode on returned value again
 * @pValueFlag: TRUE means VALUE  attribute of SELECT object returned, not innerHTML
 * RETURN: 
 *  Normal tag: decoded innerHTML
 *  INPUT tag: value attribute
 *  SELECT tag: decoded label of the selected option
 *  NULL: if element not exist
 */
function psGetElementValue(pTagObj, pValueFlag)
{
    var tagValue = null;
    if (pTagObj != null)
    {
        if (pTagObj.tagName.search(/^INPUT$/i) > -1)
            tagValue = pTagObj.value;
        else if (pTagObj.tagName.search(/^SELECT$/i) > -1)
        {
            if (pValueFlag == true)
                tagValue = pTagObj.options[pTagObj.selectedIndex].value;
            else
                tagValue = psHtmlDecode(pTagObj.options[pTagObj.selectedIndex].innerHTML);// return label instead of value
        }
        else
            tagValue = psHtmlDecode(pTagObj.innerHTML);
    }

    return tagValue;
}

/* PURPOSE: Remove all leading & trailing spaces of a string
 * Note: [&nbsp;] is also considered as a space
 * RETURN: string
 */
function psTrim(pStr)
{
	if (pStr == null || typeof(pStr) != "string")
		return pStr;
	return (pStr != null) ? pStr.replace(/&nbsp;|\u00A0/gi, ' ').replace(/^\s+|\s+$/g, '') : null;
}

/* PURPOSE: extract value from the URL
 * in format of http://xxx.com/page.ext?key1=value1&key2=value2
 * or key1=value1&key2=value2
 * RETURN: string value of the parameter
 */
function psGetValueFromUrl(pUrl, pKey)
{
	pUrl = (pUrl != null) ? "?" + psTrim(pUrl.toLowerCase()) : null;
	pKey = (pKey != null) ? psTrim(pKey.toLowerCase()) : null;

	if (pUrl == null || pKey == null || pUrl.indexOf(pKey) == -1) 
		return null;
	
	var start = pUrl.indexOf('&' + pKey + '=');
	start = (start == -1) ? pUrl.indexOf('?' + pKey + '=') : start;
	if (start >= 0)
	{
		start = start + pKey.length;
		var end = pUrl.indexOf("&", start);
		if(end == -1) 
			end = pUrl.length;
		var middle = pUrl.indexOf("=", start);
		return pUrl.substring(middle + 1, end);
	}
	return null;
}

/* PURPOSE: validate email format
 * RETURN: boolean
 */
function psCheckEmail(pEmail) 
{
    if (pEmail)
    {
        var i = pEmail.search(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
        return (i > -1);
    }

    return false;
}

/* PURPOSE: convert special HTML characters to normal character
 * Note: for each project, this function needs to be updated
 * RETURN: decoded string
 */
function psHtmlDecode(pValue)
{
    if (pValue)
    {
        pValue = pValue.replace(/&nbsp;/gi, " ");
        pValue = pValue.replace(/&quot;/gi, '"');
        pValue = pValue.replace(/&amp;/gi, "&");
        pValue = pValue.replace(/&lt;/gi, "<");
        pValue = pValue.replace(/&gt;/gi, ">");
    }

    return pValue;
}

/* PURPOSE: extract main domain from the URL
 * RETURN: main domain
 */
function psGetMainDomain(pUrl){
	var se = /^https*\:\/\/([^\/\:]+)/gi;
	var domain = (pUrl.search(se) > -1) ? RegExp.$1 : null;
	if(domain != null)
	{
		if(domain.indexOf("www")==0)
		{
			domain = domain.substring(4,domain.length);
		}
		if(G_PS_ARR_DOMAIN != null)
		{
			for(var i =0; i<G_PS_ARR_DOMAIN.length; i++)
			{
				se = new RegExp("[\.]" + G_PS_ARR_DOMAIN[i] + "$","gi");
				if(("." + domain).search(se) > -1)
				{
					domain = G_PS_ARR_DOMAIN[i];
					break;
				}
			}
		}
		domain = "." + domain;
	}
	return domain;
	
}

/* PURPOSE: extract domain part in the URL
 * RETURN: domain
 */
function psGetDomain(pUrl){
    var se = /^https*\:\/\/([^\/\:]+)/gi;
    return (pUrl.search(se) > -1) ? RegExp.$1 : null;
}

/* PURPOSE: remove unnecessary characters (dollar sign, comma, quote, minus, etc) 
 * from price to make it work properly with parseFloat/parseInt
 * RETURN: well-formed price
 */
function psCleanPrice(pPrice)
{
	var pattern = /[^0-9\.]/gi;
    return (pPrice != null ? pPrice.toString().replace(pattern, "") : null);
}

/* PURPOSE: retrieve cookie value
 * RETURN: string
 */
function psGetCookie(pCookieName)
{
	var cookies = document.cookie;
	if (!pCookieName || !cookies)
		return null;

	cookies = "; " + cookies.toLowerCase();
	var key = "; " + pCookieName.toLowerCase() + "=";
	var start = cookies.lastIndexOf(key);
	if (start >= 0)
	{
		start = start + key.length;
		var end = cookies.indexOf(";", start);
		if (end == -1)
			end = cookies.length;

		return unescape(cookies.substring(start, end));
	}

    return null;
}

function psCookieBase(pCookieName, pCookieValue, pLifeTime)
{
	var pDomain = psGetMainDomain(G_PS_URL_PATH);
	CC(pCookieName, pDomain);//delete cookie by calling coremetrics's cookie function
	if(pLifeTime == "delete") 
	{         
		return true;
	}
	
	// set cookie by calling coremetrics's cookie function
	var expire = (pLifeTime) ? (new Date((new Date()).getTime() + (1000 * pLifeTime))).toGMTString() : null;
	
	return CB(pCookieName, escape(pCookieValue), expire, pDomain);
}

function encodeHtml(strValue)
{
	if (strValue!=null)
	{
		strValue = escape(strValue);
		strValue = strValue.replace(/\//g,"%2F");
		strValue = strValue.replace(/\?/g,"%3F");
		strValue = strValue.replace(/=/g,"%3D");
		strValue = strValue.replace(/&/g,"%26");
		strValue = strValue.replace(/@/g,"%40");
	}
	return strValue;
}

/* PURPOSE: set cookie value
 * Note: if the designated cookie is too big, the old items will be removed
 * because cookie size is limited to 4K
 * @pLifeTime in seconds
 * pDomain: don't specify if using current domain
 * RETURN: boolean
 */
function psSetCookie(pCookieName, pCookieValue, pLifeTime)
{
	if (!pCookieName)
	{
		return false;
	}
	
	pCookieValue = (pCookieValue==null)? "null":pCookieValue;
	
	if(pLifeTime != "delete")
	{
		pCookieName = psTrim(pCookieName);
		var oldCookieValue = psGetCookie(pCookieName);
		oldCookieValue = (oldCookieValue==null)? "null":oldCookieValue;
		var totalSize;
		if(document.cookie.indexOf(pCookieName)>-1)
		{
			totalSize =  document.cookie.length + encodeHtml(pCookieValue).length - encodeHtml(oldCookieValue).length;
		}
		else
		{
			totalSize =  document.cookie.length  + encodeHtml(pCookieValue).length + encodeHtml(pCookieName).length;
		}
		if(totalSize > 3500)
		{
			return false;
		}
	}
	psCookieBase(pCookieName,pCookieValue,pLifeTime);
}

/* PURPOSE: set value in cookie in format of:
 * #key1~value1#key2~value2
 * RETURN: string
 * NOTE: Use null or '' for pValue to remove the pair specified by pKey
 */
function psSetValueToCookie(pCookieName, pKey, pValue)
{
	if (!pCookieName || !pKey)
	{
		return false;
	}

	pCookieName = psTrim(pCookieName);
	pKey = (pKey != null) ? "#" + psTrim(pKey).toLowerCase() + "~" : pKey;
	var catCookie = psGetCookie(pCookieName);
	catCookie = (catCookie == null) ? "" : catCookie;
	pValue = (pValue == null) ? "null" : pValue;
	var oldCatCookie = catCookie;
	
	var start = catCookie.indexOf(pKey);
	
	var totalsize;
	if (start >= 0) // Store before -> remove the old value
	{
		var oldValue = psGetValueFromCookie(pCookieName,pKey.replace(/[\~\#]/gi,""));
		oldValue = (oldValue == null) ? "null" : oldValue;
		var end = catCookie.indexOf("#", start + pKey.length);
		if (end == -1)
			end = catCookie.length;
		catCookie = catCookie.replace(catCookie.substring(start, end), "");
		totalsize =  document.cookie.length  + encodeHtml(pKey).length + encodeHtml(pValue).length - encodeHtml(oldValue).length;
	}
	else
	{
		totalsize =  document.cookie.length  + encodeHtml(pKey).length + encodeHtml(pValue).length;  
		if(document.cookie.indexOf(pCookieName)<0)
		{
			totalsize += encodeHtml(pCookieName).length;
		}
	}
	catCookie = pKey + pValue + catCookie;
	var cookieArray = null;   
	//Check existed ?
	while (totalsize > 3500)
	{
		var l1 = encodeHtml(catCookie).length;//length before pop
		cookieArray = catCookie.split("#");
		cookieArray.pop();
		catCookie = cookieArray.join("#");
		var l2 = encodeHtml(catCookie).length;//length after pop                                                        
		totalsize -=  (l1-l2);
	}   
	if(catCookie == null || catCookie == "")
	{
		catCookie = oldCatCookie;
	}
	// Save to cookie              
	psCookieBase(pCookieName, catCookie, G_PS_COOKIE_LIFETIME);
}

/* PURPOSE: get value stored in cookie in format of:
 * #key1~value1#key2~value2
 * RETURN: string
 */
function psGetValueFromCookie(pCookieName, pKey)
{
	// "normalize" input parameters
	pCookieName = psTrim(pCookieName);
	pKey = (pKey != null) ? "#" + psTrim(pKey).toLowerCase() + "~" : pKey;
	// extract catId associated with the specified key (pKey)
    var catCookie = psGetCookie(pCookieName);
    if (catCookie != null)
    {
        var start = catCookie.indexOf(pKey);
		if (start >=0 )
		{
			start = start + pKey.length;
			var end = catCookie.indexOf("#", start);
			if (end == -1)
				end = catCookie.length;
			return catCookie.substring(start, end);
		}
		return null;
    }
    return null;
}