function initSwitchAccountPopUp()
{
	$('#switchAccountWindow').dialog({ bgiframe: true,modal: true, autoOpen: false, title: 'Select Account', width:570, 
	buttons: {
		"Select": function() {
				document.forms["frmAccountsPopUp"].submit();		
		},	
		"Cancel": function() {
			$(this).dialog('close');
		}
	}
	});
}

function showSwitchAccountPopUp()
{
	$('#switchAccountWindow').dialog('open');
}



function initMyLists(id)
{	
	$.ajax({
		type:"POST",
		url: "AjaxFuncs.asp",
		dataType: "application/x-www-form-urlencoded",
		data: "action=showMyLists&id="+id,
			async: false,
			success: function(msg){document.getElementById("productDetailsSection").innerHTML = msg;}
	})
	$('#productDetailsSection').dialog({ bgiframe: true,modal: true, autoOpen: false, title: 'Add ' + id + ' To My Lists', width:630, 
	buttons: {
		"Add To List": function() {
			if (document.getElementById("txtListNameNew")) {
				if ((!document.getElementById("txtListNameExisting"))&&(document.getElementById("txtListNameNew").value=="")){
					alert("Please specify name of a new list");
				}else{
					var listnew = document.getElementById("txtListNameNew").value;
					var listexisting='';
					if (document.getElementById("txtListNameExisting")){
						listexisting = document.getElementById("txtListNameExisting").value;
					}
					var qty=document.getElementById("txtMyListQty").options[document.getElementById("txtMyListQty").selectedIndex].value;
					var um=document.getElementById("txtMyListUM").options[document.getElementById("txtMyListUM").selectedIndex].value;
					if ((parseInt(qty)>0)&&(document.getElementById("txtMyListUM").selectedIndex==0)){
						alert("Please select unit of measure for selected quantity");
						document.getElementById("txtMyListUM").focus();
					}else{
						$.ajax({
							type:"POST",
							url: "AjaxFuncs.asp",
							dataType: "application/x-www-form-urlencoded",
							data: "action=addProductToList&id="+id+"&listnew="+encodeURIComponent(listnew)+"&listexisting="+encodeURIComponent(listexisting)+"&qty="+encodeURIComponent(qty)+"&um="+encodeURIComponent(um),
							async: false,
							error: function(){
								alert('Error tring to save list.');
							},
							success: function(msg){document.getElementById("productDetailsSection").innerHTML = msg;
							if (msg.indexOf("Congratulations")>0){
								$(":button:contains('Add To List')").hide();
							}
							}
						})
					}
				}
			}
			else
			{
				$(":button:contains('Add To List')").hide();
				$(":button:contains('Close')").show();
			}
		},
		"Close": function() {
			$(this).dialog('close');
			$(this).dialog("destroy"); 
		
		}
	},
	close: function(){
		$(this).dialog("destroy"); 
	}
	});
}

function showMyLists()
{
	$(":button:contains('Add To List')").show();
	$(":button:contains('Close')").show();
	$('#productDetailsSection').dialog('open');
}

function initNotEmptyOrderPopup()
{
	$('#notEmptyOrder').dialog({ bgiframe: true,modal: true, autoOpen: false, title: 'Your order is not empty', width:570, 
	buttons: {
		"Cancel": function() {
			$(this).dialog('close');
			$(this).dialog("destroy"); 		
		},
		"Add to existent items": function() {
			document.getElementById('frmResults').submit();		
		},	
		"Clean order before adding": function() {
			$.ajax({
				type:"POST",
				url: "AjaxFuncs3.asp",
				dataType: "application/x-www-form-urlencoded",
				data: "action=cleanCurrentOrder",
				async: false,
				success: function(msg){
					 document.getElementById('frmResults').submit();
				}
			})			
		}
	}
	});
}

function showNotEmptyOrderPopup()
{
	$('#notEmptyOrder').dialog('open');
}

function initEditDefaultQtyUMPopup(listname, prodSku, index)
{
	$.ajax({
		type:"POST",
		url: "AjaxFuncs3.asp",
		dataType: "application/x-www-form-urlencoded",
		data: "action=getDefaulQtyUM&listname="+encodeURIComponent(listname)+"&prodSku="+encodeURIComponent(prodSku),
			async: false,
			success: function(msg){document.getElementById("editDefaultQtyUM").innerHTML = msg;}
	})
	$('#editDefaultQtyUM').dialog({ bgiframe: true,modal: true, autoOpen: false, title: 'Edit default values', width:570, 
	buttons: {
		"Save": function() {
			var qtyIndex = parseInt(document.getElementById("txtMyListQty").selectedIndex);
			var umIndex = parseInt(document.getElementById("txtMyListUM").selectedIndex);
			var qty=document.getElementById("txtMyListQty").options[document.getElementById("txtMyListQty").selectedIndex].value;
			var um=document.getElementById("txtMyListUM").options[document.getElementById("txtMyListUM").selectedIndex].value;
			$(this).dialog('close');
			$(this).dialog("destroy");
			$.ajax({
				type:"POST",
				url: "AjaxFuncs3.asp",
				dataType: "application/x-www-form-urlencoded",
				data: "action=saveDefaultQtyUM&listname="+encodeURIComponent(listname)+"&prodSku="+encodeURIComponent(prodSku)+"&qty="+encodeURIComponent(qty)+"&um="+encodeURIComponent(um),
				async: false,
				success: function(msg){   					  
					 document.getElementById("txtQty"+index).selectedIndex = qtyIndex;
					 document.getElementById("txtUM"+index).selectedIndex = umIndex;
				}
			})		
		},	
		"Cancel": function() {
			$(this).dialog('close');
			$(this).dialog("destroy"); 		
		}
	},
	close: function(){
		$(this).dialog("destroy"); 
	}
	});
}

function showEditDefaultQtyUMPopup()
{
	$('#editDefaultQtyUM').dialog('open');
}

function initMakeShoppingListPopup()
{
	$.ajax({
		type:"POST",
		url: "AjaxFuncs3.asp",
		dataType: "application/x-www-form-urlencoded",
		data: "action=showMakeShoppingListPopup",
			async: false,
			success: function(msg){
				document.getElementById("makeShoppingList").innerHTML = msg; 				
			}
	})
	$('#makeShoppingList').dialog({ bgiframe: true,modal: true, autoOpen: false, title: 'Make Shopping List', width:570, 
	buttons: {
		"Save": function() {	
			var listname = document.getElementById("txtListNameNew").value;	
			if (listname==""){
					alert("Please specify name of a new list");
			}else{
				$.ajax({
					type:"POST",
					url: "AjaxFuncs3.asp",
					dataType: "application/x-www-form-urlencoded",
					data: "action=makeShoppingListFromCart&listname="+encodeURIComponent(listname),
					async: false,
					error: function(){
						alert('Error trying to save list.');
					},
					success: function(msg){document.getElementById("makeShoppingList").innerHTML = msg;
					if (msg.indexOf("Congratulations")>0){
						$(":button:contains('Save')").hide();
					}
					}
				})
			}						
		},	
		"Close": function() {
			$(this).dialog('close');
			$(this).dialog("destroy"); 	
		}
	},
	close: function(){
		$(this).dialog("destroy"); 
	}
	});
}

function showMakeShoppingListPopup()
{
	$(":button:contains('Save')").show();
	$('#makeShoppingList').dialog('open');
	if (document.getElementById("txtListNameNew")){
		document.getElementById("txtListNameNew").focus();
	}
}




