/**
 * Author Jonathan Samples
 * August 21, 2008
 * 
 * CouponUI Class 
 * Manage the display and interaction with the User for offers
 */
 
 /**
  * CouponUI constructor
  */

function in_array(needle, haystack)
{
    for(var key in haystack)
    {
        if(needle === haystack[key])
        {
            return true;
        }
    }

    return false;
}

function clickdetailfunc(index)
{
	var deets = document.getElementById('dt_' + index);
	var img = document.getElementById('img_' + index);
	if (deets)
	{
		deets.style.display = 'inline';
	}
	if (img)
	{
		img.style.opacity = "0.2";
		img.style.filter = "alpha(opacity=20)";
	}
}
function hidedetailfunc(index)
{
	var deets = document.getElementById('dt_' + index);
	var img = document.getElementById('img_' + index);
	if (deets)
	{
		deets.style.display = 'none';
	}
	if (img)
	{
		img.style.opacity = "1";
		img.style.filter = "alpha(opacity=100)";
	}
}  					

 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.showIntro = true;
	
	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);
		this.model.registerEventListener("ExAdsOfferChange", this);
		this.model.registerEventListener("ShoppingListSent", this);
		this.model.registerEventListener("ClippingwithLoyaltyCard", 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.setShowIntro = function(showIntro){
	this.showIntro = showIntro;
  }
  
   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){ 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);
		  	}
		  	
		  	var addClear = document.createElement("div");
		  	addClear.className = "clear";
		  	displayArea.appendChild(addClear);
		  	
		  	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)
  		{
				this.drawMMFHeader(this.model.iframeMid);
				if (!this.model.mmfSingleMode)
				{
					var merchant = this.model.merchants[this.model.iframeMid];
					var floatLeft = document.createElement("div");
					floatLeft.style.textAlign = "left";

					header.appendChild(floatLeft);

					if(merchant.logoId && merchant.logoId != "-1")
					{
						var logo = document.createElement("img");
						logo.src = "/images/"+merchant.logoId+".png";
						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);
					}
				}
  		}
  		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.drawMMFHeader = function(merchantId)
  {
		var merchant = this.model.merchants[merchantId];
		var displayArea = document.getElementById(this.displayEleId);
		var mmf_header = document.createElement("div");
		mmf_header.style.padding = "0px 0px 15px 0px";
		displayArea.appendChild(mmf_header);
		// draw change store
		var changeStore = document.createElement("div");
		changeStore.id = "change_store_area";
		mmf_header.appendChild(changeStore);
		var csButton = document.createElement("img");
		csButton.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_changestore.png";
		csButton.onclick = new Function("cui.changeStore(false)");
		if (!this.model.mmfSingleMode)
		{
			changeStore.appendChild(csButton);
		}

		var title = document.createElement("div");
		title.id = "mmf_head_title";
 		mmf_header.appendChild(title);
		if (this.model.user.authenticated)
		{
			title.innerHTML="<b>("+this.model.user.phoneNumber.substring(0,3)+") "+this.model.user.phoneNumber.substring(3,6)+"-"+this.model.user.phoneNumber.substring(6,10)+"</b> | ";			
			var logIn = document.createElement("a");
	  	logIn.appendChild(document.createTextNode("Log Out"));
			//logOut.href = "javascript: cui.goToCoreSelf('lo=')";
			logIn.href = "javascript:wm.setLastAnchor('mm_logout');cui.goToCoreSelf('lo=')";
		}
		else
		{  		
			//title.style.fontWeight ="bold";
			title.innerHTML="<b>Welcome Guest!</b> | ";
			//new Function("wm.setLastAnchor('mmf_reg_button');cui.showRegForm(-1, false);");
			var register = document.createElement("a");
			register.appendChild(document.createTextNode("Join " + this.model.clientName ));
			register.id = "mm_login";
			register.href = "javascript:wm.setLastAnchor('mmf_reg_button');cui.showRegForm(-1, false);";
			title.appendChild(register);
			title.appendChild(document.createTextNode(" "));
			if(this.model.cfGuest)
			{
				var whyCellfire= document.createElement("a");
				whyCellfire.href = "javascript:cui.whyCellfirePopup()";
				whyCellfire.appendChild(document.createTextNode("(?)"));
				title.appendChild(whyCellfire);
			}
			// title.appendChild(document.createTextNode(" | Already a " + this.model.clientName + " Member? "));
			title.appendChild(document.createTextNode(" | "));
			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() + "')";
		}
		title.appendChild(logIn);
		
		// zip code changer
		var zipHolder = document.createElement("div");
		zipHolder.id = "mmf_zip_holder";
		mmf_header.appendChild(zipHolder); 	  	 	  		

		var zip_fm = document.createElement("form");
		zipHolder.appendChild(zip_fm);
		var url = document.location.href.split("?");
		zip_fm.action = url[0];
		zip_fm.method = "POST";
		zip_fm.id = "zip_fm";
		zip_fm.name = "zip_fm";
  		  	  		
		var zipTitle =document.createElement("div");
		zipTitle.id = "mmf_zip_title";
		zipTitle.innerHTML = "Zip Code";
		zip_fm.appendChild(zipTitle);

		var zip_int = document.createElement("input");
		zip_int.id = "cl_zipcode";
		zip_int.name = "cl_zipcode";
		zip_int.value = this.model.user.currentSearch[0];
		zip_int.maxLength="5";
		zip_int.size="5";
		zip_fm.appendChild(zip_int);

		var onclickFunc  = function(){
			if(validate_zip(document.getElementById("cl_zipcode").value)) 
				document.getElementById("zip_fm").submit();
			else 
				alert("Please enter a 5-digit zip code.");
			};
		
		var zipButton = document.createElement("img");
		zipButton.id = "mmf_zip_button";
		zipButton.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_go.png";
		zipButton.onclick = onclickFunc;
		zip_fm.appendChild(zipButton);

		
  		var banner_holder = document.createElement("div");
		var cssPrepend = this.model.iframeCssId;
  		if(!this.model.user.authenticated){
  			if(this.model.mmfWidth==7){
  	 			if(this.model.cfGuest)
	  		  		banner_holder.className = cssPrepend + "_banner_7_auth";
	  			else
	  				banner_holder.className = cssPrepend + "_banner_7_nog";  			
  	 		}else{
	  			if(this.model.cfGuest)
	  		  		banner_holder.className = cssPrepend + "_banner_auth";
	  			else
	  				banner_holder.className = cssPrepend + "_banner_nog";
  			}
  		}else if(this.model.user.authenticated && this.model.user.loyaltyCards[merchantId].cardNum==""){
  			if(this.model.mmfWidth==7)
  				banner_holder.className = cssPrepend + "_banner_7_auth";
  			else
  				banner_holder.className = cssPrepend + "_banner_auth";
  		}
  		// mmf_header.appendChild(banner_holder);
		
  }
  CouponUI.prototype.whyCellfirePopup  = function(){
		var content = document.createElement("div");
		content.style.padding ="10px 0px";		
		var title = document.createElement("div");
		title.style.padding = "5px 0px";
		title.style.fontSize="16px";
		title.style.fontWeight = "bold";
		title.style.textAlign = "left";
		title.innerHTML ="Why Join "+this.model.clientName+"?";
		var note = document.createElement("div");
		note.style.textAlign = "left";
		note.style.fontSize="14px";		
		note.innerHTML = "You don't need to be a "+this.model.clientName+" member to clip coupons, but becoming a member earns you all kinds of perks:";
		var list = document.createElement("div");
		list.style.textAlign = "left";
		list.style.fontSize="14px";
		list.innerHTML = "<ul><li>We'll remember all your savings numbers for you so you don't have to re-enter them next time.</li>"
						+"<li>Receive email notifications when new offers are available and when saved coupons are about to expire.</li>"
						+"<li>Access your account on the web or using your mobile phone for extra convenience.</li>"
						+"<li>Get statments that track the $$$ you save using "+this.model.clientName+"</li>"
						+"<li>Receive special high value promotions and coupons only available to "+this.model.clientName+" members</li>"
						+"<li>It's FREE!</li></ul>";
		var buttonDiv = document.createElement("div");
		buttonDiv.style.height = "60px";
		buttonDiv.style.textAlign = "center";
  		var signUpImage = document.createElement("img");
  		buttonDiv.appendChild(signUpImage);
			if(this.model.inMMF)//in qframe
			{
				signUpImage.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_register.png";
			}
			else
			{
				signUpImage.src = "/images/home/button_join_now.png";
				signUpImage.style.width = "140px";
				signUpImage.style.height = "30px";
			}
  		if(this.model.inIframe) signUpImage.onclick = new Function("cui.showRegForm(-1,0);");
  		else signUpImage.onclick =  new Function("window.location='/getcfnow/index.php';");
  		signUpImage.style.cursor = "pointer";
  		
		content.appendChild(title);
		content.appendChild(note);
		content.appendChild(list);
		content.appendChild(buttonDiv); 						
		
		if(this.model.inIframe)//in qframe
			var handle = this.windowManager.createNewWhiteOutWindow(content, 420, "why_cellfire",true);
		else
			var handle = this.windowManager.createNewBlackOutWindow(content, 420, 0, 0, true);
		
  		this.windowManager.showWindow(handle);	  
  }
  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(this.model.inMMF){
  			// add the section above coupon display area
  			var mmf_header = document.createElement("div");
  			this.drawMMFHeader(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.style.textAlign = "center";
		
		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.appendChild(floatLeft);
		
		if(merchant.logoId && merchant.logoId != "-1"){
			var logo = document.createElement("img");
			logo.src = "/images/"+merchant.logoId+".png";
			logo.style.cssFloat = "left";
			logo.style.styleFloat = "left";
			//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";
			}	
			if (!this.model.mmfSingleMode)
			{
				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));
			if (!this.model.mmfSingleMode)
			{
  			floatLeft.appendChild(titleText);
			}
		}

		var floatRightMargin = "5px 0px";
		var floatRightWidth = "300px";
		if (this.homePage)
		{
			floatRightWidth = "30px";
		}	
		else if (this.model.inMMF)
		{
			floatRightMargin = "10px 0px 5px 0px";
			floatRightWidth = "250px";
			if (this.model.mmfWidth == 7)
			{
				floatRightWidth = "125px";
			}	
		}
		
		var floatRight = document.createElement("div");
		floatRight.style.cssFloat = "right";
		floatRight.style.styleFloat = "right";
		floatRight.style.textAlign = "right";
		floatRight.style.width = floatRightWidth;
		floatRight.style.margin = floatRightMargin;
		merchantTitle.appendChild(floatRight);
		
		var clear = document.createElement("div");
		clear.className = "clear";
		merchantTitle.appendChild(clear);
		
		if (this.model.inMMF)
		{
			var catDiv = document.createElement("div");
			catDiv.id = "category_display_area";
			floatRight.appendChild(catDiv);
			
			var saveAmt = 0;
			// figure out amount of savings!
			for(var i in this.model.couponIdDisplayList)
			{
				var coupon = this.model.coupons[this.model.couponIdDisplayList[i]];
				if(!coupon) continue;
				if(coupon.merchantId == merchantId)
				{
					saveAmt += parseFloat(coupon.faceValue);
				}
			}
			
			var saveDiv = document.createElement("div");
			saveDiv.id = "save_amount_area";
			floatLeft.appendChild(saveDiv);
			var sdSpan = document.createElement("span");
			sdSpan.appendChild(document.createTextNode('$' + saveAmt.toFixed(2)));
			saveDiv.appendChild(sdSpan);
			saveDiv.appendChild(document.createTextNode(' in savings on this page'));
		}
		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);
				*/
				var locButton = document.createElement("span");
				locButton.align = "absmiddle";
				locButton.style.margin = "0px 0px 0px 6px";
				locButton.style.cursor = "pointer";
				locButton.style.fontWeight = "bold";
				locButton.style.textDecoration = "underline";
				locButton.innerHTML = "Store Locations";
				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);
		}
		if(merchant.isCPG) this.getMerchantTile(merchant);

  }
  
  CouponUI.prototype.drawCoupon = function(index)
  {
		var area = this.model.coupons[index].merchantId;
 		var displayArea = document.getElementById("mc_" + area);
		var couponHolder = document.getElementById("ch_"+index);

		if(!couponHolder){
			couponHolder = document.createElement("div");
			couponHolder.id = "ch_"+index;
			displayArea.appendChild(couponHolder);

		}

		// Find our canvas
		removeChildElements(couponHolder);
		var coupon = this.model.coupons[index];
		var isFreeOffer = (coupon.title.toLowerCase().substring(0,5)=="free " || coupon.title.toLowerCase().substring(coupon.title.length-5,coupon.title.length)==" free");
		
		var merchant = this.model.merchants[coupon.merchantId];
		var	mDisplayMode = merchant.displayMode;
		// Tiled view

//   			imageHolder.onclick = new Function("pageTracker._trackEvent('OfferDetails', 'View_Grid', '"+index+"');cui.showCouponDetails("+index+")");

		couponHolder.className = "couponContainer";

		var topShell = document.createElement("div");
		couponHolder.appendChild(topShell);
		topShell.onmouseover = new Function("clickdetailfunc("+index+")");
		topShell.onmouseout = new Function("hidedetailfunc("+index+")");

		var couponTop = document.createElement("div");
		topShell.appendChild(couponTop);
		couponTop.className = "couponTop";

		var imageId = this.model.coupons[index].imageId;
		var image = document.createElement("img");
		couponTop.appendChild(image);
		image.id = "img_" + index;
		image.src = "/images/"+imageId+".jpg";
		image.onclick = new Function("pageTracker._trackEvent('OfferDetails', 'View_Grid', '"+coupon.offerId+"');cui.showCouponDetails("+index+")");
		

		if(imageId == 0 || imageId == -1)
			image.src = "/includes/templates/master/images/tag_100x100.gif";

		var couponText = document.createElement("div");
		couponText.style.cursor = "pointer";
		couponText.className = "couponText";
		couponTop.appendChild(couponText);

		var titleText = coupon.title;
		if(titleText.length > 33)
		{
			titleText = titleText.substring(0,30)+"...";
		}	
		couponText.innerHTML = titleText;
		couponText.onclick = new Function("pageTracker._trackEvent('OfferDetails', 'View_Grid', '"+coupon.offerId+"');cui.showCouponDetails("+index+")");

		var detailsOverlay = document.createElement("div");
		detailsOverlay.id= "dt_" + index;
		detailsOverlay.className = "detailsOverlay";
		detailsOverlay.appendChild(document.createTextNode("click for details"));
		detailsOverlay.onclick = new Function("pageTracker._trackEvent('OfferDetails', 'View_Grid', '"+coupon.offerId+"');cui.showCouponDetails("+index+")");
		topShell.appendChild(detailsOverlay);

		if (coupon.isRebate)
		{
			var couponRebate = document.createElement("div");
			couponRebate.className = "couponRebate";
			topShell.appendChild(couponRebate);
		}

		if ((coupon.hasFaceValue > 0 || isFreeOffer)&& coupon.isCPG && coupon.isCPG != 0)
		{
			var couponSavings = document.createElement("div");
			couponSavings.className = "couponSavings";
			couponHolder.appendChild(couponSavings);
			var csInner = document.createElement("div");
			couponSavings.appendChild(csInner);
			var faceValue = coupon.displayFaceValue;
			var isRound = parseFloat(faceValue) == parseInt(faceValue);
			
			if (faceValue.indexOf("0.") > -1 && faceValue < 1)
			{
				faceValue = faceValue.substring(faceValue.length - 2) + '&cent;';
			}	
			else
			{
				var cents = faceValue.substring(faceValue.length - 2);
				var dollars = faceValue.substring(0, faceValue.indexOf('.'));
				if (cents == '00')
				{
					faceValue = dollars;
				}
				else
				{
					faceValue = dollars + '<span>' + cents + '</span>';
				}
				faceValue = '$' + faceValue;
			}
			if(isFreeOffer)
			{
				csInner.innerHTML = 'Free';
			}
			else 
			{
				csInner.innerHTML = faceValue;
			}

		}

		var couponButton = this.getCouponButton(index, false);
		couponHolder.appendChild(couponButton);		
		
 		var clearId = coupon.merchantId;
		var clear = document.getElementById("clearMerc_" + clearId);
		if(!clear)
		{
			clear = document.createElement("div");
			clear.className = "clear"
			clear.id = "clearMerc_" + clearId;
		}
		displayArea.appendChild(clear);
		
  }	
  CouponUI.prototype.drawFaqMMF = function()
  {
		var displayArea = document.getElementById("faq_display_area");
		var holder = document.createElement("div");
		displayArea.appendChild(holder);
			
		var head = document.createElement("div");
		head.style.position = "relative";
  		head.style.padding = "2px 0px 2px 0px";
  		head.style.textAlign = "center";
  		head.style.fontSize = "14px";
  		head.style.fontWeight = "bold";
  		head.appendChild(document.createTextNode(" "));
 		
		var content = document.createElement("div");
		content.style.padding = "0px 0px 0px 6px";
		content.className = "subCatHolder";
		var faqHolder =document.createElement("div");
		content.appendChild(faqHolder);
		var goFaq = document.createElement("a");
		goFaq.appendChild(document.createTextNode("Cellfire FAQ"));
		goFaq.href = "javascript: cui.goToCoreSelf('faq=1')";
		faqHolder.appendChild(goFaq);
		
		var contactHolder =document.createElement("div");
		content.appendChild(contactHolder);		
		var goCont = document.createElement("a");
		goCont.appendChild(document.createTextNode("Contact Us"));
		goCont.href = "javascript: cui.goToCoreSelf('faq=c')";
		contactHolder.appendChild(goCont);
		/*
  		var twt_img_div = document.createElement("div");
  		content.appendChild(twt_img_div);	
  		var twt_img = document.createElement("img");
  		twt_img_div.appendChild(twt_img);
  		twt_img.src ="/images/home/web_box_twitter.jpg";
  		twt_img.width = "212";
  		twt_img.height = "65";
  		twt_img.style.cursor = "pointer";
  		twt_img.onclick = new Function("window.open('http://twitter.com/Cellfire')");
		
  		var blk_img_div = document.createElement("div");
  		content.appendChild(blk_img_div);	
   		var blk_img = document.createElement("img");
  		blk_img_div.appendChild(blk_img);
  		blk_img.src = "/images/home/web_box_fb.jpg";
  		blk_img.width = "212";
  		blk_img.height = "65";
  		blk_img.style.cursor = "pointer";
  		blk_img.onclick = new Function("window.open('http://www.facebook.com/cellfirecoupons')");
 		*/
  		var foot = document.createElement("div");
		
		var hfbox = applyHeaderFooterBox(head,content,foot);
		
		holder.appendChild(hfbox);
  }
  CouponUI.prototype.getCouponButton = function(index, forDetailPopup)
  {
  	var coupon = this.model.coupons[index];
  	
		var affiliatePath = '';
		if (this.model.inIframe)
		{
			affiliatePath = this.model.iframeCssId + '/';
		}	
  	
  	var buttonName = 'button';
  	if (forDetailPopup)
  	{
  		buttonName = 'buttonBIG';
  	}	

		var couponButton = document.createElement("div");
		if (forDetailPopup)
		{
			couponButton.className = "couponButtonDetail";
			couponButton.id = "da_" + index;
		}
		else
		{
			couponButton.className = "couponButton";
			couponButton.id = "sa_" + index;
		}		
		
		var buttonValue = '';
		var saveButtonFunc = '';
		
		var isCPG = (coupon.isCPG && coupon.isCPG != 0);

		if (coupon.isSaved)
		{
			if (coupon.isRebate)
			{
				buttonValue = buttonName + '_waitingforpurchase';
			}
			else
			{
				buttonValue = buttonName + '_couponsaved';
			}
		}
		else
		{
			var card = this.model.loyaltyCards[coupon.merchantId];
			if(card && card.numSavedToCard >= card.capacity)
			{
				buttonValue = 'cardfull';	// note - currently don't have this graphic
			}
			else
			{
				if(isCPG)
				{
					// If the card has been successfully added or is pending
					if(card && card.status == 2)
					{
						// card is good to go
						if (coupon.isRebate)
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showRebateForm("+index+");");
							buttonValue = buttonName + '_getyourrebate';
							
						}
						else if(this.model.user.isXuser)
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.clipWithLoyaltyCard("+card.cardNum+","+coupon.merchantId+","+index+", 1);");
							buttonValue = buttonName + '_savetocard';
						}else
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.clip("+index+");");
							buttonValue = buttonName + '_savetocard';
						}
					}
					/*
						no pending cards
						else if( card && card.status == 1)
						{
							// card is pending - check its status before we do anything
							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+");");
						buttonValue = buttonName + '_savetocard';
					}
					else
					{
						var merchant = this.model.merchants[coupon.merchantId];
						//*
						if ( this.model.cfGuest )
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showXuserPopup("+merchant.mid+","+ index +");");
							buttonValue = buttonName + '_savetocard';
						}else if (this.model.inIframe )
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showRegForm("+index+", true);");
							buttonValue = buttonName + '_savetocard';
						}else
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showSignUpMessage("+index+");");
							buttonValue = buttonName + '_savetocard';
						}
						//*/
						//saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showXuserPopup("+merchant.mid+","+ index +");");
						//buttonValue = buttonName + '_savetocard';
					}	
				}
				else
				{
					buttonValue = buttonName + '_savetophone';
					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+");");
					}	
				}
			}
		}
		
		couponButton.style.background = "url('/includes/templates/master/images/newlook/" + affiliatePath + buttonValue + ".png') center center no-repeat";
		if (saveButtonFunc != '')
		{
			couponButton.style.cursor = "pointer";
			couponButton.onclick = saveButtonFunc;
		}
				
		return couponButton;
  }
  
  
  
  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);
  	
  	//alert('height = ' + holder.offsetHeight);
  	
  	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,size_big){
 		var hasSaved = this.model.coupons[index].isSaved;
		
		var savingArea = document.createElement("div");
		savingArea.id = "sa_"+index;
		savingArea.style.margin = "0px";
		savingArea.style.width = "146px";
		savingArea.style.height = "28px";
		savingArea.style.textAlign ="center";
		savingArea.style.overflow = "hidden";
		
		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;
				var coupon = this.model.coupons[index];
				if(coupon.isCPG && coupon.isCPG != 0)
				{
					buttonText = "savetocard";
					// If the card has been successfully added or is pending
					if(card && card.status == 2)
					{
						// card is good to go
						if(!this.model.user.isXuser){
							if (coupon.isRebate)
							{
								saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showRebateForm("+index+");");
							}
							else
							{
								saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.clip("+index+");");
							}
						}else{ // xuser
							if (coupon.isRebate)
							{
								saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showRebateForm("+index+");");
							}
							else
							{
								saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.clipWithLoyaltyCard("+card.cardNum+","+coupon.merchantId+","+index+", 1);");
							}						
						}
					}
					else if( card && card.status == 1)
					{
						// card is pending - check its status before we do anything
						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
					{
						var merchant = this.model.merchants[this.model.iframeMid];
						if (this.model.inIframe)
						{
							saveButtonFunc = new Function("pageTracker._trackEvent('Popups', 'View', '" + merchant.mid + "_QFrame_Reg'); wm.setLastAnchor('sa_" + index + "');cui.showRegForm("+index+", true);");
						}else if (this.model.cfGuest)
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showXuserPopup("+merchant.mid+","+ index +");");
						}else
						{
							saveButtonFunc = new Function("wm.setLastAnchor('sa_" + index + "');cui.showSignUpMessage("+index+");");
						}
					}	
				}
				else
				{
					buttonText = "savetophone";
					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 + "/button_savetocard.png";
					if(!size_big)	sButtonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/saveToCard.gif";
					else sButtonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/buttonBIG_savetocard.png";
					sButtonImg.style.cursor = "pointer";
					sButtonImg.alt = "Save to Card";
					sButtonImg.id = buttonText+"_"+index;
					sButtonImg.onclick = saveButtonFunc;
					savingArea.appendChild(sButtonImg);
				}
				else
				{
					var saveButton = getSaveButton("", index, buttonText, 146, saveButtonFunc,size_big);
					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)
  {
  	this.startSavingAnimation(index);

		// 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];
  		if(!coupon)	return;
  		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 couponButton = document.getElementById("sa_" + couponId);
		var cssFolder = '';
		if (this.model.inIframe)
		{
			cssFolder = '/' + this.model.iframeCssId;
		}
		couponButton.style.background = "url('/includes/templates/master/images/newlook" + cssFolder + "/button_processing.gif') center center no-repeat";
		var couponDetailButton = document.getElementById("da_" + couponId);
		if (couponDetailButton)
		{
			couponDetailButton.style.background = "url('/includes/templates/master/images/newlook" + cssFolder + "/buttonBIG_processing.gif') center center no-repeat";
		}	

 		setTimeout("cui.loopSavingAnimation(" + couponId + ");",300);
  }
  
  CouponUI.prototype.loopSavingAnimation = function(couponId)
  {
		var stepTime = 300;
		var coupon = this.model.coupons[couponId];
		if(coupon.working && coupon.working != 2){
			setTimeout("cui.loopSavingAnimation(" + couponId + ")", stepTime);
		}
		else
		{
			this.endSavingAnimation(couponId);
		}	
  }
  
  CouponUI.prototype.endSavingAnimation = function(couponId)
  {
		_MODEL.setCouponWorkingState(couponId, false);
  }
  
  CouponUI.prototype.onCouponChange = function(){
  		var coupon;
  		for(var i in this.model.coupons)
  		{
  			coupon = this.model.coupons[i];
  			if(coupon.hasChanged)
  			{
					if (!coupon.working)
					{
						this.drawCoupon(i);
						if (coupon.isRebate)
						{
							this.showRebateConfirmation(coupon.couponId);
						}	

						// If we want to show the offers being removed
						if(this.showRemoveIcon){
							//this.closeCoupon(i);
						}
						
						if (coupon.isSaved)
						{
							var shoppingList = document.getElementById("shopping_list_"+coupon.merchantId);
							var isVZI=(this.model.clientName=="Spend Smart");
							if(this.model.inIframe && !this.model.inMMF)this.lcui.createShoppingList(coupon.merchantId,isVZI);
							
							var cardBlock = document.getElementById("mc_" + coupon.merchantId);
							if (cardBlock)
							{
								var savings = 0;
								var numSaved = 0;
								var couponInner;
								for(var couponId in this.model.coupons)
								{
									couponInner = this.model.coupons[couponId];	
									if (couponInner && couponInner.isSaved && couponInner.merchantId == coupon.merchantId)
									{
										if(!couponInner.isRebate)
											savings += parseFloat(couponInner.faceValue);
										numSaved++;
									}	
								}	
								savings = savings.toFixed(2);
								var savingValue = document.getElementById("sv_" + coupon.merchantId);
								if (savingValue)
								{
									savingValue.innerHTML = "$" + savings;
								}
								var totalValue = document.getElementById("tv_" + coupon.merchantId);
								if (totalValue)
								{
									var clippedText = ' coupons clipped';
									if (numSaved == 1)
									{
										clippedText = ' coupon clipped';
									}
									totalValue.innerHTML = numSaved + clippedText;
								}
							}
						}	
							
						
						coupon.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"){
		if(param.errorId=='3'){
			var title = document.createElement("div");
			title.style.fontSize = "20px";
			title.appendChild(document.createTextNode("Coupon No Longer Available"));
			errorHolder.appendChild(title);

			var p1 = document.createElement("div");
			p1.appendChild(document.createTextNode("We're sorry but the coupon you're trying to save was so popular that we ran out. Please check out our other great coupons."));
			errorHolder.appendChild(p1);
			p1.style.margin = "3px 0px";
		}else if(param.errorId=='7' || param.errorId=='9'){
			var title = document.createElement("div");
			title.style.fontSize = "20px";
			title.appendChild(document.createTextNode("Clip Failed"));
			errorHolder.appendChild(title);

			var p1 = document.createElement("div");
			p1.appendChild(document.createTextNode("Please try again later or call customer support for assistant."));
			errorHolder.appendChild(p1);
			p1.style.margin = "3px 0px";
		}else if(param.errorId=='8'){
			var title = document.createElement("div");
			title.style.fontSize = "20px";
			title.appendChild(document.createTextNode("Wrong loyalty card"));
			errorHolder.appendChild(title);

			var p1 = document.createElement("div");
			p1.appendChild(document.createTextNode("Please enter a valid loyalty card number."));
			errorHolder.appendChild(p1);
			p1.style.margin = "3px 0px";
		}else if(param.errorId=='10'){
			var title = document.createElement("div");
			title.style.fontSize = "20px";
			title.appendChild(document.createTextNode("Duplicate clipping"));
			errorHolder.appendChild(title);

			var p1 = document.createElement("div");
			p1.appendChild(document.createTextNode("The offer is clipped to the loyalty card already."));
			errorHolder.appendChild(p1);
			p1.style.margin = "3px 0px";
		}else if(param.errorId=='11'){
			var title = document.createElement("div");
			title.style.fontSize = "20px";
			title.appendChild(document.createTextNode("Unable to Save Coupon to Card"));
			errorHolder.appendChild(title);

			var p1 = document.createElement("div");
			p1.appendChild(document.createTextNode("Sorry, but that offer has already been redeemed using your card. Coupons can only be redeemed once. Please try saving any of the other available offers."));
			errorHolder.appendChild(p1);
			p1.style.margin = "3px 0px";
		}else{
			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("."));
		}
		*/
		if(this.model.inIframe)//in qframe
			var handle = this.windowManager.createNewWhiteOutWindow(errorHolder, 380, 0, true);
		else
			var handle = this.windowManager.createNewBlackOutWindow(errorHolder, 380, 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');
		
		//CMI-4732 new offer detail banner begin
		show_new_banner = coupon.bannerAdsImagesId;
		if(coupon.bannerAdsImageId!=0 && coupon.offerAdsInfo!=""){
			var new_banner_detail_banner = document.createElement("div");
			var new_banner_img = document.createElement("img");
			new_banner_img.src =  "/images/"+coupon.bannerAdsImageId+".jpg";
			new_banner_img.width = "300";
			new_banner_img.height = "35";
			new_banner_img.onclick =new Function("pageTracker._trackEvent('Popups', 'sml_bnr_click', '" + coupon.offerId + "'),cui.showCouponAdsDetails("+ index +")");
			new_banner_detail_banner.style.cursor = "pointer"; 
			new_banner_detail_banner.appendChild(new_banner_img);
			content.appendChild(new_banner_detail_banner);
			var padding_new = document.createElement("div");
			padding_new.style.padding = "5px";
			content.appendChild(padding_new);
		}
		//CMI-4732 new offer detail banner end	
		
		imgDiv.className = "pop_img_holder";
		if(coupon.imageId != 0 && coupon.imageId != "-1" ){
			imgEle.src = "/images/"+coupon.imageId+".jpg";
			imgEle.alt = " ";
			imgEle.width ="175";
			imgEle.height = "175";
			imgDiv.appendChild(imgEle);
		}else{
			imgEle.src = "/includes/templates/master/images/tag_200x200_final.gif";
			imgEle.alt = " ";
			imgEle.width ="175";
			imgEle.height = "175";
			imgDiv.appendChild(imgEle);
		}
		
		txtDiv.className = "pop_txt_holder";
		
		var shortTerms = document.createElement("span");
		shortTerms.style.fontSize = "10px";
		shortTerms.appendChild(document.createTextNode(coupon.shortTerms));
		txtDiv.appendChild(shortTerms);
		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);
		
  		var savingHolder = document.createElement("div");
  		txtDiv.appendChild(savingHolder);
  		savingHolder.id = "animHolder_"+index;
  		savingHolder.style.width = "100%";
  		savingHolder.style.padding = "10px 0px";
   		
		var savingDiv = this.getCouponButton(index, true);
		savingHolder.appendChild(savingDiv);	
		
		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";
		buttonHolder.style.margin = "5 auto";
		
		txtDiv.appendChild(buttonHolder);
		txtDiv.appendChild(clearDiv);

		var clearDiv = document.createElement("div");
		clearDiv.className = "clear";
		buttonHolder.appendChild(clearDiv);
		//pop_txt_holder end
		
		content.appendChild(imgDiv);
		content.appendChild(clearDiv2);
		content.appendChild(txtDiv);		
		content.appendChild(clearDiv3);
		
		return content;
  }
  //added for CMI-4732 show new banner detail pop up
  CouponUI.prototype.show_new_ad_detail = function(index){
		var coupon = this.model.coupons[index];
		var content = document.createElement("div");
		content.id = "cd_content_holder_"+index;
	
		var ad_content =document.createElement("div");

		ad_content.id = "ad_content_div";
		ad_content.style.padding ="5px 0px";		
		ad_content.style.height = "410px";
		ad_content.style.textAlign = "center";
		
		code_snippet = coupon.offerAdsInfo;
		ad_content.innerHTML = code_snippet;
		
		var back_to_detail_div =document.createElement("div");
		back_to_detail_div.className = "back_to_detail_div";
		back_to_detail_div.id ="back_to_detail_div";
		back_to_detail_div.style.textAlign = "center";
		back_to_detail_div.style.padding ="5px 0px";
		
		var back_to_offer_detail = document.createElement("img");
		back_to_detail_div.appendChild(back_to_offer_detail);
		back_to_offer_detail.src = "/images/share/button_backtodetails.png";
		back_to_offer_detail.width = "140";
		back_to_offer_detail.height = "19";
		back_to_offer_detail.style.cursor = "pointer";
		back_to_offer_detail.onclick =new Function("cui.showCouponDetails("+index+")");

		content.appendChild(ad_content);		
		content.appendChild(back_to_detail_div);
		return content;
}
  //added for CMI-5419 
  CouponUI.prototype.show_mobile_popup = function(){
		var content = document.createElement("div");
		content.style.padding ="5px 0";
		
		var title_div =document.createElement("span");
		title_div.appendChild(document.createTextNode("Clip coupons on your phone!"));
		title_div.style.fontSize ="18px";
		title_div.style.padding = "0 5px 0 10px";
		title_div.style.fontWeight = "bold";
			
		var content_holder = document.createElement("div");
		content_holder.id = "content_holder";
		var table_tr = document.createElement("div");
		var text_holder = document.createElement("div");
		text_holder.id ="text_holder";
		text_holder.style.width = "260px";
		text_holder.style.verticalAlign = "top";
		text_holder.style.cssFloat ="left";
		text_holder.style.styleFloat ="left";
		
		var padding = document.createElement("div");
		padding.style.padding = "0 5px 0 10px";

		var p1=document.createElement("span");
		p1.style.fontSize ="14px";
		p1.style.textAlign = "left";
		p1.appendChild(document.createTextNode("As a Verizon Wireless customer, you can browse, view, and save coupons on-the-go using your mobile phone, too!"));
		
		var p2 =document.createElement("span");
		p2.style.fontSize ="15px";
		p2.appendChild(document.createTextNode("To access Spend Smart on your mobile phone:"));
		
		padding.appendChild(p1);
		padding.appendChild(document.createElement("br"));
		padding.appendChild(document.createElement("br"));
		padding.appendChild(p2);
		
		var list_holder =document.createElement("div");
		list_holder.style.fontSize = "14px";
		list_holder.innerHTML = "<DIR><LI>Access <b>Mobile Web</b>, Select <b>more.</b><LI>Select <b>Shop.</b><LI>Select <b>Spend Smart.</b><LI>Register or Log In and start saving.</DIR>";
		
		text_holder.appendChild(document.createElement("br"));
		text_holder.appendChild(padding);
		text_holder.appendChild(list_holder);		
		
		var img_holder = document.createElement("div");
		img_holder.style.width ="180px";
		img_holder.style.height = "250px";
		img_holder.style.cssFloat ="right";
		img_holder.style.styleFloat ="right";

		table_tr.appendChild(text_holder);
		table_tr.appendChild(img_holder);

		var img1 = document.createElement("img");
		img1.src="/images/share/screenshot_cx.png";
		img_holder.appendChild(img1);

		var bottom_div =document.createElement("div");
		bottom_div.style.padding ="0px 5px 0px 10px";
		var clear_div = document.createElement("div");
		clear_div.style.clear = "both";
		bottom_div.appendChild(clear_div);
		
		var bottom_note =document.createElement("div");
		bottom_note.style.fontSize ="15px";
		bottom_note.innerHTML ="<b>OR</b> on your Android, BlackBerry, webOS, or Windows Mobile smartphone:<br><DIR><LI>Visit <b>m.spendsmartgrocery.com</b></DIR>";
		bottom_div.appendChild(bottom_note);
		
		var back_div =document.createElement("div");
		bottom_div.appendChild(back_div);
		back_div.style.textAlign = "center";
		back_div.style.cssFloat = "none";
		
		var ok_div =document.createElement("div");
		ok_div.style.height ="31px";
		back_div.appendChild(ok_div);
		
		var ok_button = document.createElement("img");
		ok_button.src = "/images/share/ok_button.png";
		ok_button.style.cursor = "pointer";
		ok_div.appendChild(ok_button);
		
		content_holder.appendChild(table_tr);
		content.appendChild(title_div);
		content.appendChild(content_holder);
		content.appendChild(bottom_div);

  		var handle = this.windowManager.createNewWhiteOutWindow(content, 520,0,true);
		ok_button.onclick =new Function("wm.hideWindow("+handle+");");
  		this.windowManager.showWindow(handle);
}  
  //added for CMI-5419 
  CouponUI.prototype.show_fios_popup = function(){
		var content = document.createElement("div");
		content.style.padding ="10px 0 10px";		
		content.style.width ="450px";
		content.style.height ="380px";
	
		var title_div =document.createElement("div");
		content.appendChild(title_div);
		title_div.style.padding = "0px 10px 0 10px";
		var title_span =document.createElement("span");
		title_span.appendChild(document.createTextNode("Clip coupons on your TV!"));
		title_span.style.fontSize ="18px";
		title_span.style.fontWeight = "bold";
		
		var p1=document.createElement("span");
		p1.style.fontSize ="14px";
		p1.appendChild(document.createTextNode("As a Verizon FiOS TV customer, you can browse, view, and save coupons from the comfort of your living room, too!"));
		var p2 =document.createElement("span");
		p2.style.fontSize ="14px";
		p2.appendChild(document.createTextNode("To access Spend Smart on FiOS TV:"));
		
		title_div.appendChild(title_span);
		title_div.appendChild(document.createElement("br"));
		title_div.appendChild(document.createElement("br"));
		title_div.appendChild(p1);
		title_div.appendChild(document.createElement("br"));
		title_div.appendChild(document.createElement("br"));
		title_div.appendChild(p2);
		
		var list_holder =document.createElement("div");
		list_holder.style.fontSize = "14px";
		list_holder.innerHTML = "<DIR><LI>Press <b>Menu</b>, select <b>Widgets.</b><LI>Open the <b>Widget Bazaar.</b><LI>Select the <b>Spend Smart</b> widget.<LI>Register or Log In and start saving.</DIR>";
		
		var image_holder =document.createElement("div");
		image_holder.style.padding ="0px 0 2px 5px";
		
		var img1_holder = document.createElement("div");
		img1_holder.style.float ="left";
		var img1 = document.createElement("img");
		img1.src="/images/share/screenshot_fios_1.png";

		var img2_holder = document.createElement("div");
		img2_holder.style.float ="right";
		var img2 = document.createElement("img");
		img2.src="/images/share/screenshot_fios_2.png";
		img2_holder.appendChild(img2);

		image_holder.appendChild(img1);
		image_holder.appendChild(document.createTextNode('\u00a0'));		
		image_holder.appendChild(document.createTextNode('\u00a0'));		
		image_holder.appendChild(img2);

		var back_div =document.createElement("div");
		back_div.style.padding = "2px 0 2px 140px";

		var ok_button = document.createElement("img");
		ok_button.src = "/images/share/ok_button.png";
		ok_button.style.cursor = "pointer";
		back_div.appendChild(ok_button);
		
		content.appendChild(list_holder);
		content.appendChild(image_holder);
		content.appendChild(back_div);

  		var handle = this.windowManager.createNewWhiteOutWindow(content, 500,0,true);
		ok_button.onclick =new Function("wm.hideWindow("+handle+");");
  		this.windowManager.showWindow(handle);
} 
  
  CouponUI.prototype.createShoppingListPopup = function(mid){
		var content = document.createElement("div");
		content.style.padding ="10px 0px";		
		var title = document.createElement("div");
		title.style.fontSize="16px";
		title.style.fontWeight = "bold";
		title.style.textAlign = "left";
		title.innerHTML ="Create Shopping List";
		
		var card = this.model.loyaltyCards[mid];
		var note = document.createElement("div");
		note.id = "note";
		note.style.fontSize="12px";
		note.style.padding = "10px 0px";
		note.style.textAlign = "left";
		note.innerHTML ="Create a shopping list to help you remember your clipped coupons when you go shopping.";	
		var note2 = document.createElement("div");
		note2.id = "note2";
		note2.style.fontSize="12px";
		note2.style.fontWeight="bold";
		note2.style.padding = "10px 0px";
		note2.style.textAlign = "left";
		note2.innerHTML ="Select the type of shopping list to create:";	
		
		var img_div = document.createElement("div");
		img_div.style.padding = "5px";
		img_div.style.height = "50px";
		img_div.style.textAlign = "left";
		var email_img=document.createElement("img");
		email_img.src="/images/home/icon_email.png";
		email_img.style.padding = "0px 10px 0px 0px";
		email_img.onclick = new Function("cui.showEmailShoppingList("+mid+")");
		var mobile_img=document.createElement("img");
		mobile_img.src="/images/home/icon_mobile.png";
		mobile_img.style.padding = "0px 10px 0px 0px";
		mobile_img.onclick = new Function("window.open('/help/ps_help.php?reg_type=0&helpId=2');");
		var print_img=document.createElement("img");
		print_img.src="/images/home/icon_print.png";
		print_img.style.padding = "0px 5px 0px 0px";
		//print_img.onclick = new Function("var x=window.open(\"/shopping_list.php?mId="+mid+"\", \"listWindow\", \"menubar=0,status=0,toolbar=0,location=0,height=800,width=800,scrollbars=1\");wm.closeOpenWindow();");
		print_img.onclick = new Function("window.open('/shopping_list.php?mId="+mid+"','shoppingListWindow','menubar=0,status=0,toolbar=0,location=0,height=800,width=800,scrollbars=1');wm.closeOpenWindow();");
		img_div.appendChild(email_img);
		img_div.appendChild(mobile_img);
		img_div.appendChild(print_img);
		
		content.appendChild(title);
		content.appendChild(note);
		content.appendChild(note2);		
		content.appendChild(img_div);
		var handle = this.windowManager.createNewBlackOutWindow(content, 400, 0, 0,  true);
		this.windowManager.showWindow(handle);
}
  CouponUI.prototype.showEmailShoppingList = function(mid){
		var content = document.createElement("div");
		content.style.padding ="10px 0px";		
		var title = document.createElement("div");
		title.style.fontSize="16px";
		title.style.fontWeight = "bold";
		title.style.textAlign = "left";
		title.innerHTML ="Email Shopping List";
		
		var note = document.createElement("div");
		note.id = "note";
		note.style.fontSize="12px";
		note.style.padding = "10px 0px";
		note.style.textAlign = "left";
		note.innerHTML ="Enter the email address you'd like us to send your shopping list to:";	
		
		var email_div = document.createElement("div");
		email_div.style.textAlign = "left";
		var email_int = document.createElement("input");
		email_int.id = "emailInput";
		email_int.style.size="50";
		email_int.style.align = "left";
		if (this.model.user.email!="")
			email_int.value = this.model.user.email;
		email_int.onblur = new Function("if(document.getElementById('emailInput').value=='' ||!validate_email(document.getElementById('emailInput').value)) document.getElementById('errorDiv').innerHTML='Please enter a valid email address!'; else document.getElementById('errorDiv').innerHTML='';");
		email_div.appendChild(email_int);

		var error_div = document.createElement("div");
		error_div.id = "errorDiv";
		error_div.style.color = "red";
		error_div.style.textAlign = "left";	
		
		var button_div = document.createElement("div");
		button_div.style.padding = "10px 0px";
		button_div.style.textAlign = "left";
		var button = document.createElement("button");
		button.name = "Send Shopping List";
		button.innerHTML = "Send Shopping List";
		button.onclick = new Function("if(!validate_email(document.getElementById('emailInput').value)) document.getElementById('errorDiv').innerHTML='Please enter a valid email address!';else cui.emailShoppingList("+mid+",document.getElementById('emailInput').value);");
		button_div.appendChild(button);
		
		content.appendChild(title);
		content.appendChild(note);
		content.appendChild(email_div);
		content.appendChild(error_div);
		content.appendChild(button_div);
		var handle = this.windowManager.createNewBlackOutWindow(content, 350, 0, 0, true);
  		this.windowManager.showWindow(handle);
  
  }
  CouponUI.prototype.emailShoppingList = function(mid,email){
	  	// Do the Ajax
	 	var req = getXMLHttpObject();
	 	req.onreadystatechange = function(){
	 		if(req.readyState == 4){
	 			_MODEL.sentShoppingList(email);
 	 		}
	 	};
	 	var openString = "/includes/form_handlers/email_shopping_list.php?mid="+mid+"&email="+email;
	 	req.open("GET",openString,true);
	 	req.send(null);
	}  

  CouponUI.prototype.onShoppingListSent = function(email){
	  	this.windowManager.closeOpenWindow();
		var content = document.createElement("div");
		content.style.padding ="10px 0px";		
		var title = document.createElement("div");
		title.style.fontSize="16px";
		title.style.fontWeight = "bold";
		title.style.textAlign = "left";
		title.innerHTML ="Shopping List Sent!";
		
		var note = document.createElement("div");
		note.id = "note";
		note.style.fontSize="12px";
		note.style.padding = "10px 0px";
		note.style.textAlign = "left";
		note.innerHTML ="Your shopping list has been sent to "+email+". It should appear in your email inbox shortly. ";	
		
		content.appendChild(title);
		content.appendChild(note);
		var handle = this.windowManager.createNewBlackOutWindow(content, 350, 0, 0, true);
  		this.windowManager.showWindow(handle);
 	}    

  CouponUI.prototype.showCouponDetails = function(index){
  		var coupon = this.model.coupons[index];
  		var merc = this.model.merchants[coupon.merchantId];
  		var width = 350;
  		var pointAt = document.getElementById("ch_"+index);
  		
  		this.postView(coupon.couponId);
  		
  		if(coupon.detailWindowHandle)
  		{
  			this.windowManager.destroyWindow(coupon.detailWindowHandle);
  		}
			var content = this.buildCouponDetailContent(index);
			coupon.detailWindowHandle = wm.createNewCallOutWindow(content, width, "ch_"+index, true, 1);
			wm.showWindow(coupon.detailWindowHandle);
  }

  CouponUI.prototype.showCouponAdsDetails = function(index){
		var coupon = this.model.coupons[index];
		var merc = this.model.merchants[coupon.merchantId];
		var width = 350;
		var pointAt = document.getElementById("ch_"+index);
		
		this.postView(coupon.couponId);
		var content = this.show_new_ad_detail(index);
		coupon.adDetailWindowHandle = wm.createNewCallOutWindow(content, width, "ch_"+index, true, 1);
		wm.showWindow(coupon.adDetailWindowHandle);
} 
  //CMI-4731 External Clip to QFrame
  CouponUI.prototype.onExAdsOfferChange = function(){
	    var offer = this.model.exAdsOffer;
	    if(this.model.ExtClipMerchantList.length>1) {
	    	this.showExClipPopup(offer,null);
	    }else{
		    this.showExClipPopup(offer,this.model.iframeMid);
	    }
} 
  //CMI-4731 External Clip to QFrame popup
  CouponUI.prototype.showExClipPopup = function(offerId, merchantId){
	  	//alert(offerId);
	  	var index = this.model.exClipCoupon[this.model.iframeMid];
		if (!index)	{
			for (var j in this.model.exCoupon){
				var coupon = this.model.exCoupon[j];
				index = coupon.couponId;
				break;
			}
		}else{
			var coupon = this.model.coupons[index];
		}

		if (this.model.user.authenticated && merchantId>0){
			this.checkStatusAndClip(index);
			return;
		}
		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 = "24px";
  		padding_title.style.fontWeight = "bold";
		padding_title.appendChild(document.createTextNode("Digital Grocery Coupons"));
  		padding_title.appendChild(document.createElement("br"));
  		
  		var cf_div = document.createElement("div");
  		cf_div.style.height = "20px";
  		var cfImage = document.createElement("img");
  		cf_div.appendChild(cfImage);
  		cfImage.src = "/images/powered_by_cf.gif";
  		
  		padding_title.appendChild(cf_div);
  		padding_title.appendChild(document.createElement("br"));

  		var center_holder = document.createElement("div");
  		center_holder.id ="center_holder";
  		center_holder.height = "200px";
  		message.appendChild(center_holder);
  		
  		var center_div = document.createElement("div");
  		center_holder.appendChild(center_div);
  		var center_tr = document.createElement("div");
  		center_div.appendChild(center_tr);
  		
  		var center_left = document.createElement("div");
  		center_left.id ="center_left";
 		center_left.style.cssFloat = "left";
 		center_left.style.styleFloat = "left";
 		center_left.style.verticalAlign = "bottom";
 		center_left.style.width ="140px";
 		center_left.style.margin = "5px 10px 5px 15px";
 		//center_left.style.background = "transparent url('../../includes/templates/master/images/productshadow_150.jpg') no-repeat";
  		center_tr.appendChild(center_left);
 		
 		var imgEle = document.createElement("img");
 		if(coupon.imageId != 0 && coupon.imageId != "-1" ){
			imgEle.src = "/images/"+coupon.imageId+".jpg";
			imgEle.alt = " ";
			imgEle.width ="140";
			imgEle.height = "140";
			imgEle.align = "center";
			center_left.appendChild(imgEle);
		}else{
			imgEle.src = "/includes/templates/master/images/tag_200x200_final.gif";
			imgEle.alt = " ";
			imgEle.width ="140";
			imgEle.height = "140";
			center_left.appendChild(imgEle);		
		}
   		
  		var center_right = document.createElement("div");
  		center_right.style.cssFloat = "right";
  		center_right.style.styleFloat = "right";
  		center_right.id ="center_right";
  		center_right.style.width = "200px";
  		center_right.style.padding = "0px 10px 0px 0px";
  		center_right.style.verticalAlign = "top";
		var clear = document.createElement("div");
		clear.className = "clear";
		center_tr.appendChild(center_right);
		center_tr.appendChild(clear);  		
  		
		var title = document.createElement("span");
		title.style.fontSize = "12px";
		title.style.fontWeight = "bold";
		title.style.color = "black";
		title.appendChild(document.createTextNode(coupon.shortTerms));
		center_right.appendChild(title);
		
  		var padding = document.createElement("div");
  		center_right.appendChild(padding);
  		padding.style.fontSize = "12px";
  		padding.style.fontWeight = "normal"; 
  		if(merchantId){
  			var card = this.model.loyaltyCards[merchantId];
  	  		var note_div = document.createElement("div");
  	  		note_div.style.padding = "10px 5px 15px 5px";
  	  		note_div.innerHTML = "Save this and many more coupons directly to your "+card.merchantName+" " +card.cardName+". Just use your card at the checkout to redeem!";
  	  		padding.appendChild(note_div);
  	  		
  	  		var signup_div =document.createElement("div");
  	  		signup_div.style.height="40px";
  	  		signup_div.style.padding = "1px";
  	  		
	  		var signUpImage = document.createElement("img");
	  		signup_div.appendChild(signUpImage);
	  		signUpImage.style.padding ="5px 10px 10px";
	  		signUpImage.src = "/includes/templates/master/images/button_register_sm.png";
	  		signUpImage.onclick = new Function("cui.showRegForm("+index+", true);");
	  		signUpImage.style.cursor = "pointer";
	  		padding.appendChild(signup_div);
	  		
	  		var login_div = document.createElement("div");
  	  		login_div.style.height="40px";
  	  		login_div.style.padding = "1px";

	  		var loginImage = document.createElement("img");
	  		login_div.appendChild(loginImage);
	  		loginImage.style.padding ="10px 10px 10px";
	  		loginImage.src = "/includes/templates/master/images/button_login_sm.png";
	  		loginImage.onclick = new Function("cm.showLoginWindow('','" + this.getCoreSelf() + "?ex_clip_id="+offerId+"&ex_merc_id="+this.model.iframeMid+"')");
	  		loginImage.style.cursor = "pointer";
	  		
		  	padding.appendChild(login_div);
  		}else{
  	  		var merchant_select_fm = document.createElement("form");
  	  		merchant_select_fm.action = this.getCoreSelf();
  	  		merchant_select_fm.method = "GET";
  	  		merchant_select_fm.id = "merchant_select_fm";
  	  		merchant_select_fm.name = "merchant_select_fm";
			var coupon_id_input = document.createElement("input");
			coupon_id_input.type = "hidden";
			coupon_id_input.value = offerId;
			coupon_id_input.name = "ex_clip_id";
			coupon_id_input.id = "ex_clip_id";  
			var merc = document.createElement("input");
			merc.type = "hidden";
			merc.value = "";
			merc.name = "ex_merc_id";
			merc.id = "ex_merc_id"; 			
			merchant_select_fm.appendChild(coupon_id_input);
			merchant_select_fm.appendChild(merc);
			var merchant_select = this.getMerchantSelect(coupon.offerId, this.model.ExtClipMerchantList);
			merc.value = merchant_select.options[0].value;
  	  		merchant_select_fm.appendChild(merchant_select);
  	  		
  	  		var note_div = document.createElement("div");
  	  		note_div.style.padding = "10px 5px 15px 5px";
  	  		note_div.innerHTML = "To get this and many more coupons, <b>select a grocery store to redeem this coupon at:</b>";	
  	  		padding.appendChild(note_div);
  	  		padding.appendChild(merchant_select_fm);

  	  		var signUp_div = document.createElement("div");
  	  		signUp_div.style.height = "50px";
  	  		var signUpImage = document.createElement("img");
  	  		signUp_div.appendChild(signUpImage);
	  		signUpImage.style.padding ="20px 10px 10px 5px";
	  		signUpImage.src = "/includes/templates/master/images/button_continue_sm.jpg";
   		  	signUpImage.onclick = new Function("document.getElementById('merchant_select_fm').submit()");
	  		signUpImage.style.cursor = "pointer";
		  	merchant_select_fm.appendChild(signUp_div);
 	  		
	  		var bottom_div =document.createElement("div");
	  		bottom_div.style.fontSize = "12px";
	  		bottom_div.style.height = "130px";
	  		message.appendChild(bottom_div);
	  		bottom_div.appendChild(document.createElement("hr"));
	   		var title_text = document.createElement("b");
	  		title_text.appendChild(document.createTextNode("Cellfire makes coupons simple!"));	
	  		bottom_div.appendChild(title_text);	
	  		bottom_div.appendChild(document.createElement("br"));
	  		bottom_div.appendChild(document.createTextNode("1. Select the store you'd like to redeem your coupons at."));
	  		bottom_div.appendChild(document.createElement("br"));
			bottom_div.appendChild(document.createTextNode("2. Create your FREE cellfire account and provide your savings card informaiton."));
	  		bottom_div.appendChild(document.createElement("br"));
	 		bottom_div.appendChild(document.createTextNode("3. Use your card at checkout to redeem your coupons!"));
	  		bottom_div.appendChild(document.createElement("br"));
	  		bottom_div.appendChild(document.createElement("br"));
	 		bottom_div.appendChild(document.createTextNode("Cellfire coupons work at thousands of grocery stores across the country."));
	  		bottom_div.appendChild(document.createElement("br"));
  		}
 		
  		var handle = this.windowManager.createNewWhiteOutWindow(message, 450,0,true);
  		this.windowManager.showWindow(handle);	  
}
  CouponUI.prototype.getMerchantSelect = function(offerId,merchantList){
	var listOfMerchs = this.model.getUnassocBulkMerchants();
	var merchant_select = document.createElement("select");
	merchant_select.id = "merchant_select";
	for (var j in listOfMerchs){
		mId = listOfMerchs[j];
		if(in_array(mId,merchantList)){
			var merchant_option = document.createElement("option");
			var merchant = this.model.merchants[mId];
			merchant_option.text = merchant.name;
			merchant_option.value = merchant.mid;
	 		try{
	 			merchant_select.add(merchant_option,null);
	 		}catch(ex) {
	 			merchant_select.add(merchant_option);
	 		}
	 		merchant_select.onchange = new Function("document.getElementById('ex_merc_id').value=document.getElementById('merchant_select').options[document.getElementById('merchant_select').options.selectedIndex].value;");
		}
	}
	merchant_select.options.selectedIndex=0;
	return merchant_select;
  }
  //end CMI-4731
  
  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 coupon = this.model.coupons[index];
  	var callback = new Function("cui.model.fireOpinMindPixel(new Array(" + coupon.offerId + ", 0))");
  	var offerId = coupon.offerId;
  	if (coupon.isRebate)
  	{
  		offerId = 0; // add card without clipping
  		callback = new Function("wm.setLastAnchor('sa_" + coupon.couponId + "');cui.showRebateForm("+ coupon.couponId +");")
  	}	
  	this.lcui.showAddCardFormWindow(coupon.merchantId, 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+".png"
  		}
  		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); 
  		
  		/*
  		var useCardImage = document.createElement("img");
  		useCardImage.src = "/includes/templates/master/images/round_corners/button_savetocard_big.jpg";
  		useCardImage.style.width = "146px";
  		useCardImage.style.height = "26px";  	
  		useCardImage.style.cursor = "pointer";
  		useCardImage.onclick = new Function("cm.showClipWithCardWindow('','"+index+"')");
  		padding.appendChild(document.createElement("br"));
  		padding.appendChild(document.createElement("br"));
  		padding.appendChild(useCardImage); 
			*/
			
  		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 clip 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;
				//CMI-4731 External Clip to QFrame, check to see if another merchant is selected
				if(document.getElementById('merchant_select') && document.getElementById('merchant_select').options[document.getElementById('merchant_select').options.selectedIndex].value){
					mid = document.getElementById('merchant_select').options[document.getElementById('merchant_select').options.selectedIndex].value;
				}
			}
			else
			{
				coupon = false;
				mid = this.model.iframeMid;
			}	
			var card = this.model.loyaltyCards[mid];			
			var hasAlt = card.alternateTitle.length > 0;	
			
			if(!this.model.cfGuest){
			var cTitle = document.createElement("div");
			cTitle.style.width = "33%";
			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.merchantName+" "+card.cardName +" #:"));
			rForm.appendChild(cTitle);
			var cElHolder = document.createElement("div");
			cElHolder.style.width = "62%";
			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 = "33%";
				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 = "62%";
				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);
			}	
			} //if(!this.model.cfGuest)
			rForm.appendChild(document.createElement("br"));
			
			var pTitle = document.createElement("div");
			pTitle.style.width = "33%";
			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 = "62%";
			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 = "33%";
			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 = "62%";
			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 = "33%";
			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 = "62%";
			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 = "33%";
			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 = "62%";
			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 = "33%";
			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 = "62%";
			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"));

  		var signUpImage = document.createElement("input");
  		signUpImage.type = "image";
  		signUpImage.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_register.png";
  		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";
  		if (index != -1){
  			loginHolder.innerHTML = "Already using " + this.model.clientName + "? <a href=\"javascript:cm.showLoginWindow('', '" + location.pathname + "?clip="+index+"&ctli=2436')\">Log in</a>";
  		}else{
  			loginHolder.innerHTML = "Already using " + this.model.clientName + "? <a href=\"javascript:cm.showLoginWindow('', '" + location.pathname + "')\">Log in</a>";
  		}
		rForm.appendChild(emElHolder);

		var sp5 = document.createElement("div");
		sp5.style.clear = "both";
		rForm.appendChild(sp5);
		
		var footer = document.createElement("div");
		footer.style.textAlign = "center";
		var whyCf = document.createElement("a");
		whyCf.href = "javascript:cui.whyCellfirePopup()";
		whyCf.appendChild(document.createTextNode("Why join "+this.model.clientName +"?"));
		footer.appendChild(whyCf);
		footer.appendChild(document.createTextNode(" | "));
		
		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"));
		footer.appendChild(tos);

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

		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"));
		footer.appendChild(privPolicy);
		
		footer.appendChild(document.createTextNode("."));
		rForm.appendChild(footer);
			
  		var handle = this.windowManager.createNewWhiteOutWindow(message, 500, "ch_" + index,true);
  		// var handle = this.windowManager.createNewBlackOutWindow(message, 500, 0, 0, true);
  		this.windowManager.showWindow(handle);
  		if (this.model.inIframe)
  		{
  			resizeIframe();
  		}	
  
  }

	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.changeStore = function(showIntro)
{
		var 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 & this.showIntro)
		{
			this.setShowIntro(false);
			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.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.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.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";
		csiDiv.style.height = "25px";

		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.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);
		this.windowManager.showWindow(handle);
}

CouponUI.prototype.showIntroPopup = function()
{
		if (!this.showIntro)
			return;
		showIntro=this.showIntro;
		this.setShowIntro(false);
		var 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";
		
		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);
		}	

		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 rForm = document.createElement("form");
		rForm.action = "http://" + location.hostname + location.pathname;
		rForm.method = "POST";
		padding.appendChild(rForm);
		
		if (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";

			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 ( 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 (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";
		csiDiv.style.height = "25px";

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

		csiDiv.appendChild(changeStoreImage);
		
		if (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, "intro_popup",true);
		this.windowManager.showWindow(handle);
}	


CouponUI.prototype.showRebateForm = function(index)
{
	var coupon = this.model.coupons[index];
	if (!coupon.rebateFormWindowHandle)
	{
		var card = this.model.loyaltyCards[coupon.merchantId];

		var width = 400;

		var content = document.createElement("div");
		content.style.textAlign = "left";
		content.style.height = "420px";
		content.style.padding = "0px 0px 0px 5px";
		var headline = document.createElement("div");
		headline.appendChild(document.createTextNode("Here's how to get your rebate:"));
		content.appendChild(headline);
		var intro1 = document.createElement("div");
		intro1.appendChild(document.createTextNode("1. Enter your information below"));
		content.appendChild(intro1);
		var intro2 = document.createElement("div");
		if(card)
			intro2.appendChild(document.createTextNode("2. Use you " + card.cardName + " when purchasing the product at " + coupon.merchantName + "."));
		else
			intro2.appendChild(document.createTextNode("2. Use you loyalty card when purchasing the product at store."));
			
		content.appendChild(intro2);
		var intro3 = document.createElement("div");
		intro3.appendChild(document.createTextNode("3. " + coupon.rebateAction));
		content.appendChild(intro3);
		var form = document.createElement("form");
		content.appendChild(form);
		form.onsubmit = function()
		{
			return cui.validateRebateForm(index);
		}	

		var errorHolder = document.createElement("div");
		errorHolder.id = 'reb_err_' + index;
		errorHolder.style.color = "#f00";
		errorHolder.innerHTML='&nbsp;';
		form.appendChild(errorHolder);

		var fName = document.createElement("div");
		fName.style.width = "33%";
		fName.style.cssFloat = "left";
		fName.style.styleFloat = "left";
		fName.style.textAlign = "right";
		fName.style.margin = "0px 10px 0px 0px";
		fName.appendChild(document.createTextNode("First Name"));
		form.appendChild(fName);

		var fNameHolder = document.createElement("div");
		fNameHolder.style.width = "62%";
		fNameHolder.style.cssFloat = "left";
		fNameHolder.style.styleFloat = "left";
		var firstName = document.createElement("input");
		firstName.id = "reb_first_name_" + index;
		firstName.name = "reb_first_name";
		firstName.type = "text";
		firstName.size = "12";
		fNameHolder.appendChild(firstName);
		form.appendChild(fNameHolder);

		var lName = document.createElement("div");
		lName.style.width = "33%";
		lName.style.cssFloat = "left";
		lName.style.styleFloat = "left";
		lName.style.textAlign = "right";
		lName.style.margin = "0px 10px 0px 0px";
		lName.appendChild(document.createTextNode("Last Name"));
		form.appendChild(lName);

		var lNameHolder = document.createElement("div");
		lNameHolder.style.width = "62%";
		lNameHolder.style.cssFloat = "left";
		lNameHolder.style.styleFloat = "left";
		var lastName = document.createElement("input");
		lastName.id = "reb_last_name_" + index;
		lastName.name = "reb_last_name";
		lastName.type = "text";
		lastName.size = "12";
		lNameHolder.appendChild(lastName);
		form.appendChild(lNameHolder);

		var street1 = document.createElement("div");
		street1.style.width = "33%";
		street1.style.cssFloat = "left";
		street1.style.styleFloat = "left";
		street1.style.textAlign = "right";
		street1.style.margin = "0px 10px 0px 0px";
		street1.appendChild(document.createTextNode("Street"));
		form.appendChild(street1);

		var street1Holder = document.createElement("div");
		street1Holder.style.width = "62%";
		street1Holder.style.cssFloat = "left";
		street1Holder.style.styleFloat = "left";
		var street1Input = document.createElement("input");
		street1Input.id = "reb_street1_" + index;
		street1Input.name = "reb_street1";
		street1Input.type = "text";
		street1Input.size = "12";
		street1Holder.appendChild(street1Input);
		form.appendChild(street1Holder);

		var street2 = document.createElement("div");
		street2.style.width = "33%";
		street2.style.cssFloat = "left";
		street2.style.styleFloat = "left";
		street2.style.textAlign = "right";
		street2.style.margin = "0px 10px 0px 0px";
		street2.appendChild(document.createTextNode("Street 2 "));
		form.appendChild(street2);

		var street2Holder = document.createElement("div");
		street2Holder.style.width = "62%";
		street2Holder.style.cssFloat = "left";
		street2Holder.style.styleFloat = "left";
		var street2Input = document.createElement("input");
		street2Input.id = "reb_street2_" + index;
		street2Input.name = "reb_street2";
		street2Input.type = "text";
		street2Input.size = "12";
		street2Holder.appendChild(street2Input);
		form.appendChild(street2Holder);

		var city = document.createElement("div");
		city.style.width = "33%";
		city.style.cssFloat = "left";
		city.style.styleFloat = "left";
		city.style.textAlign = "right";
		city.style.margin = "0px 10px 0px 0px";
		city.appendChild(document.createTextNode("City"));
		form.appendChild(city);

		var cityHolder = document.createElement("div");
		cityHolder.style.width = "62%";
		cityHolder.style.cssFloat = "left";
		cityHolder.style.styleFloat = "left";
		var cityInput = document.createElement("input");
		cityInput.id = "reb_city_" + index;
		cityInput.name = "reb_city";
		cityInput.type = "text";
		cityInput.size = "12";
		cityHolder.appendChild(cityInput);
		form.appendChild(cityHolder);

		var state = document.createElement("div");
		state.style.width = "33%";
		state.style.cssFloat = "left";
		state.style.styleFloat = "left";
		state.style.textAlign = "right";
		state.style.margin = "0px 10px 0px 0px";
		state.appendChild(document.createTextNode("State"));
		form.appendChild(state);

		var stateHolder = document.createElement("div");
		stateHolder.style.width = "62%";
		stateHolder.style.cssFloat = "left";
		stateHolder.style.styleFloat = "left";
		var stateInput = document.createElement("input");
		stateInput.id = "reb_state_" + index;
		stateInput.name = "reb_state";
		stateInput.type = "text";
		stateInput.size = "12";
		stateHolder.appendChild(stateInput);
		form.appendChild(stateHolder);

		var zip = document.createElement("div");
		zip.style.width = "33%";
		zip.style.cssFloat = "left";
		zip.style.styleFloat = "left";
		zip.style.textAlign = "right";
		zip.style.margin = "0px 10px 0px 0px";
		zip.appendChild(document.createTextNode("Zip Code"));
		form.appendChild(zip);

		var zipHolder = document.createElement("div");
		zipHolder.style.width = "62%";
		zipHolder.style.cssFloat = "left";
		zipHolder.style.styleFloat = "left";
		var zipInput = document.createElement("input");
		zipInput.id = "reb_zip_" + index;
		zipInput.name = "reb_zip";
		zipInput.type = "text";
		zipInput.size = "12";
		zipHolder.appendChild(zipInput);
		form.appendChild(zipHolder);

		var email = document.createElement("div");
		email.style.width = "33%";
		email.style.cssFloat = "left";
		email.style.styleFloat = "left";
		email.style.textAlign = "right";
		email.style.margin = "0px 10px 0px 0px";
		email.appendChild(document.createTextNode("Email"));
		form.appendChild(email);

		var emailHolder = document.createElement("div");
		emailHolder.style.width = "62%";
		emailHolder.style.cssFloat = "left";
		emailHolder.style.styleFloat = "left";
		var emailInput = document.createElement("input");
		emailInput.id = "reb_email_" + index;
		emailInput.name = "reb_email";
		emailInput.type = "text";
		emailInput.size = "12";
		emailHolder.appendChild(emailInput);
		form.appendChild(emailHolder);

		var sp1 = document.createElement("div");
		sp1.style.clear = "both";
		sp1.appendChild(document.createElement("br"));
		form.appendChild(sp1);

		var smallPrint = document.createElement("div");
		smallPrint.appendChild(document.createTextNode("We'll send a confirmation email when your rebate is processed. You must be opted in to receive emails"));
		smallPrint.appendChild(document.createElement("br"));
		var priv = document.createElement("a");
		smallPrint.appendChild(priv);
		priv.href = coupon.rebatePrivacyLink;
		priv.appendChild(document.createTextNode("Privacy Policy"));
		form.appendChild(smallPrint);

		var buttonDiv = document.createElement("div");
		buttonDiv.style.clear = "both";
		buttonDiv.style.height = "36px";
		buttonDiv.style.textAlign = "center";

		var hiddenCoupon = document.createElement("input");
		hiddenCoupon.type = "hidden";
		hiddenCoupon.name = "reb_offer_id";
		hiddenCoupon.value = coupon.offerId;
		var hiddenMerchant = document.createElement("input");
		hiddenMerchant.type = "hidden";
		hiddenMerchant.name = "reb_merchant";
		hiddenMerchant.value = coupon.merchantId;
		buttonDiv.appendChild(hiddenCoupon);
		buttonDiv.appendChild(hiddenMerchant);

		var rebButton = document.createElement("input");
		//buttonDiv.appendChild(rebButton);
		rebButton.id = "reb_submit_" + index;
		rebButton.type = "image";
		rebButton.style.padding = "10px 10px 2px 100px";
		rebButton.src = "/includes/templates/master/images/round_corners/buttonBIG_savetocard.png";
		//rebButton.onclick = new Function("cui.validateRebateForm("+index+")");
		form.appendChild(rebButton);
		form.appendChild(buttonDiv);

		coupon.rebateFormWindowHandle = wm.createNewCallOutWindow(content, width, "ch_"+index, true, 1);
	}
	else
	{
		// clear out any existing errors
		var errorHolder = document.getElementById('reb_err_' + index);
		if (errorHolder)
		{
			errorHolder.innerHTML='&nbsp;';
		}	
	}		
	wm.showWindow(coupon.rebateFormWindowHandle);
} 

CouponUI.prototype.showRebateConfirmation = function(index)
{
	var coupon = this.model.coupons[index];
	var card = this.model.loyaltyCards[coupon.merchantId];
	
	var width = 400;

	var content = document.createElement("div");
	content.style.textAlign = "left";
	content.style.padding = "0px 0px 0px 5px";
	var headline = document.createElement("div");
	headline.style.fontSize = "15px";
	headline.style.fontWeight = "bold";
	headline.appendChild(document.createTextNode("Thanks for completing the rebate form"));
	content.appendChild(headline);
	var intro2 = document.createElement("div");
	intro2.appendChild(document.createTextNode("Use you " + card.cardName + " when purchasing the product at " + card.merchantName + "."));
	content.appendChild(intro2);
	var intro3 = document.createElement("div");
	intro3.appendChild(document.createTextNode(coupon.rebateAction));
	content.appendChild(intro3);

	var smallPrint = document.createElement("div");
	smallPrint.appendChild(document.createTextNode("We'll send a confirmation email when your rebate is processed. You must be opted in to receive emails"));
	content.appendChild(smallPrint);
	
	coupon.rebateConfWindowHandle = wm.createNewCallOutWindow(content, width, "ch_"+index, true, 1);
	wm.showWindow(coupon.rebateConfWindowHandle);
} 

CouponUI.prototype.validateRebateForm = function(index)
{
	var errMessageContainer = document.getElementById('reb_err_' + index);
	errMessageContainer.innerHTML = '&nbsp;';
	var firstName = document.getElementById('reb_first_name_' + index);
	var lastName = document.getElementById('reb_last_name_' + index);
	var street1 = document.getElementById('reb_street1_' + index);
	var street2 = document.getElementById('reb_street2_' + index);
	var city = document.getElementById('reb_city_' + index);
	var state = document.getElementById('reb_state_' + index);
	var zip = document.getElementById('reb_zip_' + index);
	var email = document.getElementById('reb_email_' + index);
	var okToSubmit = firstName && firstName.value.length > 0 &&
									 lastName && lastName.value.length > 0 &&
									 street1 && street1.value.length > 0 &&
									 city && city.value.length > 0 &&
									 state && state.value.length > 0;
	var errMessage = '';								 
	if (!okToSubmit)
	{	
		errMessage += "<br/>All fields need to be completed";
	}
	if (!(zip && validate_zip(zip.value)))
	{
		okToSubmit = false;
		errMessage += "<br/>Need a five digit zip code";
	}	
	if (!(email && validate_email(email.value)))
	{
		okToSubmit = false;
		errMessage += "<br/>Need a valid email address";
	}	
	if (okToSubmit)
	{
		var coupon = this.model.coupons[index];
		this.windowManager.hideWindow(coupon.rebateFormWindowHandle);
		this.startSavingAnimation(index);
		this.model.clipRebateOffer(index, firstName.value, lastName.value, street1.value, street2.value, city.value, state.value, zip.value, email.value);
	}
	else
	{
		// flag the form
		errMessageContainer.innerHTML = errMessage;
	}	
	return false;
}	


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);
}	
CouponUI.prototype.getMerchantTile = function(merchant)
{
	if (this.model.inIframe && !this.model.inMMF) return;
	if(!this.model.showMerchantTile) return;
	var card = this.model.loyaltyCards[merchant.mid];
	var displayArea = document.getElementById("mc_" + merchant.mid);
	var mercHolder = document.createElement("div");
	mercHolder.className = "couponContainer";
	displayArea.appendChild(mercHolder);
	var mercTop = document.createElement("div");
	mercTop.className = "mercTop";
	
	var cardImage = document.createElement("img");
	cardImage.src = "/images/" + card.frontImageId+".jpg";
	cardImage.style.padding = "5px 0px";
	mercTop.appendChild(cardImage);	
	mercHolder.appendChild(mercTop);

	var infoHolder = document.createElement("div");	
	mercTop.appendChild(infoHolder);
	infoHolder.className = "couponText";
	
	var titleDiv = document.createElement("div");
	titleDiv.innerHTML = merchant.name + ' ' + card.cardName;
	titleDiv.className = "couponTextTitle";
	
	var buttonImgDiv = document.createElement("div");
	buttonImgDiv.className = "couponButton";
	var buttonImage = document.createElement("img");
	buttonImgDiv.appendChild(buttonImage);
	mercHolder.appendChild(buttonImgDiv);
	
	var infoDiv = document.createElement("div");
	infoDiv.className = "cardInfoBox";
	infoHolder.appendChild(infoDiv);
	
	var imgFolder = '/images/home';
	if (this.model.inIframe)
	{
		imgFolder = '/includes/templates/master/images/newlook/' + this.model.iframeCssId;
	}
	
	if (this.model.user.authenticated || card.status=='2')
	{ //logged in 
		infoDiv.appendChild(titleDiv);
		
		var savings = 0;
		var numSaved = 0;
		var coupon;
		for(var couponId in this.model.coupons)
		{
			coupon = this.model.coupons[couponId];	
			if (coupon && coupon.isSaved && coupon.merchantId == merchant.mid)
			{
				if(!coupon.isRebate)
					savings += parseFloat(coupon.faceValue);
				numSaved++;
			}	
		}	
		savings = savings.toFixed(2);
		switch(parseInt(card.status)){
			case 0: //0 Card not added
				infoDiv.appendChild(document.createTextNode('Add your'));
				infoDiv.appendChild(titleDiv);
				infoDiv.appendChild(document.createTextNode('to clip these coupons'));
				buttonImage.src = imgFolder + "/merc_button_addcard.png";
				buttonImage.onclick = new Function("cui.showAddCardNoOffer("+merchant.mid+");");
				buttonImage.style.cursor = "pointer";
				break;
			case 1: //1 Pending
			case 2: //2 added successfully
				statusMessage = card.numSavedToCard+" saved offer"+(card.numSavedToCard > 1?"s":"");
				if(card.capacity < 1000) statusMessage += " (you can save "+(card.capacity-card.numSavedToCard)+" more)";
				var numdisplay = card.cardNum;
				if (!numdisplay)
				{
					numdisplay = "pending";
				}else if(this.model.user.authenticated){
					numdisplay = "<a href=\"javascript:lcui.showUpdateCardFormWindow("+ merchant.mid +", function(){document.location.reload(true)})\">" + numdisplay + "</a>";
				}else{
					numdisplay = "<a href=\"javascript:lcui.forgetCard();\">" + numdisplay + "</a>";
				}
				
				var rowCard = document.createElement("div");
				rowCard.className = "couponCardRow";

				var cardNum = document.createElement("div");
				cardNum.innerHTML = numdisplay;
				rowCard.appendChild(cardNum);
				infoDiv.appendChild(rowCard);
				
				/*				
				if(numdisplay != ""){
					cardAltValue.appendChild(document.createTextNode(numdisplay));
					cardAltLabel.appendChild(document.createTextNode(card.alternateTitle+":"));
				}
				*/
				
				var rowTotal = document.createElement("div");		
				rowTotal.className = "couponCardRow";

				var clippedText = ' coupons clipped';
				if (numSaved == 1)
				{
					clippedText = ' coupon clipped';
				}
				var totalValue = document.createElement("span");
				totalValue.id = "tv_" + merchant.mid;
				totalValue.innerHTML = "" + numSaved + clippedText;
				rowTotal.appendChild(totalValue);
				infoDiv.appendChild(rowTotal);
				
				var rowSaving = document.createElement("div");
				rowSaving.className = "couponCardRow";

				var savingTitle = document.createElement("div");
				savingTitle.className = "couponTextLeft";
				savingTitle.appendChild(document.createTextNode("Savings Value"));

				var savingValue = document.createElement("span");
				savingValue.id = "sv_" + merchant.mid;
				savingValue.innerHTML = "$" + savings;
				rowSaving.appendChild(document.createTextNode('worth '));
				rowSaving.appendChild(savingValue);
				rowSaving.appendChild(document.createTextNode(' in savings'));
				infoDiv.appendChild(rowSaving);
				
				//create shopping list

				buttonImage.src= imgFolder + "/merc_button_createshoppinglist.png";
				buttonImage.id = "shoppingList";
				buttonImage.onclick = new Function("cui.createShoppingListPopup('"+merchant.mid+"');");
				buttonImage.style.cursor = "pointer";
				buttonImage.style.display = "block";
				break;
				
			default:
				infoDiv.appendChild(document.createTextNode("There was a problem adding your card!"));
				buttonImage.src = imgFolder + "/merc_button_addcard.png";
				buttonImage.onclick = new Function("cui.showAddCardNoOffer("+merchant.mid+");");
				buttonImage.style.cursor = "pointer";
				break;
		}		
	}
	else
	{//logged out
		if(!this.model.cfGuest){// non xuser
			infoDiv.appendChild(document.createTextNode('Register and add your'));
			infoDiv.appendChild(titleDiv);
			infoDiv.appendChild(document.createTextNode('to clip these coupons'));
			buttonImage.src = imgFolder + "/merc_button_register.png";
			if (this.model.inIframe)
			{
				buttonImage.id = 'mmf_merch_reg_button';
				buttonImage.onclick = new Function("wm.setLastAnchor('mmf_merch_reg_button');cui.showRegForm(-1, false)");
			}
			else
			{
				buttonImage.onclick = new Function("window.location='/getcfnow/index.php?szc="+this.model.user.currentSearch[0]+"'");
			}
		}else{ // xuser version
			infoDiv.appendChild(document.createTextNode('Add your'));
			infoDiv.appendChild(titleDiv);
			infoDiv.appendChild(document.createTextNode('to clip these coupons'));
			buttonImage.src = imgFolder + "/merc_button_addcard.png";
			buttonImage.onclick = new Function("cui.showXuserPopup("+merchant.mid+",-1);");
			
		}
		buttonImage.style.cursor = "pointer";
	}
	//return mercHolder;

}
CouponUI.prototype.mobilePopup  = function(){
	if(!this.model.showMobilePopup){
		window.open('/help/ps_help.php?reg_type=0&amp;helpId=2','phone_picker');
		return;
	}	
	var content = document.getElementById("openCf");
	if(content) removeChildElements(content);
	var content = document.createElement("div");
	content.id= "openCf";
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.fontSize="18px";
	title.style.fontWeight = "bold";
	title.style.textAlign = "left";
	
	var note = document.createElement("div");
	note.id = "note";
	note.style.fontSize="14px";
	note.style.padding = "10px 0px";
	note.style.textAlign = "left";
	title.innerHTML ="Open Cellfire in New Window?";
	note.innerHTML ="Click \"OK\" to open Cellfire.com in a new window. There you'll find instructions on how to access"+
					"Cellfire Mobile with your mobile phone.";

	var remHolder =document.createElement("div");
	remHolder.style.padding = "0px 0px 10px 0px";
	var remInput = document.createElement("input");
	remInput.type = "checkbox";
	remInput.name = "mobileWin";
	remInput.id = "mobileWin";
	remInput.value = "0";
	remHolder.appendChild(remInput);
	remHolder.appendChild(document.createTextNode("Don't show this again"));

	content.appendChild(title);
	content.appendChild(note);
	content.appendChild(remHolder);
	var button_div = document.createElement("div");
	button_div.style.textAlign = "center";
	var ok = document.createElement("input");
	ok.type = "button";
	ok.value = "OK";	
	ok.onclick = new Function("if(document.getElementById('mobileWin').checked) model.setShowMobilePopup(false); wm.closeOpenWindow();window.open('/help/ps_help.php?reg_type=0&amp;helpId=2','phone_picker');");

	button_div.appendChild(ok);
	content.appendChild(button_div);
	var handle = this.windowManager.createNewWhiteOutWindow(content, 380, 0, true);
	this.windowManager.showWindow(handle);
}
CouponUI.prototype.showXuserPopup= function(merchantId, index)
{
	var card = this.model.user.loyaltyCards[merchantId];
	var coupon;
	var mid = merchantId;
	if (index != -1)	{
		coupon = this.model.coupons[index];
	}else	{
		coupon = false;
	}	
	var hasAlt = card.alternateTitle.length > 0;	
	var clipOffer = true;
	
	var padding = document.createElement("div");
	padding.id = "xuser_popup";
	padding.style.padding = "12px";
	padding.style.fontSize = "12px";
	padding.style.textAlign = "left";
	
	var title = document.createElement("div");
	title.style.padding = "0px 0px 5px 0px";
	title.style.width = "100%";
	title.style.fontSize = "23px";
	title.style.fontWeight = "bold";
	title.style.textAlign = 'left';
	title.innerHTML = "Add Your "+ card.merchantName + " " + card.cardName;
	padding.appendChild(title);
	
	var cardHolder = document.createElement("div");
	cardHolder.id = "cardHolder";
	cardHolder.style.width = "62%";
	cardHolder.style.height = "100px";
	cardHolder.style.cssFloat = "left";
	cardHolder.style.styleFloat = "left";
	cardHolder.style.textAlign = "left";
	cardHolder.style.margin = "0px 10px 0px 0px";
	padding.appendChild(cardHolder);
	
	var errorHolder = document.createElement("div");
	errorHolder.id = 'card_num_error';
	errorHolder.style.color = "#f00";
	errorHolder.innerHTML='&nbsp;';

	var cTitle = document.createElement("div");
	cTitle.style.width = "240px";
	cTitle.style.height = "20px";
	//cTitle.style.cssFloat = "left";
	//cTitle.style.styleFloat = "left";
	cTitle.style.margin = "0px 10px 0px 0px";
	cTitle.id = "regForm_c";
	cTitle.style.fontWeight = "bold";
	cTitle.appendChild(document.createTextNode(card.merchantName + " " + card.cardName));
	var cHolder = document.createElement("div");
	//cHolder.style.cssFloat = "left";
	//cHolder.style.styleFloat = "left";
	var cNumber = document.createElement("input");
	cHolder.appendChild(cNumber);
	cNumber.id = "lc_number_"+mid;
	cNumber.name = "lc_number_"+mid;
	cNumber.type = "text";
	cNumber.size = "20";
	cNumber.onfocus = new Function("document.getElementById('card_num_error').innerHTML = '';");
	if(this.model.user.loyaltyCards[mid].cardNum>0)
		cNumber.value = this.model.user.loyaltyCards[mid].cardNum;
	var remHolder =document.createElement("div");
	if(!this.model.remCard){
		var remInput = document.createElement("input");
		remInput.type = "checkbox";
		remInput.name = "remCard";
		remInput.id = "remCard";
		remInput.value = "1";
		remInput.checked = true;
		remHolder.appendChild(remInput);
		remHolder.appendChild(document.createTextNode("Remember on this computer"));
	}
	
	cardHolder.appendChild(cTitle);
	cardHolder.appendChild(cHolder);
	cardHolder.appendChild(remHolder);
	cardHolder.appendChild(errorHolder);
	padding.appendChild(document.createElement("br"));
	
	var imgHolder = document.createElement("div");
	imgHolder.style.width = "30%";
	imgHolder.style.height = "70px";
	imgHolder.style.cssFloat = "left";
	imgHolder.style.styleFloat = "left";
	imgHolder.style.margin = "0px 10px 0px 0px";
	var cardImg = document.createElement("img");
	cardImg.src="/images/" + card.frontImageId+".jpg";
	imgHolder.appendChild(cardImg);
	padding.appendChild(imgHolder);
	
	var clear = document.createElement("div");
	clear.style.clear = "both";
	padding.appendChild(clear);

	var addCardHolder = document.createElement("div");
	addCardHolder.style.height = "23px";
	addCardHolder.style.lineHeight = "23px";
	addCardHolder.style.align = "left";
	var signUpImage = document.createElement("input");
	signUpImage.type = "image";
	if (this.model.inIframe && index != -1 )
	{
		signUpImage.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_addcard.png";
		var merchant = this.model.merchants[this.model.iframeMid];
		//signUpImage.onclick="pageTracker._trackEvent('Popups', 'Submit', '" + merchant.mid + "_QFrame_Reg')";
	}else if(index != -1){
		signUpImage.src = "/includes/templates/master/images/newlook/buttonBIG_savetocard.png";
	}else
	{ // no offer just add card
		if (this.model.inIframe)
		{
			signUpImage.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_addcard.png";
		}
		else
		{
			signUpImage.src = "/images/home/button_addcard.png";
		}
	}
	
	if(!this.model.remCard)
		signUpImage.onclick = new Function("if(cui.validateAddCardForm(document.getElementById('lc_number_"+mid+"').value))  cui.clipWithLoyaltyCard(document.getElementById('lc_number_"+mid+"').value,"+mid+","+index+",document.getElementById('remCard').checked);");
	else
		signUpImage.onclick = new Function("if(cui.validateAddCardForm(document.getElementById('lc_number_"+mid+"').value))  cui.clipWithLoyaltyCard(document.getElementById('lc_number_"+mid+"').value,"+mid+","+index+",model.remCard);");
		
	//signUpImage.onclick = new Function("alert("+index+");");
	var reg_type = 0;

	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 = '';
  	}	
	
	addCardHolder.appendChild(signUpImage); 
	addCardHolder.appendChild(rId);
	addCardHolder.appendChild(oId);
	addCardHolder.appendChild(mId);
	addCardHolder.appendChild(cId);
	padding.appendChild(addCardHolder);

	addCardHolder.appendChild(document.createTextNode(" OR "));

	var loginButton = document.createElement("img");
	if (this.model.inIframe)
	{
		loginButton.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_login.png";
	}
	else
	{
		loginButton.src = "/images/home/button_login.png";
	}
	if (index != -1){
		loginButton.onclick = new Function("cm.showLoginWindow('', '" + location.pathname + "?clip="+index+"&ctli=2436');");
	}else{
		loginButton.onclick = new Function("cm.showLoginWindow('', '" + location.pathname + "');");
	}
	addCardHolder.appendChild(loginButton);
	
	var supportMessage = document.createElement("div");
	supportMessage.style.textAlign = "left";
	supportMessage.style.margin = "5px 0px 0px 0px";
	supportMessage.innerHTML = card.supportHTML+"<br><br>";
	padding.appendChild(supportMessage);
	
	var privPolicy = document.createElement("a");
	if (this.model.iframeCssId == 'vzn')
	{
		privPolicy.href = "http://www.verizon.com/privacy";
		privPolicy.appendChild(document.createTextNode("Verizon Privacy Policy"));
	}
	else
	{
		privPolicy.href = "/privacy_policy.php";
		privPolicy.appendChild(document.createTextNode(" Cellfire Privacy Policy"));
	}	
	privPolicy.target = "_blank";
	padding.appendChild(privPolicy);
	
	if(this.model.inIframe)//in qframe
		var handle = this.windowManager.createNewWhiteOutWindow(padding, 500, "add_card_" + index, true);
	else
		var handle = this.windowManager.createNewBlackOutWindow(padding, 500, 0, 0, true);
	this.windowManager.showWindow(handle);
}
CouponUI.prototype.validateAddCardForm = function(cardId){
	//card_num_error
	var errMessageContainer = document.getElementById('card_num_error');
	if(isNaN(cardId) || cardId=="") {
		errMessageContainer.innerHTML = "This is not a correct number. Please enter a valid number.";
		return false;
	}else {
		return true;
	}
}
CouponUI.prototype.clipWithLoyaltyCard = function(cardNum,mid,couponId,remCard){
 	this.windowManager.closeOpenWindow();
	if(couponId!='-1'){
		this.startSavingAnimation(couponId);
		this.model.coupons[couponId].working = true;
		var coupon = this.model.coupons[couponId];
	}
  	// Do the Ajax
 	var req = getXMLHttpObject();
 	req.onreadystatechange = function(){
 		if(req.readyState == 4){
 			if(req.responseText == "1" ){//clip success
 				var card = _MODEL.loyaltyCards[mid];
 				if(coupon){
 					var callback;
 					if(_MODEL.clientName=="Spend Smart") callback = new Function("lcui.createShoppingList("+mid+",true)");
 					else callback = new Function("lcui.createShoppingList("+mid+",false)");
					if(_MODEL.inIframe)callback();
					
 					_MODEL.saveSuccess(""+couponId);
 				}
 				if(card.cardNum == '') {
	 				card.cardNum = cardNum;
	 				card.status = 2;
	 				var cardLink = document.getElementById("manage_card_forget");
	 				if(cardLink) cardLink.style.display = "block";	 				
	 				_MODEL.fireEvent("LoyaltyCardChange");
	 				if(coupon)
	 				{
	 					//_MODEL.fireEvent("ClippingwithLoyaltyCard");
	 					var callback = new Function("cui.onClippingwithLoyaltyCard("+mid+")");
	 				}
	 				else 
	 				{
	 					var callback = new Function("lcui.onAddCardCallback("+mid+","+req.responseText+",false)");
	 				}
					callback();
 				}
 			}else {//clip fail
 				if(coupon){
	  				coupon.hasChanged = true;
	 				coupon.working = false;
	 				_MODEL.fireEvent("CouponChange");
	 				_MODEL.saveFailed(""+couponId,req.responseText);
 				}else{ //association failed
 					var callback = new Function("lcui.onAddCardFailed("+mid+","+req.responseText+")");
 					callback();
  				}
 			}
  		} 	
  		
 	};
 	var openString = "/includes/form_handlers/clip_with_loyalty_card.php?mid="+mid+"&cid="+couponId+"&lid="+cardNum+"&remcard="+remCard;
 	req.open("GET",openString,true);
 	req.send(null);
}  

CouponUI.prototype.onClippingwithLoyaltyCard = function(mid)
{
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.fontSize="16px";
	title.style.fontWeight = "bold";
	title.style.textAlign = "left";
	
	var note = document.createElement("div");
	note.id = "note";
	note.style.fontSize="14px";
	note.style.padding = "10px 0px";
	note.style.textAlign = "left";
	var cardText = 'card/account';
	var time = '10';
	if (mid)
	{
		var card = this.model.loyaltyCards[mid];
		cardText = card.merchantName + ' ' + card.cardName;
		time = card.processTime;
	}

	title.innerHTML ="Coupons Clipped!";
	note.innerHTML =
	"Please wait 10 minutes before attempting to redeem them at the store."+
	"To redeem, just use your " + cardText + " at checkout!" +
	'<br/><br/>You can continue clipping more coupons.';
	content.appendChild(title);
	content.appendChild(note);
	
	var okButtonContainer = document.createElement("div");
	okButtonContainer.style.height = "25px";
	okButtonContainer.style.textAlign = "center";
	var okButton = document.createElement("img");
	var styleId = 'mmc';
	if (this.model.inIframe)
	{
		styleId = this.model.iframeCssId;
	}

	okButton.src = "/includes/templates/affiliate/" + styleId + "/button_ok.png";
	okButton.onclick = function(){wm.closeOpenWindow()};
	okButton.style.cursor = "pointer";
	okButton.alt = "OK";
	okButtonContainer.appendChild(okButton);
		
	content.appendChild(okButtonContainer);
	
	
	if(this.model.inIframe)//in qframe
		var handle = this.windowManager.createNewWhiteOutWindow(content, 400, 0, true);
	else
		var handle = this.windowManager.createNewBlackOutWindow(content, 400, 0, 0, true);
	this.windowManager.showWindow(handle);
}  

CouponUI.prototype.couponSaveSuccess  = function(showSaveSuccessPopup){
	//alert("success");
	//return;
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.fontSize="16px";
	title.style.fontWeight = "bold";
	title.style.textAlign = "left";
	
	var note = document.createElement("div");
	note.id = "note";
	note.style.fontSize="12px";
	note.style.padding = "10px 0px";
	note.style.textAlign = "left";
	title.innerHTML ="Offer clipped!";
	note.innerHTML ="Your coupon is saved.  ";
	content.appendChild(title);
	content.appendChild(note);
	if(this.model.inIframe)	
		var handle = this.windowManager.createNewWhiteOutWindow(content, 350, 0, true);
	else
		var handle = this.windowManager.createNewBlackOutWindow(content, 350, 0, 0, true);
	this.windowManager.showWindow(handle);
	
}
CouponUI.prototype.couponSaveFail  = function(responseText){
	//alert("failed");
	//return;
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.fontSize="16px";
	title.style.fontWeight = "bold";
	title.style.textAlign = "left";
	
	var note = document.createElement("div");
	note.id = "note";
	note.style.fontSize="12px";
	note.style.padding = "10px 0px";
	note.style.textAlign = "left";	
	
	title.innerHTML ="Can't save offer!";
	note.innerHTML ="We are having problem saving your coupon to the card. Please check back later. ";	
	content.appendChild(title);
	content.appendChild(note);
	if(this.model.inIframe)	
		var handle = this.windowManager.createNewWhiteOutWindow(content, 350, 0, true);
	else
		var handle = this.windowManager.createNewBlackOutWindow(content, 350, 0, 0, true);
	this.windowManager.showWindow(handle);
	
}
CouponUI.prototype.showAskEmailPopup  = function(){
  	this.windowManager.closeOpenWindow();
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.padding = "0px 0px 0px 10px";
	title.style.fontSize="16px";
	title.style.fontWeight = "bold";
	title.style.textAlign = "left";
	title.innerHTML ="Get Cellfire Alerts! ";
	
	var list = document.createElement("ul");
	list.style.textAlign ="left";
	var litem1 = document.createElement("li");
	litem1.innerHTML = "Receive emails when new offers are available";
	var litem2 = document.createElement("li");
	litem2.innerHTML = "Learn about our special promotions";
	var litem3 = document.createElement("li");
	litem3.innerHTML = "Get statements that track the $$ you save";
	var litem4 = document.createElement("li");
	litem4.innerHTML = "Be reminded when saved coupons are expiring";
	list.appendChild(litem1);
	list.appendChild(litem2);
	list.appendChild(litem3);
	list.appendChild(litem4);
	
	var note = document.createElement("div");
	note.id = "note";
	note.style.fontSize = "12px";
	note.style.fontWeight = "bold";
	note.style.padding = "10px 0px";
	note.style.textAlign = "left";
	note.innerHTML ="To receive alerts, enter your email address";	
	
	var cHolder = document.createElement("div");
	cHolder.style.cssFloat = "left";
	cHolder.style.styleFloat = "left";
	var cEl = document.createElement("input");
	cHolder.appendChild(cEl);
	cEl.id = "email_in";
	cEl.type = "text";
	cEl.size = "30";	
	var errorHolder = document.createElement("div");
	errorHolder.id = 'email_error';
	//errorHolder.style.cssFloat = "left";
	//errorHolder.style.styleFloat = "left";
	errorHolder.style.color = "#f00";
	errorHolder.innerHTML='&nbsp;';

	var sp3 = document.createElement("div");
	sp3.style.clear = "both";
	
	
	var addEmailHolder = document.createElement("div");
	addEmailHolder.style.textAlign = "left";
	addEmailHolder.style.padding =" 10px 0px";
	addEmailHolder.style.height = "30px";
	addEmailHolder.style.width = "100%";
	var subdiv = document.createElement("div");
	subdiv.style.cssFloat = "left";
	subdiv.style.styleFloat = "left";
	var submitButton = document.createElement("input");
	submitButton.type = "button";
	submitButton.style.fontSize = "13px";
	addEmailHolder.appendChild(subdiv);
	subdiv.appendChild(submitButton);
	submitButton.value = "Submit";
	//submitButton.src = "/includes/templates/master/images/button_submit_big.jpg";
	submitButton.onclick = new Function("if(!validate_email(document.getElementById('email_in').value)) document.getElementById('email_error').innerHTML='Please enter a valid email address'; else cui.updateUserEmail(document.getElementById('email_in').value);");
	
	var canceldiv = document.createElement("div");
	canceldiv.style.cssFloat = "left";
	canceldiv.style.styleFloat = "left";
	canceldiv.style.padding = "0px 20px";
	addEmailHolder.appendChild(canceldiv);
	
	var cancelButton = document.createElement("input");
	cancelButton.type = "button";
	canceldiv.appendChild(cancelButton);
	cancelButton.style.fontSize = "13px";
	cancelButton.value = "No Thanks";
	cancelButton.onclick = new Function("cui.windowManager.closeOpenWindow();cui.setNeverAskEmail();");//this.windowManager.closeOpenWindow();

	content.appendChild(title);
	content.appendChild(list);
	content.appendChild(note);
	content.appendChild(cHolder);
	cHolder.appendChild(errorHolder);	
	content.appendChild(sp3);
	content.appendChild(addEmailHolder);
	var handle = this.windowManager.createNewBlackOutWindow(content, 420, 0, 0, true);
	this.windowManager.showWindow(handle);
}
CouponUI.prototype.setNeverAskEmail  = function( ){
  	this.windowManager.closeOpenWindow();
  	// Do the Ajax
 	var req = getXMLHttpObject();
 	req.onreadystatechange = function(){
 		if(req.readyState == 4){
 			if(req.responseText == "0"){ //success
 				//alert("Updated");
 			}else{
 				//alert("Sorry! Can't update the timestamp");
 			}
	 	}
 	};
 	var openString = "/includes/form_handlers/add_email.php?neverAskAgain=1";
 	req.open("GET",openString,true);
 	req.send(null);
}
//setNeverAskEmail
CouponUI.prototype.updateUserEmail  = function( email){
  	this.windowManager.closeOpenWindow();
  	// Do the Ajax
 	var req = getXMLHttpObject();
 	req.onreadystatechange = function(){
 		if(req.readyState == 4){
 			if(req.responseText == "0"){ //success
 				//alert("Great! We'll send Cellfire alerts to "+email+". You can always change your email preferences in Settings.");
 				cui.emailAddedSuccess(email);
 			}else{
 				//alert("Sorry! We'll having problem to add your email. Please try agin later.");
 				cui.emailAddedFail(email);
 			}
	 	}
 	};
 	var openString = "/includes/form_handlers/add_email.php?as_email="+email;
 	req.open("GET",openString,true);
 	req.send(null);
   	
	//alert(email);
}
CouponUI.prototype.emailAddedSuccess  = function(email){
  	this.windowManager.closeOpenWindow();
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.padding = "0px 0px 0px 10px";
	title.style.fontSize="15px";
	title.style.textAlign = "left";
	title.innerHTML ="Great! We'll send Cellfire alerts to "+email+". You can always change your email preferences in Settings.";
	content.appendChild(title);
	var handle = this.windowManager.createNewBlackOutWindow(content, 350, 0, 0, true);
	this.windowManager.showWindow(handle);
	
}

CouponUI.prototype.showRewardPopup  = function(reward){
  	this.windowManager.closeOpenWindow();
  	
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.padding = "0px 0px 0px 10px";
	title.style.fontSize="15px";
	title.style.fontWeight="bold";
	title.style.textAlign = "left";
	var note = document.createElement("div");
	note.style.padding = "10px 0px 0px 10px";
	note.style.fontSize="12px";
	note.style.textAlign = "left";	
	if(reward>0){
		title.innerHTML ="Welcome to Cellfire Happy Hour!";
		note.innerHTML = 'We just wanted to say thank you for being a loyal Cellfire user. There\'s a special coupon waiting for you after you close this popup. Look for it among the coupons and save it to your card RIGHT AWAY because it won\'t be around for long.<br/><br/>For official rules and other awesomeness, check out our <a href="http://www.facebook.com/cellfirecoupons" target="_blank">Facebook</a> page.<br/><br/>Cheers and thanks for choosing Cellfire!';
	}
	else {
			//title.innerHTML ="Sorry, you didn&#39;t win a Cyber Monday Coupon. " ;
			//note.innerHTML = "Thanks for participating and look for more promotions in the future. Check out our coupons with more than $25 in savings. ";
		return;
	}
	content.appendChild(title);
	content.appendChild(note);
	if(this.model.inIframe)	
		var handle = this.windowManager.createNewWhiteOutWindow(content, 450, 0, true);
	else
		var handle = this.windowManager.createNewBlackOutWindow(content, 450, 0, 0, true);
	this.windowManager.showWindow(handle);
	
}
CouponUI.prototype.emailAddedFail  = function(email){
  	this.windowManager.closeOpenWindow();
	var content = document.createElement("div");
	content.style.padding ="10px 0px";		
	var title = document.createElement("div");
	title.style.padding = "0px 0px 0px 10px";
	title.style.fontSize="16px";
	title.style.textAlign = "left";
	title.innerHTML ="Sorry! We are having problem adding your email. Please try again later.";
	content.appendChild(title);
	var handle = this.windowManager.createNewBlackOutWindow(content, 350, 0, 0, true);
	this.windowManager.showWindow(handle);
	
}
