/**
* The LoyaltyCardUI class
* Will handle, display and window creation for LoyaltyCard managment
*/

function LoyaltyCardUI(modelObj, listEleId, windowManager){
	this.model = modelObj;
	this.listEleId = listEleId;
	this.windowManager = windowManager;
	this.addCardFormWindowHandle = false;
	this.addCardSuccessWindowHandle = false;
	this.inSubmit = false;
	
	wm = this.windowManager;
	lcui = this;
}

LoyaltyCardUI.prototype.showAddCardFormWindowGenericError = function(merchantId, offerId, successCallback)
{
	this.showAddCardFormWindow(merchantId, offerId, successCallback);
	var topTitle = document.getElementById("add_card_error_"+merchantId);
	removeChildElements(topTitle);
	topTitle.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."));
	topTitle.className = "error";
}

LoyaltyCardUI.prototype.showAddCardFormWindow = function(merchantId, offerId, successCallback)
{
	var card = this.model.loyaltyCards[merchantId]; 

	var addCardForm = this.buildAddCardForm(merchantId, false);
	card.addCardFormWindowHandle = this.windowManager.createNewBlackOutWindow(addCardForm, 400, 0, 0, true);
	card.successCallback = successCallback;
	var offerInput = document.getElementById("add_card_offer_"+merchantId);
	offerInput.value = offerId;
	this.windowManager.showWindow(card.addCardFormWindowHandle);
}
LoyaltyCardUI.prototype.onAddCardFailed= function(merchantId, errorCode){
	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 ="Add Failed!";
	var note = document.createElement("div");
	note.id = "note";
	note.style.fontSize="14px";
	note.style.padding = "10px 0px";
	note.style.textAlign = "left";
	if(errorCode == '3')
	{
		var card = this.model.loyaltyCards[merchantId]; 
		note.innerHTML ="Sorry, that is not a valid number, please check your " + card.merchantName + " " + card.cardName + " number and enter again!";
	}
	else if(errorCode == '2')
	{
		note.innerHTML ="Whoops! You've already added this card for a different grocer. To add it for this grocer, first remove the existing card. Or try using a different card number.";
	}
	else
	{
		note.innerHTML ="We are currently having technical difficulties, please try again soon.";
	}
	
	content.appendChild(title);
	content.appendChild(note);

	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);	
}

LoyaltyCardUI.prototype.showUpdateCardFormWindow = function(merchantId, successCallback)
{
	var card = this.model.loyaltyCards[merchantId]; 
	//if(!card.addCardFormWindowHandle)
	//{
		var addCardForm = this.buildAddCardForm(merchantId, true);
		card.successCallback = successCallback;
		card.addCardFormWindowHandle = this.windowManager.createNewBlackOutWindow(addCardForm, 400, 0, 0, true);
	//}
	
	var topTitle = document.getElementById("add_card_error_"+merchantId);
	removeChildElements(topTitle);
	//topTitle.appendChild(document.createTextNode("Are you sure you want to change your " + card.merchantName + " " + card.cardName + "?"));
	// topTitle.className = "error";
	topTitle.style.fontWeight = "normal";
	topTitle.style.fontSize = "12px";
	topTitle.style.color = "#000000";
	//var ttSecond = document.createElement("span");
	//topTitle.appendChild(ttSecond);
	//ttSecond.style.fontWeight = "bold";
	//ttSecond.appendChild(document.createTextNode(" You will lose all offers that are saved to your card."));
	
	var existingCardNum = document.getElementById("lc_number_" + merchantId);
	if (existingCardNum)
	{
		existingCardNum.value = card.cardNum;
	}	

	this.windowManager.showWindow(card.addCardFormWindowHandle);
}

LoyaltyCardUI.prototype.buildAddCardForm = function(merchantId, doUpdate)
{
	var button;
	var mid = merchantId;
	var card = this.model.loyaltyCards[merchantId];
	var hasAlt = card.alternateTitle.length > 0;	

	var cont =document.getElementById( "add_card_cont");
	if(cont) removeChildElements(cont);
	//if(!cont) {
	var cont = document.createElement('div');
	//cont.id = "add_card_cont_"+mid;
	cont.id = "add_card_cont";
	var top = document.createElement('div');
	top.id = "add_card_top_"+mid;
	var body = document.createElement('div');
	body.id = "add_card_elements_"+mid;
	
	var offerInput = document.createElement("input");
	offerInput.id = "add_card_offer_"+merchantId;
	offerInput.type = "hidden";
	cont.appendChild(offerInput);
	
	if (doUpdate)
	{
		var updateInput = document.createElement("input");
		updateInput.id = "update_card_"+merchantId;
		updateInput.type = "hidden";
		updateInput.value = "1";
		cont.appendChild(updateInput);
	}
	
	var newWord = "";

	var title = document.createElement("div");
	title.style.fontSize = "18px";
	title.style.fontWeight = "bold";
	title.style.margin = "10px 0px 0px 0px";
	title.style.color = "black";
	title.style.textAlign = "left";
	if (doUpdate && this.model.user.authenticated)
	{
		title.appendChild(document.createTextNode("Update your "+card.merchantName+" "+card.cardName));
		newWord = "";
	}else if(doUpdate){
		title.appendChild(document.createTextNode("Forget All"));
		newWord = "";
	}
	else
	{
		title.appendChild(document.createTextNode("Add your "+card.merchantName+" "+card.cardName));
	}	
	top.appendChild(title);
	
	var error = document.getElementById("add_card_error_"+merchantId);
	if(error);
	removeChildElements(error);	
	var topTitle = document.createElement("div");
	topTitle.style.height = "20px";
	topTitle.style.fontSize = "11px";
	topTitle.style.textAlign = "left";
	topTitle.id = "add_card_error_"+mid;
	top.appendChild(topTitle);
	top.appendChild(document.createElement("br"));
	var clear = document.createElement("div"); 
	clear.className = "clear"; 
	clear.style.height = "1px";
	top.appendChild(clear);
	
	var cardNumDiv = document.createElement("div");
	var image_holder = document.createElement('div');
	image_holder.style.cssFloat = "right";
	image_holder.style.styleFloat = "right";
	image_holder.style.width = "150px";
	image_holder.style.height = "60px";
	image_holder.style.margin = "5px 10px 5px 10px";
	var image = document.createElement('img');
	image.src = "/images/"+card.frontImageId+".jpg";
	image_holder.appendChild(image);
	var cardNum_holder = document.createElement("div");
	cardNumDiv.appendChild(cardNum_holder);
	cardNumDiv.appendChild(image_holder);
	cardNum_holder.style.cssFloat = "left";
	cardNum_holder.style.styleFloat = "left";
	cardNum_holder.style.width = "40%";
	cardNum_holder.style.height = "30px";
	var cardNum_label = document.createElement("span");
	cardNum_label.style.fontSize = "12px";
	cardNum_label.style.fontWeight = "bold";
	cardNum_label.appendChild(document.createTextNode(card.merchantName+" "+card.cardName));
	cardNum_holder.appendChild(cardNum_label);
	cardNum_holder.appendChild(document.createElement("br"));
	var cardNum_input = document.createElement("input")
	cardNum_input.type = "text";
	cardNum_input.size = "12";
	cardNum_input.name = "lc_number_"+mid;
	cardNum_input.id = "lc_number_"+mid;
	cardNum_input.onkeypress = new Function("event",""+
		"var code;"+
		"if(window.event)"+
			"code = window.event.keyCode;"+
		"else if(event.which)"+
			"code = event.which;"+
		"if(code == 13)" +
			"lcui.submitAddCardForm("+mid+");");
	
	cardNum_holder.appendChild(cardNum_input);
	cardNum_holder.appendChild(document.createElement('br'));
	
	var or_holder = document.createElement('div');
	or_holder.style.width = "10%";
	or_holder.style.fontSize = "12px";
	or_holder.style.fontWeight = "bold";
	or_holder.style.cssFloat = "left";
	or_holder.style.styleFloat = "left";
	or_holder.style.color = "black";
	or_holder.style.textAlign = "center";
	or_holder.appendChild(document.createElement("br"));
	or_holder.appendChild(document.createTextNode("OR"));
	if (hasAlt)
	{
		var altNum_holder = document.createElement("div");
		altNum_holder.style.cssFloat = "right";
		altNum_holder.style.styleFloat = "right";
		altNum_holder.style.width = "50%";
		var altNum_label = document.createElement("span");
		altNum_label.style.fontSize = "12px";
		altNum_label.style.fontWeight = "bold";
		altNum_label.appendChild(document.createTextNode("Associated "+card.alternateTitle));
		altNum_holder.appendChild(altNum_label);
		altNum_holder.appendChild(document.createElement("br"));
		var altNum_input = document.createElement("input");
		altNum_input.type = "text";
		altNum_input.size = "13";
		altNum_input.name = "lc_alt_"+mid;
		altNum_input.id = "lc_alt_"+mid;
		altNum_input.onkeypress = new Function("event",""+
			"var code;"+
			"if(window.event)"+
				"code = window.event.keyCode;"+
			"else if(event.which)"+
				"code = event.which;"+
			"if(code == 13)" +
				"lcui.submitAddCardForm("+mid+");");
		altNum_holder.appendChild(altNum_input);
	}	
	else
	{
		var noAlt_holder = document.createElement("div");
		noAlt_holder.style.width = "50%";
		noAlt_holder.style.cssFloat = "right";
		noAlt_holder.style.styleFloat = "right";
		var altNum_input = document.createElement("input");
		altNum_input.type = "hidden";
		altNum_input.value = "";
		altNum_input.name = "lc_alt_"+mid;
		altNum_input.id = "lc_alt_"+mid;
		noAlt_holder.appendChild(altNum_input);
		var noAltMessage = document.createElement("div");
		noAlt_holder.appendChild(noAltMessage);
	}	
	
	var  buttonAlign = "left";
	if (hasAlt)
	{
		buttonAlign = "right";
	}	
	var bottom_holder = document.createElement("div");
	
	var button_holder = document.createElement("div");
	bottom_holder.appendChild(button_holder);
	button_holder.style.textAlign = "right";
	var button = document.createElement("div");
	if (this.model.inIframe)
	{
		
		button.style.margin = "0px 0px 0px 0px";
		button.style.height = "35px";
		button.style.fontSize = "18px";
		button.style.fontWeight = "bold";
		var buttonImg = document.createElement("img");
		button.appendChild(buttonImg);
		buttonImg.onclick = new Function("lcui.submitAddCardForm("+mid+");");
		buttonImg.style.padding = "0px 10px 0px 5px";
		if (doUpdate || (card.cardNum!="" && card.status==2))
		{
			buttonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/change_card.png";
			buttonImg.alt = "Change Card";
		}
		else
		{
			
			buttonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_addcard.png";
			buttonImg.alt = "Add Card";
		}
		buttonImg.style.cursor = "pointer";
		var removeImg = document.createElement("img");
	
		if (doUpdate)
		{
			var orText = document.createTextNode("OR");
			button.appendChild(orText);
			button.appendChild(removeImg);
			removeImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/remove_card.png";
			removeImg.onclick = new Function("lcui.removeCard("+mid+");");
			removeImg.style.margin = "0px 10px 0px 15px";
			removeImg.style.cursor = "pointer";
		}
	}
	else
	{
		if((card.cardNum!="" && card.status==2) && doUpdate && this.model.user.authenticated){
			button.style.margin = "0px 0px 0px 0px";
			button.style.height = "35px";
			button.style.fontSize = "18px";
			button.style.fontWeight = "bold";
			
			var buttonImg = document.createElement("img");
			button.appendChild(buttonImg);
			buttonImg.onclick = new Function("lcui.submitAddCardForm("+mid+");");
			buttonImg.style.padding = "0px 10px 0px 5px";
			buttonImg.src = "/images/home/change_card.png";
			buttonImg.alt = "Update Card";
			buttonImg.style.cursor = "pointer";
			
			var removeImg = document.createElement("img");
			var orText = document.createTextNode("OR");
			button.appendChild(orText);
			button.appendChild(removeImg);
			removeImg.src = "/images/home/remove_card.png";
			removeImg.onclick = new Function("lcui.removeCard("+mid+");");
			removeImg.style.margin = "0px 10px 0px 15px";
		}else if(card.cardNum!="" && card.status==2){
			button_holder.style.textAlign = "left";
			button_holder.style.height = "40px";
			var buttonImage = document.createElement("img");
			button_holder.appendChild(buttonImage);
			buttonImage.src = "/images/home/forget_cards.png";
			buttonImage.onclick = new Function("window.location=document.location.pathname + '?lo=';");
		}else{
			button_holder.appendChild(document.createElement("br"));
			button = getButton("blue","Add Card", 80, new Function("lcui.submitAddCardForm("+mid+");"));
		}
	}
	button.style.cssFloat = buttonAlign;
	button.style.styleFloat = buttonAlign;
	
	button_holder.appendChild(button);
	
	var clear = document.createElement("div");
	clear.className = "clear";
	button_holder.appendChild(clear);
	
	if (!doUpdate){
		var support = document.createElement("div");
		support.style.padding = "0px 0px 5px 0px";
		bottom_holder.appendChild(support);
		support.innerHTML = card.supportHTML;
	}
	body.appendChild(cardNumDiv);
	if (hasAlt)
	{
		body.appendChild(or_holder);
		body.appendChild(altNum_holder);
	}
	else
	{
		body.appendChild(noAlt_holder);
	}	
	
	var clear = document.createElement("div"); clear.className = "clear";
	body.appendChild(clear);
	
	cont.appendChild(top);
	cont.appendChild(body);
	cont.appendChild(bottom_holder);
	cont.appendChild(document.createTextNode('Please note that you may need to re-clip your coupons after updating'));
	var anotherClear = document.createElement("div");
	anotherClear.className = "clear";
	cont.appendChild(anotherClear);
//}
	return cont;
}

LoyaltyCardUI.prototype.showAddCardSuccessWindow = function(merchantId, offerCount){
	var card = this.model.loyaltyCards[merchantId];
	
	if(card.addCardSuccessWindowHandle){
		this.windowManager.showWindow(card.addCardSuccessWindowHandle);
		return;
	}
	
	var addCardSuccess = this.buildAddCardSuccess(merchantId, offerCount);
	card.addCardSuccessWindowHandle = this.windowManager.createNewBlackOutWindow(addCardSuccess, 350, 0, 0, true);
	
	this.windowManager.showWindow(card.addCardSuccessWindowHandle);
}

LoyaltyCardUI.prototype.buildAddCardSuccess = function(merchantId, offerCount){
	var card = this.model.loyaltyCards[merchantId];
	
	var success = document.createElement('div');
	success.id = "add_card_success_"+merchantId;

	var title = document.createElement("div");
	title.style.fontSize = "16px";
	title.style.color = "black";
	title.style.margin = "15px 0px 0px 0px";
	title.style.fontWeight = "bold";
	
	var titleText = "Saving Coupon to " +  card.cardName;
	var bodyText = "Your coupon is being saved to your " + card.merchantName + " " +  card.cardName + ". It will be available to redeem in about " + card.processTime + " minutes.";
	if (offerCount > 1)
	{
		titleText = "Saving Coupons to " +  card.cardName;
		bodyText = "Your coupons are being saved to your " + card.merchantName + " " +  card.cardName + ". They will be available to redeem in about " + card.processTime + " minutes."	;
	}
	if(offerCount==0){
		titleText = card.cardName + " Added";
		bodyText = "You can start saving offers to your " + card.merchantName + " " +  card.cardName + ". They will be available to redeem in about " + card.processTime + " minutes."	;
	}
	
	title.appendChild(document.createTextNode(titleText));
	success.appendChild(title);
	var footnote = document.createElement("div");
	if(offerCount){
		success.appendChild(document.createElement("br"));
		success.appendChild(document.createTextNode(bodyText + " Just use your " +  card.cardName + " at checkout to get your discount!"));
		success.appendChild(document.createElement("br"));
		success.appendChild(document.createElement("br"));
		footnote.appendChild(document.createTextNode("Click \"OK\" to continue saving coupons to your " +  card.cardName + "."));
	}
	// footnote.style.fontWeight = "bold";
	success.appendChild(footnote);
	
	var center = document.createElement("div");
	center.style.textAlign = "center";
	center.style.margin = "10px 0px";
	success.appendChild(center);
	
	var closeButton;
	if (this.model.inIframe)
	{
		closeButton = document.createElement("div");
		closeButton.style.height = "25px";
		var closeButtonImg = document.createElement("img");
		closeButtonImg.onclick = function(){wm.closeOpenWindow()};
		closeButtonImg.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_ok.png";
		closeButtonImg.style.cursor = "pointer";
		closeButtonImg.alt = "OK";
		closeButton.appendChild(closeButtonImg);
	}	
	else
	{
		var buttonOnClick = function(){wm.closeOpenWindow()};
		closeButton = getButton("blue","OK", 50, buttonOnClick);
		closeButton.style.margin = "auto";
	}	
	center.appendChild(closeButton);
	var cDiv = document.createElement("div");
	cDiv.className = "clear";
	center.appendChild(cDiv);
	
	return success;
}
LoyaltyCardUI.prototype.showRemoveCardSuccessWindow  = function(){
	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 ="Card removed!";
	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);
	
}
LoyaltyCardUI.prototype.successOKCallback = function(merchantId){
	var card = this.model.loyaltyCards[merchantId];
	this.windowManager.hideWindow(card.addCardSuccessWindowHandle);
}

LoyaltyCardUI.prototype.submitAddCardForm = function(merchantId){
	var offerIdInput = document.getElementById("add_card_offer_"+merchantId);
	var offerId = 0;
	if (offerIdInput && offerIdInput.value != 0)
	{
		offerId = offerIdInput.value;
	}	
	var doingAssoc = this.model.isOfferAssocOffer(offerId);
	
	var updateFlag = document.getElementById("update_card_" + merchantId);
	var doingUpdate = (updateFlag && updateFlag.value == "1");
	var error = document.getElementById("add_card_error_"+merchantId);
	error.className = "error";
	removeChildElements(error);
	
	var loader_img = document.createElement("img");
	//if(!loader_img) alert("loader image no good");
	loader_img.src="/includes/templates/master/images/loader.gif";
	error.appendChild(loader_img);
	if(lcui.validateAddCardForm(merchantId)){
		this.postAddCardForm(merchantId, offerId, doingAssoc, doingUpdate);
	}
	else{
		removeChildElements(error);
		error.appendChild(document.createTextNode("I'm sorry, that is an invalid card number. Please try again."));
	}
}
LoyaltyCardUI.prototype.removeCard = function(merchantId){
	var error = document.getElementById("add_card_error_"+merchantId);
	error.className = "error";
	removeChildElements(error);
	
	var loader_img = document.createElement("img");
	loader_img.src="/includes/templates/master/images/loader.gif";
	error.appendChild(loader_img);
	var notAddingOffer = true;
	var req = getXMLHttpObject();
	req.onreadystatechange = function(){
		if(req.readyState == 4){
			var callback = new Function("lcui.onRemoveCardCallback("+merchantId+","+req.responseText+")");
			callback();
		}
	}

	var openString = "/includes/form_handlers/loyalty.php?remove="+merchantId;

	req.open("GET",openString,true);
	req.send(null);
}
LoyaltyCardUI.prototype.createShoppingList = function(merchantId, isVZI){
	if(isVZI) shopListVzn='&vzn=1';
	else shopListVzn='';
	var main;
	main = document.getElementById("shopping_list_"+merchantId);
	if(main) return;;
		
	var holder = document.getElementById("manage_body_area");
	if (holder)
	{
		var main = document.createElement("div");
		main.id = "shopping_list_"+merchantId;
		holder.appendChild(main);
		main.className = "manage_element";
		var title = document.createElement("div");
		main.appendChild(title);
		title.calssName ="manage_title";
		title.innerHTML = "Create Shopping List";
		var imgHolder = document.createElement("div");
		main.appendChild(imgHolder);
		var email = document.createElement("img");
		var mobile = document.createElement("img");
		var print = document.createElement("img");
		imgHolder.appendChild(email);
		imgHolder.appendChild(mobile);
		imgHolder.appendChild(print);
		email.className = "shoppingList";
		mobile.className = "shoppingList";
		print.className = "shoppingList";
		email.src = "/images/home/icon_email.png";
		mobile.src = "/images/home/icon_mobile.png";
		print.src = "/images/home/icon_print.png";	
		email.onclick = function(){cui.showEmailShoppingList(merchantId); };
		mobile.onclick = function(){cui.mobilePopup();};
		print.onclick = new Function("window.open('/shopping_list.php?mId="+merchantId+"','shoppingListWindow','menubar=0,status=0,toolbar=0,location=0,height=800,width=800,scrollbars=1');wm.closeOpenWindow();");
	}
}
LoyaltyCardUI.prototype.cancelAddCardForm = function(merchantId){
	//var error = document.getElementById("add_card_error_"+merchantId);
	//removeChildElements(error);
	this.windowManager.hideWindow(this.model.loyaltyCards[merchantId].addCardFormWindowHandle);
}

LoyaltyCardUI.prototype.validateAddCardForm = function(merchantId){
	if(isNaN(document.getElementById("lc_number_"+merchantId).value)) {
		return false;
	}else {
		return true;
	}
}

LoyaltyCardUI.prototype.postAddCardForm = function(mid, offerId, doingAssoc, doingUpdate){
	if (!this.inSubmit)
	{
		customPageTracker('add_card_popup_attempt');
		var notAddingOffer = (offerId == 0);
		var req = getXMLHttpObject();
		req.onreadystatechange = function(){
			if(req.readyState == 4){
				var callback = new Function("lcui.onAddCardCallback("+mid+","+req.responseText+", " + notAddingOffer + ")");
				callback();
			}
		}

		var cardNum = document.getElementById("lc_number_"+mid).value;
		var altNum = document.getElementById("lc_alt_"+mid).value;
		var openString = "/includes/form_handlers/loyalty.php?mid="+mid+"&offer="+offerId+"&cnum="+cardNum+"&altnum="+altNum + "&doingAssoc=" + doingAssoc + "&doingUpdate=" + doingUpdate;

		req.open("GET",openString,true);
		req.send(null);
		this.inSubmit = true;
	}	
}
LoyaltyCardUI.prototype.onRemoveCardCallback = function(merchantId, code){
	this.inSubmit = false;
	var error = document.getElementById("add_card_error_"+merchantId);
	error.className = "error";
	removeChildElements(error);
	
	var card = this.model.loyaltyCards[merchantId]; //if(!card) alert("onAddCardCallback card not found"); 
		
	if(code == 0){ // Success
		/*
		this.model.loyaltyCards[merchantId].cardNum = "";
		this.model.loyaltyCards[merchantId].status = 0;
		var cardLink = document.getElementById("manage_card_update");
		if(cardLink) cardLink.style.display = "none";
		var shoppingList = document.getElementById("shopping_list_"+merchantId);
		if(shoppingList) shoppingList.style.display = "none";
		var saved = document.getElementById("manage_card_saved");
		if(saved) saved.innerHTML = "0";
		var saveValue = document.getElementById("manage_card_savings");
		if(saveValue) saveValue.innerHTML = "0";
		
		this.model.fireEvent("LoyaltyCardChange");
		*/
		this.showRemoveCardSuccessWindow();
		window.location=document.location.pathname;
	}	
	else
	{
		error.appendChild(document.createTextNode("We are currently having technical difficulties, please try again soon. Error("+code+")"));
	}
}

LoyaltyCardUI.prototype.onAddCardCallback = function(merchantId, code, dontShowOKBox){
	this.inSubmit = false;
	var error = document.getElementById("add_card_error_"+merchantId);
	if(error)
		removeChildElements(error);
	
	var card = this.model.loyaltyCards[merchantId]; //if(!card) alert("onAddCardCallback card not found"); 
	var pleaseEnter; 
	if (card && card.alternateTitle.length > 0)
	{
		pleaseEnter =  "lease enter the " + card.cardName + " number or the " + card.alternateTitle + " that is associated with your card.";
	}
	else
	{
		pleaseEnter =  "lease enter the " + card.cardName + " number that is associated with your card.";
	}	
	
	// alert("onAddCardCallback - merchantId = " + merchantId + " code = " + code + " dontShowOKBox = " + dontShowOKBox);
		
	if(code == 1){ // Success
		var newCard;
		var cardNumInput = document.getElementById("lc_number_"+merchantId); 
		var altNumInput = document.getElementById("lc_alt_"+merchantId);
		if(!altNumInput) {
			var altNum ="";
		}else{
			var altNum = altNumInput.value;
		}
		var offerId = 0;
		var of = document.getElementById("add_card_offer_"+merchantId);
		if(of) offerId = of.Value;
		if(this.model.user.authenticated){
			//if(this.model.inIframe){
			//var cardLink = document.getElementById("manage_card_update");
			//if(cardLink) cardLink.style.display = "block";	
			//}else{
			//	window.location=document.location.pathname;
			//}
			window.location=document.location.pathname;
		}
				
		if(!card.successCallback);// alert("addCardCallback card has no callback function");
		else card.successCallback();
		if(cardNumInput)
			this.model.updateLoyaltyCard(merchantId, cardNumInput.value, altNum, 2, offerId);
		if (!dontShowOKBox)
		{
			if(offerId)
				this.showAddCardSuccessWindow(merchantId, 1);
			else
				this.showAddCardSuccessWindow(merchantId, 0);
		}	
		else
		{
			this.windowManager.hideWindow(this.model.loyaltyCards[merchantId].addCardFormWindowHandle);
		}	
	}
	else if(code == 2){ // already in use
		customPageTracker('add_card_error_already_in_use');
		error.appendChild(document.createTextNode("Whoops! You've already added this card for a different grocer. To add it for this grocer, first remove the existing card. Or try using a different card number."));
		error.style.height = "50px";
	}
	else if(code == 3){ // Checksum failed
		customPageTracker('add_card_error_checksum');
		error.appendChild(document.createTextNode("Sorry, that is not a valid number, p" + pleaseEnter));
	}
	else if(code == 4){ // Empty fields
		customPageTracker('add_card_error_missing_values');
		error.appendChild(document.createTextNode("P" + pleaseEnter));
	}
	else if(code == -2){ // Remove failed 'cos offers are pending
		customPageTracker('add_card_error_update_remove_error');
		error.appendChild(document.createTextNode("You currently have coupons that are in the process of being saved to your card. Please wait a few minutes before trying to remove your card."));
	}	
	else
	{
		error.appendChild(document.createTextNode("We are currently having technical difficulties, please try again soon. Error("+code+")"));
	}
}

LoyaltyCardUI.prototype.showSavingsHistory = function(merchantId){
	var card = this.model.loyaltyCards[merchantId];
	if(!card || card.status != 2) return;
	
	if(card.savingsHistoryWindowHandle){
		this.windowManager.showWindow(card.savingsHistoryWindowHandle);
		return;
	}
	
	var saveHistory = this.buildSavingsHistory(merchantId);
	card.savingsHistoryWindowHandle = this.windowManager.createNewBlackOutWindow(saveHistory, 500, 0, 0, true);
	this.windowManager.showWindow(card.savingsHistoryWindowHandle);
}
LoyaltyCardUI.prototype.forgetCard  = function(){
	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 ="Forget All Savings Cards/Accounts";
	note.innerHTML ="Would you like to remove all your added savings cards/accounts?";
	content.appendChild(title);
	content.appendChild(note);
	var button_div = document.createElement("div");
	button_div.style.height = "25px";
	var forget = document.createElement("img");
	if (this.model.inMMF)
	{
		forget.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_forgetcards.png";
	}
	else
	{
		forget.src = "/images/home/button_forgetcards.png";
	}
	forget.alt = "Forget Cards";
	forget.style.cursor = "pointer";
	forget.onclick = new Function("window.location=document.location.pathname + '?lo=';");
	var cancel = document.createElement("img");
	if (this.model.inMMF)
	{
		cancel.src = "/includes/templates/affiliate/" + this.model.iframeCssId + "/button_cancel.png";
	}
	else
	{
		cancel.src = "/images/home/button_cancel.png";
	}
	cancel.style.padding = "0px 15px";
	cancel.style.cursor = "pointer";
	cancel.alt = "Cancel button";	
	cancel.onclick = new Function("wm.closeOpenWindow();");
	button_div.appendChild(forget);
	button_div.appendChild(cancel);
	content.appendChild(button_div);
	if(this.model.inIframe)//in qframe
		var handle = this.windowManager.createNewWhiteOutWindow(content, 380, 0, true);
	else
		var handle = this.windowManager.createNewBlackOutWindow(content, 380, 0, 0, true);
	this.windowManager.showWindow(handle);
}
LoyaltyCardUI.prototype.buildSavingsHistory = function(merchantId){
	var holder = document.createElement('div');
	holder.style.width = "100%";
	holder.style.position = "relative";
	
	var card = this.model.loyaltyCards[merchantId];
	if(!card) return holder;
	
	var content = document.createElement('div');
	content.style.padding = "10px";
	//content.style.position = "relative";
	content.style.display = "inline-block";
	holder.appendChild(content);
	
	var title = document.createElement('div');
	title.style.position = "relative";
	title.style.width = "100%";
	title.style.borderBottom = "1px solid #666666";
	//title.style.height = "30px";
	//title.style.top = "0";
	//title.style.left = "0";
	title.style.fontSize = "32px";
	content.appendChild(title);
	
	var logo = document.createElement('img');
	logo.src = "/images/"+card.frontImageId+".jpg";
	logo.style.width = "60px";
	logo.style.height = "36px";
	logo.style.margin = "0px 3px 0px 0px";
	title.appendChild(logo);

	/*
	var titleBack = document.createElement('div');
	titleBack.className = "locationLogoBack";
	//title.appendChild(titleBack);
	*/
	
	var titleText = document.createElement('div');
	title.appendChild(document.createTextNode(card.merchantName+" "+card.cardName));
	titleText.style.position = "relative";
	titleText.style.zIndex = "100";
	titleText.style.display = "inline-block";
	//title.appendChild(titleText);
	
	var table = document.createElement('table');
	table.style.width = "100%";
	content.appendChild(table);
	
	var row = document.createElement('tr');
	table.appendChild(row);
	var date = document.createElement('th');
	date.appendChild(document.createTextNode("Date"));
	row.appendChild(date);
	var description = document.createElement('th');
	description.appendChild(document.createTextNode("Description"));
	row.appendChild(description);
	var savings = document.createElement('th');
	savings.appendChild(document.createTextNode("Savings"));
	row.appendChild(savings);
	
	for(var i in card.history){
		var trans = card.history[i];
		if(!trans) continue;
		var row = document.createElement('tr');
		table.appendChild(row);
		var date = document.createElement('td');
		date.appendChild(document.createTextNode(trans.transactionTime));
		row.appendChild(date);
		var description = document.createElement('td');
		description.appendChild(document.createTextNode(trans.couponName));
		row.appendChild(description);
		var savings = document.createElement('td');
		savings.appendChild(document.createTextNode("$"+trans.saving));
		row.appendChild(savings);
	}
	
	return holder;
}

