//jas - this file contains code to create a shopper-specific cookie containing their specified store information (from the "MY CITY" box in the header)
//Note: this file is called on every page on the site

//grabs store info (store_info=store name) from URL querystring
if(document.URL.indexOf("receipt.asp")==-1)
{
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
	var store_var ="";
	if (qsParm['store_info']){
		store_var = qsParm['store_info'];
	}
}

//variables to store cookies globally
var store_cookie = "";
var city_cookie = "";

//retrieves cookie information from shopper's computer
function getCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{ begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;
}

//sets cookie preferences (used within makeCookie function)
function setCookie(NameOfCookie, value, expiredays)
{ var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

//deletes a cookie
function delCookie (NameOfCookie)
{ if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

//makes a cookie
function makeCookie(user_city) { 
        setCookie('usercity',user_city,365);
	toggle_city();
	document.getElementById("city_div").innerHTML=user_city;
	set_city();
	if(document.URL.indexOf("default.asp")!=-1 || document.URL.indexOf(".asp")==-1)
	{
		return false;
	}
	else
	{
		return(visit_store());
	}
}

//displays MY CITY dropdown, a red box with cities to choose (located in defaullt, Style1 of Site Manager)
function toggle_city()
{
	if (document.getElementById("headerbox").style.display=="block")
	{
		document.getElementById("headerbox").style.display="none";
		document.getElementById("city_button").src="assets/images/headercity.gif";
		
	}else{
		document.getElementById("headerbox").style.display="block";
		document.getElementById("city_button").src="assets/images/headercity_arrowdown.gif";
	}
	return false;
}

//random function
function getRandom(mi,ma){return (Math.round(Math.random()*(ma-mi)))+mi;}

//displays random city image in header
function drawRandom(){
	
	var imglist=new Array();
	var min=0;
	var max=0;
	
	imglist[0]="atlanta.jpg";
	imglist[1]="baltimore.jpg";
	imglist[2]="boston.jpg";
	imglist[3]="new_york.jpg";
	imglist[4]="philadelphia.jpg";
	imglist[5]="providence.jpg";
	imglist[6]="washington.jpg";
	
	max=imglist.length-1; //gets the maximum number
	
	
}

//sets city preference (from "MY CITY" dropdown in header) and stores in cookie - to be used throughout the site
function set_city()
{
	var usercity=getCookie('usercity');
	if (usercity!=null) 
	{
		
		var usercity_split = usercity.split(",");
		if (usercity_split.length>1)
		{
			city_cookie = usercity_split[0]
			store_cookie = usercity_split[1];
			document.getElementById("city_div").innerHTML=usercity_split[0];
			//fill_store_info(store_cookie,city_cookie);
			fill_store_info();
			//alert(store_cookie);
			
		}
		else
		{
			document.getElementById("city_div").innerHTML="Store Locator";
			drawRandom();
		}
	}
	else
	{
		document.getElementById("city_div").innerHTML="Store Locator";
		drawRandom();
	}
}

//fill home page red box info (right side) based on cookie
function fill_store_info()
{
	if (document.getElementById(store_cookie) && document.getElementById("storebox_div"))
	{
		if (store_cookie!="" && city_cookie!="")
		{
			var store_info = "";
			store_info="<a href=\"dept.asp?dept_id=103&store_info="+store_cookie+"&\" onclick=\"return(visitargs('dept.asp','dept_id=103&store_info="+store_cookie+"','URL'));\" class=storebox_links>"+store_cookie.split("_").join(" ")+" Store</a><br>";
			store_info+="<div id=store_information>"+document.getElementById(store_cookie).innerHTML+"</div>";
			store_info+="<a href=\"dept.asp?dept_id=103&store_info="+store_cookie+"&\" onclick=\"return(visitargs('dept.asp','dept_id=103&store_info="+store_cookie+"','URL'));\" class=storebox_links2>View a Map</a>";
			//store_info+="<a href=\"dept.asp?dept_id=103&store_info="+city_cookie+"&\" onclick=\"return(visitargs('dept.asp','dept_id=103&store_info="+city_cookie+"','URL'));\" class=storebox_links>All "+city_cookie.toLowerCase()+" Stores</a>";
			store_info+="<div style='padding-top:3px;'><a href=\"dept.asp?dept_id=103&\" onclick=\"return(visitargs('dept.asp','dept_id=103&','URL'));\" class=storebox_links>View All Store Locations</a></div>";
			store_info+="<div style='padding-top:3px;'><a href=\"dept.asp?dept_id=106&\" onclick=\"return(visitargs('dept.asp','dept_id=106&','URL'));\" class=storebox_links>Store Coupon</a></div>";
			
			document.getElementById("storebox_div").innerHTML=store_info;
		}
		else
		{
			document.getElementById("storebox_div").innerHTML="";
		}
	}
	//alert(store_cookie);
}

//visit store info page and show specific store (used on links in MY CITY dropdown -- in default, style1 of the Site Manager)
function visit_store()
{
	var store_link = "dept_id=103&store_info="+store_cookie;
	//alert(store_link);
	visitargs("dept.asp",store_link,"URL");
	return false;
}


//fill store_name div based on cookie -- this applies to the Find a Store page (dept 103)
/* I have changed the functions to hide and display the divs instead of recreating them when the link is clicked. I have also added the iframe/google maps portion */
function fill_about_store_info(){	// gets called on page load - to display the user's set location or go to default
	if (store_cookie!="" || store_var!=""){
		var store_name = "";
		if (store_var!=""){store_name=store_var}else{store_name=store_cookie}
		if(document.getElementById('about_' + store_name)){
			document.getElementById('about_' + store_name).style.display = "block";
			if(gmaps[store_name]){document.getElementById(store_name + "_iframe_div").innerHTML = gmaps[store_name];}
			currentInfoDiv = "about_" + store_name;
		}
	}else{
		currentInfoDiv = "about_main_image";
		document.getElementById('about_main_image').style.display = "block";
	}
return false;
}

//fill store_name div based on clicking link on page -- this applies to the Find a Store page (dept 103)
function link_store_info(store_name){	// gets called on link click 
	if (store_name!=""){
	if(currentInfoDiv != ""){document.getElementById(currentInfoDiv).style.display = "none";} // hide current div
	if(document.getElementById('about_' + store_name)){
		document.getElementById('about_' + store_name).style.display = "block";
		if(gmaps[store_name]){document.getElementById(store_name + "_iframe_div").innerHTML = gmaps[store_name];}
		currentInfoDiv = "about_" + store_name;
	}
 }
 else{
	currentInfoDiv = "about_main_image";
	document.getElementById('about_main_image').style.display = "block";
 }
 return false;
}