
function getExpiryInfo(ctr)
{
	if(ctr == undefined)
		ctr = 0;
		
	//examine cookies to get current item list
	ticks_cookie = getSiteCookie("crtitms");//get the cartitems cookie
	if(ticks_cookie.length > 0)
	{
		displayTicketTimer(ticks_cookie,ctr);
		setTimeout("getExpiryInfo('"+(++ctr)+"')", 1000);//run every second (1000 milliseconds)
	}
}

function displayTicketTimer(cookie,ctr)
{
	tickets = cookie.split('~');
	tickets.pop();//remove the last item which is empty due to the trailing separator
	
	if(tickets.length > 0)
	{
		//so, for each ticket in the cart.....
		for(var i=0;i<tickets.length;i++)
		{
			//now take the individual ticket and split it into its values
			tktInfo = tickets[i].split(',');//this should give us the correct amount of values
			
			if(tktInfo.length == 4)
			{
				//VALUE LIST////////////////////////////////////////////
				//0-expiry, 1-tktId, 2-showId, 3-canRenew
				////////////////////////////////////////////////////////
				//assign vars
				var c_expiry = tktInfo[0];
				c_tktId = tktInfo[1];
				c_showId = tktInfo[2];
				c_canRenew = tktInfo[3];
				
				//WALLA
				var divId = "tmr" + ((c_showId=='mrc') ? 'mrc' : '') + c_tktId;
				division = document.getElementById(divId);
				
				if(division != undefined)
				{
					//do calcs
					diff = calculateValues(c_expiry);
					division.innerHTML = getDivText(diff,c_tktId,c_showId,c_canRenew,ctr);
				}
			}
		}
	}
}

function calculateValues(expiry)
{
    //if we are given the date as universal time
    //then can we compare that time to a universal now time?
    
    //expiry is server expiry time
    //we convert that utc time to a local server time to make the diff work
    local_expiry = new Date(expiry);//expiry represents utc but is in local format - MDT
    
    //do the same thing with now - convert it to utc then local
    nw = new Date().toUTCString();
    nwn = nw.toString().replace(/UTC/i,"");
    
    local_now = new Date(nwn);
    
    //get the millisecond representations
    ticks_exp = Date.parse(local_expiry);
    ticks_now = Date.parse(local_now);
    
    //compute difference
    dif = ticks_exp - ticks_now;
    
    return dif;
}


function getDivText(diff,tktId,showId,canRenew,ctr)
{
	var divText = "error";
	
	//change link needs to be shown/hidden
	var pkpLink = document.getElementById("Pkp" + tktId);//dont worry about merch here
	
	//also hide/show the add more time link
	var idString = "TmrFnc" + ((showId=='mrc') ? 'mrc' : '') + tktId;
	var addTime = document.getElementById(idString);
	
						
	if(diff.valueOf() >= 0)
	{
		if(pkpLink != undefined) 
			pkpLink.style.display = "inline";
		
		if(addTime != undefined && canRenew) 
		{
			addTime.style.display = "inline";
			var even = ctr%2;
			if(diff<=90000)
			{
				var even = ctr%2;
				addTime.className = ((even) ? "flashOn" : "flashOff");
			}
		}
		
		return displayClock(diff);
	}
	
	if(pkpLink != undefined) 
		pkpLink.style.display = "none";
	if(addTime != undefined) 
		addTime.style.display = "none";
		
	return displayExpired(ctr,tktId,showId);
}

function displayClock(diff)
{
	//display clock in div
	var allSecs = Math.floor(diff.valueOf()/1000);
	
	var mins = Math.floor(allSecs/60);
	var secs = allSecs - (mins*60);
	
	return "item reserved for <div class='Clock_wonb'>" + ((mins<10) ? "0" : "") + mins + "m:" + ((secs<10) ? "0" : "") + secs + "s</div>";
}


function showRemovals()
{
	//get the cookie written by Cart_Main
	var  c_rem = getSiteCookie("remvd");

	if(c_rem.length > 0)
	{
		var retStr = "The folowing items have expired and have been removed from your shopping cart:\r\n\r\n";
		retStr += c_rem;
		alert(retStr);
	}
}

//makes classes:
//	ItmExpired
//	flashon/flashoff
function displayExpired(ctr,tktId,showId)//showId can also be "merch" to show context
{
	var even = ctr%2;
	var retStr = "<span class='ItmExpired red " + ((even) ? "flashOn" : "flashOff") + "'>these items have expired in your cart!</span>";

	if(showId == 'mrc') {
		retStr += "<a class='red' href='/Store/ChooseMerch.aspx?mite=" + tktId + "&byp=1'>check availability</a>";

	}
	else { 
		retStr += "<a class='red' href='/Store/ChooseTicket.aspx?sid=" + showId + "&byp=1'>check availability</a>";

	}
	
	return retStr;
}


function getSiteCookie(cookieName)
{
	if (document.cookie.length>0)
	{
		if(cookieName == undefined)
		{
			return unescape(document.cookie);
		}
		else 
		{
			c_name = cookieName;//this is our site cookie
			c_start=document.cookie.indexOf(c_name + "=")
			
			if (c_start!=-1)
			{ 
				c_start=c_start + c_name.length+1;//allow for equal sign
				
				//check for other cookies within the cookie
				//if not found then use the end of this cookie
				//if not found then use the end of the entire cookie
				c_end=document.cookie.indexOf("&",c_start);
				if (c_end == -1)
					c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) 
					c_end=document.cookie.length;
					
				var retVal = unescape(document.cookie.substring(c_start,c_end));
					
				return retVal;
			}
		}
	}
	
	return "";
}