// Writes a cookie containing the kw querystring parameter using cookie reference "kw"
// If the url provided from Overture contains a goto URL then it will redirect to that page,
// If the url provided does not contain a goto URL then it will redirect to the default URL
function writeRedirect(defaultURL)
{
	// parse querystring into hashmap
	var qs  = document.location.search.substring(1)
	var qstr2
	qstr2 = ""
	var qsArray = qs.split("&")
	var qsMap = {};
	
	for (i= 0 ; i < qsArray.length; i++)
	{
		var param = qsArray[i];
		var key = param.substring(0, param.indexOf("="));
		var value = unescape(param.substring(param.indexOf("=")+1, param.length));
		qsMap[key] = value;	
	}
	
	var goto, orig, kw;

	if (qsMap["goto"])
	{
		qstr2 = qstr2 + "goto="+qsMap["goto"];
	}
	else
		qstr2 = qstr2 + "goto="+defaultURL;	
		
	if (qsMap["WT.srch"] == '1')
	{
		if (qsMap["DCSext.ppc_kw"])
		{
			qstr2 = qstr2 + "&kw="+qsMap["DCSext.ppc_kw"];
		}
		else
			if (qsMap["kw"])
				qstr2 = qstr2 + "&kw="+qsMap["kw"];
	}
	
	if (qsMap["WT.mc_id"])
	{
		qstr2 = qstr2 + "&orig="+qsMap["WT.mc_id"];
	}
	else
		if (qsMap["orig"])
		{
			qstr2 = qstr2 + "&orig="+qsMap["orig"];
		}
		else 
		{
			defaultMarketingCode="PPC";
			qstr2= qstr2 + "&orig="+defaultMarketingCode;
		}
	// initialize goto, apply custom redirect if it exists otherwise apply default

	
	// build redirect url
	//qstr2= escape(qstr2);

	//kw = escape(kw);
	
	var redirectURL

	redirectURL = "http://www.secure-res.com/rdx.asp?"+qstr2;
	
//document.write(redirectURL)
	if (kwckdisable != "0")
	{
	    if (qsMap["kw"])
		{
			createCookie("kw", qsMap["kw"], 7);
		}
		if (qsMap["DCSext.ppc_kw"])
		{
			
			createCookie("kw", qsMap["DCSext.ppc_kw"], 7);
		}
	}
	location.replace(redirectURL);
}

var secs
var timerID = null
var timerRunning = false
var delay = 3000

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 1
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        	
		//var defaultURL = "http://www.sheratonstudiocity.com/";
		
		// The default marketing code to use only if you don't provide a orig= parameter in the URL
		//var defaultMarketingCode = "PPC";
		
		writeRedirect(defaultURL);
    }
    else
    {
        //self.status = "You will be redirect in " & secs 
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

// Displays the kewyords stored in the keyword cookie if it exists
function displayCookieKeywords()
{
	document.write(readCookie("kw"));
}

// cookie functions: http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}
