
/* Version 30.08.10 */



var clientId = 1091;

var praxisHostArr = new Array('www.imwalking.de','www.sichereinkaufen.info');

var testHost = "yala.otto.de";

var testHostSslEnabled = 1;

var trace_mode = 0;

var domain_name = ".imwalking.de";


function getLMServletHost() {
	var host = location.hostname;
	var protocol = "http://";
	var praxisHosts = praxisHostArr.join(" ") + " ";
	if ( praxisHosts.toUpperCase().indexOf( host.toUpperCase() + " " ) > -1 ) {
		if ( location.protocol.toUpperCase().indexOf( "HTTPS" ) > -1 ) 
			protocol = "https://";
	} else {	
		host = testHost;
		if ( testHostSslEnabled == 1 ) 
			if ( location.protocol.toUpperCase().indexOf( "HTTPS" ) > -1 ) 
				protocol = "https://";
	}
	return protocol + host;
}

function isSid( s ) {
  if ( s == null ) return false;
  if ( s == "" ) return false;
  if ( s.indexOf( "?" ) > -1 ) return false;
  if ( s.indexOf( "&" ) > -1 ) return false;
  if ( s.indexOf( "/" ) > -1 ) return false;
  if ( s.indexOf( "10") == 0 ) return false;
  if ( s.length<32 ) return false;
  if ( s.length>64 ) return false;
  return true;
}

function hasSid( URL ) {
  if ( URL == null ) return false;	
  var startTag = ";sid=";
  var pos = URL.indexOf( startTag );
  return ( pos > -1 )
}
  
function getSessionIdFromUrl( URL ) {
  var startTag = ";sid=";
  var pos = URL.indexOf( startTag );
  if ( pos != -1 ) URL = URL.substring( pos + startTag.length );
  pos = URL.indexOf( "?" );
  if ( pos != -1 ) URL = URL.substring( 0, pos );
  // wenn hash existiert aber wegen url rewriting keine Parameter
  pos = URL.indexOf( "#" );
  if ( pos != -1 ) URL = URL.substring( 0, pos );
  // tk 22.10.04 added g -> replace all '=' signs
  // dw removed 30.08.10 URL = URL.replace( /=/g, '' );
  return URL;
}

function getSessionId( URL ) {
  	// first given sid	
  	if ( isSid( URL ) ) {
  		if ( trace_mode == 1 ) alert( "given sid" )
  		return URL;	
  	}	
  	// cookie
  	var sid = getStatCookie( "sid" );
  	if ( sid != null && sid != "" && sid != "null" ) {
  		if ( trace_mode == 1 ) alert( "sid cookie" )
  		return sid;
  	}
  	// sid from given url	 	
  	if ( hasSid( URL ) ) {
  		if ( trace_mode == 1 ) alert( "given url" )
  		return getSessionIdFromUrl( URL );
  	}	
  	// search sid in links	
  	if( document.links ) {
    	var found = 0;
    	var i = 0;
		sid = "";
    	while( i<document.links.length && found == 0 ) {
      		if( hasSid( document.links[ i ].href ) ) {
        		sid = getSessionIdFromUrl( document.links[ i ].href );
        		found = 1;
      		}
	  		i++;
		}
  		if ( trace_mode == 1 ) alert( "found in link" )
		return sid;
  	}	
}	


function getArtNoFromProdRef( prodRef ) {
  var artNo = "";
  var arr = prodRef.split( /@/ );
  if ( arr[2] ) {
    var arr2 = arr[2].split( /-/ );
    if ( arr2[0] ) artNo = arr2[0];
  }  
  return artNo;
}


function getStatCookie( name ) {
   name += "=";
   var len = name.length;
   var cookie_len = document.cookie.length;
   var i = 0;
   while ( i < cookie_len ) {
     var j = i + len;
	 if ( document.cookie.substring(i,j) == name ) return getStatCookieValue(j);
	 i = document.cookie.indexOf( " ", i ) + 1;
	 if ( i == 0 ) break;
   }
   return null;
}

function getStatCookieValue( position ) {
   var end = document.cookie.indexOf(";", position );
   if ( end == -1 ) end = document.cookie.length;
   return unescape( document.cookie.substring( position, end ) );
}

function setStatCookie(name, value, expires, path) {
   var exp = new Date();
   exp.setTime(exp.getTime() + expires);
   document.cookie = name + "=" + value + "; expires=" + exp.toGMTString() +
	     ((path == null ) ? "" : ("; path=" + path ));
}

function setStatTempCookie(name, value, path) {
	var cookie_string = name + "=" + value + ((path == null ) ? "" : ("; path=" + path ));
	if ( domain_name != "" )
		cookie_string += ";domain=" + domain_name;
	document.cookie = cookie_string;
}

function getReferrer() {
	var attr = "";
	var wkReferrer = getStatCookie( "wkReferrer" );
	if ( trace_mode == 1 ) alert( "referrer from cookie:/" + wkReferrer + "/" )
	if ( wkReferrer != null && wkReferrer != "" ) {
		if ( wkReferrer.indexOf( location.hostname ) == -1 ) {
			attr = "&setWkReferrer=" + escape( wkReferrer );
		}	
		setStatTempCookie( "wkReferrer", "", "/" );
	}	
	else {
		wkReferrer = document.referrer;
		if ( trace_mode == 1 ) alert( "referrer from document.referrer:/" + wkReferrer + "/" )
		if ( wkReferrer != null && wkReferrer != "" ) {
			if ( wkReferrer.indexOf( location.hostname ) == -1 || wkReferrer.indexOf( location.hostname ) > 10 ) {
				attr = "&setWkReferrer=" + escape( wkReferrer );
			}	
			setStatTempCookie( "wkReferrer", "", "/" );
		}	
	}
	if ( trace_mode == 1 ) alert( "referrer-attr:/" + attr + "/" )
	return attr;	
}

function getLocationSearchImas() {
  var uebergabe=location.search.substring(1,location.search.length);
  uebergabe=unescape(uebergabe);
  var angaben=new Array();
  if ( uebergabe.indexOf("&") > -1 ) {
  	var angabenTemp=uebergabe.split("&");
    for( var z = 1; z < angabenTemp.length; z++){
		var paar=angabenTemp[z].split("=");
		// assoziatives Arrayfeld bilden
		angaben[paar[0]]=paar[1];
  	}
  }	
  return angaben;
  // access values with angaben[<AttributeName>]
}

function getPromo() {
	var attr = "";
	// get it from anchor
	var hash = location.hash;
	if ( hash.indexOf( "lmPromo" ) > -1 ) {
    	var arr = hash.split( /=/ );
    	if ( arr.length > 1 ) 
    		attr = "&lmPromo=" + escape( arr[1] );
	}
	// get it from parameter
	if ( attr == "" ) {
		var pars = getLocationSearchImas();
		try {
			var lmPromo = pars[ "lmPromo" ];
			if ( lmPromo )
				attr = "&lmPromo=" + escape( lmPromo );
		} catch(e) {
		}	
	}
	return attr;
}


var LMArrLen = 10;
var LMimg = new Array(LMArrLen);
var LMArrPos = 0;

function setLandmarkWithSession( sessionId, landmark, attributes ) {
  var sessionId = clientId + getSessionId( sessionId );
  sessionId = sessionId.replace( /=/g, '' );
  LMimg[ LMArrPos ] = new Image();
  var URL = getLMServletHost() + "/servlet/LandmarkServlet?clientId=" + clientId + 
                        "&landmark=" + landmark +
                        "&ssid=" + sessionId;
  if ( attributes ) URL = URL + attributes;  
  URL = URL + getReferrer();                      
  URL = URL + getPromo();
  LMimg[ LMArrPos++ ].src = URL;     
  if ( LMArrPos == LMArrLen ) LMArrPos = 0;
  if ( trace_mode != 0 ) alert( "Lm set: " + URL );
}

function setLandmark( landmark, attributes ) {
  setLandmarkWithSession( null, landmark, attributes );
}




var mp_landmark = "";
var mp_attributes = "";
var mp_sessionId = "";
var mp_descriptions = ""; // special var for LM special basket, description will be omitted in case of more than 10 basket entries to avoid too long urls
var mp_count = 0;         // counts number of items in basket 

function startLandmark( landmark ) {
  mp_sessionId = location.href;
  mp_attributes = "";
  mp_descriptions = "";
  mp_count = 0;
  mp_landmark = landmark;
}

function addBasketStatus( status ) {
  var attributes = "&status=";
  if ( status == "5" ) attributes += "BB"; 
  else
    if ( status == "4" ) attributes += "LE"; 
    else attributes += "AN"; 
  addAttribute( attributes );
}

function addAttribute( attribute ) {
  if ( attribute.indexOf( "&" ) != 0 ) attribute = "&" + attribute;
  mp_attributes = mp_attributes + attribute;
}

function addArtDescription( description ) {
  if ( description.indexOf( "&" ) != 0 ) description = "&" + description;
  mp_descriptions = mp_descriptions + description;
  mp_count += 1;
}

function setSessionId( sessionId ) {
  mp_sessionId = sessionId;
}

function setMPLandmark() {
  if ( mp_count <= 10 ) mp_attributes += mp_descriptions;
  setLandmarkWithSession( mp_sessionId, mp_landmark, mp_attributes );
}  



function redirectLandmarkGeneralWithSession( sessionId, landmark, attributes, redirectURL, mitHistory ) {
  var URL = getLMServletHost() + "/servlet/LandmarkServlet?clientId=" + clientId + 
                        "&landmark=" + landmark +
                        "&ssid=" + clientId + sessionId +
			"&redirectUrl=" + escape( redirectURL );
  if ( attributes ) URL = URL + attributes;       
  if ( trace_mode != 0 ) alert( "Lm set: " + URL );
  if ( mitHistory != 0 ) window.document.location.href = URL;
  else 			 window.document.location.replace( URL );
}

function redirectLandmarkGeneral( landmark, attributes, redirectURL, mitHistory ) {
  var sessionId = getSessionId( redirectURL );
  redirectLandmarkGeneralWithSession( sessionId, landmark, attributes, redirectURL, mitHistory );
}


function redirectLandmark( landmark, attributes, redirectURL ) {
  redirectLandmarkGeneral( landmark, attributes, redirectURL, 1 );
}

function redirectLandmarkNoHistory( landmark, attributes, redirectURL ) {
  redirectLandmarkGeneral( landmark, attributes, redirectURL, 0 );
}
 
function redirectLandmarkWithSession( sessionId, landmark, attributes, redirectURL ) {
  redirectLandmarkGeneralWithSession( getSessionId( sessionId ), landmark, attributes, redirectURL, 1 );
}

function redirectLandmarkNoHistoryWithSession( sessionId, landmark, attributes, redirectURL ) {
  redirectLandmarkGeneralWithSession( getSessionId( sessionId ), landmark, attributes, redirectURL, 0 );
}

