//script för kundvagn

function add_to_basket(productid)
{	
	var pid = productid;
	var action = "add";
	var amount = 1;
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{
		
		if(xmlhttp.readyState == 4)
		{	
			
			if(xmlhttp.status == 200)
			{
				
				var res;
				
				res = xmlhttp.responseText;
				location.reload();
			}

		}

	}	
	
	xmlhttp.open("GET","../basket.php?productid=" + pid + "&call=" + action + "&amount=1&dummy=" + new Date().getTime(),true);
	xmlhttp.send(null);
		

	/* Slut på AJAX-block */	

}

function increaseBasket(productid)
{

	var pid = productid;
	var action = "increase";
	var amount = document.getElementById("increase" + productid).value;
	
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{

		if(xmlhttp.readyState == 4)
		{
			location.reload();
		}
			
	};	
	
	xmlhttp.open("GET","../basket.php?productid=" + productid + "&call=" + action + "&amount=" + amount,true);
	xmlhttp.send(null);

	/* Slut på AJAX-block */	

}

function deleteFromBasket(productid)
{

	var pid = productid;
	var action = "delete";
	var amount = document.getElementById("delete" + productid).value;
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}

	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			var res = xmlhttp.responseText;
			
			if(res != "")
			{
				alert(res);
					
			}
			location.reload();
		}
	
	};	
	
	
	xmlhttp.open("GET","../basket.php?productid=" + productid + "&call=" + action + "&amount=" + amount,true);
	xmlhttp.send(null);
	

	/* Slut på AJAX-block */	

}

//Nya funktioner för att ha en annan sökväg på show_avdelning, kassa och varukorg

function add_to_basket_2(productid)
{	
	var pid = productid;
	var action = "add";
	var amount = 1;
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{
		
		if(xmlhttp.readyState == 4)
		{	
			
			if(xmlhttp.status == 200)
			{
				
				var res;
				
				res = xmlhttp.responseText;
				location.reload();
			}

		}

	}	
	
	xmlhttp.open("GET","./basket.php?productid=" + pid + "&call=" + action + "&amount=1&dummy=" + new Date().getTime(),true);
	xmlhttp.send(null);
		

	/* Slut på AJAX-block */	

}

function increaseBasket_2(productid)
{

	var pid = productid;
	var action = "increase";
	var amount = document.getElementById("increase" + productid).value;
	
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{

		if(xmlhttp.readyState == 4)
		{
			location.reload();
		}
			
	};	
	
	xmlhttp.open("GET","./basket.php?productid=" + productid + "&call=" + action + "&amount=" + amount,true);
	xmlhttp.send(null);

	/* Slut på AJAX-block */	

}

function deleteFromBasket_2(productid)
{

	var pid = productid;
	var action = "delete";
	var amount = document.getElementById("delete" + productid).value;
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}

	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			var res = xmlhttp.responseText;
			
			if(res != "")
			{
				alert(res);
					
			}
			location.reload();
		}
	
	};	
	
	
	xmlhttp.open("GET","./basket.php?productid=" + productid + "&call=" + action + "&amount=" + amount,true);
	xmlhttp.send(null);
	

	/* Slut på AJAX-block */	

}

function send_payment_form(form,paymentmethod)
{
	
	var pnr_orgnr = form.pnr_orgnr.value;
	var fn = form.firstname.value;
	var ln = form.lastname.value;
	var address_1 = form.address_1.value;
	var address_2 = form.address_2.value;
	var pc = form.postalcode.value;
	var city = form.city.value;
	var country = form.country.value;
	var phone_home = form.phone_home.value;
	var phone_work = form.phone_work.value;
	var mobile = form.phone_mobile.value;
	var email = form.email.value;
	
	/*if(pnr_orgnr == "" || pnr_orgnr.length < 10)
	{
		alert("Var god fyll i ditt personnr eller orgnr");
		
		return false;
		
	}*/	
	if(fn == "")
	{
		alert("Var god fyll i ditt förnamn");
		
		return false;
		
	}
	else if(ln == "")
	{
		alert("Var god fyll i ditt efternamn");
		
		return false;
		
	}
	else if(address_1 == "")
	{
		alert("Var god fyll i din gatuadress");
		
		return false;
		
	}
	else if(pc == "")
	{
		alert("Var god fyll i ditt postnummer");
		
		return false;
	}
	else if(city == "")
	{
		alert("Var god fyll i din postort");
		
		return false;
		
	}
	else if(phone_home == "" && mobile == "")
	{
		alert("Var god fyll i telefonnummer eller mobilnummer");
		
		return false;
		
	}
	else if(email == "")
	{
		alert("Var god fyll i din e-mailadress");
		
		return false;
		
	}
	else
	{
		if(paymentmethod == "card")
		{	
			form.action = "./payment.php";
			form.payment_method.value = paymentmethod;
			form.submitted.value = "yes";
		
		}
		if(paymentmethod == "invoice")
		{
			form.action = "./payment.php";
			form.payment_method.value = paymentmethod;
			form.submitted.value = "yes";
		}
		if(paymentmethod == "bank")
		{
			form.action = "./payment.php";
			form.payment_method.value = paymentmethod;
			form.submitted.value = "yes";
		}	
	
		form.submit();
		
	}
	
}

//Kontrollerar landskoden för att bestämma om kund kan beställa mot faktura

function checkCountryCode(cc)
{
	
	if(cc == "SE" || cc == "NO")
	{
	
		document.checkout_form.paymethodBtnFaktura.disabled = false;

	}
	else
	{
	
		document.checkout_form.paymethodBtnFaktura.disabled = true;
	}
}
