/**
 * Author Jonathan Samples
 * August 21, 2008
 * 
 * CouponUI Class 
 * Manage the display and interaction with the User for offers
 */
 
 /**
  * CouponUI constructor
  */
 function CouponUI(modelObj, loyaltyCardUI, displayEleId, pageEleId, windowManager, showRemoveIcon){
 	this.model = modelObj;
 	this.lcui = loyaltyCardUI;
 	this.displayEleId = displayEleId;
 	this.pageEleId = document.getElementById(pageEleId)?pageEleId:false; // if the element does not exist then we set it to false.
 	this.windowManager = windowManager;
 	this.mercDisplayList = new Array();
 	this.currentPage = 1;
 	this.totalPages = 1;
	this.showPrintOption = false;
	this.showRemoveIcon = showRemoveIcon;
	this.featuredOffers = false;
	this.homePage = false;
	this.merchantPage = false;
	
	this.floatIndex = 0;
	this.floatArray = new Array("left", "right", "left", "right", "left", "right");
	
	this.generateMercDisplayList();
	
	// if window manager doesn't exist create one
	if(!this.windowManager) this.windowManager = new WindowManager();
 	
 	cui = this;
 	if(this.model){
	  	this.model.registerEventListener("DisplayListChange", this);
	  	this.model.registerEventListener("CouponSaveFailed", this);
	  	this.model.registerEventListener("CouponChange", this);
	  	this.model.registerEventListener("CouponRemovedFromDisplay", this);
	  	this.model.registerEventListener("LoyaltyCardChange", this);
 	}
 }
 
 /**
  * CouponUI Init function should be called on document load
  */
  CouponUI.prototype.onDisplayListChange = function(){
  	if (!this.featuredOffers)
  	{
			this.generateMercDisplayList();
			this.currentPage = 1;
			this.drawPagination();
		}	
  	this.initCouponDisplay();
  	this.windowManager.closeOpenWindow();
  	//window.scrollTo(0,100);
  }
  
  CouponUI.prototype.onLoyaltyCardChange = function(){
  	this.initCouponDisplay();
  }
  
   CouponUI.prototype.drawPagination = function(){
  		var pageArea = document.getElementById(this.pageEleId);
  		if(!pageArea) return;
  		
  		var count = 2;
  		while(pageArea){
	  		removeChildElements(pageArea);
	  		
	  		var prevLink = document.createElement("div");
	  		prevLink.appendChild(document.createTextNode("<< Previous"))
	  		if(this.currentPage != 1){ 
	  			prevLink.className = "couponPageLink_enabled"; 
	  			prevLink.onclick = new Function("cui.setPage("+(this.currentPage-1)+")");
	  		}
	  		else prevLink.className = "couponPageLink_disabled";
	  		pageArea.appendChild(prevLink);
	  		
	  		for(var i = 1; i <= this.totalPages; i++){
	  			var pageLink = document.createElement("div");
	  			pageLink.appendChild(document.createTextNode(i))
	  			pageLink.className = "couponPageLink_enabled";
	  			if(i == this.currentPage)  pageLink.className = "couponPageLink_active";
	  			pageLink.onclick = new Function("cui.setPage("+(i)+")");
	  			pageArea.appendChild(pageLink);
	  		}
	  		
	  		var nextLink = document.createElement("div");
	  		nextLink.appendChild(document.createTextNode("Next >>"))
	  		
	  		if(this.currentPage != this.totalPages){ 
	  			nextLink.className = "couponPageLink_enabled"; 
	  			nextLink.onclick = new Function("cui.setPage("+(this.currentPage+1)+")");
	  		}
	  		else nextLink.className = "couponPageLink_disabled";
	  		pageArea.appendChild(nextLink);
	  		
	  		var clear = document.createElement("div");
	  		clear.className = "clear";
	  		pageArea.appendChild(clear);
	  		
	  		pageArea = document.getElementById(this.pageEleId+"_"+count);
	  		count++;
  		}
  }
  
  CouponUI.prototype.initCouponDisplay = function(){
  		var displayArea = document.getElementById(this.displayEleId);
  		if(!this.model.couponIdDisplayList) return;
  		if (!this.featuredOffers)
  		{
  			removeChildElements(displayArea);
  		}	
  		for(var mercId in this.model.merchants){
  			this.model.merchants[mercId].initialized = false;
  		}
  		if(this.model.couponIdDisplayList.length && this.model.couponIdDisplayList.length > 0){
		  	for(var i = 0; i < this.model.couponIdDisplayList.length; i++){
		  		var coupon = this.model.coupons[this.model.couponIdDisplayList[i]];
		  		
		  		if(!coupon || coupon.working){ continue; }
		  		var merchant = this.model.merchants[coupon.merchantId];
		  		
		  		if (!this.featuredOffers)
		  		{
			  		// Skip the ones that are not on this page.
						if(this.mercDisplayList[coupon.merchantId] != this.currentPage) continue;
						if(!merchant.initialized){
							this.drawMerchant(coupon.merchantId);
							merchant.initialized = true;
						}
					}		  		
		  		this.drawCoupon(coupon.couponId);
		  	}
		  	
		  	if(!this.featuredOffers && !this.homePage && !this.model.inIframe){
			  	var toTop = document.createElement("div");
			  	toTop.style.width = "100%";
			  	toTop.style.textAlign = "right";
			  	toTop.style.margin = "10px 0px";
			  	
			  	var topLink = document.createElement("a");
			  	topLink.href = "#"+displayArea.id;
			  	topLink.appendChild(document.createTextNode("Back to top"));
			  	toTop.appendChild(topLink);
			  	displayArea.appendChild(toTop);
		  	}
  		}
  		else{
  			if(this.showRemoveIcon)
  				this.drawNoSavedCouponError();
  			else
  	  			this.drawNoCouponError();
  			
  		}
  }
  
  CouponUI.prototype.drawNoCouponError = function(){
  		var displayArea = document.getElementById(this.displayEleId);
  		if(!displayArea) return;
  		
  		var header = document.createElement('div');
  		
  		if (this.model.inMMF)
  		{
				header.style.padding = "2px 0px";
	  		var merchant = this.model.merchants[this.model.iframeMid];
				var floatLeft = document.createElement("div");
				floatLeft.style.cssFloat = "left";
				floatLeft.style.styleFloat = "left";
				floatLeft.style.width = "300px";
				floatLeft.style.textAlign = "left";

				header.appendChild(floatLeft);
		
				if(merchant.logoId && merchant.logoId != "-1")
				{
					var logo = document.createElement("img");
					logo.src = "/images/"+merchant.logoId+".jpg";
					logo.style.width = "300px";
					logo.style.height = "30px";
					floatLeft.appendChild(logo);

				}
				else
				{
					var titleText = document.createElement("div");
					titleText.style.fontSize = "16px";
					titleText.style.fontWeight = "bold";
					titleText.style.padding = "10px 0px 0px 0px";
					titleText.appendChild(document.createTextNode(merchant.name));
					floatLeft.appendChild(titleText);
				}
	  		this.addMMFTitleRight(header);
  		}
  		else
  		{
				header.style.fontSize = "16px";
				header.style.fontWeight = "bold";
				header.style.padding = "3px 0px";
				header.appendChild(document.createTextNode("We're sorry,"));
			}	
  		
  		var body = document.createElement('div');
  		body.style.fontSize = "14px";
  		body.style.padding = "5px";
  		
  		var message = "There are no more offers available in your area. Please try searching in another location.";
  		var footer = false;
  		if (this.model.inIframe)
  		{
  			footer = document.createElement('div');
  			if (this.model.inMMF)
  			{
					body.style.textAlign = "center";
  				message = "There are no more coupons available at this time. New coupons appear every other Tuesday. Please check back soon.";
  			}
  			else
  			{
  				message = "There are no more offers available at this time. Please check back soon.";
  			}	
  		}	
  		

  		body.appendChild(document.createTextNode(message));
  		
  		var hfbox = applyHeaderFooterBox(header, body, footer);
  		
  		displayArea.appendChild(hfbox);
  }
  
  CouponUI.prototype.addMMFTitleRight = function(titleDiv)
  {
		var floatRightWidth = "300px";
		var floatRight = document.createElement("div");
		floatRight.id = "mm_login";
		floatRight.style.cssFloat = "right";
		floatRight.style.styleFloat = "right";
		floatRight.style.textAlign = "right";
		floatRight.style.width = floatRightWidth;
		floatRight.style.margin = "5px 0px";
		titleDiv.appendChild(floatRight);

		var clear = document.createElement("div");
		clear.className = "clear";
		titleDiv.appendChild(clear);

		if (this.model.user.authenticated)
		{
			var logOut = document.createElement("a");
			logOut.appendChild(document.createTextNode("Log Out"));
			logOut.href = "javascript: cui.goToCoreSelf('lo=')";
			floatRight.appendChild(logOut);
			floatRight.appendChild(document.createTextNode(" " + this.getFormattedPhone(this.model.user.phoneNumber)));
		}
		else
		{
			var logIn = document.createElement("a");
			logIn.appendChild(document.createTextNode("Log In"));
			logIn.href = "javascript:wm.setLastAnchor('mm_login');cm.showLoginWindow('mm_login', '" + this.getCoreSelf() + "')";
			floatRight.appendChild(logIn);
		}
		floatRight.appendChild(document.createTextNode(" | "));

		var goFaq = document.createElement("a");
		goFaq.appendChild(document.createTextNode("FAQ"));
		goFaq.href = "javascript: cui.goToCoreSelf('faq=1')";
		floatRight.appendChild(goFaq);

		floatRight.appendChild(document.createTextNode(" | "));

		var goCont = document.createElement("a");
		goCont.appendChild(document.createTextNode("Contact Us"));
		goCont.href = "javascript: cui.goToCoreSelf('faq=c')";
		floatRight.appendChild(goCont);
	}  
  
  CouponUI.prototype.drawNoSavedCouponError = function(){
  		var displayArea = document.getElementById(this.displayEleId);
  		if(!displayArea) return;
  		
  		var header = document.createElement('div');
  		header.style.fontSize = "16px";
  		header.style.fontWeight = "bold";
  		header.style.padding = "3px 0px";
  		header.appendChild(document.createTextNode("No saved offers..."));
  		
  		var body = document.createElement('div');
  		body.style.fontSize = "14px";
  		body.style.padding = "5px";
  		body.innerHTML = "You haven't saved any offers yet.  To start saving <a href=\"/deals.php\">click here</a>";
  		
  		var hfbox = applyHeaderFooterBox(header, body);
  		
  		displayArea.appendChild(hfbox);
  }
  
  CouponUI.prototype.drawMerchant = function(merchantId){
  		var merchant = this.model.merchants[merchantId];
  		var displayArea = document.getElementById(this.displayEleId);
  		var merchantHolder = document.getElementById("mh_"+merchantId);
  		var card = this.model.loyaltyCards[merchantId];

		if(!merchantHolder)
		{
			merchantHolder = document.createElement("div");
			merchantHolder.style.margin = "0px 0px 10px 0px";
			displayArea.appendChild(merchantHolder);
			merchantHolder.id = "mh_"+merchantId;
			merchantHolder.className = "solid_top_gray";
			if (this.homePage && !merchant.isCPG)
			{
				merchantHolder.style.width = "49%";
				merchantHolder.style.cssFloat = this.floatArray[this.floatIndex];
				merchantHolder.style.styleFloat = this.floatArray[this.floatIndex];
				this.floatIndex++;
			}	
  	}
  		
  	removeChildElements(merchantHolder);
		
		var merchantContent = document.createElement("div");
		var merchantTitle = document.createElement("div");
		merchantTitle.style.padding = "2px 0px";
		
		merchantContent.id = "mc_"+merchantId;	
		
		var floatLeft = document.createElement("div");
		floatLeft.style.cssFloat = "left";
		floatLeft.style.styleFloat = "left";
		floatLeft.style.width = "300px";
		floatLeft.style.textAlign = "left";
		
		//merchantTitle.style.width = "100%";
		merchantTitle.appendChild(floatLeft);
		
		if(merchant.logoId && merchant.logoId != "-1"){
			var logo = document.createElement("img");
			logo.src = "/images/"+merchant.logoId+".jpg";
			logo.style.width = "300px";
			logo.style.height = "30px";
			if (!this.model.inMMF)
			{
				logo.onclick = new Function("window.location='merchant.php?merchant="+merchantId+"';");
				logo.style.cursor = "pointer";
			}	
			floatLeft.appendChild(logo);
			
		}
		else{
			var titleText = document.createElement("div");
			titleText.style.fontSize = "16px";
			titleText.style.fontWeight = "bold";
			titleText.style.padding = "10px 0px 0px 0px";
			if (!this.model.inMMF)
			{
				titleText.onclick = new Function("window.location='merchant.php?merchant="+merchantId+"';");
				titleText.style.cursor = "pointer";
			}
			titleText.appendChild(document.createTextNode(merchant.name));
  			floatLeft.appendChild(titleText);
		}

		var floatRightWidth = "300px";
		if (this.homePage)
		{
			floatRightWidth = "30px";
		}	
		else if (this.model.inMMF)
		{
			floatRightWidth = "250px";
			if (this.model.mmfWidth == 7)
			{
				floatRightWidth = "200px";
			}	
		}	
		
		var floatRight = document.createElement("div");
		floatRight.style.cssFloat = "right";
		floatRight.style.styleFloat = "right";
		floatRight.style.textAlign = "right";
		floatRight.style.width = floatRightWidth;
		floatRight.style.margin = "5px 0px";
		merchantTitle.appendChild(floatRight);

		var clear = document.createElement("div");
		clear.className = "clear";
		merchantTitle.appendChild(clear);
		
		if (this.model.inMMF)
		{
			if (this.model.user.authenticated)
			{
				var logOut = document.createElement("a");
				logOut.href = "javascript: cui.goToCoreSelf('lo=')";
				if (this.model.mmfWidth == 7)
				{
					logOut.appendChild(document.createTextNode(this.getFormattedPhone(this.model.user.phoneNumber)));
				}
				else
				{
					logOut.appendChild(document.createTextNode("Log Out"));
					floatRight.appendChild(document.createTextNode(this.getFormattedPhone(this.model.user.phoneNumber) + " "));
				}	
				floatRight.appendChild(logOut);
			}
			else
			{
				var logIn = document.createElement("a");
				logIn.appendChild(document.createTextNode("Log In"));
				logIn.id = "mm_login";
				logIn.href = "javascript:wm.setLastAnchor('mm_login');cm.showLoginWindow('mm_login', '" + this.getCoreSelf() + "')";
				floatRight.appendChild(logIn);
			}
			floatRight.appendChild(document.createTextNode(" | "));

			var goFaq = document.createElement("a");
			goFaq.appendChild(document.createTextNode("FAQ"));
			goFaq.href = "javascript: cui.goToCoreSelf('faq=1')";
			floatRight.appendChild(goFaq);

			floatRight.appendChild(document.createTextNode(" | "));
			
			var goCont = document.createElement("a");
			goCont.appendChild(document.createTextNode("Contact Us"));
			goCont.href = "javascript: cui.goToCoreSelf('faq=c')";
			floatRight.appendChild(goCont);
		}
		else if (!this.homePage)
		{
		
			if (this.showPrintOption && merchant.isCPG && this.model.loyaltyCards[merchantId].status == 2)
			{
				var printButton = document.createElement("img");
				printButton.align = "absmiddle";
				printButton.alt = "Print Shopping List";
				printButton.title = "Print Shopping List";
				printButton.style.margin = "0px 0px 0px 6px";
				printButton.alt = "Print Shopping List";
				printButton.style.cursor = "pointer";
				printButton.src = "/includes/templates/master/images/round_corners/button_print.png";
				printButton.onclick = new Function("var x=window.open(\"/shopping_list.php?mId="+merchant.mid+"\", \"listWindow\", \"menubar=0,status=0,toolbar=0,location=0,height=800,width=800,scrollbars=1\");");
				floatRight.appendChild(printButton);
			}			

			// If we have a location already
			if(!this.merchantPage && this.model.user.currentSearch && this.model.user.currentSearch.length > 0){
				var locButton = document.createElement("img");
				locButton.align = "absmiddle";
				locButton.style.margin = "0px 0px 0px 6px";
				locButton.alt = "See map of store locations";
				locButton.title = "See map of store locations";
				locButton.alt = "Locations";
				locButton.style.cursor = "pointer";
				locButton.src = "/includes/templates/master/images/round_corners/button_locations.png";
				locButton.onclick = new Function("cui.showMerchantLocationMap("+merchant.mid+");");
				floatRight.appendChild(locButton);
			}

			if(merchant.isCPG)
			{
					var viewToggle = document.createElement("div");
					//floatRight.appendChild(viewToggle);
					viewToggle.style.display = "inline-block";

					var tileButton = document.createElement("img");
					floatRight.appendChild(tileButton);
					tileButton.align = "absmiddle";
					tileButton.style.margin = "0px 0px 0px 6px";
					tileButton.alt = "View these offers as a grid";

					var listButton = document.createElement("img");
					floatRight.appendChild(listButton);
					listButton.align = "absmiddle";
					listButton.style.margin = "0px 3px 0px 0px";
					listButton.alt = "View these offers as a list";


					if(!merchant.displayMode || merchant.displayMode == 1){
						tileButton.src = "/includes/templates/master/images/round_corners/grid_active.jpg";
						listButton.src = "/includes/templates/master/images/round_corners/list_inactive.jpg";
						tileButton.style.cursor = "pointer";
						tileButton.onclick = new Function("cui.setMerchantDisplayMode("+merchantId+", 2);");

					}
					else if(merchant.displayMode && merchant.displayMode == 2){
						tileButton.src = "/includes/templates/master/images/round_corners/grid_inactive.jpg";
						listButton.style.cursor = "pointer";
						listButton.onclick = new Function("cui.setMerchantDisplayMode("+merchantId+", 1);");
						listButton.src = "/includes/templates/master/images/round_corners/list_active.jpg";

					}

					tileButton.alt = "Grid View";
				tileButton.title = "Grid View";
				listButton.alt = "List View";
				listButton.title = "List View";
			}
		}		
		var footer = false;
		if(this.model.inIframe)
		{
			footer = document.createElement("div");
		}
		var useMerchantTitle = merchantTitle;
		if (this.merchantPage && merchant.isCPG && !this.model.inMMF)
		{
			useMerchantTitle = false;
		}	
   		
		var round_corners = applyHeaderFooterBox(useMerchantTitle,merchantContent,footer);
		merchantHolder.appendChild(round_corners);
   		
		var merchantContent = document.getElementById("mc_"+merchantId);
		if(merchantContent)
		{
			removeChildElements(merchantContent);
		}

		if (!this.homePage && !this.merchantPage && merchant.isCPG && card && (card.status != 1 && card.status != 2))
		{
			var mWelcome = document.createElement("div");
			// mWelcome.style.borderBottom = "1px solid #BBBBBB";
			mWelcome.style.color = "#666666";
			mWelcome.style.background = "#e4e4e4";
			mWelcome.style.padding = "5px 0px";
			var text1 = "Save coupons directly to your " + card.merchantName + " " + card.cardName + "!";
			var text2 = "Click 'Save to Card' underneath your favorite " + card.merchantName + " coupon to add your " + card.cardName + " information.";
			if (!this.model.user.authenticated)
			{
				text2 = "Click on the 'Save to Card' button underneath your favorite coupon to get started.";
			}	
			var tSpan = document.createElement("span");
			tSpan.style.fontWeight = "bold";
			tSpan.style.margin = "2px 5px";
			tSpan.appendChild(document.createTextNode(text1));
			mWelcome.appendChild(tSpan);
			mWelcome.appendChild(document.createElement("br"));
			var tSpan2 = document.createElement("span");
			tSpan2.style.margin = "2px 5px";
			tSpan2.appendChild(document.createTextNode(text2));
			mWelcome.appendChild(tSpan2);
			merchantContent.appendChild(mWelcome);
		}	
		
		if (this.model.inMMF)
		{
			var cssAppend = '';
			if (this.model.mmfWidth == 7)
			{
				cssAppend = '_7';
			}	
			var showAuthBanner = (this.model.user.authenticated && card && (card.status != 1 && card.status != 2));
			var showNewBanner = (!this.model.user.authenticated);
			if (showAuthBanner)
			{
				cssAppend += '_auth';
			}
			if (showAuthBanner || showNewBanner)
			{
				var cssPrepend = this.model.iframeCssId;
				var mmcBannerContainer = document.createElement("div");
				mmcBannerContainer.className = cssPrepend + "_banner_container";
				merchantContent.appendChild(mmcBannerContainer);
				var mmcBanner = document.createElement("div");
				mmcBanner.className = cssPrepend + "_banner" + cssAppend;
				mmcBannerContainer.appendChild(mmcBanner);
				mmcRegButton = document.createElement("div");
				mmcRegButton.className = cssPrepend + "_reg_button" + cssAppend;
				mmcRegButton.id = "mmf_reg_button";
				mmcRegButton.onclick = new Function("wm.setLastAnchor('mmf_reg_button');cui.showRegForm(-1, false);");
			}
			if (showNewBanner)
			{
				mmcBanner.appendChild(mmcRegButton);
			}	
  	} 		
  }
  
  CouponUI.prototype.drawCoupon = function(index){
			var area = this.model.coupons[index].merchantId;
  		if (this.featuredOffers)
  		{
  			area = "featured";
  		}	
  		var displayArea = document.getElementById("mc_" + area);
  		var hider = document.getElementById("chider_"+index);
  		var couponHolder = document.getElementById("ch_"+index);
  		
  		// If the couponHolder for this coupon doesn't exist then we need to create it.
  		// Here we create the holder, the rounded corners and the coupon content canvas.
  		if(!couponHolder){
  			hider = document.createElement('div');
  			hider.id = "chider_"+index;
  			couponHolder = document.createElement("div");
  			couponHolder.id = "ch_"+index;
  			couponHolder.style.padding = "5px";
	  		//couponHolder.onclick = new Function("cui.coupons["+index+"].title = \"You Clicked Me!\"; cui.drawCoupon("+index+");");
	  		hider.appendChild(couponHolder);
  			displayArea.appendChild(hider);
  			
  			var couponContent = document.createElement("div");
  			couponContent.id = "cc_"+index;
  			//couponContent.style.border = "1px solid green";
  			//couponContent.style.padding = "2px";
  			
  			couponHolder.appendChild(couponContent);
  		}
  		
  		// Find our canvas
  		var couponContent = document.getElementById("cc_"+index);
  		// Remove the DOM children, to clean the slate 
  		removeChildElements(couponContent);
  		var coupon = this.model.coupons[index];
  		var merchant = this.model.merchants[coupon.merchantId];
  		var	mDisplayMode = merchant.displayMode;
  		if (this.featuredOffers || this.homePage)
  		{
  			mDisplayMode = 2;
  		}	
  		if(mDisplayMode && mDisplayMode == 1){
  			
		  		// Here we actually start to draw the coupon display
		  		var imageHolder = document.createElement("div");
		  		imageHolder.className = "coupon_img_holder";
		  	
		  		couponContent.appendChild(imageHolder);
		  		
		  		
		  		var imageId = coupon.imageId;
		  		var	imageWidth = 50;
		  		var	imageHeight = 50;
		  		
		  		var cDisplayMode = coupon.displayMode;
		  		if (this.featuredOffers || this.homePage)
		  		{
		  			cDisplayMode  = 2;
		  		}	
		  		
		  		if(cDisplayMode && cDisplayMode == 1){
		  			imageWidth = 150;
		  			imageHeight = 150;
		  		}
		  		if(imageId != "-1" && imageId != "0"){
		  			var image = document.createElement('img');
		  			//image.src = "/images/"+imageId+"_"+imageWidth+"_"+imageWidth+".jpg";
		  			image.src = "/images/"+imageId+".jpg";
			  		image.onclick = new Function("cui.toggleDetails("+index+");");
			  		image.style.cursor = "pointer";
			  		image.style.height = imageHeight + "px";
			  		image.style.width = imageWidth + "px";
			  		imageHolder.appendChild(image);
		  		}
		  				  		
		  		var couponBody = document.createElement("div");
		  		couponBody.className = "coupon_body";
		  		couponContent.appendChild(couponBody);
		  		
		  		if(cDisplayMode && cDisplayMode == 1){
		  			couponBody.style.width = "325px";
		  		}
		  		var title = document.createElement("div");
		  		title.style.fontSize = "16px";
		  		title.style.color = "#333333";
		  		title.style.fontWeight = "bold";
		  		title.innerHTML = this.model.coupons[index].title;
		  		
		  		couponBody.appendChild(title);
		  		var spacer = document.createElement("div");
		  		spacer.style.height="3px"
		  		couponBody.appendChild(spacer);
		  		var shortTerms = coupon.shortTerms;
		  		if(shortTerms.length > 65 && (!cDisplayMode || cDisplayMode === 0) )
		  			shortTerms = shortTerms.substring(0,62)+"..."; 
		  		couponBody.appendChild(document.createTextNode(shortTerms));
		  			  		
		  		var savingHolder = document.createElement("div");
		  		couponContent.appendChild(savingHolder);
		  		savingHolder.id = "animHolder_"+index;
		  		savingHolder.style.cssFloat = "right";
		  		savingHolder.style.styleFloat = "right";
		  		savingHolder.style.width = "150px";
		  		savingHolder.style.textAlign = "center";
		  		savingHolder.style.margin = "5px auto";
		  		//savingHolder.style.color = "#666666";
		  		savingHolder.style.fontWeight= "";
		
		  		// Show the Clip Button, etc that are only applicable to authenticated users
		  		
		  			var savingDiv = this.getSavingDiv(index);
		  			savingDiv.style.margin = "auto";
		  			savingHolder.appendChild(savingDiv);
		  		
		  		
		  		// If expanded then show the full coupon details
		  		if(cDisplayMode && cDisplayMode == 1){
					couponHolder.style.background = "#e4e4e4";
					var detailHolder = document.createElement("div");
		  			detailHolder.style.width = "490px";
		  			detailHolder.style.cssFloat = "left";
		  			detailHolder.style.styleFloat = "left";
		  			detailHolder.style.margin = "0px 0px 0px 14px";
		  			//detailHolder.style.color = "#666666"
		  			
		  			var expires = document.createElement("div");
		  			expires.style.fontWeight = "bold";
		  			expires.style.color = "#555555";
		  			if(coupon.isCPG && coupon.isCPG != "0"){
			  			expires.appendChild(document.createTextNode("Offer must be saved by "+this.model.coupons[index].deliverByDate));
			  			expires.appendChild(document.createElement("br"));
		  			}
		  			var expspan = document.createElement("span");
					expspan.innerHTML = getFormattedDate(coupon.expireDate, false, "error", "Expires");
					expires.appendChild(expspan);
		  			detailHolder.appendChild(expires);
		  			
		  			
		  			var longTerms = document.createElement("div");
		  			longTerms.style.margin = "10px 0px";
		  			longTerms.style.fontSize = "9px";
		  			longTerms.style.color = "#555555";
		  			coupon.longTerms = coupon.longTerms.replace(/\n|\r/ig,"<br/>");
		  			longTerms.innerHTML = coupon.longTerms;
		  			detailHolder.appendChild(longTerms);
		  			
		  			var expireMessage = document.createElement('div');
		  			expireMessage.style.margin = "0px 0px 5px 0px";
		  			expireMessage.style.fontWeight = "bold";
		  			detailHolder.appendChild(expireMessage);
		  			
		  			if (!coupon.isRestricted)
		  			{		  			
							var fbSpan = document.createElement("div")
							var shareOnFb = document.createElement("a");
							fbSpan.style.background = "transparent url(/images/share/fb_share_icon.png) no-repeat";
							shareOnFb.style.color = "#555555";
							shareOnFb.style.textDecoration = "underline";
							fbSpan.style.height = "16px";
							shareOnFb.style.fontWeight = "normal";
							fbSpan.style.margin = "0px 0px 0px 0px";
							fbSpan.style.padding = "0px 0px 0px 20px";
							shareOnFb.appendChild(document.createTextNode("Share on Facebook"));
							shareOnFb.href = "javascript: cui.shareOnFB(" + index + ", 'list_detail')";
							fbSpan.appendChild(shareOnFb);
							detailHolder.appendChild(fbSpan);
							detailHolder.appendChild(document.createElement("br"));
						}	
		  			
		  			var hideDetails = document.createElement("a");
		  			hideDetails.style.color = "#555555";
		  			hideDetails.style.textDecoration = "underline";
		  			hideDetails.style.fontWeight = "normal";
		  			hideDetails.appendChild(document.createTextNode("Hide Full Details"));
		  			hideDetails.href = "javascript: cui.toggleDetails("+index+")";
		  			detailHolder.appendChild(hideDetails);
		  			
		  			couponContent.appendChild(detailHolder);
		  		}
		  		else
		  		{
		  			couponHolder.style.background = "";
		  			couponBody.appendChild(document.createElement("br"));
		  			var showDetailsDiv = document.createElement("div");
		  			var showDetails = document.createElement("a");
		  			showDetails.style.color = "#555555";
		  			showDetails.style.textDecoration = "underline";
		  			showDetails.style.fontWeight = "normal";
		  			showDetails.appendChild(document.createTextNode("View Full Details"));
		  			showDetails.href = "javascript:pageTracker._trackEvent('OfferDetails', 'View_List', '"+index+"'); cui.toggleDetails("+index+")";
		  			showDetailsDiv.appendChild(showDetails);
		  			showDetailsDiv.style.cssFloat = "left";
		  			showDetailsDiv.style.styleFloat = "left";
		  			couponBody.appendChild(showDetailsDiv);
		  			
		  			if (!coupon.isRestricted)
		  			{		  			
							var fbSpan = document.createElement("div")
							var shareOnFb = document.createElement("a");
							fbSpan.style.background = "transparent url(/images/share/fb_share_icon.png) no-repeat";
							shareOnFb.style.color = "#555555";
							shareOnFb.style.textDecoration = "underline";
							fbSpan.style.height = "16px";
							fbSpan.style.cssFloat = "left";
							fbSpan.style.styleFloat = "left";
							shareOnFb.style.fontWeight = "normal";
							fbSpan.style.margin = "0px 0px 0px 10px";
							fbSpan.style.padding = "0px 0px 0px 20px";
							shareOnFb.appendChild(document.createTextNode("Share on Facebook"));
							shareOnFb.href = "javascript: cui.shareOnFB(" + index + ", 'list_lite')";
							fbSpan.appendChild(shareOnFb);
							couponBody.appendChild(fbSpan);
						}
					}
		  		
	  	}
  		// Tiled view
  		else if(mDisplayMode && mDisplayMode == 2){
  			hider.style.cssFloat = "left";
  			hider.style.styleFloat = "left";
  			hider.style.height = "170px";
				if (this.homePage || this.model.inMMF)
				{
					if (this.model.inMMF)
					{
						if (this.model.mmfWidth == 7)
						{
							hider.style.width = "32%";
						}
						else
						{
							hider.style.width = "24%";
						}
					}
					if (this.homePage)
					{
						if (coupon.isCPG && coupon.isCPG != "0")
						{
							hider.style.width = "20%";
						}
						else
						{
							hider.style.width = "50%";
						}
					}	
					hider.style.margin = "2px auto";
					couponHolder.style.width = "167px";
					couponHolder.style.margin = "0px auto";
				}
				else
				{
					hider.style.width = "167px";
					hider.style.margin = "2px";
				}	
  			
  			couponHolder.style.padding = "0px";
  			couponHolder.className = "none";
  			couponHolder.onmouseover = new Function(
  					"this.className = 'solid_gray';"
  					);
  			couponHolder.onmouseout = new Function(
  					"this.className = 'none';"
  					);
  			
  			var content = document.createElement("div");
  			content.style.position = "relative";
  			var roundCorners = applyRoundedCorners(content,1,"");
  			couponContent.appendChild(roundCorners);
  			content.style.textAlign = "center";
  			  			
  			var imageHolder = document.createElement("div");
  			content.appendChild(imageHolder);
  			imageHolder.id = "ih_"+index;
  			imageHolder.style.position = "relative";
  			imageHolder.style.width = "100px";
  			imageHolder.style.height = "100px";
  			imageHolder.style.background = "transparent url(/includes/templates/master/images/productshadow.png) no-repeat";
  			imageHolder.style.margin = "0px auto 5px auto";
  			imageHolder.onclick = new Function("pageTracker._trackEvent('OfferDetails', 'View_Grid', '"+index+"');cui.showCouponDetails("+index+")");
  			imageHolder.style.cursor = "pointer";  		
  			
  			var imageId = this.model.coupons[index].imageId;
  			var image = document.createElement("img");
  			imageHolder.appendChild(image);
  			image.src = "/images/"+imageId+".jpg";
  			
  			if(imageId == 0 || imageId == -1)
  				image.src = "/includes/templates/master/images/tag_100x100.gif";
  			
  			image.style.height = "100px";
  			image.style.width = "100px";
  			
  			var title = document.createElement("div");
  			content.appendChild(title);
  			title.style.fontWeight = "bold";
  			title.style.fontSize = "12px";
  			title.style.height = "25px";
  			title.style.margin = "0px auto 5px auto";
  			title.onclick = new Function("cui.showCouponDetails("+index+")");
  			title.style.cursor = "pointer";
  			
  			
  			var titleText = coupon.title;
  			if (this.featuredOffers)
  			{
  				titleText = merchant.name + ": " + coupon.title;
  			}	
  			/*
  			else if (coupon.isCPG)
  			{
  				titleText = this.getTitleSavings(coupon.title);
  				titleText += "<br/>";
  				titleText += this.getTitleMain(coupon.title);
  			}	
  			*/
  			if(titleText.length > 33)
  				titleText = titleText.substring(0,30)+"...";
  			//title.appendChild(document.createTextNode(titleText));
  			title.innerHTML = titleText;
  			
  			var savingHolder = document.createElement("div");
  			content.appendChild(savingHolder);
  			// savingHolder.style.visibility = "hidden";
  			
  			var savingDiv = this.getSavingDiv(index);
  			var animHolder = document.createElement("div");
  			animHolder.id = "animHolder_"+index;
  			animHolder.style.height = "60px";
			animHolder.style.width = "60px";
			animHolder.style.top = "0px";
			animHolder.style.right = "0px";
			animHolder.style.position = "absolute";
			animHolder.style.textAlign = "right";
  			imageHolder.appendChild(animHolder);
  			if(this.model.coupons[index].isSaved)
  			{
  				if(coupon.working)
  				{
  					this.endSavingAnimation(index, 5);
  				}
  				else
  				{
						var savedIcon = document.createElement("img");
						savedIcon.src = "/includes/templates/master/images/round_corners/checkmark.png";
						animHolder.appendChild(savedIcon); 
						if (this.model.inIframe)
						{
							savingDiv.style.margin = "auto";
							savingHolder.appendChild(savingDiv);
						}
  				}
  			}
  			else
  			{
  				savingDiv.style.margin = "auto";
  				savingHolder.appendChild(savingDiv);
  			}
		  	
  		}
  		
  		var clear = document.createElement("div");
  		clear.className = "clear";
  		couponContent.appendChild(clear);
  		
  		clear = document.createElement("div");
  		clear.className = "clear";
  		couponHolder.appendChild(clear);
  		
  		var clearId = coupon.merchantId;
  		if (this.featuredOffers)
  		{
  			clearId = "featured";
  		}	
  		
  		clear = document.getElementById("clearMerc_"+clearId);
  		if(!clear){
  			clear = document.createElement("div");
  			clear.className = "clear"
  			clear.id = "clearMerc_"+clearId;
  		}
  		displayArea.appendChild(clear);
  			
  }	
  
  CouponUI.prototype.toggleDetails = function(index){
  	var coupon = this.model.coupons[index];
  	var hider = document.getElementById("chider_"+index);
  	var holder = document.getElementById("ch_"+index);
  	hider.style.height = holder.offsetHeight+"px";
  	
  	if(!coupon.displayMode || coupon.displayMode == 0){
  		coupon.displayMode = 1;
  		this.postView(index);
  	}
  	else
  		coupon.displayMode = 0;
  		
 	hider.style.overflow = "hidden";
  	this.drawCoupon(index);
  	
  	setTimeout("openDiv(30,\"chider_"+index+"\","+holder.offsetHeight+")",10);
  }
  
  CouponUI.prototype.setMerchantDisplayMode = function(merchantId, mode){
  	this.model.merchants[merchantId].displayMode = mode;
  	this.drawMerchant(merchantId);
  	for(var i in this.model.couponIdDisplayList){
  		var coupon = this.model.coupons[this.model.couponIdDisplayList[i]];
  		if(!coupon) continue;
  		if(coupon.merchantId == merchantId)
  			this.drawCoupon(coupon.couponId);
  	}
  	
  	this.windowManager.closeOpenWindow();
  	
  }
  
  CouponUI.prototype.closeCoupon = function(couponId){
  		var id = "chider_"+couponId;
  		var coupon = this.model.coupons[couponId];
  		var mh = document.getElementById('mh_'+coupon.merchantId);
  		// If it is smaller than 100 px then it has not more offers and needs to be closed.
  		if(mh.offsetHeight < 130)
  			this.closeMerchant(coupon.merchantId);
  		else{
	  		var callback = new Function("cui.onCloseCouponFinish("+couponId+");");
	  		closeDiv(20, id, callback);
  		}
  		
  }
  
  CouponUI.prototype.onCloseCouponFinish = function(couponId){
  		var chider = document.getElementById('chider_'+couponId);
  		chider.parentNode.removeChild(chider);
  		this.model.removeCouponFromDisplayList(couponId);
  }
  
  CouponUI.prototype.closeMerchant = function(merchantId){
  		var id = 'mh_'+merchantId;
  		var callback = new Function("cui.onCloseMerchantFinish("+merchantId+");");
  		closeDiv(20,id,callback);
  }
  
  CouponUI.prototype.onCloseMerchantFinish = function(merchantId){
  		var mercHolder = document.getElementById('mh_'+merchantId);
  		mercHolder.parentNode.removeChild(mercHolder);
  		this.model.removeMerchantFromDisplayList(merchantId);
  }
  
  CouponUI.prototype.fadeCoupon = function(index){
  		var ch_id = "ch_"+index;
  		var couponHolder = document.getElementById(ch_id);
  		if(couponHolder){
  			fadeDiv("ch_"+index, 30, 10);
  			setTimeout("closeDiv(30,'"+ch_id+"')", 2000)
  		}
  }

  CouponUI.prototype.getSavingDiv = function(index){
  		var hasSaved = this.model.coupons[index].isSaved;
		
		var savingArea = document.createElement("div");
		savingArea.id = "sa_"+index;
		savingArea.style.margin = "0px 0px 0px 0px";
		savingArea.style.width = "133px";
		
		if(hasSaved){
			var savedIcon = document.createElement("img");
			savedIcon.src = "/includes/templates/master/images/round_corners/checkmark.png"; 
			savedIcon.id = "savedIcon_"+index;
			
			if(!this.model.coupons[index].isCPG || this.model.coupons[index].isCPG == "0" || this.model.coupons[index].isCPG == 0){
				savedIcon.onclick = new Function("cui.unclip("+index+")");
				savedIcon.style.cursor = "pointer";
				if(this.showRemoveIcon){
					savedIcon = getButton("red","Remove", 100, new Function("cui.unclip("+index+")")); 
				}
				else
					savedIcon.onmouseover = new Function("cui.toggleSavedIcon("+index+")");
			}
			
			if (this.model.inIframe)
			{
				var sButtonImg = document.createElement("img");
				sButtonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/savedToCard.gif";
				sButtonImg.style.margin = "auto";
				sButtonImg.alt = "Saved to Card";
				savingArea.appendChild(sButtonImg);
			}
			else
			{
				savingArea.appendChild(savedIcon);
			}
			
		}
		else{
			var card = this.model.loyaltyCards[this.model.coupons[index].merchantId]
			if(card && card.numSavedToCard >= card.capacity){
				var message = getButton("gray","Card is Full", 100, false);
				message.style.margin = "auto";
				savingArea.appendChild(message);				
			}
			else{
				var buttonText = "";
				var saveButtonFunc;
				if(this.model.coupons[index].isCPG && this.model.coupons[index].isCPG != 0)
				{
					buttonText = "Save to Card";
					// If the card has been successfully added or is pending
					if(card && card.status == 1){
						saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.clip("+index+");");
					}
					else if( card && card.status == 2)
					{
						saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "'); _MODEL.loadLoyaltyCards(new Function(\"cui.checkStatusAndClip("+index+");\"))");
					}
					else if(this.model.user.authenticated){
						saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showAddCard("+index+");");
					}
					else
					{
						if (this.model.inIframe)
						{
							var merchant = this.model.merchants[this.model.iframeMid];
							saveButtonFunc = new Function("pageTracker._trackEvent('Popups', 'View', '" + merchant.mid + "_QFrame_Reg'); wm.setLastAnchor('sa_" + index + "');cui.showRegForm("+index+", true);");
						}
						else
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showSignUpMessage("+index+");");
						}
					}	
				}
				else
				{
					buttonText = "Save to Phone";
					if(this.model.user.authenticated && this.model.user.wsPhoneId != 2000)
						saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.clip("+index+");");
					else if(this.model.user.authenticated)
						saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showNeedClientWindow("+index+")");
					else
						saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showSignUpMessage("+index+");");
						
				}
				if (this.model.inIframe)
				{
					var sButtonImg = document.createElement("img");
					sButtonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/saveToCard.gif";
					sButtonImg.style.margin = "auto";
					sButtonImg.style.cursor = "pointer";
					sButtonImg.alt = "Save to Card";
					sButtonImg.onclick = saveButtonFunc;
					savingArea.appendChild(sButtonImg);
				}
				else
				{
					var saveButton = getButton("", buttonText, 110, saveButtonFunc);
					saveButton.style.margin = "auto";
					savingArea.appendChild(saveButton);
				}	
			}
			
			if(!this.featuredOffers && !this.homePage && (!this.model.coupons[index].displayMode || this.model.coupons[index].displayMode == 0) && this.model.merchants[this.model.coupons[index].merchantId].displayMode == 1){
				var spacer = document.createElement("div");
				spacer.style.height = "5px";
				savingArea.appendChild(spacer);
				var expspan = document.createElement("span");
				expspan.innerHTML = getFormattedDate(this.model.coupons[index].expireDate, false, "error", "Expires");
				savingArea.appendChild(expspan);
			}
				
		}
		
		return savingArea;
  }
  
  CouponUI.prototype.clip = function(index){
  		var savingAreaNormal = document.getElementById("sa_"+index);
  		removeChildElements(savingAreaNormal);
  		var coupon = this.model.coupons[index];
  		var type = "phone";
  		if(coupon.isCPG && coupon.isCPG != "0")
  			type = "card";
  		savingAreaNormal.style.color = "#000000";
  		savingAreaNormal.appendChild(document.createTextNode("Saving to "+type+"..."))
  		//savingAreaNormal.appendChild(document.createTextNode("Saving..."))
  		// Show the loading icon
  		this.startSavingAnimation(index);
  		/*
  		removeChildElements(savingAreaNormal);
  		var loadingIcon1 = document.createElement("img");
  		loadingIcon1.src = "/includes/templates/master/images/loader_small_red.gif";
  		savingAreaNormal.appendChild(loadingIcon1);
  		*/ 
  		
  		// Log another view for some reason
  		this.postView(index);
  		
	 	this.windowManager.closeOpenWindow();
	 	this.model.saveCoupon(index); 	
	 	
  }
  
  CouponUI.prototype.checkStatusAndClip = function(index)
  {
  		var coupon = this.model.coupons[index];
  		var lc = this.model.loyaltyCards[coupon.merchantId];
  		if(lc.status == 1 || lc.status == 2)
  			this.clip(index);
  		else
  			this.showAddCard(index);
  			
  }
  
  CouponUI.prototype.startSavingAnimation = function(couponId){
  		var stepTime = 75;
  		var coupon = this.model.coupons[couponId];
  		var type = "phone";
  		if(coupon.isCPG && coupon.isCPG != "0")
  			type = "card";
  			
  		// Load all the images
  		var image = document.createElement("img");
  		for(var i = 1; i < 8; i++)
  			image.src = "/includes/templates/master/images/animation/save2"+type+"_"+i+".png";
  			
  		this.drawSavingAnimationStep(couponId, 1);
  		setTimeout("cui.drawSavingAnimationStep("+couponId+",2);",stepTime);
  		setTimeout("cui.drawSavingAnimationStep("+couponId+",3);",stepTime*2);
  		setTimeout("cui.loopSavingAnimation("+couponId+",4);",stepTime*3);
  }
  
  CouponUI.prototype.loopSavingAnimation = function(couponId, currentStep){
  		var stepTime = 200;
  		var coupon = this.model.coupons[couponId];
  		var nextStep = currentStep+1;
  		if(coupon.working && coupon.working != 2){
	  		if(nextStep > 8) nextStep = 4;
	  		this.drawSavingAnimationStep(couponId, nextStep)
	  		setTimeout("cui.loopSavingAnimation("+couponId+","+(nextStep)+")", stepTime);
  		}
  		else
  			this.endSavingAnimation(couponId, nextStep);
  }
  
  CouponUI.prototype.drawSavingAnimationStep = function(couponId, step){
  		var holder = document.getElementById("animHolder_"+couponId);
  		if(!holder && !image) return;
  		var image = document.getElementById("saveAnim_"+couponId);
  		if(!image){
  			removeChildElements(holder);
  			image = document.createElement("img");
  			image.id = "saveAnim_"+couponId;
  			holder.appendChild(image);
  		}
  		var coupon = this.model.coupons[couponId];
  		var type = "phone";
  		if(coupon.isCPG && coupon.isCPG != "0")
  			type = "card";
  		
  		if(step < 12)	
  			image.src = "/includes/templates/master/images/animation/save2"+type+"_"+step+".png";
  		else
  			image.src = "/includes/templates/master/images/round_corners/checkmark.png";
  			
  			
 }
  
  CouponUI.prototype.endSavingAnimation = function(couponId, step){
  		var stepTime = 100;
  		var coupon = this.model.coupons[couponId];
  		var type = "phone";
  		if(coupon.isCPG && coupon.isCPG != "0")
  			type = "card";
  			
  		var count = 1;
  		for(var i = step; i <= 11; i++){
  			var s = i;
  			if(i > 8) s = 5-(i%8);
  			setTimeout("cui.drawSavingAnimationStep("+couponId+","+s+");",stepTime*count);
  			count++;
  		}
  		
  		setTimeout("_MODEL.setCouponWorkingState("+couponId+", false);", stepTime*count);
  		
  }
  
  CouponUI.prototype.onCouponChange = function(){
  		for(var i in this.model.coupons){
  			if(this.model.coupons[i].hasChanged && !this.model.coupons[i].working){
  				//alert("Drawing couponId: "+i+"; hasChanged: "+this.model.coupons[i].hasChanged);
  				this.drawCoupon(i);
  				
  				// If we want to show the offers being removed
  				if(this.showRemoveIcon){
  					this.closeCoupon(i);
  				}
  				this.model.coupons[i].hasChanged = false;
  			}
  		}
  		
  }
  
  CouponUI.prototype.onCouponSaveFailed = function(param){
	  	if(param && param.couponId){
	  		var coupon = this.model.coupons[param.couponId];
	  		if(coupon){
	  			var card = this.model.loyaltyCards[coupon.merchantId];
	  			if(card && (card.status == 3 || card.status == 4)){
	  				this.lcui.showAddCardFormWindow(coupon.merchantId, coupon.offerId, function(){});
	  				var error = document.getElementById("add_card_error_"+coupon.merchantId);
	  				error.className = "error";
	  				if(card.status == 3)
	  					error.innerHTML = "We were unable to find your card account. Check the number and try again. New card holders: it can take up to 5 business days before your card account can be accessed.";
	  				else
	  					error.innerHTML = "The card you added is already in use by another " + this.model.clientName + " user. Please try another card.";
	  				return;
	  			}
	  		}
	  	}
	  	
		var errorHolder = document.createElement("div");
		errorHolder.style.width = "100%";
		errorHolder.style.textAlign = "left";
		
		//var coupon = this.model.coupons[index];
		//if(coupon.isCPG || coupon.isCPG != "0"){
			var title = document.createElement("div");
			title.style.fontSize = "20px";
			title.appendChild(document.createTextNode("Sorry..."));
			errorHolder.appendChild(title);

			var p1 = document.createElement("div");
			p1.appendChild(document.createTextNode("We were unable to find your card account. Check the number and try again. New card holders: it can take up to 5 business days before your card account can be accessed."));
			errorHolder.appendChild(p1);
			p1.style.margin = "3px 0px";
			
			var p2 = document.createElement("div");
			p2.appendChild(document.createTextNode("Please "));
			var cardLink = document.createElement("a");
			cardLink.href = "/account_overview.php";
			cardLink.appendChild(document.createTextNode("click here"));
			p2.appendChild(cardLink);
			p2.appendChild(document.createTextNode(" to check the status of your card. If you continue to have this problem, please "));
			
			var supportLink = document.createElement("a");
			supportLink.href="/feedback.php";
			supportLink.appendChild(document.createTextNode("contact support"));
			p2.appendChild(supportLink);
			
			p2.appendChild(document.createTextNode("."));
			p2.style.margin = "3px 0px";
			errorHolder.appendChild(p2);
		//}
		/*else{
			errorHolder.appendChild(document.createTextNode("Sorry, we were unable to save this offer. Please try again later. If you continue to have this problem, please "));
			var supportLink = document.createElement("a");
			supportLink.href="/feedback.php";
			supportLink.appendChild(document.createTextNode("contact support"));
			errorHolder.appendChild(supportLink);
			errorHolder.appendChild(document.createTextNode("."));
		}
		*/
		var handle = this.windowManager.createNewBlackOutWindow(errorHolder, 300, 0, 0, true);
		this.windowManager.showWindow(handle);
		this.initCouponDisplay();
  }
  
  CouponUI.prototype.unclip = function(index){
  		var savingAreaNormal = document.getElementById("sa_"+index);
  		
  		// Show the loading icon
  		removeChildElements(savingAreaNormal);
  		var loadingIcon1 = document.createElement("img");
  		loadingIcon1.src = "/includes/templates/master/images/loader_small_red.gif";
  		savingAreaNormal.appendChild(loadingIcon1);
  		
  		this.model.unsaveCoupon(index);
	 	
	 	this.windowManager.closeOpenWindow();	
  }
  
  CouponUI.prototype.buildCouponDetailContent = function(index){
  		var coupon = this.model.coupons[index];
  		var merc = this.model.merchants[coupon.merchantId];
  		var content = document.createElement("div");
  		content.id = "cd_content_"+index;
  		content.style.textAlign = "center";
		
		var imgDiv = document.createElement('div');
		var txtDiv = document.createElement('div');
		var imgEle = document.createElement('img');
		var clearDiv = document.createElement('div');
		var clearDiv2 = document.createElement('div');
		var clearDiv3 = document.createElement('div');
		var buttonHolder = document.createElement('div');
				
		imgDiv.className = "pop_img_holder";
		if(coupon.imageId != 0 && coupon.imageId != "-1" ){
			imgEle.src = "/images/"+coupon.imageId+".jpg";
			//imgEle.src = "/images/default_product.jpg";
			//imgEle.className = "default_product";
			imgEle.alt = " ";
			imgEle.width ="200";
			imgEle.height = "200";
			imgDiv.appendChild(imgEle);
		}
		else
			imgDiv.style.background = "url(/includes/templates/master/images/tag_200x200_final.gif) no-repeat";
			
		
		
		txtDiv.className = "pop_txt_holder";
		var title = document.createElement("span");
		title.style.fontSize = "16px";
		title.style.fontWeight = "bold";
		title.style.color = "black";
		title.appendChild(document.createTextNode(merc.name+": "+coupon.title));
		txtDiv.appendChild(title);
		txtDiv.appendChild(document.createElement("br"));
		txtDiv.appendChild(document.createElement("br"));
		
		var shortTerms = document.createElement("span");
		shortTerms.style.fontSize = "14px";
		shortTerms.appendChild(document.createTextNode(coupon.shortTerms));
		txtDiv.appendChild(shortTerms);
		txtDiv.appendChild(document.createElement("br"));
		txtDiv.appendChild(document.createElement("br"));
		
		var expires = document.createElement("strong");
		expires.appendChild(document.createTextNode("Must save before "+coupon.deliverByDate));
		expires.appendChild(document.createElement("br"));
		var expspan = document.createElement("span");
		expspan.innerHTML = getFormattedDate(coupon.expireDate, false, "error", "Expires");
		expires.appendChild(expspan);
		txtDiv.appendChild(expires);
		txtDiv.appendChild(document.createElement("br"));
		txtDiv.appendChild(document.createElement("br"));
		
		coupon.longTerms =  coupon.longTerms.replace(/\n|\r/ig,"<br/>");
		var lt = document.createElement("span");
		lt.innerHTML = coupon.longTerms;
		txtDiv.appendChild(lt);
		txtDiv.appendChild(document.createElement("br"));
		
		clearDiv.className = "clear";
		clearDiv2.className = "clear";
		clearDiv3.className = "clear";
		
		buttonHolder.className = "pop_button_holder";
		
		shareButton = document.createElement("img");
		shareButton.src = "/images/share/fb_share_detail.png";
		shareButton.style.margin = "auto";
		shareButton.style.cursor = "pointer";
		shareButton.alt = "Share on Facebook";
		shareButton.onclick = new Function("cui.shareOnFB("+ index +", 'detail')");
		
		
		// shareButton = getButton("blue", "FB TEST",125, new Function("cui.shareOnFB("+ index +", 'detail');"));
		// shareButton.style.margin = "auto";
		
		txtDiv.appendChild(buttonHolder);
		txtDiv.appendChild(clearDiv);
		if (!this.model.inIframe && !coupon.isRestricted)
		{
			buttonHolder.appendChild(shareButton);
		}	
		var clearDiv = document.createElement("div");
		clearDiv.className = "clear";
		buttonHolder.appendChild(clearDiv);
		
		content.appendChild(imgDiv);
		content.appendChild(clearDiv2);
		content.appendChild(txtDiv);		
		content.appendChild(clearDiv3);
		
		return content;
  }
  
  CouponUI.prototype.showCouponDetails = function(index){
  		var coupon = this.model.coupons[index];
  		var merc = this.model.merchants[coupon.merchantId];
  		var width = 300;
  		var pointAt = document.getElementById("ih_"+index);
  		
  		this.postView(coupon.couponId);
  		
  		if(coupon.detailWindowHandle){
  			this.windowManager.showWindow(coupon.detailWindowHandle);
  		}
  		else{
  			var content = this.buildCouponDetailContent(index);
  			coupon.detailWindowHandle = wm.createNewCallOutWindow(content, width, "ih_"+index, true, 1);
  			wm.showWindow(coupon.detailWindowHandle);
  		}
  			
  }
  
  CouponUI.prototype.buildNeedClient = function(index){
  	var container = document.createElement("div");
  	var title = document.createElement("div");
  	container.appendChild(title);
  	title.style.fontSize = "16px";
  	title.style.fontWeight = "bold";
  	title.appendChild(document.createTextNode("Setup " + this.model.clientName + " on your phone"));
  	
  	var bodyText = document.createElement("div");
  	container.appendChild(bodyText);
  	bodyText.appendChild(document.createTextNode("To save this offer you must first setup " + this.model.clientName + " on your phone."));
	bodyText.style.margin = "5px 0px";
	
	var buttonHolder = document.createElement("div");
	container.appendChild(buttonHolder);
	buttonHolder.style.textAlign = "center";
	buttonHolder.style.width = "100%";
	buttonHolder.style.margin = "10px 0px";
	
	var button = getButton("blue", "Setup " + this.model.clientName + " on Phone", 200, function(){window.location="/help/ps_help.php?reg_type=0&helpId=2";});
	button.style.margin = "auto";
	buttonHolder.appendChild(button);
	
	var buttonHolder = document.createElement("div");
	container.appendChild(buttonHolder);
	buttonHolder.style.textAlign = "center";
	buttonHolder.style.width = "100%";
	buttonHolder.style.margin = "10px 0px";
	buttonHolder.appendChild(document.createTextNode("Already have " + this.model.clientName + " set up on your phone?"))
	
	var param = "c="+index+"&z="+(this.model.user.currentSearch[0])+"&r=/deals.php";
	var button = getButton("blue", "Save offer to Phone", 200, function(){window.location="/includes/form_handlers/loli.php?"+param;});
	button.style.margin = "10px auto";
	buttonHolder.appendChild(button);
	
	return container;
  }
  
  CouponUI.prototype.showNeedClientWindow = function(index){
  	if(this.needClientWindowHandle){
  		this.windowManager.showWindow(this.needClientWindowHandle);
  		return;
  	}
  	
  	this.needClientWindowHandle = this.windowManager.createNewBlackOutWindow(this.buildNeedClient(index), 330, 0, 0, true);
  	this.windowManager.showWindow(this.needClientWindowHandle);	
  }
  
  CouponUI.prototype.showAddCard = function(index){
  	var callback = new Function("cui.model.fireOpinMindPixel(new Array(" + this.model.coupons[index].offerId + ", 0))");
  	this.lcui.showAddCardFormWindow(this.model.coupons[index].merchantId, this.model.coupons[index].offerId, callback);
  }
  
  CouponUI.prototype.showAddCardNoOffer = function(merchantId)
  {
  	this.lcui.showAddCardFormWindow(merchantId, 0, null);
  }

  CouponUI.prototype.postView =  function(cid){
	  	var req = getXMLHttpObject();
	  	var openString = "/includes/form_handlers/log_view.php?cid="+cid;
	  	// alert(openString);
	  	req.open("GET",openString,true);
	  	req.send(null);
	}
  
  CouponUI.prototype.generateMercDisplayList = function(){
  		this.mercDisplayList = new Array();
  		var mdl = this.mercDisplayList; 
  		var page = 1;
  		var cemCount = 0;
  		for(var index in this.model.couponIdDisplayList){
  			var coupon = this.model.coupons[this.model.couponIdDisplayList[index]];
  			if(!coupon) continue;
  			var merchant = this.model.merchants[coupon.merchantId];
  			if(!merchant) continue;
  			if(!mdl[coupon.merchantId]){
  				// If the pageEleId is null or false then put everything on the first page.
  				if(this.pageEleId)
  					mdl[coupon.merchantId] = page;
  				else
  					mdl[coupon.merchantId] = 1;
  				this.totalPages = page;
  				if(merchant.isCPG) page++;
  				else{
  					cemCount++;
  					if(cemCount >= 5){
  						cemCount = 0;
  						page++;
  					}
  				}
  			}
  		}
  } 
  
  CouponUI.prototype.setPage = function(page){
  		if(page < 1) page = 1;
  		else if(page > this.totalPages) page = this.totalPages;
		this.currentPage = page;
		this.drawPagination();
		this.initCouponDisplay();
		this.windowManager.closeOpenWindow();
		//window.scrollTo(0,100);
		
  }
  
  CouponUI.prototype.showMerchantLocationMap = function(merchantId){
  		var merchant = this.model.merchants[merchantId];
  		if(!merchant) return;
  		if(merchant.mapWindowHandle){
  			this.windowManager.showWindow(merchant.mapWindowHandle);
  			return;
  		}
  		
  		var mapHolder = document.createElement("div");
  		mapHolder.id = "mercLocMapHolder_"+merchantId;
		mapHolder.style.width = "100%";
  		mapHolder.style.height = "400px";
  		mapHolder.style.position = "relative";
  			
		var mapEle = document.createElement("div");
		mapHolder.appendChild(mapEle);
  		mapEle.id = "mercLocMapEle_"+merchantId;
  		mapEle.style.width = "100%";
  		mapEle.style.height = "400px";
  		mapHolder.style.position = "relative";
  		
  		  		
  		var loader = document.createElement("img");
  		loader.src = "/includes/templates/master/images/loader.gif";
  		loader.style.margin = "200px 0px 0px 0px";
  		
  		mapEle.appendChild(loader);
  		
  		var merchant = this.model.merchants[merchantId];
  		
  		var logo;
  		
  		if(merchant.logoId != -1){
	  		logo = document.createElement("img");
	  		logo.src = "/images/"+merchant.logoId+".jpg"
  		}
  		else{
  			logo = document.createElement("div");
  			logo.style.fontSize = "24px";
  			logo.style.fontWeight = "normal";
  			logo.style.color = "#333333";
  			logo.appendChild(document.createTextNode(merchant.name));
  		}
  		
		logo.style.position = "absolute";
	  	logo.style.top = "-5px";
	  	logo.style.left = "10px";
	  	logo.style.zIndex = "100";
	  	mapHolder.appendChild(logo);
	  	
  		var logoBack = document.createElement("div");
	  	logoBack.className = "locationLogoBack";
	  	mapHolder.appendChild(logoBack);
	  	
  		merchant.mapWindowHandle = cui.windowManager.createNewBlackOutWindow(mapHolder, 500, 0, 0, true);
  		cui.windowManager.showWindow(merchant.mapWindowHandle);
  		
  		//this.buildMercLocMapElement(merchantId);
  		
  		var callback = function(merchantId){
  			cui.buildMercLocMapElement(merchantId);
  			var merchant = _MODEL.merchants[merchantId];
  			var mapHolder = document.getElementById("mercLocMapEle_"+merchantId);
  			
  			if(!merchant.locations || merchant.locations.length == 0){
  				removeChildElements(mapHolder);
	  			var noLocMessage = document.createElement("div");
	  			noLocMessage.style.position = "relative";
	  			noLocMessage.style.top = "100px";
	  			noLocMessage.style.width = "100%";
	  			noLocMessage.style.textAlign = "center";
	  			noLocMessage.style.fontSize = "18px";
	  			noLocMessage.style.fontWeight = "bold";
	  			noLocMessage.appendChild(document.createTextNode("Sorry, there are no locations in your area"));
	  			mapHolder.appendChild(noLocMessage);
  			}
  		}
  		this.getMercLocations(merchantId, callback);
  }
  
  CouponUI.prototype.buildMercLocMapElement = function(merchantId){
  		var mapEle = document.getElementById("mercLocMapEle_"+merchantId);
  		
  		var merchant = this.model.merchants[merchantId];
  		if(merchant.locations && merchant.locations.length > 0){
  			
  			
  			var center = [this.model.user.displayLoc[2],
  							this.model.user.displayLoc[3]];
  							
  			//alert(center[0]+", "+center[1]);
  			
  			var count = 0;
	  		for(var i = 0; i < merchant.locations.length; i++){
	  			if(merchant.locations[i].latitude != 0 && merchant.locations[i].longitude != 0) count++;
	  			//center[0] += parseFloat(merchant.locations[i].latitude);
	  			//center[1] += parseFloat(merchant.locations[i].longitude);
	  		}
	  		
	  		//center[0] /= parseFloat(count);
	  		//center[1] /= parseFloat(count);
	  		removeChildElements(mapEle);
	  		
	  		if(count > 0){
	  		
		  		var gmap = new GMap2(mapEle);
		  		var topLeft = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,40));
		  		gmap.addControl(new GLargeMapControl(), topLeft);
		  		gmap.setCenter(new GLatLng(center[0],center[1]),10);
		  		
				var Icon = new GIcon();
				Icon.image = "/includes/templates/master/images/mappin.png";
				Icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
				Icon.iconSize = new GSize(16, 32);
				Icon.shadowSize = new GSize(35, 32);
				Icon.iconAnchor = new GPoint(8, 32);
				Icon.infoWindowAnchor = new GPoint(9, 10);
				Icon.infoShadowAnchor = new GPoint(18, 23);
				Icon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				Icon.printImage = "coldmarkerie.gif";
				Icon.mozPrintImage = "coldmarkerff.gif";

		  	
	  			for(var i = 0; i < merchant.locations.length; i++){
	  				var location = merchant.locations[i];
	  				var marker = new GMarker(new GLatLng(location.latitude,location.longitude), Icon);
					var displayHTML = "<span style='color: black; font-size: 14px'><strong style='font-size: 16px'>"+merchant.name+"</strong><br/>";
					displayHTML += location.addressLine1+"<br/>";
					if(location.addressLine2 != "")
						displayHTML += location.addressLine2+"<br/>";
					displayHTML += location.addressLine3;
					displayHTML += "</span>";
					
					marker.bindInfoWindowHtml(displayHTML);
					gmap.addOverlay(marker);
	  			}
	  		}
	  		else{
	  			var list = document.createElement("ol");
	  			for(var i = 0; i < merchant.locations.length; i++){
	  				var location = merchant.locations[i];
	  				
	  				var li = document.createElement("li");
	  				li.style.marginTop = "30px";
	  				list.appendChild(li);
	  				li.appendChild(document.createTextNode(location.addressLine1));
	  				li.appendChild(document.createElement("br"));
	  				if(location.addressLine2 != ""){
						li.appendChild(document.createTextNode(location.addressLine2));
	  					li.appendChild(document.createElement("br"));
					}
		  			li.appendChild(document.createTextNode(location.addressLine3));
	  				li.appendChild(document.createElement("br"));
		  		}
		  		mapEle.appendChild(list);
		  		mapEle.style.textAlign = "left";
	  		}
   		} 		
  		
  		return mapEle;
  }
  
  CouponUI.prototype.getMercLocations = function(merchantId, callback){
  		var req = getXMLHttpObject();
	 	req.onreadystatechange = function(){
	 		if(req.readyState == 4){
	 				_MODEL.merchants[merchantId].locations = eval(req.responseText);
	 				callback(merchantId);
	 		}
	 	};
	 	
	 	var openString = "/includes/services/merchant_locations.php?merchantId="+merchantId;
	 	req.open("GET",openString,true);
	 	req.send(null);
  }
  
  CouponUI.prototype.toggleSavedIcon = function(index){
  		var icon = document.getElementById("savedIcon_"+index);
  		var button = document.getElementById("removeButton_"+index);
  		
  		if(!icon){
  			icon = document.createElement("img");
  			icon.src = "/includes/templates/master/images/round_corners/checkmark.png"; 
  			icon.id = "savedIcon_"+index;
  			icon.style.cursor = "pointer";
  			icon.onmouseover = new Function("cui.toggleSavedIcon("+index+");");
  			button.parentNode.replaceChild(icon, button);
  		}
  		else{
			button = document.createElement("img");
			button.src = "/includes/templates/master/images/remove.png";
			button.onmouseout = new Function("cui.toggleSavedIcon("+index+");");
			button.onclick = new Function("cui.unclip("+index+");");
			button.id = "removeButton_"+index;
			button.style.cursor = "pointer";
			icon.parentNode.replaceChild(button, icon);
  		}
  }
  
  CouponUI.prototype.showSignUpMessage = function(index){
  		var message = document.createElement("div");
  		message.style.width = "100%";
  		
  		var padding_title = document.createElement("div");
  		message.appendChild(padding_title);
  		padding_title.style.padding = "5px";
  		
  		padding_title.style.fontSize = "18px";
  		padding_title.style.fontWeight = "bold";
  		
		padding_title.appendChild(document.createTextNode("To save these great deals, you need to create a " + this.model.clientName + " account."));
  		padding_title.appendChild(document.createElement("br"));
  		padding_title.appendChild(document.createElement("br"));
  		
  		var padding = document.createElement("div");
  		message.appendChild(padding);
  		padding.style.padding = "2px";
  		
  		padding.style.fontSize = "16px";
  		padding.style.fontWeight = "normal"; 
  		 		
		padding.appendChild(document.createTextNode("Registration is FREE!"))	
  		padding.appendChild(document.createElement("br"));
  		
  		var signUpImage = document.createElement("img");
  		signUpImage.src = "/includes/templates/master/images/button_getcellfire_register.jpg";
  		signUpImage.style.width = "200px";
  		signUpImage.style.height = "50px";
  		
  		var coupon = this.model.coupons[index];
  		var reg_type = (coupon.isCPG && coupon.isCPG == 1)?"2":"1";
  		var params = "reg_type="+reg_type+"&xclip="+coupon.offerId+"&xmerc="+coupon.merchantId+"&";
  		signUpImage.onclick = new Function("window.location='/getcfnow/index.php?"+params+"'");
  		signUpImage.style.cursor = "pointer";
  		padding.appendChild(signUpImage); 

  		padding.appendChild(document.createElement("br"));
  		padding.appendChild(document.createElement("br"));
  		padding.appendChild(document.createTextNode("Already using Cellfire?"))	
  		padding.appendChild(document.createElement("br"));

/*
  		var loginHolder = document.createElement("div");
  		padding.appendChild(loginHolder);
  		loginHolder.style.margin = "20px 0px 0px 0px";
  		loginHolder.innerHTML = "<a href=\"javascript:cm.showLoginWindow('','/deals.php?clip="+index+"&ctli=2436')\">Already using " + this.model.clientName + "?</a>";
*/
  		var loginImage = document.createElement("img");
  		loginImage.src = "/includes/templates/master/images/13586_thumb_button_biglogin.jpeg";
  		loginImage.style.width = "200px";
  		loginImage.style.height = "50px";
  		
  		loginImage.onclick = new Function("cm.showLoginWindow('','/deals.php?clip="+index+"&ctli=2436')");
  		loginImage.style.cursor = "pointer";
  		padding.appendChild(loginImage);   		
  		
  		var handle = this.windowManager.createNewBlackOutWindow(message, 400,0,0,true);
  		this.windowManager.showWindow(handle);
  
  }

  CouponUI.prototype.showRegForm = function(index, clipOffer){
  		var message = document.createElement("div");
  		message.style.width = "100%";
  		
  		var padding = document.createElement("div");
  		message.appendChild(padding);
  		padding.style.padding = "12px";
  		padding.style.fontSize = "12px";
  		
  		var tSpan = document.createElement("span");
  		padding.appendChild(tSpan);
  		tSpan.style.fontSize = "16px";
  		tSpan.style.fontWeight = "bold";
  		tSpan.appendChild(document.createTextNode("Register for " + this.model.clientName + " - It's FREE!"));
  		padding.appendChild(document.createElement("br"));
  		
  		
  		var rForm = document.createElement("form");
  		rForm.action = "https://" + location.hostname + location.pathname;
  		rForm.method = "POST";
  		//rForm.onsubmit ="alert(123);return false;"
  		padding.appendChild(rForm);
  		
			var iDiv = document.createElement("div");
			iDiv.id = "regForm_i";
			iDiv.appendChild(document.createTextNode("To save these coupons, create your " + this.model.clientName + " account"));
			rForm.appendChild(iDiv);
			rForm.appendChild(document.createElement("br"));

			var coupon;
			var mid;
			if (index != -1)
			{
				coupon = this.model.coupons[index];
				mid = coupon.merchantId;
			}
			else
			{
				coupon = false;
				mid = this.model.iframeMid;
			}	
			var card = this.model.loyaltyCards[mid];			
			var hasAlt = card.alternateTitle.length > 0;	

			var cTitle = document.createElement("div");
			cTitle.style.width = "30%";
			cTitle.style.height = "30px";
			cTitle.style.cssFloat = "left";
			cTitle.style.styleFloat = "left";
			cTitle.style.textAlign = "right";
			cTitle.style.margin = "0px 10px 0px 0px";
			cTitle.id = "regForm_c";
			cTitle.style.fontWeight = "bold";
			cTitle.appendChild(document.createTextNode(card.cardName +  " Number:"));
			rForm.appendChild(cTitle);
			var cElHolder = document.createElement("div");
			cElHolder.style.width = "65%";
			cElHolder.style.height = "30px";
			cElHolder.style.cssFloat = "left";
			cElHolder.style.styleFloat = "left";
			var cEl = document.createElement("input");
			cEl.id = "";
			cEl.name = "cnum";
			cEl.type = "text";
			cEl.size = "20";
			cElHolder.appendChild(cEl);
			rForm.appendChild(cElHolder);
			
			if (hasAlt)
			{
				var caTitle = document.createElement("div");
				caTitle.style.width = "30%";
				caTitle.style.cssFloat = "left";
				caTitle.style.styleFloat = "left";
				caTitle.style.textAlign = "right";
				caTitle.style.margin = "0px 10px 0px 0px";
				caTitle.id = "regForm_ca";
				caTitle.style.fontWeight = "bold";
				caTitle.appendChild(document.createTextNode("Or Associated " + card.alternateTitle +  ":"));
				rForm.appendChild(caTitle);
				var caElHolder = document.createElement("div");
				caElHolder.style.width = "65%";
				caElHolder.style.cssFloat = "left";
				caElHolder.style.styleFloat = "left";
				var caEl = document.createElement("input");
				caEl.id = "";
				caEl.name = "altnum";
				caEl.type = "text";
				caEl.size = "20";
				caElHolder.appendChild(caEl);
				caElHolder.appendChild(document.createElement("br"));
				var caElNote = document.createElement("div");
				caElNote.style.margin = "2px 0px 10px 0px";
				caElNote.style.fontSize = "11px";
				caElNote.appendChild(document.createTextNode("(Use the one you give at checkout)"));
				caElHolder.appendChild(caElNote);
				rForm.appendChild(caElHolder);
			}	
			else
			{
				var noAlt_holder = document.createElement("div");
				noAlt_holder.style.fontSize = "11px";
				noAlt_holder.style.clear = "both";
				var altNum_input = document.createElement("input");
				altNum_input.type = "hidden";
				altNum_input.value = "";
				altNum_input.name = "altnum";
				altNum_input.id = "lc_alt_"+mid;
				noAlt_holder.appendChild(altNum_input);
				var noAltMessage = document.createElement("div");
				noAltMessage.innerHTML = card.supportHTML;
				noAlt_holder.appendChild(noAltMessage);
				rForm.appendChild(noAlt_holder);
			}	
			
			rForm.appendChild(document.createElement("br"));
			
			var pTitle = document.createElement("div");
			pTitle.style.width = "30%";
			pTitle.style.cssFloat = "left";
			pTitle.style.styleFloat = "left";
			pTitle.style.textAlign = "right";
			pTitle.style.margin = "0px 10px 0px 0px";
			pTitle.id = "regForm_p";
			pTitle.style.fontWeight = "bold";
			pTitle.appendChild(document.createTextNode("Mobile Phone #:"));
			pTitle.appendChild(document.createElement("br"));
			var pSpan = document.createElement("span");
			pSpan.style.fontWeight = "normal";
			pSpan.style.fontSize = "11px";
			pSpan.appendChild(document.createTextNode("(will be Account ID)"));
			pTitle.appendChild(pSpan);
			rForm.appendChild(pTitle);
			var pElHolder = document.createElement("div");
			pElHolder.style.width = "65%";
			pElHolder.style.cssFloat = "left";
			pElHolder.style.styleFloat = "left";
			var pEl = document.createElement("input");
			pEl.id = "";
			pEl.name = "reg_cell";
			pEl.type = "text";
			pEl.size = "12";
			pElHolder.appendChild(pEl);
			rForm.appendChild(pElHolder);
			
			var sp1 = document.createElement("div");
			sp1.style.clear = "both";
			sp1.appendChild(document.createElement("br"));
			rForm.appendChild(sp1);

			var yTitle = document.createElement("div");
			yTitle.style.width = "30%";
			yTitle.style.cssFloat = "left";
			yTitle.style.styleFloat = "left";
			yTitle.style.textAlign = "right";
			yTitle.style.margin = "0px 10px 0px 0px";
			yTitle.id = "regForm_y";
			yTitle.style.fontWeight = "bold";
			yTitle.appendChild(document.createTextNode("Year of Birth:"));
			yTitle.appendChild(document.createElement("br"));
			var ySpan = document.createElement("span");
			ySpan.style.fontWeight = "normal";
			ySpan.style.fontSize = "11px";
			ySpan.appendChild(document.createTextNode("(will be Password)"));
			yTitle.appendChild(ySpan);
			rForm.appendChild(yTitle);
			var yElHolder = document.createElement("div");
			yElHolder.style.width = "65%";
			yElHolder.style.cssFloat = "left";
			yElHolder.style.styleFloat = "left";
			var yEl = document.createElement("input");
			yEl.id = "";
			yEl.name = "reg_yob";
			yEl.type = "text";
			yEl.size = "12";
			yElHolder.appendChild(yEl);
			rForm.appendChild(yElHolder);
			var ySpan = document.createElement("span");
			ySpan.style.margin = "0px 0px 0px 20px";
			
			var sp2 = document.createElement("div");
			sp2.style.clear = "both";
			sp2.appendChild(document.createElement("br"));
			rForm.appendChild(sp2);

			var zTitle = document.createElement("div");
			zTitle.style.width = "30%";
			zTitle.style.cssFloat = "left";
			zTitle.style.styleFloat = "left";
			zTitle.style.textAlign = "right";
			zTitle.style.margin = "0px 10px 0px 0px";
			zTitle.id = "regForm_z";
			zTitle.style.fontWeight = "bold";
			zTitle.appendChild(document.createTextNode("Zip Code:"));
			rForm.appendChild(zTitle);
			var zElHolder = document.createElement("div");
			zElHolder.style.width = "65%";
			zElHolder.style.cssFloat = "left";
			zElHolder.style.styleFloat = "left";
			var zEl = document.createElement("input");
			zEl.id = "";
			zEl.name = "reg_zip";
			zEl.type = "text";
			zEl.size = "12";
			zElHolder.appendChild(zEl);
			rForm.appendChild(zElHolder);
			
			var sp3 = document.createElement("div");
			sp3.style.clear = "both";
			sp3.appendChild(document.createElement("br"));
			rForm.appendChild(sp3);

			var gTitle = document.createElement("div");
			gTitle.style.width = "30%";
			gTitle.style.cssFloat = "left";
			gTitle.style.styleFloat = "left";
			gTitle.style.textAlign = "right";
			gTitle.style.margin = "0px 10px 0px 0px";
			gTitle.id = "regForm_g";
			gTitle.style.fontWeight = "bold";
			gTitle.appendChild(document.createTextNode("Gender:"));
			gTitle.appendChild(document.createElement("br"));
			var gSpan = document.createElement("span");
			gSpan.style.fontWeight = "normal";
			gSpan.style.fontSize = "11px";
			gSpan.appendChild(document.createTextNode("(optional)"));
			gTitle.appendChild(gSpan);
			rForm.appendChild(gTitle);
			
			var gElHolder = document.createElement("div");
			gElHolder.style.width = "65%";
			gElHolder.style.cssFloat = "left";
			gElHolder.style.styleFloat = "left";
			var rMale;
			try
			{
				rMale = document.createElement('<input type="radio" name="reg_gender"/>');
			}
			catch (e)
			{
				rMale = document.createElement("input");
				rMale.setAttribute("name", "reg_gender");
				rMale.setAttribute("type", "radio");
			}	
			rMale.setAttribute("value", "1");
			gElHolder.appendChild(rMale);
			gElHolder.appendChild(document.createTextNode("Male"));
			
			var rFemale;
			try
			{
				rFemale = document.createElement('<input type="radio" name="reg_gender"/>');
			}
			catch (e)
			{
				rFemale = document.createElement("input");
				rFemale.setAttribute("name", "reg_gender");
				rFemale.setAttribute("type", "radio");
			}	
			rFemale.setAttribute("value", "2");
			gElHolder.appendChild(rFemale);
			gElHolder.appendChild(document.createTextNode("Female"));
			
			rForm.appendChild(gElHolder);
	
			var sp4 = document.createElement("div");
			sp4.style.clear = "both";
			sp4.appendChild(document.createElement("br"));
			rForm.appendChild(sp4);
			
			var emTitle = document.createElement("div");
			emTitle.style.width = "30%";
			emTitle.style.cssFloat = "left";
			emTitle.style.styleFloat = "left";
			emTitle.style.textAlign = "right";
			emTitle.style.margin = "0px 10px 0px 0px";
			emTitle.id = "regForm_em";
			emTitle.style.fontWeight = "bold";
			emTitle.appendChild(document.createTextNode("Email:"));
			emTitle.appendChild(document.createElement("br"));
			var emSpan = document.createElement("span");
			emSpan.style.fontWeight = "normal";
			emSpan.style.fontSize = "11px";
			emSpan.appendChild(document.createTextNode("(optional)"));
			emTitle.appendChild(emSpan);
			rForm.appendChild(emTitle);
			var emElHolder = document.createElement("div");
			emElHolder.style.width = "65%";
			emElHolder.style.cssFloat = "left";
			emElHolder.style.styleFloat = "left";
			emElHolder.style.fontSize = "11px";
			var emEl = document.createElement("input");
			emEl.id = "";
			emEl.name = "reg_email";
			emEl.type = "text";
			emEl.size = "20";
			emElHolder.appendChild(emEl);

			emElHolder.appendChild(document.createElement("br"));

			var cOpt;
			try
			{
				cOpt = document.createElement('<input type="checkbox" name="reg_emailopt" checked/>');
			}
			catch (e)
			{
				cOpt = document.createElement("input");
				cOpt.setAttribute("name", "reg_emailopt");
				cOpt.setAttribute("type", "checkbox");
			}	
			cOpt.setAttribute("value", "1");
			cOpt.setAttribute("checked", "true");
			emElHolder.appendChild(cOpt);
			emElHolder.appendChild(document.createTextNode("Send me alerts when new offers are available on " + this.model.clientName + ""));
			
			if (this.model.iframe3pe)
			{
				emElHolder.appendChild(document.createElement("br"));

				var mOpt;
				try
				{
					mOpt = document.createElement('<input type="checkbox" name="reg_mercopt"/>');
				}
				catch (e)
				{
					mOpt = document.createElement("input");
					mOpt.setAttribute("name", "reg_mercopt");
					mOpt.setAttribute("type", "checkbox");
				}	
				mOpt.setAttribute("value", "1");
				emElHolder.appendChild(mOpt);
				emElHolder.appendChild(document.createTextNode("Sign up for the " + card.merchantName + " Email Savings Program and be notified by " + card.merchantName + " of weekly specials, recipe ideas, " + card.cardName + " program updates, and much more."));
			}
			
			emElHolder.appendChild(document.createElement("br"));
			emElHolder.appendChild(document.createElement("br"));


			emElHolder.appendChild(document.createTextNode("By selecting Register, you agree to the "));

			var tos = document.createElement("a");
			if (this.model.iframeCssId == 'vzn')
			{
				tos.href = "javascript:loadTos()";			
			}
			else
			{
				tos.href = "/terms_service.php";
				tos.target = "_blank";
			}	
			tos.appendChild(document.createTextNode("Terms of Service"));
			emElHolder.appendChild(tos);

			emElHolder.appendChild(document.createTextNode(" and "));

			var privPolicy = document.createElement("a");
			if (this.model.iframeCssId == 'vzn')
			{
				privPolicy.href = "http://www.verizon.com/privacy";
			}
			else
			{
				privPolicy.href = "/privacy_policy.php";
			}	
			privPolicy.target = "_blank";
			privPolicy.appendChild(document.createTextNode("Privacy Policy"));
			emElHolder.appendChild(privPolicy);
			
			emElHolder.appendChild(document.createTextNode("."));
			

			emElHolder.appendChild(document.createElement("br"));
			emElHolder.appendChild(document.createElement("br"));

  		var signUpImage = document.createElement("input");
  		signUpImage.type = "image";
  		signUpImage.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_register.jpg";
  		if (this.model.inIframe)
  		{
			var merchant = this.model.merchants[this.model.iframeMid];
  			signUpImage.onclick="pageTracker._trackEvent('Popups', 'Submit', '" + merchant.mid + "_QFrame_Reg')";
  			//signUpImage.onclick=new Function("alert('"+merchant.name+ "')");
  		}
  		
			var reg_type = 2;
			if (index != -1)
			{
  			reg_type = (coupon.isCPG && coupon.isCPG == 1)?"2":"1";
  		}	

  		var rId = document.createElement("input");
  		rId.type = "hidden";
  		rId.name = "reg_type";
  		rId.value = reg_type;
  		
  		var oId = document.createElement("input");
  		oId.type = "hidden";
  		oId.name = "xclip";
  		if (clipOffer)
  		{
  			oId.value = coupon.offerId;
  		}
  		else
  		{
  			oId.value = '';
  		}	
  		
  		var mId = document.createElement("input");
  		mId.type = "hidden";
  		mId.name = "xmerc";
  		mId.value = mid;
  		
  		var cId = document.createElement("input");
  		cId.type = "hidden";
  		cId.name = "clip";
  		if (clipOffer)
  		{
	  		cId.value = coupon.couponId;
	  	}
	  	else
	  	{
	  		cId.value = '';
	  	}	
  		
  		var ctId = document.createElement("input");
  		ctId.type = "hidden";
  		ctId.name = "ctli";
  		ctId.value = "1234";
  		
  		emElHolder.appendChild(signUpImage); 
  		emElHolder.appendChild(rId);
  		emElHolder.appendChild(oId);
  		emElHolder.appendChild(mId);
  		emElHolder.appendChild(cId);
  		emElHolder.appendChild(ctId);
  		
  		var loginHolder = document.createElement("div");
  		emElHolder.appendChild(loginHolder);
  		loginHolder.style.margin = "20px 0px 10px 0px";
  		loginHolder.innerHTML = "Already using " + this.model.clientName + "? <a href=\"javascript:cm.showLoginWindow('', '" + location.pathname + "?clip="+index+"&ctli=2436')\">Log in</a>";
  		
			rForm.appendChild(emElHolder);

			var sp5 = document.createElement("div");
			sp5.style.clear = "both";
			rForm.appendChild(sp5);
			
  		var handle = this.windowManager.createNewWhiteOutWindow(message, 500, "ih_" + index,true);
  		// var handle = this.windowManager.createNewBlackOutWindow(message, 500, 0, 0, true);
  		this.windowManager.showWindow(handle);
  
  }

	CouponUI.prototype.setPrintOption = function(printOption)
	{
		this.showPrintOption = printOption;
	}	
	
	CouponUI.prototype.setFeaturedOffers = function(featuredOffers)
	{
		this.featuredOffers = featuredOffers;
	}	
	
	CouponUI.prototype.setHomePage = function(homePage)
	{
		this.homePage = homePage;
	}	
	
	CouponUI.prototype.setMerchantPage = function(merchantPage)
	{
		this.merchantPage = merchantPage;
	}	
	
	CouponUI.prototype.drawFeaturedMerchant = function(){
		this.featuredOffers = true;
	
		var displayArea = document.getElementById(this.displayEleId);
		var merchantHolder = document.getElementById("mh_featured");

		if(!merchantHolder){
			merchantHolder = document.createElement("div");
			merchantHolder.style.margin = "0px 0px 10px 0px";
			displayArea.appendChild(merchantHolder);
			merchantHolder.id = "mh_featured";
			merchantHolder.className = "solid_top_gray";
			}

		removeChildElements(merchantHolder);

		var merchantContent = document.createElement("div");
		var merchantTitle = document.createElement("div");
		merchantTitle.style.padding = "2px 0px";

		var footer = document.createElement("div");
		var round_corners = applyHeaderFooterBox(merchantTitle,merchantContent);
		merchantHolder.appendChild(round_corners);
		merchantContent.id = "mc_featured";	

		var floatRight = document.createElement("div");
		floatRight.style.cssFloat = "right";
		floatRight.style.styleFloat = "right";
		floatRight.style.textAlign = "right";
		floatRight.style.margin = "6px 0px 0px 0px";

		var floatLeft = document.createElement("div");
		floatLeft.style.cssFloat = "left";
		floatLeft.style.styleFloat = "left";
		floatLeft.style.textAlign = "left";

		merchantTitle.appendChild(floatRight);
		merchantTitle.appendChild(floatLeft);
		
		var cityState = "";
		if (this.model && this.model.user.displayLoc[0])
		{
			cityState = " in " + this.model.user.displayLoc[0];
			if (this.model.user.displayLoc[1])
			{
				cityState += ", " + this.model.user.displayLoc[1];
			}
			if (this.model.user.currentSearch[0])
			{
				cityState += " " + this.model.user.currentSearch[0];
			}
		}	

		var titleText = document.createElement("div");
		titleText.style.fontSize = "16px";
		titleText.style.fontWeight = "bold";
		titleText.style.padding = "2px 0px 0px 0px";
		titleText.appendChild(document.createTextNode("Featured Offers" + cityState));
		floatLeft.appendChild(titleText);
		
		titleText = document.createElement("a");
		titleText.style.fontSize = "12px";
		titleText.style.fontWeight = "bold";
		titleText.style.padding = "2px 0px 0px 0px";
		titleText.href = "/deals.php";
		titleText.appendChild(document.createTextNode("See All Offers"));
		floatRight.appendChild(titleText);

		var clear = document.createElement("div");
		clear.className = "clear";
		merchantTitle.appendChild(clear);

		var merchantContent = document.getElementById("mc_featured");
		if(merchantContent) removeChildElements(merchantContent);

	}
	
	CouponUI.prototype.getTitleMain = function(origTitle)
	{
		var title = origTitle;
		if (origTitle.indexOf("Save") > -1)
		{
			title = origTitle.substring(0, origTitle.indexOf("Save"));
		}
		return title;
	}	

	CouponUI.prototype.getTitleSavings = function(origTitle)
	{
		var title = "";
		if (origTitle.indexOf("Save") > -1)
		{
			title = origTitle.substring(origTitle.indexOf("Save"));
		}
		return title;
	}
	
	CouponUI.prototype.shareOnFB = function(index, whichButton)
	{
		// alert('FB SHARE');
		var offer = this.model.coupons[index];
		if (offer)
		{
			//customPageTracker('fb_connect_click');
			//var imgLoc = document.getElementById("fbc_button");
			//imgLoc.src = "/includes/templates/master/images/loader_small_red.gif";
			// imgLoc.style.cursor = "default";
			this.model.setFBIndex(index);
			this.model.setFBButton(whichButton);
			customPageTracker('fb_connect_click_' + this.model.fbButton);
			
			FB.Bootstrap.requireFeatures(["Connect"], function()
			{
				FB.Facebook.init("b14533a4f141c79f2968217bb766937d", "/xd_receiver.htm");
				FB.Connect.ifUserConnected(cui.showFBdialog, cui.notLoggedIn);
			});
		}	
	}
	
	CouponUI.prototype.notLoggedIn = function()
	{
		FB.Connect.requireSession(cui.showFBdialog);
	}	
	
	CouponUI.prototype.showFBdialog = function()
	{
		var index = this.model.fbIndex;
		// alert('FB SHARE 2 - index = ' + index);
		var offer = this.model.coupons[index];
		if (offer)
		{
			var bundleId = this.model.getFBBundleId(index);
			var tlc = this.model.getFBtlc(index);
			customPageTracker('fb_connect_show_dialog_' + this.model.fbButton + '_' + tlc);
			var merchantCard = "";
			if (offer.isCPG && offer.isCPG != "0")
			{
				merchantCard = this.model.merchants[offer.merchantId].name + " " + this.model.loyaltyCards[offer.merchantId].cardName;
			}
			var currDomain = document.domain;
			if (currDomain.indexOf("timh") == 0)
			{
				currDomain = "www.cellfire.com";
			}	
			var template_data = 
				{
				"offer_url": "http://" + currDomain + "/external/" + tlc + "/" + offer.offerId,
				"offer_title": offer.title,
				"merchant_card": merchantCard,
				"merchant_name": this.model.merchants[offer.merchantId].name,
				"cf_link_url": "http://" + currDomain + "/" + tlc,
				"cf_deals_url" : "http://" + currDomain + "/deals.php?a=" + tlc,
				"images":[{"src":"http://" + currDomain + "/images/" + offer.imageId + ".jpg",
				"href":"http://" + currDomain + "/external/" + tlc + "/" + offer.offerId}]
				}		
			// alert("ABOUT TO CONNECT\nBUNDLE ID = " + bundleId + "\nTEMPLATE_DATA = " + template_data);	
			FB.Connect.showFeedDialog(bundleId, template_data);
		}	
	}	
	
CouponUI.prototype.changeStore = function(showIntro)
{
		var tAlign = "left";
		/*
		if (!showIntro)
		{
			tAlign = "left";
		}	
		*/
		var message = document.createElement("div");
		message.style.width = "100%";
		
		var padding = document.createElement("div");
		message.appendChild(padding);
		padding.style.padding = "12px";
		padding.style.fontSize = "12px";

		if (showIntro)
		{
			var tImage = document.createElement("img");
			tImage.src = "/images/affiliate/mm_powered.png";
			tImage.style.width = "400px";
			tImage.style.height = "37px";
			padding.appendChild(tImage);
			padding.appendChild(document.createElement("br"));
			padding.appendChild(document.createElement("br"));
			var sSpan = document.createElement("span");
			sSpan.style.fontWeight = "bold";
			sSpan.appendChild(document.createTextNode(this.model.clientName + " makes coupons simple!"));
			padding.appendChild(sSpan);
			padding.appendChild(document.createElement("br"));
			var ulist = document.createElement("ul");
			padding.appendChild(ulist);
			var li2 = document.createElement("li");
			li2.appendChild(document.createTextNode("Save coupons to your grocery savings card"));
			var li3 = document.createElement("li");
			li3.appendChild(document.createTextNode("Swipe your card at checkout to redeem!"));
			var li4 = document.createElement("li");
			li4.appendChild(document.createTextNode(this.model.clientName + " coupons work at thousands of grocery stores across the country"));
			var li5 = document.createElement("li");
			li5.appendChild(document.createTextNode(this.model.clientName + " coupons are accessible from the web, your mobile phone, and FiOS TV"));
			ulist.appendChild(li2);
			ulist.appendChild(li3);
			ulist.appendChild(li4);
			if (this.model.clientName == "Spend Smart")
			{
				ulist.appendChild(li5);
			}	
		}
		else
		{
			var tSpan = document.createElement("span");
			padding.appendChild(tSpan);
			tSpan.style.fontSize = "16px";
			tSpan.style.fontWeight = "bold";
			tSpan.appendChild(document.createTextNode("Change Store"));
		}	
		padding.appendChild(document.createElement("br"));
		
		var iDiv = document.createElement("div");
		iDiv.style.textAlign = tAlign;
		padding.appendChild(iDiv);
		padding.appendChild(document.createElement("br"));
		var iSpan = document.createElement("span");
		iSpan.style.fontWeight = "bold";
		iSpan.appendChild(document.createTextNode("To view available coupons, select a grocer:"));
		iDiv.appendChild(iSpan);
		/*
		var iSpan = document.createElement("span");
		iSpan.id = "change_store_is";
		if (showIntro && this.model.hasLocalMerchants())
		{
			iSpan.appendChild(document.createTextNode(" in your area"));
		}
		iDiv.appendChild(iSpan);
		iDiv.appendChild(document.createTextNode(":"));
		*/

		var rForm = document.createElement("form");
		rForm.action = "http://" + location.hostname + location.pathname;
		rForm.method = "POST";
		padding.appendChild(rForm);
		
		if (showIntro && this.model.hasLocalMerchants())
		{
			var icDiv = document.createElement("div");
			icDiv.id = "change_store_ic";
			icDiv.style.margin = "0px auto";
			icDiv.style.textAlign = "left";
			rForm.appendChild(icDiv);
			
			var iciDiv = document.createElement("div");
			icDiv.appendChild(iciDiv);
			iciDiv.style.width = "400px";

			// show local merchants via logos
			var locals = this.model.getLocalMerchants();
			var imgObject,imgDiv;
			for (var j in locals)
			{
				imgDiv = document.createElement("div");
				iciDiv.appendChild(imgDiv);
				imgDiv.style.cssFloat = "left";
				imgDiv.style.styleFloat = "left";
				imgDiv.style.height = "64px";
				imgDiv.style.width = "124px";
				imgDiv.style.padding = "2px";
				imgObject = document.createElement("input");
				imgObject.type = "image";
				imgObject.name = "change_merchant";
				imgObject.value = locals[j];
				imgObject.src = "/images/" + this.model.merchants[locals[j]].bannerId + "_120_60.jpg";
				imgObject.style.height = "60px";
				imgObject.style.width = "120px";
				imgDiv.appendChild(imgObject);
			}	
			var cDiv = document.createElement("div");
			cDiv.className = "clear";
			iciDiv.appendChild(cDiv);
			var toDropDown = document.createElement("a");
			toDropDown.href = "javascript:cui.flipChangeStore()";
			toDropDown.appendChild(document.createTextNode("Choose from more grocers"));
			icDiv.appendChild(toDropDown);
		}
		// show all merchants via drop down
		var ddDiv = document.createElement("div");
		ddDiv.id = "change_store_dd";
		ddDiv.style.textAlign = tAlign;
		if (showIntro && this.model.hasLocalMerchants())
		{
			ddDiv.style.display = "none";
		}	
		rForm.appendChild(ddDiv);

		var sDropDown = document.createElement("select");
		sDropDown.name = "change_merchant";
		sDropDown.id = "cm_drop_down";
		ddDiv.appendChild(sDropDown);

		if (showIntro && this.model.hasLocalMerchants())
		{
			ddDiv.style.display = "none";
			sDropDown.name="not_used_change_merchant";
		}	

		var sOption, mId;
		var listOfMerchs = model.getUnassocBulkMerchants()
		if (listOfMerchs)
		{
			for (var j in listOfMerchs)
			{
				mId = listOfMerchs[j];
				sOption = document.createElement("option");
				sOption.value = mId;
				sOption.appendChild(document.createTextNode(this.model.loyaltyCards[mId].merchantName));
				sDropDown.appendChild(sOption);
			}	
		}	
		ddDiv.appendChild(document.createElement("br"));
		
		var csiDiv = document.createElement("div");
		ddDiv.appendChild(csiDiv)
		csiDiv.style.margin = "5px 0px 0px 0px";

		var changeStoreImage = document.createElement("input");
		changeStoreImage.type = "image";
		changeStoreImage.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_select.png";

		csiDiv.appendChild(changeStoreImage);
		
		if (showIntro && this.model.clientName != "Spend Smart")
		{
			var poweredDiv = document.createElement("div");
			padding.appendChild(poweredDiv);
			poweredDiv.style.cssFloat = "right";
			poweredDiv.style.styleFloat = "right";
			poweredDiv.style.width = "155px";
			var pImg = document.createElement("img");
			pImg.style.width = "150px";
			pImg.style.height = "23px";
			pImg.src = "/images/affiliate/powered_by_cf.png"
			poweredDiv.appendChild(pImg);
		}	

		var handle = this.windowManager.createNewWhiteOutWindow(message, 500, "change_store",true);
		// var handle = this.windowManager.createNewBlackOutWindow(message, 500, 0, 0, true);
		this.windowManager.showWindow(handle);

}
	
CouponUI.prototype.flipChangeStore = function()
{
	var icDiv = document.getElementById("change_store_ic");	
	var ddDiv = document.getElementById("change_store_dd");	
	// var iSpan = document.getElementById("change_store_is");	
	var dd = document.getElementById("cm_drop_down");
	if (dd)
	{
		dd.name = "change_merchant";
	}	
	if (icDiv)
	{
		icDiv.style.display = "none";
	}
	if (ddDiv)
	{
		ddDiv.style.display = "block";
	}	
	/*
	if (iSpan)
	{
		iSpan.style.display = "none";
	}	
	*/
}

CouponUI.prototype.goToSelf = function(xtraParam)
{
	var cLoc = location.href;
	alert(cLoc);
	if (cLoc.indexOf('?') > -1)
	{
		cLoc += "&" + xtraParam;
	}
	else
	{
		cLoc += "?" + xtraParam;
	}
	location.href = cLoc;
}

CouponUI.prototype.goToCoreSelf = function (extraParam)
{
	var cLoc = this.getCoreSelf(extraParam);
	location.href = cLoc;
}	

CouponUI.prototype.getCoreSelf = function(extraParam)
{
	var cLoc = location.href;
	if (cLoc.indexOf('?'))
	{
		cLoc = cLoc.substring(0, cLoc.indexOf('?'));
	}
	if (extraParam != null && extraParam.length > 0)
	{
		cLoc += "?" + extraParam;
	}	
	return cLoc;
}	

CouponUI.prototype.getFormattedPhone = function(phoneNumber)
{
	return "(" + phoneNumber.substring(0,3) + ") " + phoneNumber.substring(3,6) + "-" + phoneNumber.substring(6);
}	
