iGiveInstalled=1;
igdebug=0;
var IE = (navigator.appName.indexOf("Microsoft") != -1);
var myWin;
var loction = "";
var referrer = "";
var windowreferrer = "";
function displaymall (loc, ref, winref)
{
	loction = loc;
	referrer = ref;
	windowreferrer = winref;
}
stores = new Array(
"1bookstreet",
"4sure",
"800",
"800giftcertificate",
"800-trekker",
"aaaballoons",
"aaafruitbaskets",
"aaronsbabybaskets",
"abtelectronics",
"allherb",
"amazon",
"americangreetings",
"areyougame",
"art",
"artselect",
"ashford",
"AshlaneChocolates",
"AshlaneGiftBaskets",
"Astrology",
"atyouroffice",
"autoaccessory",
"automotive",
"avon",
"babycenter",
"babystyle",
"bamm",
"bargainoutfitters",
"barnesandnoble",
"beoutdoors",
"beyond",
"bigdogs",
"bisoubisou",
"bisou-bisou",
"bn",
"bookcloseouts",
"booksamillion",
"booksforcooks",
"borders",
"Bose",
"brainstorms",
"britannica",
"brooksbrothers",
"brylanehome",
"bugleboy",
"burpee",
"buy",
"buyitontheweb",
"carparts",
"CartoonBank",
"caselogic",
"cattoys",
"cdnow",
"cheapesthotels",
"chefscatalog",
"chiasso",
"chocolatesource",
"classroomdirect",
"CloudWalkers",
"collectiblestoday",
"ColoradoPen",
"computers4sure",
"cooking",
"crucialtechnology",
"customgolf",
"Cybiko",
"dell",
"dell4me",
"DexterShoe",
"DexterShoes",
"disneystore",
"dogtoys",
"drugstore",
"duncraft",
"DunhamsSports",
"ebags",
"EconomyTravel",
"eddiebauer",
"EddieBauerKids",
"EddieBauerOutlet",
"edelights",
"ehobbies",
"eJewelry",
"eLens",
"Elisabeth",
"emerchandise",
"esprit",
"esticker",
"etera",
"ethicalshopper",
"eToys",
"everythingwireless",
"flooz",
"flower",
"fogdog",
"fossil",
"fragrancedirect",
"fragrancenet",
"franklincovey",
"franklinmint",
"frenchtoast",
"furniturefind",
"gadgetuniverse",
"games2learn",
"gap",
"GarnetHill",
"gear",
"gemplers",
"gendarme",
"giftbaskets",
"giftcertificates",
"gifttree",
"GoldViolin",
"gourmetgiftmail",
"greatflowers",
"greenmarketplace",
"guideoutdoors",
"guidinglightvideo",
"Half",
"hallmark",
"hallmarkflowers",
"hammacherschlemmer",
"HandSpring",
"HarryandDavid",
"health4her",
"hearthsong",
"hersheygifts",
"hickoryfarms",
"hifi",
"HotWire",
"ibeauty",
"ibm",
"igo",
"igogolf",
"illuminations",
"immunesupport",
"iparty",
"jcpenney",
"jcrew",
"jcwhitney",
"jewelryspotlight",
"JJill",
"JosABank",
"landscapeusa",
"landsend",
"learnandplay",
"lensexpress",
"levillage",
"Lionel",
"lucy",
"luxuryfinder",
"magazineofthemonth",
"magazineoutlet",
"magellans",
"maidenform",
"maps",
"marthastewart",
"MCSports",
"momastore",
"mp3",
"mvp",
"nationalbusinessfurniture",
"neocomputers",
"netoy",
"nextmonet",
"nextplanetover",
"NickelodeonMagazines",
"nirvanachocolates",
"nordstrom",
"officefurniture",
"officemax",
"OldNavy",
"omahasteaks",
"onlineofficesupplies",
"orvis",
"oshkoshbgosh",
"Oshmans",
"outdoordecor",
"outpost",
"PacificSunwear",
"panache",
"patagonia",
"paulfredrick",
"pcconnection",
"perfumania",
"personalize",
"PetFoodDirect",
"PetMedExpress",
"petsmart",
"phonecube",
"phonicsdirect",
"photoalley",
"powells",
"proflowers",
"recordedbooks",
"rei",
"reioutlet",
"rei-outlet",
"Riddell",
"scholarsbookshelf",
"shades",
"shaperimage",
"shopmattlel",
"shopsports",
"smarterkids",
"smashbox",
"sparks",
"spawish",
"spencergifts",
"spiegel",
"SpilsburyPuzzles",
"sportsmansguide",
"stacksandstacks",
"stressless",
"swissarmy",
"tavolo",
"territoryahead",
"thecompanystore",
"thousandcranestea",
"tiemaster",
"totallyfuntoys",
"Tower",
"ToysRUs",
"travelnow",
"tshipley",
"tshirts",
"t-shirts",
"tsisoccer",
"tutorials",
"ubid",
"ultimateoutlet",
"UnCommonGoods",
"underneath",
"usaopoly",
"WalMart",
"warehouse",
"wearguard",
"webehave",
"webehavebabies",
"WebRX",
"weegear",
"WestMarine",
"wickers",
"Wine",
"Woolrich",
"worldofscience",
"yakpak",
"ZanyBrainy",
"zones"
);
vendorID = new Array(
"706",
"208",
"173",
"97",
"257",
"356",
"371",
"372",
"192",
"404",
"259",
"48",
"412",
"10",
"403",
"349",
"716",
"218",
"852",
"11",
"108",
"284",
"38",
"151",
"320",
"319",
"771",
"65",
"420",
"17",
"449",
"193",
"193",
"65",
"228",
"319",
"194",
"719",
"891",
"254",
"402",
"246",
"248",
"370",
"188",
"466",
"374",
"285",
"821",
"715",
"128",
"9",
"175",
"324",
"766",
"429",
"736",
"840",
"386",
"839",
"208",
"146",
"186",
"713",
"887",
"134",
"134",
"908",
"908",
"298",
"27",
"129",
"767",
"914",
"182",
"822",
"692",
"814",
"815",
"317",
"301",
"921",
"909",
"910",
"283",
"131",
"446",
"545",
"267",
"12",
"282",
"260",
"489",
"15",
"290",
"773",
"41",
"314",
"293",
"135",
"168",
"735",
"230",
"304",
"810",
"419",
"462",
"220",
"648",
"275",
"373",
"837",
"117",
"26",
"106",
"754",
"244",
"907",
"295",
"318",
"165",
"873",
"859",
"198",
"223",
"400",
"185",
"252",
"870",
"310",
"416",
"167",
"137",
"288",
"414",
"721",
"147",
"40",
"375",
"219",
"816",
"825",
"35",
"240",
"311",
"399",
"457",
"820",
"448",
"428",
"450",
"183",
"197",
"417",
"199",
"461",
"913",
"206",
"322",
"406",
"768",
"200",
"309",
"247",
"184",
"883",
"427",
"644",
"456",
"718",
"895",
"62",
"111",
"46",
"297",
"882",
"376",
"60",
"826",
"756",
"413",
"96",
"213",
"636",
"24",
"888",
"889",
"242",
"709",
"385",
"369",
"221",
"211",
"302",
"224",
"225",
"225",
"922",
"202",
"140",
"141",
"138",
"33",
"105",
"214",
"130",
"203",
"401",
"351",
"827",
"772",
"231",
"159",
"229",
"25",
"507",
"418",
"357",
"201",
"116",
"899",
"864",
"241",
"737",
"730",
"730",
"190",
"191",
"289",
"421",
"923",
"171",
"251",
"835",
"209",
"270",
"118",
"769",
"890",
"447",
"824",
"215",
"897",
"819",
"204",
"313",
"877",
"210"
);
alternates = new Array(
"victoriassecret",
"cabelas",
"cdw"
);
	if (IE) {var ig_state_value = getState(), timeout_Ref;}
	else	{var ig_state_value = loction}
	var host_name_ = window.location.hostname;
	host_name_.toLowerCase();	//alert("Referrer = " + document.referrer + "\nHost = " + host_name_ + "\nState = " + ig_state_value);
	if (parent.frames.length > 0)
	{
		if (igdebug == 1) {alert("Self = " + self.location + "\nReferrer = " + document.referrer + "\nTop = '" + top.location + "'\nFirstFrame = " + parent.frames[0].location + "\nFrameLen = " + parent.frames.length + "\nHost = " + host_name_ + "\nState = " + ig_state_value);}
	}
	else
	{
		if (igdebug == 1) {alert("Self = " + self.location + "\nReferrer = " + document.referrer + "\nTop = " + top.location + "\nFirstFrame = <no frames here>" + "\nFrameLen = " + parent.frames.length + "\nHost = " + host_name_ + "\nState = " + ig_state_value);}
	}
	//NOTE:  To prevent popup from a popup, exclude the domain where popup comes from
	if (IE)
	{
		if ((ig_state_value.indexOf("bfast.com") != -1) || (ig_state_value.indexOf("linksynergy.com") != -1) || (ig_state_value.indexOf("cc-dt.com") != -1) || (ig_state_value.indexOf("interferenza.net") != -1)  || (ig_state_value.indexOf("commission-junction.com") != -1))
		{
			setState(self.location);
			if (igdebug == 1) {alert("Self = " + self.location + "\nNew State = " + ig_state_value);}
		}
		else if ((ig_state_value.indexOf("&x=") != -1) )
		{
			if (igdebug == 1) {alert("Parsed State = " + ig_state_value.substring(ig_state_value.indexOf("&x=") + 3, ig_state_value.length));}
			setState(ig_state_value.substring(ig_state_value.indexOf("&x=") + 3, ig_state_value.length));
			if (igdebug == 1) {alert("Self = " + self.location + "\nNew State = " + ig_state_value);}
			ig_state_value = getState()
			if (igdebug == 1) {alert("Self = " + self.location + "\nFinal State = " + ig_state_value);}
		}
	}
	if ((host_name_.indexOf("igive.com") == -1) && (ig_state_value.indexOf("bfast.com") == -1) && (ig_state_value.indexOf("linksynergy.com") == -1) && (ig_state_value.indexOf("cc-dt.com") == -1) && (ig_state_value.indexOf("interferenza.net") == -1)  && (ig_state_value.indexOf("commission-junction.com") == -1) && (ig_state_value.indexOf("flycast.com") == -1)) 
	{
		if ( (document.referrer == "")  ||  ((parent.frames.length > 0) && (self.location == parent.frames[0].location) &&  (ParseDomainName(document.referrer) == ParseDomainName(self.location) ) ) )
		{
			if (igdebug == 1) {alert("State " + ParseDomainName(ig_state_value) + " == Host " + ParseDomainName(host_name_));}
			// compare current domain with the one set from popup.  If they are same, don't popup again		
			if (ParseDomainName(ig_state_value) != ParseDomainName(host_name_))
			{
				if (IE)
				{
					doPopUp();
				}
				else
				{
					var currentdate = new Date();
					var newsecs = currentdate.getTime()
					var cookietime = igGet_cookie('igive');
					if ((newsecs - cookietime) > 30000)
					{
						doPopUp();
					}
				}
			}
		}
	}
	function doPopUp()
	{
		//put doPopUp code inline with function
		var winName, url, res;
		var found = 0;
		var loc = 0;
		var variable = ParseDomainName(host_name_);
		var url_parts = variable.split( "." );
		variable = url_parts[0].toLowerCase();
		for (x = 0; x < stores.length; x ++)
		{
			mallStore = stores[x].toLowerCase()
			if (variable == mallStore)
			{
				found = 1;
				loc  = x;
				x = stores.length;
			}
		}
		if (found == 1)
		{
			if ((myWin == null) || (myWin.closed = true))
			{
				if (igdebug == 1) {alert("Found!\nvendor_id = " + vendorID[loc] );}
				if (IE)
				{
					setState("");
				}
				url = "http://v5.igive.com/popup.cfm?redirect="+vendorID[loc]+"&name="+stores[loc]
				myWin = window.open (url, winName, "location=no,buttons=no,toolbar=no,resizable=yes,scrollbars=yes,width=484,height=330");		
				myWin.focus();			
				if (IE)
				{
					// set an event to check for popup response
					timeout_Ref = setInterval('redirectURL()', 1000);
					if (igdebug == 1) {alert ("timeout_Ref =" + timeout_Ref);}
				}
				else
				{
					//set cookie to figure out how long it's been since we popped
					var currentdate = new Date();
					var currentsecs = currentdate.getTime()
					var cookieDomain = "." + ParseDomainName(host_name_);
					document.cookie="igive="+currentsecs+";domain="+cookieDomain;
				}
			}
		}
		else
		{
			if (igdebug == 1) {alert("Not Found!" );}
			if (IE)
			{
				setState(self.location);
			}
 /*   Not yet implemented ---------------------------------------------------- 
			for (y = 0; y < alternates.length; y ++)
			{
				altstore = alternates[y].toLowerCase()
//				if(variable.indexOf(altstore) != -1)
				if(variable == altstore)
				{
					found = 2;
					loc = y;
					y = alternates.length;
				}		
			}
			
			if (found ==2)
			{
				if (IE)
				{
					setState("");
				}
				url = "http://v5.igive.com/popup.cfm?alternate="+alternates[loc]
				myWin = window.open (url, winName, "location=no,buttons=no,toolbar=no,resizable=yes,scrollbars=yes,width=484,height=330");		
				myWin.focus();
				if (IE)
				{
					// set an event to check for popup response
					timeout_Ref = setInterval('redirectURL()', 1000);	
				}
			}
      Not yet implemented ---------------------------------------------------- */
		}
	}
	function redirectURL ()
	{
		var redirect_url = "";
		redirect_url = getState()
		if (igdebug == 1) {alert("redirectURL \ntimeout_Ref = " + timeout_Ref + "\nredirect_url = " + redirect_url + "\nPopup = " + myWin.closed);}
		// need to search of http:// because it was going to invalid URL at startup
		if (redirect_url.indexOf("http://") != -1)
		{
			if (igdebug == 1) {alert("Clearing Interval " + timeout_Ref +" and redirecting" );}
			window.clearInterval (timeout_Ref);
			if (parent.frames.length > 0) 
			{
				parent.location.href = redirect_url;
   			}
			else
			{
				window.location.href = redirect_url;
			}
		}
		else if (myWin.closed == true)
		{
			if (igdebug == 1) {alert("Clearing Interval " + timeout_Ref);}
			window.clearInterval (timeout_Ref);
		}

	}
	function ParseDomainName (url)
	{
		var temp = new String(url);
		var url_parts = temp.split( "/" );
		var domain_name;
		var ar_domain, temp, i;
		var all_domains;
		all_domains = ".co.com.org.net";
		if (url_parts.length >= 2)
		{
			host_name = url_parts[2].toLowerCase();
		}
		else
		{
			host_name = url;
		}

		ar_domain = host_name.split(".");
		domain_name = ar_domain[ar_domain.length - 1];

		if (ar_domain.length == 2)
		{
			domain_name = host_name;
			return domain_name;
		}

		for (i = (ar_domain.length - 2); i > 0; i--)
		{
			temp = "." + ar_domain[i];
				
			if (all_domains.indexOf(temp) != -1)
			{
				domain_name = ar_domain[i] + "." + domain_name;
			}
			else
			{
				domain_name = ar_domain[i] + "." + domain_name;
				return domain_name;
			}
		}
		return domain_name;
	}

	function igGet_cookie(Name) 
	{
  		var search = Name + "="
  		var returnvalue = "";
 	 	if (document.cookie.length > 0) 
	 	{
    			offset = document.cookie.indexOf(search)
   			if (offset != -1) 
			{ 
      				offset += search.length
     				end = document.cookie.indexOf(";", offset);
      				if (end == -1)
		         			end = document.cookie.length;
      				returnvalue=unescape(document.cookie.substring(offset, end))
      			}
   		}
  		return returnvalue;
	}
