/********************************************************************
 * Post-Conversion Script 
 *
 * Copyright by Josef Witt GmbH, Weiden
 *
 * $Id: post_conversion.js 20152 2008-11-24 11:56:44Z stekop $
 ********************************************************************/

// ------------------------
// global definitions
// ------------------------
var GW_COOKIE_NAME = "gewinnungsweg";
var GW_COOKIE_EXPIRE_DAYS = 30;
var FIRST_COOKIE_WINS = true;

// -----------------------------------------------
// redirect to url and add gw parameter
// -----------------------------------------------
function redirectWithGw(url, gw) {			
	if (parametersInUrl() == true) {	
		insertposition = url.indexOf(PARAMETER_START) + PARAMETER_START.length;		
		addParameterStart = "";
	}
	else {
		insertposition = url.lastIndexOf("/") + 1;
		addParameterStart = "_/";
	}
	start = url.substring(0, insertposition);
	inserttext = addParameterStart + URL_PARAMETER_GW_NAME + "/" + gw + "/";
	end = url.substring(insertposition, url.length);
		
	url = start + inserttext + end;	
	window.location.href = url;
}



// ------------------------------------------------------------------------------------
// Executed when script is inserted in a page
// ------------------------------------------------------------------------------------

// if gw is in url
if (isGwParameterInUrl()) {	
	if (!(FIRST_COOKIE_WINS && userHasCookie(GW_COOKIE_NAME))) { 
		if (getGwParameter() != readCookieValue(GW_COOKIE_NAME)) {
			gw = getGwParameter();
			// overwrite cookie with new value
			if ( gw.length == 9 ) {
				writeCookie(GW_COOKIE_NAME, getGwParameter(), GW_COOKIE_EXPIRE_DAYS, "/");
				// DEBUG: alert("cookie has been written");
			}
		}
		else {
			// if there is a cookie with the same value do not overwrite the cookie
			// DEBUG: alert("cookie with same value alredy present (Value: " + readCookieValue(GW_COOKIE_NAME) +")");			 
		}
	}
	else {		
		// DEBUG: alert("cookie alredy present (Value: " + readCookieValue(GW_COOKIE_NAME) +")");		  
	}
}		

