// General functions //

function NewWindow(file, name, width, height)
{	
	return window.open(file, name, 'width='+width+',height='+height+',resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
}

function ShowMap(lang, city)
{
	//var mapWindow = NewWindow('images/map.jpg', 'Map', 586, 407);
	var mapWindow;
	if (city == 'cph')
	{
		mapWindow = NewWindow('http://maps.google.com/maps?source=ig&hl='+lang+'&q=Kunst+og+K%C3%B8kkent%C3%B8j&ie=UTF8&sll=56.124220,11.018018&sspn=1.815103,3.129065&ei=j04fSp7pHeaOjAe_ucn8Bg&cd=1&cid=55680175,12582550,3459213161331010311&li=lmd', 'Map', 980, 700);
	}
	else if (city = 'aarhus')
	{
		mapWindow = NewWindow('http://maps.google.com/maps?source=ig&hl='+lang+'&geocode=&q=Kunst+og+K%C3%B8kkent%C3%B8j,+%C3%85rhus&sll=56.156623,10.206615&sspn=0.009775,0.018947&ie=UTF8&hq=Kunst+og+K%C3%B8kkent%C3%B8j,&hnear=Aarhus,+Denmark&z=16&iwloc=A', 'Map', 980, 700);
	}
	else
	{
		return;
	}
	if (mapWindow) mapWindow.focus();
}

function ShowConditions(lang, arg)
{
	var conditionsWindow = NewWindow('conditionsPopup.php?lang='+lang+arg, 'Conditions', 700, 700);
	if (conditionsWindow) conditionsWindow.focus();
}

function ShowLostPassword(lang)
{
	var lostPasswordWindow = NewWindow('lostpassword.php?lang='+lang, 'LostPassword', 550, 150);
	if (lostPasswordWindow) lostPasswordWindow.focus();
}

function ChangeCustomerDetails(lang)
{
	if (window.opener)
	{
		window.opener.document.location = 'account.php?lang='+lang+'&action=edit';
		window.opener.focus();
	}
}



// Items functions //

function ShowDetailsWithList(kaldeId1, kaldeId2, kaldeId3, kundenr, vognnr)
{
	var detailsWindow = NewWindow('details.php?kaldeId1='+kaldeId1+'&kaldeId2='+kaldeId2+'&kaldeId3='+kaldeId3+'&kundenr='+kundenr+'&vognnr='+vognnr, 'details_'+kaldeId1+'_'+kaldeId2+'_'+kaldeId3, 580, 680);
	if (detailsWindow) detailsWindow.focus();
}

function ShowDetails(kaldeId1, kaldeId2, kaldeId3)
{
	ShowDetailsWithList(kaldeId1, kaldeId2, kaldeId3, 0, 0)
}



// Basket functions //

function ShowBasket(params)
{
	var basketWindow = NewWindow('basket.php?'+params, 'Basket', 700, 700);
	if (basketWindow) basketWindow.focus();
}

function UpdateBasket(submitform)
{
	var basketWindow = NewWindow('basket.php?action=show', 'Basket', 700, 700);
	if (basketWindow) basketWindow.focus();
	submitform.submit();
}

function SaveRecipient()
{
	document.frm
}

function DeleteEntireOrder(lang)
{
	if (confirm(lang == 'uk' ? "Are you sure that you want to delete the entire order?" : "Er du sikker på at du vil slette hele ordren?") == true)
	{
		document.location = 'basket.php?lang='+lang+'&action=deleteAll';
	}
}

function GoToCheckout(lang, authed, anyItemsInBasket)
{
	if (authed)
	{
		if (anyItemsInBasket)
		{
			document.frmLevering.action.value = "checkout";
			document.frmLevering.submit();
		}
		else
		{
			alert(lang == 'uk' ? 'You have no items in your shopping cart!' : 'Du har ingen varer i indkøbsvognen!');
		}
	}
	else
	{
		alert(lang == 'uk' ? 'You have to login in order to proceed.' : 'Du er nødt til at logge ind for at fortsætte.');
	}
}

function CheckNumber(num, minLength)
{
	if (num.length < minLength)
		return false; // Number too short

	var validChars = "0123456789";
	for (i = 0; i < num.length; i++)
	{
		if (validChars.indexOf(num.charAt(i)) == -1)
			return false; // Number contained invalid char
	}

	return true; // Number appears to be valid
}

function Checkout(lang)
{
	if (document.getElementById('handelsbetingelser').checked)
	{
		if (document.getElementById('betaling_kort').checked)
		{
			// Betaling med kort - Lav validering
			if (CheckNumber(document.getElementById('cardno').value, 12))
			{
				// Kortnummer er gyldigt - Check CVC
				if (CheckNumber(document.getElementById('cvc').value, 3))
				{
					// CVC også gyldigt - Disable knap og submit info til betalingsgateway
					document.ePay.cardSubmit.disabled = true;
					document.ePay.cardSubmit.className = "grey";
					return true;
				}
				else
				{
					alert(lang == "uk" ? "The given card verification code (CVC) appears to be invalid!" : "Den angivne kontrolkode (CVC) ser ud til at være ugyldig!");
					return false;
				}
			}
			else
			{
				alert(lang == "uk" ? "The given card number appears to be invalid!" : "Det angivne kortnummer ser ud til at være ugyldigt!");
				return false;
			}
		}
		else if (document.getElementById('betaling_faktura').checked)
		{
			// Betaling med faktura - Disable knap og submit info
			document.frmInvoice.invoiceSubmit.disabled = true;
			document.frmInvoice.invoiceSubmit.className = "grey";
			return true;
		}
		else
		{
			// Fejl
			alert('Din betaling kan ikke gennemføres. Kontakt venligst Kunst og Køkkentøj.');
			return false;
		}
	}
	else
	{
		alert(lang == "uk" ? "You must accept our conditions of use to continue!" : "Du skal acceptere handelsbetingelserne for at fortsætte!");
		return false;
	}
}


// Wish list functions //

function copyWishList(lang)
{
	if (confirm(lang == 'uk' ? "Are you sure you want to copy the wish list to your shopping cart? This will remove the items that are currently in the cart." : "Er du sikker på at du vil kopiere hele ønskelisten til din indkøbsvogn? Dette vil fjerne alle de varer du allerede har i vognen.") == true)
	{
		document.frmCopyList.submit();
	}
}

function deleteWishList(lang)
{
	if (confirm(lang == 'uk' ? "Are you sure you want to delete this wish list?" : "Er du sikker på at du vil slette denne ønskeliste?") == true)
	{
		document.frmDeleteList.submit();
	}
}

