
	var callbackAddToList = function(message) {
		alert(message);
		analyticsLog("AddToList");
	}

    var callbackAddToListNew = function(message, listOid, listName) {
        alert(message);
        top.mainContentFrameName.addNewList(listOid, listName);
    	analyticsLog("AddToList");
    }


	var promptObj = function(oid, name, pIsList, pIsBasket, pIsSearch, pIsIphoneCart) {
		this.oid = oid;
		this.name = name;
		this.isList = pIsList;
		this.isBasket = pIsBasket;
		this.isSearch = pIsSearch;
		this.isIphoneCart = pIsIphoneCart;
		this.busy = false;
	}

	promptObj.prototype.changeText = function(/*boolean*/ textRename, /*String*/ newText) {
		if (textRename) {
			document.getElementById('renameListTitleName').innerHTML=newText;
			document.getElementById('renameListTitle').style.display='block';
			document.getElementById('newListTitle').style.display='none';
			document.getElementById('searchKey').style.display='none';
		} else if (!this.isSearch) {
			document.getElementById('newListTitle').style.display='block';
			document.getElementById('renameListTitle').style.display='none';
			document.getElementById('searchKey').style.display='none';
		} else if (this.isSearch) {
			document.getElementById('newListTitle').style.display='none';
			document.getElementById('renameListTitle').style.display='none';
			document.getElementById('searchKey').style.display='block';
		}
	}

    promptObj.prototype.showPrompt = function() {
    	if (this.isIphoneCart) {
    		document.getElementById('textQuestion').style.display='none';
    		document.getElementById('choiceQuestion').style.display='block';
    	} else {
    		document.getElementById('choiceQuestion').style.display='none';
    		document.getElementById('textQuestion').style.display='block';
    		this.changeText(this.isList, this.name);
    	}
		display("fullscreen", "block");
		if (this.isSearch) {
			$("#listNameText001").val( $("#enter").val() );
		}
		else {
			$("#listNameText001").val("");
		}
		if (!this.isIphoneCart) {
			document.getElementById("listNameText001").focus();
		}
		this.busy = true;
	}

	promptObj.prototype.validatePrompt = function(buttonPressed) {
		this.replyOk = false;
		if ($("#listNameText001").val() != undefined && $("#listNameText001").val() != "" && buttonPressed == 'submit') {
			if (!this.isList && this.isBasket && !this.isSearch) {
				top.basketTotalFrameName.document.getElementById('encadre').style.visibility='hidden';
				top.basketTotalFrameName.document.getElementById('saveAsWait').style.height=top.basketTotalFrameName.openedTotal.offsetHeight+'px';
				top.basketTotalFrameName.document.getElementById('saveAsWait').style.display='block';
				$.post("/leshop/lists.do",
						{ action: "saveBasketAsList" , listName : $("#listNameText001").val() },
						function(jsonObject) {
							var message = jsonObject.saveBasketAsListResult;
							callbackSaveBasketAsList(message);
						},
						"json");
			} else if (this.isList && !this.isBasket && !this.isSearch) {
				top.mainContentFrameName.document.location.href='/leshop/List.do?strListOid=' + this.oid + '&ListNewName=' + $("#listNameText001").val();
			} else if (!this.isList && !this.isBasket && !this.isSearch) {
				$.post("/leshop/lists.do",
						{ action: "addProductToList", 
							prodOid : this.oid, 
							productName : this.name, 
							listOid : -1, 
							listName : $("#listNameText001").val(),
							isNewList : true},
						function(jsonObject) {
							callbackAddToListNew(jsonObject.addProductMessageResult, jsonObject.addProductListOidResult, jsonObject.addProductListNameResult);
						},
						"json");
				/*$.ajax({
					type: 'POST',
					url: '/leshop/lists.do',
					data: { action: "addProductToList", 
							prodOid : this.oid, 
							productName : this.name, 
							listOid : -1, 
							listName : $("#listNameText001").val(),
							isNewList : true},
					dataType: 'json',
					success: function(jsonObject) {
								callbackAddToListNew(jsonObject.addProductMessageResult, jsonObject.addProductListOidResult, jsonObject.addProductListNameResult);
							},
					error: function (request, status, error) {
								alert(request + status + "");
							}
					});*/
			} else if (!this.isList && !this.isBasket && this.isSearch) {
				top.mainContentFrameName.document.location.href='/leshop/Search.do?strSearchString=' + $("#listNameText001").val();
				$("#enter").val( $("#listNameText001").val() );
			}
		} else if (!this.isList && !this.isBasket && this.isSearch) {
			hideMainLoading();
		} else {
			// do nothing
		}
		$("#listNameText001").val("");
		display("fullscreen", "none");
		this.busy = false;
	}

	promptObj.prototype.checkKey = function (e){
		var characterCode = 0;
		if (window.event && window.event.keyCode) {
			characterCode = window.event.keyCode; //IE
		} else if (e && e.keyCode) {
			characterCode = e.keyCode; //FF
		}
		if (characterCode != 0) {
			if(characterCode == 13){
				this.validatePrompt('submit');
				return false;
			} else if (characterCode == 27) {
				this.validatePrompt('cancel');
				return false;
			}
		}
		return true;
	}

    promptObj.prototype.validateChoice = function(buttonPressed) {
    	//var basketQueryString = top.basketListFrameName.document.location.search;
    	var basketUrl = top.basketListFrameName.document.location.href.split("?")[0];
    	switch (buttonPressed) {
    		case 'persistentCart':
    			top.basketListFrameName.document.location.href = basketUrl + '?action=getFromPersistentCart';
    			break;
    		case 'cookie':
    			top.basketListFrameName.document.location.href = basketUrl + '?action=getFromCookie';
    			break;
    		default:
    			break;
    	}
    	
		display("fullscreen", "none");
		this.busy = false;
    	
    }
    

	var promptDialog = null;

	function addToNewList(productoid, productname) {
		promptDialog = new promptObj(productoid, productname, false, false, false, false);
		promptDialog.showPrompt();
	}

	function addToList(productoid, productname, listoid, listname) {
		$.post("/leshop/lists.do",
			{ action: "addProductToList", 
				prodOid : productoid, 
				productName : productname, 
				listOid : listoid, 
				listName : listname,
				isNewList : false},
			function(jsonObject) {
					callbackAddToList(jsonObject.addProductMessageResult);
			},
			"json");
	}

	// needs to be called from the iframe !
	var callbackGenerateLists = function(data) {
		$("#genericListContainer").html(data);
		genericListContainer = document.getElementById("genericListContainer");
		genericList = genericListContainer.getElementsByTagName('ul')[0];
		setGenericList();
		setListsEvents();
	}

	var callbackSaveBasketAsList = function(data) {
		top.basketTotalFrameName.document.getElementById('saveAsWait').style.display='none';
		top.basketTotalFrameName.document.getElementById('encadre').style.visibility='visible';
		alert(data);
		analyticsLog("SaveBasketAsList");
	}

	var callbackDaysWithRecipes = function(data) {
		if (data != "") {
			data = data.substr(0,data.length-1).split(",");
			var dayData = /(\d+):(\d+)$/;
			var response = new Array();
			for (var i=0; i<data.length; i++) {
				dayData.exec(data[i]);
				response["D"+RegExp.$1] = RegExp.$2;
			}
			recipes_calendar.updateValidDays(response);
		}
		else recipes_calendar.release();
	}

	function saveBasketAsList() {
		promptDialog = new promptObj(0,'', false, true, false, false);
		promptDialog.showPrompt();
	}

	function renameList(listoid, listname) {
		promptDialog = new promptObj (listoid, listname, true, false, false, false);
		promptDialog.showPrompt();
	}

	function search0() {
		promptDialog = new promptObj (0, '', false, false, true, false);
		promptDialog.showPrompt();
	}
	
	function chooseCart() {
		promptDialog = new promptObj (0, '', false, false, false, true);
		promptDialog.showPrompt();
	}

	function excludeFavorite(oid, row) {
		$.post("/leshop/generalAction.do",
			{action : "excludeFavorite", oid : oid},
			function(data) {
				row.fadeOut("slow", function() {
					// if row is tr and prev sibling contains has friend then it is favorite's suggestion and we need to remove it
					if (row.is('tr')) {
						var hasFriend = row.prev().find('div.hasFriend');
						if (hasFriend.length == 1) {
							row.prev().hide();
						}						
					}
				});
			},
			"json"
		);
	}



